From 862c26137cb96714c7e026cd39bf304534ad1b5c Mon Sep 17 00:00:00 2001 From: abetomo Date: Tue, 16 May 2017 14:12:54 +0900 Subject: [PATCH 1/8] Fix not explicitly exit when `result` is present Because Node.js 4.3 sometimes truncate on the way if the result size is large --- lib/main.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/main.js b/lib/main.js index f077411a..198c7915 100644 --- a/lib/main.js +++ b/lib/main.js @@ -72,10 +72,10 @@ Lambda.prototype._runHandler = function (handler, event, program, context) { process.exit(-1); } console.log('Success:'); - if (result) { - console.log(JSON.stringify(result)); + if (!result) { + process.exit(0); } - process.exit(0); + console.log(JSON.stringify(result)); }; context.getRemainingTimeInMillis = function () { From 2e03e8d0d64c8cde11ad940cd55a152a7a8200be Mon Sep 17 00:00:00 2001 From: abetomo Date: Tue, 16 May 2017 14:14:38 +0900 Subject: [PATCH 2/8] Add test when `node-lambda run` was executed --- test/main.js | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/test/main.js b/test/main.js index 0bc5e728..941ba6cf 100644 --- a/test/main.js +++ b/test/main.js @@ -9,6 +9,7 @@ var Hoek = require('hoek'); var lambda = require(path.join(__dirname, '..', 'lib', 'main')); var zip = require('node-zip'); var rimraf = require('rimraf'); +var spawn = require('child_process').spawn; var assert = chai.assert; @@ -849,4 +850,82 @@ describe('node-lambda', function () { }); }); }); + + describe('node-lambda run', function () { + const nodeLambdaPath = path.join('bin', 'node-lambda'); + const _generateHandlerFile = function (callbackString) { + fs.writeFileSync( + '__test.js', + fs.readFileSync('index.js').toString() + .replace(/callback\(null\);/, callbackString) + ); + }; + + before(function () { + // Restore default value + program.eventFile = 'event.json'; + program.contextFile = 'context.json'; + + lambda.setup(program); + }); + + after(function () { + [ + '.env', + 'context.json', + 'event.json', + 'deploy.env', + 'event_sources.json', + '__test.js' + ].forEach(function(file) { + fs.unlinkSync(file); + }); + }); + + it('`node-lambda run` exitCode is `0` (callback(null))', function (done) { + const run = spawn(nodeLambdaPath, ['run']); + var stdoutString = ''; + run.stdout.on('data', function (data) { + stdoutString += data.toString().replace(/\r|\n/g, ''); + }); + + run.on('exit', function (code) { + assert.match(stdoutString, /Success:$/); + assert.equal(code, 0); + done(); + }); + }); + + it('`node-lambda run` exitCode is `0` (callback(null, "text"))', function (done) { + _generateHandlerFile('callback(null, "text");'); + + const run = spawn(nodeLambdaPath, ['run', '--handler', '__test.handler']); + var stdoutString = ''; + run.stdout.on('data', function (data) { + stdoutString += data.toString().replace(/\r|\n/g, ''); + }); + + run.on('exit', function (code) { + assert.match(stdoutString, /Success:"text"$/); + assert.equal(code, 0); + done(); + }); + }); + + it('`node-lambda run` exitCode is `255` (callback(new Error("e")))', function (done) { + _generateHandlerFile('callback(new Error("e"));'); + + const run = spawn(nodeLambdaPath, ['run', '--handler', '__test.handler']); + var stdoutString = ''; + run.stdout.on('data', function (data) { + stdoutString += data.toString().replace(/\r|\n/g, ''); + }); + + run.on('exit', function (code) { + assert.match(stdoutString, /Error: Error: e$/); + assert.equal(code, 255); + done(); + }); + }); + }); }); From 3a5f4d03f0f03d0e89f7fb44f84a22005e50b94f Mon Sep 17 00:00:00 2001 From: abetomo Date: Tue, 16 May 2017 14:33:32 +0900 Subject: [PATCH 3/8] Modify exitcode to positive integer Because the value changes on Windows and Linux. --- lib/main.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/main.js b/lib/main.js index 198c7915..51cfd992 100644 --- a/lib/main.js +++ b/lib/main.js @@ -69,7 +69,7 @@ Lambda.prototype._runHandler = function (handler, event, program, context) { var callback = function (err, result) { if (err) { console.log('Error: ' + err); - process.exit(-1); + process.exit(255); } console.log('Success:'); if (!result) { @@ -85,7 +85,7 @@ Lambda.prototype._runHandler = function (handler, event, program, context) { if (['nodejs4.3', 'nodejs6.10'].indexOf(program.runtime) == -1) { console.error("Runtime [" + program.runtime + "] is not supported."); - process.exit(-2); + process.exit(254); } handler(event, context, callback); }; From aa0c5aca1e4e0a6c0fdd7beb21f1888e11ee1af3 Mon Sep 17 00:00:00 2001 From: abetomo Date: Tue, 16 May 2017 14:37:41 +0900 Subject: [PATCH 4/8] Fix to run on `node` Because it is not executed unless you explicitly specify the node command on Windows in case of executing in unit test. --- test/main.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/main.js b/test/main.js index 941ba6cf..ef818981 100644 --- a/test/main.js +++ b/test/main.js @@ -883,7 +883,7 @@ describe('node-lambda', function () { }); it('`node-lambda run` exitCode is `0` (callback(null))', function (done) { - const run = spawn(nodeLambdaPath, ['run']); + const run = spawn('node', [nodeLambdaPath, 'run']); var stdoutString = ''; run.stdout.on('data', function (data) { stdoutString += data.toString().replace(/\r|\n/g, ''); @@ -899,7 +899,7 @@ describe('node-lambda', function () { it('`node-lambda run` exitCode is `0` (callback(null, "text"))', function (done) { _generateHandlerFile('callback(null, "text");'); - const run = spawn(nodeLambdaPath, ['run', '--handler', '__test.handler']); + const run = spawn('node', [nodeLambdaPath, 'run', '--handler', '__test.handler']); var stdoutString = ''; run.stdout.on('data', function (data) { stdoutString += data.toString().replace(/\r|\n/g, ''); @@ -915,7 +915,7 @@ describe('node-lambda', function () { it('`node-lambda run` exitCode is `255` (callback(new Error("e")))', function (done) { _generateHandlerFile('callback(new Error("e"));'); - const run = spawn(nodeLambdaPath, ['run', '--handler', '__test.handler']); + const run = spawn('node', [nodeLambdaPath, 'run', '--handler', '__test.handler']); var stdoutString = ''; run.stdout.on('data', function (data) { stdoutString += data.toString().replace(/\r|\n/g, ''); From 77b045dfa50ad297f5e9ead921acaf678ca3fb38 Mon Sep 17 00:00:00 2001 From: abetomo Date: Tue, 16 May 2017 14:45:54 +0900 Subject: [PATCH 5/8] Add reason for specifying node command --- test/main.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/main.js b/test/main.js index ef818981..5b58bb86 100644 --- a/test/main.js +++ b/test/main.js @@ -852,6 +852,8 @@ describe('node-lambda', function () { }); describe('node-lambda run', function () { + // The reason for specifying the node command in this test is to support Windows. + const nodeLambdaPath = path.join('bin', 'node-lambda'); const _generateHandlerFile = function (callbackString) { fs.writeFileSync( From 9af8213f27804117ff1e8961fd6cde3aaade4371 Mon Sep 17 00:00:00 2001 From: abetomo Date: Wed, 17 May 2017 11:37:13 +0900 Subject: [PATCH 6/8] Added `bin/node-lambda` test I divided files --- test/main.js | 83 +--------------------------------------- test/node-lambda.js | 84 +++++++++++++++++++++++++++++++++++++++++ test/schedule_events.js | 2 +- 3 files changed, 86 insertions(+), 83 deletions(-) create mode 100644 test/node-lambda.js diff --git a/test/main.js b/test/main.js index 5b58bb86..3a95502d 100644 --- a/test/main.js +++ b/test/main.js @@ -9,7 +9,6 @@ var Hoek = require('hoek'); var lambda = require(path.join(__dirname, '..', 'lib', 'main')); var zip = require('node-zip'); var rimraf = require('rimraf'); -var spawn = require('child_process').spawn; var assert = chai.assert; @@ -46,7 +45,7 @@ function _timeout(params) { } } -describe('node-lambda', function () { +describe('lib/main', function () { if (process.platform == 'win32') { // It seems that it takes time for file operation in Windows. // So set `timeout(60000)` for the whole test. @@ -850,84 +849,4 @@ describe('node-lambda', function () { }); }); }); - - describe('node-lambda run', function () { - // The reason for specifying the node command in this test is to support Windows. - - const nodeLambdaPath = path.join('bin', 'node-lambda'); - const _generateHandlerFile = function (callbackString) { - fs.writeFileSync( - '__test.js', - fs.readFileSync('index.js').toString() - .replace(/callback\(null\);/, callbackString) - ); - }; - - before(function () { - // Restore default value - program.eventFile = 'event.json'; - program.contextFile = 'context.json'; - - lambda.setup(program); - }); - - after(function () { - [ - '.env', - 'context.json', - 'event.json', - 'deploy.env', - 'event_sources.json', - '__test.js' - ].forEach(function(file) { - fs.unlinkSync(file); - }); - }); - - it('`node-lambda run` exitCode is `0` (callback(null))', function (done) { - const run = spawn('node', [nodeLambdaPath, 'run']); - var stdoutString = ''; - run.stdout.on('data', function (data) { - stdoutString += data.toString().replace(/\r|\n/g, ''); - }); - - run.on('exit', function (code) { - assert.match(stdoutString, /Success:$/); - assert.equal(code, 0); - done(); - }); - }); - - it('`node-lambda run` exitCode is `0` (callback(null, "text"))', function (done) { - _generateHandlerFile('callback(null, "text");'); - - const run = spawn('node', [nodeLambdaPath, 'run', '--handler', '__test.handler']); - var stdoutString = ''; - run.stdout.on('data', function (data) { - stdoutString += data.toString().replace(/\r|\n/g, ''); - }); - - run.on('exit', function (code) { - assert.match(stdoutString, /Success:"text"$/); - assert.equal(code, 0); - done(); - }); - }); - - it('`node-lambda run` exitCode is `255` (callback(new Error("e")))', function (done) { - _generateHandlerFile('callback(new Error("e"));'); - - const run = spawn('node', [nodeLambdaPath, 'run', '--handler', '__test.handler']); - var stdoutString = ''; - run.stdout.on('data', function (data) { - stdoutString += data.toString().replace(/\r|\n/g, ''); - }); - - run.on('exit', function (code) { - assert.match(stdoutString, /Error: Error: e$/); - assert.equal(code, 255); - done(); - }); - }); - }); }); diff --git a/test/node-lambda.js b/test/node-lambda.js new file mode 100644 index 00000000..0115c82d --- /dev/null +++ b/test/node-lambda.js @@ -0,0 +1,84 @@ +'use strict'; + +const assert = require('chai').assert; +const path = require('path'); +const fs = require('fs-extra'); +const spawn = require('child_process').spawn; +const execSync = require('child_process').execSync; +const nodeLambdaPath = path.join('bin', 'node-lambda'); + +// The reason for specifying the node command in this test is to support Windows. +describe('bin/node-lambda', () => { + describe('node-lambda run', () => { + const _generateHandlerFile = (callbackString) => { + fs.writeFileSync( + '__test.js', + fs.readFileSync('index.js').toString() + .replace(/callback\(null\);/, callbackString) + ); + }; + + before(() => { + execSync(`node ${nodeLambdaPath} setup`); + }); + + after(() => { + [ + '.env', + 'context.json', + 'event.json', + 'deploy.env', + 'event_sources.json', + '__test.js' + ].forEach(function(file) { + fs.unlinkSync(file); + }); + }); + + it('`node-lambda run` exitCode is `0` (callback(null))', function (done) { + const run = spawn('node', [nodeLambdaPath, 'run']); + var stdoutString = ''; + run.stdout.on('data', function (data) { + stdoutString += data.toString().replace(/\r|\n/g, ''); + }); + + run.on('exit', function (code) { + assert.match(stdoutString, /Success:$/); + assert.equal(code, 0); + done(); + }); + }); + + it('`node-lambda run` exitCode is `0` (callback(null, "text"))', function (done) { + _generateHandlerFile('callback(null, "text");'); + + const run = spawn('node', [nodeLambdaPath, 'run', '--handler', '__test.handler']); + var stdoutString = ''; + run.stdout.on('data', function (data) { + stdoutString += data.toString().replace(/\r|\n/g, ''); + }); + + run.on('exit', function (code) { + assert.match(stdoutString, /Success:"text"$/); + assert.equal(code, 0); + done(); + }); + }); + + it('`node-lambda run` exitCode is `255` (callback(new Error("e")))', function (done) { + _generateHandlerFile('callback(new Error("e"));'); + + const run = spawn('node', [nodeLambdaPath, 'run', '--handler', '__test.handler']); + var stdoutString = ''; + run.stdout.on('data', function (data) { + stdoutString += data.toString().replace(/\r|\n/g, ''); + }); + + run.on('exit', function (code) { + assert.match(stdoutString, /Error: Error: e$/); + assert.equal(code, 255); + done(); + }); + }); + }); +}); diff --git a/test/schedule_events.js b/test/schedule_events.js index 1f5d2e84..7806009a 100644 --- a/test/schedule_events.js +++ b/test/schedule_events.js @@ -38,7 +38,7 @@ const mockResponse = { var schedule = null; -describe('schedule_events', () => { +describe('lib/schedule_events', () => { before(() => { aws.mock('CloudWatchEvents', 'putRule', (params, callback) => { callback(null, mockResponse.putRule); From 2f5f5cd6538acdffa0890b8bade4dd06e3c8a480 Mon Sep 17 00:00:00 2001 From: abetomo Date: Wed, 17 May 2017 12:13:50 +0900 Subject: [PATCH 7/8] Modify to arrow function --- test/node-lambda.js | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/test/node-lambda.js b/test/node-lambda.js index 0115c82d..4e68e93d 100644 --- a/test/node-lambda.js +++ b/test/node-lambda.js @@ -18,9 +18,7 @@ describe('bin/node-lambda', () => { ); }; - before(() => { - execSync(`node ${nodeLambdaPath} setup`); - }); + before(() => execSync(`node ${nodeLambdaPath} setup`)); after(() => { [ @@ -30,51 +28,49 @@ describe('bin/node-lambda', () => { 'deploy.env', 'event_sources.json', '__test.js' - ].forEach(function(file) { - fs.unlinkSync(file); - }); + ].forEach((file) => fs.unlinkSync(file)); }); - it('`node-lambda run` exitCode is `0` (callback(null))', function (done) { + it('`node-lambda run` exitCode is `0` (callback(null))', (done) => { const run = spawn('node', [nodeLambdaPath, 'run']); var stdoutString = ''; - run.stdout.on('data', function (data) { + run.stdout.on('data', (data) => { stdoutString += data.toString().replace(/\r|\n/g, ''); }); - run.on('exit', function (code) { + run.on('exit', (code) => { assert.match(stdoutString, /Success:$/); assert.equal(code, 0); done(); }); }); - it('`node-lambda run` exitCode is `0` (callback(null, "text"))', function (done) { + it('`node-lambda run` exitCode is `0` (callback(null, "text"))', (done) => { _generateHandlerFile('callback(null, "text");'); const run = spawn('node', [nodeLambdaPath, 'run', '--handler', '__test.handler']); var stdoutString = ''; - run.stdout.on('data', function (data) { + run.stdout.on('data', (data) => { stdoutString += data.toString().replace(/\r|\n/g, ''); }); - run.on('exit', function (code) { + run.on('exit', (code) => { assert.match(stdoutString, /Success:"text"$/); assert.equal(code, 0); done(); }); }); - it('`node-lambda run` exitCode is `255` (callback(new Error("e")))', function (done) { + it('`node-lambda run` exitCode is `255` (callback(new Error("e")))', (done) => { _generateHandlerFile('callback(new Error("e"));'); const run = spawn('node', [nodeLambdaPath, 'run', '--handler', '__test.handler']); var stdoutString = ''; - run.stdout.on('data', function (data) { + run.stdout.on('data', (data) => { stdoutString += data.toString().replace(/\r|\n/g, ''); }); - run.on('exit', function (code) { + run.on('exit', (code) => { assert.match(stdoutString, /Error: Error: e$/); assert.equal(code, 255); done(); From e82687d375e8db3909914c48b665dab927407556 Mon Sep 17 00:00:00 2001 From: abetomo Date: Wed, 17 May 2017 12:14:18 +0900 Subject: [PATCH 8/8] Modify how to specify path --- test/node-lambda.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/node-lambda.js b/test/node-lambda.js index 4e68e93d..b0b39558 100644 --- a/test/node-lambda.js +++ b/test/node-lambda.js @@ -5,7 +5,7 @@ const path = require('path'); const fs = require('fs-extra'); const spawn = require('child_process').spawn; const execSync = require('child_process').execSync; -const nodeLambdaPath = path.join('bin', 'node-lambda'); +const nodeLambdaPath = path.join(__dirname, '..', 'bin', 'node-lambda'); // The reason for specifying the node command in this test is to support Windows. describe('bin/node-lambda', () => {