|
1 |
| -import * as fs from "fs"; |
2 |
| -import * as path from "path"; |
3 |
| - |
4 |
| -import * as core from "@actions/core"; |
5 | 1 | import test from "ava";
|
6 |
| -import * as sinon from "sinon"; |
7 | 2 |
|
8 |
| -import * as actionsUtil from "./actions-util"; |
9 | 3 | import { computeAutomationID } from "./api-client";
|
10 | 4 | import { EnvVar } from "./environment";
|
11 |
| -import * as gitUtils from "./git-utils"; |
12 |
| -import { setupActionsVars, setupTests } from "./testing-utils"; |
13 |
| -import { initializeEnvironment, withTmpDir } from "./util"; |
| 5 | +import { setupTests } from "./testing-utils"; |
| 6 | +import { initializeEnvironment } from "./util"; |
14 | 7 |
|
15 | 8 | setupTests(test);
|
16 | 9 |
|
17 |
| -test("getRef() throws on the empty string", async (t) => { |
18 |
| - process.env["GITHUB_REF"] = ""; |
19 |
| - await t.throwsAsync(gitUtils.getRef); |
20 |
| -}); |
21 |
| - |
22 |
| -test("getRef() returns merge PR ref if GITHUB_SHA still checked out", async (t) => { |
23 |
| - await withTmpDir(async (tmpDir: string) => { |
24 |
| - setupActionsVars(tmpDir, tmpDir); |
25 |
| - const expectedRef = "refs/pull/1/merge"; |
26 |
| - const currentSha = "a".repeat(40); |
27 |
| - process.env["GITHUB_REF"] = expectedRef; |
28 |
| - process.env["GITHUB_SHA"] = currentSha; |
29 |
| - |
30 |
| - const callback = sinon.stub(gitUtils, "getCommitOid"); |
31 |
| - callback.withArgs("HEAD").resolves(currentSha); |
32 |
| - |
33 |
| - const actualRef = await gitUtils.getRef(); |
34 |
| - t.deepEqual(actualRef, expectedRef); |
35 |
| - callback.restore(); |
36 |
| - }); |
37 |
| -}); |
38 |
| - |
39 |
| -test("getRef() returns merge PR ref if GITHUB_REF still checked out but sha has changed (actions checkout@v1)", async (t) => { |
40 |
| - await withTmpDir(async (tmpDir: string) => { |
41 |
| - setupActionsVars(tmpDir, tmpDir); |
42 |
| - const expectedRef = "refs/pull/1/merge"; |
43 |
| - process.env["GITHUB_REF"] = expectedRef; |
44 |
| - process.env["GITHUB_SHA"] = "b".repeat(40); |
45 |
| - const sha = "a".repeat(40); |
46 |
| - |
47 |
| - const callback = sinon.stub(gitUtils, "getCommitOid"); |
48 |
| - callback.withArgs("refs/remotes/pull/1/merge").resolves(sha); |
49 |
| - callback.withArgs("HEAD").resolves(sha); |
50 |
| - |
51 |
| - const actualRef = await gitUtils.getRef(); |
52 |
| - t.deepEqual(actualRef, expectedRef); |
53 |
| - callback.restore(); |
54 |
| - }); |
55 |
| -}); |
56 |
| - |
57 |
| -test("getRef() returns head PR ref if GITHUB_REF no longer checked out", async (t) => { |
58 |
| - await withTmpDir(async (tmpDir: string) => { |
59 |
| - setupActionsVars(tmpDir, tmpDir); |
60 |
| - process.env["GITHUB_REF"] = "refs/pull/1/merge"; |
61 |
| - process.env["GITHUB_SHA"] = "a".repeat(40); |
62 |
| - |
63 |
| - const callback = sinon.stub(gitUtils, "getCommitOid"); |
64 |
| - callback.withArgs(tmpDir, "refs/pull/1/merge").resolves("a".repeat(40)); |
65 |
| - callback.withArgs(tmpDir, "HEAD").resolves("b".repeat(40)); |
66 |
| - |
67 |
| - const actualRef = await gitUtils.getRef(); |
68 |
| - t.deepEqual(actualRef, "refs/pull/1/head"); |
69 |
| - callback.restore(); |
70 |
| - }); |
71 |
| -}); |
72 |
| - |
73 |
| -test("getRef() returns ref provided as an input and ignores current HEAD", async (t) => { |
74 |
| - await withTmpDir(async (tmpDir: string) => { |
75 |
| - setupActionsVars(tmpDir, tmpDir); |
76 |
| - const getAdditionalInputStub = sinon.stub(actionsUtil, "getOptionalInput"); |
77 |
| - getAdditionalInputStub.withArgs("ref").resolves("refs/pull/2/merge"); |
78 |
| - getAdditionalInputStub.withArgs("sha").resolves("b".repeat(40)); |
79 |
| - |
80 |
| - // These values are be ignored |
81 |
| - process.env["GITHUB_REF"] = "refs/pull/1/merge"; |
82 |
| - process.env["GITHUB_SHA"] = "a".repeat(40); |
83 |
| - |
84 |
| - const callback = sinon.stub(gitUtils, "getCommitOid"); |
85 |
| - callback.withArgs("refs/pull/1/merge").resolves("b".repeat(40)); |
86 |
| - callback.withArgs("HEAD").resolves("b".repeat(40)); |
87 |
| - |
88 |
| - const actualRef = await gitUtils.getRef(); |
89 |
| - t.deepEqual(actualRef, "refs/pull/2/merge"); |
90 |
| - callback.restore(); |
91 |
| - getAdditionalInputStub.restore(); |
92 |
| - }); |
93 |
| -}); |
94 |
| - |
95 |
| -test("getRef() returns CODE_SCANNING_REF as a fallback for GITHUB_REF", async (t) => { |
96 |
| - await withTmpDir(async (tmpDir: string) => { |
97 |
| - setupActionsVars(tmpDir, tmpDir); |
98 |
| - const expectedRef = "refs/pull/1/HEAD"; |
99 |
| - const currentSha = "a".repeat(40); |
100 |
| - process.env["CODE_SCANNING_REF"] = expectedRef; |
101 |
| - process.env["GITHUB_REF"] = ""; |
102 |
| - process.env["GITHUB_SHA"] = currentSha; |
103 |
| - |
104 |
| - const actualRef = await gitUtils.getRef(); |
105 |
| - t.deepEqual(actualRef, expectedRef); |
106 |
| - }); |
107 |
| -}); |
108 |
| - |
109 |
| -test("getRef() returns GITHUB_REF over CODE_SCANNING_REF if both are provided", async (t) => { |
110 |
| - await withTmpDir(async (tmpDir: string) => { |
111 |
| - setupActionsVars(tmpDir, tmpDir); |
112 |
| - const expectedRef = "refs/pull/1/merge"; |
113 |
| - const currentSha = "a".repeat(40); |
114 |
| - process.env["CODE_SCANNING_REF"] = "refs/pull/1/HEAD"; |
115 |
| - process.env["GITHUB_REF"] = expectedRef; |
116 |
| - process.env["GITHUB_SHA"] = currentSha; |
117 |
| - |
118 |
| - const actualRef = await gitUtils.getRef(); |
119 |
| - t.deepEqual(actualRef, expectedRef); |
120 |
| - }); |
121 |
| -}); |
122 |
| - |
123 |
| -test("getRef() throws an error if only `ref` is provided as an input", async (t) => { |
124 |
| - await withTmpDir(async (tmpDir: string) => { |
125 |
| - setupActionsVars(tmpDir, tmpDir); |
126 |
| - const getAdditionalInputStub = sinon.stub(actionsUtil, "getOptionalInput"); |
127 |
| - getAdditionalInputStub.withArgs("ref").resolves("refs/pull/1/merge"); |
128 |
| - |
129 |
| - await t.throwsAsync( |
130 |
| - async () => { |
131 |
| - await gitUtils.getRef(); |
132 |
| - }, |
133 |
| - { |
134 |
| - instanceOf: Error, |
135 |
| - message: |
136 |
| - "Both 'ref' and 'sha' are required if one of them is provided.", |
137 |
| - }, |
138 |
| - ); |
139 |
| - getAdditionalInputStub.restore(); |
140 |
| - }); |
141 |
| -}); |
142 |
| - |
143 |
| -test("getRef() throws an error if only `sha` is provided as an input", async (t) => { |
144 |
| - await withTmpDir(async (tmpDir: string) => { |
145 |
| - setupActionsVars(tmpDir, tmpDir); |
146 |
| - process.env["GITHUB_WORKSPACE"] = "/tmp"; |
147 |
| - const getAdditionalInputStub = sinon.stub(actionsUtil, "getOptionalInput"); |
148 |
| - getAdditionalInputStub.withArgs("sha").resolves("a".repeat(40)); |
149 |
| - |
150 |
| - await t.throwsAsync( |
151 |
| - async () => { |
152 |
| - await gitUtils.getRef(); |
153 |
| - }, |
154 |
| - { |
155 |
| - instanceOf: Error, |
156 |
| - message: |
157 |
| - "Both 'ref' and 'sha' are required if one of them is provided.", |
158 |
| - }, |
159 |
| - ); |
160 |
| - getAdditionalInputStub.restore(); |
161 |
| - }); |
162 |
| -}); |
163 |
| - |
164 | 10 | test("computeAutomationID()", async (t) => {
|
165 | 11 | let actualAutomationID = computeAutomationID(
|
166 | 12 | ".github/workflows/codeql-analysis.yml:analyze",
|
@@ -216,150 +62,3 @@ test("initializeEnvironment", (t) => {
|
216 | 62 | initializeEnvironment("1.2.3");
|
217 | 63 | t.deepEqual(process.env[EnvVar.VERSION], "1.2.3");
|
218 | 64 | });
|
219 |
| - |
220 |
| -test("isAnalyzingDefaultBranch()", async (t) => { |
221 |
| - process.env["GITHUB_EVENT_NAME"] = "push"; |
222 |
| - process.env["CODE_SCANNING_IS_ANALYZING_DEFAULT_BRANCH"] = "true"; |
223 |
| - t.deepEqual(await gitUtils.isAnalyzingDefaultBranch(), true); |
224 |
| - process.env["CODE_SCANNING_IS_ANALYZING_DEFAULT_BRANCH"] = "false"; |
225 |
| - |
226 |
| - await withTmpDir(async (tmpDir) => { |
227 |
| - setupActionsVars(tmpDir, tmpDir); |
228 |
| - const envFile = path.join(tmpDir, "event.json"); |
229 |
| - fs.writeFileSync( |
230 |
| - envFile, |
231 |
| - JSON.stringify({ |
232 |
| - repository: { |
233 |
| - default_branch: "main", |
234 |
| - }, |
235 |
| - }), |
236 |
| - ); |
237 |
| - process.env["GITHUB_EVENT_PATH"] = envFile; |
238 |
| - |
239 |
| - process.env["GITHUB_REF"] = "main"; |
240 |
| - process.env["GITHUB_SHA"] = "1234"; |
241 |
| - t.deepEqual(await gitUtils.isAnalyzingDefaultBranch(), true); |
242 |
| - |
243 |
| - process.env["GITHUB_REF"] = "refs/heads/main"; |
244 |
| - t.deepEqual(await gitUtils.isAnalyzingDefaultBranch(), true); |
245 |
| - |
246 |
| - process.env["GITHUB_REF"] = "feature"; |
247 |
| - t.deepEqual(await gitUtils.isAnalyzingDefaultBranch(), false); |
248 |
| - |
249 |
| - fs.writeFileSync( |
250 |
| - envFile, |
251 |
| - JSON.stringify({ |
252 |
| - schedule: "0 0 * * *", |
253 |
| - }), |
254 |
| - ); |
255 |
| - process.env["GITHUB_EVENT_NAME"] = "schedule"; |
256 |
| - process.env["GITHUB_REF"] = "refs/heads/main"; |
257 |
| - t.deepEqual(await gitUtils.isAnalyzingDefaultBranch(), true); |
258 |
| - |
259 |
| - const getAdditionalInputStub = sinon.stub(actionsUtil, "getOptionalInput"); |
260 |
| - getAdditionalInputStub |
261 |
| - .withArgs("ref") |
262 |
| - .resolves("refs/heads/something-else"); |
263 |
| - getAdditionalInputStub |
264 |
| - .withArgs("sha") |
265 |
| - .resolves("0000000000000000000000000000000000000000"); |
266 |
| - process.env["GITHUB_EVENT_NAME"] = "schedule"; |
267 |
| - process.env["GITHUB_REF"] = "refs/heads/main"; |
268 |
| - t.deepEqual(await gitUtils.isAnalyzingDefaultBranch(), false); |
269 |
| - getAdditionalInputStub.restore(); |
270 |
| - }); |
271 |
| -}); |
272 |
| - |
273 |
| -test("determineBaseBranchHeadCommitOid non-pullrequest", async (t) => { |
274 |
| - const infoStub = sinon.stub(core, "info"); |
275 |
| - |
276 |
| - process.env["GITHUB_EVENT_NAME"] = "hucairz"; |
277 |
| - process.env["GITHUB_SHA"] = "100912429fab4cb230e66ffb11e738ac5194e73a"; |
278 |
| - const result = await gitUtils.determineBaseBranchHeadCommitOid(__dirname); |
279 |
| - t.deepEqual(result, undefined); |
280 |
| - t.deepEqual(0, infoStub.callCount); |
281 |
| - |
282 |
| - infoStub.restore(); |
283 |
| -}); |
284 |
| - |
285 |
| -test("determineBaseBranchHeadCommitOid not git repository", async (t) => { |
286 |
| - const infoStub = sinon.stub(core, "info"); |
287 |
| - |
288 |
| - process.env["GITHUB_EVENT_NAME"] = "pull_request"; |
289 |
| - process.env["GITHUB_SHA"] = "100912429fab4cb230e66ffb11e738ac5194e73a"; |
290 |
| - |
291 |
| - await withTmpDir(async (tmpDir) => { |
292 |
| - await gitUtils.determineBaseBranchHeadCommitOid(tmpDir); |
293 |
| - }); |
294 |
| - |
295 |
| - t.deepEqual(1, infoStub.callCount); |
296 |
| - t.deepEqual( |
297 |
| - infoStub.firstCall.args[0], |
298 |
| - "git call failed. Will calculate the base branch SHA on the server. Error: " + |
299 |
| - "The checkout path provided to the action does not appear to be a git repository.", |
300 |
| - ); |
301 |
| - |
302 |
| - infoStub.restore(); |
303 |
| -}); |
304 |
| - |
305 |
| -test("determineBaseBranchHeadCommitOid other error", async (t) => { |
306 |
| - const infoStub = sinon.stub(core, "info"); |
307 |
| - |
308 |
| - process.env["GITHUB_EVENT_NAME"] = "pull_request"; |
309 |
| - process.env["GITHUB_SHA"] = "100912429fab4cb230e66ffb11e738ac5194e73a"; |
310 |
| - const result = await gitUtils.determineBaseBranchHeadCommitOid( |
311 |
| - path.join(__dirname, "../../i-dont-exist"), |
312 |
| - ); |
313 |
| - t.deepEqual(result, undefined); |
314 |
| - t.deepEqual(1, infoStub.callCount); |
315 |
| - t.assert( |
316 |
| - infoStub.firstCall.args[0].startsWith( |
317 |
| - "git call failed. Will calculate the base branch SHA on the server. Error: ", |
318 |
| - ), |
319 |
| - ); |
320 |
| - t.assert( |
321 |
| - !infoStub.firstCall.args[0].endsWith( |
322 |
| - "The checkout path provided to the action does not appear to be a git repository.", |
323 |
| - ), |
324 |
| - ); |
325 |
| - |
326 |
| - infoStub.restore(); |
327 |
| -}); |
328 |
| - |
329 |
| -test("decodeGitFilePath unquoted strings", async (t) => { |
330 |
| - t.deepEqual(gitUtils.decodeGitFilePath("foo"), "foo"); |
331 |
| - t.deepEqual(gitUtils.decodeGitFilePath("foo bar"), "foo bar"); |
332 |
| - t.deepEqual(gitUtils.decodeGitFilePath("foo\\\\bar"), "foo\\\\bar"); |
333 |
| - t.deepEqual(gitUtils.decodeGitFilePath('foo\\"bar'), 'foo\\"bar'); |
334 |
| - t.deepEqual(gitUtils.decodeGitFilePath("foo\\001bar"), "foo\\001bar"); |
335 |
| - t.deepEqual(gitUtils.decodeGitFilePath("foo\\abar"), "foo\\abar"); |
336 |
| - t.deepEqual(gitUtils.decodeGitFilePath("foo\\bbar"), "foo\\bbar"); |
337 |
| - t.deepEqual(gitUtils.decodeGitFilePath("foo\\fbar"), "foo\\fbar"); |
338 |
| - t.deepEqual(gitUtils.decodeGitFilePath("foo\\nbar"), "foo\\nbar"); |
339 |
| - t.deepEqual(gitUtils.decodeGitFilePath("foo\\rbar"), "foo\\rbar"); |
340 |
| - t.deepEqual(gitUtils.decodeGitFilePath("foo\\tbar"), "foo\\tbar"); |
341 |
| - t.deepEqual(gitUtils.decodeGitFilePath("foo\\vbar"), "foo\\vbar"); |
342 |
| - t.deepEqual( |
343 |
| - gitUtils.decodeGitFilePath("\\a\\b\\f\\n\\r\\t\\v"), |
344 |
| - "\\a\\b\\f\\n\\r\\t\\v", |
345 |
| - ); |
346 |
| -}); |
347 |
| - |
348 |
| -test("decodeGitFilePath quoted strings", async (t) => { |
349 |
| - t.deepEqual(gitUtils.decodeGitFilePath('"foo"'), "foo"); |
350 |
| - t.deepEqual(gitUtils.decodeGitFilePath('"foo bar"'), "foo bar"); |
351 |
| - t.deepEqual(gitUtils.decodeGitFilePath('"foo\\\\bar"'), "foo\\bar"); |
352 |
| - t.deepEqual(gitUtils.decodeGitFilePath('"foo\\"bar"'), 'foo"bar'); |
353 |
| - t.deepEqual(gitUtils.decodeGitFilePath('"foo\\001bar"'), "foo\x01bar"); |
354 |
| - t.deepEqual(gitUtils.decodeGitFilePath('"foo\\abar"'), "foo\x07bar"); |
355 |
| - t.deepEqual(gitUtils.decodeGitFilePath('"foo\\bbar"'), "foo\bbar"); |
356 |
| - t.deepEqual(gitUtils.decodeGitFilePath('"foo\\fbar"'), "foo\fbar"); |
357 |
| - t.deepEqual(gitUtils.decodeGitFilePath('"foo\\nbar"'), "foo\nbar"); |
358 |
| - t.deepEqual(gitUtils.decodeGitFilePath('"foo\\rbar"'), "foo\rbar"); |
359 |
| - t.deepEqual(gitUtils.decodeGitFilePath('"foo\\tbar"'), "foo\tbar"); |
360 |
| - t.deepEqual(gitUtils.decodeGitFilePath('"foo\\vbar"'), "foo\vbar"); |
361 |
| - t.deepEqual( |
362 |
| - gitUtils.decodeGitFilePath('"\\a\\b\\f\\n\\r\\t\\v"'), |
363 |
| - "\x07\b\f\n\r\t\v", |
364 |
| - ); |
365 |
| -}); |
0 commit comments