diff --git a/lib/main.js b/lib/main.js index 03866b0c..748dbfcb 100644 --- a/lib/main.js +++ b/lib/main.js @@ -64,7 +64,10 @@ Lambda.prototype.run = function (program) { const event = require(path.join(process.cwd(), program.eventFile)) const context = require(path.join(process.cwd(), program.contextFile)) - this._runHandler(handler, event, program, context) + if (Object.prototype.toString.call(event) !== '[object Array]') { + return this._runHandler(handler, event, program, context) + } + this._runMultipleHandlers(event) } Lambda.prototype._runHandler = (handler, event, program, context) => { @@ -95,6 +98,47 @@ Lambda.prototype._runHandler = (handler, event, program, context) => { handler(event, context, callback) } +Lambda.prototype._runMultipleHandlers = (events) => { + console.log(`!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! +Usually you will receive a single Object from AWS Lambda. +We added support for event.json to contain an array, +so you can easily test run multiple events. +!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! +`) + + const _argv = process.argv + const eventFileOptionIndex = (() => { + const index = _argv.indexOf('-j') + if (index >= 0) return index + return _argv.indexOf('--eventFile') + })() + + // In order to reproduce the logic of callbackWaitsForEmptyEventLoop, + // we are going to execute `node-lambda run`. + events.forEach((event, i) => { + const tmpEventFile = `.${i}_tmp_event.json` + const command = () => { + if (eventFileOptionIndex === -1) { + return _argv.concat(['-j', tmpEventFile]).join(' ') + } + _argv[eventFileOptionIndex + 1] = tmpEventFile + return _argv.join(' ') + } + + fs.writeFileSync(tmpEventFile, JSON.stringify(event)) + exec(command(), { + maxBuffer: maxBufferSize, + env: process.env + }, (err, stdout, stderr) => { + console.log('>>> Event:', event, '<<<') + if (err) console.error(err) + console.log(stdout) + console.log(stderr) + fs.unlinkSync(tmpEventFile) + }) + }) +} + Lambda.prototype._params = function (program, buffer) { var params = { FunctionName: program.functionName + diff --git a/test/node-lambda.js b/test/node-lambda.js index 0f3eafba..2b90d9fb 100644 --- a/test/node-lambda.js +++ b/test/node-lambda.js @@ -17,8 +17,8 @@ describe('bin/node-lambda', () => { '--handler', '__test.handler', '--eventFile', 'event.json' ]) - var stdoutString = '' - var stderrString = '' + let stdoutString = '' + let stderrString = '' run.stdout.on('data', (data) => { stdoutString += data.toString().replace(/\r|\n/g, '') }) @@ -169,5 +169,32 @@ describe('bin/node-lambda', () => { }, done) }) }) + + describe('node-lambda run (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`', (done) => { + _generateEventFile(eventObj) + _testMain({ + stdoutRegExp: / no: [123] .+ no: [123] .+ no: [123] .+Success:/, + exitCode: 0 + }, done) + }) + }) }) })