From 29f2da3630a83ecb6d7f69066bf9092a193a4f1e Mon Sep 17 00:00:00 2001 From: Diego Caravana Date: Thu, 21 Jan 2016 01:55:26 +0100 Subject: [PATCH 1/3] added package_folder option to lambda_invoke task --- tasks/lambda_invoke.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/tasks/lambda_invoke.js b/tasks/lambda_invoke.js index 18ab6a3..8dbd438 100644 --- a/tasks/lambda_invoke.js +++ b/tasks/lambda_invoke.js @@ -12,6 +12,7 @@ module.exports = function (grunt) { var path = require('path'); var fs = require('fs'); + var process = require('process'); // Please see the Grunt documentation for more information regarding task @@ -20,6 +21,7 @@ module.exports = function (grunt) { grunt.registerMultiTask('lambda_invoke', 'Invokes a lambda function for testing purposes', function () { var options = this.options({ + 'package_folder': './', 'handler': 'handler', 'file_name': 'index.js', 'event': 'event.json' @@ -57,10 +59,19 @@ module.exports = function (grunt) { identity: null }; + var cwd; + if(options.package_folder) { + cwd = process.cwd(); + process.chdir(path.resolve(options.package_folder)); + } + var lambda = require(path.resolve(options.file_name)); var event = JSON.parse(fs.readFileSync(path.resolve(options.event), "utf8")); lambda[options.handler](event, context); - + + if(cwd) { + process.chdir(cwd); + } }); }; From 232e611f1b0c3e5438feb21b378a84b5b1e15c43 Mon Sep 17 00:00:00 2001 From: Diego Caravana Date: Sun, 24 Jan 2016 00:46:13 +0100 Subject: [PATCH 2/3] removed require('process') since process is always available --- tasks/lambda_invoke.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/tasks/lambda_invoke.js b/tasks/lambda_invoke.js index 8dbd438..eb9a039 100644 --- a/tasks/lambda_invoke.js +++ b/tasks/lambda_invoke.js @@ -12,8 +12,6 @@ module.exports = function (grunt) { var path = require('path'); var fs = require('fs'); - var process = require('process'); - // Please see the Grunt documentation for more information regarding task // creation: http://gruntjs.com/creating-tasks From e7d2f3a828ef7d0b80256460a28b95638f73308e Mon Sep 17 00:00:00 2001 From: Diego Caravana Date: Sun, 24 Jan 2016 01:53:16 +0100 Subject: [PATCH 3/3] added test for lambda_invoke package_folder option --- Gruntfile.js | 7 ++++++ test/fixtures/package_folder_option/.test | 1 + test/fixtures/package_folder_option/index.js | 3 +++ .../package_folder_option/package.json | 12 +++++++++ test/lambda_invoke_test.js | 25 +++++++++++++++++++ 5 files changed, 48 insertions(+) create mode 100644 test/fixtures/package_folder_option/.test create mode 100644 test/fixtures/package_folder_option/index.js create mode 100644 test/fixtures/package_folder_option/package.json diff --git a/Gruntfile.js b/Gruntfile.js index 8186e73..4acbbcd 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -49,6 +49,13 @@ module.exports = function (grunt) { event: 'test/fixtures/event.json', handler: 'myfunction' } + }, + package_folder_options: { + options: { + package_folder: 'test/fixtures/package_folder_option', + file_name: 'index.js', + event: '../../../test/fixtures/event.json' + } } }, lambda_package: { diff --git a/test/fixtures/package_folder_option/.test b/test/fixtures/package_folder_option/.test new file mode 100644 index 0000000..5e1c309 --- /dev/null +++ b/test/fixtures/package_folder_option/.test @@ -0,0 +1 @@ +Hello World \ No newline at end of file diff --git a/test/fixtures/package_folder_option/index.js b/test/fixtures/package_folder_option/index.js new file mode 100644 index 0000000..85cabf3 --- /dev/null +++ b/test/fixtures/package_folder_option/index.js @@ -0,0 +1,3 @@ +exports.handler = function (event, context) { + context.done(null, process.cwd()); +}; diff --git a/test/fixtures/package_folder_option/package.json b/test/fixtures/package_folder_option/package.json new file mode 100644 index 0000000..07bac12 --- /dev/null +++ b/test/fixtures/package_folder_option/package.json @@ -0,0 +1,12 @@ +{ + "name": "my-lambda-function", + "description": "An Example Lamda Function", + "version": "0.0.1", + "private": "true", + "dependencies": { + }, + "devDependencies": { + }, + "bundledDependencies": [ + ] +} \ No newline at end of file diff --git a/test/lambda_invoke_test.js b/test/lambda_invoke_test.js index b16fa97..9261d97 100644 --- a/test/lambda_invoke_test.js +++ b/test/lambda_invoke_test.js @@ -1,6 +1,7 @@ 'use strict'; var grunt = require('grunt'); +var path = require('path'); /* ======== A Handy Little Nodeunit Reference ======== @@ -70,6 +71,30 @@ exports.lambda_invoke = { var expected = getNormalizedFile('test/expected/failure_options'); var actual = grunt.util.normalizelf(result.stdout); test.equal(actual, expected); + test.done(); + }); + }, + package_folder_options: function (test) { + test.expect(2); + + grunt.util.spawn({ + grunt: true, + args: ['lambda_invoke:package_folder_options', '--no-color'] + }, function (err, result, code) { + + var cwd = process.cwd(); + + // test cwd inside the function + var expected_cwd = 'Running "lambda_invoke:package_folder_options" (lambda_invoke) task\n\n\nSuccess! Message:\n------------------\n' + + path.join(cwd, 'test/fixtures/package_folder_option') + + '\n\nDone, without errors.'; + + var actual_cwd = grunt.util.normalizelf(result.stdout); + test.equal(actual_cwd, expected_cwd); + + // test back from the function + test.equal(process.cwd(), cwd); + test.done(); }); }