Skip to content

Commit ede9ca7

Browse files
committed
Refactor test count file
Give the variable an explicit name. Use mktemp for better portability. Use trap to ensure we remove this file at the end, whatever happens.
1 parent 409ea0e commit ede9ca7

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

bash_unit

+12-7
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,13 @@ SED="$(type -P sed)"
3434
GREP="$(type -P grep)"
3535
RM="$(type -P rm)"
3636
SHUF="$(type -P sort) -R"
37-
TEMPFILE="$(pwd)/$$.tmp"
37+
38+
# Store the number of tests run in a file so that the value is available
39+
# from the parent process. This will become an issue if we start considering
40+
# parallel test execution in the future.
41+
TEST_COUNT_FILE="$(mktemp)"
42+
# shellcheck disable=2064 # Use single quotes, expands now, not when signaled.
43+
trap "$RM -f \"$TEST_COUNT_FILE\"" EXIT
3844

3945
fail() {
4046
local message=${1:-}
@@ -246,9 +252,9 @@ run_tests() {
246252
local tests_to_run="$(set | "$GREP" -E '^test.* \(\)' | "$GREP" -E "$test_pattern" | "$SED" -e 's: .*::' | maybe_shuffle)"
247253
fi
248254

249-
local test_count=$(cat "${TEMPFILE}")
255+
local test_count=$(cat "${TEST_COUNT_FILE}")
250256
test_count=$((test_count + $(count "$pending_tests") + $(count "$tests_to_run") + $(count "$skipped_tests")))
251-
echo "${test_count}" > "${TEMPFILE}"
257+
echo "${test_count}" > "${TEST_COUNT_FILE}"
252258

253259
for pending_test in $pending_tests
254260
do
@@ -561,7 +567,7 @@ fi
561567

562568
#run tests received as parameters
563569
failure=0
564-
echo 0 > "${TEMPFILE}"
570+
echo 0 > "${TEST_COUNT_FILE}"
565571
for test_file in "$@"
566572
do
567573
notify_suite_starting "$test_file"
@@ -587,10 +593,9 @@ done
587593

588594
if ((failure))
589595
then
590-
notify_suites_failed "$(cat "${TEMPFILE}")"
596+
notify_suites_failed "$(cat "${TEST_COUNT_FILE}")"
591597
else
592-
notify_suites_succeded "$(cat "${TEMPFILE}")"
598+
notify_suites_succeded "$(cat "${TEST_COUNT_FILE}")"
593599
fi
594600

595-
unlink "$TEMPFILE"
596601
exit $failure

0 commit comments

Comments
 (0)