Skip to content

Commit b0cd76b

Browse files
committed
Move Git functions to git-utils.ts
1 parent dfed55c commit b0cd76b

13 files changed

+507
-488
lines changed

src/actions-util.test.ts

+49-48
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ import * as sinon from "sinon";
88
import * as actionsUtil from "./actions-util";
99
import { computeAutomationID } from "./api-client";
1010
import { EnvVar } from "./environment";
11+
import * as gitUtils from "./git-utils";
1112
import { setupActionsVars, setupTests } from "./testing-utils";
1213
import { initializeEnvironment, withTmpDir } from "./util";
1314

1415
setupTests(test);
1516

1617
test("getRef() throws on the empty string", async (t) => {
1718
process.env["GITHUB_REF"] = "";
18-
await t.throwsAsync(actionsUtil.getRef);
19+
await t.throwsAsync(gitUtils.getRef);
1920
});
2021

2122
test("getRef() returns merge PR ref if GITHUB_SHA still checked out", async (t) => {
@@ -26,10 +27,10 @@ test("getRef() returns merge PR ref if GITHUB_SHA still checked out", async (t)
2627
process.env["GITHUB_REF"] = expectedRef;
2728
process.env["GITHUB_SHA"] = currentSha;
2829

29-
const callback = sinon.stub(actionsUtil, "getCommitOid");
30+
const callback = sinon.stub(gitUtils, "getCommitOid");
3031
callback.withArgs("HEAD").resolves(currentSha);
3132

32-
const actualRef = await actionsUtil.getRef();
33+
const actualRef = await gitUtils.getRef();
3334
t.deepEqual(actualRef, expectedRef);
3435
callback.restore();
3536
});
@@ -43,11 +44,11 @@ test("getRef() returns merge PR ref if GITHUB_REF still checked out but sha has
4344
process.env["GITHUB_SHA"] = "b".repeat(40);
4445
const sha = "a".repeat(40);
4546

46-
const callback = sinon.stub(actionsUtil, "getCommitOid");
47+
const callback = sinon.stub(gitUtils, "getCommitOid");
4748
callback.withArgs("refs/remotes/pull/1/merge").resolves(sha);
4849
callback.withArgs("HEAD").resolves(sha);
4950

50-
const actualRef = await actionsUtil.getRef();
51+
const actualRef = await gitUtils.getRef();
5152
t.deepEqual(actualRef, expectedRef);
5253
callback.restore();
5354
});
@@ -59,11 +60,11 @@ test("getRef() returns head PR ref if GITHUB_REF no longer checked out", async (
5960
process.env["GITHUB_REF"] = "refs/pull/1/merge";
6061
process.env["GITHUB_SHA"] = "a".repeat(40);
6162

62-
const callback = sinon.stub(actionsUtil, "getCommitOid");
63+
const callback = sinon.stub(gitUtils, "getCommitOid");
6364
callback.withArgs(tmpDir, "refs/pull/1/merge").resolves("a".repeat(40));
6465
callback.withArgs(tmpDir, "HEAD").resolves("b".repeat(40));
6566

66-
const actualRef = await actionsUtil.getRef();
67+
const actualRef = await gitUtils.getRef();
6768
t.deepEqual(actualRef, "refs/pull/1/head");
6869
callback.restore();
6970
});
@@ -80,11 +81,11 @@ test("getRef() returns ref provided as an input and ignores current HEAD", async
8081
process.env["GITHUB_REF"] = "refs/pull/1/merge";
8182
process.env["GITHUB_SHA"] = "a".repeat(40);
8283

83-
const callback = sinon.stub(actionsUtil, "getCommitOid");
84+
const callback = sinon.stub(gitUtils, "getCommitOid");
8485
callback.withArgs("refs/pull/1/merge").resolves("b".repeat(40));
8586
callback.withArgs("HEAD").resolves("b".repeat(40));
8687

87-
const actualRef = await actionsUtil.getRef();
88+
const actualRef = await gitUtils.getRef();
8889
t.deepEqual(actualRef, "refs/pull/2/merge");
8990
callback.restore();
9091
getAdditionalInputStub.restore();
@@ -100,7 +101,7 @@ test("getRef() returns CODE_SCANNING_REF as a fallback for GITHUB_REF", async (t
100101
process.env["GITHUB_REF"] = "";
101102
process.env["GITHUB_SHA"] = currentSha;
102103

103-
const actualRef = await actionsUtil.getRef();
104+
const actualRef = await gitUtils.getRef();
104105
t.deepEqual(actualRef, expectedRef);
105106
});
106107
});
@@ -114,7 +115,7 @@ test("getRef() returns GITHUB_REF over CODE_SCANNING_REF if both are provided",
114115
process.env["GITHUB_REF"] = expectedRef;
115116
process.env["GITHUB_SHA"] = currentSha;
116117

117-
const actualRef = await actionsUtil.getRef();
118+
const actualRef = await gitUtils.getRef();
118119
t.deepEqual(actualRef, expectedRef);
119120
});
120121
});
@@ -127,7 +128,7 @@ test("getRef() throws an error if only `ref` is provided as an input", async (t)
127128

128129
await t.throwsAsync(
129130
async () => {
130-
await actionsUtil.getRef();
131+
await gitUtils.getRef();
131132
},
132133
{
133134
instanceOf: Error,
@@ -148,7 +149,7 @@ test("getRef() throws an error if only `sha` is provided as an input", async (t)
148149

149150
await t.throwsAsync(
150151
async () => {
151-
await actionsUtil.getRef();
152+
await gitUtils.getRef();
152153
},
153154
{
154155
instanceOf: Error,
@@ -219,7 +220,7 @@ test("initializeEnvironment", (t) => {
219220
test("isAnalyzingDefaultBranch()", async (t) => {
220221
process.env["GITHUB_EVENT_NAME"] = "push";
221222
process.env["CODE_SCANNING_IS_ANALYZING_DEFAULT_BRANCH"] = "true";
222-
t.deepEqual(await actionsUtil.isAnalyzingDefaultBranch(), true);
223+
t.deepEqual(await gitUtils.isAnalyzingDefaultBranch(), true);
223224
process.env["CODE_SCANNING_IS_ANALYZING_DEFAULT_BRANCH"] = "false";
224225

225226
await withTmpDir(async (tmpDir) => {
@@ -237,13 +238,13 @@ test("isAnalyzingDefaultBranch()", async (t) => {
237238

238239
process.env["GITHUB_REF"] = "main";
239240
process.env["GITHUB_SHA"] = "1234";
240-
t.deepEqual(await actionsUtil.isAnalyzingDefaultBranch(), true);
241+
t.deepEqual(await gitUtils.isAnalyzingDefaultBranch(), true);
241242

242243
process.env["GITHUB_REF"] = "refs/heads/main";
243-
t.deepEqual(await actionsUtil.isAnalyzingDefaultBranch(), true);
244+
t.deepEqual(await gitUtils.isAnalyzingDefaultBranch(), true);
244245

245246
process.env["GITHUB_REF"] = "feature";
246-
t.deepEqual(await actionsUtil.isAnalyzingDefaultBranch(), false);
247+
t.deepEqual(await gitUtils.isAnalyzingDefaultBranch(), false);
247248

248249
fs.writeFileSync(
249250
envFile,
@@ -253,7 +254,7 @@ test("isAnalyzingDefaultBranch()", async (t) => {
253254
);
254255
process.env["GITHUB_EVENT_NAME"] = "schedule";
255256
process.env["GITHUB_REF"] = "refs/heads/main";
256-
t.deepEqual(await actionsUtil.isAnalyzingDefaultBranch(), true);
257+
t.deepEqual(await gitUtils.isAnalyzingDefaultBranch(), true);
257258

258259
const getAdditionalInputStub = sinon.stub(actionsUtil, "getOptionalInput");
259260
getAdditionalInputStub
@@ -264,7 +265,7 @@ test("isAnalyzingDefaultBranch()", async (t) => {
264265
.resolves("0000000000000000000000000000000000000000");
265266
process.env["GITHUB_EVENT_NAME"] = "schedule";
266267
process.env["GITHUB_REF"] = "refs/heads/main";
267-
t.deepEqual(await actionsUtil.isAnalyzingDefaultBranch(), false);
268+
t.deepEqual(await gitUtils.isAnalyzingDefaultBranch(), false);
268269
getAdditionalInputStub.restore();
269270
});
270271
});
@@ -274,7 +275,7 @@ test("determineBaseBranchHeadCommitOid non-pullrequest", async (t) => {
274275

275276
process.env["GITHUB_EVENT_NAME"] = "hucairz";
276277
process.env["GITHUB_SHA"] = "100912429fab4cb230e66ffb11e738ac5194e73a";
277-
const result = await actionsUtil.determineBaseBranchHeadCommitOid(__dirname);
278+
const result = await gitUtils.determineBaseBranchHeadCommitOid(__dirname);
278279
t.deepEqual(result, undefined);
279280
t.deepEqual(0, infoStub.callCount);
280281

@@ -288,7 +289,7 @@ test("determineBaseBranchHeadCommitOid not git repository", async (t) => {
288289
process.env["GITHUB_SHA"] = "100912429fab4cb230e66ffb11e738ac5194e73a";
289290

290291
await withTmpDir(async (tmpDir) => {
291-
await actionsUtil.determineBaseBranchHeadCommitOid(tmpDir);
292+
await gitUtils.determineBaseBranchHeadCommitOid(tmpDir);
292293
});
293294

294295
t.deepEqual(1, infoStub.callCount);
@@ -306,7 +307,7 @@ test("determineBaseBranchHeadCommitOid other error", async (t) => {
306307

307308
process.env["GITHUB_EVENT_NAME"] = "pull_request";
308309
process.env["GITHUB_SHA"] = "100912429fab4cb230e66ffb11e738ac5194e73a";
309-
const result = await actionsUtil.determineBaseBranchHeadCommitOid(
310+
const result = await gitUtils.determineBaseBranchHeadCommitOid(
310311
path.join(__dirname, "../../i-dont-exist"),
311312
);
312313
t.deepEqual(result, undefined);
@@ -326,39 +327,39 @@ test("determineBaseBranchHeadCommitOid other error", async (t) => {
326327
});
327328

328329
test("decodeGitFilePath unquoted strings", async (t) => {
329-
t.deepEqual(actionsUtil.decodeGitFilePath("foo"), "foo");
330-
t.deepEqual(actionsUtil.decodeGitFilePath("foo bar"), "foo bar");
331-
t.deepEqual(actionsUtil.decodeGitFilePath("foo\\\\bar"), "foo\\\\bar");
332-
t.deepEqual(actionsUtil.decodeGitFilePath('foo\\"bar'), 'foo\\"bar');
333-
t.deepEqual(actionsUtil.decodeGitFilePath("foo\\001bar"), "foo\\001bar");
334-
t.deepEqual(actionsUtil.decodeGitFilePath("foo\\abar"), "foo\\abar");
335-
t.deepEqual(actionsUtil.decodeGitFilePath("foo\\bbar"), "foo\\bbar");
336-
t.deepEqual(actionsUtil.decodeGitFilePath("foo\\fbar"), "foo\\fbar");
337-
t.deepEqual(actionsUtil.decodeGitFilePath("foo\\nbar"), "foo\\nbar");
338-
t.deepEqual(actionsUtil.decodeGitFilePath("foo\\rbar"), "foo\\rbar");
339-
t.deepEqual(actionsUtil.decodeGitFilePath("foo\\tbar"), "foo\\tbar");
340-
t.deepEqual(actionsUtil.decodeGitFilePath("foo\\vbar"), "foo\\vbar");
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");
341342
t.deepEqual(
342-
actionsUtil.decodeGitFilePath("\\a\\b\\f\\n\\r\\t\\v"),
343+
gitUtils.decodeGitFilePath("\\a\\b\\f\\n\\r\\t\\v"),
343344
"\\a\\b\\f\\n\\r\\t\\v",
344345
);
345346
});
346347

347348
test("decodeGitFilePath quoted strings", async (t) => {
348-
t.deepEqual(actionsUtil.decodeGitFilePath('"foo"'), "foo");
349-
t.deepEqual(actionsUtil.decodeGitFilePath('"foo bar"'), "foo bar");
350-
t.deepEqual(actionsUtil.decodeGitFilePath('"foo\\\\bar"'), "foo\\bar");
351-
t.deepEqual(actionsUtil.decodeGitFilePath('"foo\\"bar"'), 'foo"bar');
352-
t.deepEqual(actionsUtil.decodeGitFilePath('"foo\\001bar"'), "foo\x01bar");
353-
t.deepEqual(actionsUtil.decodeGitFilePath('"foo\\abar"'), "foo\x07bar");
354-
t.deepEqual(actionsUtil.decodeGitFilePath('"foo\\bbar"'), "foo\bbar");
355-
t.deepEqual(actionsUtil.decodeGitFilePath('"foo\\fbar"'), "foo\fbar");
356-
t.deepEqual(actionsUtil.decodeGitFilePath('"foo\\nbar"'), "foo\nbar");
357-
t.deepEqual(actionsUtil.decodeGitFilePath('"foo\\rbar"'), "foo\rbar");
358-
t.deepEqual(actionsUtil.decodeGitFilePath('"foo\\tbar"'), "foo\tbar");
359-
t.deepEqual(actionsUtil.decodeGitFilePath('"foo\\vbar"'), "foo\vbar");
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");
360361
t.deepEqual(
361-
actionsUtil.decodeGitFilePath('"\\a\\b\\f\\n\\r\\t\\v"'),
362+
gitUtils.decodeGitFilePath('"\\a\\b\\f\\n\\r\\t\\v"'),
362363
"\x07\b\f\n\r\t\v",
363364
);
364365
});

0 commit comments

Comments
 (0)