Skip to content

Commit ac1d5c4

Browse files
committed
no regex + tests
1 parent c95cc09 commit ac1d5c4

File tree

2 files changed

+66
-4
lines changed

2 files changed

+66
-4
lines changed

packages/@aws-cdk/aws-lambda/lib/code.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,19 @@ export abstract class Code {
6767
* @param options Docker build options
6868
*/
6969
public static fromDockerBuild(path: string, options: DockerBuildAssetOptions = {}): AssetCode {
70-
const imagePath = options.imagePath ?? '/asset';
70+
let imagePath = options.imagePath ?? '/asset/.';
71+
72+
// ensure imagePath ends with /. to copy the **content** at this path
73+
if (imagePath.endsWith('/')) {
74+
imagePath = `${imagePath}.`;
75+
} else if (!imagePath.endsWith('/.')) {
76+
imagePath = `${imagePath}/.`;
77+
}
78+
7179
const assetPath = cdk.DockerImage
7280
.fromBuild(path, options)
73-
// ensure imagePath ends with /. to copy the **content** at this path
74-
.cp(imagePath.replace(/(\/|\/\.)?$/, '/.'), options.outputPath);
81+
.cp(imagePath, options.outputPath);
82+
7583
return new AssetCode(assetPath);
7684
}
7785

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

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,23 @@ describe('code', () => {
331331
});
332332

333333
describe('lambda.Code.fromDockerBuild', () => {
334+
let fromBuildMock: jest.SpyInstance<cdk.DockerImage>;
335+
let cpMock: jest.Mock<any, any>;
336+
337+
beforeEach(() => {
338+
cpMock = jest.fn().mockReturnValue(path.join(__dirname, 'docker-build-lambda'));
339+
fromBuildMock = jest.spyOn(cdk.DockerImage, 'fromBuild').mockImplementation(() => ({
340+
cp: cpMock,
341+
image: 'tag',
342+
run: jest.fn(),
343+
toJSON: jest.fn(),
344+
}));
345+
});
346+
347+
afterEach(() => {
348+
fromBuildMock.mockRestore();
349+
});
350+
334351
test('can use the result of a Docker build as an asset', () => {
335352
// given
336353
const stack = new cdk.Stack();
@@ -346,10 +363,47 @@ describe('code', () => {
346363
// then
347364
expect(stack).toHaveResource('AWS::Lambda::Function', {
348365
Metadata: {
349-
[cxapi.ASSET_RESOURCE_METADATA_PATH_KEY]: 'asset.20276d7803bfd6f4a2a5dc48692eb300d676b2a0dfdd4bda78c56c7c2c461515',
366+
[cxapi.ASSET_RESOURCE_METADATA_PATH_KEY]: 'asset.fbafdbb9ae8d1bae0def415b791a93c486d18ebc63270c748abecc3ac0ab9533',
350367
[cxapi.ASSET_RESOURCE_METADATA_PROPERTY_KEY]: 'Code',
351368
},
352369
}, ResourcePart.CompleteDefinition);
370+
371+
expect(fromBuildMock).toHaveBeenCalledWith(path.join(__dirname, 'docker-build-lambda'), {});
372+
expect(cpMock).toHaveBeenCalledWith('/asset/.', undefined);
373+
});
374+
375+
test('fromDockerBuild appends /. to an image path not ending with a /', () => {
376+
// given
377+
const stack = new cdk.Stack();
378+
379+
// when
380+
new lambda.Function(stack, 'Fn', {
381+
code: lambda.Code.fromDockerBuild(path.join(__dirname, 'docker-build-lambda'), {
382+
imagePath: '/my/image/path',
383+
}),
384+
handler: 'index.handler',
385+
runtime: lambda.Runtime.NODEJS_12_X,
386+
});
387+
388+
// then
389+
expect(cpMock).toHaveBeenCalledWith('/my/image/path/.', undefined);
390+
});
391+
392+
test('fromDockerBuild appends . to an image path ending with a /', () => {
393+
// given
394+
const stack = new cdk.Stack();
395+
396+
// when
397+
new lambda.Function(stack, 'Fn', {
398+
code: lambda.Code.fromDockerBuild(path.join(__dirname, 'docker-build-lambda'), {
399+
imagePath: '/my/image/path/',
400+
}),
401+
handler: 'index.handler',
402+
runtime: lambda.Runtime.NODEJS_12_X,
403+
});
404+
405+
// then
406+
expect(cpMock).toHaveBeenCalledWith('/my/image/path/.', undefined);
353407
});
354408
});
355409
});

0 commit comments

Comments
 (0)