Skip to content

Commit ca9233a

Browse files
lczykpgrange
authored andcommitted
fix: correct set -e behavior
1 parent 008e016 commit ca9233a

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

bash_unit

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,8 @@ run_test_suite() {
219219

220220
if run_setup_suite
221221
then
222-
run_tests || failure=$?
222+
run_tests
223+
failure=$?
223224
else
224225
failure=1
225226
fi
@@ -274,8 +275,16 @@ run_tests() {
274275
(
275276
local status=0
276277
declare -F | "$GREP" ' setup$' >/dev/null && setup
277-
(__bash_unit_current_test__="$test" run_test) || status=$?
278-
declare -F | "$GREP" ' teardown$' >/dev/null && teardown
278+
# make sure teardown runs even if the test fails
279+
local has_teardown=0
280+
# shellcheck disable=SC2034 # foo appears unused. Verify it or export it.
281+
declare -F | "$GREP" ' teardown$' >/dev/null && has_teardown=1
282+
trap '((has_teardown)) && teardown' EXIT
283+
284+
# NOTE: we do *not* want to use the || or && syntax with the subshell
285+
# below because it would cause the set -e in run_test to be ignored
286+
( __bash_unit_current_test__="$test" run_test )
287+
status=$?
279288
exit $status
280289
)
281290
failure=$(( $? || failure))
@@ -285,9 +294,18 @@ run_tests() {
285294
}
286295

287296
run_test() {
288-
set -e
289297
notify_test_starting "$__bash_unit_current_test__"
290-
"$__bash_unit_current_test__" && notify_test_succeeded "$__bash_unit_current_test__"
298+
(
299+
set -e
300+
"$__bash_unit_current_test__"
301+
)
302+
local status=$?
303+
if (( $status != 0 )); then
304+
# notify_test_failed "$__bash_unit_current_test__"
305+
exit $status
306+
else
307+
notify_test_succeeded "$__bash_unit_current_test__"
308+
fi
291309
}
292310

293311
run_teardown_suite() {

0 commit comments

Comments
 (0)