Files
fleet/scripts/ci/test-nav-contract-audit.sh
CatoandClaude Opus 5.5 31de29870b fix(scripts): grep -q is never the reader of a pipe under pipefail (#140)
* fix(scripts): grep -q is never the reader of a pipe under pipefail

`echo "$x" | grep -q PAT` under `set -o pipefail` can report "not found"
for text that IS there: grep -q exits on its first match and closes the
pipe, the writer takes SIGPIPE (141), and pipefail makes the pipeline
status 141, so an `if` takes the else branch on a successful match.

It is not confined to payloads past the ~4KB pipe buffer. Measured in
loki (loki#874) on a real 1,158-byte output, 3,000 calls: 2/3000 false
misses for a phrase near the top, 0/3000 via a here-string. Rare per
call; a suite makes hundreds. loki's retire-site suite went red on
exactly those phrases twice this week.

48 copies in 14 scripts here, rewritten to `grep -q PAT <<<"$x"`,
including:
- auto-merge-sweep.sh: the required-check coverage test. A false miss
  marks a green job "uncovered".
- gc-merged-worktrees.sh: `in_use`. A false miss reads a live session's
  worktree as unused. The pushed / clean / MERGED guards still stand, so
  it cannot lose work, but it can remove a directory from under a live
  session.
- cicd-hygiene, nav-contract, org-drift, verify-floor audits and
  verify-predicates: false passes and false findings.

Checked: all 14 files pass `bash -n`; every file is a 1:1 line swap (48
lines); the 8 line-continuation sites keep the here-string before the
trailing backslash; fleet's 30 CI test scripts pass before and after
(30/30 -> 30/30); workflows still parse.

Gate: scripts/ci/test-no-grep-q-in-a-pipe.sh, wired into ci.yml. Scans
scripts/ and templates/, self-tests its detector both ways, and was
proven by restoring the original auto-merge-sweep.sh line.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Cato <41178744+catomean@users.noreply.github.com>

* fix(ci): main was red on a ratchet that fell, and a sweep test lied

Two failures on fleet main that were blocking every PR, including this
one. Neither was caused by the grep -q rewrite: main (7ed94a2, the base
of this branch) failed CI on the same step for the same reason.

1. org-drift ratchet fell. `org-drift-audit.sh --check` failed on main
   since c43464d with "baseline entries with no matching violation - the
   ratchet fell: bitbaum/README.md". Good news: someone fixed that drift.
   Nobody lowered the baseline, so main stayed red and the auto-merge
   sweep refused everything. Line removed; --check now passes (44 repos
   swept, 3 known violations, no new contradictions).

2. test-auto-merge-sweep.sh lied. Its verdict `[ "$FAIL" -eq 0 ]` sat at
   line 400 of 473, with ~70 lines of tests appended after it. A bash
   script exits with its LAST command's status, so the verdict was
   discarded: the suite exited 0 while printing "27 passed, 1 failed",
   and its later tests were not even counted. Verdict moved to the end;
   the suite now counts 38.

   The hidden failure was real and was mine: test 13 asserted the sweep
   always "waits for CI to catch up" when the newest base run is for an
   older commit. The deadlock fix changed that - it waits only while a
   run is in flight, and otherwise dispatches CI. Test 13 now asserts
   both cases (13a waits and dispatches nothing; 13b dispatches and does
   not merge). `rearms()` moved above its first use.

Proven by mutation: an injected late failure now fails the suite;
reverting the sweep to always-wait fails 13b; making it never wait fails
13a.

Gate: scripts/ci/test-verdict-is-last.sh, wired into ci.yml, requires
every suite's verdict to be its file's last command. Proven by restoring
the old mid-file verdict. All 34 fleet CI commands are now green, and
none exits 0 while reporting a failure.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Cato <41178744+catomean@users.noreply.github.com>

---------

Signed-off-by: Cato <41178744+catomean@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-24 11:58:34 -07:00

206 lines
6.8 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# Self-test for nav-contract-audit.sh, run BEFORE the real sweep for the same
# reason test-hosted-supabase-audit.sh runs before that one: a detector that
# has silently stopped catching anything reports a clean fleet, and a clean
# report from a broken detector is worse than no report — it is an absent
# check that produces a tick.
#
# Builds tiny synthetic git repos under a scratch DEV_ROOT and points the
# audit at them, rather than sourcing internals — the script under test is a
# straight-through sweep, not a library of functions. Each fixture pins one
# side of a judgement the real sweep had to make on 2026-08-31:
#
# 1. catches a real defect (styles isActive, never announces it)
# 2. stays quiet on a repo with no nav computing an active state at all
# 3. stays quiet on a repo that already announces correctly
# 4. stays quiet on a tab list correctly using aria-selected
# (this exact false positive fired on evig's AnalyseTabs before the
# audit was narrowed to a repo-level assertion)
# 5. the baseline suppresses a named repo
# 6. two directories with the same origin remote count as one repo
#
# Pure: no network, no real fleet checkout.
set -uo pipefail
HERE="$(cd "$(dirname "$0")" && pwd)"
SCRIPT="$HERE/nav-contract-audit.sh"
SCRATCH="$(mktemp -d)"
trap 'rm -rf "$SCRATCH"' EXIT
PASS=0; FAIL=0
ok() { printf ' ✓ %s\n' "$1"; PASS=$((PASS + 1)); }
no() { printf ' ✗ %s\n' "$1"; FAIL=$((FAIL + 1)); }
# make_repo <dev_root> <name> <file_path> <file_content> [origin_url]
make_repo() {
local root="$1" name="$2" file="$3" content="$4" origin="${5:-}"
local dir="$root/$name"
mkdir -p "$dir/$(dirname "$file")"
printf '%s' "$content" > "$dir/$file"
git -C "$dir" init -q -b main
git -C "$dir" -c user.email=t@test -c user.name=t -c commit.gpgsign=false \
add -A
git -C "$dir" -c user.email=t@test -c user.name=t -c commit.gpgsign=false \
-c core.hooksPath=/dev/null commit -q -m init
# The audit reads a REMOTE-TRACKING ref (origin/<branch>), never a local
# branch — a bare `git init` fixture has no such ref until something fetches
# it. Fake the ref directly rather than standing up a real remote.
git -C "$dir" update-ref refs/remotes/origin/main "$(git -C "$dir" rev-parse HEAD)"
if [ -n "$origin" ]; then
git -C "$dir" remote add origin "$origin"
fi
}
run_audit() {
local root="$1"
DEV_ROOT="$root" bash "$SCRIPT" 2>&1
echo "EXIT:$?"
}
# ── 1. catches a real defect ────────────────────────────────────────────────
d="$SCRATCH/case1"
make_repo "$d" broken-repo "components/Nav.tsx" '
export function Nav({ pathname }) {
return (
<nav>
{items.map(i => (
<Link href={i.href} className={pathname === i.href ? "active" : ""}>
{i.label}
</Link>
))}
</nav>
);
}
'
out="$(run_audit "$d")"
if grep -q "EXIT:1" <<<"$out" && grep -q "broken-repo" <<<"$out"; then
ok "catches a repo that styles the current page and never announces it"
else
no "should have failed and named broken-repo, got: $out"
fi
# ── 2. quiet when nothing computes an active state ──────────────────────────
d="$SCRATCH/case2"
make_repo "$d" static-repo "components/Footer.tsx" '
export function Footer() {
return (
<nav>
<Link href="/a">A</Link>
<Link href="/b">B</Link>
</nav>
);
}
'
out="$(run_audit "$d")"
if grep -q "EXIT:0" <<<"$out"; then
ok "stays quiet on a nav with no active-state computation (nothing to announce)"
else
no "should have passed, got: $out"
fi
# ── 3. quiet when it already announces correctly ────────────────────────────
d="$SCRATCH/case3"
make_repo "$d" correct-repo "components/Nav.tsx" '
export function Nav({ pathname }) {
return (
<nav>
{items.map(i => (
<Link href={i.href} aria-current={pathname === i.href ? "page" : undefined}>
{i.label}
</Link>
))}
</nav>
);
}
'
out="$(run_audit "$d")"
if grep -q "EXIT:0" <<<"$out"; then
ok "stays quiet on a repo that already sets aria-current"
else
no "should have passed, got: $out"
fi
# ── 4. quiet on a tab list (aria-selected is correct there, not aria-current) ─
# This exact shape fired on evig's AnalyseTabs before the rule excluded it.
d="$SCRATCH/case4"
make_repo "$d" tabs-repo "components/nav/AnalyseTabs.tsx" '
export function AnalyseTabs({ activeTab }) {
return (
<nav role="tablist">
{tabs.map(t => (
<Link href={t.href} role="tab" aria-selected={activeTab === t.id}>
{t.label}
</Link>
))}
</nav>
);
}
'
out="$(run_audit "$d")"
if grep -q "EXIT:0" <<<"$out"; then
ok "does NOT demand aria-current from a tab list using aria-selected"
else
no "should have passed (tab list is exempt), got: $out"
fi
# ── 5. baseline suppresses a named repo ─────────────────────────────────────
d="$SCRATCH/case5"
make_repo "$d" exempt-repo "components/Nav.tsx" '
export function Nav({ pathname }) {
return (
<nav>
<Link href={x} className={pathname === x ? "active" : ""}>Only</Link>
</nav>
);
}
'
bad="$(NAV_CONTRACT_BASELINE=/nonexistent run_audit "$d")"
if grep -q "EXIT:1" <<<"$bad"; then
ok "fails without a baseline entry (sanity check before testing suppression)"
else
no "sanity check itself failed, got: $bad"
fi
baseline="$SCRATCH/case5.baseline"
printf 'exempt-repo # test fixture\n' > "$baseline"
good="$(NAV_CONTRACT_BASELINE="$baseline" run_audit "$d")"
if grep -q "EXIT:0" <<<"$good"; then
ok "a baseline entry suppresses a named repo"
else
no "baseline should have suppressed exempt-repo, got: $good"
fi
# ── 6. two directories, one origin remote → counted once ───────────────────
d="$SCRATCH/case6"
make_repo "$d" repo-a "components/Nav.tsx" '
export function Nav({ pathname }) {
return (
<nav>
<Link href={x} className={pathname === x ? "active" : ""}>Only</Link>
</nav>
);
}
' "https://github.com/example/same-repo.git"
make_repo "$d" repo-b "components/Nav.tsx" '
export function Nav({ pathname }) {
return (
<nav>
<Link href={x} className={pathname === x ? "active" : ""}>Only</Link>
</nav>
);
}
' "https://github.com/example/same-repo.git"
out="$(run_audit "$d")"
# Should report exactly ONE offending repo, not two, despite two directories.
n=$(echo "$out" | grep -cE "^ (repo-a|repo-b) —")
if [ "$n" = "1" ]; then
ok "two clones of the same origin remote are audited once, not twice"
else
no "expected exactly 1 finding for the deduped repo, got $n: $out"
fi
echo
echo "$PASS/$((PASS + FAIL)) nav-contract-audit self-tests passed"
[ "$FAIL" -eq 0 ]