Cotton code
175 lines · 8.2 KB · TypeScriptRaw
/** * cargo-nextest on the `rust` lane — provisioned by the lane, never by whoever * last sat at the machine. * * `rust` used to name the Linux CI box, one box, and somebody had * `cargo install cargo-nextest`d on it by hand months before the job started * using nextest. PR #941 moved the job onto the `cotton-rust` LABEL GROUP, * which the two check Macs also declare, and the first `rust` job the * dispatcher handed to a Mac died on "rust: unit tests" (run 183, PR #956): * *     error: no such command: `nextest` * * The signing Mac carries no cargo-nextest. Another Mac carries 0.9.143 in * `~/.cargo/bin`, which the lane's cargo never looks in — a job's environment * is the LOGIN environment and the toolchain step prepends its own * `CARGO_HOME/bin`. Neither machine was misconfigured; the lane was. * * `scripts/ci/setup-nextest.sh` is the durable form, on `setup-sccache.sh`'s * and `setup-pnpm.sh`'s pattern — pinned, digest-checked, fetched into the * lane's HOME once — and this file holds it there: * *   - the `rust` job runs it BEFORE the first `nextest run` step; *   - the pin carries an asset AND a published sha256 per platform, chosen *     from `uname`, and an unrecognised platform FAILS by name; *   - the digest is checked with the same two-tool ladder as sccache's; *   - the lane's bin goes on `$GITHUB_PATH` (which PREPENDS), so a *     hand-installed copy on any machine in the group is stepped over. * * The fetch branch is exercised by hand (a throwaway HOME); a vitest that * downloads 16 MB from GitHub on every run is a test of the network. What runs * here needs none: the adopt branch against a stub that reports the pin, and * the two refusals — a stale hand-installed copy, and an unpinned platform. */import { spawnSync } from "node:child_process";import { chmodSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";import { tmpdir } from "node:os";import { join, resolve } from "node:path";import { parse as parseYaml } from "yaml";import { afterEach, describe, expect, it } from "vitest";const ROOT = resolve(__dirname, "../..");const SCRIPT = join(ROOT, "scripts/ci/setup-nextest.sh");const source = readFileSync(SCRIPT, "utf8");type Step = { name?: string; run?: string; if?: string };/** The pinned version, read from the script so a bump moves one number. */const version = source.match(/^version=([0-9]+\.[0-9]+\.[0-9]+)$/m)?.[1];let scratch: string | undefined;afterEach(() => {  if (scratch) rmSync(scratch, { recursive: true, force: true });  scratch = undefined;});/** * A scratch HOME, a stub directory ahead of the PATH, and the script run * against both. Nothing here reaches the network unless the script decides to * fetch — and `curl` is stubbed to fail loudly when it does, so a test that * accidentally takes the fetch branch goes red rather than slow. */function runWith(stubs: Record<string, string>) {  scratch = mkdtempSync(join(tmpdir(), "setup-nextest-"));  const stubDir = join(scratch, "stubs");  const home = join(scratch, "home");  mkdirSync(stubDir, { recursive: true });  mkdirSync(home, { recursive: true });  for (const [name, body] of Object.entries(stubs)) {    const path = join(stubDir, name);    writeFileSync(path, body);    chmodSync(path, 0o755);  }  const githubPath = join(scratch, "github_path");  writeFileSync(githubPath, "");  const result = spawnSync("bash", [SCRIPT], {    encoding: "utf8",    env: {      HOME: home,      PATH: `${stubDir}:/usr/bin:/bin:/usr/sbin:/sbin`,      GITHUB_PATH: githubPath,    },  });  return { ...result, githubPath: readFileSync(githubPath, "utf8") };}/** A `cargo-nextest` that answers `nextest --version` like the real one. */const nextestStub = (v: string) =>  `#!/bin/sh\n[ "$1" = nextest ] || exit 1\necho "cargo-nextest ${v} (deadbeef 2026-01-01)"\n`;/** A `curl` that refuses, so an unintended fetch branch is loud and offline. */const noCurl = `#!/bin/sh\necho "this test must not reach the network" >&2\nexit 7\n`;describe("setup-nextest.sh — the pin", () => {  it("reads the bootstrap it is asserting about — a moved script must go red, not quiet", () => {    // The loud-null rule: every assertion below is a regex over this string,    // and a path that stopped resolving would turn each of them into a regex    // over nothing.    expect(source.length, `${SCRIPT} is empty or missing`).toBeGreaterThan(0);    expect(version, "the script pins an exact cargo-nextest").toBeTruthy();  });  it("carries an asset AND a sha256 per lane, chosen from `uname` — Linux musl, universal Darwin", () => {    expect(source).toMatch(/case "\$\(uname -s\)\/\$\(uname -m\)" in/);    expect(source).toMatch(/Linux\/x86_64\)/);    expect(source).toMatch(/Darwin\/arm64\)/);    expect(source).toMatch(/asset="cargo-nextest-\$version-x86_64-unknown-linux-musl"/);    // nextest publishes ONE Apple asset, a universal binary; there is no    // aarch64-only name to prefer, unlike sccache's.    expect(source).toMatch(/asset="cargo-nextest-\$version-universal-apple-darwin"/);    // A sha PER asset, and two different ones: one hash reused across two    // downloads would be a check that cannot fail on the second.    const shas = [...source.matchAll(/^\s*sha=([0-9a-f]{64})\s*$/gm)].map((m) => m[1]);    expect(shas.length, "one pinned sha256 per asset").toBe(2);    expect(new Set(shas).size, "the two assets must not share a hash").toBe(2);  });  it("checks the hash with whichever tool the lane has, and never falls through a bad one", () => {    // macOS ships no `sha256sum`; `shasum -a 256 -c -` reads the same    // "<sha>  <path>" line and exits non-zero on a mismatch, so the hard    // failure is identical on both lanes.    expect(source).toMatch(/command -v sha256sum/);    expect(source).toMatch(/sha256sum -c -/);    expect(source).toMatch(/shasum -a 256 -c -/);    expect(source).toMatch(/set -euo pipefail/);    expect(source).not.toMatch(/\|\|\s*true/);  });  it("fetches into the same durable root sccache and the target directories use", () => {    expect(source).toMatch(/root="\$HOME\/\.cache\/cottondb\/rust"/);    expect(source).toMatch(/bin="\$root\/bin"/);  });  it("puts the lane's bin on THIS shell's PATH as well as $GITHUB_PATH", () => {    // `$GITHUB_PATH` reaches the NEXT step; the `--version` print at the end of    // the fetch branch runs in THIS one — the same trap `setup-sccache.sh`    // fell into on a Mac with no hand-installed copy.    const exports = source.match(/export PATH="\$bin:\$PATH"/g) ?? [];    expect(exports.length, "both non-host branches must export it").toBe(2);    const appends = source.match(/echo "\$bin" >> "\$GITHUB_PATH"/g) ?? [];    expect(appends.length).toBe(2);  });});describe("setup-nextest.sh — what it decides, offline", () => {  it("adopts a copy on PATH that is already the pin, and prepends nothing", () => {    const run = runWith({ "cargo-nextest": nextestStub(version!), curl: noCurl });    expect(run.status).toBe(0);    expect(run.stdout).toMatch(new RegExp(`already the pinned ${version!.replace(/\./g, "\\.")}`));    // Nothing to prepend: the PATH already resolves to the pin.    expect(run.githubPath.trim()).toBe("");  });  it("does NOT adopt a hand-installed copy of another version — another Mac's 0.9.143", () => {    // The exact machine state behind run 183: a cargo-nextest is on the PATH    // and it is not the one this repo pins. Adopting it would make "what CI    // ran" a fact of the machine again.    const run = runWith({ "cargo-nextest": nextestStub("0.9.1"), curl: noCurl });    expect(run.stdout).toMatch(/no pinned .* on this host — fetching it/);    // …and the fetch is the stubbed curl, which refuses: the point of this    // assertion is the branch taken, not the download.    expect(run.status).not.toBe(0);  });  it("refuses a platform it has no pin for, by name, before touching the network", () => {    const run = runWith({      uname: `#!/bin/sh\n[ "$1" = "-s" ] && echo Plan9 || echo pdp11\n`,      curl: noCurl,    });    expect(run.status).toBe(1);    expect(run.stderr).toMatch(/::error::scripts\/ci\/setup-nextest\.sh has no pinned cargo-nextest/);    expect(run.stderr).toMatch(/Plan9\/pdp11/);    expect(run.stdout).not.toMatch(/fetching/);  });});