Cotton code
179 lines · 5.7 KB · ShellRaw
#!/usr/bin/env bash## The TEST phase of `pnpm rust:verify`: run every workspace crate's tests and# tell the truth about which crates ran.## WHY THIS EXISTS. `cargo test` over the workspace stops at the first failing# target, and the "N passed; M failed" it leaves on the screen reads like a# whole-workspace result. On 2026-09-11 a lane read "718 passed, 1 failed" as# the workspace and worked on from there; `cotton-tools`, with 40 failing cases,# had never been built at all. A verifier that reports a subset as the whole is# worse than one that reports nothing, because nobody goes looking. So:##   - each workspace member runs SEPARATELY, so one crate cannot stop its#     siblings — including when it fails to COMPILE, which `--no-fail-fast`#     does not cover (that flag continues past failing tests, not past a broken#     build),#   - each run passes `--no-fail-fast`, so one red test target inside a crate#     does not hide that crate's other targets (`cotton-tools` alone has a#     dozen),#   - the run ends with a per-crate table and the line "N crates ran, M did#     not", which NAMES every crate that did not run,#   - the exit code is non-zero when any crate failed OR any crate did not run.#     A crate that never ran is a failure of the verification, not a detail.## Run it from the workspace directory — the one holding the `[workspace]`# Cargo.toml, whose `members` list is the set of crates that must run.# `scripts/rust-verify.sh` does that inside its `git archive HEAD` extraction.## Extra arguments pass through to each `cargo test`. `-p` / `--package` narrows# the set: the table then covers exactly the crates named, and nothing is# reported as missing that was never asked for.##   bash scripts/rust-verify-tests.sh#   bash scripts/rust-verify-tests.sh -p cotton-store -p cotton-server#   bash scripts/rust-verify-tests.sh --no-default-features# NOT `set -e`: a red cargo is an outcome this script reports, not a crash that# should end it — the whole point is that the crates after the red one still run.set -uo pipefailMANIFEST="Cargo.toml"if [ ! -f "$MANIFEST" ]; then  echo "rust-verify: no $MANIFEST in $PWD — run this from the workspace directory" >&2  exit 2fi# The workspace's own `members` list, in its own order. Read from the manifest# rather than from `cargo metadata` so that a crate cargo cannot even load still# appears in the table as one that did not run.read_members() {  awk '    /^[[:space:]]*members[[:space:]]*=/ { in_members = 1 }    in_members { print; if (index($0, "]")) exit }  ' "$MANIFEST" | grep -o '"[^"]*"' | tr -d '"'}# `-p`/`--package` chooses the set; `--workspace`/`--all` is the default and is# dropped, because cargo refuses it alongside the `-p` this script adds.selected=()passthrough=()while [ $# -gt 0 ]; do  case "$1" in    -p|--package)      if [ $# -lt 2 ]; then        echo "rust-verify: $1 needs a crate name" >&2        exit 2      fi      selected+=("$2")      shift 2      ;;    --package=*)      selected+=("${1#*=}")      shift      ;;    --workspace|--all)      shift      ;;    *)      passthrough+=("$1")      shift      ;;  esacdonecrates=()if [ ${#selected[@]} -gt 0 ]; then  crates=("${selected[@]}")else  while IFS= read -r member; do    [ -n "$member" ] && crates+=("$member")  done < <(read_members)fiif [ ${#crates[@]} -eq 0 ]; then  echo "rust-verify: found no workspace members in $PWD/$MANIFEST —" \       "refusing to report a pass over nothing" >&2  exit 2fiLOGDIR="$(mktemp -d "${TMPDIR:-/tmp}/rust-verify-tests.XXXXXX")"cleanup_logs() { rm -rf "$LOGDIR"; }trap cleanup_logs EXITnames=()verdicts=()passed=()failed=()notes=()red_crates=()for crate in "${crates[@]}"; do  echo  echo "rust-verify: cargo test -p $crate --no-fail-fast"  log="$LOGDIR/${crate//\//_}.log"  cargo test -p "$crate" --no-fail-fast ${passthrough[@]+"${passthrough[@]}"} 2>&1 | tee "$log"  status="${PIPESTATUS[0]}"  # Every test binary and doc-test leaves one "test result:" line. No such line  # means no test target ran at all: a build failure, a crate cargo could not  # resolve, or a filter that matched nothing.  read -r results p f < <(awk '    /test result:/ {      for (i = 1; i <= NF; i++) {        if ($(i + 1) == "passed;") p += $i        if ($(i + 1) == "failed;") f += $i      }      n++    }    END { print n + 0, p + 0, f + 0 }  ' "$log")  verdict="ran"  note=""  if [ "$results" -eq 0 ]; then    verdict="did not run"    note="cargo exit $status, no test target produced a result"  elif [ "$f" -eq 0 ] && [ "$status" -ne 0 ]; then    # Red with every result line green: a later target failed to build or link    # after an earlier one had already run. Not a pass.    note="cargo exit $status with no failing case — a target broke after the first ran"  fi  names+=("$crate")  verdicts+=("$verdict")  passed+=("$p")  failed+=("$f")  notes+=("$note")  if [ "$f" -gt 0 ] || [ "$status" -ne 0 ] || [ "$results" -eq 0 ]; then    red_crates+=("$crate")  fidoneechoecho "rust-verify: test summary — one cargo test per crate, each --no-fail-fast"printf '  %-24s %-12s %8s %8s  %s\n' "crate" "ran" "passed" "failed" "note"ran_count=0missing=()for i in "${!names[@]}"; do  printf '  %-24s %-12s %8s %8s  %s\n' \    "${names[$i]}" "${verdicts[$i]}" "${passed[$i]}" "${failed[$i]}" "${notes[$i]}"  if [ "${verdicts[$i]}" = "ran" ]; then    ran_count=$((ran_count + 1))  else    missing+=("${names[$i]}")  fidoneechoecho "rust-verify: $ran_count crates ran, ${#missing[@]} did not"if [ ${#missing[@]} -gt 0 ]; then  echo "rust-verify: never ran: ${missing[*]}" >&2fiif [ ${#red_crates[@]} -gt 0 ]; then  echo "rust-verify: red: ${red_crates[*]}" >&2  exit 1fiecho "rust-verify: every crate ran green"exit 0