Skip to content

Commit c3ebad2

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

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed

src/actions-util.test.ts

Lines changed: 117 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 originalContext = github.context;
14+
github.context.payload = mockPayload;
15+
try {
16+
return testFn();
17+
} finally {
18+
github.context.payload = originalContext.payload;
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,88 @@ 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() with Default Setup environment variables", (t) => {
116+
withMockedContext({}, () => {
117+
withMockedEnv(
118+
{
119+
CODE_SCANNING_REF: "refs/heads/feature-branch",
120+
CODE_SCANNING_BASE_BRANCH: "main",
121+
},
122+
() => {
123+
t.deepEqual(getPullRequestBranches(), {
124+
base: "main",
125+
head: "refs/heads/feature-branch",
126+
});
127+
t.is(isAnalyzingPullRequest(), true);
128+
},
129+
);
130+
});
131+
});
132+
133+
test("getPullRequestBranches() returns undefined when only CODE_SCANNING_REF is set", (t) => {
134+
withMockedContext({}, () => {
135+
withMockedEnv(
136+
{
137+
CODE_SCANNING_REF: "refs/heads/feature-branch",
138+
CODE_SCANNING_BASE_BRANCH: undefined,
139+
},
140+
() => {
141+
t.is(getPullRequestBranches(), undefined);
142+
t.is(isAnalyzingPullRequest(), false);
143+
},
144+
);
145+
});
146+
});
147+
148+
test("getPullRequestBranches() returns undefined when only CODE_SCANNING_BASE_BRANCH is set", (t) => {
149+
withMockedContext({}, () => {
150+
withMockedEnv(
151+
{
152+
CODE_SCANNING_REF: undefined,
153+
CODE_SCANNING_BASE_BRANCH: "main",
154+
},
155+
() => {
156+
t.is(getPullRequestBranches(), undefined);
157+
t.is(isAnalyzingPullRequest(), false);
158+
},
159+
);
160+
});
161+
});
162+
163+
test("getPullRequestBranches() returns undefined when no PR context", (t) => {
164+
withMockedContext({}, () => {
165+
withMockedEnv(
166+
{
167+
CODE_SCANNING_REF: undefined,
168+
CODE_SCANNING_BASE_BRANCH: undefined,
169+
},
170+
() => {
171+
t.is(getPullRequestBranches(), undefined);
172+
t.is(isAnalyzingPullRequest(), false);
173+
},
174+
);
175+
});
176+
});
177+
61178
test("initializeEnvironment", (t) => {
62179
initializeEnvironment("1.2.3");
63180
t.deepEqual(process.env[EnvVar.VERSION], "1.2.3");

0 commit comments

Comments
 (0)