1+ import { Annotations , App , Stack , Tags } from '../lib' ;
2+ import { getWarnings } from './util' ;
3+
4+ describe ( 'ChangeSet tagging with explicitStackTags' , ( ) => {
5+ test ( 'tags are applied to ChangeSets even when explicitStackTags is enabled' , ( ) => {
6+ // GIVEN
7+ const app = new App ( {
8+ context : {
9+ '@aws-cdk/core:explicitStackTags' : true ,
10+ } ,
11+ } ) ;
12+ const stack = new Stack ( app , 'TestStack' ) ;
13+
14+ // WHEN
15+ Tags . of ( stack ) . add ( 'TestKey' , 'TestValue' ) ;
16+ Tags . of ( stack ) . add ( 'Environment' , 'Production' ) ;
17+ Tags . of ( stack ) . add ( 'Project' , 'CDK-Fix' ) ;
18+
19+ // THEN
20+ // Stack should have tags for ChangeSets
21+ const stackTags = stack . tags . tagValues ( ) ;
22+ expect ( stackTags ) . toHaveProperty ( 'TestKey' , 'TestValue' ) ;
23+ expect ( stackTags ) . toHaveProperty ( 'Environment' , 'Production' ) ;
24+ expect ( stackTags ) . toHaveProperty ( 'Project' , 'CDK-Fix' ) ;
25+ } ) ;
26+
27+ test ( 'applyToChangeSets can be disabled explicitly' , ( ) => {
28+ // GIVEN
29+ const app = new App ( {
30+ context : {
31+ '@aws-cdk/core:explicitStackTags' : true ,
32+ } ,
33+ } ) ;
34+ const stack = new Stack ( app , 'TestStack' ) ;
35+
36+ // WHEN
37+ Tags . of ( stack ) . add ( 'TestKey' , 'TestValue' , { applyToChangeSets : false } ) ;
38+
39+ // THEN
40+ // Stack should NOT have tags when applyToChangeSets is false
41+ const stackTags = stack . tags . tagValues ( ) ;
42+ expect ( Object . keys ( stackTags ) ) . toHaveLength ( 0 ) ;
43+ } ) ;
44+
45+ test ( 'legacy behavior is maintained when explicitStackTags is not enabled' , ( ) => {
46+ // GIVEN
47+ const app = new App ( {
48+ context : {
49+ '@aws-cdk/core:explicitStackTags' : false ,
50+ } ,
51+ } ) ;
52+ const stack = new Stack ( app , 'TestStack' ) ;
53+
54+ // WHEN
55+ Tags . of ( stack ) . add ( 'TestKey' , 'TestValue' ) ;
56+
57+ // THEN
58+ // Stack should have tags (legacy behavior)
59+ const stackTags = stack . tags . tagValues ( ) ;
60+ expect ( stackTags ) . toHaveProperty ( 'TestKey' , 'TestValue' ) ;
61+ } ) ;
62+
63+ test ( 'direct stack tagging still works with addStackTag' , ( ) => {
64+ // GIVEN
65+ const app = new App ( {
66+ context : {
67+ '@aws-cdk/core:explicitStackTags' : true ,
68+ } ,
69+ } ) ;
70+ const stack = new Stack ( app , 'TestStack' ) ;
71+
72+ // WHEN
73+ stack . addStackTag ( 'DirectTag' , 'DirectValue' ) ;
74+
75+ // THEN
76+ const stackTags = stack . tags . tagValues ( ) ;
77+ expect ( stackTags ) . toHaveProperty ( 'DirectTag' , 'DirectValue' ) ;
78+ } ) ;
79+
80+ test ( 'mixed tagging approaches work together' , ( ) => {
81+ // GIVEN
82+ const app = new App ( {
83+ context : {
84+ '@aws-cdk/core:explicitStackTags' : true ,
85+ } ,
86+ } ) ;
87+ const stack = new Stack ( app , 'TestStack' ) ;
88+
89+ // WHEN
90+ stack . addStackTag ( 'DirectTag' , 'DirectValue' ) ;
91+ Tags . of ( stack ) . add ( 'AspectTag' , 'AspectValue' ) ;
92+ Tags . of ( stack ) . add ( 'NoChangeSetTag' , 'NoValue' , { applyToChangeSets : false } ) ;
93+
94+ // THEN
95+ const stackTags = stack . tags . tagValues ( ) ;
96+ expect ( stackTags ) . toHaveProperty ( 'DirectTag' , 'DirectValue' ) ;
97+ expect ( stackTags ) . toHaveProperty ( 'AspectTag' , 'AspectValue' ) ;
98+ expect ( stackTags ) . not . toHaveProperty ( 'NoChangeSetTag' ) ;
99+ } ) ;
100+
101+ test ( 'tags with tokens show warning but still apply to ChangeSets' , ( ) => {
102+ // GIVEN
103+ const app = new App ( {
104+ context : {
105+ '@aws-cdk/core:explicitStackTags' : true ,
106+ } ,
107+ } ) ;
108+ const stack = new Stack ( app , 'TestStack' ) ;
109+
110+ // WHEN
111+ Tags . of ( stack ) . add ( 'StaticKey' , 'StaticValue' ) ;
112+ Tags . of ( stack ) . add ( 'TokenKey' , stack . stackName ) ; // This contains a token
113+
114+ // Synthesize to trigger warnings
115+ app . synth ( ) ;
116+
117+ // THEN
118+ const warnings = getWarnings ( app . synth ( ) ) ;
119+ // Should have warning about token tags
120+ const tokenWarnings = warnings . filter ( w =>
121+ w . message . includes ( 'Ignoring stack tags that contain deploy-time values' )
122+ ) ;
123+ expect ( tokenWarnings . length ) . toBeGreaterThan ( 0 ) ;
124+
125+ // But static tags should still be applied
126+ const stackTags = stack . tags . tagValues ( ) ;
127+ expect ( stackTags ) . toHaveProperty ( 'StaticKey' , 'StaticValue' ) ;
128+ } ) ;
129+ } ) ;
0 commit comments