Files
deverenz/.claude/hooks/check-on-stop.sh
T
kasun e91d05497d
CI / checks (pull_request) Successful in 48s
chore: set up Python project skeleton with CI checks
pyright skips .claude/scripts (it always adds its dot-directory exclude); ruff still checks it. The Stop hook now also runs ruff format --check, matching CI.
2026-11-15 15:23:57 +01:00

36 lines
1.2 KiB
Bash
Executable File

#!/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