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

feat(assertions): match into serialized json #16456

Merged
merged 8 commits into from
Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 1 addition & 4 deletions packages/@aws-cdk/assertions/lib/match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,16 +292,13 @@ class SerializedJson extends Matcher {
parsed = JSON.parse(actual);
} catch (err) {
if (err instanceof SyntaxError) {
result.push(this, [], 'Invalid JSON string');
result.push(this, [], `Invalid JSON string: ${actual}`);
return result;
} else {
throw err;
}
}

if (parsed == null) {
result.push(this, [], 'Invalid JSON string');
}
const matcher = Matcher.isMatcher(this.pattern) ? this.pattern : new LiteralMatch(this.name, this.pattern);
BenChaimberg marked this conversation as resolved.
Show resolved Hide resolved
const innerResult = matcher.test(parsed);
result.compose(`(${this.name})`, innerResult);
Expand Down
6 changes: 6 additions & 0 deletions packages/@aws-cdk/assertions/test/match.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,12 @@ describe('Matchers', () => {
expectFailure(matcher, '{ "Foo": ["Baz"] }', ['Missing element [Bar] at pattern index 0 at (serializedJson)/Foo']);
expectFailure(matcher, '{ "Bar": ["Baz"] }', ['Missing key at (serializedJson)/Foo']);
});

test('invalid json string', () => {
matcher = Match.serializedJson({ Foo: 'Bar' });

expectFailure(matcher, '{ "Foo"', [/invalid JSON string/i]);
});
});
});

Expand Down
1 change: 0 additions & 1 deletion packages/@aws-cdk/pipelines/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
"organization": true
},
"devDependencies": {
"@aws-cdk/assert-internal": "0.0.0",
"@aws-cdk/assertions": "0.0.0",
"@aws-cdk/aws-apigateway": "0.0.0",
"@aws-cdk/aws-ecr-assets": "0.0.0",
Expand Down
1 change: 0 additions & 1 deletion packages/@aws-cdk/pipelines/test/compliance/assets.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as fs from 'fs';
import * as path from 'path';
import { Capture, Match, Template } from '@aws-cdk/assertions';
import '@aws-cdk/assert-internal/jest';
import * as cb from '@aws-cdk/aws-codebuild';
import * as ec2 from '@aws-cdk/aws-ec2';
import { Stack, Stage } from '@aws-cdk/core';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Match, Template } from '@aws-cdk/assertions';
import '@aws-cdk/assert-internal/jest';
import * as cb from '@aws-cdk/aws-codebuild';
import * as secretsmanager from '@aws-cdk/aws-secretsmanager';
import { Stack } from '@aws-cdk/core';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Match, Template } from '@aws-cdk/assertions';
import '@aws-cdk/assert-internal/jest';
import { Topic } from '@aws-cdk/aws-sns';
import { Stack } from '@aws-cdk/core';
import * as cdkp from '../../lib';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable import/no-extraneous-dependencies */
import { Match, Template } from '@aws-cdk/assertions';
import '@aws-cdk/assert-internal/jest';
import * as cb from '@aws-cdk/aws-codebuild';
import * as cp from '@aws-cdk/aws-codepipeline';
import { Stack, Stage } from '@aws-cdk/core';
Expand Down
91 changes: 45 additions & 46 deletions packages/@aws-cdk/pipelines/test/compliance/stack-ordering.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { arrayWith, objectLike } from '@aws-cdk/assert-internal';
import '@aws-cdk/assert-internal/jest';
import { Match, Template } from '@aws-cdk/assertions';
import { App, Stack } from '@aws-cdk/core';
import { behavior, LegacyTestGitHubNpmPipeline, ModernTestGitHubNpmPipeline, OneStackApp, PIPELINE_ENV, sortedByRunOrder, TestApp, ThreeStackApp, TwoStackApp } from '../testhelpers';
import { behavior, LegacyTestGitHubNpmPipeline, ModernTestGitHubNpmPipeline, OneStackApp, PIPELINE_ENV, sortByRunOrder, TestApp, ThreeStackApp, TwoStackApp } from '../testhelpers';

