跳到主要内容

系统化调试

收藏0

遇到 bug、测试失败或异常行为时,先定位根因再动手改的四阶段排查流程,附带根因回溯、纵深防御与条件等待三篇方法文档。

让 AI 安装

星光尘缘最新版本 0.1.0更新于 2026-11-08下载 0 次

find-polluter.sh

1.9 KB
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
6set -e
7
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
13
14POLLUTION_CHECK="$1"
15TEST_PATTERN="$2"
16
17echo "🔍 Searching for test that creates: $POLLUTION_CHECK"
18echo "Test pattern: $TEST_PATTERN"
19echo ""
20
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
33
34echo "Found $TOTAL test files"
35echo ""
36
37COUNT=0
38for TEST_FILE in $TEST_FILES; do
39 COUNT=$((COUNT + 1))
40
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
47
48 echo "[$COUNT/$TOTAL] Testing: $TEST_FILE"
49
50 # Run the test
51 npm test "$TEST_FILE" > /dev/null 2>&1 || true
52
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
69
70echo ""
71echo "✅ No polluter found - all tests clean!"
72exit 0

文件内容是平台检查时读出的文本,只供查看。平台不会运行包里的任何脚本或程序。