Skip to content

Commit b7ffb01

Browse files
authored
Merge pull request #4 from fourTheorem/feat/support-extensionless-function-index
feat(function): support extensionless function index
2 parents 3a25e51 + e78bc5f commit b7ffb01

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/function.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ export class PythonFunction extends Function {
6666
const architecture = props.architecture ?? Architecture.ARM_64;
6767
const rootDir = path.resolve(props.rootDir);
6868

69-
const resolvedHandler = `${index.slice(0, -3)}.${handler}`.replace(/\//g, '.');
69+
// Strip .py from the end of handler if it exists
70+
const strippedIndex = index.endsWith('.py') ? index.slice(0, -3) : index;
71+
72+
const resolvedHandler = `${strippedIndex}.${handler}`.replace(/\//g, '.');
7073

7174
if (runtime.family !== RuntimeFamily.PYTHON) {
7275
throw new Error('Only Python runtimes are supported');

test/function.test.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ test('Create a function from basic_app', async () => {
3939
});
4040

4141
const template = Template.fromStack(stack);
42-
console.log(JSON.stringify(template.toJSON(), null, ' '));
4342

4443
template.hasResourceProperties('AWS::Lambda::Function', {
4544
Handler: 'handler.lambda_handler',
@@ -55,6 +54,29 @@ test('Create a function from basic_app', async () => {
5554
expect(contents).toContain('handler.py');
5655
});
5756

57+
test('Create a function from basic_app with no .py index extension', async () => {
58+
const app = new App({});
59+
const stack = new Stack(app, 'test');
60+
new PythonFunction(stack, 'basic_app', {
61+
rootDir: path.join(resourcesPath, 'basic_app'),
62+
index: 'handler',
63+
handler: 'lambda_handler',
64+
runtime: Runtime.PYTHON_3_12,
65+
architecture: await getDockerHostArch(),
66+
});
67+
68+
const template = Template.fromStack(stack);
69+
70+
template.hasResourceProperties('AWS::Lambda::Function', {
71+
Handler: 'handler.lambda_handler',
72+
Runtime: 'python3.12',
73+
Code: {
74+
S3Bucket: Match.anyValue(),
75+
S3Key: Match.anyValue(),
76+
},
77+
});
78+
});
79+
5880
test('Create a function with workspaces_app', async () => {
5981
const app = new App({});
6082
const stack = new Stack(app, 'wstest');
@@ -68,7 +90,6 @@ test('Create a function with workspaces_app', async () => {
6890
});
6991

7092
const template = Template.fromStack(stack);
71-
console.log(JSON.stringify(template.toJSON(), null, ' '));
7293

7394
template.hasResourceProperties('AWS::Lambda::Function', {
7495
Handler: 'app_handler.handle_event',

0 commit comments

Comments
 (0)