From 6e6b41099d8b07537a660aaea9b6dbdebb1c314b Mon Sep 17 00:00:00 2001 From: marcin Date: Sat, 13 Sep 2025 18:52:24 +0100 Subject: [PATCH 1/5] feat: tests hardening --- tests/test_core.sh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) 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 From eef9aeaa5026a875245d558a922d64f38d0376bf Mon Sep 17 00:00:00 2001 From: marcin Date: Sat, 13 Sep 2025 18:54:25 +0100 Subject: [PATCH 2/5] feat: additional tests --- tests/test_cli.sh | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/test_cli.sh b/tests/test_cli.sh index fc32fa7..65806fe 100644 --- a/tests/test_cli.sh +++ b/tests/test_cli.sh @@ -31,6 +31,20 @@ 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_run_all_file_parameters() { bash_unit_output=$($BASH_UNIT \ <(echo "test_one() { echo -n ; }") \ From d22dc6a5dff8c3373b7a98903f888dec69161b18 Mon Sep 17 00:00:00 2001 From: marcin Date: Sat, 13 Sep 2025 18:56:22 +0100 Subject: [PATCH 3/5] feat!: failing test case --- tests/test_cli.sh | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/test_cli.sh b/tests/test_cli.sh index 65806fe..5959348 100644 --- a/tests/test_cli.sh +++ b/tests/test_cli.sh @@ -45,6 +45,13 @@ 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 ; }") \ From 5f8a05db3835079028dd832770945ec600a889fb Mon Sep 17 00:00:00 2001 From: marcin Date: Sat, 13 Sep 2025 18:58:17 +0100 Subject: [PATCH 4/5] fix: correct set -e behavior --- bash_unit | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/bash_unit b/bash_unit index 87e177d..2397fc9 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() { From 314be87f0ce86ab4ed1f372099bf9eb3bebe6e39 Mon Sep 17 00:00:00 2001 From: marcin Date: Fri, 19 Sep 2025 09:41:43 +0100 Subject: [PATCH 5/5] chore: appease shellcheck --- bash_unit | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bash_unit b/bash_unit index 2397fc9..8dee7ed 100755 --- a/bash_unit +++ b/bash_unit @@ -299,7 +299,7 @@ run_test() { "$__bash_unit_current_test__" ) local status=$? - if (( $status != 0 )); then + if (( status != 0 )); then # notify_test_failed "$__bash_unit_current_test__" exit $status else