Skip to content

Commit 02dfc0a

Browse files
abetomoDeviaVir
authored andcommitted
Fix timing to check Runtime (motdotla#310)
* Modify arrow function and `var` to `const` * Add stderr test to `bin/node-lambda`test * Add 'Runtime is not supported' test * Fix timing to check Runtime If it is an unsupported Runtime, it ends with an error. Therefore, I checked it at an early stage
1 parent 5f176ec commit 02dfc0a

2 files changed

Lines changed: 51 additions & 17 deletions

File tree

lib/main.js

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,28 +45,33 @@ Lambda.prototype.setup = function (program) {
4545
}
4646

4747
Lambda.prototype.run = function (program) {
48+
if (['nodejs4.3', 'nodejs6.10'].indexOf(program.runtime) === -1) {
49+
console.error(`Runtime [${program.runtime}] is not supported.`)
50+
process.exit(254)
51+
}
52+
4853
this._createSampleFile(program.eventFile, 'event.json')
49-
var splitHandler = program.handler.split('.')
50-
var filename = splitHandler[0] + '.js'
51-
var handlername = splitHandler[1]
54+
const splitHandler = program.handler.split('.')
55+
const filename = splitHandler[0] + '.js'
56+
const handlername = splitHandler[1]
5257

5358
// Set custom environment variables if program.configFile is defined
5459
if (program.configFile) {
5560
this._setRunTimeEnvironmentVars(program)
5661
}
5762

58-
var handler = require(path.join(process.cwd(), filename))[handlername]
59-
var event = require(path.join(process.cwd(), program.eventFile))
60-
var context = require(path.join(process.cwd(), program.contextFile))
63+
const handler = require(path.join(process.cwd(), filename))[handlername]
64+
const event = require(path.join(process.cwd(), program.eventFile))
65+
const context = require(path.join(process.cwd(), program.contextFile))
6166

6267
this._runHandler(handler, event, program, context)
6368
}
6469

65-
Lambda.prototype._runHandler = function (handler, event, program, context) {
66-
var startTime = new Date()
67-
var timeout = Math.min(program.timeout, 300) * 1000 // convert the timeout into milliseconds
70+
Lambda.prototype._runHandler = (handler, event, program, context) => {
71+
const startTime = new Date()
72+
const timeout = Math.min(program.timeout, 300) * 1000 // convert the timeout into milliseconds
6873

69-
var callback = function (err, result) {
74+
const callback = (err, result) => {
7075
if (err) {
7176
process.exitCode = 255
7277
console.log('Error: ' + err)
@@ -82,15 +87,11 @@ Lambda.prototype._runHandler = function (handler, event, program, context) {
8287
}
8388
}
8489

85-
context.getRemainingTimeInMillis = function () {
86-
var currentTime = new Date()
90+
context.getRemainingTimeInMillis = () => {
91+
const currentTime = new Date()
8792
return timeout - (currentTime - startTime)
8893
}
8994

90-
if (['nodejs4.3', 'nodejs6.10'].indexOf(program.runtime) === -1) {
91-
console.error(`Runtime [${program.runtime}] is not supported.`)
92-
process.exit(254)
93-
}
9495
handler(event, context, callback)
9596
}
9697

test/node-lambda.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,21 @@ describe('bin/node-lambda', () => {
1818
'--eventFile', 'event.json'
1919
])
2020
var stdoutString = ''
21+
var stderrString = ''
2122
run.stdout.on('data', (data) => {
2223
stdoutString += data.toString().replace(/\r|\n/g, '')
2324
})
25+
run.stderr.on('data', (data) => {
26+
stderrString += data.toString().replace(/\r|\n/g, '')
27+
})
2428

2529
run.on('exit', (code) => {
26-
assert.match(stdoutString, expectedValues.stdoutRegExp)
30+
if (expectedValues.stdoutRegExp) {
31+
assert.match(stdoutString, expectedValues.stdoutRegExp)
32+
}
33+
if (expectedValues.stderrRegExp) {
34+
assert.match(stderrString, expectedValues.stderrRegExp)
35+
}
2736
assert.equal(code, expectedValues.exitCode)
2837
done()
2938
})
@@ -136,5 +145,29 @@ describe('bin/node-lambda', () => {
136145
})
137146
})
138147
})
148+
149+
describe('node-lambda run (Runtime is not supported)', () => {
150+
const eventObj = {
151+
asyncTest: false,
152+
callbackWaitsForEmptyEventLoop: true // True is the default value of Lambda
153+
}
154+
155+
before(() => {
156+
process.env.AWS_RUNTIME = 'test'
157+
})
158+
after(() => {
159+
process.env.AWS_RUNTIME = 'nodejs6.10'
160+
})
161+
162+
it('`node-lambda run` exitCode is `254` (callback(null))', (done) => {
163+
_generateEventFile(Object.assign(eventObj, {
164+
callbackCode: 'callback(null);'
165+
}))
166+
_testMain({
167+
stderrRegExp: /^Runtime \[test\] is not supported\.$/,
168+
exitCode: 254
169+
}, done)
170+
})
171+
})
139172
})
140173
})

0 commit comments

Comments
 (0)