Skip to content

Commit 65ba82c

Browse files
authored
Merge pull request #73 from pgrange/teardown_anyway
fixes #43
2 parents ead8c07 + 4cb2fe5 commit 65ba82c

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

README.adoc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,9 @@ You may write a *teardown_suite* function that will be executed only once after
170170

171171
If you write code outside of any bash function, this code will be executed once at test file loading time since
172172
your file is a bash script and *bash_unit* sources it before running your tests. It is suggested to write a
173-
*setup_suite* function and avoid any code outside a bash function.
173+
*setup_suite* function and avoid any code outside a bash function. you must not use any bash_unit assertion
174+
in setup_suite or use exit in setup_suite for teardown_suite to be run.
175+
See https://github.com/pgrange/bash_unit/issues/43[issue 43] for more details.
174176

175177
If you want to keep an eye on a test not yet implemented, prefix the name of the function by *todo* instead of test.
176178
Test to do are not executed and do not impact the global status of your test suite but are displayed in *bash_unit* output.

bash_unit

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,26 @@ stacktrace() {
144144
run_test_suite() {
145145
local failure=0
146146

147-
declare -F | "$GREP" ' setup_suite$' >/dev/null && setup_suite
147+
if run_setup_suite
148+
then
149+
run_tests || failure=$?
150+
else
151+
failure=$?
152+
fi
153+
run_teardown_suite
154+
155+
return $failure
156+
}
157+
158+
run_setup_suite() {
159+
if declare -F | "$GREP" ' setup_suite$' >/dev/null
160+
then
161+
setup_suite
162+
fi
163+
}
164+
165+
run_tests() {
166+
local failure=0
148167

149168
for pending_test in $(set | "$GREP" -E '^(pending|todo).* \(\)' | "$GREP" -E "$test_pattern" | "$SED" -e 's: .*::')
150169
do
@@ -164,9 +183,6 @@ run_test_suite() {
164183
)
165184
failure=$(( $? || failure))
166185
done
167-
168-
declare -F | "$GREP" ' teardown_suite$' >/dev/null && teardown_suite
169-
170186
return $failure
171187
}
172188

@@ -176,6 +192,13 @@ run_test() {
176192
"$__bash_unit_current_test__" && notify_test_succeeded "$__bash_unit_current_test__"
177193
}
178194

195+
run_teardown_suite() {
196+
if declare -F | "$GREP" ' teardown_suite$' >/dev/null
197+
then
198+
teardown_suite
199+
fi
200+
}
201+
179202
usage() {
180203
echo "$1" >&2
181204
echo "$0 [-f <output format>] [-p <pattern1>] [-p <pattern2>] ... <test_file1> <test_file2> ..." >&2
@@ -364,7 +387,7 @@ for test_file in "$@"
364387
do
365388
notify_suite_starting "$test_file"
366389
(
367-
set -e # Ensure bash_unit with exit with failure
390+
set -e # Ensure bash_unit will exit with failure
368391
# in case of syntax error.
369392
if [[ "${STICK_TO_CWD}" != true ]]
370393
then

tests/test_cli.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,17 @@ test_bash_unit_runs_teardown_even_in_case_of_failure() {
103103
"$($BASH_UNIT <(echo 'test_fail() { fail ; } ; teardown() { echo "ran teardown" >&2 ; }') 2>&1 >/dev/null)"
104104
}
105105

106+
test_bash_unit_runs_teardown_suite_even_in_case_of_failure() {
107+
assert_equals "ran teardown_suite" \
108+
"$($BASH_UNIT <(echo 'test_fail() { fail ; } ; teardown_suite() { echo "ran teardown_suite" >&2 ; }') 2>&1 >/dev/null)"
109+
}
110+
111+
test_bash_unit_runs_teardown_suite_even_in_case_of_failure_setup_suite() {
112+
#FIX https://github.com/pgrange/bash_unit/issues/43
113+
assert_equals "ran teardown_suite" \
114+
"$($BASH_UNIT <(echo 'setup_suite() { return 1 ; } ; teardown_suite() { echo "ran teardown_suite" >&2 ; }') 2>&1 >/dev/null)"
115+
}
116+
106117
test_one_test_should_stop_after_first_assertion_failure() {
107118
#FIX https://github.com/pgrange/bash_unit/issues/10
108119
assert_equals "before failure" \

0 commit comments

Comments
 (0)