From a30fd3be826e3d4ef1e59e14c34f6f1c9a65d909 Mon Sep 17 00:00:00 2001 From: kaizen3031593 Date: Mon, 16 Aug 2021 17:13:39 -0400 Subject: [PATCH 1/2] throw descriptive error when no stacks match input for 'cdk diff' and 'cdk deploy' --- packages/aws-cdk/lib/cdk-toolkit.ts | 10 +++++++--- packages/aws-cdk/test/cdk-toolkit.test.ts | 8 ++++++++ packages/aws-cdk/test/diff.test.ts | 10 ++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/packages/aws-cdk/lib/cdk-toolkit.ts b/packages/aws-cdk/lib/cdk-toolkit.ts index 55ca1c6e24903..5c1e3261bcd53 100644 --- a/packages/aws-cdk/lib/cdk-toolkit.ts +++ b/packages/aws-cdk/lib/cdk-toolkit.ts @@ -397,7 +397,7 @@ export class CdkToolkit { defaultBehavior: DefaultSelection.OnlySingle, }); - await this.validateStacks(stacks); + await this.validateStacks(stacks, selector.patterns); return stacks; } @@ -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; } @@ -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){ + throw new Error(`No stacks match the name(s) ${stackNames}`); + } + stacks.processMetadataMessages({ ignoreErrors: this.props.ignoreErrors, strict: this.props.strict, diff --git a/packages/aws-cdk/test/cdk-toolkit.test.ts b/packages/aws-cdk/test/cdk-toolkit.test.ts index 8f53405b749e4..605e0fabd08fd 100644 --- a/packages/aws-cdk/test/cdk-toolkit.test.ts +++ b/packages/aws-cdk/test/cdk-toolkit.test.ts @@ -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 diff --git a/packages/aws-cdk/test/diff.test.ts b/packages/aws-cdk/test/diff.test.ts index c1bcd11c78caa..e14ebe9bd9779 100644 --- a/packages/aws-cdk/test/diff.test.ts +++ b/packages/aws-cdk/test/diff.test.ts @@ -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; From d379e8e06124485b7d2d49f33f9ded8c991c2175 Mon Sep 17 00:00:00 2001 From: kaizen3031593 Date: Mon, 16 Aug 2021 17:19:10 -0400 Subject: [PATCH 2/2] fix linter --- packages/aws-cdk/lib/cdk-toolkit.ts | 2 +- packages/aws-cdk/test/cdk-toolkit.test.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/aws-cdk/lib/cdk-toolkit.ts b/packages/aws-cdk/lib/cdk-toolkit.ts index 5c1e3261bcd53..8edefe66fc333 100644 --- a/packages/aws-cdk/lib/cdk-toolkit.ts +++ b/packages/aws-cdk/lib/cdk-toolkit.ts @@ -436,7 +436,7 @@ export class CdkToolkit { * Validate the stacks for errors and warnings according to the CLI's current settings */ private async validateStacks(stacks: StackCollection, stackNames: string[]) { - if (stacks.stackCount == 0){ + if (stacks.stackCount == 0) { throw new Error(`No stacks match the name(s) ${stackNames}`); } diff --git a/packages/aws-cdk/test/cdk-toolkit.test.ts b/packages/aws-cdk/test/cdk-toolkit.test.ts index 605e0fabd08fd..f4573637ee5f4 100644 --- a/packages/aws-cdk/test/cdk-toolkit.test.ts +++ b/packages/aws-cdk/test/cdk-toolkit.test.ts @@ -42,7 +42,7 @@ 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'); });