-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathbuildInfo.test.js
More file actions
113 lines (96 loc) · 3.45 KB
/
buildInfo.test.js
File metadata and controls
113 lines (96 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { afterEach, beforeEach, describe, it, expect, vi } from "vitest"
import { commitUrl, formatBuildTime, shortSha } from "../src/buildInfo"
import { resolveGitCommit } from "../src/resolveGitCommit"
describe("formatBuildTime", () => {
it("formats an ISO timestamp as 'YYYY-MM-DD HH:mm UTC'", () => {
expect(formatBuildTime("2026-04-30T14:30:42.123Z")).toBe(
"2026-04-30 14:30 UTC"
)
})
it("drops seconds and fractional seconds", () => {
expect(formatBuildTime("2026-01-01T00:00:59.999Z")).toBe(
"2026-01-01 00:00 UTC"
)
})
it("returns empty string for falsy input", () => {
expect(formatBuildTime("")).toBe("")
expect(formatBuildTime(null)).toBe("")
expect(formatBuildTime(undefined)).toBe("")
})
})
describe("shortSha", () => {
it("truncates a full git SHA to 7 chars", () => {
expect(shortSha("a1b2c3d4e5f67890abcdef1234567890abcdef12")).toBe("a1b2c3d")
})
it("returns the input unchanged when shorter than 7 chars", () => {
expect(shortSha("abc")).toBe("abc")
})
it("returns empty string for falsy input", () => {
expect(shortSha("")).toBe("")
expect(shortSha(null)).toBe("")
expect(shortSha(undefined)).toBe("")
})
})
describe("commitUrl", () => {
it("joins repo URL and SHA with /commit/", () => {
expect(
commitUrl("https://github.com/skohub-io/skohub-vocabs", "a1b2c3d")
).toBe("https://github.com/skohub-io/skohub-vocabs/commit/a1b2c3d")
})
it("strips a single trailing slash from the repo URL", () => {
expect(
commitUrl("https://github.com/skohub-io/skohub-vocabs/", "a1b2c3d")
).toBe("https://github.com/skohub-io/skohub-vocabs/commit/a1b2c3d")
})
it("returns empty string when either input is missing", () => {
expect(commitUrl("", "a1b2c3d")).toBe("")
expect(commitUrl("https://example.com/r", "")).toBe("")
expect(commitUrl(null, null)).toBe("")
})
})
describe("resolveGitCommit", () => {
const ORIGINAL_ENV = process.env
let execSync
beforeEach(() => {
process.env = { ...ORIGINAL_ENV }
delete process.env.GITHUB_SHA
delete process.env.CI_COMMIT_SHA
execSync = vi.fn()
})
afterEach(() => {
process.env = ORIGINAL_ENV
})
it("returns GITHUB_SHA when set", () => {
process.env.GITHUB_SHA = "github1111111111111111111111111111111111"
expect(resolveGitCommit({ execSync })).toBe(
"github1111111111111111111111111111111111"
)
expect(execSync).not.toHaveBeenCalled()
})
it("returns CI_COMMIT_SHA when GITHUB_SHA is absent", () => {
process.env.CI_COMMIT_SHA = "gitlab2222222222222222222222222222222222"
expect(resolveGitCommit({ execSync })).toBe(
"gitlab2222222222222222222222222222222222"
)
expect(execSync).not.toHaveBeenCalled()
})
it("prefers GITHUB_SHA over CI_COMMIT_SHA", () => {
process.env.GITHUB_SHA = "gh"
process.env.CI_COMMIT_SHA = "gl"
expect(resolveGitCommit({ execSync })).toBe("gh")
})
it("falls back to `git rev-parse HEAD` when no env var is set", () => {
execSync.mockReturnValue("local333333333\n")
expect(resolveGitCommit({ execSync })).toBe("local333333333")
expect(execSync).toHaveBeenCalledWith("git rev-parse HEAD", {
encoding: "utf8",
stdio: ["ignore", "pipe", "ignore"],
})
})
it("returns null when env vars are missing and git fails", () => {
execSync.mockImplementation(() => {
throw new Error("not a git repository")
})
expect(resolveGitCommit({ execSync })).toBeNull()
})
})