Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): 'deploy' and 'diff' silently does nothing when given unknown stack name #16073

Merged
merged 3 commits into from
Aug 18, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions packages/aws-cdk/lib/cdk-toolkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ export class CdkToolkit {
defaultBehavior: DefaultSelection.OnlySingle,
});

await this.validateStacks(stacks);
await this.validateStacks(stacks, selector.patterns);

return stacks;
}
Expand All @@ -415,7 +415,7 @@ export class CdkToolkit {
? allStacks.filter(art => art.validateOnSynth ?? false)
: new StackCollection(assembly, []);

await this.validateStacks(selectedForDiff.concat(autoValidateStacks));
await this.validateStacks(selectedForDiff.concat(autoValidateStacks), stackNames);

return selectedForDiff;
}
Expand All @@ -435,7 +435,11 @@ export class CdkToolkit {
/**
* Validate the stacks for errors and warnings according to the CLI's current settings
*/
private async validateStacks(stacks: StackCollection) {
private async validateStacks(stacks: StackCollection, stackNames: string[]) {
if (stacks.stackCount == 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with this check, but I think it should not be part of this function. Validating that there are stacks is not what this function is doing (it's checking stack metadata).

How about adding a new function?

function validateStacksSelected(stacks: StackCollection, stackNames: string[]);

Or something?

throw new Error(`No stacks match the name(s) ${stackNames}`);
}

stacks.processMetadataMessages({
ignoreErrors: this.props.ignoreErrors,
strict: this.props.strict,
Expand Down
8 changes: 8 additions & 0 deletions packages/aws-cdk/test/cdk-toolkit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ function defaultToolkitSetup() {
}

describe('deploy', () => {
test('fails when no valid stack names are given', async () => {
// GIVEN
const toolkit = defaultToolkitSetup();

// WHEN
await expect(() => toolkit.deploy({ selector: { patterns: ['Test-Stack-D'] } })).rejects.toThrow('No stacks match the name(s) Test-Stack-D');
});

describe('makes correct CloudFormation calls', () => {
test('without options', async () => {
// GIVEN
Expand Down
10 changes: 10 additions & 0 deletions packages/aws-cdk/test/diff.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ test('throws an error during diffs on stack with error metadata', async () => {
})).rejects.toThrow(/Found errors/);
});

test('throws an error if no valid stack names given', async () => {
const buffer = new StringWritable();

// WHEN
await expect(() => toolkit.diff({
stackNames: ['X', 'Y', 'Z'],
stream: buffer,
})).rejects.toThrow('No stacks match the name(s) X,Y,Z');
});

class StringWritable extends Writable {
public data: string;
private readonly _decoder: NodeStringDecoder;
Expand Down