From d9b6bc8f22b072afd3dcef39b6414727ce2d4057 Mon Sep 17 00:00:00 2001 From: sakurai-ryo Date: Thu, 4 Jan 2024 22:49:15 +0900 Subject: [PATCH 1/2] fix: quiet flag to print stack name when having diffs --- packages/aws-cdk/lib/cdk-toolkit.ts | 14 ++++++++------ packages/aws-cdk/lib/diff.ts | 8 +++++--- packages/aws-cdk/test/diff.test.ts | 28 +++++++++++++++++++++++++--- 3 files changed, 38 insertions(+), 12 deletions(-) diff --git a/packages/aws-cdk/lib/cdk-toolkit.ts b/packages/aws-cdk/lib/cdk-toolkit.ts index 43a2638154a3a..c8298109fb8ee 100644 --- a/packages/aws-cdk/lib/cdk-toolkit.ts +++ b/packages/aws-cdk/lib/cdk-toolkit.ts @@ -1,6 +1,7 @@ import * as path from 'path'; import { format } from 'util'; import * as cxapi from '@aws-cdk/cx-api'; +import * as cfnDiff from '@aws-cdk/cloudformation-diff'; import * as chalk from 'chalk'; import * as chokidar from 'chokidar'; import * as fs from 'fs-extra'; @@ -152,10 +153,6 @@ export class CdkToolkit { } else { // Compare N stacks against deployed templates for (const stack of stacks.stackArtifacts) { - if (!quiet) { - stream.write(format('Stack %s\n', chalk.bold(stack.displayName))); - } - const templateWithNames = await this.props.deployments.readCurrentTemplateWithNestedStacks( stack, options.compareAgainstProcessedTemplate, ); @@ -172,10 +169,15 @@ export class CdkToolkit { stream, }) : undefined; + const diff = cfnDiff.fullDiff(currentTemplate, stack.template, changeSet); + if (!quiet || !diff.isEmpty) { + stream.write(format('Stack %s\n', chalk.bold(stack.displayName))); + } + const stackCount = options.securityOnly - ? (numberFromBool(printSecurityDiff(currentTemplate, stack, RequireApproval.Broadening, changeSet)) > 0 ? 1 : 0) - : (printStackDiff(currentTemplate, stack, strict, contextLines, quiet, changeSet, stream) > 0 ? 1 : 0); + ? (numberFromBool(printSecurityDiff(currentTemplate, stack, RequireApproval.Broadening, changeSet, diff)) > 0 ? 1 : 0) + : (printStackDiff(currentTemplate, stack, strict, contextLines, quiet, changeSet, stream, diff) > 0 ? 1 : 0); diffs += stackCount + nestedStackCount; } diff --git a/packages/aws-cdk/lib/diff.ts b/packages/aws-cdk/lib/diff.ts index a1f025db055a2..70887ef619099 100644 --- a/packages/aws-cdk/lib/diff.ts +++ b/packages/aws-cdk/lib/diff.ts @@ -23,9 +23,10 @@ export function printStackDiff( context: number, quiet: boolean, changeSet?: CloudFormation.DescribeChangeSetOutput, - stream?: cfnDiff.FormatStream): number { + stream?: cfnDiff.FormatStream, + stackDiff?: cfnDiff.TemplateDiff): number { - let diff = cfnDiff.fullDiff(oldTemplate, newTemplate.template, changeSet); + let diff = stackDiff ?? cfnDiff.fullDiff(oldTemplate, newTemplate.template, changeSet); // detect and filter out mangled characters from the diff let filteredChangesCount = 0; @@ -81,8 +82,9 @@ export function printSecurityDiff( newTemplate: cxapi.CloudFormationStackArtifact, requireApproval: RequireApproval, changeSet?: CloudFormation.DescribeChangeSetOutput, + stackDiff?: cfnDiff.TemplateDiff, ): boolean { - const diff = cfnDiff.fullDiff(oldTemplate, newTemplate.template, changeSet); + const diff = stackDiff ?? cfnDiff.fullDiff(oldTemplate, newTemplate.template, changeSet); if (difRequiresApproval(diff, requireApproval)) { // eslint-disable-next-line max-len diff --git a/packages/aws-cdk/test/diff.test.ts b/packages/aws-cdk/test/diff.test.ts index a7b5905e12f87..214b82d9bf013 100644 --- a/packages/aws-cdk/test/diff.test.ts +++ b/packages/aws-cdk/test/diff.test.ts @@ -187,15 +187,37 @@ describe('non-nested stacks', () => { // WHEN const exitCode = await toolkit.diff({ - stackNames: ['A', 'A'], + stackNames: ['D'], stream: buffer, fail: false, quiet: true, }); // THEN - expect(buffer.data.trim()).not.toContain('Stack A'); - expect(buffer.data.trim()).not.toContain('There were no differences'); + const plainTextOutput = buffer.data.replace(/\x1B\[[0-?]*[ -/]*[@-~]/g, ''); + expect(plainTextOutput).not.toContain('Stack D'); + expect(plainTextOutput).not.toContain('There were no differences'); + expect(buffer.data.trim()).toContain('✨ Number of stacks with differences: 0'); + expect(exitCode).toBe(0); + }); + + test('when quiet mode is enabled, stacks with diffs should print stack name to stdout', async () => { + // GIVEN + const buffer = new StringWritable(); + + // WHEN + const exitCode = await toolkit.diff({ + stackNames: ['A'], + stream: buffer, + fail: false, + quiet: true, + }); + + // THEN + const plainTextOutput = buffer.data.replace(/\x1B\[[0-?]*[ -/]*[@-~]/g, ''); + expect(plainTextOutput).toContain('Stack A'); + expect(plainTextOutput).not.toContain('There were no differences'); + expect(buffer.data.trim()).toContain('✨ Number of stacks with differences: 1'); expect(exitCode).toBe(0); }); }); From fd24c9ba41e57cbbd655b1211d2397a2e5d322a8 Mon Sep 17 00:00:00 2001 From: sakurai-ryo Date: Thu, 4 Jan 2024 23:36:45 +0900 Subject: [PATCH 2/2] fix lint --- packages/aws-cdk/lib/cdk-toolkit.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/aws-cdk/lib/cdk-toolkit.ts b/packages/aws-cdk/lib/cdk-toolkit.ts index c8298109fb8ee..3f65bc8424a3e 100644 --- a/packages/aws-cdk/lib/cdk-toolkit.ts +++ b/packages/aws-cdk/lib/cdk-toolkit.ts @@ -1,7 +1,7 @@ import * as path from 'path'; import { format } from 'util'; -import * as cxapi from '@aws-cdk/cx-api'; import * as cfnDiff from '@aws-cdk/cloudformation-diff'; +import * as cxapi from '@aws-cdk/cx-api'; import * as chalk from 'chalk'; import * as chokidar from 'chokidar'; import * as fs from 'fs-extra';