|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { spawnSync } from "node:child_process"; |
| 3 | +import { join } from "node:path"; |
| 4 | + |
| 5 | +import pkg from "../package.json" with { type: "json" }; |
| 6 | +import { withTempDir } from "../testkit/temporary-dirs.js"; |
| 7 | +import { |
| 8 | + collectGitInfo, |
| 9 | + composeDisplayVersion, |
| 10 | + type GitInfo, |
| 11 | + type RunResult, |
| 12 | +} from "./build-info.js"; |
| 13 | + |
| 14 | +const repoRoot = join(import.meta.dir, ".."); |
| 15 | +const scriptPath = join(repoRoot, "scripts", "build-info.ts"); |
| 16 | + |
| 17 | +const DISPLAY_VERSION_RE = |
| 18 | + /^v\d+\.\d+\.\d+(-\d+-g[0-9a-f]+)?(\+g[0-9a-f]+)?(-dirty)?$/; |
| 19 | + |
| 20 | +describe("composeDisplayVersion", () => { |
| 21 | + test("on tag, clean tree -> plain version", () => { |
| 22 | + const info: GitInfo = { |
| 23 | + tag: "v0.3.36", |
| 24 | + count: 0, |
| 25 | + hash: "9af7e1e", |
| 26 | + dirty: false, |
| 27 | + }; |
| 28 | + expect(composeDisplayVersion("0.3.36", info)).toBe("v0.3.36"); |
| 29 | + }); |
| 30 | + |
| 31 | + test("on tag, dirty tree -> -dirty suffix", () => { |
| 32 | + const info: GitInfo = { |
| 33 | + tag: "v0.3.36", |
| 34 | + count: 0, |
| 35 | + hash: "9af7e1e", |
| 36 | + dirty: true, |
| 37 | + }; |
| 38 | + expect(composeDisplayVersion("0.3.36", info)).toBe("v0.3.36-dirty"); |
| 39 | + }); |
| 40 | + |
| 41 | + test("N commits past tag -> -N-g<hash>", () => { |
| 42 | + const info: GitInfo = { |
| 43 | + tag: "v0.3.36", |
| 44 | + count: 7, |
| 45 | + hash: "9af7e1e", |
| 46 | + dirty: false, |
| 47 | + }; |
| 48 | + expect(composeDisplayVersion("0.3.36", info)).toBe("v0.3.36-7-g9af7e1e"); |
| 49 | + }); |
| 50 | + |
| 51 | + test("N commits past tag, dirty -> -N-g<hash>-dirty", () => { |
| 52 | + const info: GitInfo = { |
| 53 | + tag: "v0.3.36", |
| 54 | + count: 7, |
| 55 | + hash: "9af7e1e", |
| 56 | + dirty: true, |
| 57 | + }; |
| 58 | + expect(composeDisplayVersion("0.3.36", info)).toBe( |
| 59 | + "v0.3.36-7-g9af7e1e-dirty", |
| 60 | + ); |
| 61 | + }); |
| 62 | + |
| 63 | + test("no tags -> +g<hash>", () => { |
| 64 | + const info: GitInfo = { |
| 65 | + tag: null, |
| 66 | + count: null, |
| 67 | + hash: "9af7e1e", |
| 68 | + dirty: false, |
| 69 | + }; |
| 70 | + expect(composeDisplayVersion("0.3.36", info)).toBe("v0.3.36+g9af7e1e"); |
| 71 | + }); |
| 72 | + |
| 73 | + test("no tags, dirty -> +g<hash>-dirty", () => { |
| 74 | + const info: GitInfo = { |
| 75 | + tag: null, |
| 76 | + count: null, |
| 77 | + hash: "9af7e1e", |
| 78 | + dirty: true, |
| 79 | + }; |
| 80 | + expect(composeDisplayVersion("0.3.36", info)).toBe( |
| 81 | + "v0.3.36+g9af7e1e-dirty", |
| 82 | + ); |
| 83 | + }); |
| 84 | + |
| 85 | + test("not a git repo / metadata unavailable -> plain version", () => { |
| 86 | + const info: GitInfo = { |
| 87 | + tag: null, |
| 88 | + count: null, |
| 89 | + hash: null, |
| 90 | + dirty: false, |
| 91 | + }; |
| 92 | + expect(composeDisplayVersion("0.3.36", info)).toBe("v0.3.36"); |
| 93 | + }); |
| 94 | + |
| 95 | + test("tag present but commit count unavailable -> plain version", () => { |
| 96 | + const info: GitInfo = { |
| 97 | + tag: "v0.3.36", |
| 98 | + count: null, |
| 99 | + hash: "9af7e1e", |
| 100 | + dirty: false, |
| 101 | + }; |
| 102 | + expect(composeDisplayVersion("0.3.36", info)).toBe("v0.3.36"); |
| 103 | + }); |
| 104 | +}); |
| 105 | + |
| 106 | +interface FakeSpec { |
| 107 | + stdout?: string; |
| 108 | + exitCode?: number; |
| 109 | + throws?: boolean; |
| 110 | +} |
| 111 | + |
| 112 | +function fakeRun(specs: Record<string, FakeSpec>) { |
| 113 | + return (args: string[]): RunResult => { |
| 114 | + const spec = specs[args.join(" ")] ?? {}; |
| 115 | + if (spec.throws) throw new Error(`git ${args.join(" ")} exploded`); |
| 116 | + return { stdout: spec.stdout ?? "", exitCode: spec.exitCode ?? 0 }; |
| 117 | + }; |
| 118 | +} |
| 119 | + |
| 120 | +describe("collectGitInfo", () => { |
| 121 | + test("git repo, clean tree", () => { |
| 122 | + const run = fakeRun({ |
| 123 | + "rev-parse --is-inside-work-tree": { stdout: "true" }, |
| 124 | + "describe --tags --match v* --abbrev=0": { stdout: "v0.3.36" }, |
| 125 | + "rev-list --count v0.3.36..HEAD": { stdout: "7" }, |
| 126 | + "rev-parse --short=7 HEAD": { stdout: "9af7e1e" }, |
| 127 | + "diff-index --quiet HEAD --": { exitCode: 0 }, |
| 128 | + }); |
| 129 | + expect(collectGitInfo("/repo", run)).toEqual({ |
| 130 | + tag: "v0.3.36", |
| 131 | + count: 7, |
| 132 | + hash: "9af7e1e", |
| 133 | + dirty: false, |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + test("git repo, dirty tree (diff-index exits 1)", () => { |
| 138 | + const run = fakeRun({ |
| 139 | + "rev-parse --is-inside-work-tree": { stdout: "true" }, |
| 140 | + "describe --tags --match v* --abbrev=0": { stdout: "v0.3.36" }, |
| 141 | + "rev-list --count v0.3.36..HEAD": { stdout: "7" }, |
| 142 | + "rev-parse --short=7 HEAD": { stdout: "9af7e1e" }, |
| 143 | + "diff-index --quiet HEAD --": { exitCode: 1 }, |
| 144 | + }); |
| 145 | + expect(collectGitInfo("/repo", run)).toEqual({ |
| 146 | + tag: "v0.3.36", |
| 147 | + count: 7, |
| 148 | + hash: "9af7e1e", |
| 149 | + dirty: true, |
| 150 | + }); |
| 151 | + }); |
| 152 | + |
| 153 | + test("git repo, exactly on tag", () => { |
| 154 | + const run = fakeRun({ |
| 155 | + "rev-parse --is-inside-work-tree": { stdout: "true" }, |
| 156 | + "describe --tags --match v* --abbrev=0": { stdout: "v0.3.36" }, |
| 157 | + "rev-list --count v0.3.36..HEAD": { stdout: "0" }, |
| 158 | + "rev-parse --short=7 HEAD": { stdout: "9af7e1e" }, |
| 159 | + "diff-index --quiet HEAD --": { exitCode: 0 }, |
| 160 | + }); |
| 161 | + expect(collectGitInfo("/repo", run)).toEqual({ |
| 162 | + tag: "v0.3.36", |
| 163 | + count: 0, |
| 164 | + hash: "9af7e1e", |
| 165 | + dirty: false, |
| 166 | + }); |
| 167 | + }); |
| 168 | + |
| 169 | + test("no tags (describe throws) degrades tag/count, keeps hash", () => { |
| 170 | + const run = fakeRun({ |
| 171 | + "rev-parse --is-inside-work-tree": { stdout: "true" }, |
| 172 | + "describe --tags --match v* --abbrev=0": { throws: true }, |
| 173 | + "rev-parse --short=7 HEAD": { stdout: "9af7e1e" }, |
| 174 | + "diff-index --quiet HEAD --": { exitCode: 0 }, |
| 175 | + }); |
| 176 | + expect(collectGitInfo("/repo", run)).toEqual({ |
| 177 | + tag: null, |
| 178 | + count: null, |
| 179 | + hash: "9af7e1e", |
| 180 | + dirty: false, |
| 181 | + }); |
| 182 | + }); |
| 183 | + |
| 184 | + test("no tags (describe exits 128 with empty stdout) degrades tag/count, keeps hash", () => { |
| 185 | + const run = fakeRun({ |
| 186 | + "rev-parse --is-inside-work-tree": { stdout: "true" }, |
| 187 | + "describe --tags --match v* --abbrev=0": { exitCode: 128 }, |
| 188 | + "rev-parse --short=7 HEAD": { stdout: "9af7e1e" }, |
| 189 | + "diff-index --quiet HEAD --": { exitCode: 0 }, |
| 190 | + }); |
| 191 | + expect(collectGitInfo("/repo", run)).toEqual({ |
| 192 | + tag: null, |
| 193 | + count: null, |
| 194 | + hash: "9af7e1e", |
| 195 | + dirty: false, |
| 196 | + }); |
| 197 | + }); |
| 198 | + |
| 199 | + test("not a git repo (--is-inside-work-tree -> false)", () => { |
| 200 | + const run = fakeRun({ |
| 201 | + "rev-parse --is-inside-work-tree": { stdout: "false" }, |
| 202 | + }); |
| 203 | + expect(collectGitInfo("/outside", run)).toEqual({ |
| 204 | + tag: null, |
| 205 | + count: null, |
| 206 | + hash: null, |
| 207 | + dirty: false, |
| 208 | + }); |
| 209 | + }); |
| 210 | + |
| 211 | + test("git missing (rev-parse exits non-zero) -> all null", () => { |
| 212 | + const run = fakeRun({ |
| 213 | + "rev-parse --is-inside-work-tree": { exitCode: 128 }, |
| 214 | + }); |
| 215 | + expect(collectGitInfo("/outside", run)).toEqual({ |
| 216 | + tag: null, |
| 217 | + count: null, |
| 218 | + hash: null, |
| 219 | + dirty: false, |
| 220 | + }); |
| 221 | + }); |
| 222 | +}); |
| 223 | + |
| 224 | +describe("build-info CLI", () => { |
| 225 | + test("outside a git repo prints a plain-version define", async () => { |
| 226 | + await withTempDir("build-info-not-git-", async (dir) => { |
| 227 | + const res = spawnSync(process.execPath, [scriptPath], { |
| 228 | + cwd: dir, |
| 229 | + encoding: "utf8", |
| 230 | + }); |
| 231 | + expect(res.status).toBe(0); |
| 232 | + expect(res.stdout.trim()).toBe( |
| 233 | + `process.env.CORBITS_BUILD_INFO="v${pkg.version}"`, |
| 234 | + ); |
| 235 | + }); |
| 236 | + }); |
| 237 | + |
| 238 | + test("from the repo root prints a define matching the suffix regex", () => { |
| 239 | + const res = spawnSync(process.execPath, [scriptPath], { |
| 240 | + cwd: repoRoot, |
| 241 | + encoding: "utf8", |
| 242 | + }); |
| 243 | + expect(res.status).toBe(0); |
| 244 | + const line = res.stdout.trim(); |
| 245 | + expect(line.startsWith("process.env.CORBITS_BUILD_INFO=")).toBe(true); |
| 246 | + const value: string = JSON.parse( |
| 247 | + line.slice("process.env.CORBITS_BUILD_INFO=".length), |
| 248 | + ); |
| 249 | + expect(value).toMatch(DISPLAY_VERSION_RE); |
| 250 | + }); |
| 251 | +}); |
0 commit comments