zero-check-pipeline/validate/tests.sh
Travis Herbranson 145e0b0a98 tests: fix SIGPIPE under pipefail; use -print -quit instead of | head -1
`set -o pipefail` at the top of tests.sh combined with `find ... | head -1`
is a silent-killer pattern. When `find` produces lots of matches (e.g.
when uv has installed dozens of third-party packages with test_*.py
files under .venv), `head -1` reads its single line and closes the pipe.
`find` writes again, gets SIGPIPE, exits 141. pipefail propagates 141.
`set -e` fires. tests.sh exits 141 before printing `-- Python (pytest ...) --`
or any other output. run-all.sh records `tests: failed` with no
indication of WHY.

Reproduced directly:
    bash -c 'set -euo pipefail; x=$(find <large-tree> -name "test_*.py" -o -name "*_test.py" | head -1); echo "got: $x"'
    exit=141              # "got:" never prints

Fix: replace `| head -1` with `-print -quit` so find stops itself
after the first match instead of getting cut off mid-write. As a free
bonus, also prune the well-known noise dirs (.venv, .git, etc.) so the
probe doesn't even consider third-party test files in the first place —
this keeps the probe fast on dense trees and avoids matching
@pytest.fixture-decorated files in installed packages.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-12 07:51:33 -04:00

77 lines
2.0 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m'
TARGET="${1:-.}"
errors=0
echo "=== Test Suite ==="
# Probe for test files. Prune known noise dirs so we don't trip on
# third-party test files installed under .venv, and use -print -quit so
# the probe doesn't SIGPIPE under `set -o pipefail` on dense trees
# (which exits the script silently with status 141 before any stage
# output is printed).
has_python=$(find "$TARGET" \
\( -path '*/__pycache__' -o -path '*/.venv' -o -path '*/venv' \
-o -path '*/.git' -o -path '*/node_modules' \) -prune \
-o \( -name 'test_*.py' -o -name '*_test.py' \) -print -quit)
has_go=$(find "$TARGET" \
\( -path '*/.git' -o -path '*/vendor' -o -path '*/node_modules' \) -prune \
-o -name '*_test.go' -print -quit)
if [ -z "$has_python" ] && [ -z "$has_go" ]; then
printf "${YELLOW}!${NC} No test files found — skipping\n"
exit 0
fi
if [ -n "$has_python" ]; then
if ! command -v pytest &>/dev/null; then
printf "${RED}${NC} pytest not installed\n"
exit 2
fi
has_uvlock=$(find "$TARGET" -maxdepth 2 -name "uv.lock" | head -1)
echo "-- Python (pytest --cov) --"
if [ -n "$has_uvlock" ]; then
printf " mode: uv project\n"
uv_dir=$(dirname "$has_uvlock")
test_cmd="uv run --directory $uv_dir pytest --cov --cov-report=term-missing -q"
else
printf " mode: global pytest\n"
test_cmd="pytest $TARGET --cov --cov-report=term-missing -q"
fi
if $test_cmd 2>/dev/null; then
printf "${GREEN}${NC} Python tests passed\n"
else
printf "${RED}${NC} Python tests failed\n"
errors=$((errors + 1))
fi
fi
if [ -n "$has_go" ]; then
if ! command -v go &>/dev/null; then
printf "${RED}${NC} go not installed\n"
exit 2
fi
echo "-- Go (go test -cover) --"
if go test -cover ./... 2>/dev/null; then
printf "${GREEN}${NC} Go tests passed\n"
else
printf "${RED}${NC} Go tests failed\n"
errors=$((errors + 1))
fi
fi
if [ "$errors" -gt 0 ]; then
exit 1
else
exit 0
fi