Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.
| Merge branch 'worktree-agent-a63425cb2d4935c5f' | 1 | import { test } from "node:test"; |
| 2 | import assert from "node:assert/strict"; | |
| 3 | import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; | |
| 4 | import { tmpdir } from "node:os"; | |
| 5 | import { join } from "node:path"; | |
| 6 | import { depsHash, externalPackages, rustVersion, stale, unitHash, usedHashes } from "./ci-cache.mjs"; | |
| 7 | ||
| 8 | const LOCK = (runner, serde) => `# This file is automatically @generated by Cargo. | |
| 9 | version = 4 | |
| 10 | ||
| 11 | [[package]] | |
| 12 | name = "g1t-runner" | |
| 13 | version = "${runner}" | |
| 14 | dependencies = [ | |
| 15 | "serde", | |
| 16 | ] | |
| 17 | ||
| 18 | [[package]] | |
| 19 | name = "serde" | |
| 20 | version = "${serde}" | |
| 21 | source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 22 | checksum = "abc" | |
| 23 | `; | |
| 24 | ||
| 25 | test("a release's version bump keeps the Rust caches' key, a new dependency changes it", () => { | |
| 26 | assert.deepEqual(externalPackages(LOCK("0.3.3", "1.0.200")).length, 1); | |
| 27 | assert.equal(depsHash(LOCK("0.3.3", "1.0.200")), depsHash(LOCK("0.3.4", "1.0.200"))); | |
| 28 | assert.notEqual(depsHash(LOCK("0.3.3", "1.0.200")), depsHash(LOCK("0.3.3", "1.0.201"))); | |
| 29 | assert.match(depsHash(LOCK("0.3.3", "1.0.200")), /^[0-9a-f]{16}$/); | |
| 30 | }); | |
| 31 | ||
| 32 | test("the toolchain is read from rustc -V", () => { | |
| 33 | assert.equal(rustVersion("rustc 1.99.0 (90b35a623 2026-09-30)\n"), "1.99.0"); | |
| 34 | assert.equal(rustVersion("rustc 1.100.0-nightly (abc 2026-10-01)"), "1.100.0-nightly"); | |
| 35 | assert.equal(rustVersion(""), null); | |
| 36 | }); | |
| 37 | ||
| 38 | test("a unit's hash is read from its files and folders", () => { | |
| 39 | assert.equal(unitHash("libserde-0123456789abcdef.rlib"), "0123456789abcdef"); | |
| 40 | assert.equal(unitHash("serde-0123456789abcdef"), "0123456789abcdef"); | |
| 41 | assert.equal(unitHash("g1t_actions-0123456789abcdef.d"), "0123456789abcdef"); | |
| 42 | assert.equal(unitHash("repository_workflows-0123456789abcdef"), "0123456789abcdef"); | |
| 43 | assert.equal(unitHash("g1t"), null); | |
| 44 | assert.equal(unitHash("libg1t.rlib"), null); | |
| 45 | }); | |
| 46 | ||
| 47 | test("only the units the build did not use are pruned", () => { | |
| 48 | const dir = mkdtempSync(join(tmpdir(), "ci-cache-")); | |
| 49 | try { | |
| 50 | const [used, old, script, run] = ["1111111111111111", "2222222222222222", "3333333333333333", "4444444444444444"]; | |
| 51 | for (const sub of ["deps", "build", ".fingerprint"]) mkdirSync(join(dir, sub), { recursive: true }); | |
| 52 | for (const f of [`libserde-${used}.rlib`, `libserde-${used}.rmeta`, `serde-${used}.d`, `libserde-${old}.rlib`, `serde-${old}.d`, `tests-${used}`]) { | |
| 53 | writeFileSync(join(dir, "deps", f), "x"); | |
| 54 | } | |
| 55 | for (const d of [`serde-${script}`, `serde-${run}`, `serde-${old}`]) mkdirSync(join(dir, "build", d)); | |
| 56 | for (const d of [`serde-${used}`, `serde-${script}`, `serde-${run}`, `serde-${old}`]) mkdirSync(join(dir, ".fingerprint", d)); | |
| 57 | const messages = [ | |
| 58 | " Compiling serde", | |
| 59 | JSON.stringify({ reason: "compiler-artifact", filenames: [join(dir, "build", `serde-${script}`, "build-script-build")], executable: null }), | |
| 60 | JSON.stringify({ reason: "build-script-executed", out_dir: join(dir, "build", `serde-${run}`, "out") }), | |
| 61 | JSON.stringify({ reason: "compiler-artifact", filenames: [join(dir, "deps", `libserde-${used}.rlib`), join(dir, "deps", `libserde-${used}.rmeta`)] }), | |
| 62 | JSON.stringify({ reason: "compiler-artifact", filenames: [], executable: join(dir, "deps", `tests-${used}`) }), | |
| 63 | JSON.stringify({ reason: "build-finished", success: true }), | |
| 64 | ].join("\n"); | |
| 65 | const hashes = usedHashes(messages); | |
| 66 | assert.deepEqual([...hashes].sort(), [used, script, run].sort()); | |
| 67 | const gone = stale(dir, hashes).map((p) => p.slice(dir.length + 1).replaceAll("\\", "/")).sort(); | |
| 68 | assert.deepEqual(gone, [`.fingerprint/serde-${old}`, `build/serde-${old}`, `deps/libserde-${old}.rlib`, `deps/serde-${old}.d`]); | |
| 69 | // Nothing listed: nothing would be kept, so the caller prunes nothing. | |
| 70 | assert.equal(usedHashes("error: could not compile").size, 0); | |
| 71 | } finally { | |
| 72 | rmSync(dir, { recursive: true, force: true }); | |
| 73 | } | |
| 74 | }); |
This file's history is long; its oldest lines are credited to the oldest commit read.