Skip to content

Commit 259b537

Browse files
test: add fuzzy-match edge case and validate state file assertions (merge worktree-20260324-211622)
2 parents e76fc83 + d261737 commit 259b537

File tree

5 files changed

+44
-17
lines changed

5 files changed

+44
-17
lines changed

plugins/dso/scripts/project-detect.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ _discover_suites() {
126126
for subdir in "$project_dir/$test_root"/*/; do
127127
[[ -d "$subdir" ]] || continue
128128
local has_test_files
129-
has_test_files="$(find "$subdir" -maxdepth 1 -name 'test_*.py' -print -quit 2>/dev/null)"
129+
has_test_files="$(find "$subdir" -maxdepth 1 -name 'test_*.py' 2>/dev/null | head -1)"
130130
if [[ -n "$has_test_files" ]]; then
131131
local dirname
132132
dirname="$(basename "$subdir")"

plugins/dso/scripts/validate.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -501,8 +501,7 @@ _test_state_already_passed() {
501501
# Compute the expected command hash so we can verify the state file
502502
# belongs to this command (not a different test command).
503503
local expected_hash
504-
expected_hash=$(echo -n "${test_cmd}:$(pwd)" | sha256sum 2>/dev/null | awk '{print $1}' || \
505-
echo -n "${test_cmd}:$(pwd)" | python3 -c "import sys,hashlib; print(hashlib.sha256(sys.stdin.buffer.read()).hexdigest())")
504+
expected_hash=$(python3 -c "import hashlib,sys; print(hashlib.sha256(sys.argv[1].encode()).hexdigest())" "${test_cmd}:$(pwd)")
506505
python3 - "$expected_hash" "$state_file" <<PYEOF 2>/dev/null
507506
import json, sys
508507
expected_hash = sys.argv[1]

tests/hooks/test-fuzzy-match.sh

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -228,18 +228,19 @@ test_benchmark_20_files() {
228228
git -C "$repo" add -A && git -C "$repo" commit -m "add 20 pairs" --quiet 2>/dev/null
229229

230230
local start_time end_time elapsed
231-
start_time=$(date +%s)
231+
start_time=$(python3 -c "import time; print(time.time())")
232232
for i in $(seq 1 20); do
233233
fuzzy_find_associated_tests "$repo/src/module_${i}.py" "$repo" >/dev/null 2>&1
234234
done
235-
end_time=$(date +%s)
236-
elapsed=$(( end_time - start_time ))
235+
end_time=$(python3 -c "import time; print(time.time())")
236+
elapsed=$(python3 -c "print(int(($end_time - $start_time) * 1000))")
237237

238-
if (( elapsed < 10 )); then
238+
# elapsed is now in milliseconds; 10s = 10000ms
239+
if (( elapsed < 10000 )); then
239240
(( ++PASS ))
240241
else
241242
(( ++FAIL ))
242-
echo "FAIL: benchmark_20_files — took ${elapsed}s (limit: 10s)" >&2
243+
echo "FAIL: benchmark_20_files — took ${elapsed}ms (limit: 10000ms)" >&2
243244
fi
244245
}
245246

@@ -287,6 +288,27 @@ test_red_phase_counter_additive() {
287288
fi
288289
}
289290

291+
# ── Test 12: source-file-is-test-file edge case ──────────────────────────────
292+
# Bug dso-ovj9: when the source file IS a test file (e.g., tests/test_foo.py),
293+
# fuzzy_find_associated_tests should return itself (it's its own test).
294+
test_source_file_is_test_file() {
295+
if (( ! _FUZZY_MATCH_LOADED )); then
296+
(( ++FAIL ))
297+
echo "FAIL: test_source_file_is_test_file — fuzzy-match.sh not loaded" >&2
298+
return
299+
fi
300+
local repo
301+
repo=$(create_test_repo)
302+
mkdir -p "$repo/tests"
303+
echo "pass" > "$repo/tests/test_foo.py"
304+
git -C "$repo" add -A && git -C "$repo" commit -m "add test" --quiet 2>/dev/null
305+
306+
local result
307+
result=$(fuzzy_find_associated_tests "$repo/tests/test_foo.py" "$repo")
308+
# A test file queried as source should return itself (it's its own associated test)
309+
assert_ne "source-is-test: result should be non-empty" "" "$result"
310+
}
311+
290312
# ── Run all tests ────────────────────────────────────────────────────────────
291313
test_bash_convention_matches
292314
test_python_convention_matches
@@ -299,6 +321,7 @@ test_custom_test_dirs
299321
test_benchmark_20_files
300322
test_dogfood_bump_version
301323
test_red_phase_counter_additive
324+
test_source_file_is_test_file
302325

303326
# ── Summary ──────────────────────────────────────────────────────────────────
304327
# In RED phase (library missing), all tests correctly FAIL. Print summary with

tests/hooks/test-review-complexity-classifier.sh

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -659,15 +659,7 @@ test_classifier_is_merge_commit_size_action_none() {
659659
setup_temp_dir
660660
local diff_file
661661
diff_file=$(create_n_line_diff 600 "src/foo.py")
662-
export MOCK_MERGE_HEAD=1
663-
CLASSIFIER_OUTPUT=""
664-
CLASSIFIER_EXIT=0
665-
if [[ -x "$CLASSIFIER" ]]; then
666-
CLASSIFIER_OUTPUT=$(MOCK_MERGE_HEAD=1 bash "$CLASSIFIER" < "$diff_file" 2>/dev/null) || CLASSIFIER_EXIT=$?
667-
else
668-
CLASSIFIER_EXIT=127
669-
fi
670-
unset MOCK_MERGE_HEAD
662+
MOCK_MERGE_HEAD=1 run_classifier "$diff_file"
671663

672664
local size_action=""
673665
if [[ "$CLASSIFIER_EXIT" -eq 0 ]] && is_valid_json "$CLASSIFIER_OUTPUT"; then

tests/scripts/test-validate-test-batched-integration.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,19 @@ output=$(
146146
# validate.sh must exit 2 when tests are pending
147147
assert_eq "test_validate_exits_2_on_partial_tests exits 2 (pending)" "2" "$rc"
148148

149+
# Bug dso-w7bs: verify the state file was written with expected partial content
150+
if [[ -f "$_partial_state_file" ]]; then
151+
_state_has_interrupted=$(python3 -c "
152+
import json,sys
153+
d=json.load(open(sys.argv[1]))
154+
print('yes' if d.get('signal_interrupted') else 'no')
155+
" "$_partial_state_file" 2>/dev/null || echo "error")
156+
assert_eq "test_validate_exits_2_on_partial_tests state file has signal_interrupted" "yes" "$_state_has_interrupted"
157+
else
158+
(( ++FAIL ))
159+
echo "FAIL: test_validate_exits_2_on_partial_tests — state file not created at $_partial_state_file" >&2
160+
fi
161+
149162
assert_pass_if_clean "test_validate_exits_2_on_partial_tests"
150163

151164
# ============================================================================

0 commit comments

Comments
 (0)