Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import assert from 'node:assert';
import { FunctionDefinition, renderFunctions } from './source_builder';
import { printNodeArray } from '../test_utils/ts_node_printer';
import { Runtime } from '@aws-sdk/client-lambda';

describe('render function', () => {
describe('import', () => {
Expand Down Expand Up @@ -40,13 +41,33 @@ describe('render function', () => {
const source = printNodeArray(rendered);
assert.match(source, /name: /);
});
it('does render runtime property', () => {
test.each([[Runtime.nodejs16x], [Runtime.nodejs18x], [Runtime.nodejs20x], [Runtime.nodejs22x]])(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking out loud, could we also pass the expectedRuntime of 16, 18, 20, 22 along with this and use it directly instead of parsing like const expectedRuntime = nodejsRuntime.split('nodejs')[1].split('.')[0]; What do you think?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good callout! The main reasoning for doing it this way in the test was to make sure we are deriving the expected version from the runtime string itself (as a source of truth). In this way, we can catch any regressions if we change the mapping in the actual switch case. Otherwise, we would need to maintain additional mapping in the test file, which could be prone to errors. Let me know what you think !

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, using the Runtime enum string from the lambda sdk as a source of truth. Yes, that makes sense.

'does render runtime property for %s nodejs.',
(nodejsRuntime: Runtime) => {
const definition: FunctionDefinition = {};
definition.runtime = nodejsRuntime;

const rendered = renderFunctions(definition);
const source = printNodeArray(rendered);
const expectedRuntime = nodejsRuntime.split('nodejs')[1].split('.')[0];
assert(expectedRuntime);
assert.match(source, new RegExp(`runtime: ${expectedRuntime}`));
},
);

it('throws error for unsupported nodejs runtime', () => {
const definition: FunctionDefinition = {};
definition.runtime = Runtime.nodejs14x;

assert.throws(() => renderFunctions(definition), /Unsupported nodejs runtime/);
});
it('does not render runtime property for unsupported runtimes', () => {
const definition: FunctionDefinition = {};
definition.runtime = 'nodejs18.x';
definition.runtime = Runtime.dotnet8;

const rendered = renderFunctions(definition);
const source = printNodeArray(rendered);
assert.match(source, /runtime: 18/);
assert.doesNotMatch(source, /runtime: /);
});
it('does render timeoutSeconds property', () => {
const definition: FunctionDefinition = {};
Expand Down
27 changes: 20 additions & 7 deletions packages/amplify-gen2-codegen/src/function/source_builder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ts, { ObjectLiteralElementLike } from 'typescript';
import { EnvironmentResponse, Runtime } from '@aws-sdk/client-lambda';
import { renderResourceTsFile } from '../resource/resource';
import assert from 'node:assert';

export interface FunctionDefinition {
category?: string;
Expand Down Expand Up @@ -96,14 +97,26 @@ export function createFunctionDefinition(
);
}

let nodeRuntime = 0;
if (definition?.runtime) {
const runtime = definition?.runtime;
if (runtime === Runtime.nodejs16x) {
nodeRuntime = 16;
} else if (runtime === Runtime.nodejs18x) {
nodeRuntime = 18;
const runtime = definition?.runtime;
if (runtime && runtime.includes('nodejs')) {
let nodeRuntime: number | undefined;
switch (runtime) {
case Runtime.nodejs16x:
nodeRuntime = 16;
break;
case Runtime.nodejs18x:
nodeRuntime = 18;
break;
case Runtime.nodejs20x:
nodeRuntime = 20;
break;
case Runtime.nodejs22x:
nodeRuntime = 22;
break;
default:
throw new Error(`Unsupported nodejs runtime for function: ${runtime}`);
}
assert(nodeRuntime, 'Expected nodejs version to be set');

defineFunctionProperties.push(createParameter('runtime', factory.createNumericLiteral(nodeRuntime)));
}
Expand Down
Loading