Skip to content

Commit 5f8a05d

Browse files
committed
fix: correct set -e behavior
1 parent d22dc6a commit 5f8a05d

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
@@ -218,7 +218,8 @@ run_test_suite() {
218218

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

286295
run_test() {
287-
set -e
288296
notify_test_starting "$__bash_unit_current_test__"
289-
"$__bash_unit_current_test__" && notify_test_succeeded "$__bash_unit_current_test__"
297+
(
298+
set -e
299+
"$__bash_unit_current_test__"
300+
)
301+
local status=$?
302+
if (( $status != 0 )); then
303+
# notify_test_failed "$__bash_unit_current_test__"
304+
exit $status
305+
else
306+
notify_test_succeeded "$__bash_unit_current_test__"
307+
fi
290308
}
291309

292310
run_teardown_suite() {

0 commit comments

Comments
 (0)