setup-pre-commit-hook.sh 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/bin/bash
  2. # setup-pre-commit-hook.sh
  3. # Sets up a pre-commit hook to validate test suites before committing
  4. #
  5. # Usage:
  6. # ./scripts/validation/setup-pre-commit-hook.sh
  7. set -e
  8. # Colors
  9. GREEN='\033[0;32m'
  10. BLUE='\033[0;34m'
  11. NC='\033[0m'
  12. # Get script directory
  13. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  14. PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
  15. HOOK_FILE="$PROJECT_ROOT/.git/hooks/pre-commit"
  16. echo -e "${BLUE}Setting up pre-commit hook for test suite validation...${NC}"
  17. echo ""
  18. # Create pre-commit hook
  19. cat > "$HOOK_FILE" << 'EOF'
  20. #!/bin/bash
  21. # Pre-commit hook: Validate test suite JSON files
  22. # Get list of staged JSON files in config directories
  23. STAGED_SUITES=$(git diff --cached --name-only --diff-filter=ACM | grep -E 'evals/agents/.*/config/.*\.json$' || true)
  24. if [[ -n "$STAGED_SUITES" ]]; then
  25. echo "🔍 Validating test suite JSON files..."
  26. # Run validation
  27. if ! ./scripts/validation/validate-test-suites.sh --all; then
  28. echo ""
  29. echo "❌ Test suite validation failed!"
  30. echo " Fix the errors above before committing."
  31. echo ""
  32. echo "To skip this check (not recommended):"
  33. echo " git commit --no-verify"
  34. exit 1
  35. fi
  36. echo "✅ Test suite validation passed"
  37. fi
  38. exit 0
  39. EOF
  40. # Make hook executable
  41. chmod +x "$HOOK_FILE"
  42. echo -e "${GREEN}✅ Pre-commit hook installed${NC}"
  43. echo ""
  44. echo "The hook will automatically validate test suite JSON files before each commit."
  45. echo ""
  46. echo "To test it:"
  47. echo " 1. Edit a test suite: evals/agents/openagent/config/core-tests.json"
  48. echo " 2. Stage the file: git add evals/agents/openagent/config/core-tests.json"
  49. echo " 3. Try to commit: git commit -m 'test'"
  50. echo ""
  51. echo "To skip validation (not recommended):"
  52. echo " git commit --no-verify"