Skip to content

Commit 8c5122e

Browse files
committed
Add getPullRequestBranches() tests
1 parent 6a51e63 commit 8c5122e

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed

src/actions-util.test.ts

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,47 @@
1+
import * as github from "@actions/github";
12
import test from "ava";
23

4+
import { getPullRequestBranches, isAnalyzingPullRequest } from "./actions-util";
35
import { computeAutomationID } from "./api-client";
46
import { EnvVar } from "./environment";
57
import { setupTests } from "./testing-utils";
68
import { initializeEnvironment } from "./util";
79

810
setupTests(test);
911

12+
function withMockedContext<T>(mockPayload: any, testFn: () => T): T {
13+
const originalPayload = github.context.payload;
14+
github.context.payload = mockPayload;
15+
try {
16+
return testFn();
17+
} finally {
18+
github.context.payload = originalPayload;
19+
}
20+
}
21+
22+
function withMockedEnv<T>(
23+
envVars: Record<string, string | undefined>,
24+
testFn: () => T,
25+
): T {
26+
const originalEnv = { ...process.env };
27+
28+
// Apply environment changes
29+
for (const [key, value] of Object.entries(envVars)) {
30+
if (value === undefined) {
31+
delete process.env[key];
32+
} else {
33+
process.env[key] = value;
34+
}
35+
}
36+
37+
try {
38+
return testFn();
39+
} finally {
40+
// Restore original environment
41+
process.env = originalEnv;
42+
}
43+
}
44+
1045
test("computeAutomationID()", async (t) => {
1146
let actualAutomationID = computeAutomationID(
1247
".github/workflows/codeql-analysis.yml:analyze",
@@ -58,6 +93,102 @@ test("computeAutomationID()", async (t) => {
5893
);
5994
});
6095

96+
test("getPullRequestBranches() with pull request context", (t) => {
97+
withMockedContext(
98+
{
99+
pull_request: {
100+
number: 123,
101+
base: { ref: "main" },
102+
head: { label: "user:feature-branch" },
103+
},
104+
},
105+
() => {
106+
t.deepEqual(getPullRequestBranches(), {
107+
base: "main",
108+
head: "user:feature-branch",
109+
});
110+
t.is(isAnalyzingPullRequest(), true);
111+
},
112+
);
113+
});
114+
115+
test("getPullRequestBranches() returns undefined with push context", (t) => {
116+
withMockedContext(
117+
{
118+
push: {
119+
ref: "refs/heads/main",
120+
},
121+
},
122+
() => {
123+
t.is(getPullRequestBranches(), undefined);
124+
t.is(isAnalyzingPullRequest(), false);
125+
},
126+
);
127+
});
128+
129+
test("getPullRequestBranches() with Default Setup environment variables", (t) => {
130+
withMockedContext({}, () => {
131+
withMockedEnv(
132+
{
133+
CODE_SCANNING_REF: "refs/heads/feature-branch",
134+
CODE_SCANNING_BASE_BRANCH: "main",
135+
},
136+
() => {
137+
t.deepEqual(getPullRequestBranches(), {
138+
base: "main",
139+
head: "refs/heads/feature-branch",
140+
});
141+
t.is(isAnalyzingPullRequest(), true);
142+
},
143+
);
144+
});
145+
});
146+
147+
test("getPullRequestBranches() returns undefined when only CODE_SCANNING_REF is set", (t) => {
148+
withMockedContext({}, () => {
149+
withMockedEnv(
150+
{
151+
CODE_SCANNING_REF: "refs/heads/feature-branch",
152+
CODE_SCANNING_BASE_BRANCH: undefined,
153+
},
154+
() => {
155+
t.is(getPullRequestBranches(), undefined);
156+
t.is(isAnalyzingPullRequest(), false);
157+
},
158+
);
159+
});
160+
});
161+
162+
test("getPullRequestBranches() returns undefined when only CODE_SCANNING_BASE_BRANCH is set", (t) => {
163+
withMockedContext({}, () => {
164+
withMockedEnv(
165+
{
166+
CODE_SCANNING_REF: undefined,
167+
CODE_SCANNING_BASE_BRANCH: "main",
168+
},
169+
() => {
170+
t.is(getPullRequestBranches(), undefined);
171+
t.is(isAnalyzingPullRequest(), false);
172+
},
173+
);
174+
});
175+
});
176+
177+
test("getPullRequestBranches() returns undefined when no PR context", (t) => {
178+
withMockedContext({}, () => {
179+
withMockedEnv(
180+
{
181+
CODE_SCANNING_REF: undefined,
182+
CODE_SCANNING_BASE_BRANCH: undefined,
183+
},
184+
() => {
185+
t.is(getPullRequestBranches(), undefined);
186+
t.is(isAnalyzingPullRequest(), false);
187+
},
188+
);
189+
});
190+
});
191+
61192
test("initializeEnvironment", (t) => {
62193
initializeEnvironment("1.2.3");
63194
t.deepEqual(process.env[EnvVar.VERSION], "1.2.3");

0 commit comments

Comments
 (0)