|
1 | 1 | import * as fs from 'fs'; |
2 | 2 | import { testDeprecated } from '@aws-cdk/cdk-build-tools'; |
3 | 3 | import { Construct, Node } from 'constructs'; |
4 | | -import { flattenMeta, toCloudFormation } from './util'; |
| 4 | +import { flattenMeta, getWarnings, toCloudFormation } from './util'; |
5 | 5 | import * as cxapi from '../../cx-api'; |
6 | 6 | import { Fact, RegionInfo } from '../../region-info'; |
7 | 7 | import type { ITaggableV2 } from '../lib'; |
@@ -1074,6 +1074,82 @@ describe('stack', () => { |
1074 | 1074 | expect(customResources2.length).toBeGreaterThan(0); |
1075 | 1075 | }); |
1076 | 1076 |
|
| 1077 | + test('emits a single warning per app when cross-stack reference flag is unset', () => { |
| 1078 | + // GIVEN - no context flag set, multiple consumer stacks |
| 1079 | + const app = new App(); |
| 1080 | + const stack1 = new Stack(app, 'Stack1'); |
| 1081 | + const resource1 = new CfnResource(stack1, 'Resource1', { type: 'AWS::S3::Bucket' }); |
| 1082 | + const resource2 = new CfnResource(stack1, 'Resource2', { type: 'AWS::SNS::Topic' }); |
| 1083 | + const stack2 = new Stack(app, 'Stack2'); |
| 1084 | + const stack3 = new Stack(app, 'Stack3'); |
| 1085 | + |
| 1086 | + // WHEN - cross-stack references from multiple consumers |
| 1087 | + new CfnResource(stack2, 'Consumer1', { |
| 1088 | + type: 'AWS::S3::Bucket', |
| 1089 | + properties: { Prop1: resource1.getAtt('Arn') }, |
| 1090 | + }); |
| 1091 | + new CfnResource(stack3, 'Consumer2', { |
| 1092 | + type: 'AWS::S3::Bucket', |
| 1093 | + properties: { Prop2: resource2.getAtt('Arn') }, |
| 1094 | + }); |
| 1095 | + |
| 1096 | + const assembly = app.synth(); |
| 1097 | + const warnings = getWarnings(assembly); |
| 1098 | + |
| 1099 | + // THEN - only one warning in the entire app |
| 1100 | + const relevantWarnings = warnings.filter(w => |
| 1101 | + w.message.includes('@aws-cdk/core:crossStackReferencesDefaultStrong'), |
| 1102 | + ); |
| 1103 | + expect(relevantWarnings).toHaveLength(1); |
| 1104 | + }); |
| 1105 | + |
| 1106 | + test('no warning when cross-stack reference flag is explicitly set', () => { |
| 1107 | + // GIVEN - context flag explicitly set to 'strong' |
| 1108 | + const app = new App({ context: { [cxapi.DEFAULT_CROSS_STACK_REFERENCES]: 'strong' } }); |
| 1109 | + const stack1 = new Stack(app, 'Stack1'); |
| 1110 | + const resource1 = new CfnResource(stack1, 'Resource1', { type: 'AWS::S3::Bucket' }); |
| 1111 | + const stack2 = new Stack(app, 'Stack2'); |
| 1112 | + |
| 1113 | + // WHEN |
| 1114 | + new CfnResource(stack2, 'Consumer1', { |
| 1115 | + type: 'AWS::S3::Bucket', |
| 1116 | + properties: { Prop1: resource1.getAtt('Arn') }, |
| 1117 | + }); |
| 1118 | + |
| 1119 | + const assembly = app.synth(); |
| 1120 | + const warnings = getWarnings(assembly); |
| 1121 | + |
| 1122 | + // THEN - no warning because the flag is explicitly set |
| 1123 | + const relevantWarnings = warnings.filter(w => |
| 1124 | + w.message.includes('@aws-cdk/core:crossStackReferencesDefaultStrong'), |
| 1125 | + ); |
| 1126 | + expect(relevantWarnings).toHaveLength(0); |
| 1127 | + }); |
| 1128 | + |
| 1129 | + test('emits transitional warning when cross-stack reference flag is set to both', () => { |
| 1130 | + // GIVEN - context flag set to 'both' |
| 1131 | + const app = new App({ context: { [cxapi.DEFAULT_CROSS_STACK_REFERENCES]: 'both' } }); |
| 1132 | + const stack1 = new Stack(app, 'Stack1'); |
| 1133 | + const resource1 = new CfnResource(stack1, 'Resource1', { type: 'AWS::S3::Bucket' }); |
| 1134 | + const stack2 = new Stack(app, 'Stack2'); |
| 1135 | + |
| 1136 | + // WHEN |
| 1137 | + new CfnResource(stack2, 'Consumer1', { |
| 1138 | + type: 'AWS::S3::Bucket', |
| 1139 | + properties: { Prop1: resource1.getAtt('Arn') }, |
| 1140 | + }); |
| 1141 | + |
| 1142 | + const assembly = app.synth(); |
| 1143 | + const warnings = getWarnings(assembly); |
| 1144 | + |
| 1145 | + // THEN - transitional warning telling user to move to 'weak' |
| 1146 | + const relevantWarnings = warnings.filter(w => |
| 1147 | + w.message.includes('@aws-cdk/core:crossStackReferencesBothTransitional'), |
| 1148 | + ); |
| 1149 | + expect(relevantWarnings).toHaveLength(1); |
| 1150 | + expect(relevantWarnings[0].path).toContain('Stack2'); |
| 1151 | + }); |
| 1152 | + |
1077 | 1153 | test('cross-region strong references use ExportWriter/ExportReader', () => { |
1078 | 1154 | // GIVEN - strength is explicitly 'strong' |
1079 | 1155 | const app = new App({ context: { [cxapi.DEFAULT_CROSS_STACK_REFERENCES]: 'strong' } }); |
|
0 commit comments