Skip to content

Commit c063e7b

Browse files
authored
Merge pull request #513 from TypedDevs/bug/errors-setup-teardown-still-not-caught-correctly
Errors setup teardown still not caught correctly
2 parents 14baf6f + 2436af9 commit c063e7b

10 files changed

Lines changed: 145 additions & 4 deletions

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
- Fix lifecycle hooks not catching failing commands (exit code errors)
6+
37
## [0.26.0](https://github.com/TypedDevs/bashunit/compare/0.25.0...0.26.0) - 2025-11-02
48

59
- Add `assert_unsuccessful_code` assertion to check for non-zero exit codes

src/runner.sh

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,10 +346,7 @@ 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-
if ! runner::run_set_up "$test_file"; then
350-
status=$?
351-
exit "$status"
352-
fi
349+
runner::run_set_up "$test_file" || exit $?
353350
354351
# 2>&1: Redirects the std-error (FD 2) to the std-output (FD 1).
355352
# points to the original std-output.

tests/acceptance/bashunit_setup_before_script_error_test.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,29 @@ function test_bashunit_when_set_up_before_script_errors() {
3434
assert_contains "$assertions_summary" "$actual"
3535
assert_general_error "$(./bashunit --no-parallel --env "$TEST_ENV_FILE" "$test_file")"
3636
}
37+
38+
function test_bashunit_when_set_up_before_script_with_failing_command() {
39+
local test_file=./tests/acceptance/fixtures/test_bashunit_when_setup_before_script_with_failing_command.sh
40+
local fixture=$test_file
41+
42+
local header_line="Running $fixture"
43+
local error_line="✗ Error: Set up before script"
44+
local message_line=" Hook 'set_up_before_script' failed with exit code 1"
45+
local tests_summary="Tests: 1 failed, 1 total"
46+
local assertions_summary="Assertions: 0 failed, 0 total"
47+
48+
local actual_raw
49+
set +e
50+
actual_raw="$(./bashunit --no-parallel --detailed --env "$TEST_ENV_FILE" "$test_file")"
51+
set -e
52+
53+
local actual
54+
actual="$(printf "%s" "$actual_raw" | strip_ansi)"
55+
56+
assert_contains "$header_line" "$actual"
57+
assert_contains "$error_line" "$actual"
58+
assert_contains "$message_line" "$actual"
59+
assert_contains "$tests_summary" "$actual"
60+
assert_contains "$assertions_summary" "$actual"
61+
assert_general_error "$(./bashunit --no-parallel --env "$TEST_ENV_FILE" "$test_file")"
62+
}

tests/acceptance/bashunit_setup_error_test.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,29 @@ function test_bashunit_when_set_up_errors() {
3434
assert_contains "$assertions_summary" "$actual"
3535
assert_general_error "$(./bashunit --no-parallel --env "$TEST_ENV_FILE" "$test_file")"
3636
}
37+
38+
function test_bashunit_when_set_up_with_failing_command() {
39+
local test_file=./tests/acceptance/fixtures/test_bashunit_when_setup_with_failing_command.sh
40+
local fixture=$test_file
41+
42+
local header_line="Running $fixture"
43+
local error_line="✗ Error: Set up"
44+
local message_line=" Hook 'set_up' failed with exit code 1"
45+
local tests_summary="Tests: 1 failed, 1 total"
46+
local assertions_summary="Assertions: 0 failed, 0 total"
47+
48+
local actual_raw
49+
set +e
50+
actual_raw="$(./bashunit --no-parallel --detailed --env "$TEST_ENV_FILE" "$test_file")"
51+
set -e
52+
53+
local actual
54+
actual="$(printf "%s" "$actual_raw" | strip_ansi)"
55+
56+
assert_contains "$header_line" "$actual"
57+
assert_contains "$error_line" "$actual"
58+
assert_contains "$message_line" "$actual"
59+
assert_contains "$tests_summary" "$actual"
60+
assert_contains "$assertions_summary" "$actual"
61+
assert_general_error "$(./bashunit --no-parallel --env "$TEST_ENV_FILE" "$test_file")"
62+
}

