Skip to content

Commit

Permalink
chore: fix lambda examples fail with error ENAMETOOLONG (#206)
Browse files Browse the repository at this point in the history
Resolves #164 

Note this changes removes running (some) tests in a temporary working dir. Changing the working directory breaks assumptions about the CWD for file paths. There was no apparent reason why this was introduced in the first place in #110.

There is an open question about how we want to deal with relative file paths. The two options are:
- relative to the CWD
- relative to the template

There is a comment in [deconstruction.ts](https://github.com/cdklabs/decdk/blob/ee07573d61a470288890d14d739dfb88092b51b8/src/deconstruction.ts#L853-L854) that indicates it should be relative to the template file. However the current implementation in [decdk.ts](https://github.com/cdklabs/decdk/blob/145238a53f1f9f8db5c1808500e6bac4c8a0d4a1/src/decdk.ts#L25) does NOT do that (note the missing `workingDirectory` prop).

I've left it at the current implementation (relative to the CWD) for now, which I think is also closer to how imperative CDK behaves. In future the most transparent might be a command line option. Related #200
  • Loading branch information
mrgrain authored Aug 23, 2022
1 parent f6ea701 commit 3ea07a2
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 26 deletions.
2 changes: 1 addition & 1 deletion examples/apigw.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"Type": "aws-cdk-lib.aws_lambda.Function",
"Properties": {
"code": {
"fromAsset": { "path": "." }
"fromAsset": { "path": "examples/lambda-handler" }
},
"runtime": "PYTHON_3_6",
"handler": "index.handler"
Expand Down
16 changes: 9 additions & 7 deletions examples/lambda-events.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"handler": "app.hello_handler",
"runtime": "PYTHON_3_6",
"code": {
"fromAsset": { "path": "." }
"fromAsset": { "path": "examples/lambda-handler" }
},
"environment": {
"Param": "f"
Expand Down Expand Up @@ -51,13 +51,15 @@
}
]
},
"Overrides": [{
"ChildConstructPath": "ServiceRole",
"Update": {
"Path": "Properties.Description",
"Value": "This value has been overridden"
"Overrides": [
{
"ChildConstructPath": "ServiceRole",
"Update": {
"Path": "Properties.Description",
"Value": "This value has been overridden"
}
}
}]
]
}
}
}
3 changes: 3 additions & 0 deletions examples/lambda-handler/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
exports.handler = async function() {
return 'SUCCESS';
}
12 changes: 7 additions & 5 deletions examples/lambda-role-removed.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@
"Lambda": {
"Type": "aws-cdk-lib.aws_lambda.Function",
"Properties": {
"code": { "fromAsset": { "path": "." } },
"code": { "fromAsset": { "path": "examples/lambda-handler" } },
"runtime": "NODEJS",
"handler": "index.handler",
"description": "This function has its service role removed, for demonstration purposes. You probably wouldn't do this to a real function"
},
"Overrides": [{
"ChildConstructPath": "ServiceRole",
"RemoveResource": true
}]
"Overrides": [
{
"ChildConstructPath": "ServiceRole",
"RemoveResource": true
}
]
}
}
}
2 changes: 1 addition & 1 deletion examples/lambda-topic.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"Lambda": {
"Type": "aws-cdk-lib.aws_lambda.Function",
"Properties": {
"code": { "fromAsset": { "path": "." } },
"code": { "fromAsset": { "path": "examples/lambda-handler" } },
"runtime": "NODEJS",
"handler": "index.handler",
"events": [
Expand Down
8 changes: 4 additions & 4 deletions test/__snapshots__/synth.test.ts.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions test/synth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@ import { Testing } from './util';

const dir = path.join(__dirname, '..', 'examples');

for (const templateFile of fs.readdirSync(dir)) {
test(templateFile, async () => {
const template = await readTemplate(path.resolve(dir, templateFile));
const isTemplateFile = (dirent: fs.Dirent): boolean =>
dirent.isFile() &&
(dirent.name.endsWith('.json') || dirent.name.endsWith('.yaml'));

const examples = fs
.readdirSync(dir, { withFileTypes: true })
.filter(isTemplateFile);

for (const templateFile of examples) {
test(templateFile.name, async () => {
const template = await readTemplate(path.resolve(dir, templateFile.name));

const output = await Testing.synth(template);
expect(output.template).toMatchSnapshot();
Expand Down
5 changes: 0 additions & 5 deletions test/util.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import * as os from 'os';
import * as path from 'path';
import * as cdk from 'aws-cdk-lib';
import { Template } from 'aws-cdk-lib/assertions';
import * as fs from 'fs-extra';
import * as reflect from 'jsii-reflect';
import { DeclarativeStack, loadTypeSystem } from '../src';

Expand All @@ -29,14 +26,12 @@ export class Testing {
}

private static async prepare(template: any) {
const workingDirectory = fs.mkdtempSync(path.join(os.tmpdir(), 'decdk-'));
const stackName = 'Test';
const typeSystem = await obtainTypeSystem();

const app = new cdk.App();

const stack = new DeclarativeStack(app, stackName, {
workingDirectory,
template,
typeSystem,
});
Expand Down

0 comments on commit 3ea07a2

Please sign in to comment.