Skip to content

Commit 4ffddd4

Browse files
chore: make some tests less directory dependent (#24605)
In order to make these tests work post repo-restructure, change them to correctly use `__dirname` in order to point to relative files like assets when needed.
1 parent 294adb0 commit 4ffddd4

File tree

8 files changed

+71
-42
lines changed

8 files changed

+71
-42
lines changed

packages/@aws-cdk/aws-ecr-assets/test/tarball-asset.test.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ import { TarballImageAsset } from '../lib';
1111

1212

1313
describe('image asset', () => {
14+
const tarballFile = path.join(__dirname, 'demo-tarball', 'empty.tar');
1415
test('test instantiating Asset Image', () => {
1516
// GIVEN
1617
const app = new App();
1718
const stack = new Stack(app);
1819
const asset = new TarballImageAsset(stack, 'Image', {
19-
tarballFile: __dirname + '/demo-tarball/empty.tar',
20+
tarballFile,
2021
});
2122

2223
// WHEN
@@ -56,7 +57,7 @@ describe('image asset', () => {
5657
const stack = new Stack();
5758
const user = new iam.User(stack, 'MyUser');
5859
const asset = new TarballImageAsset(stack, 'Image', {
59-
tarballFile: 'test/demo-tarball/empty.tar',
60+
tarballFile,
6061
});
6162

6263
// WHEN
@@ -118,7 +119,7 @@ describe('image asset', () => {
118119
const app = new App();
119120
const stack = new Stack(app);
120121
const image = new TarballImageAsset(stack, 'MyAsset', {
121-
tarballFile: 'test/demo-tarball/empty.tar',
122+
tarballFile,
122123
});
123124

124125
const session = app.synth();
@@ -145,10 +146,10 @@ describe('image asset', () => {
145146
}),
146147
});
147148
const asset1 = new TarballImageAsset(stack1, 'MyAsset', {
148-
tarballFile: 'test/demo-tarball/empty.tar',
149+
tarballFile,
149150
});
150151
const asset2 = new TarballImageAsset(stack2, 'MyAsset', {
151-
tarballFile: 'test/demo-tarball/empty.tar',
152+
tarballFile,
152153
});
153154

154155
test('stack with default synthesizer', () => {

packages/@aws-cdk/aws-lambda-go/test/docker.test.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { spawnSync } from 'child_process';
22
import * as path from 'path';
33

4+
const docker = process.env.CDK_DOCKER ?? 'docker';
45
beforeAll(() => {
5-
spawnSync('docker', ['build', '-t', 'golang', path.join(__dirname, '../lib')]);
6+
spawnSync(docker, ['build', '-t', 'golang', path.join(__dirname, '../lib')]);
67
});
78

89
test('golang is available', async () => {
9-
const proc = spawnSync('docker', [
10+
const proc = spawnSync(docker, [
1011
'run', 'golang',
1112
'sh', '-c',
1213
'go version',

packages/@aws-cdk/aws-lambda-go/test/function.test.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ test('GoFunction with defaults', () => {
4343
test('GoFunction with using provided runtime', () => {
4444
// WHEN
4545
new GoFunction(stack, 'handler', {
46-
entry: 'test/lambda-handler-vendor/cmd/api',
46+
entry: path.join(__dirname, 'lambda-handler-vendor/cmd/api'),
4747
runtime: Runtime.PROVIDED,
4848
});
4949

@@ -60,7 +60,7 @@ test('GoFunction with using provided runtime', () => {
6060
test('GoFunction with using golang runtime', () => {
6161
// WHEN
6262
new GoFunction(stack, 'handler', {
63-
entry: 'test/lambda-handler-vendor/cmd/api',
63+
entry: path.join(__dirname, 'lambda-handler-vendor/cmd/api'),
6464
runtime: Runtime.GO_1_X,
6565
});
6666

@@ -77,7 +77,7 @@ test('GoFunction with using golang runtime', () => {
7777
test('GoFunction with container env vars', () => {
7878
// WHEN
7979
new GoFunction(stack, 'handler', {
80-
entry: 'test/lambda-handler-vendor/cmd/api',
80+
entry: path.join(__dirname, 'lambda-handler-vendor/cmd/api'),
8181
bundling: {
8282
environment: {
8383
KEY: 'VALUE',
@@ -94,15 +94,15 @@ test('GoFunction with container env vars', () => {
9494

9595
test('throws with the wrong runtime family', () => {
9696
expect(() => new GoFunction(stack, 'handler', {
97-
entry: 'test/lambda-handler-vendor/cmd/api',
97+
entry: path.join(__dirname, 'lambda-handler-vendor/cmd/api'),
9898
runtime: Runtime.PYTHON_3_8,
9999
})).toThrow(/Only `go` and `provided` runtimes are supported/);
100100
});
101101

102102
test('resolves entry to an absolute path', () => {
103103
// WHEN
104104
new GoFunction(stack, 'fn', {
105-
entry: 'test/lambda-handler-vendor/cmd/api/main.go',
105+
entry: path.join(__dirname, 'lambda-handler-vendor/cmd/api/main.go'),
106106
});
107107

108108
expect(Bundling.bundle).toHaveBeenCalledWith(expect.objectContaining({
@@ -112,22 +112,22 @@ test('resolves entry to an absolute path', () => {
112112

113113
test('throws with no existing go.mod file', () => {
114114
expect(() => new GoFunction(stack, 'handler', {
115-
entry: 'test/lambda-handler-vendor/cmd/api',
115+
entry: path.join(__dirname, 'lambda-handler-vendor/cmd/api'),
116116
moduleDir: '/does/not/exist/go.mod',
117117
})).toThrow(/go.mod file at \/does\/not\/exist\/go.mod doesn't exist/);
118118
});
119119

120120
test('throws with incorrect moduleDir file', () => {
121121
expect(() => new GoFunction(stack, 'handler', {
122-
entry: 'test/lambda-handler-vendor/cmd/api',
122+
entry: path.join(__dirname, 'lambda-handler-vendor/cmd/api'),
123123
moduleDir: '/does/not/exist.mod',
124124
})).toThrow(/moduleDir is specifying a file that is not go.mod/);
125125
});
126126

127127
test('custom moduleDir can be used', () => {
128128
new GoFunction(stack, 'handler', {
129-
entry: 'test/lambda-handler-vendor/cmd/api',
130-
moduleDir: 'test/lambda-handler-vendor',
129+
entry: path.join(__dirname, 'lambda-handler-vendor/cmd/api'),
130+
moduleDir: path.join(__dirname, 'lambda-handler-vendor'),
131131
});
132132

133133
Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Function', {
@@ -137,8 +137,8 @@ test('custom moduleDir can be used', () => {
137137

138138
test('custom moduleDir with file path can be used', () => {
139139
new GoFunction(stack, 'handler', {
140-
entry: 'test/lambda-handler-vendor/cmd/api',
141-
moduleDir: 'test/lambda-handler-vendor/go.mod',
140+
entry: path.join(__dirname, 'lambda-handler-vendor/cmd/api'),
141+
moduleDir: path.join(__dirname, 'lambda-handler-vendor/go.mod'),
142142
});
143143

144144
Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Function', {

packages/@aws-cdk/aws-lambda-nodejs/test/bundling.test.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as child_process from 'child_process';
2+
import * as fs from 'fs';
23
import * as os from 'os';
34
import * as path from 'path';
45
import { Architecture, Code, Runtime, RuntimeFamily } from '@aws-cdk/aws-lambda';
@@ -641,7 +642,7 @@ test('esbuild bundling with pre compilations', () => {
641642
architecture: Architecture.X86_64,
642643
});
643644

644-
const compilerOptions = util.getTsconfigCompilerOptions(path.join(__dirname, '..', 'tsconfig.json'));
645+
const compilerOptions = util.getTsconfigCompilerOptions(findParentTsConfigPath(__dirname));
645646

646647
// Correctly bundles with esbuild
647648
expect(Code.fromAsset).toHaveBeenCalledWith(path.dirname(packageLock), {
@@ -845,3 +846,14 @@ test('Custom bundling file copy variant', () => {
845846
}),
846847
});
847848
});
849+
850+
function findParentTsConfigPath(dir: string, depth: number = 1, limit: number = 5): string {
851+
const target = path.join(dir, 'tsconfig.json');
852+
if (fs.existsSync(target)) {
853+
return target;
854+
} else if (depth < limit) {
855+
return findParentTsConfigPath(path.join(dir, '..'), depth + 1, limit);
856+
}
857+
858+
throw new Error(`No \`package.json\` file found within ${depth} parent directories`);
859+
}

packages/@aws-cdk/aws-lambda-nodejs/test/docker.test.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
import { spawnSync } from 'child_process';
22
import * as path from 'path';
33

4+
const docker = process.env.CDK_DOCKER ?? 'docker';
45
beforeAll(() => {
5-
spawnSync('docker', ['build', '-t', 'esbuild', path.join(__dirname, '../lib')]);
6+
spawnSync(docker, ['build', '-t', 'esbuild', path.join(__dirname, '../lib')]);
67
});
78

89
test('esbuild is available', () => {
9-
const proc = spawnSync('docker', [
10+
const proc = spawnSync(docker, [
1011
'run', 'esbuild',
1112
'esbuild', '--version',
1213
]);
1314
expect(proc.status).toEqual(0);
1415
});
1516

1617
test('can npm install with non root user', () => {
17-
const proc = spawnSync('docker', [
18+
const proc = spawnSync(docker, [
1819
'run', '-u', '1000:1000',
1920
'esbuild',
2021
'bash', '-c', [
@@ -27,7 +28,7 @@ test('can npm install with non root user', () => {
2728
});
2829

2930
test('can yarn install with non root user', () => {
30-
const proc = spawnSync('docker', [
31+
const proc = spawnSync(docker, [
3132
'run', '-u', '500:500',
3233
'esbuild',
3334
'bash', '-c', [
@@ -40,7 +41,7 @@ test('can yarn install with non root user', () => {
4041
});
4142

4243
test('can pnpm install with non root user', () => {
43-
const proc = spawnSync('docker', [
44+
const proc = spawnSync(docker, [
4445
'run', '-u', '500:500',
4546
'esbuild',
4647
'bash', '-c', [
@@ -53,7 +54,7 @@ test('can pnpm install with non root user', () => {
5354
});
5455

5556
test('cache folders have the right permissions', () => {
56-
const proc = spawnSync('docker', [
57+
const proc = spawnSync(docker, [
5758
'run', 'esbuild',
5859
'bash', '-c', [
5960
'stat -c \'%a\' /tmp/npm-cache',

packages/@aws-cdk/aws-lambda-python/test/function.test.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ beforeEach(() => {
4747

4848
test('PythonFunction with defaults', () => {
4949
new PythonFunction(stack, 'handler', {
50-
entry: 'test/lambda-handler',
50+
entry: path.join(__dirname, 'lambda-handler'),
5151
runtime: Runtime.PYTHON_3_8,
5252
});
5353

@@ -62,7 +62,7 @@ test('PythonFunction with defaults', () => {
6262

6363
test('PythonFunction with index in a subdirectory', () => {
6464
new PythonFunction(stack, 'handler', {
65-
entry: 'test/lambda-handler-sub',
65+
entry: path.join(__dirname, 'lambda-handler-sub'),
6666
index: 'inner/custom_index.py',
6767
handler: 'custom_handler',
6868
runtime: Runtime.PYTHON_3_8,
@@ -79,7 +79,7 @@ test('PythonFunction with index in a subdirectory', () => {
7979

8080
test('PythonFunction with index in a nested subdirectory', () => {
8181
new PythonFunction(stack, 'handler', {
82-
entry: 'test/lambda-handler-sub-nested',
82+
entry: path.join(__dirname, 'lambda-handler-sub-nested'),
8383
index: 'inner/inner2/custom_index.py',
8484
handler: 'custom_handler',
8585
runtime: Runtime.PYTHON_3_8,
@@ -96,7 +96,7 @@ test('PythonFunction with index in a nested subdirectory', () => {
9696

9797
test('throws when index is not py', () => {
9898
expect(() => new PythonFunction(stack, 'Fn', {
99-
entry: 'test/lambda-handler',
99+
entry: path.join(__dirname, 'lambda-handler'),
100100
index: 'index.js',
101101
runtime: Runtime.PYTHON_3_8,
102102
})).toThrow(/Only Python \(\.py\) index files are supported/);
@@ -111,37 +111,37 @@ test('throws when entry does not exist', () => {
111111

112112
test('throws with the wrong runtime family', () => {
113113
expect(() => new PythonFunction(stack, 'handler1', {
114-
entry: 'test/lambda-handler',
114+
entry: path.join(__dirname, 'lambda-handler'),
115115
runtime: Runtime.NODEJS_14_X,
116116
})).toThrow(/Only `PYTHON` runtimes are supported/);
117117
});
118118

119119
test('allows specifying hash type', () => {
120120
new PythonFunction(stack, 'source1', {
121-
entry: 'test/lambda-handler-nodeps',
121+
entry: path.join(__dirname, 'lambda-handler-nodeps'),
122122
index: 'index.py',
123123
handler: 'handler',
124124
runtime: Runtime.PYTHON_3_8,
125125
});
126126

127127
new PythonFunction(stack, 'source2', {
128-
entry: 'test/lambda-handler-nodeps',
128+
entry: path.join(__dirname, 'lambda-handler-nodeps'),
129129
index: 'index.py',
130130
handler: 'handler',
131131
runtime: Runtime.PYTHON_3_8,
132132
bundling: { assetHashType: AssetHashType.SOURCE },
133133
});
134134

135135
new PythonFunction(stack, 'output', {
136-
entry: 'test/lambda-handler-nodeps',
136+
entry: path.join(__dirname, 'lambda-handler-nodeps'),
137137
index: 'index.py',
138138
handler: 'handler',
139139
runtime: Runtime.PYTHON_3_8,
140140
bundling: { assetHashType: AssetHashType.OUTPUT },
141141
});
142142

143143
new PythonFunction(stack, 'custom', {
144-
entry: 'test/lambda-handler-nodeps',
144+
entry: path.join(__dirname, 'lambda-handler-nodeps'),
145145
index: 'index.py',
146146
handler: 'handler',
147147
runtime: Runtime.PYTHON_3_8,

packages/@aws-cdk/aws-lambda/test/function.test.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { AdotLambdaLayerJavaSdkVersion } from '../lib/adot-layers';
2121
import { calculateFunctionHash } from '../lib/function-hash';
2222

2323
describe('function', () => {
24+
const dockerLambdaHandlerPath = path.join(__dirname, 'docker-lambda-handler');
2425
test('default function', () => {
2526
const stack = new cdk.Stack();
2627

@@ -1211,7 +1212,7 @@ describe('function', () => {
12111212
const stack = new cdk.Stack();
12121213

12131214
new lambda.Function(stack, 'MyLambda', {
1214-
code: lambda.Code.fromAssetImage(path.join(__dirname, 'docker-lambda-handler')),
1215+
code: lambda.Code.fromAssetImage(dockerLambdaHandlerPath),
12151216
handler: lambda.Handler.FROM_IMAGE,
12161217
runtime: lambda.Runtime.FROM_IMAGE,
12171218
});
@@ -2622,13 +2623,13 @@ describe('function', () => {
26222623
const stack = new cdk.Stack();
26232624

26242625
expect(() => new lambda.Function(stack, 'Fn1', {
2625-
code: lambda.Code.fromAssetImage('test/docker-lambda-handler'),
2626+
code: lambda.Code.fromAssetImage(dockerLambdaHandlerPath),
26262627
handler: lambda.Handler.FROM_IMAGE,
26272628
runtime: lambda.Runtime.FROM_IMAGE,
26282629
})).not.toThrow();
26292630

26302631
expect(() => new lambda.Function(stack, 'Fn2', {
2631-
code: lambda.Code.fromAssetImage('test/docker-lambda-handler'),
2632+
code: lambda.Code.fromAssetImage(dockerLambdaHandlerPath),
26322633
handler: 'index.handler',
26332634
runtime: lambda.Runtime.FROM_IMAGE,
26342635
})).toThrow(/handler must be.*FROM_IMAGE/);
@@ -2638,13 +2639,13 @@ describe('function', () => {
26382639
const stack = new cdk.Stack();
26392640

26402641
expect(() => new lambda.Function(stack, 'Fn1', {
2641-
code: lambda.Code.fromAssetImage('test/docker-lambda-handler'),
2642+
code: lambda.Code.fromAssetImage(dockerLambdaHandlerPath),
26422643
handler: lambda.Handler.FROM_IMAGE,
26432644
runtime: lambda.Runtime.FROM_IMAGE,
26442645
})).not.toThrow();
26452646

26462647
expect(() => new lambda.Function(stack, 'Fn2', {
2647-
code: lambda.Code.fromAssetImage('test/docker-lambda-handler'),
2648+
code: lambda.Code.fromAssetImage(dockerLambdaHandlerPath),
26482649
handler: lambda.Handler.FROM_IMAGE,
26492650
runtime: lambda.Runtime.GO_1_X,
26502651
})).toThrow(/runtime must be.*FROM_IMAGE/);
@@ -2737,7 +2738,7 @@ describe('function', () => {
27372738
});
27382739

27392740
expect(() => new lambda.DockerImageFunction(stack, 'MyLambda', {
2740-
code: lambda.DockerImageCode.fromImageAsset(path.join(__dirname, 'docker-lambda-handler')),
2741+
code: lambda.DockerImageCode.fromImageAsset(dockerLambdaHandlerPath),
27412742
layers: [layer],
27422743
})).toThrow(/Layers are not supported for container image functions/);
27432744
});
@@ -3087,7 +3088,7 @@ describe('function', () => {
30873088
expect(
30883089
() =>
30893090
new lambda.DockerImageFunction(stack, 'MyLambda', {
3090-
code: lambda.DockerImageCode.fromImageAsset(path.join(__dirname, 'docker-lambda-handler')),
3091+
code: lambda.DockerImageCode.fromImageAsset(dockerLambdaHandlerPath),
30913092
adotInstrumentation: {
30923093
layerVersion: lambda.AdotLayerVersion.fromJavaSdkLayerVersion(AdotLambdaLayerJavaSdkVersion.V1_19_0),
30933094
execWrapper: lambda.AdotLambdaExecWrapper.REGULAR_HANDLER,

0 commit comments

Comments
 (0)