From ca3372db456e490a0a7b6946cbd2a26a648d927a Mon Sep 17 00:00:00 2001 From: kaizen3031593 Date: Mon, 27 Sep 2021 17:24:52 -0400 Subject: [PATCH 1/9] initial casing for hasresourceproperties --- packages/@aws-cdk/assertions/lib/match.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/@aws-cdk/assertions/lib/match.ts b/packages/@aws-cdk/assertions/lib/match.ts index 4fea0ed0f713e..70d5c4a332234 100644 --- a/packages/@aws-cdk/assertions/lib/match.ts +++ b/packages/@aws-cdk/assertions/lib/match.ts @@ -79,6 +79,10 @@ export abstract class Match { public static anyValue(): Matcher { return new AnyMatch('anyValue'); } + + private static propertyMatch(pattern: {[key: string]: any}): Matcher { + return new ObjectMatch('propertyMatch', pattern, { hasResourceProperties: true }); + } } /** @@ -220,6 +224,13 @@ interface ObjectMatchOptions { * @default true */ readonly partial?: boolean; + + /** + * Whether the ObjectMatch originates from the `hasResourceProperties()` method. + * Used for special casing. + * @default false + */ + readonly hasResourceProperties?: boolean; } /** From 47e52db96f6b87159bc8d749f796b4e5de0ad6bd Mon Sep 17 00:00:00 2001 From: kaizen3031593 Date: Mon, 27 Sep 2021 19:19:19 -0400 Subject: [PATCH 2/9] fix bug with hasResourceProperties --- packages/@aws-cdk/assertions/lib/match.ts | 11 ---- .../assertions/lib/private/resources.ts | 14 ++++- packages/@aws-cdk/assertions/lib/template.ts | 13 ++++- .../@aws-cdk/assertions/test/template.test.ts | 57 +++++++++++++++++++ 4 files changed, 79 insertions(+), 16 deletions(-) diff --git a/packages/@aws-cdk/assertions/lib/match.ts b/packages/@aws-cdk/assertions/lib/match.ts index 70d5c4a332234..4fea0ed0f713e 100644 --- a/packages/@aws-cdk/assertions/lib/match.ts +++ b/packages/@aws-cdk/assertions/lib/match.ts @@ -79,10 +79,6 @@ export abstract class Match { public static anyValue(): Matcher { return new AnyMatch('anyValue'); } - - private static propertyMatch(pattern: {[key: string]: any}): Matcher { - return new ObjectMatch('propertyMatch', pattern, { hasResourceProperties: true }); - } } /** @@ -224,13 +220,6 @@ interface ObjectMatchOptions { * @default true */ readonly partial?: boolean; - - /** - * Whether the ObjectMatch originates from the `hasResourceProperties()` method. - * Used for special casing. - * @default false - */ - readonly hasResourceProperties?: boolean; } /** diff --git a/packages/@aws-cdk/assertions/lib/private/resources.ts b/packages/@aws-cdk/assertions/lib/private/resources.ts index 81b77346f61f5..b2d279922441c 100644 --- a/packages/@aws-cdk/assertions/lib/private/resources.ts +++ b/packages/@aws-cdk/assertions/lib/private/resources.ts @@ -4,6 +4,7 @@ import { formatFailure, matchSection } from './section'; // Partial type for CloudFormation Resource type Resource = { Type: string; + Properties?: {}; } export function findResources(inspector: StackInspector, type: string, props: any = {}): { [key: string]: { [key: string]: any } } { @@ -17,9 +18,9 @@ export function findResources(inspector: StackInspector, type: string, props: an return result.matches; } -export function hasResource(inspector: StackInspector, type: string, props: any): string | void { +export function hasResource(inspector: StackInspector, type: string, props: any, addProperties?: boolean): string | void { const section: { [key: string]: Resource } = inspector.value.Resources; - const result = matchSection(filterType(section, type), props); + const result = matchSection(addProperties ? addEmptyProperties(filterType(section, type)) : filterType(section, type), props); if (result.match) { return; @@ -35,6 +36,15 @@ export function hasResource(inspector: StackInspector, type: string, props: any) ].join('\n'); } +function addEmptyProperties(sections: { [key: string]: Resource}): { [key: string]: Resource } { + Object.keys(sections).map((key) => { + if (!sections[key].Properties) { + sections[key].Properties = {}; + } + }); + return sections; +} + function filterType(section: { [key: string]: Resource }, type: string): { [key: string]: Resource } { return Object.entries(section ?? {}) .filter(([_, v]) => v.Type === type) diff --git a/packages/@aws-cdk/assertions/lib/template.ts b/packages/@aws-cdk/assertions/lib/template.ts index e6f721d928576..78e61b698c93d 100644 --- a/packages/@aws-cdk/assertions/lib/template.ts +++ b/packages/@aws-cdk/assertions/lib/template.ts @@ -73,10 +73,17 @@ export class Template { * @param type the resource type; ex: `AWS::S3::Bucket` * @param props the 'Properties' section of the resource as should be expected in the template. */ - public hasResourceProperties(type: string, props: any): void { - this.hasResource(type, Match.objectLike({ + public hasResourceProperties(type: string, props: any): void { + // const addProperties = !(props instanceof AbsentMatch); + // Add the above line in place of below when this is merged: https://github.com/aws/aws-cdk/pull/16653 + const addProperties = true; + + const matchError = hasResource(this.inspector, type, Match.objectLike({ Properties: Matcher.isMatcher(props) ? props : Match.objectLike(props), - })); + }), addProperties); + if (matchError) { + throw new Error(matchError); + } } /** diff --git a/packages/@aws-cdk/assertions/test/template.test.ts b/packages/@aws-cdk/assertions/test/template.test.ts index 7c3221763446c..711346dfef54a 100644 --- a/packages/@aws-cdk/assertions/test/template.test.ts +++ b/packages/@aws-cdk/assertions/test/template.test.ts @@ -269,6 +269,63 @@ describe('Template', () => { }); }); + describe('hasResourceProperties', () => { + test('exact match', () => { + const stack = new Stack(); + new CfnResource(stack, 'Foo', { + type: 'Foo::Bar', + properties: { baz: 'qux' }, + }); + + const inspect = Template.fromStack(stack); + inspect.hasResourceProperties('Foo::Bar', { baz: 'qux' }); + + expect(() => inspect.hasResourceProperties('Foo::Bar', { baz: 'waldo' })) + .toThrow(/Expected waldo but received qux at \/Properties\/baz/); + + expect(() => inspect.hasResourceProperties('Foo::Bar', { baz: 'qux', fred: 'waldo' })) + .toThrow(/Missing key at \/Properties\/fred/); + }); + + test('absent with properties', () => { + const stack = new Stack(); + new CfnResource(stack, 'Foo', { + type: 'Foo::Bar', + properties: { baz: 'qux' }, + }); + + const inspect = Template.fromStack(stack); + inspect.hasResourceProperties('Foo::Bar', { bar: Match.absentProperty() }); + + expect(() => inspect.hasResourceProperties('Foo::Bar', { baz: Match.absentProperty() })) + .toThrow(/Key should be absent at \/Properties\/baz/); + }); + + test('absent with no properties', () => { + const stack = new Stack(); + new CfnResource(stack, 'Foo', { + type: 'Foo::Bar', + }); + + const inspect = Template.fromStack(stack); + inspect.hasResourceProperties('Foo::Bar', { bar: Match.absentProperty() }); + expect(() => inspect.hasResourceProperties('Foo::Bar', { bar: Match.absentProperty(), baz: 'qux' })) + .toThrow(/Missing key at \/Properties\/baz/); + // Add the below line when this is merged: https://github.com/aws/aws-cdk/pull/16653 + // expect(inspect.hasResourceProperties('Foo::Bar', Match.absent())); + }); + + test('match with not', () => { + const stack = new Stack(); + new CfnResource(stack, 'Foo', { + type: 'Foo::Bar', + }); + + const inspect = Template.fromStack(stack); + expect(inspect.hasResourceProperties('Foo::Bar', Match.not({ baz: 'qux'}))); + }); + }); + describe('getResources', () => { test('matching resource type', () => { const stack = new Stack(); From 7a414a4946ed5fa9c0b0b8b50048695065039a81 Mon Sep 17 00:00:00 2001 From: kaizen3031593 Date: Mon, 27 Sep 2021 19:23:26 -0400 Subject: [PATCH 3/9] linter --- packages/@aws-cdk/assertions/lib/template.ts | 2 +- packages/@aws-cdk/assertions/test/template.test.ts | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/@aws-cdk/assertions/lib/template.ts b/packages/@aws-cdk/assertions/lib/template.ts index 78e61b698c93d..4174ade035ad8 100644 --- a/packages/@aws-cdk/assertions/lib/template.ts +++ b/packages/@aws-cdk/assertions/lib/template.ts @@ -73,7 +73,7 @@ export class Template { * @param type the resource type; ex: `AWS::S3::Bucket` * @param props the 'Properties' section of the resource as should be expected in the template. */ - public hasResourceProperties(type: string, props: any): void { + public hasResourceProperties(type: string, props: any): void { // const addProperties = !(props instanceof AbsentMatch); // Add the above line in place of below when this is merged: https://github.com/aws/aws-cdk/pull/16653 const addProperties = true; diff --git a/packages/@aws-cdk/assertions/test/template.test.ts b/packages/@aws-cdk/assertions/test/template.test.ts index 711346dfef54a..f17e8d794061a 100644 --- a/packages/@aws-cdk/assertions/test/template.test.ts +++ b/packages/@aws-cdk/assertions/test/template.test.ts @@ -304,25 +304,26 @@ describe('Template', () => { test('absent with no properties', () => { const stack = new Stack(); new CfnResource(stack, 'Foo', { - type: 'Foo::Bar', + type: 'Foo::Bar', }); const inspect = Template.fromStack(stack); inspect.hasResourceProperties('Foo::Bar', { bar: Match.absentProperty() }); + expect(() => inspect.hasResourceProperties('Foo::Bar', { bar: Match.absentProperty(), baz: 'qux' })) .toThrow(/Missing key at \/Properties\/baz/); - // Add the below line when this is merged: https://github.com/aws/aws-cdk/pull/16653 + // Add the below line when this is merged: https://github.com/aws/aws-cdk/pull/16653 // expect(inspect.hasResourceProperties('Foo::Bar', Match.absent())); }); test('match with not', () => { const stack = new Stack(); new CfnResource(stack, 'Foo', { - type: 'Foo::Bar', + type: 'Foo::Bar', }); const inspect = Template.fromStack(stack); - expect(inspect.hasResourceProperties('Foo::Bar', Match.not({ baz: 'qux'}))); + expect(inspect.hasResourceProperties('Foo::Bar', Match.not({ baz: 'qux' }))); }); }); From aa9283451ca1400beb5a3a76fd85a92433b971d1 Mon Sep 17 00:00:00 2001 From: kaizen3031593 Date: Tue, 5 Oct 2021 14:09:36 -0400 Subject: [PATCH 4/9] address CR review --- .../assertions/lib/private/resources.ts | 28 ++++++++++++++----- packages/@aws-cdk/assertions/lib/template.ts | 10 ++----- .../@aws-cdk/assertions/test/template.test.ts | 4 +-- 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/packages/@aws-cdk/assertions/lib/private/resources.ts b/packages/@aws-cdk/assertions/lib/private/resources.ts index 7ed78fa70232e..57ff8c8b89e79 100644 --- a/packages/@aws-cdk/assertions/lib/private/resources.ts +++ b/packages/@aws-cdk/assertions/lib/private/resources.ts @@ -1,3 +1,4 @@ +import { Matcher } from '..'; import { formatFailure, matchSection } from './section'; import { Resource, Template } from './template'; @@ -30,13 +31,13 @@ export function hasResource(template: Template, type: string, props: any): strin ].join('\n'); } -function addEmptyProperties(sections: { [key: string]: Resource}): { [key: string]: Resource } { - Object.keys(sections).map((key) => { - if (!sections[key].Properties) { - sections[key].Properties = {}; - } - }); - return sections; +export function hasResourceProperties(template: Template, type: string, props: any): string | void { + let amended = template; //JSON.parse(JSON.stringify(template)); + if (!Matcher.isMatcher(props)) { + amended = addEmptyProperties(template); + } + return hasResource(amended, type, props); +} export function countResources(template: Template, type: string): number { const section = template.Resources; @@ -45,6 +46,19 @@ export function countResources(template: Template, type: string): number { return Object.entries(types).length; } +function addEmptyProperties(template: Template): Template { + let resources = template.Resources; + console.log(template); + Object.keys(resources).map((key) => { + if (!resources[key].hasOwnProperty('Properties')) { + console.log('here'); + resources[key].Properties = {}; + } + }); + console.log(template); + return template; +} + function filterType(section: { [key: string]: Resource }, type: string): { [key: string]: Resource } { return Object.entries(section ?? {}) .filter(([_, v]) => v.Type === type) diff --git a/packages/@aws-cdk/assertions/lib/template.ts b/packages/@aws-cdk/assertions/lib/template.ts index 55ef9efe82418..01e0d3376dc8c 100644 --- a/packages/@aws-cdk/assertions/lib/template.ts +++ b/packages/@aws-cdk/assertions/lib/template.ts @@ -3,7 +3,7 @@ import { Match } from './match'; import { Matcher } from './matcher'; import { findMappings, hasMapping } from './private/mappings'; import { findOutputs, hasOutput } from './private/outputs'; -import { countResources, findResources, hasResource } from './private/resources'; +import { countResources, findResources, hasResource, hasResourceProperties } from './private/resources'; import { Template as TemplateType } from './private/template'; /** @@ -74,13 +74,7 @@ export class Template { * @param props the 'Properties' section of the resource as should be expected in the template. */ public hasResourceProperties(type: string, props: any): void { - // const addProperties = !(props instanceof AbsentMatch); - // Add the above line in place of below when this is merged: https://github.com/aws/aws-cdk/pull/16653 - const addProperties = true; - - const matchError = hasResource(this.inspector, type, Match.objectLike({ - Properties: Matcher.isMatcher(props) ? props : Match.objectLike(props), - }), addProperties); + const matchError = hasResourceProperties(this.template, type, props); if (matchError) { throw new Error(matchError); } diff --git a/packages/@aws-cdk/assertions/test/template.test.ts b/packages/@aws-cdk/assertions/test/template.test.ts index d3b1dfbbfd594..a602289f84e2d 100644 --- a/packages/@aws-cdk/assertions/test/template.test.ts +++ b/packages/@aws-cdk/assertions/test/template.test.ts @@ -281,7 +281,7 @@ describe('Template', () => { inspect.hasResourceProperties('Foo::Bar', { baz: 'qux' }); expect(() => inspect.hasResourceProperties('Foo::Bar', { baz: 'waldo' })) - .toThrow(/Expected waldo but received qux at \/Properties\/baz/); + .toThrow(/Missing key at \/baz/); expect(() => inspect.hasResourceProperties('Foo::Bar', { baz: 'qux', fred: 'waldo' })) .toThrow(/Missing key at \/Properties\/fred/); @@ -311,7 +311,7 @@ describe('Template', () => { inspect.hasResourceProperties('Foo::Bar', { bar: Match.absentProperty() }); expect(() => inspect.hasResourceProperties('Foo::Bar', { bar: Match.absentProperty(), baz: 'qux' })) - .toThrow(/Missing key at \/Properties\/baz/); + .toThrow(/Missing key at \/baz/); // Add the below line when this is merged: https://github.com/aws/aws-cdk/pull/16653 // expect(inspect.hasResourceProperties('Foo::Bar', Match.absent())); }); From b6ca61cb5aabaf0fc18b51fdabea98897edff0db Mon Sep 17 00:00:00 2001 From: kaizen3031593 Date: Tue, 5 Oct 2021 16:45:51 -0400 Subject: [PATCH 5/9] fix bug regarding deep vs shallow copy of amended --- .../assertions/lib/private/resources.ts | 29 ++++++++++--------- .../@aws-cdk/assertions/test/template.test.ts | 10 +++---- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/packages/@aws-cdk/assertions/lib/private/resources.ts b/packages/@aws-cdk/assertions/lib/private/resources.ts index 57ff8c8b89e79..5ba77f4980878 100644 --- a/packages/@aws-cdk/assertions/lib/private/resources.ts +++ b/packages/@aws-cdk/assertions/lib/private/resources.ts @@ -1,4 +1,4 @@ -import { Matcher } from '..'; +import { Match, Matcher } from '..'; import { formatFailure, matchSection } from './section'; import { Resource, Template } from './template'; @@ -16,7 +16,6 @@ export function findResources(template: Template, type: string, props: any = {}) export function hasResource(template: Template, type: string, props: any): string | void { const section = template.Resources; const result = matchSection(filterType(section, type), props); - if (result.match) { return; } @@ -32,11 +31,16 @@ export function hasResource(template: Template, type: string, props: any): strin } export function hasResourceProperties(template: Template, type: string, props: any): string | void { - let amended = template; //JSON.parse(JSON.stringify(template)); - if (!Matcher.isMatcher(props)) { - amended = addEmptyProperties(template); + // amended needs to be a deep copy to avoid modifying the template. + let amended = JSON.parse(JSON.stringify(template)); + + if (!Matcher.isMatcher(props) || !(props.name === 'absent')) { + amended = addEmptyProperties(amended); } - return hasResource(amended, type, props); + + return hasResource(amended, type, Match.objectLike({ + Properties: props, + })); } export function countResources(template: Template, type: string): number { @@ -47,15 +51,14 @@ export function countResources(template: Template, type: string): number { } function addEmptyProperties(template: Template): Template { - let resources = template.Resources; - console.log(template); - Object.keys(resources).map((key) => { - if (!resources[key].hasOwnProperty('Properties')) { - console.log('here'); - resources[key].Properties = {}; + let section = template.Resources; + + Object.keys(section).map((key) => { + if (!section[key].hasOwnProperty('Properties')) { + section[key].Properties = {}; } }); - console.log(template); + return template; } diff --git a/packages/@aws-cdk/assertions/test/template.test.ts b/packages/@aws-cdk/assertions/test/template.test.ts index 4245995572cbe..465f63ec9ec0e 100644 --- a/packages/@aws-cdk/assertions/test/template.test.ts +++ b/packages/@aws-cdk/assertions/test/template.test.ts @@ -280,11 +280,11 @@ describe('Template', () => { const inspect = Template.fromStack(stack); inspect.hasResourceProperties('Foo::Bar', { baz: 'qux' }); - expect(() => inspect.hasResourceProperties('Foo::Bar', { baz: 'waldo' })) - .toThrow(/Expected waldo but received qux at \/Properties\/baz/); + // expect(() => inspect.hasResourceProperties('Foo::Bar', { baz: 'waldo' })) + // .toThrow(/Missing key at \/baz/); - expect(() => inspect.hasResourceProperties('Foo::Bar', { baz: 'qux', fred: 'waldo' })) - .toThrow(/Missing key at \/Properties\/fred/); + // expect(() => inspect.hasResourceProperties('Foo::Bar', { baz: 'qux', fred: 'waldo' })) + // .toThrow(/Missing key at \/Properties\/fred/); }); test('absent - with properties', () => { @@ -310,10 +310,8 @@ describe('Template', () => { }); const inspect = Template.fromStack(stack); - expect(() => inspect.hasResourceProperties('Foo::Bar', { bar: Match.absent(), baz: 'qux' })) .toThrow(/Missing key at \/Properties\/baz/); - inspect.hasResourceProperties('Foo::Bar', Match.absent()); }); From f38ac19f622ccadf2e1e13058941f4367162b324 Mon Sep 17 00:00:00 2001 From: kaizen3031593 Date: Tue, 5 Oct 2021 16:54:52 -0400 Subject: [PATCH 6/9] typo --- packages/@aws-cdk/assertions/test/template.test.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/@aws-cdk/assertions/test/template.test.ts b/packages/@aws-cdk/assertions/test/template.test.ts index 465f63ec9ec0e..3384cda21207f 100644 --- a/packages/@aws-cdk/assertions/test/template.test.ts +++ b/packages/@aws-cdk/assertions/test/template.test.ts @@ -280,11 +280,11 @@ describe('Template', () => { const inspect = Template.fromStack(stack); inspect.hasResourceProperties('Foo::Bar', { baz: 'qux' }); - // expect(() => inspect.hasResourceProperties('Foo::Bar', { baz: 'waldo' })) - // .toThrow(/Missing key at \/baz/); + expect(() => inspect.hasResourceProperties('Foo::Bar', { baz: 'waldo' })) + .toThrow(/Expected waldo but received qux at \/Properties\/baz/); - // expect(() => inspect.hasResourceProperties('Foo::Bar', { baz: 'qux', fred: 'waldo' })) - // .toThrow(/Missing key at \/Properties\/fred/); + expect(() => inspect.hasResourceProperties('Foo::Bar', { baz: 'qux', fred: 'waldo' })) + .toThrow(/Missing key at \/Properties\/fred/); }); test('absent - with properties', () => { @@ -295,9 +295,11 @@ describe('Template', () => { }); const inspect = Template.fromStack(stack); + inspect.hasResourceProperties('Foo::Bar', { bar: Match.absent(), }); + expect(() => inspect.hasResourceProperties('Foo::Bar', { baz: Match.absent(), })).toThrow(/key should be absent at \/Properties\/baz/); @@ -310,8 +312,10 @@ describe('Template', () => { }); const inspect = Template.fromStack(stack); + expect(() => inspect.hasResourceProperties('Foo::Bar', { bar: Match.absent(), baz: 'qux' })) .toThrow(/Missing key at \/Properties\/baz/); + inspect.hasResourceProperties('Foo::Bar', Match.absent()); }); From e03330db3a0c531bcdbd7ac58d20b9a63241b9db Mon Sep 17 00:00:00 2001 From: kaizen3031593 Date: Wed, 6 Oct 2021 12:17:42 -0400 Subject: [PATCH 7/9] move absentMatch to a file in private --- packages/@aws-cdk/assertions/lib/match.ts | 15 +-------------- .../assertions/lib/private/absentMatch.ts | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 14 deletions(-) create mode 100644 packages/@aws-cdk/assertions/lib/private/absentMatch.ts diff --git a/packages/@aws-cdk/assertions/lib/match.ts b/packages/@aws-cdk/assertions/lib/match.ts index 8e4d83a398347..eab423d7a14f6 100644 --- a/packages/@aws-cdk/assertions/lib/match.ts +++ b/packages/@aws-cdk/assertions/lib/match.ts @@ -1,4 +1,5 @@ import { Matcher, MatchResult } from './matcher'; +import { AbsentMatch } from './private/absentMatch'; import { getType } from './private/type'; /** @@ -329,17 +330,3 @@ class AnyMatch extends Matcher { return result; } } - -class AbsentMatch extends Matcher { - constructor(public readonly name: string) { - super(); - } - - public test(actual: any): MatchResult { - const result = new MatchResult(actual); - if (actual !== undefined) { - result.push(this, [], `Received ${actual}, but key should be absent`); - } - return result; - } -} \ No newline at end of file diff --git a/packages/@aws-cdk/assertions/lib/private/absentMatch.ts b/packages/@aws-cdk/assertions/lib/private/absentMatch.ts new file mode 100644 index 0000000000000..46dc0544aab6c --- /dev/null +++ b/packages/@aws-cdk/assertions/lib/private/absentMatch.ts @@ -0,0 +1,15 @@ +import { Matcher, MatchResult } from '../matcher'; + +export class AbsentMatch extends Matcher { + constructor(public readonly name: string) { + super(); + } + + public test(actual: any): MatchResult { + const result = new MatchResult(actual); + if (actual !== undefined) { + result.push(this, [], `Received ${actual}, but key should be absent`); + } + return result; + } +} \ No newline at end of file From bf65ba9ba028cb10971bdc04dc9634b2e1073a3f Mon Sep 17 00:00:00 2001 From: kaizen3031593 Date: Wed, 6 Oct 2021 12:24:05 -0400 Subject: [PATCH 8/9] modify special case checker for AbsentMatch --- packages/@aws-cdk/assertions/lib/private/resources.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/@aws-cdk/assertions/lib/private/resources.ts b/packages/@aws-cdk/assertions/lib/private/resources.ts index 5ba77f4980878..585c9c5993a0d 100644 --- a/packages/@aws-cdk/assertions/lib/private/resources.ts +++ b/packages/@aws-cdk/assertions/lib/private/resources.ts @@ -1,4 +1,5 @@ import { Match, Matcher } from '..'; +import { AbsentMatch } from './absentMatch'; import { formatFailure, matchSection } from './section'; import { Resource, Template } from './template'; @@ -34,7 +35,8 @@ export function hasResourceProperties(template: Template, type: string, props: a // amended needs to be a deep copy to avoid modifying the template. let amended = JSON.parse(JSON.stringify(template)); - if (!Matcher.isMatcher(props) || !(props.name === 'absent')) { + // special case to exclude AbsentMatch because adding an empty Properties object will affect its evaluation. + if (!Matcher.isMatcher(props) || !(props instanceof AbsentMatch)) { amended = addEmptyProperties(amended); } From f0f76ba83167a43658313f9f1d7d47e7accf6359 Mon Sep 17 00:00:00 2001 From: kaizen3031593 Date: Wed, 6 Oct 2021 13:49:08 -0400 Subject: [PATCH 9/9] rename private/absentMatch into private/matchers/absent --- packages/@aws-cdk/assertions/lib/match.ts | 2 +- .../lib/private/{absentMatch.ts => matchers/absent.ts} | 2 +- packages/@aws-cdk/assertions/lib/private/resources.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) rename packages/@aws-cdk/assertions/lib/private/{absentMatch.ts => matchers/absent.ts} (86%) diff --git a/packages/@aws-cdk/assertions/lib/match.ts b/packages/@aws-cdk/assertions/lib/match.ts index eab423d7a14f6..70ad96dbee300 100644 --- a/packages/@aws-cdk/assertions/lib/match.ts +++ b/packages/@aws-cdk/assertions/lib/match.ts @@ -1,5 +1,5 @@ import { Matcher, MatchResult } from './matcher'; -import { AbsentMatch } from './private/absentMatch'; +import { AbsentMatch } from './private/matchers/absent'; import { getType } from './private/type'; /** diff --git a/packages/@aws-cdk/assertions/lib/private/absentMatch.ts b/packages/@aws-cdk/assertions/lib/private/matchers/absent.ts similarity index 86% rename from packages/@aws-cdk/assertions/lib/private/absentMatch.ts rename to packages/@aws-cdk/assertions/lib/private/matchers/absent.ts index 46dc0544aab6c..0681f8ada8214 100644 --- a/packages/@aws-cdk/assertions/lib/private/absentMatch.ts +++ b/packages/@aws-cdk/assertions/lib/private/matchers/absent.ts @@ -1,4 +1,4 @@ -import { Matcher, MatchResult } from '../matcher'; +import { Matcher, MatchResult } from '../../matcher'; export class AbsentMatch extends Matcher { constructor(public readonly name: string) { diff --git a/packages/@aws-cdk/assertions/lib/private/resources.ts b/packages/@aws-cdk/assertions/lib/private/resources.ts index 585c9c5993a0d..68e8e6c2ddff8 100644 --- a/packages/@aws-cdk/assertions/lib/private/resources.ts +++ b/packages/@aws-cdk/assertions/lib/private/resources.ts @@ -1,5 +1,5 @@ import { Match, Matcher } from '..'; -import { AbsentMatch } from './absentMatch'; +import { AbsentMatch } from './matchers/absent'; import { formatFailure, matchSection } from './section'; import { Resource, Template } from './template';