let app: App;
let pipelineStack: Stack;
Expand All @@ -28,16 +27,16 @@ behavior('interdependent stacks are in the right order', (suite) => {

function THEN_codePipelineExpectation() {
// THEN
expect(pipelineStack).toHaveResourceLike('AWS::CodePipeline::Pipeline', {
Stages: arrayWith({
Template.fromStack(pipelineStack).hasResourceProperties('AWS::CodePipeline::Pipeline', {
Stages: Match.arrayWith([{
Name: 'MyApp',
Actions: sortedByRunOrder([
objectLike({ Name: 'Stack1.Prepare' }),
objectLike({ Name: 'Stack1.Deploy' }),
objectLike({ Name: 'Stack2.Prepare' }),
objectLike({ Name: 'Stack2.Deploy' }),
Actions: sortByRunOrder([
Match.objectLike({ Name: 'Stack1.Prepare' }),
Match.objectLike({ Name: 'Stack1.Deploy' }),
Match.objectLike({ Name: 'Stack2.Prepare' }),
Match.objectLike({ Name: 'Stack2.Deploy' }),
]),
}),
}]),
});
}
});
Expand All @@ -59,20 +58,20 @@ behavior('multiple independent stacks go in parallel', (suite) => {
});

function THEN_codePipelineExpectation() {
expect(pipelineStack).toHaveResourceLike('AWS::CodePipeline::Pipeline', {
Stages: arrayWith({
Template.fromStack(pipelineStack).hasResourceProperties('AWS::CodePipeline::Pipeline', {
Stages: Match.arrayWith([{
Name: 'MyApp',
Actions: sortedByRunOrder([
Actions: sortByRunOrder([
// 1 and 2 in parallel
objectLike({ Name: 'Stack1.Prepare' }),
objectLike({ Name: 'Stack2.Prepare' }),
objectLike({ Name: 'Stack1.Deploy' }),
objectLike({ Name: 'Stack2.Deploy' }),
Match.objectLike({ Name: 'Stack1.Prepare' }),
Match.objectLike({ Name: 'Stack2.Prepare' }),
Match.objectLike({ Name: 'Stack1.Deploy' }),
Match.objectLike({ Name: 'Stack2.Deploy' }),
// Then 3
objectLike({ Name: 'Stack3.Prepare' }),
objectLike({ Name: 'Stack3.Deploy' }),
Match.objectLike({ Name: 'Stack3.Prepare' }),
Match.objectLike({ Name: 'Stack3.Deploy' }),
]),
}),
}]),
});
}
});
Expand All @@ -86,18 +85,18 @@ behavior('user can request manual change set approvals', (suite) => {
});

// THEN
expect(pipelineStack).toHaveResourceLike('AWS::CodePipeline::Pipeline', {
Stages: arrayWith({
Template.fromStack(pipelineStack).hasResourceProperties('AWS::CodePipeline::Pipeline', {
Stages: Match.arrayWith([{
Name: 'MyApp',
Actions: sortedByRunOrder([
objectLike({ Name: 'Stack1.Prepare' }),
objectLike({ Name: 'ManualApproval' }),
objectLike({ Name: 'Stack1.Deploy' }),
objectLike({ Name: 'Stack2.Prepare' }),
objectLike({ Name: 'ManualApproval2' }),
objectLike({ Name: 'Stack2.Deploy' }),
Actions: sortByRunOrder([
Match.objectLike({ Name: 'Stack1.Prepare' }),
Match.objectLike({ Name: 'ManualApproval' }),
Match.objectLike({ Name: 'Stack1.Deploy' }),
Match.objectLike({ Name: 'Stack2.Prepare' }),
Match.objectLike({ Name: 'ManualApproval2' }),
Match.objectLike({ Name: 'Stack2.Deploy' }),
]),
}),
}]),
});
});

Expand All @@ -114,28 +113,28 @@ behavior('user can request extra runorder space between prepare and deploy', (su
});

// THEN
expect(pipelineStack).toHaveResourceLike('AWS::CodePipeline::Pipeline', {
Stages: arrayWith({
Template.fromStack(pipelineStack).hasResourceProperties('AWS::CodePipeline::Pipeline', {
Stages: Match.arrayWith([{
Name: 'MyApp',
Actions: sortedByRunOrder([
objectLike({
Actions: sortByRunOrder([
Match.objectLike({
Name: 'Stack1.Prepare',
RunOrder: 1,
}),
objectLike({
Match.objectLike({
Name: 'Stack1.Deploy',
RunOrder: 3,
}),
objectLike({
Match.objectLike({
Name: 'Stack2.Prepare',
RunOrder: 4,
}),
objectLike({
Match.objectLike({
Name: 'Stack2.Deploy',
RunOrder: 6,
}),
]),
}),
}]),
});
});

Expand All @@ -153,24 +152,24 @@ behavior('user can request both manual change set approval and extraRunOrderSpac
});

// THEN
expect(pipelineStack).toHaveResourceLike('AWS::CodePipeline::Pipeline', {
Stages: arrayWith({
Template.fromStack(pipelineStack).hasResourceProperties('AWS::CodePipeline::Pipeline', {
Stages: Match.arrayWith([{
Name: 'MyApp',
Actions: sortedByRunOrder([
objectLike({
Actions: sortByRunOrder([
Match.objectLike({
Name: 'Stack.Prepare',
RunOrder: 1,
}),
objectLike({
Match.objectLike({
Name: 'ManualApproval',
RunOrder: 2,
}),
objectLike({
Match.objectLike({
Name: 'Stack.Deploy',
RunOrder: 4,
}),
]),
}),
}]),
});
});

Expand Down
1 change: 0 additions & 1 deletion packages/@aws-cdk/pipelines/test/compliance/synths.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Capture, Match, Template } from '@aws-cdk/assertions';
import '@aws-cdk/assert-internal/jest';
import * as cbuild from '@aws-cdk/aws-codebuild';
import * as codepipeline from '@aws-cdk/aws-codepipeline';
import * as ec2 from '@aws-cdk/aws-ec2';
Expand Down
Loading