-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfind_broken_test
More file actions
executable file
·383 lines (348 loc) · 11.3 KB
/
Copy pathfind_broken_test
File metadata and controls
executable file
·383 lines (348 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#!/usr/bin/env bash
set -euo pipefail
# find_broken_test
# - Runs Go tests in two tiers: fast packages first (changed ones before the rest),
# then optional slow packages when RUN_SLOW_TESTS=1.
# - Known manual/experimental packages are blocked unless ALLOW_MANUAL=1.
# - Suppresses output for passing tests; prints full logs for the failing test.
# - Integration/browser/benchmark stages only run when slow tests are enabled.
ROOT_DIR="$(git rev-parse --show-toplevel 2>/dev/null || pwd)"
cd "$ROOT_DIR"
log() { printf "%s\n" "$*" >&2; }
debug() { if [[ "${DEBUG:-}" == "1" ]]; then printf "[debug] %s\n" "$*" >&2; fi }
# Packages that require long-running assets or external setup. These only run
# when RUN_SLOW_TESTS=1 and we default to skipping them.
SLOW_PATTERNS=(
"github.com/lee101/gobed"
"github.com/lee101/gobed/ann"
)
# Packages that are known to be experimental or require manual setup. We refuse
# to auto-run them unless ALLOW_MANUAL=1 is set.
MANUAL_PATTERNS=(
"github.com/lee101/gobed/benchmarks..."
"github.com/lee101/gobed/bed..."
"github.com/lee101/gobed/cmd..."
"github.com/lee101/gobed/examples..."
"github.com/lee101/gobed/gpu..."
"github.com/lee101/gobed/gpu_search..."
"github.com/lee101/gobed/internal..."
"github.com/lee101/gobed/pkg/gobed..."
"github.com/lee101/gobed/pkg/gpu..."
"github.com/lee101/gobed/pkg/search..."
"github.com/lee101/gobed/test..."
"github.com/lee101/gobed/test_dup..."
)
pattern_matches() {
local pkg="$1"
shift || return 1
for raw in "$@"; do
[[ -z "$raw" ]] && continue
local glob="${raw//.../*}"
if [[ "$pkg" == $glob ]]; then
return 0
fi
done
return 1
}
array_contains() {
local needle="$1"
shift || true
for item in "$@"; do
[[ "$item" == "$needle" ]] && return 0
done
return 1
}
dedupe_in_place() {
local -n arr=$1
declare -A seen=()
local unique=()
for item in "${arr[@]}"; do
[[ -z "$item" ]] && continue
if [[ -z "${seen[$item]:-}" ]]; then
unique+=("$item")
seen["$item"]=1
fi
done
arr=("${unique[@]}")
}
array_difference() {
local -n result=$1
local -n base=$2
local -n remove=$3
result=()
for item in "${base[@]}"; do
if ! array_contains "$item" "${remove[@]}"; then
result+=("$item")
fi
done
}
# Collect changed files relative to the main branch or upstream
_get_base_ref() {
if git rev-parse --verify --quiet origin/main >/dev/null; then
echo "origin/main"
else
# Fallback to the first remote main-like branch or initial commit
git for-each-ref --format='%(refname:short)' refs/remotes | grep -E 'main|master' | head -n1 || echo "$(git rev-list --max-parents=0 HEAD)"
fi
}
get_changed_paths() {
local base
base=$(_get_base_ref)
# Include staged/unstaged and untracked files
git status --porcelain=v1 | awk '{print $2}'
# Plus diff from base for committed changes on branch
git diff --name-only "$(git merge-base HEAD "$base")"...HEAD || true
}
# Map filesystem paths to Go packages
packages_from_paths() {
local paths=()
while IFS= read -r p; do
[[ -z "$p" ]] && continue
# Skip non-go files quickly; still map dirs
if [[ -d "$p" ]]; then
paths+=("$p")
else
paths+=("$(dirname "$p")")
fi
done
# Unique dirs
printf '%s\n' "${paths[@]}" | awk 'NF' | sort -u | while IFS= read -r dir; do
# Only include dirs that are part of a Go module and buildable
if go list -f '{{.ImportPath}}' "$dir" >/dev/null 2>&1; then
go list -f '{{.ImportPath}}' "$dir"
fi
done | sort -u
}
run_go_tests_for_packages() {
local tag_opts=$1; shift || true
local pkgs=("$@")
if [[ ${#pkgs[@]} -eq 0 ]]; then
return 0
fi
local go_args=()
if [[ -n "${GO_TEST_FLAGS:-}" ]]; then
# shellcheck disable=SC2206
go_args=(${GO_TEST_FLAGS})
fi
for pkg in "${pkgs[@]}"; do
[[ -z "$pkg" ]] && continue
debug "Testing package: $pkg ${tag_opts} ${GO_TEST_FLAGS:-}"
local tmp
tmp="$(mktemp)"
# Capture full verbose output for the package
local run_pat
run_pat="${GO_TEST_RUN:-.}"
if ! go test "${go_args[@]}" -run "$run_pat" -v ${tag_opts} "$pkg" >"$tmp" 2>&1; then
log "\n=================================================="
log "❌ FAILED: $pkg"
log "==================================================\n"
cat "$tmp"
rm -f "$tmp"
exit 1
fi
rm -f "$tmp"
done
}
has_integration_tests() {
rg -q "_integration_test\\.go|// *\+build +integration|//go:build +integration" --type go || return 1
}
bench_pkgs() {
# Find packages with Benchmark functions
rg -n "^func +(Benchmark\w+)\(" --type go --glob "**/*_test.go" --no-messages -l | xargs -r -n1 dirname | sort -u | while IFS= read -r d; do
go list "$d" 2>/dev/null || true
done | sort -u
}
run_benchmarks() {
local pkgs
mapfile -t pkgs < <(bench_pkgs || true)
if [[ ${#pkgs[@]} -eq 0 ]]; then
debug "No explicit Go benchmarks detected; skipping benchmark stage."
return 0
fi
for pkg in "${pkgs[@]}"; do
debug "Benchmarking package: $pkg"
local tmp
tmp="$(mktemp)"
if ! go test -bench=. -run=^$ -benchmem "$pkg" >"$tmp" 2>&1; then
log "\n=================================================="
log "❌ BENCH FAILED: $pkg"
log "==================================================\n"
cat "$tmp"
rm -f "$tmp"
exit 1
fi
# On success, keep quiet to avoid noise
rm -f "$tmp"
done
}
packages_with_tests() {
local dirs
local search_root="${TEST_PKG_ROOT:-.}"
local pkg_glob="${TEST_PKG_GLOBS:-**}"
if command -v rg >/dev/null 2>&1; then
mapfile -t dirs < <(rg --no-messages -l --glob "$pkg_glob/*_test.go" --type go "$search_root" 2>/dev/null | xargs -r -n1 dirname | sort -u)
else
mapfile -t dirs < <(find "$search_root" -type f -name '*_test.go' -print 2>/dev/null | xargs -r -n1 dirname | sort -u)
fi
for d in "${dirs[@]:-}"; do
go list "$d" 2>/dev/null || true
done | sort -u
}
main() {
mapfile -t test_pkgs < <(packages_with_tests || true)
declare -A test_pkg_set=()
for p in "${test_pkgs[@]:-}"; do
test_pkg_set["$p"]=1
done
local fast_pkgs=()
local slow_pkgs=()
local manual_pkgs=()
for p in "${test_pkgs[@]:-}"; do
if pattern_matches "$p" "${MANUAL_PATTERNS[@]}"; then
manual_pkgs+=("$p")
continue
fi
if pattern_matches "$p" "${SLOW_PATTERNS[@]}"; then
slow_pkgs+=("$p")
continue
fi
fast_pkgs+=("$p")
done
mapfile -t changed_paths < <(get_changed_paths | awk 'NF' | sort -u)
mapfile -t changed_pkgs_all < <(printf '%s\n' "${changed_paths[@]:-}" | packages_from_paths | sort -u)
local changed_fast=()
local changed_slow=()
local changed_manual=()
local changed_no_tests=()
for p in "${changed_pkgs_all[@]:-}"; do
if pattern_matches "$p" "${MANUAL_PATTERNS[@]}"; then
changed_manual+=("$p")
continue
fi
if pattern_matches "$p" "${SLOW_PATTERNS[@]}"; then
if [[ -n "${test_pkg_set[$p]:-}" ]]; then
changed_slow+=("$p")
else
changed_no_tests+=("$p")
fi
continue
fi
if [[ -n "${test_pkg_set[$p]:-}" ]]; then
changed_fast+=("$p")
else
changed_no_tests+=("$p")
fi
done
if pkg_here=$(go list . 2>/dev/null); then
if pattern_matches "$pkg_here" "${MANUAL_PATTERNS[@]}"; then
changed_manual+=("$pkg_here")
elif pattern_matches "$pkg_here" "${SLOW_PATTERNS[@]}"; then
if [[ -n "${test_pkg_set[$pkg_here]:-}" ]]; then
changed_slow+=("$pkg_here")
fi
elif [[ -n "${test_pkg_set[$pkg_here]:-}" ]]; then
changed_fast+=("$pkg_here")
fi
fi
dedupe_in_place changed_fast
dedupe_in_place changed_slow
dedupe_in_place changed_manual
dedupe_in_place changed_no_tests
if [[ ${#changed_manual[@]} -gt 0 && "${ALLOW_MANUAL:-0}" != "1" ]]; then
log "⚠️ Changes touch packages that require manual verification:"
for pkg in "${changed_manual[@]}"; do
log " - $pkg"
done
log "Set ALLOW_MANUAL=1 to acknowledge and continue."
exit 1
elif [[ ${#changed_manual[@]} -gt 0 ]]; then
debug "ALLOW_MANUAL=1: skipping manual packages ${changed_manual[*]}"
fi
if [[ ${#changed_slow[@]} -gt 0 && "${RUN_SLOW_TESTS:-0}" != "1" ]]; then
log "⚠️ Changes touch slow-test packages. Re-run with RUN_SLOW_TESTS=1 to include them:"
for pkg in "${changed_slow[@]}"; do
log " - $pkg"
done
exit 1
fi
if [[ ${#changed_no_tests[@]} -gt 0 ]]; then
debug "Changed paths without Go tests: ${changed_no_tests[*]}"
fi
local fast_flags
if [[ -z "${FAST_GO_TEST_FLAGS+x}" ]]; then
fast_flags="-short"
else
fast_flags="${FAST_GO_TEST_FLAGS}"
fi
if [[ ${#fast_pkgs[@]} -gt 0 ]]; then
GO_TEST_FLAGS="$fast_flags"
if [[ ${#changed_fast[@]} -gt 0 ]]; then
run_go_tests_for_packages "${GO_TEST_TAGS:-}" "${changed_fast[@]}"
fi
local fast_remaining=()
array_difference fast_remaining fast_pkgs changed_fast
if [[ ${#fast_remaining[@]} -gt 0 ]]; then
run_go_tests_for_packages "${GO_TEST_TAGS:-}" "${fast_remaining[@]}"
else
debug "No additional fast packages beyond changed set."
fi
unset GO_TEST_FLAGS
else
debug "No fast Go test packages detected."
fi
if [[ ${#slow_pkgs[@]} -gt 0 ]]; then
if [[ "${RUN_SLOW_TESTS:-0}" == "1" ]]; then
local slow_flags
if [[ -z "${SLOW_GO_TEST_FLAGS+x}" ]]; then
slow_flags=""
else
slow_flags="${SLOW_GO_TEST_FLAGS}"
fi
GO_TEST_FLAGS="$slow_flags"
if [[ ${#changed_slow[@]} -gt 0 ]]; then
run_go_tests_for_packages "${SLOW_GO_TEST_TAGS:-${GO_TEST_TAGS:-}}" "${changed_slow[@]}"
fi
local slow_remaining=()
array_difference slow_remaining slow_pkgs changed_slow
if [[ ${#slow_remaining[@]} -gt 0 ]]; then
run_go_tests_for_packages "${SLOW_GO_TEST_TAGS:-${GO_TEST_TAGS:-}}" "${slow_remaining[@]}"
else
debug "No additional slow packages beyond changed set."
fi
unset GO_TEST_FLAGS
else
log "⏭️ Skipping slow Go packages (set RUN_SLOW_TESTS=1 to include):"
for pkg in "${slow_pkgs[@]}"; do
log " - $pkg"
done
fi
fi
if [[ "${RUN_SLOW_TESTS:-0}" == "1" ]]; then
if has_integration_tests; then
mapfile -t it_dirs < <(rg -n "_integration_test\\.go|// *\\+build +integration|//go:build +integration" --type go --no-messages -l | xargs -r -n1 dirname | sort -u)
mapfile -t it_pkgs < <(printf '%s\n' "${it_dirs[@]:-}" | while IFS= read -r d; do go list "$d" 2>/dev/null || true; done | sort -u)
if [[ ${#it_pkgs[@]} -gt 0 ]]; then
GO_TEST_FLAGS="${INTEGRATION_GO_TEST_FLAGS:-${SLOW_GO_TEST_FLAGS:-}}"
run_go_tests_for_packages "-tags=integration" "${it_pkgs[@]}"
unset GO_TEST_FLAGS
fi
fi
if [[ -f package.json ]] && jq -e '.scripts.test' package.json >/dev/null 2>&1; then
tmp="$(mktemp)"
if ! npm test --silent >"$tmp" 2>&1; then
log "\n=================================================="
log "❌ BROWSER TESTS FAILED (npm test)"
log "==================================================\n"
cat "$tmp"
rm -f "$tmp"
exit 1
fi
rm -f "$tmp"
fi
run_benchmarks
else
debug "Skipping integration/browser/benchmark stages; enable RUN_SLOW_TESTS=1 to include them."
fi
log "✅ Selected Go test stages completed successfully"
}
main "$@"