Skip to content

Commit 0f917d6

Browse files
kaizenccmrgraingithub-actions
authored
fix(toolkit): diff prints stack name not display name (#454)
Fixes #448 Prior to #301 we were using both `stackName` and `displayName` interchangably in different areas of the CLI. #301 chose to standardize to `stackName`. This PR changes that to `displayName`. --- By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license --------- Signed-off-by: github-actions <[email protected]> Co-authored-by: Momo Kornher <[email protected]> Co-authored-by: github-actions <[email protected]>
1 parent 263bc3e commit 0f917d6

File tree

2 files changed

+90
-1
lines changed

2 files changed

+90
-1
lines changed

packages/@aws-cdk/toolkit-lib/lib/api/diff/diff-formatter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ export class DiffFormatter {
154154
this.ioHelper = props.ioHelper;
155155
this.oldTemplate = props.templateInfo.oldTemplate;
156156
this.newTemplate = props.templateInfo.newTemplate;
157-
this.stackName = props.templateInfo.newTemplate.stackName;
157+
this.stackName = props.templateInfo.newTemplate.displayName ?? props.templateInfo.newTemplate.stackName;
158158
this.changeSet = props.templateInfo.changeSet;
159159
this.nestedStacks = props.templateInfo.nestedStacks;
160160
this.isImport = props.templateInfo.isImport ?? false;

packages/aws-cdk/test/commands/diff.test.ts

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,3 +1150,92 @@ Resources
11501150
expect(exitCode).toBe(0);
11511151
});
11521152
});
1153+
1154+
describe('stack display names', () => {
1155+
beforeEach(() => {
1156+
cloudFormation = instanceMockFrom(Deployments);
1157+
cloudFormation.readCurrentTemplateWithNestedStacks.mockImplementation((_stackArtifact: CloudFormationStackArtifact) => {
1158+
return Promise.resolve({
1159+
deployedRootTemplate: {},
1160+
nestedStacks: {},
1161+
});
1162+
});
1163+
cloudExecutable = new MockCloudExecutable({
1164+
stacks: [
1165+
{
1166+
stackName: 'MyParent',
1167+
displayName: 'Parent/NestedStack',
1168+
template: { resource: 'ParentStack' },
1169+
},
1170+
{
1171+
stackName: 'MyChild',
1172+
displayName: 'Parent/NestedStack/MyChild',
1173+
template: { resource: 'ChildStack' },
1174+
},
1175+
],
1176+
}, undefined, ioHost);
1177+
1178+
toolkit = new CdkToolkit({
1179+
cloudExecutable,
1180+
deployments: cloudFormation,
1181+
configuration: cloudExecutable.configuration,
1182+
sdkProvider: cloudExecutable.sdkProvider,
1183+
});
1184+
});
1185+
1186+
test('diff should display stack paths instead of logical IDs', async () => {
1187+
// WHEN
1188+
const exitCode = await toolkit.diff({
1189+
stackNames: ['Parent/NestedStack', 'Parent/NestedStack/MyChild'],
1190+
});
1191+
1192+
// THEN
1193+
const parentOutput = notifySpy.mock.calls[0][0].message.replace(/\x1B\[[0-?]*[ -/]*[@-~]/g, '');
1194+
const childOutput = notifySpy.mock.calls[1][0].message.replace(/\x1B\[[0-?]*[ -/]*[@-~]/g, '');
1195+
1196+
// Verify that the display name (path) is shown instead of the logical ID
1197+
expect(parentOutput).toContain('Stack Parent/NestedStack/MyChild');
1198+
expect(parentOutput).not.toContain('Stack MyChild');
1199+
1200+
expect(childOutput).toContain('Stack Parent/NestedStack');
1201+
expect(childOutput).not.toContain('Stack MyParent');
1202+
1203+
expect(notifySpy).toHaveBeenCalledWith(expect.objectContaining({
1204+
message: expect.stringContaining('✨ Number of stacks with differences: 2'),
1205+
}));
1206+
expect(exitCode).toBe(0);
1207+
});
1208+
1209+
test('diff should fall back to logical ID if display name is not available', async () => {
1210+
// Create a new cloud executable with stacks that don't have display names
1211+
cloudExecutable = new MockCloudExecutable({
1212+
stacks: [
1213+
{
1214+
stackName: 'NoDisplayNameStack',
1215+
// No displayName provided
1216+
template: { resource: 'ParentStack' },
1217+
},
1218+
],
1219+
}, undefined, ioHost);
1220+
1221+
toolkit = new CdkToolkit({
1222+
cloudExecutable,
1223+
deployments: cloudFormation,
1224+
configuration: cloudExecutable.configuration,
1225+
sdkProvider: cloudExecutable.sdkProvider,
1226+
});
1227+
1228+
// WHEN
1229+
const exitCode = await toolkit.diff({
1230+
stackNames: ['NoDisplayNameStack'],
1231+
});
1232+
1233+
// THEN
1234+
const output = notifySpy.mock.calls[0][0].message.replace(/\x1B\[[0-?]*[ -/]*[@-~]/g, '');
1235+
1236+
// Verify that the logical ID is shown when display name is not available
1237+
expect(output).toContain('Stack NoDisplayNameStack');
1238+
1239+
expect(exitCode).toBe(0);
1240+
});
1241+
});

0 commit comments

Comments
 (0)