Skip to content

Commit a827b58

Browse files
committed
Move FF test utils out of main file
1 parent 81143cc commit a827b58

File tree

2 files changed

+81
-64
lines changed

2 files changed

+81
-64
lines changed

src/feature-flags.test.ts

Lines changed: 6 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,30 @@
11
import * as fs from "fs";
22
import * as path from "path";
33

4-
import test, { ExecutionContext } from "ava";
4+
import test from "ava";
55

66
import * as defaults from "./defaults.json";
77
import { EnvVar } from "./environment";
88
import {
99
Feature,
1010
featureConfig,
11-
FeatureEnablement,
1211
FEATURE_FLAGS_FILE_NAME,
1312
FeatureConfig,
14-
FeatureWithoutCLI,
15-
initFeatures,
1613
} from "./feature-flags";
17-
import { getRunnerLogger } from "./logging";
18-
import { parseRepositoryNwo } from "./repository";
14+
import {
15+
setUpFeatureFlagTests,
16+
getFeatureIncludingCodeQlIfRequired,
17+
assertAllFeaturesUndefinedInApi,
18+
} from "./feature-flags/testing-util";
1919
import {
2020
getRecordingLogger,
2121
initializeFeatures,
2222
LoggedMessage,
2323
mockCodeQLVersion,
2424
mockFeatureFlagApiEndpoint,
25-
setupActionsVars,
2625
setupTests,
2726
stubFeatureFlagApiEndpoint,
2827
} from "./testing-utils";
29-
import { ToolsFeature } from "./tools-features";
30-
import * as util from "./util";
3128
import { GitHubVariant, initializeEnvironment, withTmpDir } from "./util";
3229

3330
setupTests(test);
@@ -36,8 +33,6 @@ test.beforeEach(() => {
3633
initializeEnvironment("1.2.3");
3734
});
3835

39-
const testRepositoryNwo = parseRepositoryNwo("github/example");
40-
4136
test(`All features are disabled if running against GHES`, async (t) => {
4237
await withTmpDir(async (tmpDir) => {
4338
const loggedMessages = [];
@@ -558,56 +553,3 @@ test("initFeatures returns an `OfflineFeatures` instance in CCR", async (t) => {
558553
t.is("OfflineFeatures", features.constructor.name);
559554
});
560555
});
561-
562-
function assertAllFeaturesUndefinedInApi(
563-
t: ExecutionContext<unknown>,
564-
loggedMessages: LoggedMessage[],
565-
) {
566-
for (const feature of Object.keys(featureConfig)) {
567-
t.assert(
568-
loggedMessages.find(
569-
(v) =>
570-
v.type === "debug" &&
571-
(v.message as string).includes(feature) &&
572-
(v.message as string).includes("undefined in API response"),
573-
) !== undefined,
574-
);
575-
}
576-
}
577-
578-
function setUpFeatureFlagTests(
579-
tmpDir: string,
580-
logger = getRunnerLogger(true),
581-
gitHubVersion = { type: GitHubVariant.DOTCOM } as util.GitHubVersion,
582-
): FeatureEnablement {
583-
setupActionsVars(tmpDir, tmpDir);
584-
585-
return initFeatures(gitHubVersion, testRepositoryNwo, tmpDir, logger);
586-
}
587-
588-
/**
589-
* Returns an argument to pass to `getValue` that if required includes a CodeQL object meeting the
590-
* minimum version or tool feature requirements specified by the feature.
591-
*/
592-
function getFeatureIncludingCodeQlIfRequired(
593-
features: FeatureEnablement,
594-
feature: Feature,
595-
) {
596-
const config = featureConfig[
597-
feature
598-
] satisfies FeatureConfig as FeatureConfig;
599-
if (
600-
config.minimumVersion === undefined &&
601-
config.toolsFeature === undefined
602-
) {
603-
return features.getValue(feature as FeatureWithoutCLI);
604-
}
605-
606-
return features.getValue(
607-
feature,
608-
mockCodeQLVersion(
609-
"9.9.9",
610-
Object.fromEntries(Object.values(ToolsFeature).map((v) => [v, true])),
611-
),
612-
);
613-
}

src/feature-flags/testing-util.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { type ExecutionContext } from "ava";
2+
3+
import {
4+
Feature,
5+
featureConfig,
6+
FeatureConfig,
7+
FeatureEnablement,
8+
FeatureWithoutCLI,
9+
initFeatures,
10+
} from "../feature-flags";
11+
import { getRunnerLogger } from "../logging";
12+
import { parseRepositoryNwo } from "../repository";
13+
import {
14+
LoggedMessage,
15+
mockCodeQLVersion,
16+
setupActionsVars,
17+
} from "../testing-utils";
18+
import { ToolsFeature } from "../tools-features";
19+
import { GitHubVariant } from "../util";
20+
import * as util from "../util";
21+
22+
const testRepositoryNwo = parseRepositoryNwo("github/example");
23+
24+
export function assertAllFeaturesUndefinedInApi(
25+
t: ExecutionContext<unknown>,
26+
loggedMessages: LoggedMessage[],
27+
) {
28+
for (const feature of Object.keys(featureConfig)) {
29+
t.assert(
30+
loggedMessages.find(
31+
(v) =>
32+
v.type === "debug" &&
33+
(v.message as string).includes(feature) &&
34+
(v.message as string).includes("undefined in API response"),
35+
) !== undefined,
36+
);
37+
}
38+
}
39+
40+
export function setUpFeatureFlagTests(
41+
tmpDir: string,
42+
logger = getRunnerLogger(true),
43+
gitHubVersion = { type: GitHubVariant.DOTCOM } as util.GitHubVersion,
44+
): FeatureEnablement {
45+
setupActionsVars(tmpDir, tmpDir);
46+
47+
return initFeatures(gitHubVersion, testRepositoryNwo, tmpDir, logger);
48+
}
49+
50+
/**
51+
* Returns an argument to pass to `getValue` that if required includes a CodeQL object meeting the
52+
* minimum version or tool feature requirements specified by the feature.
53+
*/
54+
export function getFeatureIncludingCodeQlIfRequired(
55+
features: FeatureEnablement,
56+
feature: Feature,
57+
) {
58+
const config = featureConfig[
59+
feature
60+
] satisfies FeatureConfig as FeatureConfig;
61+
if (
62+
config.minimumVersion === undefined &&
63+
config.toolsFeature === undefined
64+
) {
65+
return features.getValue(feature as FeatureWithoutCLI);
66+
}
67+
68+
return features.getValue(
69+
feature,
70+
mockCodeQLVersion(
71+
"9.9.9",
72+
Object.fromEntries(Object.values(ToolsFeature).map((v) => [v, true])),
73+
),
74+
);
75+
}

0 commit comments

Comments
 (0)