find-polluter.sh 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/usr/bin/env bash
  2. # Bisection script to find which test creates unwanted files/state
  3. # Usage: ./find-polluter.sh <file_or_dir_to_check> <test_pattern>
  4. # Example: ./find-polluter.sh '.git' 'src/**/*.test.ts'
  5. set -e
  6. if [ $# -ne 2 ]; then
  7. echo "Usage: $0 <file_to_check> <test_pattern>"
  8. echo "Example: $0 '.git' 'src/**/*.test.ts'"
  9. exit 1
  10. fi
  11. POLLUTION_CHECK="$1"
  12. TEST_PATTERN="$2"
  13. echo "🔍 Searching for test that creates: $POLLUTION_CHECK"
  14. echo "Test pattern: $TEST_PATTERN"
  15. echo ""
  16. # Get list of test files (find . emits ./-prefixed paths, so accept the
  17. # pattern written with or without a leading ./)
  18. TEST_PATTERN="${TEST_PATTERN#./}"
  19. # find -path can't match '**/' against zero directory levels, so a pattern
  20. # like src/**/*.test.ts would skip src/top.test.ts; also try the pattern
  21. # with '**/' collapsed to cover files directly under the base directory.
  22. TEST_FILES=$(find . \( -path "./$TEST_PATTERN" -o -path "./${TEST_PATTERN//\*\*\//}" \) | sort -u)
  23. if [ -z "$TEST_FILES" ]; then
  24. TOTAL=0
  25. else
  26. TOTAL=$(printf '%s\n' "$TEST_FILES" | wc -l | tr -d ' ')
  27. fi
  28. echo "Found $TOTAL test files"
  29. echo ""
  30. COUNT=0
  31. for TEST_FILE in $TEST_FILES; do
  32. COUNT=$((COUNT + 1))
  33. # Skip if pollution already exists
  34. if [ -e "$POLLUTION_CHECK" ]; then
  35. echo "⚠️ Pollution already exists before test $COUNT/$TOTAL"
  36. echo " Skipping: $TEST_FILE"
  37. continue
  38. fi
  39. echo "[$COUNT/$TOTAL] Testing: $TEST_FILE"
  40. # Run the test
  41. npm test "$TEST_FILE" > /dev/null 2>&1 || true
  42. # Check if pollution appeared
  43. if [ -e "$POLLUTION_CHECK" ]; then
  44. echo ""
  45. echo "🎯 FOUND POLLUTER!"
  46. echo " Test: $TEST_FILE"
  47. echo " Created: $POLLUTION_CHECK"
  48. echo ""
  49. echo "Pollution details:"
  50. ls -la "$POLLUTION_CHECK"
  51. echo ""
  52. echo "To investigate:"
  53. echo " npm test $TEST_FILE # Run just this test"
  54. echo " cat $TEST_FILE # Review test code"
  55. exit 1
  56. fi
  57. done
  58. echo ""
  59. echo "✅ No polluter found - all tests clean!"
  60. exit 0