diff --git a/lib/main.js b/lib/main.js index f077411a..51cfd992 100644 --- a/lib/main.js +++ b/lib/main.js @@ -69,13 +69,13 @@ 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) { - console.log(JSON.stringify(result)); + if (!result) { + process.exit(0); } - process.exit(0); + console.log(JSON.stringify(result)); }; context.getRemainingTimeInMillis = function () { @@ -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); }; diff --git a/test/main.js b/test/main.js index 0bc5e728..3a95502d 100644 --- a/test/main.js +++ b/test/main.js @@ -45,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. diff --git a/test/node-lambda.js b/test/node-lambda.js new file mode 100644 index 00000000..b0b39558 --- /dev/null +++ b/test/node-lambda.js @@ -0,0 +1,80 @@ +'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(__dirname, '..', '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((file) => fs.unlinkSync(file)); + }); + + it('`node-lambda run` exitCode is `0` (callback(null))', (done) => { + const run = spawn('node', [nodeLambdaPath, 'run']); + var stdoutString = ''; + run.stdout.on('data', (data) => { + stdoutString += data.toString().replace(/\r|\n/g, ''); + }); + + run.on('exit', (code) => { + assert.match(stdoutString, /Success:$/); + assert.equal(code, 0); + 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', (data) => { + stdoutString += data.toString().replace(/\r|\n/g, ''); + }); + + 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")))', (done) => { + _generateHandlerFile('callback(new Error("e"));'); + + const run = spawn('node', [nodeLambdaPath, 'run', '--handler', '__test.handler']); + var stdoutString = ''; + run.stdout.on('data', (data) => { + stdoutString += data.toString().replace(/\r|\n/g, ''); + }); + + run.on('exit', (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);