Skip to content

Commit 5d4c2cf

Browse files
committed
Update SAM CLI to accept a runtime parameter in local invokes
1 parent df810aa commit 5d4c2cf

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

packages/core/src/shared/sam/cli/samCliLocalInvoke.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,8 @@ export interface SamCliLocalInvokeInvocationArguments {
217217
name?: string
218218
/** AWS region */
219219
region?: string
220+
/** Runtime to use for testing. Can be different from the one specified in the template. */
221+
runtime?: string
220222
}
221223

222224
/**
@@ -265,6 +267,7 @@ export class SamCliLocalInvokeInvocation {
265267
...(this.args.parameterOverrides ?? [])
266268
)
267269
invokeArgs.push(...(this.args.extraArgs ?? []))
270+
pushIf(invokeArgs, !!this.args.runtime, '--runtime', this.args.runtime)
268271

269272
return await this.args.invoker.invoke({
270273
options: {

packages/core/src/shared/sam/localLambdaRunner.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ async function invokeLambdaHandler(
247247
parameterOverrides: config.parameterOverrides,
248248
name: config.name,
249249
region: config.region,
250+
runtime: config.lambda?.runtime,
250251
}
251252

252253
// sam local invoke ...

packages/core/src/test/shared/sam/cli/samCliLocalInvoke.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ describe('SamCliLocalInvokeInvocation', async function () {
6868
// `extraArgs` are appended to the end.
6969
assert.strictEqual(invokeArgs.args[10], '--build-dir')
7070
assert.strictEqual(invokeArgs.args[11], 'my/build/dir/')
71+
72+
// 'runtime' is the last argument.
73+
assert.strictEqual(invokeArgs.args[invokeArgs.args.length - 2], '--runtime')
74+
assert.strictEqual(invokeArgs.args[invokeArgs.args.length - 1], 'python3.10')
7175
}
7276
)
7377

@@ -78,6 +82,7 @@ describe('SamCliLocalInvokeInvocation', async function () {
7882
environmentVariablePath: nonRelevantArg,
7983
invoker: taskInvoker,
8084
extraArgs: ['--build-dir', 'my/build/dir/'],
85+
runtime: 'python3.10',
8186
}).execute()
8287
})
8388

@@ -302,4 +307,39 @@ describe('SamCliLocalInvokeInvocation', async function () {
302307
invoker: taskInvoker,
303308
}).execute()
304309
})
310+
311+
it('Passes runtime to sam cli', async function () {
312+
const runtime = 'python3.10'
313+
314+
const taskInvoker: SamLocalInvokeCommand = new TestSamLocalInvokeCommand(
315+
(invokeArgs: SamLocalInvokeCommandArgs) => {
316+
assertArgsContainArgument(invokeArgs.args, '--runtime', runtime)
317+
}
318+
)
319+
320+
await new SamCliLocalInvokeInvocation({
321+
templateResourceName: nonRelevantArg,
322+
templatePath: placeholderTemplateFile,
323+
eventPath: placeholderEventFile,
324+
environmentVariablePath: nonRelevantArg,
325+
invoker: taskInvoker,
326+
runtime: runtime,
327+
}).execute()
328+
})
329+
330+
it('Does not pass runtime to sam cli when undefined', async function () {
331+
const taskInvoker: SamLocalInvokeCommand = new TestSamLocalInvokeCommand(
332+
(invokeArgs: SamLocalInvokeCommandArgs) => {
333+
assertArgNotPresent(invokeArgs.args, '--runtime')
334+
}
335+
)
336+
337+
await new SamCliLocalInvokeInvocation({
338+
templateResourceName: nonRelevantArg,
339+
templatePath: placeholderTemplateFile,
340+
eventPath: placeholderEventFile,
341+
environmentVariablePath: nonRelevantArg,
342+
invoker: taskInvoker,
343+
}).execute()
344+
})
305345
})

0 commit comments

Comments
 (0)