Skip to content

Commit fb86e90

Browse files
committed
Run test setup and teardown the same way test cases are run
1 parent 75cfaff commit fb86e90

1 file changed

Lines changed: 54 additions & 53 deletions

File tree

src/testrunner.sh

Lines changed: 54 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,54 @@ function testrunner::absolute_path() {
4848

4949
declare test_output_dir=$(mktemp -d)
5050

51+
function testrunner::run_test() {
52+
local testcase=$1
53+
54+
local pretty_testcase=${testcase#test_}
55+
local pretty_testcase=${pretty_testcase//[_]/ }
56+
printf -- "- ${pretty_testcase} "
57+
58+
test_failure=""
59+
trap 'testrunner::register_failure "$BASH_COMMAND" $? && return' ERR INT
60+
61+
# Work around older bash versions not getting location correct on error
62+
set -o functrace
63+
local -a actual_lineno
64+
local -a actual_source
65+
trap 'actual_lineno+=($LINENO); actual_source+=(${BASH_SOURCE[0]})' DEBUG
66+
67+
${testcase} >>$test_output_file 2>&1
68+
trap - ERR INT DEBUG
69+
70+
if [[ -z "$test_failure" ]]; then
71+
printf "${kGreen}${kReset}\n"
72+
73+
if [[ $DEBUG -eq 1 ]]; then
74+
testrunner::print_test_output
75+
fi
76+
return 0
77+
else
78+
tests_failed+=1
79+
printf "${kRed}${kReset}\n"
80+
81+
IFS='|' read -r filename line_number expression \
82+
evaluated_expression exit_code <<< "$test_failure"
83+
84+
testrunner::print_location $filename $line_number
85+
86+
printf "Expression:\n\n"
87+
printf " ${kBold}${expression}${kReset}"
88+
if [[ $evaluated_expression != $expression ]]; then
89+
printf " (${evaluated_expression})"
90+
fi
91+
printf "\n\nFailed with exit code ${kBold}${exit_code}${kReset}\n"
92+
93+
testrunner::print_test_output
94+
95+
return 1
96+
fi
97+
}
98+
5199
function testrunner::run_tests() {
52100
local pretty_testsuite=$(basename $testsuite)
53101
local test_output_file="${test_output_dir}/${pretty_testsuite}.log"
@@ -73,72 +121,25 @@ function testrunner::run_tests() {
73121
return;
74122
fi
75123

76-
printf "${kUnderline}Running ${#testcases[@]} tests from ${pretty_testsuite}...${kReset}\n"
124+
printf "${kUnderline}Running ${#testcases[@]} tests from ${pretty_testsuite}...${kReset}\n\n"
77125

78126
if testrunner::function_declared setup; then
79-
setup >>$test_output_file 2>&1
80-
fi
81-
82-
if [[ $DEBUG -eq 0 ]] || ! testrunner::print_test_output "Setup"; then
83-
printf "\n"
127+
testrunner::run_test setup
128+
test $? -eq 0 || return
84129
fi
85130

86131
local test_failure
87132
for testcase in "${testcases[@]}" ; do
88133
tests_total+=1
89134

90-
local pretty_testcase=${testcase#test_}
91-
local pretty_testcase=${pretty_testcase//[_]/ }
92-
printf -- "- ${pretty_testcase} "
93-
94-
test_failure=""
95-
trap 'testrunner::register_failure "$BASH_COMMAND" $?' ERR INT
96-
97-
# Work around older bash versions not getting location correct on error
98-
set -o functrace
99-
local -a actual_lineno
100-
local -a actual_source
101-
trap 'actual_lineno+=($LINENO); actual_source+=(${BASH_SOURCE[0]})' DEBUG
102-
103-
${testcase} >>$test_output_file 2>&1
104-
trap - ERR INT DEBUG
105-
106-
if [[ -z "$test_failure" ]]; then
107-
printf "${kGreen}${kReset}\n"
108-
109-
if [[ $DEBUG -eq 1 ]]; then
110-
testrunner::print_test_output
111-
fi
112-
else
113-
tests_failed+=1
114-
printf "${kRed}${kReset}\n"
115-
116-
IFS='|' read -r filename line_number expression \
117-
evaluated_expression exit_code <<< "$test_failure"
118-
119-
testrunner::print_location $filename $line_number
120-
121-
printf "Expression:\n\n"
122-
printf " ${kBold}${expression}${kReset}"
123-
if [[ $evaluated_expression != $expression ]]; then
124-
printf " (${evaluated_expression})"
125-
fi
126-
printf "\n\nFailed with exit code ${kBold}${exit_code}${kReset}\n"
127-
128-
testrunner::print_test_output
129-
130-
if [[ ${exit_code} -eq 130 ]]; then
131-
break; # Interrupted
132-
fi
133-
fi
135+
testrunner::run_test $testcase
136+
test $? -eq 0 || break
134137
done
135138

136139
if testrunner::function_declared teardown; then
137-
teardown >>$test_output_file
140+
testrunner::run_test teardown
138141
fi
139142

140-
[[ $DEBUG -eq 1 ]] && testrunner::print_test_output "Teardown"
141-
142143
if [[ -z "$test_failure" ]]; then
143144
printf "\n" # Blank line in case the last test passed
144145
fi

0 commit comments

Comments
 (0)