/** * `pnpm rust:verify test` must not report a subset as the workspace. * * On 2026-09-11 a lane read the tail of a verify run — "718 passed, 1 failed" — * as the whole Rust workspace and carried on. `cotton-tools` had never been * built: `cargo test` had stopped at the first failing target, and the counts on * the screen belonged to the crates before it. Forty red cases were invisible, * and nothing in the output said a crate was missing. * * So `scripts/rust-verify-tests.sh` runs one `cargo test --no-fail-fast` per * workspace member and ends with a table plus the line "N crates ran, M did * not". This file drives it against a stub `cargo` that can fail a crate's * tests, break a crate's build, or pass — and holds the three properties the * lane failure needs: * * - a red crate does not stop the crates after it, * - a crate that never ran is NAMED, never silent, * - the exit code is non-zero when any crate failed OR any crate did not run. */import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from"node:fs";import { tmpdir } from"node:os";import { join, resolve } from"node:path";import { spawnSync } from"node:child_process";import { afterEach, describe, expect, it } from"vitest";const ROOT = resolve(__dirname, "../..");const SCRIPT = join(ROOT, "scripts/rust-verify-tests.sh");let scratch = "";afterEach(() => {if (scratch) rmSync(scratch, { recursive: true, force: true }); scratch = "";});typeFixture = { dir: string; bin: string; calls: string };/** * A `cargo` on PATH that records its arguments and answers like the real one: * * STUB_FAIL="beta" — beta's tests run and one fails (a result line, exit 101) * STUB_NO_BUILD="gamma" — gamma does not compile (no result line at all, exit 101) * * The distinction is the whole point: `--no-fail-fast` carries a run past a * failing test but not past a broken build, so both shapes have to be visible. */function stubCargo(dir: string): string {const bin = join(dir, "bin"); mkdirSync(bin, { recursive: true }); writeFileSync( join(bin, "cargo"), ["#!/usr/bin/env bash",`echo "$@" >> "${join(dir, "calls.txt")}"`,"crate=''","prev=''",'for arg in "$@"; do',' if [ "$prev" = "-p" ]; then crate="$arg"; fi',' prev="$arg"',"done",'if [[ " ${STUB_NO_BUILD:-} " == *" $crate "* ]]; then',' echo "error[E0425]: cannot find value \\`nope\\` in this scope"',' echo "error: could not compile \\`$crate\\` (lib test) due to 1 previous error"'," exit 101","fi",'if [[ " ${STUB_FAIL:-} " == *" $crate "* ]]; then',' echo "running 3 tests"',' echo "test result: FAILED. 2 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out"'," exit 101","fi",'echo "running 4 tests"','echo "test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out"',// A second target in the same crate, so the passed counts add up.'echo "test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out"',"exit 0", ].join("\n"), { mode: 0o755 }, );return bin;}function makeFixture(members = ["alpha", "beta", "gamma"]): Fixture { scratch = mkdtempSync(join(tmpdir(), "rust-verify-table-"));const dir = join(scratch, "crates"); mkdirSync(dir, { recursive: true }); writeFileSync( join(dir, "Cargo.toml"),`[workspace]\nresolver = "2"\nmembers = [\n${members.map((m) => `"${m}",`).join("\n")}\n]\n`, );return { dir, bin: stubCargo(scratch), calls: join(scratch, "calls.txt") };}function run(f: Fixture, args: string[] = [], env: Record<string, string> = {}) {const r = spawnSync("bash", [SCRIPT, ...args], { encoding: "utf8", timeout: 20000, cwd: f.dir,// Off the machine's real build mutex: `rust-verify.sh` takes it on a Mac// since 28 Sep 2026, and a test must never queue behind a CI build. env: { ...process.env, COTTON_MACHINE_LOCK_OS: "Linux", PATH: `${f.bin}:${process.env.PATH ?? ""}`, ...env }, });return r;}const calls = (f: Fixture): string[] => {try {return readFileSync(f.calls, "utf8").trim().split("\n").filter(Boolean); } catch {return []; }};describe("rust-verify-tests.sh — every crate runs, and the table says which", () => { it("runs one cargo test per workspace member, each with --no-fail-fast", () => {const f = makeFixture();const r = run(f); expect(r.status).toBe(0); expect(calls(f)).toEqual(["test -p alpha --no-fail-fast","test -p beta --no-fail-fast","test -p gamma --no-fail-fast", ]); }); it("prints a per-crate table with the passed and failed counts summed per crate", () => {const f = makeFixture();const r = run(f); expect(r.stdout).toMatch(/crate\s+ran\s+passed\s+failed/);// Two result lines per green crate: 4 + 1. expect(r.stdout).toMatch(/alpha\s+ran\s+5\s+0/); expect(r.stdout).toMatch(/beta\s+ran\s+5\s+0/); expect(r.stdout).toMatch(/3 crates ran, 0 did not/); }); it("a crate whose tests fail does not stop the crates after it, and the run is red", () => {const f = makeFixture();const r = run(f, [], { STUB_FAIL: "beta" }); expect(r.status).toBe(1);// The crate after the red one still ran — the bug this file exists for. expect(calls(f)).toContain("test -p gamma --no-fail-fast"); expect(r.stdout).toMatch(/beta\s+ran\s+2\s+1/); expect(r.stdout).toMatch(/3 crates ran, 0 did not/); expect(r.stderr).toMatch(/red: beta/); }); it("a crate that never ran is named in the table, in the count and on stderr", () => {const f = makeFixture();const r = run(f, [], { STUB_NO_BUILD: "gamma" }); expect(r.status).toBe(1); expect(r.stdout).toMatch(/gamma\s+did not run\s+0\s+0\s+cargo exit 101, no test target produced a result/); expect(r.stdout).toMatch(/2 crates ran, 1 did not/); expect(r.stderr).toMatch(/never ran: gamma/); }); it("a crate that did not run is red even when every crate that ran was green", () => {const f = makeFixture(["alpha", "beta"]);const r = run(f, [], { STUB_NO_BUILD: "beta" }); expect(r.status).not.toBe(0); expect(r.stdout).toMatch(/1 crates ran, 1 did not/); expect(r.stdout).not.toMatch(/every crate ran green/); }); it("-p narrows the set, so nothing is reported missing that was never asked for", () => {const f = makeFixture();const r = run(f, ["-p", "alpha"]); expect(r.status).toBe(0); expect(calls(f)).toEqual(["test -p alpha --no-fail-fast"]); expect(r.stdout).toMatch(/1 crates ran, 0 did not/); }); it("passes extra arguments through to every crate's cargo test", () => {const f = makeFixture(["alpha"]);const r = run(f, ["--no-default-features"]); expect(r.status).toBe(0); expect(calls(f)).toEqual(["test -p alpha --no-fail-fast --no-default-features"]); }); it("refuses to report a pass over a workspace it found no members in", () => {const f = makeFixture([]);const r = run(f); expect(r.status).toBe(2); expect(r.stderr).toMatch(/found no workspace members/); expect(calls(f)).toEqual([]); }); it("run outside a workspace directory says so rather than passing", () => {const f = makeFixture();const r = spawnSync("bash", [SCRIPT], { encoding: "utf8", timeout: 20000, cwd: scratch, env: { ...process.env, COTTON_MACHINE_LOCK_OS: "Linux", PATH: `${f.bin}:${process.env.PATH ?? ""}` }, }); expect(r.status).toBe(2); expect(r.stderr).toMatch(/run thisfrom the workspace directory/); });});