Skip to content

Commit ee2fe68

Browse files
authored
fix(doubles): use real mktemp path to prevent mock interference (#603)
1 parent eea58d2 commit ee2fe68

6 files changed

Lines changed: 28 additions & 9 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
- Batch coverage recording with in-memory buffering
3030

3131
### Fixed
32+
- Mocking `mktemp` no longer breaks spy creation (#602)
3233
- JUnit XML report now conforms to the standard schema
3334
- Remove non-standard `passed`, `incomplete`, `snapshot` attributes from `<testsuite>` and `status`, `assertions` from `<testcase>`
3435
- Add `errors="0"` attribute and `<failure>`/`<skipped>` child elements per the JUnit spec

src/assert.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,8 @@ function assert_exec() {
384384
done
385385

386386
local stdout_file stderr_file
387-
stdout_file=$(mktemp)
388-
stderr_file=$(mktemp)
387+
stdout_file=$("$MKTEMP")
388+
stderr_file=$("$MKTEMP")
389389

390390
eval "$cmd" >"$stdout_file" 2>"$stderr_file"
391391
local exit_code=$?

src/env.sh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,10 +271,11 @@ EXIT_CODE_STOP_ON_FAILURE=4
271271
TEMP_DIR_PARALLEL_TEST_SUITE="${TMPDIR:-/tmp}/bashunit/parallel/${_BASHUNIT_OS:-Unknown}/$(bashunit::random_str 8)"
272272
TEMP_FILE_PARALLEL_STOP_ON_FAILURE="$TEMP_DIR_PARALLEL_TEST_SUITE/.stop-on-failure"
273273
TERMINAL_WIDTH="$(bashunit::env::find_terminal_width)"
274-
FAILURES_OUTPUT_PATH=$(mktemp)
275-
SKIPPED_OUTPUT_PATH=$(mktemp)
276-
INCOMPLETE_OUTPUT_PATH=$(mktemp)
277274
CAT="$(command -v cat)"
275+
MKTEMP="$(command -v mktemp)"
276+
FAILURES_OUTPUT_PATH=$("$MKTEMP")
277+
SKIPPED_OUTPUT_PATH=$("$MKTEMP")
278+
INCOMPLETE_OUTPUT_PATH=$("$MKTEMP")
278279

279280
# Initialize temp directory once at startup for performance
280281
BASHUNIT_TEMP_DIR="${TMPDIR:-/tmp}/bashunit/tmp"

src/globals.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function bashunit::temp_file() {
4848
# We're at script level (e.g., in set_up_before_script) - use script ID
4949
test_prefix="${BASHUNIT_CURRENT_SCRIPT_ID}_"
5050
fi
51-
mktemp "$BASHUNIT_TEMP_DIR/${test_prefix}${prefix}.XXXXXXX"
51+
"$MKTEMP" "$BASHUNIT_TEMP_DIR/${test_prefix}${prefix}.XXXXXXX"
5252
}
5353

5454
function bashunit::temp_dir() {
@@ -61,7 +61,7 @@ function bashunit::temp_dir() {
6161
# We're at script level (e.g., in set_up_before_script) - use script ID
6262
test_prefix="${BASHUNIT_CURRENT_SCRIPT_ID}_"
6363
fi
64-
mktemp -d "$BASHUNIT_TEMP_DIR/${test_prefix}${prefix}.XXXXXXX"
64+
"$MKTEMP" -d "$BASHUNIT_TEMP_DIR/${test_prefix}${prefix}.XXXXXXX"
6565
}
6666

6767
function bashunit::cleanup_testcase_temp_files() {

src/runner.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -890,10 +890,10 @@ function bashunit::runner::parse_result_parallel() {
890890
fi
891891

892892
local unique_test_result_file
893-
if unique_test_result_file=$(mktemp -p "$test_suite_dir" "$template" 2>/dev/null); then
893+
if unique_test_result_file=$("$MKTEMP" -p "$test_suite_dir" "$template" 2>/dev/null); then
894894
true
895895
else
896-
unique_test_result_file=$(mktemp "$test_suite_dir/$template")
896+
unique_test_result_file=$("$MKTEMP" "$test_suite_dir/$template")
897897
fi
898898
mv "$unique_test_result_file" "${unique_test_result_file}.result"
899899
unique_test_result_file="${unique_test_result_file}.result"

tests/functional/doubles_test.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,20 @@ function test_spy_commands_called_once_when_executing_a_sourced_function() {
8989
assert_have_been_called_times 1 awk
9090
assert_have_been_called_times 1 head
9191
}
92+
93+
function test_mock_mktemp_does_not_break_spy_creation() {
94+
# shellcheck disable=SC2329
95+
mock_mktemp() {
96+
echo "/tmp/mocked_temp_file"
97+
}
98+
99+
bashunit::mock mktemp mock_mktemp
100+
101+
bashunit::spy rm
102+
103+
rm -f "/tmp/mocked_temp_file"
104+
105+
assert_have_been_called rm
106+
assert_have_been_called_times 1 rm
107+
assert_have_been_called_with rm "-f" "/tmp/mocked_temp_file"
108+
}

0 commit comments

Comments
 (0)