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/tasks/lambda_invoke.js b/tasks/lambda_invoke.js index 18ab6a3..eb9a039 100644 --- a/tasks/lambda_invoke.js +++ b/tasks/lambda_invoke.js @@ -13,13 +13,13 @@ module.exports = function (grunt) { var path = require('path'); var fs = require('fs'); - // Please see the Grunt documentation for more information regarding task // creation: http://gruntjs.com/creating-tasks 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 +57,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); + } }); }; 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(); }); }