Files
fleet/scripts/ci/test-hosted-supabase-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

102 lines
5.8 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# Tests for the hosted-Supabase audit — chiefly the two judgements that decide
# whether it is useful or merely loud.
#
# 1. It must catch a REAL pointer: a dashboard link, a pooler host, a project
# ref. botsmann's setup doc carried all three shapes, and the app served
# PGRST205 for months because nobody could tell they were dead.
# 2. It must NOT catch a placeholder or the product docs. A gate that fires on
# `your-project.supabase.co`, or on a link to supabase.com/docs, gets muted
# — and a muted gate protects nothing.
#
# Pure: no network, no box, no fleet checkout.
set -uo pipefail
HERE="$(cd "$(dirname "$0")" && pwd)"
SCRIPT="$HERE/hosted-supabase-audit.sh"
PASS=0; FAIL=0
ok() { printf ' ✓ %s\n' "$1"; PASS=$((PASS + 1)); }
no() { printf ' ✗ %s\n' "$1"; FAIL=$((FAIL + 1)); }
eq() { [ "$1" = "$2" ] && ok "$3" || no "$3 (want '$1', got '$2')"; }
matches() { grep -qE "$(hosted_pattern)" <<<"$1" && ok "$2" || no "$2 (should match)"; }
no_match() { grep -qE "$(hosted_pattern)" <<<"$1" && no "$2 (should NOT match)" || ok "$2"; }
export HOSTED_SUPABASE_AUDIT_LIB_ONLY=1
# shellcheck source=/dev/null
source "$SCRIPT"
unset HOSTED_SUPABASE_AUDIT_LIB_ONLY
# Sourcing the audit runs its `set -euo pipefail` in OUR shell. A test suite
# whose whole job is to run commands that fail must not die on the first one:
# on a GitHub runner `git commit` exits 128 for want of an identity, and this
# suite stopped mid-file with 128 and no failure report — a red that named
# nothing. Take -e back; keep -u and pipefail.
set +e
echo "hosted_pattern — the shapes that actually misled someone"
matches 'https://supabase.com/dashboard/project/_/sql' "a dashboard link, botsmann's documented migration step"
matches 'https://supabase.com/dashboard/account/tokens' "the account-tokens link orangecat's runbook carried"
matches 'https://app.supabase.com' "the older app. host"
matches 'psql -h aws-0-eu-central-1.pooler.supabase.com -p 6543' "a pooler connection string"
matches 'https://jkjmhtirxwhljpkcfxqe.supabase.co' "botsmann's retired project ref"
matches 'https://ckpynkpsfnuqndplaapc.supabase.co' "printcraft's retired project ref"
matches 'db.ohkueislstxomdjavyhs.supabase.co:5432' "orangecat's, inside a pg_dump URL"
echo
echo "hosted_pattern — what must stay legal, or the gate gets muted"
no_match 'https://supabase.com/docs/guides/storage' "product docs are still correct for us"
no_match 'https://supabase.com/docs' "a bare docs link"
no_match 'NEXT_PUBLIC_SUPABASE_URL="https://your-project.supabase.co"' "a placeholder misleads nobody"
no_match 'https://supabase.orangecat.ch' "our own self-hosted host"
no_match 'import { createClient } from "@supabase/supabase-js"' "the package name is not a host"
echo
echo "is_exempt_path — paths whose job is to describe the old world"
is_exempt_path 'docs/archive/2026-h1/NOTES.md' && ok "docs/archive is history, not instruction" || no "docs/archive should be exempt"
is_exempt_path 'app/docs/archive/OLD.md' && ok "a nested docs/archive too" || no "nested archive should be exempt"
is_exempt_path 'scripts/ci/hosted-supabase.baseline' && ok "the baseline names the refs it allows" || no "baseline should be exempt"
is_exempt_path 'scripts/ci/no-hosted-supabase.sh' && ok "botsmann's local gate states the patterns" || no "local gate should be exempt"
is_exempt_path 'docs/operations/DECOMMISSION.md' && no "a live runbook must NOT be auto-exempt" || ok "a runbook is judged, not waved through"
is_exempt_path '.env.example' && no ".env.example must NOT be auto-exempt" || ok ".env.example is judged"
echo
echo "baseline_keys — comments and blanks are not licences"
TMP="$(mktemp)"
printf '# a header\n\nfoo/bar.md # why it is allowed\n\n \nbaz/qux.ts\n' > "$TMP"
eq 'foo/bar.md
baz/qux.ts' "$(baseline_keys "$TMP")" "reasons stripped, blanks dropped, keys kept"
eq '' "$(baseline_keys /nonexistent/baseline)" "a missing baseline allows nothing, and does not crash"
echo
echo "in_baseline — exact keys, so a prefix cannot smuggle a file through"
in_baseline 'a/b.md' 'a/b.md' 'c/d.md' && ok "an exact key is allowed" || no "exact key should match"
in_baseline 'a/b.md' 'a/b.md.bak' && no "a longer path must not match" || ok "no substring match"
in_baseline 'a/b.md' 'x/y.md' && no "an unrelated key must not match" || ok "unrelated key rejected"
in_baseline 'a/b.md' && no "an empty baseline allows nothing" || ok "empty baseline allows nothing"
echo
echo "repo_ref — audit what is SHARED, not a session's stale checkout"
D="$(mktemp -d)"
trap 'rm -rf "$D" "$TMP"' EXIT
git -C "$D" init -q 2>/dev/null
# Explicit identity: a runner has none, and this fixture must not care.
git -C "$D" -c user.email=test@example.invalid -c user.name=test \
-c commit.gpgsign=false commit -q --allow-empty -m init 2>/dev/null
eq HEAD "$(repo_ref "$D")" "no remote falls back to HEAD rather than failing the sweep"
git -C "$D" update-ref refs/remotes/origin/main HEAD
eq origin/main "$(repo_ref "$D")" "origin/main wins — a local main 4 commits stale reports fixed files as broken"
echo
echo "the sweep must never pass vacuously"
out="$(DEV_ROOT=/nonexistent bash "$SCRIPT" --check 2>&1)"; rc=$?
eq 0 "$rc" "no checkout exits 0, so a runner without the fleet is not a red herring"
grep -q 'SKIPPED' <<<"$out" && ok "but it says SKIPPED — a vacuous pass would read as coverage" \
|| no "must announce the skip, not print a tick"
echo
printf '%d passed, %d failed\n' "$PASS" "$FAIL"
[ "$FAIL" -eq 0 ]