#!/usr/bin/env bash # Stop hook: block "done" while ruff or tests fail on changed Python code. input=$(cat) [[ "$(jq -r '.stop_hook_active // false' <<<"$input")" == "true" ]] && exit 0 cd "$CLAUDE_PROJECT_DIR" || exit 0 [[ -f pyproject.toml ]] || exit 0 command -v uv >/dev/null || exit 0 # -uall lists files inside new folders (e.g. a fresh src/), not just the folder. git status --porcelain -uall | grep -q '\.py$' || exit 0 if ! out=$(uv run --quiet ruff check . 2>&1); then echo "ruff check failed, fix before finishing:" >&2 tail -n 30 <<<"$out" >&2 exit 2 fi # Files written via Bash skip the PostToolUse formatter, so check format here like CI does. if ! out=$(uv run --quiet ruff format --check . 2>&1); then echo "ruff format check failed, run 'uv run ruff format .':" >&2 tail -n 30 <<<"$out" >&2 exit 2 fi if grep -q pyright pyproject.toml && ! out=$(uv run --quiet pyright 2>&1); then echo "pyright failed, fix before finishing:" >&2 tail -n 30 <<<"$out" >&2 exit 2 fi out=$(uv run --quiet pytest -q -x 2>&1) status=$? # 0 = passed, 5 = no tests collected yet; neither should block. if [[ $status -ne 0 && $status -ne 5 ]]; then echo "pytest failed, fix before finishing:" >&2 tail -n 40 <<<"$out" >&2 exit 2 fi exit 0