Skip to content

Commit eed3b75

Browse files
committed
Add diff-informed-analysis-utils.test.ts
1 parent ca3b5e1 commit eed3b75

File tree

1 file changed

+179
-0
lines changed

1 file changed

+179
-0
lines changed
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
import test, { ExecutionContext } from "ava";
2+
import * as sinon from "sinon";
3+
4+
import * as actionsUtil from "./actions-util";
5+
import type { PullRequestBranches } from "./actions-util";
6+
import * as apiClient from "./api-client";
7+
import { shouldPerformDiffInformedAnalysis } from "./diff-informed-analysis-utils";
8+
import { Feature, Features } from "./feature-flags";
9+
import { getRunnerLogger } from "./logging";
10+
import { parseRepositoryNwo } from "./repository";
11+
import {
12+
setupTests,
13+
mockCodeQLVersion,
14+
mockFeatureFlagApiEndpoint,
15+
setupActionsVars,
16+
} from "./testing-utils";
17+
import { GitHubVariant, withTmpDir } from "./util";
18+
import type { GitHubVersion } from "./util";
19+
20+
setupTests(test);
21+
22+
interface DiffInformedAnalysisTestCase {
23+
featureEnabled: boolean;
24+
gitHubVersion: GitHubVersion;
25+
pullRequestBranches: PullRequestBranches;
26+
codeQLVersion: string;
27+
diffInformedQueriesEnvVar?: boolean;
28+
}
29+
30+
const defaultTestCase: DiffInformedAnalysisTestCase = {
31+
featureEnabled: true,
32+
gitHubVersion: {
33+
type: GitHubVariant.DOTCOM,
34+
},
35+
pullRequestBranches: {
36+
base: "main",
37+
head: "feature-branch",
38+
},
39+
codeQLVersion: "2.21.0",
40+
};
41+
42+
const testShouldPerformDiffInformedAnalysis = test.macro({
43+
exec: async (
44+
t: ExecutionContext,
45+
_title: string,
46+
partialTestCase: Partial<DiffInformedAnalysisTestCase>,
47+
expectedResult: boolean,
48+
) => {
49+
return await withTmpDir(async (tmpDir) => {
50+
setupActionsVars(tmpDir, tmpDir);
51+
52+
const testCase = { ...defaultTestCase, ...partialTestCase };
53+
const logger = getRunnerLogger(true);
54+
const codeql = mockCodeQLVersion(testCase.codeQLVersion);
55+
56+
if (testCase.diffInformedQueriesEnvVar !== undefined) {
57+
process.env.CODEQL_ACTION_DIFF_INFORMED_QUERIES =
58+
testCase.diffInformedQueriesEnvVar.toString();
59+
} else {
60+
delete process.env.CODEQL_ACTION_DIFF_INFORMED_QUERIES;
61+
}
62+
63+
const features = new Features(
64+
testCase.gitHubVersion,
65+
parseRepositoryNwo("github/example"),
66+
tmpDir,
67+
logger,
68+
);
69+
mockFeatureFlagApiEndpoint(200, {
70+
[Feature.DiffInformedQueries]: testCase.featureEnabled,
71+
});
72+
73+
const getGitHubVersionStub = sinon
74+
.stub(apiClient, "getGitHubVersion")
75+
.resolves(testCase.gitHubVersion);
76+
77+
let getPullRequestBranchesStub: sinon.SinonStub | undefined;
78+
if (testCase.pullRequestBranches !== undefined) {
79+
getPullRequestBranchesStub = sinon
80+
.stub(actionsUtil, "getPullRequestBranches")
81+
.returns(testCase.pullRequestBranches);
82+
}
83+
84+
const result = await shouldPerformDiffInformedAnalysis(
85+
codeql,
86+
features,
87+
logger,
88+
);
89+
90+
t.is(result, expectedResult);
91+
92+
delete process.env.CODEQL_ACTION_DIFF_INFORMED_QUERIES;
93+
94+
getGitHubVersionStub.restore();
95+
if (getPullRequestBranchesStub) {
96+
getPullRequestBranchesStub.restore();
97+
}
98+
});
99+
},
100+
title: (_, title) => `shouldPerformDiffInformedAnalysis: ${title}`,
101+
});
102+
103+
test(
104+
testShouldPerformDiffInformedAnalysis,
105+
"returns true in the default test case",
106+
{},
107+
true,
108+
);
109+
110+
test(
111+
testShouldPerformDiffInformedAnalysis,
112+
"returns false when feature flag is disabled from the API",
113+
{
114+
featureEnabled: false,
115+
},
116+
false,
117+
);
118+
119+
test(
120+
testShouldPerformDiffInformedAnalysis,
121+
"returns false when CODEQL_ACTION_DIFF_INFORMED_QUERIES is set to false",
122+
{
123+
featureEnabled: true,
124+
diffInformedQueriesEnvVar: false,
125+
},
126+
false,
127+
);
128+
129+
test(
130+
testShouldPerformDiffInformedAnalysis,
131+
"returns true when CODEQL_ACTION_DIFF_INFORMED_QUERIES is set to true",
132+
{
133+
featureEnabled: false,
134+
diffInformedQueriesEnvVar: true,
135+
},
136+
true,
137+
);
138+
139+
test(
140+
testShouldPerformDiffInformedAnalysis,
141+
"returns false for CodeQL version 2.20.0",
142+
{
143+
codeQLVersion: "2.20.0",
144+
},
145+
false,
146+
);
147+
148+
test(
149+
testShouldPerformDiffInformedAnalysis,
150+
"returns false for GHES version 3.18.5",
151+
{
152+
gitHubVersion: {
153+
type: GitHubVariant.GHES,
154+
version: "3.18.5",
155+
},
156+
},
157+
false,
158+
);
159+
160+
test(
161+
testShouldPerformDiffInformedAnalysis,
162+
"returns true for GHES version 3.19.0",
163+
{
164+
gitHubVersion: {
165+
type: GitHubVariant.GHES,
166+
version: "3.19.0",
167+
},
168+
},
169+
true,
170+
);
171+
172+
test(
173+
testShouldPerformDiffInformedAnalysis,
174+
"returns false when not a pull request",
175+
{
176+
pullRequestBranches: undefined,
177+
},
178+
false,
179+
);

0 commit comments

Comments
 (0)