Skip to content

Commit 1383fda

Browse files
committed
modified tests
1 parent 2aaacc1 commit 1383fda

File tree

4 files changed

+49
-61
lines changed

4 files changed

+49
-61
lines changed

packages/aws-cdk-lib/core/lib/private/feature-flag-report.ts

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { FlagInfo } from '../../../cx-api/lib/private/flag-modeling';
88
* Creates a FeatureFlag object based on flag information given.
99
*/
1010
function parseFeatureFlagInfo(flagName: string, info: FlagInfo, root: IConstruct): FeatureFlag {
11-
const userValue = root.node.tryGetContext(flagName) ?? false;
11+
const userValue = root.node.tryGetContext(flagName) ?? undefined;
1212

1313
let parsedFlag: FeatureFlag = {
1414
userValue: userValue,
@@ -24,21 +24,16 @@ function parseFeatureFlagInfo(flagName: string, info: FlagInfo, root: IConstruct
2424
* and create a Feature Flag report.
2525
*/
2626
export function generateFeatureFlagReport(builder: CloudAssemblyBuilder, root: IConstruct): void {
27-
try {
28-
const featureFlags: Record<string, FeatureFlag> = {};
29-
30-
for (const [flagName, flagInfo] of Object.entries(feats.FLAGS)) {
31-
featureFlags[flagName] = parseFeatureFlagInfo(flagName, flagInfo, root);
32-
}
33-
34-
builder.addArtifact('feature flag report', {
35-
type: ArtifactType.FEATURE_FLAG_REPORT,
36-
properties: {
37-
module: '@aws-cdk/core',
38-
flags: featureFlags,
39-
},
40-
});
41-
} catch (error) {
42-
throw error;
27+
const featureFlags: Record<string, FeatureFlag> = {};
28+
for (const [flagName, flagInfo] of Object.entries(feats.FLAGS)) {
29+
featureFlags[flagName] = parseFeatureFlagInfo(flagName, flagInfo, root);
4330
}
31+
32+
builder.addArtifact('aws-cdk-lib/feature-flag-report', {
33+
type: ArtifactType.FEATURE_FLAG_REPORT,
34+
properties: {
35+
module: 'aws-cdk-lib',
36+
flags: featureFlags,
37+
},
38+
});
4439
}

packages/aws-cdk-lib/core/test/feature-flag-report.test.ts

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,41 +9,28 @@ jest.mock('../../cx-api', () => {
99
...actual,
1010
CloudAssemblyBuilder: jest.fn().mockImplementation(() => ({
1111
addArtifact: jest.fn(),
12+
buildAssembly: jest.fn().mockReturnValue({
13+
tryGetArtifact: jest.fn().mockReturnValue('aws-cdk-lib/feature-flag-report'),
14+
}),
1215
})),
1316
};
1417
});
1518

1619
describe('generate feature flag report', () => {
17-
test('populates the correct userValue based on the context', () => {
18-
const app = new App({
19-
context: {
20-
'@aws-cdk/aws-ec2:bastionHostUseAmazonLinux2023ByDefault': true,
21-
'@aws-cdk/core:aspectStabilization': false,
22-
},
23-
});
20+
test('feature flag report can be retrieved from CloudAssembly using its artifact ID', () => {
21+
const app = new App();
2422
const builder = new cxapi.CloudAssemblyBuilder('/tmp/test');
25-
const spy = jest.spyOn(builder, 'addArtifact');
2623

2724
generateFeatureFlagReport(builder, app);
2825

29-
expect(spy).toHaveBeenCalledTimes(1);
30-
31-
const [artifactId, artifact] = spy.mock.calls[0];
32-
33-
expect(artifact).toBeDefined();
34-
expect(artifact?.properties).toBeDefined();
35-
36-
const props = artifact.properties as FeatureFlagReportProperties;
37-
const flags = props.flags;
38-
expect(flags).toBeDefined();
39-
40-
expect(flags?.['@aws-cdk/aws-ec2:bastionHostUseAmazonLinux2023ByDefault']?.userValue).toEqual(true);
41-
expect(flags?.['@aws-cdk/core:aspectStabilization']?.userValue).toEqual(false);
26+
const cloudAssembly = builder.buildAssembly();
27+
expect(cloudAssembly.tryGetArtifact('aws-cdk-lib/feature-flag-report')).toEqual('aws-cdk-lib/feature-flag-report');
4228
});
43-
test('successfully creates an artifact stored in the CloudAssembly', () => {
29+
test('report contains context values that represent the feature flags', () => {
4430
const app = new App({
4531
context: {
46-
'@aws-cdk/aws-s3:grantWriteWithoutAcl': true,
32+
'@aws-cdk/aws-ec2:bastionHostUseAmazonLinux2023ByDefault': true,
33+
'@aws-cdk/core:aspectStabilization': false,
4734
},
4835
});
4936
const builder = new cxapi.CloudAssemblyBuilder('/tmp/test');
@@ -55,28 +42,34 @@ describe('generate feature flag report', () => {
5542

5643
const [artifactId, artifact] = spy.mock.calls[0];
5744

58-
expect(artifactId).toEqual('feature flag report');
59-
expect(artifact).toMatchObject({
45+
expect(artifact).toEqual(expect.objectContaining({
6046
type: 'cdk:feature-flag-report',
61-
properties: {
62-
module: '@aws-cdk/core',
63-
},
64-
});
65-
66-
const flags = (artifact.properties as FeatureFlagReportProperties).flags;
67-
expect(flags).toBeDefined();
68-
expect(flags['@aws-cdk/aws-s3:grantWriteWithoutAcl']).toBeDefined();
69-
expect(flags['@aws-cdk/aws-s3:grantWriteWithoutAcl'].userValue).toBe(true);
47+
properties: expect.objectContaining({
48+
module: 'aws-cdk-lib',
49+
flags: expect.objectContaining({
50+
'@aws-cdk/aws-ec2:bastionHostUseAmazonLinux2023ByDefault': expect.objectContaining({
51+
userValue: true,
52+
}),
53+
'@aws-cdk/core:aspectStabilization': expect.objectContaining({
54+
userValue: false,
55+
}),
56+
}),
57+
}),
58+
}));
7059
});
71-
test('defaults userValue to false when not set in context', () => {
60+
test('defaults userValue to undefined when not set in context', () => {
7261
const app = new App();
7362
const builder = new cxapi.CloudAssemblyBuilder('/tmp/test');
7463
const spy = jest.spyOn(builder, 'addArtifact');
7564

7665
generateFeatureFlagReport(builder, app);
7766

7867
const flags = (spy.mock.calls[0][1].properties as FeatureFlagReportProperties).flags;
79-
expect(flags['@aws-cdk/aws-ec2:bastionHostUseAmazonLinux2023ByDefault'].userValue).toBe(false);
68+
expect(flags).toEqual(expect.objectContaining({
69+
'@aws-cdk/aws-ec2:bastionHostUseAmazonLinux2023ByDefault': expect.objectContaining({
70+
userValue: undefined,
71+
}),
72+
}));
8073
});
8174
});
8275

packages/aws-cdk-lib/core/test/stage.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -260,10 +260,10 @@ describe('stage', () => {
260260
displayName: 'StageLevel1',
261261
},
262262
},
263-
'feature flag report': {
263+
'aws-cdk-lib/feature-flag-report': {
264264
type: 'cdk:feature-flag-report',
265265
properties: {
266-
module: '@aws-cdk/core',
266+
module: 'aws-cdk-lib',
267267
flags: expect.any(Object),
268268
},
269269
},
@@ -278,10 +278,10 @@ describe('stage', () => {
278278
displayName: 'StageLevel1/StageLevel2',
279279
},
280280
},
281-
'feature flag report': {
281+
'aws-cdk-lib/feature-flag-report': {
282282
type: 'cdk:feature-flag-report',
283283
properties: {
284-
module: '@aws-cdk/core',
284+
module: 'aws-cdk-lib',
285285
flags: expect.any(Object),
286286
},
287287
},
@@ -296,10 +296,10 @@ describe('stage', () => {
296296
displayName: 'StageLevel1/StageLevel2/StageLevel3',
297297
},
298298
},
299-
'feature flag report': {
299+
'aws-cdk-lib/feature-flag-report': {
300300
type: 'cdk:feature-flag-report',
301301
properties: {
302-
module: '@aws-cdk/core',
302+
module: 'aws-cdk-lib',
303303
flags: expect.any(Object),
304304
},
305305
},

packages/aws-cdk-lib/core/test/synthesis.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ describe('synthesis', () => {
2929
type: 'cdk:tree',
3030
properties: { file: 'tree.json' },
3131
},
32-
'feature flag report': {
32+
'aws-cdk-lib/feature-flag-report': {
3333
type: 'cdk:feature-flag-report',
3434
properties: {
35-
module: '@aws-cdk/core',
35+
module: 'aws-cdk-lib',
3636
flags: expect.any(Object),
3737
},
3838
},

0 commit comments

Comments
 (0)