diff --git a/bash_unit b/bash_unit index 87e177d..8dee7ed 100755 --- a/bash_unit +++ b/bash_unit @@ -218,7 +218,8 @@ run_test_suite() { if run_setup_suite then - run_tests || failure=$? + run_tests + failure=$? else failure=1 fi @@ -273,8 +274,16 @@ run_tests() { ( local status=0 declare -F | "$GREP" ' setup$' >/dev/null && setup - (__bash_unit_current_test__="$test" run_test) || status=$? - declare -F | "$GREP" ' teardown$' >/dev/null && teardown + # make sure teardown runs even if the test fails + local has_teardown=0 + # shellcheck disable=SC2034 # foo appears unused. Verify it or export it. + declare -F | "$GREP" ' teardown$' >/dev/null && has_teardown=1 + trap '((has_teardown)) && teardown' EXIT + + # NOTE: we do *not* want to use the || or && syntax with the subshell + # below because it would cause the set -e in run_test to be ignored + ( __bash_unit_current_test__="$test" run_test ) + status=$? exit $status ) failure=$(( $? || failure)) @@ -284,9 +293,18 @@ run_tests() { } run_test() { - set -e notify_test_starting "$__bash_unit_current_test__" - "$__bash_unit_current_test__" && notify_test_succeeded "$__bash_unit_current_test__" + ( + set -e + "$__bash_unit_current_test__" + ) + local status=$? + if (( status != 0 )); then + # notify_test_failed "$__bash_unit_current_test__" + exit $status + else + notify_test_succeeded "$__bash_unit_current_test__" + fi } run_teardown_suite() { diff --git a/tests/test_cli.sh b/tests/test_cli.sh index fc32fa7..5959348 100644 --- a/tests/test_cli.sh +++ b/tests/test_cli.sh @@ -31,6 +31,27 @@ EOF )" } +test_exit_code_not_0_in_case_of_non_zero_exit_code() { + assert_fails "$BASH_UNIT <($CAT << EOF +function test_fails() { false ; } +EOF +)" +} + +test_exit_code_0_in_case_of_zero_exit_code() { + assert "$BASH_UNIT <($CAT << EOF +function test_succeeds() { true ; } +EOF +)" +} + +test_exit_code_not_0_in_case_of_non_zero_exit_code_followed_by_zero_exit_code() { + assert_fails "$BASH_UNIT <($CAT << EOF +function test_fails() { false ; true ; } +EOF +)" +} + test_run_all_file_parameters() { bash_unit_output=$($BASH_UNIT \ <(echo "test_one() { echo -n ; }") \ diff --git a/tests/test_core.sh b/tests/test_core.sh index cc3bd27..19a8081 100644 --- a/tests/test_core.sh +++ b/tests/test_core.sh @@ -151,7 +151,8 @@ test_assert_status_code_fails() { test_assert_shows_stderr_on_failure() { message="$(with_bash_unit_err \ assert 'echo some error message >&2; echo some ok message; echo another ok message; exit 2' - )" + )" || true + # )" assert_equals "\ some error message" \ @@ -161,7 +162,8 @@ some error message" \ test_assert_shows_stdout_on_failure() { message="$(with_bash_unit_out \ assert 'echo some error message >&2; echo some ok message; echo another ok message; exit 2' - )" + )" || true + # )" assert_equals "\ some ok message