Cotton code
104 lines · 5.1 KB · ShellRaw
#!/usr/bin/env bash# The local gate: what a PR's `checks` and `rust` jobs would have told you, in# the time it takes to read the diff once more.##   pnpm verify:fast## WHAT IT RUNS, AND WHY THESE AND NOT THE REST:##   cargo nextest run -p cotton-store -p cotton-core -p cotton-tools#       The three crates the phone, the Mac and the server ALL link, and the#       three that hold the file format, the domain engines and the MCP surface.#       A break here is a break everywhere. What is deliberately NOT here is#       `cotton-server` — its several dozen integration binaries are most of the#       workspace's test time, and they exercise HTTP transport rather than the#       rules underneath it.#   pnpm test:scripts#       `turbo test` never sees `scripts/__tests__` (it is under no workspace#       package), and those are the harness's own guards — the ones a change to#       a workflow, a hook or a generator makes red.## IT IS NOT `pnpm check`, AND IT IS NOT CI. An unfiltered `turbo test`, clippy,# the client build, the doctests and both Swift suites are the merge gate's# job and they run on the PR. This is the thirty-second answer to "is what I am# about to push obviously broken", which is the question a pre-push hook can# afford to ask.## FAIL-OPEN ON A MISSING TOOLCHAIN, LOUDLY. A machine without cargo gets a# named message and the script SKIPS the Rust half rather than failing: this# runs from a `pre-push` hook, and a hook that blocks a push because a tool is# not installed teaches people `--no-verify` as a habit. A machine WITH cargo# but without `cargo-nextest` runs the same crates under `cargo test` — slower,# same answer — and names the install line, because since 2026-09-27 the hook# is a real gate on a change to these three crates and a gate that skipped# whenever a plugin was missing would be no gate on a fresh Mac. A real test# failure exits non-zero either way.set -euo pipefail# NOTHING BELOW MAY SEE THE REPOSITORY THAT LAUNCHED IT. Git runs a hook with# `GIT_DIR` (and in a worktree `GIT_COMMON_DIR`, `GIT_INDEX_FILE`, …) exported,# and every `git init` / `git commit` a test makes in a scratch directory then# acts on THIS repository: on 2026-09-27 a push wrote the fixtures' commits onto# `main`, set `core.bare = true` and overwrote `origin/main`. The list is git's# own, so a variable a later git adds is dropped too.if command -v git >/dev/null 2>&1; then  # shellcheck disable=SC2046  unset $(git rev-parse --local-env-vars 2>/dev/null) 2>/dev/null || truefiROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"cd "$ROOT"status=0say() { echo "[verify:fast] $*"; }# --- the Rust core -----------------------------------------------------------if ! command -v cargo >/dev/null 2>&1; then  say "no cargo on PATH — skipping the Rust half. Install rustup to have it here."else  if cargo nextest --version >/dev/null 2>&1; then    runner=(cargo nextest run)  else    say "cargo-nextest is not installed — running the same crates under \`cargo test\` (slower)."    say "  cargo install cargo-nextest --locked      (Cotton note \"The Rust core — the file format, and how a change to it lands\")"    runner=(cargo test)  fi  say "${runner[*]} -p cotton-store -p cotton-core -p cotton-tools"  # UNDER THE MACHINE'S BUILD MUTEX on a Mac (`scripts/ci/with-machine-lock.sh`;  # a no-op on Linux). PR #1026's Swift suite died with a signal while a  # hand-run cargo had the same Mac's memory, and two `swift` jobs on the signing Mac  # died mid-compile on 27 Sep 2026 beside a session's cargo runs. A test run  # beside a CI build on a 16 GB Mac is the two of them swapping; queued, each  # gets the machine whole, and the lock says who it is waiting for.  # The workspace manifest is `crates/Cargo.toml` at the repo ROOT, and this may  # be run from anywhere — so it is named rather than assumed.  if ! BUILD_LOCK_LABEL="${BUILD_LOCK_LABEL:-local-verify-fast}" \      bash "$ROOT/scripts/ci/with-machine-lock.sh" \      "${runner[@]}" --manifest-path crates/Cargo.toml \      -p cotton-store -p cotton-core -p cotton-tools; then    say "the Rust core is RED"    status=1  fifi# --- the harness's own tests -------------------------------------------------# TWO WORKERS unless the caller named a cap. This runs on Macs that are also# running CI jobs, and at full parallelism the tests that spawn shells (setup-pnpm# "four jobs at once", relay-watchdog) blew vitest's 5 s# timeout and turned a push advisory red for nothing. A cap under any of the# three names `vitest.workers.ts` reads is the caller's and is left alone.if [ -z "${VITEST_MAX_WORKERS:-}${VITEST_MAX_FORKS:-}${VITEST_MAX_THREADS:-}" ]; then  export VITEST_MAX_WORKERS=2fisay "pnpm test:scripts (worker cap: ${VITEST_MAX_WORKERS:-${VITEST_MAX_FORKS:-${VITEST_MAX_THREADS:-}}})"if ! pnpm -s test:scripts; then  say "scripts/__tests__ is RED"  status=1fiif [ "$status" = 0 ]; then  say "green — the rest (clippy, doctests, the phone build, both Swift suites) runs on the PR."else  say "RED. Push anyway with \`git push --no-verify\` if you know why."fiexit "$status"