Cotton code
66 lines · 2.8 KB · TypeScriptRaw
/** * `scripts/ci/cargo-phases.sh` — a job's cargo phases inside one build slot, * with the phase that broke named the way a failed step used to be. */import { spawnSync } from "node:child_process";import { mkdirSync, mkdtempSync, rmSync } from "node:fs";import { tmpdir } from "node:os";import { join } from "node:path";import { afterAll, describe, expect, it } from "vitest";const ROOT = join(__dirname, "..", "..");const SCRIPT = join(ROOT, "scripts", "ci", "cargo-phases.sh");const temps: string[] = [];afterAll(() => {  for (const dir of temps.splice(0)) rmSync(dir, { recursive: true, force: true });});function run(args: string[]) {  const dir = mkdtempSync(join(tmpdir(), "cargo-phases-"));  temps.push(dir);  mkdirSync(join(dir, "crates"));  return spawnSync("bash", [SCRIPT, ...args], {    cwd: dir,    encoding: "utf8",    env: { PATH: process.env.PATH ?? "/usr/bin:/bin", HOME: dir },  });}describe("cargo-phases.sh", () => {  // RULE: cargo-phases.sh runs each phase from the cargo workspace, in order, as a log group, and stops at the first phase that fails.  it("runs the phases in order from crates/, as log groups, and stops at the first red one", () => {    const r = run(["one::echo first; pwd", "two::echo second; exit 3", "three::echo never"]);    expect(r.status).toBe(3);    expect(r.stdout).toMatch(/::group::one\n.*\nfirst\n.*\/crates\n::endgroup::/s);    expect(r.stdout).toMatch(/one passed in \d+m\d+s/);    expect(r.stdout).toMatch(/::group::two\n(.*\n)*second\n::endgroup::/);    expect(r.stdout).not.toMatch(/never/);  });  // RULE: The first failing phase is announced as an ::error:: line naming it and its exit status, so a red bar still says which phase broke.  it("names the phase that broke, with its exit status, as the run page's error line", () => {    const r = run(["rust: unit tests::exit 101", "rust: the client build::true"]);    expect(r.status).toBe(101);    expect(r.stdout).toMatch(/^::error::rust: unit tests failed \(exit 101\) after \d+m\d+s — the phases after it did not run$/m);    expect(r.stdout).not.toMatch(/::group::rust: the client build/);  });  it("exits 0 when every phase passes", () => {    const r = run(["a::true", "b::true"]);    expect(r.status).toBe(0);    expect(r.stdout).toMatch(/a passed/);    expect(r.stdout).toMatch(/b passed/);  });  // RULE: cargo-phases.sh refuses a phase without a name or a command, and refuses to run with no phases at all.  it("refuses a malformed phase and an empty list", () => {    expect(run([]).status).toBe(2);    expect(run(["no-separator"]).status).toBe(2);    expect(run(["::true"]).status).toBe(2);    expect(run(["name::"]).status).toBe(2);    // Nothing ran: the refusal is before the first phase.    expect(run(["ok::echo ran", "bad"]).stdout).not.toMatch(/ran/);  });});