From 43a0dfdadc60101b96089e504411758e9c679b22 Mon Sep 17 00:00:00 2001 From: mnovak Date: Sun, 22 Nov 2026 18:47:17 +0100 Subject: [PATCH] [fabrica] Release pin, tests first (QC M1): a pin from an older commit with code changed since is refused, a pin from the parent where only pin.json changed is accepted and names its own commit (red: the tag's commit is stamped on any pin) --- scripts/release-pin.test.mjs | 133 +++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 scripts/release-pin.test.mjs diff --git a/scripts/release-pin.test.mjs b/scripts/release-pin.test.mjs new file mode 100644 index 0000000..854b217 --- /dev/null +++ b/scripts/release-pin.test.mjs @@ -0,0 +1,133 @@ +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at https://mozilla.org/MPL/2.0/. + +// QC phase 4, M1 (tests first): a release may publish only a pin made from the code it releases. +// node --test scripts/ +// Each case builds a small git repository in the temp folder and runs release-pin.mjs on it as the +// release pipeline does (IANITOR_REPO points it there). + +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { execFileSync, spawnSync } from "node:child_process"; +import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { dirname, join, resolve } from "node:path"; +import { fileURLToPath } from "node:url"; + +const script = join(dirname(fileURLToPath(import.meta.url)), "release-pin.mjs"); +const HASH = "sha256-" + "A".repeat(43) + "="; + +function repo() { + const dir = mkdtempSync(join(tmpdir(), "ianitor-pin-test-")); + const git = (...args) => + execFileSync("git", ["-c", "user.name=t", "-c", "user.email=t@t.test", "-c", "core.hooksPath=", ...args], { + cwd: dir, + encoding: "utf8", + }).trim(); + git("init", "-q", "-b", "main"); + const write = (p, text) => { + mkdirSync(dirname(join(dir, p)), { recursive: true }); + writeFileSync(join(dir, p), text); + }; + const commit = (msg) => { + git("add", "-A"); + git("commit", "-q", "-m", msg); + return git("rev-parse", "HEAD"); + }; + return { dir, git, write, commit, done: () => rmSync(dir, { recursive: true, force: true }) }; +} + +/// The pin nix-pin.sh writes for the commit `pinned` (its source tree from release-pin itself). +function pinFor(r, pinned) { + const tree = execFileSync(process.execPath, [script, "--source-tree", pinned], { + env: { ...process.env, IANITOR_REPO: r.dir }, + encoding: "utf8", + }).trim(); + return JSON.stringify({ + version: "1.0.0", + system: "x86_64-linux", + commit: pinned, + sourceTree: tree, + nixpkgs: "0123456789abcdef0123456789abcdef01234567", + outputs: { + ianitor: { storePath: "/nix/store/" + "a".repeat(32) + "-ianitor-1.0.0", narHash: HASH, narSize: 1 }, + "ianitor-web": { storePath: "/nix/store/" + "b".repeat(32) + "-ianitor-web-1.0.0", narHash: HASH, narSize: 1 }, + }, + }); +} + +function run(r, tagCommit) { + return spawnSync(process.execPath, [script, "1.0.0", "v1.0.0", tagCommit], { + env: { ...process.env, IANITOR_REPO: r.dir }, + encoding: "utf8", + }); +} + +test("no pin: refused", () => { + const r = repo(); + try { + r.write("crates/a.rs", "fn a() {}\n"); + const c = r.commit("code"); + const out = run(r, c); + assert.equal(out.status, 1, out.stderr); + assert.match(out.stderr, /pin\.json/); + } finally { + r.done(); + } +}); + +test("a pin from an older commit, with code changed since: refused", () => { + const r = repo(); + try { + r.write("crates/a.rs", "fn a() {}\n"); + const a = r.commit("code at A"); + r.write(".fabrica/nix/pin.json", pinFor(r, a)); + r.commit("pin A"); + r.write("crates/a.rs", "fn a() { fixed() }\n"); + const b = r.commit("fix at B"); + const out = run(r, b); + assert.equal(out.status, 1, out.stdout + out.stderr); + assert.match(out.stderr, /not the code of/); + } finally { + r.done(); + } +}); + +test("a pin from the parent where only pin.json changed: accepted, and the pinned commit is published", () => { + const r = repo(); + try { + r.write("crates/a.rs", "fn a() {}\n"); + const a = r.commit("code at A"); + r.write(".fabrica/nix/pin.json", pinFor(r, a)); + const b = r.commit("[repo] Release: the Nix pin"); + const out = run(r, b); + assert.equal(out.status, 0, out.stdout + out.stderr); + const published = JSON.parse(readFileSync(join(r.dir, "target/nix/ianitor-nix.json"), "utf8")); + assert.equal(published.commit, a, "the pin names the commit it was built from"); + assert.equal(published.taggedCommit, b); + assert.equal(published.tag, "v1.0.0"); + assert.match(published.flake, new RegExp(`rev=${a}`)); + } finally { + r.done(); + } +}); + +test("a pin whose recorded tree was edited by hand: refused", () => { + const r = repo(); + try { + r.write("crates/a.rs", "fn a() {}\n"); + const a = r.commit("code"); + const pin = JSON.parse(pinFor(r, a)); + pin.sourceTree = "0".repeat(64); + r.write(".fabrica/nix/pin.json", JSON.stringify(pin)); + const b = r.commit("pin"); + assert.equal(run(r, b).status, 1); + } finally { + r.done(); + } +}); + +test("the repository is the script's own unless IANITOR_REPO says otherwise", () => { + assert.equal(resolve(dirname(script), ".."), resolve(dirname(fileURLToPath(import.meta.url)), "..")); +});