diff --git a/packages/cdk-assets/lib/private/docker.ts b/packages/cdk-assets/lib/private/docker.ts index 8f9ab535d1836..8c563b8d59923 100644 --- a/packages/cdk-assets/lib/private/docker.ts +++ b/packages/cdk-assets/lib/private/docker.ts @@ -37,9 +37,9 @@ export class Docker { '--tag', options.tag, ...options.target ? ['--target', options.target] : [], ...options.file ? ['--file', options.file] : [], - options.directory, + '.' ]; - await this.execute(buildCommand); + await this.execute(buildCommand, { cwd: options.directory }); } /** @@ -100,4 +100,4 @@ async function obtainEcrCredentials(ecr: AWS.ECR, logger?: Logger) { function flatten(x: string[][]) { return Array.prototype.concat([], ...x); -} \ No newline at end of file +} diff --git a/packages/cdk-assets/test/docker-images.test.ts b/packages/cdk-assets/test/docker-images.test.ts index b20c5636f5c18..f4ee947cd7823 100644 --- a/packages/cdk-assets/test/docker-images.test.ts +++ b/packages/cdk-assets/test/docker-images.test.ts @@ -7,6 +7,7 @@ import { mockAws, mockedApiFailure, mockedApiResult } from './mock-aws'; import { mockSpawn } from './mock-child_process'; let aws: ReturnType; +const absoluteDockerPath = '/simple/cdk.out/dockerdir'; beforeEach(() => { mockfs({ '/simple/cdk.out/assets.json': JSON.stringify({ @@ -33,7 +34,7 @@ beforeEach(() => { dockerImages: { theAsset: { source: { - directory: '/simple/cdk.out/dockerdir' + directory: absoluteDockerPath }, destinations: { theDestination: { @@ -112,7 +113,7 @@ describe('with a complete manifest', () => { mockSpawn( { commandLine: ['docker', 'login', '--username', 'user', '--password-stdin', 'https://proxy.com/'] }, { commandLine: ['docker', 'inspect', 'cdkasset-theasset'], exitCode: 1 }, - { commandLine: ['docker', 'build', '--tag', 'cdkasset-theasset', '/simple/cdk.out/dockerdir'] }, + { commandLine: ['docker', 'build', '--tag', 'cdkasset-theasset', '.'], cwd: absoluteDockerPath }, { commandLine: ['docker', 'tag', 'cdkasset-theasset', '12345.amazonaws.com/repo:abcdef'] }, { commandLine: ['docker', 'push', '12345.amazonaws.com/repo:abcdef'] }, ); @@ -135,7 +136,7 @@ test('correctly identify Docker directory if path is absolute', async () => { // Only care about the 'build' command line { commandLine: ['docker', 'login'], prefix: true, }, { commandLine: ['docker', 'inspect'], exitCode: 1, prefix: true }, - { commandLine: ['docker', 'build', '--tag', 'cdkasset-theasset', '/simple/cdk.out/dockerdir'] }, + { commandLine: ['docker', 'build', '--tag', 'cdkasset-theasset', '.'], cwd: absoluteDockerPath }, { commandLine: ['docker', 'tag'], prefix: true }, { commandLine: ['docker', 'push'], prefix: true }, ); diff --git a/packages/cdk-assets/test/mock-child_process.ts b/packages/cdk-assets/test/mock-child_process.ts index 78f3341b62db2..e7ab39934349e 100644 --- a/packages/cdk-assets/test/mock-child_process.ts +++ b/packages/cdk-assets/test/mock-child_process.ts @@ -7,6 +7,7 @@ if (!(child_process as any).spawn.mockImplementationOnce) { export interface Invocation { commandLine: string[]; + cwd?: string; exitCode?: number; stdout?: string; @@ -20,7 +21,7 @@ export function mockSpawn(...invocations: Invocation[]) { let mock = (child_process.spawn as any); for (const _invocation of invocations) { const invocation = _invocation; // Mirror into variable for closure - mock = mock.mockImplementationOnce((binary: string, args: string[], _options: any) => { + mock = mock.mockImplementationOnce((binary: string, args: string[], options: child_process.SpawnOptions) => { if (invocation.prefix) { // Match command line prefix expect([binary, ...args].slice(0, invocation.commandLine.length)).toEqual(invocation.commandLine); @@ -29,6 +30,10 @@ export function mockSpawn(...invocations: Invocation[]) { expect([binary, ...args]).toEqual(invocation.commandLine); } + if (invocation.cwd != null) { + expect(options.cwd).toBe(invocation.cwd); + } + const child: any = new events.EventEmitter(); child.stdin = new events.EventEmitter(); child.stdin.write = jest.fn(); @@ -57,4 +62,4 @@ function mockEmit(emitter: events.EventEmitter, event: string, data: any) { setImmediate(() => { emitter.emit(event, data); }); -} \ No newline at end of file +}