Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added package_folder option to lambda_invoke task #62

Merged
merged 3 commits into from
Jan 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
13 changes: 11 additions & 2 deletions tasks/lambda_invoke.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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);
}
});

};
1 change: 1 addition & 0 deletions test/fixtures/package_folder_option/.test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World
3 changes: 3 additions & 0 deletions test/fixtures/package_folder_option/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
exports.handler = function (event, context) {
context.done(null, process.cwd());
};
12 changes: 12 additions & 0 deletions test/fixtures/package_folder_option/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "my-lambda-function",
"description": "An Example Lamda Function",
"version": "0.0.1",
"private": "true",
"dependencies": {
},
"devDependencies": {
},
"bundledDependencies": [
]
}
25 changes: 25 additions & 0 deletions test/lambda_invoke_test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var grunt = require('grunt');
var path = require('path');

/*
======== A Handy Little Nodeunit Reference ========
Expand Down Expand Up @@ -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();
});
}
Expand Down