-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathmain.sh
More file actions
206 lines (168 loc) · 5.72 KB
/
main.sh
File metadata and controls
206 lines (168 loc) · 5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env bash
function main::exec_tests() {
local filter=$1
local files=("${@:2}")
local test_files
test_files=()
while IFS= read -r line; do
test_files+=("$line")
done < <(helper::load_test_files "$filter" "${files[@]}")
internal_log "exec_tests" "filter:$filter" "files:${test_files[*]}"
if [[ ${#test_files[@]} -eq 0 || -z "${test_files[0]}" ]]; then
printf "%sError: At least one file path is required.%s\n" "${_COLOR_FAILED}" "${_COLOR_DEFAULT}"
console_header::print_help
exit 1
fi
# Trap SIGINT (Ctrl-C) and call the cleanup function
trap 'main::cleanup' SIGINT
trap '[[ $? -eq $EXIT_CODE_STOP_ON_FAILURE ]] && main::handle_stop_on_failure_sync' EXIT
if env::is_parallel_run_enabled && ! parallel::is_enabled; then
printf "%sWarning: Parallel tests are supported on macOS, Ubuntu and Windows.\n" "${_COLOR_INCOMPLETE}"
printf "For other OS (like Alpine), --parallel is not enabled due to inconsistent results,\n"
printf "particularly involving race conditions.%s " "${_COLOR_DEFAULT}"
printf "%sFallback using --no-parallel%s\n" "${_COLOR_SKIPPED}" "${_COLOR_DEFAULT}"
fi
if parallel::is_enabled; then
parallel::init
fi
console_header::print_version_with_env "$filter" "${test_files[@]}"
if env::is_verbose_enabled; then
if env::is_simple_output_enabled; then
echo ""
fi
printf '%*s\n' "$TERMINAL_WIDTH" '' | tr ' ' '#'
printf "%s\n" "Filter: ${filter:-None}"
printf "%s\n" "Total files: ${#test_files[@]}"
printf "%s\n" "Test files:"
printf -- "- %s\n" "${test_files[@]}"
printf '%*s\n' "$TERMINAL_WIDTH" '' | tr ' ' '.'
env::print_verbose
printf '%*s\n' "$TERMINAL_WIDTH" '' | tr ' ' '#'
fi
runner::load_test_files "$filter" "${test_files[@]}"
if parallel::is_enabled; then
wait
fi
if parallel::is_enabled && parallel::must_stop_on_failure; then
printf "\r%sStop on failure enabled...%s\n" "${_COLOR_SKIPPED}" "${_COLOR_DEFAULT}"
fi
console_results::print_failing_tests_and_reset
console_results::render_result
exit_code=$?
if [[ -n "$BASHUNIT_LOG_JUNIT" ]]; then
reports::generate_junit_xml "$BASHUNIT_LOG_JUNIT"
fi
if [[ -n "$BASHUNIT_REPORT_HTML" ]]; then
reports::generate_report_html "$BASHUNIT_REPORT_HTML"
fi
if parallel::is_enabled; then
parallel::cleanup
fi
internal_log "Finished tests" "exit_code:$exit_code"
exit $exit_code
}
function main::exec_benchmarks() {
local filter=$1
local files=("${@:2}")
local bench_files
bench_files=()
while IFS= read -r line; do
bench_files+=("$line")
done < <(helper::load_bench_files "$filter" "${files[@]}")
internal_log "exec_benchmarks" "filter:$filter" "files:${bench_files[*]}"
if [[ ${#bench_files[@]} -eq 0 || -z "${bench_files[0]}" ]]; then
printf "%sError: At least one file path is required.%s\n" "${_COLOR_FAILED}" "${_COLOR_DEFAULT}"
console_header::print_help
exit 1
fi
console_header::print_version_with_env "$filter" "${bench_files[@]}"
runner::load_bench_files "$filter" "${bench_files[@]}"
benchmark::print_results
internal_log "Finished benchmarks"
}
function main::cleanup() {
printf "%sCaught Ctrl-C, killing all child processes...%s\n" "${_COLOR_SKIPPED}" "${_COLOR_DEFAULT}"
# Kill all child processes of this script
pkill -P $$
cleanup_script_temp_files
if parallel::is_enabled; then
parallel::cleanup
fi
exit 1
}
function main::handle_stop_on_failure_sync() {
printf "\n%sStop on failure enabled...%s\n" "${_COLOR_SKIPPED}" "${_COLOR_DEFAULT}"
console_results::print_failing_tests_and_reset
console_results::render_result
cleanup_script_temp_files
if parallel::is_enabled; then
parallel::cleanup
fi
exit 1
}
function main::exec_assert() {
local original_assert_fn=$1
local args=("${@:2}")
local assert_fn=$original_assert_fn
# Check if the function exists
if ! type "$assert_fn" > /dev/null 2>&1; then
assert_fn="assert_$assert_fn"
if ! type "$assert_fn" > /dev/null 2>&1; then
echo "Function $original_assert_fn does not exist." 1>&2
exit 127
fi
fi
# Get the last argument safely by calculating the array length
local last_index=$((${#args[@]} - 1))
local last_arg="${args[$last_index]}"
local output=""
local inner_exit_code=0
local bashunit_exit_code=0
# Handle different assert_* functions
case "$assert_fn" in
assert_exit_code)
output=$(main::handle_assert_exit_code "$last_arg")
inner_exit_code=$?
# Remove the last argument and append the exit code
args=("${args[@]:0:last_index}")
args+=("$inner_exit_code")
;;
*)
# Add more cases here for other assert_* handlers if needed
;;
esac
if [[ -n "$output" ]]; then
echo "$output" 1>&1
assert_fn="assert_same"
fi
# Run the assertion function and write into stderr
"$assert_fn" "${args[@]}" 1>&2
bashunit_exit_code=$?
if [[ "$(state::get_tests_failed)" -gt 0 ]] || [[ "$(state::get_assertions_failed)" -gt 0 ]]; then
return 1
fi
return "$bashunit_exit_code"
}
function main::handle_assert_exit_code() {
local cmd="$1"
local output
local inner_exit_code=0
if [[ $(command -v "${cmd%% *}") ]]; then
output=$(eval "$cmd" 2>&1 || echo "inner_exit_code:$?")
local last_line
last_line=$(echo "$output" | tail -n 1)
if echo "$last_line" | grep -q 'inner_exit_code:[0-9]*'; then
inner_exit_code=$(echo "$last_line" | grep -o 'inner_exit_code:[0-9]*' | cut -d':' -f2)
local number_pattern='^[0-9]+$'
if ! [[ $inner_exit_code =~ $number_pattern ]]; then
inner_exit_code=1
fi
output=$(echo "$output" | sed '$d')
fi
echo "$output"
return "$inner_exit_code"
else
echo "Command not found: $cmd" 1>&2
return 127
fi
}