Skip to content

Commit e85fca8

Browse files
committed
Merge branch 'main' into codex/add-assert_match_snapshot_ignore_colors
2 parents 4c30149 + ba8e23b commit e85fca8

7 files changed

Lines changed: 35 additions & 17 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+
}

tests/functional/doubles_test.sh

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,3 @@ function test_mock_ps_when_executing_a_sourced_function() {
1212

1313
assert_match_snapshot "$(top_mem)"
1414
}
15-
16-
function test_spy_commands_called_once_when_executing_a_sourced_function() {
17-
18-
source ./tests/functional/fixtures/doubles_function.sh
19-
spy ps
20-
spy awk
21-
spy head
22-
23-
top_mem
24-
25-
assert_have_been_called_times 1 ps
26-
assert_have_been_called_times 1 awk
27-
assert_have_been_called_times 1 head
28-
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env bash
22

33
function top_mem() {
4-
ps -eo cmd,%mem --sort=-%mem | awk '$2 >= 1.0 {print $0}' | head -n 3
4+
ps | awk '$2 >= 1.0 {print $0}' | head -n 3
55
}

0 commit comments

Comments
 (0)