Skip to content

Commit

Permalink
fix(core): make bundlingRequired minimatch matching the same as aws-cdk
Browse files Browse the repository at this point in the history
The original fix as per 5cff2d9, with the addition of bypassing
minimatch when `*` is the given pattern (return true). This gives
special meaning to `*`, since `*` in minimatch doesn't produce the
desired selection (all stacks).

fixes #19927
  • Loading branch information
stevehodgkiss committed Jul 20, 2022
1 parent d7ac3bb commit 5556b60
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 3 deletions.
9 changes: 6 additions & 3 deletions packages/@aws-cdk/core/lib/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1168,10 +1168,13 @@ export class Stack extends Construct implements ITaggable {
public get bundlingRequired() {
const bundlingStacks: string[] = this.node.tryGetContext(cxapi.BUNDLING_STACKS) ?? ['*'];

// bundlingStacks is of the form `Stage/Stack`, convert it to `Stage-Stack` before comparing to stack name
if (bundlingStacks.length === 1 && bundlingStacks[0] === '*') {
return true; // bundle all stacks if a stack pattern isn't supplied on the CLI
}

return bundlingStacks.some(pattern => minimatch(
this.stackName,
pattern.replace('/', '-'),
this.node.path, // use the same value for pattern matching as the aws-cdk CLI (displayName / hierarchicalId)
pattern,
));
}
}
Expand Down
75 changes: 75 additions & 0 deletions packages/@aws-cdk/core/test/staging.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,81 @@ describe('staging', () => {
expect(dockerStubInput).not.toMatch(DockerStubCommand.MULTIPLE_FILES);
});

test('correctly skips bundling with stack under stage and custom stack name', () => {
// GIVEN
const app = new App();

const stage = new Stage(app, 'Stage');
stage.node.setContext(cxapi.BUNDLING_STACKS, ['Stage/Stack1']);

const stack1 = new Stack(stage, 'Stack1', { stackName: 'unrelated-stack1-name' });
const stack2 = new Stack(stage, 'Stack2', { stackName: 'unrelated-stack2-name' });
const directory = path.join(__dirname, 'fs', 'fixtures', 'test1');

// WHEN
new AssetStaging(stack1, 'Asset', {
sourcePath: directory,
assetHashType: AssetHashType.OUTPUT,
bundling: {
image: DockerImage.fromRegistry('alpine'),
command: [DockerStubCommand.SUCCESS],
},
});

new AssetStaging(stack2, 'Asset', {
sourcePath: directory,
assetHashType: AssetHashType.OUTPUT,
bundling: {
image: DockerImage.fromRegistry('alpine'),
command: [DockerStubCommand.MULTIPLE_FILES],
},
});

// THEN
const dockerStubInput = readDockerStubInputConcat();
// Docker ran for the asset in Stack1
expect(dockerStubInput).toMatch(DockerStubCommand.SUCCESS);
// Docker did not run for the asset in Stack2
expect(dockerStubInput).not.toMatch(DockerStubCommand.MULTIPLE_FILES);
});

test('correctly bundles with stack under stage and the default stack pattern', () => {
// GIVEN
const app = new App();

const stage = new Stage(app, 'Stage');

const stack1 = new Stack(stage, 'Stack1');
const stack2 = new Stack(stage, 'Stack2');
const directory = path.join(__dirname, 'fs', 'fixtures', 'test1');

// WHEN
new AssetStaging(stack1, 'Asset', {
sourcePath: directory,
assetHashType: AssetHashType.OUTPUT,
bundling: {
image: DockerImage.fromRegistry('alpine'),
command: [DockerStubCommand.SUCCESS],
},
});

new AssetStaging(stack2, 'Asset', {
sourcePath: directory,
assetHashType: AssetHashType.OUTPUT,
bundling: {
image: DockerImage.fromRegistry('alpine'),
command: [DockerStubCommand.MULTIPLE_FILES],
},
});

// THEN
const dockerStubInput = readDockerStubInputConcat();
// Docker ran for the asset in Stack1
expect(dockerStubInput).toMatch(DockerStubCommand.SUCCESS);
// Docker ran for the asset in Stack2
expect(dockerStubInput).toMatch(DockerStubCommand.MULTIPLE_FILES);
});

test('bundling still occurs with partial wildcard', () => {
// GIVEN
const app = new App();
Expand Down

0 comments on commit 5556b60

Please sign in to comment.