|
| 1 | +import * as github from "@actions/github"; |
1 | 2 | import test from "ava";
|
2 | 3 |
|
| 4 | +import { getPullRequestBranches, isAnalyzingPullRequest } from "./actions-util"; |
3 | 5 | import { computeAutomationID } from "./api-client";
|
4 | 6 | import { EnvVar } from "./environment";
|
5 | 7 | import { setupTests } from "./testing-utils";
|
6 | 8 | import { initializeEnvironment } from "./util";
|
7 | 9 |
|
8 | 10 | setupTests(test);
|
9 | 11 |
|
| 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 | + |
10 | 45 | test("computeAutomationID()", async (t) => {
|
11 | 46 | let actualAutomationID = computeAutomationID(
|
12 | 47 | ".github/workflows/codeql-analysis.yml:analyze",
|
@@ -58,6 +93,102 @@ test("computeAutomationID()", async (t) => {
|
58 | 93 | );
|
59 | 94 | });
|
60 | 95 |
|
| 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 | + |
61 | 192 | test("initializeEnvironment", (t) => {
|
62 | 193 | initializeEnvironment("1.2.3");
|
63 | 194 | t.deepEqual(process.env[EnvVar.VERSION], "1.2.3");
|
|
0 commit comments