Skip to content

Commit

Permalink
no regex + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jogold committed Mar 11, 2021
1 parent c95cc09 commit ac1d5c4
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 4 deletions.
14 changes: 11 additions & 3 deletions packages/@aws-cdk/aws-lambda/lib/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,19 @@ export abstract class Code {
* @param options Docker build options
*/
public static fromDockerBuild(path: string, options: DockerBuildAssetOptions = {}): AssetCode {
const imagePath = options.imagePath ?? '/asset';
let imagePath = options.imagePath ?? '/asset/.';

// ensure imagePath ends with /. to copy the **content** at this path
if (imagePath.endsWith('/')) {
imagePath = `${imagePath}.`;
} else if (!imagePath.endsWith('/.')) {
imagePath = `${imagePath}/.`;
}

const assetPath = cdk.DockerImage
.fromBuild(path, options)
// ensure imagePath ends with /. to copy the **content** at this path
.cp(imagePath.replace(/(\/|\/\.)?$/, '/.'), options.outputPath);
.cp(imagePath, options.outputPath);

return new AssetCode(assetPath);
}

Expand Down
56 changes: 55 additions & 1 deletion packages/@aws-cdk/aws-lambda/test/code.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,23 @@ describe('code', () => {
});

describe('lambda.Code.fromDockerBuild', () => {
let fromBuildMock: jest.SpyInstance<cdk.DockerImage>;
let cpMock: jest.Mock<any, any>;

beforeEach(() => {
cpMock = jest.fn().mockReturnValue(path.join(__dirname, 'docker-build-lambda'));
fromBuildMock = jest.spyOn(cdk.DockerImage, 'fromBuild').mockImplementation(() => ({
cp: cpMock,
image: 'tag',
run: jest.fn(),
toJSON: jest.fn(),
}));
});

afterEach(() => {
fromBuildMock.mockRestore();
});

test('can use the result of a Docker build as an asset', () => {
// given
const stack = new cdk.Stack();
Expand All @@ -346,10 +363,47 @@ describe('code', () => {
// then
expect(stack).toHaveResource('AWS::Lambda::Function', {
Metadata: {
[cxapi.ASSET_RESOURCE_METADATA_PATH_KEY]: 'asset.20276d7803bfd6f4a2a5dc48692eb300d676b2a0dfdd4bda78c56c7c2c461515',
[cxapi.ASSET_RESOURCE_METADATA_PATH_KEY]: 'asset.fbafdbb9ae8d1bae0def415b791a93c486d18ebc63270c748abecc3ac0ab9533',
[cxapi.ASSET_RESOURCE_METADATA_PROPERTY_KEY]: 'Code',
},
}, ResourcePart.CompleteDefinition);

expect(fromBuildMock).toHaveBeenCalledWith(path.join(__dirname, 'docker-build-lambda'), {});
expect(cpMock).toHaveBeenCalledWith('/asset/.', undefined);
});

test('fromDockerBuild appends /. to an image path not ending with a /', () => {
// given
const stack = new cdk.Stack();

// when
new lambda.Function(stack, 'Fn', {
code: lambda.Code.fromDockerBuild(path.join(__dirname, 'docker-build-lambda'), {
imagePath: '/my/image/path',
}),
handler: 'index.handler',
runtime: lambda.Runtime.NODEJS_12_X,
});

// then
expect(cpMock).toHaveBeenCalledWith('/my/image/path/.', undefined);
});

test('fromDockerBuild appends . to an image path ending with a /', () => {
// given
const stack = new cdk.Stack();

// when
new lambda.Function(stack, 'Fn', {
code: lambda.Code.fromDockerBuild(path.join(__dirname, 'docker-build-lambda'), {
imagePath: '/my/image/path/',
}),
handler: 'index.handler',
runtime: lambda.Runtime.NODEJS_12_X,
});

// then
expect(cpMock).toHaveBeenCalledWith('/my/image/path/.', undefined);
});
});
});
Expand Down

0 comments on commit ac1d5c4

Please sign in to comment.