Skip to content

Commit ba8e23b

Browse files
authored
Merge pull request #405 from TypedDevs/codex/fix-parallel-test-failing-due-to-shared-global-file
Fix parallel spy temp file conflicts
2 parents a57a59e + 1d34c28 commit ba8e23b

5 files changed

Lines changed: 34 additions & 2 deletions

File tree

src/runner.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,13 @@ function runner::run_test() {
136136
local fn_name="$1"
137137
shift
138138

139+
# Export a unique test identifier so that test doubles can
140+
# create temporary files scoped per test run. This prevents
141+
# race conditions when running tests in parallel.
142+
local sanitized_fn_name
143+
sanitized_fn_name="$(helper::normalize_variable_name "$fn_name")"
144+
export BASHUNIT_CURRENT_TEST_ID="${sanitized_fn_name}_$$"
145+
139146
local interpolated_fn_name="$(helper::interpolate_function_name "$fn_name" "$@")"
140147
local current_assertions_failed="$(state::get_assertions_failed)"
141148
local current_assertions_snapshot="$(state::get_assertions_snapshot)"

src/test_doubles.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ function spy() {
4848
export "${variable}_params"
4949

5050
local times_file params_file
51-
times_file=$(temp_file "${variable}_times")
52-
params_file=$(temp_file "${variable}_params")
51+
local test_id="${BASHUNIT_CURRENT_TEST_ID:-global}"
52+
times_file=$(temp_file "${test_id}_${variable}_times")
53+
params_file=$(temp_file "${test_id}_${variable}_params")
5354
echo 0 > "$times_file"
5455
: > "$params_file"
5556
export "${variable}_times_file"="$times_file"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env bash
2+
3+
function test_spy_file1() {
4+
spy date
5+
date
6+
assert_have_been_called_times 1 date
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env bash
2+
3+
function test_spy_file2() {
4+
spy date
5+
date
6+
assert_have_been_called_times 1 date
7+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
function test_spies_work_in_parallel() {
5+
local file1=tests/acceptance/fixtures/test_parallel_spy_file1.sh
6+
local file2=tests/acceptance/fixtures/test_parallel_spy_file2.sh
7+
8+
./bashunit --parallel "$file1" "$file2"
9+
assert_successful_code
10+
}

0 commit comments

Comments
 (0)