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