Skip to content

Commit ed9e35e

Browse files
committed
fix: count all tests as failed when set_up_before_script fails
1 parent 8c69039 commit ed9e35e

File tree

5 files changed

+66
-0
lines changed

5 files changed

+66
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Fixed
66
- Stop executing remaining commands in `set_up`/`tear_down` after first failure
7+
- Count all tests as failed when `set_up_before_script` fails
78

89
## [0.27.0](https://github.com/TypedDevs/bashunit/compare/0.26.0...0.27.0) - 2025-11-26
910

src/learn.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env bash
2+
# shellcheck disable=SC2016
23

34
##
45
# Interactive learning module for bashunit

src/runner.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ function runner::load_test_files() {
2727
# Update function cache after sourcing new test file
2828
CACHED_ALL_FUNCTIONS=$(declare -F | awk '{print $3}')
2929
if ! runner::run_set_up_before_script "$test_file"; then
30+
# Count the test functions that couldn't run due to set_up_before_script failure
31+
# and add them as failed (minus 1 since the hook failure already counts as 1)
32+
local filtered_functions
33+
filtered_functions=$(helper::get_functions_to_run "test" "$filter" "$CACHED_ALL_FUNCTIONS")
34+
if [[ -n "$filtered_functions" ]]; then
35+
# shellcheck disable=SC2206
36+
local functions_to_run=($filtered_functions)
37+
local additional_failures=$((${#functions_to_run[@]} - 1))
38+
for ((i = 0; i < additional_failures; i++)); do
39+
state::add_tests_failed
40+
done
41+
fi
3042
runner::clean_set_up_and_tear_down_after_script
3143
if ! parallel::is_enabled; then
3244
cleanup_script_temp_files
@@ -75,6 +87,18 @@ function runner::load_bench_files() {
7587
# Update function cache after sourcing new bench file
7688
CACHED_ALL_FUNCTIONS=$(declare -F | awk '{print $3}')
7789
if ! runner::run_set_up_before_script "$bench_file"; then
90+
# Count the bench functions that couldn't run due to set_up_before_script failure
91+
# and add them as failed (minus 1 since the hook failure already counts as 1)
92+
local filtered_functions
93+
filtered_functions=$(helper::get_functions_to_run "bench" "$filter" "$CACHED_ALL_FUNCTIONS")
94+
if [[ -n "$filtered_functions" ]]; then
95+
# shellcheck disable=SC2206
96+
local functions_to_run=($filtered_functions)
97+
local additional_failures=$((${#functions_to_run[@]} - 1))
98+
for ((i = 0; i < additional_failures; i++)); do
99+
state::add_tests_failed
100+
done
101+
fi
78102
runner::clean_set_up_and_tear_down_after_script
79103
cleanup_script_temp_files
80104
continue

tests/acceptance/bashunit_setup_before_script_error_test.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,30 @@ function test_bashunit_when_set_up_before_script_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_before_script_fails_with_multiple_tests() {
65+
local test_file=./tests/acceptance/fixtures/test_bashunit_when_setup_before_script_fails_with_multiple_tests.sh
66+
local fixture=$test_file
67+
68+
local header_line="Running $fixture"
69+
local error_line="✗ Error: Set up before script"
70+
local message_line=" Hook 'set_up_before_script' failed with exit code 1"
71+
# When set_up_before_script fails, all test functions should be counted as failed
72+
local tests_summary="Tests: 2 failed, 2 total"
73+
local assertions_summary="Assertions: 0 failed, 0 total"
74+
75+
local actual_raw
76+
set +e
77+
actual_raw="$(./bashunit --no-parallel --detailed --env "$TEST_ENV_FILE" "$test_file")"
78+
set -e
79+
80+
local actual
81+
actual="$(printf "%s" "$actual_raw" | strip_ansi)"
82+
83+
assert_contains "$header_line" "$actual"
84+
assert_contains "$error_line" "$actual"
85+
assert_contains "$message_line" "$actual"
86+
assert_contains "$tests_summary" "$actual"
87+
assert_contains "$assertions_summary" "$actual"
88+
assert_general_error "$(./bashunit --no-parallel --env "$TEST_ENV_FILE" "$test_file")"
89+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env bash
2+
3+
function set_up_before_script() {
4+
false
5+
}
6+
7+
function test_asserting_foo_strings() {
8+
assert_same "foo" "foo"
9+
}
10+
11+
function test_asserting_bar_strings() {
12+
assert_same "bar" "bar"
13+
}

0 commit comments

Comments
 (0)