test-find-polluter.sh 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
  4. REPO_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
  5. SCRIPT_UNDER_TEST="$REPO_ROOT/skills/systematic-debugging/find-polluter.sh"
  6. FAILURES=0
  7. TEST_ROOT="$(mktemp -d)"
  8. cleanup() {
  9. rm -rf "$TEST_ROOT"
  10. }
  11. trap cleanup EXIT
  12. pass() {
  13. echo " [PASS] $1"
  14. }
  15. fail() {
  16. echo " [FAIL] $1"
  17. FAILURES=$((FAILURES + 1))
  18. }
  19. assert_contains() {
  20. local haystack="$1"
  21. local needle="$2"
  22. local description="$3"
  23. if printf '%s' "$haystack" | grep -Fq -- "$needle"; then
  24. pass "$description"
  25. else
  26. fail "$description (expected output to contain: $needle)"
  27. fi
  28. }
  29. # Toy project: one top-level test, one nested test. A stubbed `npm` on PATH
  30. # creates the pollution marker whenever any test runs, so the first test file
  31. # executed is always identified as the polluter.
  32. setup_project() {
  33. PROJECT="$TEST_ROOT/project"
  34. rm -rf "$PROJECT"
  35. mkdir -p "$PROJECT/src/feature" "$PROJECT/bin"
  36. echo "test('top')" > "$PROJECT/src/top.test.ts"
  37. echo "test('nested')" > "$PROJECT/src/feature/nested.test.ts"
  38. cat > "$PROJECT/bin/npm" <<'EOF'
  39. #!/usr/bin/env bash
  40. touch pollution.marker
  41. EOF
  42. chmod +x "$PROJECT/bin/npm"
  43. }
  44. # run_polluter <pattern> — runs the script in the toy project with the stub
  45. # npm first on PATH; captures combined output, never aborts on exit code.
  46. run_polluter() {
  47. local pattern="$1"
  48. rm -f "$PROJECT/pollution.marker"
  49. (
  50. cd "$PROJECT"
  51. PATH="$PROJECT/bin:$PATH" "$SCRIPT_UNDER_TEST" 'pollution.marker' "$pattern" 2>&1
  52. ) || true
  53. }
  54. echo "Test: documented pattern finds nested test files (issue #2008)"
  55. setup_project
  56. OUTPUT="$(run_polluter 'src/**/*.test.ts')"
  57. assert_contains "$OUTPUT" "FOUND POLLUTER" "documented pattern runs tests and detects pollution"
  58. echo "Test: documented pattern also finds top-level test files"
  59. setup_project
  60. OUTPUT="$(run_polluter 'src/**/*.test.ts')"
  61. assert_contains "$OUTPUT" "Found 2 test files" "src/**/*.test.ts matches src/top.test.ts and src/feature/nested.test.ts"
  62. echo "Test: ./-prefixed pattern matches the same files"
  63. setup_project
  64. OUTPUT="$(run_polluter './src/**/*.test.ts')"
  65. assert_contains "$OUTPUT" "Found 2 test files" "leading ./ on the pattern is accepted"
  66. echo "Test: non-matching pattern reports an honest zero"
  67. setup_project
  68. OUTPUT="$(run_polluter 'nomatch/**/*.test.ts')"
  69. assert_contains "$OUTPUT" "Found 0 test files" "empty result counts as 0, not 1"
  70. assert_contains "$OUTPUT" "No polluter found" "empty result exits via the clean path"
  71. echo ""
  72. if [ "$FAILURES" -gt 0 ]; then
  73. echo "$FAILURES test(s) failed"
  74. exit 1
  75. fi
  76. echo "All tests passed"