Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions bash_unit
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@ run_test_suite() {

if run_setup_suite
then
run_tests || failure=$?
run_tests
failure=$?
else
failure=1
fi
Expand Down Expand Up @@ -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))
Expand All @@ -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() {
Expand Down
21 changes: 21 additions & 0 deletions tests/test_cli.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 ; }") \
Expand Down
6 changes: 4 additions & 2 deletions tests/test_core.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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" \
Expand All @@ -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
Expand Down
Loading