tests/acceptance/bashunit_teardown_after_script_error_test.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,29 @@ function test_bashunit_when_tear_down_after_script_errors() {
3434
assert_contains "$assertions_summary" "$actual"
3535
assert_general_error "$(./bashunit --no-parallel --env "$TEST_ENV_FILE" "$test_file")"
3636
}
37+
38+
function test_bashunit_when_tear_down_after_script_with_failing_command() {
39+
local test_file=./tests/acceptance/fixtures/test_bashunit_when_teardown_after_script_with_failing_command.sh
40+
local fixture=$test_file
41+
42+
local header_line="Running $fixture"
43+
local error_line="✗ Error: Tear down after script"
44+
local message_line=" Hook 'tear_down_after_script' failed with exit code 1"
45+
local tests_summary="Tests: 1 passed, 1 failed, 2 total"
46+
local assertions_summary="Assertions: 1 passed, 0 failed, 1 total"
47+
48+
local actual_raw
49+
set +e
50+
actual_raw="$(./bashunit --no-parallel --detailed --env "$TEST_ENV_FILE" "$test_file")"
51+
set -e
52+
53+
local actual
54+
actual="$(printf "%s" "$actual_raw" | strip_ansi)"
55+
56+
assert_contains "$header_line" "$actual"
57+
assert_contains "$error_line" "$actual"
58+
assert_contains "$message_line" "$actual"
59+
assert_contains "$tests_summary" "$actual"
60+
assert_contains "$assertions_summary" "$actual"
61+
assert_general_error "$(./bashunit --no-parallel --env "$TEST_ENV_FILE" "$test_file")"
62+
}

tests/acceptance/bashunit_teardown_error_test.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,29 @@ function test_bashunit_when_tear_down_errors() {
3434
assert_contains "$assertions_summary" "$actual"
3535
assert_general_error "$(./bashunit --no-parallel --env "$TEST_ENV_FILE" "$test_file")"
3636
}
37+
38+
function test_bashunit_when_tear_down_with_failing_command() {
39+
local test_file=./tests/acceptance/fixtures/test_bashunit_when_teardown_with_failing_command.sh
40+
local fixture=$test_file
41+
42+
local header_line="Running $fixture"
43+
local error_line="✗ Error: Tear down"
44+
local message_line=" Hook 'tear_down' failed with exit code 1"
45+
local tests_summary="Tests: 0 passed, 1 failed, 1 total"
46+
local assertions_summary="Assertions: 1 passed, 0 failed, 1 total"
47+
48+
local actual_raw
49+
set +e
50+
actual_raw="$(./bashunit --no-parallel --detailed --env "$TEST_ENV_FILE" "$test_file")"
51+
set -e
52+
53+
local actual
54+
actual="$(printf "%s" "$actual_raw" | strip_ansi)"
55+
56+
assert_contains "$header_line" "$actual"
57+
assert_contains "$error_line" "$actual"
58+
assert_contains "$message_line" "$actual"
59+
assert_contains "$tests_summary" "$actual"
60+
assert_contains "$assertions_summary" "$actual"
61+
assert_general_error "$(./bashunit --no-parallel --env "$TEST_ENV_FILE" "$test_file")"
62+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
3+
function set_up_before_script() {
4+
false
5+
}
6+
7+
function test_dummy() {
8+
assert_same "foo" "foo"
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
3+
function set_up() {
4+
false
5+
}
6+
7+
function test_dummy() {
8+
assert_same "foo" "foo"
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
3+
function tear_down_after_script() {
4+
false
5+
}
6+
7+
function test_dummy() {
8+
assert_same "foo" "foo"
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
3+
function tear_down() {
4+
false
5+
}
6+
7+
function test_dummy() {
8+
assert_same "foo" "foo"
9+
}

0 commit comments

Comments
 (0)