-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
feature-flag.ts
79 lines (72 loc) · 2.8 KB
/
feature-flag.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import * as path from 'path';
import * as fs from 'fs-extra';
import * as semver from 'semver';
/* eslint-disable jest/no-export */
type Flags = {[key: string]: any};
/**
* jest helper function to be used when testing future flags. Twin function of the `testLegacyBehavior()`.
* This should be used for testing future flags that will be removed in CDKv2, and updated such that these
* will be the default behaviour.
*
* This function is specifically for unit tests that verify the behaviour when future flags are enabled.
*
* The version of CDK is determined by running `scripts/resolve-version.js`, and the logic is as follows:
*
* When run in CDKv1, the specified 'flags' parameter are passed into the CDK App's context, and then
* the test is executed.
*
* When run in CDKv2, the specified 'flags' parameter is ignored, since the default behaviour should be as if
* they are enabled, and then the test is executed.
*/
export function testFutureBehavior<T>(
name: string,
flags: Flags,
cdkApp: new (props?: { context: Flags }) => T,
fn: (app: T) => void,
repoRoot: string = path.join(process.cwd(), '..', '..', '..')) {
const major = cdkMajorVersion(repoRoot);
if (major === 2) {
const app = new cdkApp();
return test(name, async () => fn(app));
}
const app = new cdkApp({ context: flags });
return test(name, () => fn(app));
}
/**
* jest helper function to be used when testing future flags. Twin function of the `testFutureBehavior()`.
* This should be used for testing future flags that will be removed in CDKv2, and updated such that these
* will be the default behaviour.
*
* This function is specifically for unit tests that verify the behaviour when future flags are disabled.
*
* The version of CDK is determined by running `scripts/resolve-version.js`, and the logic is as follows:
*
* When run in CDKv1, the test is executed as normal.
*
* When run in CDKv2, the test is skipped, since the feature flag usage is unsupported and blocked.
*/
export function testLegacyBehavior<T>(
name: string,
cdkApp: new () => T,
fn: (app: T) => void,
repoRoot: string = path.join(process.cwd(), '..', '..', '..')) {
const major = cdkMajorVersion(repoRoot);
if (major === 2) {
return;
}
const app = new cdkApp();
return test(name, () => fn(app));
}
function cdkMajorVersion(repoRoot: string) {
const resolveVersionPath = path.join(repoRoot, 'scripts', 'resolve-version.js');
if (!fs.existsSync(resolveVersionPath)) {
throw new Error(`file not present at path ${resolveVersionPath}. You will likely need to set 'repoRoot'.`);
}
// eslint-disable-next-line @typescript-eslint/no-require-imports
const ver = require(resolveVersionPath).version;
const sem = semver.parse(ver);
if (!sem) {
throw new Error(`version ${ver} is not a semver`);
}
return sem.major;
}