Skip to content

Commit bc412db

Browse files
committed
chore(tests): add basic unit test
1 parent 1feb739 commit bc412db

File tree

11 files changed

+99
-36
lines changed

11 files changed

+99
-36
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1-
# replace this
1+
# replace this
2+
3+
4+
```
5+
error: failed to create directory `/.local/share/uv/python`
6+
```
7+
ensure uv.lock exists

src/bundling.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ export class Bundling {
146146
commands.push(...[
147147
`rsync -rLv ${excludeArgs.join(' ')} ${options.inputDir}/ ${options.outputDir}`,
148148
`cd ${options.outputDir}`, // uv pip install needs to be run from here for editable deps to relative paths to be resolved
149-
`VIRTUAL_ENV=/tmp/venv uv sync ${uvCommonArgs} ${uvPackageArgs} --compile-bytecode --no-dev --frozen --no-editable --link-mode=copy`,
150-
`VIRTUAL_ENV=/tmp/venv uv export ${uvCommonArgs} ${uvPackageArgs} --no-dev --frozen --no-editable > ${reqsFile}`,
151-
`uv pip install -r ${reqsFile} --target ${options.outputDir} --reinstall --compile-bytecode --link-mode=copy --editable $(grep -e "^\./" ${reqsFile})`,
149+
`uv sync ${uvCommonArgs} ${uvPackageArgs} --python-preference=only-system --compile-bytecode --no-dev --frozen --no-editable --link-mode=copy`,
150+
`uv export ${uvCommonArgs} ${uvPackageArgs} --no-dev --frozen --no-editable > ${reqsFile}`,
151+
`uv pip install -r ${reqsFile} --target ${options.outputDir} --reinstall --compile-bytecode --link-mode=copy`, // --editable $(grep -e "^\./" ${reqsFile})`,
152152
`rm -rf ${options.outputDir}/.venv`,
153153
]);
154154
commands.push(

src/function.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,17 @@ export class PythonFunction extends Function {
9393
});
9494

9595
const assetPath = ((this.node.defaultChild) as CfnFunction).getMetadata('aws:asset:path');
96-
const codePath = path.join(process.env.CDK_OUTDIR as string, assetPath);
96+
if (assetPath) { // TODO - remove - we always need one
97+
const codePath = path.join(process.env.CDK_OUTDIR as string, assetPath);
9798

98-
const pythonPaths = getPthFilePaths(codePath);
99+
const pythonPaths = getPthFilePaths(codePath);
99100

100-
if (pythonPaths.length > 0) {
101-
let pythonPathValue = environment.PYTHONPATH;
102-
const addedPaths = pythonPaths.join(':');
103-
pythonPathValue = pythonPathValue ? `${pythonPathValue}:${addedPaths}` : addedPaths;
104-
this.addEnvironment('PYTHONPATH', pythonPathValue);
101+
if (pythonPaths.length > 0) {
102+
let pythonPathValue = environment.PYTHONPATH;
103+
const addedPaths = pythonPaths.join(':');
104+
pythonPathValue = pythonPathValue ? `${pythonPathValue}:${addedPaths}` : addedPaths;
105+
this.addEnvironment('PYTHONPATH', pythonPathValue);
106+
}
105107
}
106108
}
107109
}

test/function.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import * as fs from 'node:fs/promises';
2+
import * as path from 'node:path';
3+
import { App, Stack } from 'aws-cdk-lib';
4+
import { Match, Template } from 'aws-cdk-lib/assertions';
5+
import { Runtime } from 'aws-cdk-lib/aws-lambda';
6+
import { PythonFunction } from '../src';
7+
8+
const resourcesPath = path.resolve(__dirname, 'resources');
9+
10+
11+
test('Create a function', async () => {
12+
const app = new App({});
13+
const stack = new Stack(app, 'test');
14+
new PythonFunction(stack, 'basic_app', {
15+
rootDir: path.join(resourcesPath, 'basic_app'),
16+
index: 'handler.py',
17+
handler: 'lambda_handler',
18+
runtime: Runtime.PYTHON_3_12,
19+
});
20+
21+
const template = Template.fromStack(stack);
22+
console.log(JSON.stringify(template.toJSON(), null, ' '));
23+
24+
template.hasResourceProperties('AWS::Lambda::Function', {
25+
Handler: 'handler.lambda_handler',
26+
Runtime: 'python3.12',
27+
Code: {
28+
S3Bucket: Match.anyValue(),
29+
S3Key: Match.anyValue(),
30+
},
31+
});
32+
const functions = Object.values(template.findResources('AWS::Lambda::Function'));
33+
expect(functions.length).toBe(1);
34+
expect(functions[0].Properties.Handler).toBe('handler.lambda_handler');
35+
expect(functions[0].Properties.Runtime).toBe('python3.12');
36+
const [assetHash] = functions[0].Properties.Code.S3Key.split('.');
37+
const assetPath = path.join(app.outdir, `asset.${assetHash}`);
38+
const contents = await fs.readdir(assetPath);
39+
expect(contents).toContain('handler.py');
40+
});

test/hello.test.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.12

test/resources/basic_app/README.md

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def lambda_handler(event, context):
2+
return {
3+
'statusCode': 200,
4+
'body': 'Hello from Lambda!'
5+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[project]
2+
name = "basic-app"
3+
version = "0.1.0"
4+
description = "Add your description here"
5+
readme = "README.md"
6+
requires-python = ">=3.12"
7+
dependencies = []

test/resources/basic_app/uv.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)