Skip to content

Commit b17ca0e

Browse files
authored
Merge pull request #515 from TypedDevs/fix/512-edge-case-errors-in-tear-down-and-set_up
Edge case errors in tear down and set Up
2 parents 2bf6f72 + 1e2184e commit b17ca0e

4 files changed

Lines changed: 62 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88

99
### Fixed
1010
- Pass arguments to mocked functions
11-
- Fix lifecycle hooks not catching failing commands (exit code errors)
11+
- Fix lifecycle hooks not catching intermediate failing commands in `set_up` and `tear_down`
12+
13+
### Changed
14+
- Simplify CI: use only `-latest` runners for Ubuntu and macOS, remove deprecated `macos-13`
1215

1316
## [0.26.0](https://github.com/TypedDevs/bashunit/compare/0.25.0...0.26.0) - 2025-11-02
1417

src/runner.sh

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,14 @@ function runner::run_test() {
346346
# shellcheck disable=SC2064
347347
trap 'exit_code=$?; runner::cleanup_on_exit "$test_file" "$exit_code"' EXIT
348348
state::initialize_assertions_count
349-
runner::run_set_up "$test_file" || exit $?
349+
350+
# Run set_up and capture exit code without || to preserve errexit behavior
351+
local setup_exit_code=0
352+
runner::run_set_up "$test_file"
353+
setup_exit_code=$?
354+
if [[ $setup_exit_code -ne 0 ]]; then
355+
exit $setup_exit_code
356+
fi
350357
351358
# 2>&1: Redirects the std-error (FD 2) to the std-output (FD 1).
352359
# points to the original std-output.
@@ -740,9 +747,22 @@ function runner::execute_test_hook() {
740747
local hook_output_file
741748
hook_output_file=$(temp_file "${hook_name}_output")
742749

750+
# Enable errexit and errtrace to catch any failing command in the hook
751+
set -eE
752+
# Set up trap to catch errors and record the exit status
753+
trap 'status=$?; set +eE; trap - ERR' ERR
754+
743755
{
744756
"$hook_name"
745-
} >"$hook_output_file" 2>&1 || status=$?
757+
} >"$hook_output_file" 2>&1
758+
759+
# Capture final status and clean up
760+
local block_exit=$?
761+
if [[ $status -eq 0 ]]; then
762+
status=$block_exit
763+
fi
764+
trap - ERR
765+
set +eE
746766

747767
if [[ -f "$hook_output_file" ]]; then
748768
hook_output=""

tests/acceptance/bashunit_setup_error_test.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,29 @@ function test_bashunit_when_set_up_with_failing_command() {
6060
assert_contains "$assertions_summary" "$actual"
6161
assert_general_error "$(./bashunit --no-parallel --env "$TEST_ENV_FILE" "$test_file")"
6262
}
63+
64+
function test_bashunit_when_set_up_with_intermediate_failing_command() {
65+
local test_file=./tests/acceptance/fixtures/test_bashunit_when_setup_with_intermediate_failing_command.sh
66+
local fixture=$test_file
67+
68+
local header_line="Running $fixture"
69+
local error_line="✗ Error: Set up"
70+
local message_line=" Hook 'set_up' failed with exit code 1"
71+
local tests_summary="Tests: 1 failed, 1 total"
72+
local assertions_summary="Assertions: 0 failed, 0 total"
73+
74+
local actual_raw
75+
set +e
76+
actual_raw="$(./bashunit --no-parallel --detailed --env "$TEST_ENV_FILE" "$test_file")"
77+
set -e
78+
79+
local actual
80+
actual="$(printf "%s" "$actual_raw" | strip_ansi)"
81+
82+
assert_contains "$header_line" "$actual"
83+
assert_contains "$error_line" "$actual"
84+
assert_contains "$message_line" "$actual"
85+
assert_contains "$tests_summary" "$actual"
86+
assert_contains "$assertions_summary" "$actual"
87+
assert_general_error "$(./bashunit --no-parallel --env "$TEST_ENV_FILE" "$test_file")"
88+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env bash
2+
3+
function set_up() {
4+
false
5+
true
6+
}
7+
8+
function test_dummy() {
9+
assert_same "foo" "foo"
10+
}

0 commit comments

Comments
 (0)