|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import { execFileSync } from "node:child_process"; |
| 3 | +import { existsSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs"; |
| 4 | +import { tmpdir } from "node:os"; |
| 5 | +import { join } from "node:path"; |
| 6 | +import { test } from "node:test"; |
| 7 | + |
| 8 | +const script = join(import.meta.dirname, "base-file.sh"); |
| 9 | +const git = (cwd, ...args) => |
| 10 | + execFileSync( |
| 11 | + "git", |
| 12 | + ["-c", "user.email=t@t", "-c", "user.name=t", "-c", "commit.gpgsign=false", ...args], |
| 13 | + { |
| 14 | + cwd, |
| 15 | + encoding: "utf8", |
| 16 | + }, |
| 17 | + ).trim(); |
| 18 | + |
| 19 | +// A PR run checks out the merge commit shallowly; the base branch then moves before the gate reads its baseline. |
| 20 | +function raceFixture(baseline) { |
| 21 | + const root = mkdtempSync(join(tmpdir(), "base-file-")); |
| 22 | + const origin = join(root, "origin.git"); |
| 23 | + const work = join(root, "work"); |
| 24 | + git(root, "init", "-q", "--bare", origin); |
| 25 | + git(root, "init", "-q", "-b", "main", work); |
| 26 | + git(work, "remote", "add", "origin", origin); |
| 27 | + if (baseline) writeFileSync(join(work, "baseline.json"), baseline); |
| 28 | + writeFileSync(join(work, "readme"), "base"); |
| 29 | + git(work, "add", "."); |
| 30 | + git(work, "commit", "-qm", "base"); |
| 31 | + const base = git(work, "rev-parse", "HEAD"); |
| 32 | + git(work, "switch", "-qc", "pr"); |
| 33 | + writeFileSync(join(work, "pr"), "change"); |
| 34 | + git(work, "add", "."); |
| 35 | + git(work, "commit", "-qm", "pr"); |
| 36 | + git(work, "switch", "-q", "main"); |
| 37 | + git(work, "merge", "-q", "--no-ff", "-m", "merge", "pr"); |
| 38 | + const merge = git(work, "rev-parse", "HEAD"); |
| 39 | + git(work, "push", "-q", "origin", `${merge}:refs/pull/1/merge`); |
| 40 | + git(work, "reset", "-q", "--hard", base); |
| 41 | + writeFileSync(join(work, "baseline.json"), '{"total":852}'); |
| 42 | + git(work, "add", "."); |
| 43 | + git(work, "commit", "-qm", "base moves on"); |
| 44 | + git(work, "push", "-q", "origin", "HEAD:main"); |
| 45 | + const checkout = join(root, "checkout"); |
| 46 | + git(root, "init", "-q", checkout); |
| 47 | + git(checkout, "remote", "add", "origin", `file://${origin}`); |
| 48 | + git(checkout, "fetch", "-q", "--depth=1", "origin", "refs/pull/1/merge"); |
| 49 | + git(checkout, "checkout", "-q", "FETCH_HEAD"); |
| 50 | + return { checkout, base, out: join(root, "out.json") }; |
| 51 | +} |
| 52 | + |
| 53 | +test("reads the base the run measured, not the base branch tip that moved since", () => { |
| 54 | + const { checkout, base, out } = raceFixture('{"total":846}'); |
| 55 | + const log = execFileSync("bash", [script, "baseline.json", out], { |
| 56 | + cwd: checkout, |
| 57 | + encoding: "utf8", |
| 58 | + }); |
| 59 | + assert.equal(readFileSync(out, "utf8"), '{"total":846}'); |
| 60 | + assert.match(log, new RegExp(`measured: ${base}`)); |
| 61 | +}); |
| 62 | + |
| 63 | +test("a base without the file leaves no output", () => { |
| 64 | + const { checkout, out } = raceFixture(null); |
| 65 | + writeFileSync(out, "stale"); |
| 66 | + execFileSync("bash", [script, "baseline.json", out], { cwd: checkout, encoding: "utf8" }); |
| 67 | + assert.equal(existsSync(out), false); |
| 68 | +}); |
0 commit comments