Skip to content

Commit f19cab8

Browse files
Aki Arviopgrange
authored andcommitted
Ensure test failure printed if test fails
- When a test failed due to a non-zero exit code, the FAILURE status was not printed after the test name. Looked like this: "Running test_name ... Overall result: FAILURE" instead of the expected: "Running test_name ... FAILURE Overall result: FAILURE" - Makes the failure reporting consistent and finding failure easier
1 parent 844a6cd commit f19cab8

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

bash_unit

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ fail() {
5555
[[ -n "$stderr" ]] && [ -s "$stderr" ] && notify_stderr < "$stderr"
5656

5757
stacktrace | notify_stack
58-
exit 1
58+
# Exit with special code 99 to signal that notify_test_failed was already called
59+
exit 99
5960
}
6061

6162
assert() {
@@ -303,8 +304,12 @@ run_test() {
303304
)
304305
local status=$?
305306
if (( status != 0 )); then
306-
# notify_test_failed "$__bash_unit_current_test__"
307-
exit $status
307+
# Check if fail() was called (exit code 99 means notification already done)
308+
if [[ ${status} -ne 99 ]]; then
309+
notify_test_failed "$__bash_unit_current_test__"
310+
fi
311+
# Always exit with 1 for failure
312+
exit 1
308313
else
309314
notify_test_succeeded "$__bash_unit_current_test__"
310315
fi

tests/test_cli.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,11 @@ test_one_test_should_stop_when_assert_fails() {
204204
"$($BASH_UNIT <(echo 'test_fail() { echo "before failure" >&2 ; assert false ; echo "after failure" >&2 ; }') 2>&1 >/dev/null)"
205205
}
206206

207+
test_failure_status_printed_after_test_name() {
208+
assert_matches "Running test_fail_should_print_failure ... FAILURE"$'\n'"Overall result: FAILURE" \
209+
"$($BASH_UNIT <(echo 'test_fail_should_print_failure() { false; }'))"
210+
}
211+
207212
setup() {
208213
# fake basic unix commands bash_unit relies on so that
209214
# we ensure bash_unit keeps working when people fake

0 commit comments

Comments
 (0)