Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184#!/usr/bin/env bash# auto-test.sh - Run corresponding test file after Write/Edit tool calls.# Reads PostToolUse JSON from stdin, maps source files to test files by convention.# Outputs [auto-test] prefixed results to stderr so Claude can see pass/fail.# Exits 2 when tests fail so Claude Code surfaces the stderr output.set -euo pipefail
input=$(cat)file_path=$(echo "$input" | jq -r '.tool_input.file_path // ""')
if [ -z "$file_path" ] || [ ! -f "$file_path" ]; then exit 0fi
has_failures=0
# Run a test command and emit prefixed output to stderr.# Usage: report <label> <file> <cmd> [args...]report() { local label="$1" file="$2" shift 2 local out rc=0 out=$(timeout 45 "$@" 2>&1) || rc=$? if [ -n "$out" ]; then local prefix="[auto-test] $label: $file: " printf '%s\n' "$out" | awk -v p="$prefix" '{print p $0}' >&2 fi if [ "$rc" -ne 0 ]; then has_failures=2 fi}
# Walk up from a directory to find a file marker (Gemfile, package.json, etc.).# Prints the directory containing the marker. Returns 1 if not found.find_project_root() { local dir="$1" marker="$2" while [ "$dir" != "/" ]; do if [ -f "$dir/$marker" ]; then echo "$dir" return 0 fi dir=$(dirname "$dir") done return 1}
# Project-specific config: walk up from the file's directory looking for .auto-tools.json.# Returns "disabled", a command string, or "absent" (fall through to defaults)._auto_tools_config_cmd() { local section="$1" ext="$2" dir config_file dir="$(dirname "$file_path")" while [ "$dir" != "/" ]; do config_file="$dir/.auto-tools.json" if [ -f "$config_file" ]; then jq -r --arg s "$section" --arg e "$ext" ' if ((.[$s] | type) == "object") and (.[$s] | has($e)) then if .[$s][$e] == null then "disabled" else .[$s][$e] end else "absent" end ' "$config_file" 2>/dev/null || echo "absent" return fi dir="$(dirname "$dir")" done echo "absent"}
ext=".${file_path##*.}"_test_cmd="$(_auto_tools_config_cmd "test" "$ext")"if [ "$_test_cmd" = "disabled" ]; then exit 0elif [ "$_test_cmd" != "absent" ]; then _subcmd="${_test_cmd/\{\}/\"\$AFMT_FILE\"}" _out=$(AFMT_FILE="$file_path" timeout 45 bash -c "$_subcmd" 2>&1) || has_failures=2 if [ -n "$_out" ]; then printf '%s\n' "$_out" | awk -v p="[auto-test] custom: $file_path: " '{print p $0}' >&2 fi exit "$has_failures"fi
case "$file_path" in# --- Ruby ---*_spec.rb) # Test file edited directly: run it if root=$(find_project_root "$(dirname "$file_path")" "Gemfile"); then report "rspec" "$file_path" bundle exec rspec "$file_path" --format progress --no-color fi ;;*.rb) # Source file: map to corresponding spec if root=$(find_project_root "$(dirname "$file_path")" "Gemfile"); then rel="${file_path#"$root/"}" spec_rel="${rel#app/}" spec_rel="spec/${spec_rel%.rb}_spec.rb" spec_path="$root/$spec_rel" if [ -f "$spec_path" ]; then report "rspec" "$spec_path" bundle exec rspec "$spec_path" --format progress --no-color fi fi ;;# --- Go ---*_test.go) # Test file: run the package pkg_dir=$(dirname "$file_path") report "go test" "$file_path" go test "$pkg_dir" -count=1 -v ;;*.go) # Source file: run package tests if any _test.go files exist pkg_dir=$(dirname "$file_path") if ls "$pkg_dir"/*_test.go &>/dev/null; then report "go test" "$file_path" go test "$pkg_dir" -count=1 -v fi ;;# --- JavaScript / TypeScript (test files) ---*.test.ts | *.test.tsx | *.test.js | *.test.jsx | *.spec.ts | *.spec.tsx | *.spec.js | *.spec.jsx) if root=$(find_project_root "$(dirname "$file_path")" "package.json"); then report "jest" "$file_path" npx jest --testPathPattern "$(basename "$file_path")" --no-coverage fi ;;# --- JavaScript / TypeScript (source files) ---*.ts | *.tsx | *.js | *.jsx) dir=$(dirname "$file_path") base=$(basename "$file_path") ext="${base##*.}" name="${base%.*}" for test_file in "$dir/$name.test.$ext" "$dir/$name.spec.$ext" "$dir/__tests__/$name.test.$ext"; do if [ -f "$test_file" ]; then if root=$(find_project_root "$dir" "package.json"); then report "jest" "$test_file" npx jest --testPathPattern "$(basename "$test_file")" --no-coverage fi break fi done ;;# --- Python (test files) ---*/test_*.py | *_test.py) if root=$(find_project_root "$(dirname "$file_path")" "pyproject.toml") || root=$(find_project_root "$(dirname "$file_path")" "setup.py"); then report "pytest" "$file_path" python -m pytest "$file_path" -v --no-header --tb=short fi ;;# --- Python (source files) ---*.py) dir=$(dirname "$file_path") base=$(basename "$file_path" .py) if root=$(find_project_root "$dir" "pyproject.toml") || root=$(find_project_root "$dir" "setup.py"); then for test_file in "$root/tests/test_${base}.py" "$root/tests/${base}_test.py" "$dir/test_${base}.py"; do if [ -f "$test_file" ]; then report "pytest" "$test_file" python -m pytest "$test_file" -v --no-header --tb=short break fi done fi ;;# --- Shell / Bats (test files) ---*.bats) # Test file edited directly: run it with bats report "bats" "$file_path" bats "$file_path" ;;# --- Shell / Bats (source files) ---*.sh) dir=$(dirname "$file_path") base=$(basename "$file_path" .sh) if [[ "$dir" == */scripts ]]; then # Primary: sibling scripts/ -> tests/ (e.g., plugins/foo/scripts/bar.sh -> plugins/foo/tests/bar.bats) parent=$(dirname "$dir") test_file="$parent/tests/${base}.bats" if [ -f "$test_file" ]; then report "bats" "$test_file" bats "$test_file" fi else # Fallback: tests/ subdirectory or same directory for test_file in "$dir/tests/${base}.bats" "$dir/${base}.bats"; do if [ -f "$test_file" ]; then report "bats" "$test_file" bats "$test_file" break fi done fi ;;esac
exit "$has_failures"