#!/bin/bash # Pre-commit hook to enforce TDD compliance # Verifies that test files are modified alongside production code set -e # Get staged files STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM) # Separate test and production files TEST_FILES="" PROD_FILES="" for file in $STAGED_FILES; do case "$file" in *test*.py|*_test.py|*tests/*.py) TEST_FILES="$TEST_FILES $file" ;; *.test.ts|*.test.tsx|*.spec.ts|*.spec.tsx|*/__tests__/*) TEST_FILES="$TEST_FILES $file" ;; *_test.rs|*/tests/*.rs) TEST_FILES="$TEST_FILES $file" ;; *.py|*.ts|*.tsx|*.js|*.jsx|*.rs) # Skip __init__.py, conftest.py, config files case "$file" in *__init__.py|*conftest.py|*config*.py|*.config.ts|*.config.js) ;; *) PROD_FILES="$PROD_FILES $file" ;; esac ;; esac done # Check if we have production files without test files if [ -n "$PROD_FILES" ] && [ -z "$TEST_FILES" ]; then echo "TDD VIOLATION: Production code changed without test changes" echo "" echo "Production files modified:" for file in $PROD_FILES; do echo " - $file" done echo "" echo "Please ensure you're following TDD:" echo " 1. Write a failing test first (RED)" echo " 2. Write minimum code to pass (GREEN)" echo " 3. Refactor if needed (REFACTOR)" echo "" echo "If this is a legitimate exception (config, types, etc.), use:" echo " git commit --no-verify" exit 1 fi exit 0