diff --git a/bin/node-lambda b/bin/node-lambda index df554fde..731c0bcd 100755 --- a/bin/node-lambda +++ b/bin/node-lambda @@ -55,6 +55,7 @@ const AWS_DLQ_TARGET_ARN = (() => { return undefined })() const PROXY = process.env.PROXY || process.env.http_proxy || '' +const ENABLE_RUN_MULTIPLE_EVENTS = true program .command('deploy') @@ -126,6 +127,8 @@ program .option('-f, --configFile [' + CONFIG_FILE + ']', 'Path to file holding secret environment variables (e.g. "deploy.env")', CONFIG_FILE) .option('-x, --contextFile [' + CONTEXT_FILE + ']', 'Context JSON File', CONTEXT_FILE) + .option('-M, --enableRunMultipleEvents [' + ENABLE_RUN_MULTIPLE_EVENTS + ']', 'Enable run multiple events', + ENABLE_RUN_MULTIPLE_EVENTS) .action((prg) => lambda.run(prg)) program diff --git a/lib/main.js b/lib/main.js index b413b046..0c1b7415 100644 --- a/lib/main.js +++ b/lib/main.js @@ -67,11 +67,17 @@ class Lambda { const handler = require(path.join(process.cwd(), filename))[handlername] const event = require(path.join(process.cwd(), program.eventFile)) const context = require(path.join(process.cwd(), program.contextFile)) + const enableRunMultipleEvents = (() => { + if (typeof program.enableRunMultipleEvents === 'boolean') { + return program.enableRunMultipleEvents + } + return program.enableRunMultipleEvents === 'true' + })() - if (!Array.isArray(event)) { - return this._runHandler(handler, event, program, context) + if (Array.isArray(event) && enableRunMultipleEvents === true) { + return this._runMultipleHandlers(event) } - this._runMultipleHandlers(event) + this._runHandler(handler, event, program, context) } _runHandler (handler, event, program, context) { diff --git a/test/node-lambda.js b/test/node-lambda.js index dcabaf30..857b3030 100644 --- a/test/node-lambda.js +++ b/test/node-lambda.js @@ -197,6 +197,53 @@ describe('bin/node-lambda', () => { }, done) }) }) + + describe('node-lambda run (disable Multiple events))', () => { + const eventObj = [{ + asyncTest: false, + callbackWaitsForEmptyEventLoop: true, + callbackCode: 'callback(null);', + no: 1 + }, { + asyncTest: false, + callbackWaitsForEmptyEventLoop: true, + callbackCode: 'callback(null);', + no: 2 + }, { + asyncTest: false, + callbackWaitsForEmptyEventLoop: true, + callbackCode: 'callback(null);', + no: 3 + }] + + it('`node-lambda run` exitCode is `0`', function (done) { + this.timeout(10000) // give it time to multiple executions + + _generateEventFile(eventObj) + const run = spawn('node', [ + nodeLambdaPath, 'run', + '--handler', 'index.handler', + '--eventFile', 'event.json', + '-M', 'false' + ]) + let stdoutString = '' + run.stdout.on('data', (data) => { + stdoutString += data.toString().replace(/\r|\n/g, '') + }) + + run.on('exit', (code) => { + const expected = 'Running index.handler==================================event ' + + '[ { asyncTest: false, callbackWaitsForEmptyEventLoop: true, callbackCode: \'callback(null);\', no: 1 }, ' + + '{ asyncTest: false, callbackWaitsForEmptyEventLoop: true, callbackCode: \'callback(null);\', no: 2 }, ' + + '{ asyncTest: false, callbackWaitsForEmptyEventLoop: true, callbackCode: \'callback(null);\', no: 3 } ]' + + '==================================Stopping index.handlerSuccess:' + + assert.equal(stdoutString, expected) + assert.equal(code, 0) + done() + }) + }) + }) }) describe('node-lambda --version', () => {