-
-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathconsole_results.sh
More file actions
449 lines (367 loc) · 15.1 KB
/
console_results.sh
File metadata and controls
449 lines (367 loc) · 15.1 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#!/usr/bin/env bash
# shellcheck disable=SC2155
_BASHUNIT_TOTAL_TESTS_COUNT=0
function bashunit::console_results::render_result() {
if [[ "$(bashunit::state::is_duplicated_test_functions_found)" == true ]]; then
bashunit::console_results::print_execution_time
printf "%s%s%s\n" "${_BASHUNIT_COLOR_RETURN_ERROR}" "Duplicate test functions found" "${_BASHUNIT_COLOR_DEFAULT}"
printf "File with duplicate functions: %s\n" "$(bashunit::state::get_file_with_duplicated_function_names)"
printf "Duplicate functions: %s\n" "$(bashunit::state::get_duplicated_function_names)"
return 1
fi
if bashunit::env::is_tap_output_enabled; then
printf "1..%d\n" "$_BASHUNIT_TOTAL_TESTS_COUNT"
if [[ $_BASHUNIT_TESTS_FAILED -gt 0 ]]; then
return 1
fi
return 0
fi
if bashunit::env::is_simple_output_enabled; then
printf "\n\n"
fi
# Cache state values to avoid repeated subshell invocations
local tests_passed=$_BASHUNIT_TESTS_PASSED
local tests_skipped=$_BASHUNIT_TESTS_SKIPPED
local tests_incomplete=$_BASHUNIT_TESTS_INCOMPLETE
local tests_snapshot=$_BASHUNIT_TESTS_SNAPSHOT
local tests_failed=$_BASHUNIT_TESTS_FAILED
local assertions_passed=$_BASHUNIT_ASSERTIONS_PASSED
local assertions_skipped=$_BASHUNIT_ASSERTIONS_SKIPPED
local assertions_incomplete=$_BASHUNIT_ASSERTIONS_INCOMPLETE
local assertions_snapshot=$_BASHUNIT_ASSERTIONS_SNAPSHOT
local assertions_failed=$_BASHUNIT_ASSERTIONS_FAILED
local total_tests=0
total_tests=$((total_tests + tests_passed))
total_tests=$((total_tests + tests_skipped))
total_tests=$((total_tests + tests_incomplete))
total_tests=$((total_tests + tests_snapshot))
total_tests=$((total_tests + tests_failed))
local total_assertions=0
total_assertions=$((total_assertions + assertions_passed))
total_assertions=$((total_assertions + assertions_skipped))
total_assertions=$((total_assertions + assertions_incomplete))
total_assertions=$((total_assertions + assertions_snapshot))
total_assertions=$((total_assertions + assertions_failed))
printf "%sTests: %s" "$_BASHUNIT_COLOR_FAINT" "$_BASHUNIT_COLOR_DEFAULT"
if [[ "$tests_passed" -gt 0 ]] || [[ "$assertions_passed" -gt 0 ]]; then
printf " %s%s passed%s," "$_BASHUNIT_COLOR_PASSED" "$tests_passed" "$_BASHUNIT_COLOR_DEFAULT"
fi
if [[ "$tests_skipped" -gt 0 ]] || [[ "$assertions_skipped" -gt 0 ]]; then
printf " %s%s skipped%s," "$_BASHUNIT_COLOR_SKIPPED" "$tests_skipped" "$_BASHUNIT_COLOR_DEFAULT"
fi
if [[ "$tests_incomplete" -gt 0 ]] || [[ "$assertions_incomplete" -gt 0 ]]; then
printf " %s%s incomplete%s," "$_BASHUNIT_COLOR_INCOMPLETE" "$tests_incomplete" "$_BASHUNIT_COLOR_DEFAULT"
fi
if [[ "$tests_snapshot" -gt 0 ]] || [[ "$assertions_snapshot" -gt 0 ]]; then
printf " %s%s snapshot%s," "$_BASHUNIT_COLOR_SNAPSHOT" "$tests_snapshot" "$_BASHUNIT_COLOR_DEFAULT"
fi
if [[ "$tests_failed" -gt 0 ]] || [[ "$assertions_failed" -gt 0 ]]; then
printf " %s%s failed%s," "$_BASHUNIT_COLOR_FAILED" "$tests_failed" "$_BASHUNIT_COLOR_DEFAULT"
fi
printf " %s total\n" "$total_tests"
printf "%sAssertions:%s" "$_BASHUNIT_COLOR_FAINT" "$_BASHUNIT_COLOR_DEFAULT"
if [[ "$tests_passed" -gt 0 ]] || [[ "$assertions_passed" -gt 0 ]]; then
printf " %s%s passed%s," "$_BASHUNIT_COLOR_PASSED" "$assertions_passed" "$_BASHUNIT_COLOR_DEFAULT"
fi
if [[ "$tests_skipped" -gt 0 ]] || [[ "$assertions_skipped" -gt 0 ]]; then
printf " %s%s skipped%s," "$_BASHUNIT_COLOR_SKIPPED" "$assertions_skipped" "$_BASHUNIT_COLOR_DEFAULT"
fi
if [[ "$tests_incomplete" -gt 0 ]] || [[ "$assertions_incomplete" -gt 0 ]]; then
printf " %s%s incomplete%s," "$_BASHUNIT_COLOR_INCOMPLETE" "$assertions_incomplete" "$_BASHUNIT_COLOR_DEFAULT"
fi
if [[ "$tests_snapshot" -gt 0 ]] || [[ "$assertions_snapshot" -gt 0 ]]; then
printf " %s%s snapshot%s," "$_BASHUNIT_COLOR_SNAPSHOT" "$assertions_snapshot" "$_BASHUNIT_COLOR_DEFAULT"
fi
if [[ "$tests_failed" -gt 0 ]] || [[ "$assertions_failed" -gt 0 ]]; then
printf " %s%s failed%s," "$_BASHUNIT_COLOR_FAILED" "$assertions_failed" "$_BASHUNIT_COLOR_DEFAULT"
fi
printf " %s total\n" "$total_assertions"
if [[ "$tests_failed" -gt 0 ]]; then
printf "\n%s%s%s\n" "$_BASHUNIT_COLOR_RETURN_ERROR" " Some tests failed " "$_BASHUNIT_COLOR_DEFAULT"
bashunit::console_results::print_execution_time
return 1
fi
if [[ "$tests_incomplete" -gt 0 ]]; then
printf "\n%s%s%s\n" "$_BASHUNIT_COLOR_RETURN_INCOMPLETE" " Some tests incomplete " "$_BASHUNIT_COLOR_DEFAULT"
bashunit::console_results::print_execution_time
return 0
fi
if [[ "$tests_skipped" -gt 0 ]]; then
printf "\n%s%s%s\n" "$_BASHUNIT_COLOR_RETURN_SKIPPED" " Some tests skipped " "$_BASHUNIT_COLOR_DEFAULT"
bashunit::console_results::print_execution_time
return 0
fi
if [[ "$tests_snapshot" -gt 0 ]]; then
printf "\n%s%s%s\n" "$_BASHUNIT_COLOR_RETURN_SNAPSHOT" " Some snapshots created " "$_BASHUNIT_COLOR_DEFAULT"
bashunit::console_results::print_execution_time
return 0
fi
if [[ $total_tests -eq 0 ]]; then
printf "\n%s%s%s\n" "$_BASHUNIT_COLOR_RETURN_ERROR" " No tests found " "$_BASHUNIT_COLOR_DEFAULT"
bashunit::console_results::print_execution_time
return 1
fi
printf "\n%s%s%s\n" "$_BASHUNIT_COLOR_RETURN_SUCCESS" " All tests passed " "$_BASHUNIT_COLOR_DEFAULT"
bashunit::console_results::print_execution_time
return 0
}
function bashunit::console_results::print_execution_time() {
if ! bashunit::env::is_show_execution_time_enabled; then
return
fi
local time
time=$(bashunit::clock::total_runtime_in_milliseconds)
# Strip decimal portion (integer truncation, Bash 3.0 compatible)
time="${time%%.*}"
time="${time:-0}"
if [[ "$time" -lt 1000 ]]; then
printf "${_BASHUNIT_COLOR_BOLD}%s${_BASHUNIT_COLOR_DEFAULT}\n" \
"Time taken: ${time}ms"
return
fi
local time_in_seconds=$((time / 1000))
if [[ "$time_in_seconds" -ge 60 ]]; then
local minutes=$((time_in_seconds / 60))
local seconds=$((time_in_seconds % 60))
printf "${_BASHUNIT_COLOR_BOLD}%s${_BASHUNIT_COLOR_DEFAULT}\n" \
"Time taken: ${minutes}m ${seconds}s"
return
fi
local integer_part=$((time / 1000))
local decimal_part=$(( (time % 1000) / 10 ))
local formatted_seconds
formatted_seconds=$(printf "%d.%02d" "$integer_part" "$decimal_part")
printf "${_BASHUNIT_COLOR_BOLD}%s${_BASHUNIT_COLOR_DEFAULT}\n" \
"Time taken: ${formatted_seconds}s"
}
function bashunit::console_results::format_duration() {
local duration_ms="$1"
if [[ "$duration_ms" -ge 60000 ]]; then
local time_in_seconds=$((duration_ms / 1000))
local minutes=$((time_in_seconds / 60))
local seconds=$((time_in_seconds % 60))
echo "${minutes}m ${seconds}s"
elif [[ "$duration_ms" -ge 1000 ]]; then
local integer_part=$((duration_ms / 1000))
local decimal_part=$(( (duration_ms % 1000) / 10 ))
local formatted_seconds
formatted_seconds=$(printf "%d.%02d" "$integer_part" "$decimal_part")
echo "${formatted_seconds}s"
else
echo "${duration_ms}ms"
fi
}
function bashunit::console_results::print_hook_completed() {
local hook_name="$1"
local duration_ms="$2"
if bashunit::env::is_simple_output_enabled; then
return
fi
if bashunit::env::is_failures_only_enabled; then
return
fi
if bashunit::env::is_no_progress_enabled; then
return
fi
if bashunit::env::is_tap_output_enabled; then
return
fi
if bashunit::parallel::is_enabled; then
return
fi
local line
line=$(printf "%s● %s%s" \
"$_BASHUNIT_COLOR_PASSED" "$hook_name" "$_BASHUNIT_COLOR_DEFAULT")
local time_display
time_display=$(bashunit::console_results::format_duration "$duration_ms")
printf "%s\n" "$(bashunit::str::rpad "$line" "$time_display")"
}
function bashunit::console_results::print_successful_test() {
local test_name=$1
shift
local duration=${1:-"0"}
shift
local line
if [[ -z "$*" ]]; then
line=$(printf "%s✓ Passed%s: %s" "$_BASHUNIT_COLOR_PASSED" "$_BASHUNIT_COLOR_DEFAULT" "$test_name")
else
local quoted_args=""
local arg
for arg in "$@"; do
if [[ -z "$quoted_args" ]]; then
quoted_args="'$arg'"
else
quoted_args="$quoted_args, '$arg'"
fi
done
line=$(printf "%s✓ Passed%s: %s (%s)" \
"$_BASHUNIT_COLOR_PASSED" "$_BASHUNIT_COLOR_DEFAULT" "$test_name" "$quoted_args")
fi
local full_line=$line
if bashunit::env::is_show_execution_time_enabled; then
local time_display
if [[ "$duration" -ge 60000 ]]; then
local time_in_seconds=$((duration / 1000))
local minutes=$((time_in_seconds / 60))
local seconds=$((time_in_seconds % 60))
time_display="${minutes}m ${seconds}s"
elif [[ "$duration" -ge 1000 ]]; then
local integer_part=$((duration / 1000))
local decimal_part=$(( (duration % 1000) / 10 ))
local formatted_seconds
formatted_seconds=$(printf "%d.%02d" "$integer_part" "$decimal_part")
time_display="${formatted_seconds}s"
else
time_display="${duration}ms"
fi
full_line="$(printf "%s\n" "$(bashunit::str::rpad "$line" "$time_display")")"
fi
bashunit::state::print_line "successful" "$full_line"
}
function bashunit::console_results::print_failure_message() {
local test_name=$1
local failure_message=$2
local line
line="$(printf "\
${_BASHUNIT_COLOR_FAILED}✗ Failed${_BASHUNIT_COLOR_DEFAULT}: %s
${_BASHUNIT_COLOR_FAINT}Message:${_BASHUNIT_COLOR_DEFAULT} \
${_BASHUNIT_COLOR_BOLD}'%s'${_BASHUNIT_COLOR_DEFAULT}\n" \
"${test_name}" "${failure_message}")"
bashunit::state::print_line "failure" "$line"
}
function bashunit::console_results::print_failed_test() {
local function_name=$1
local expected=$2
local failure_condition_message=$3
local actual=$4
local extra_key=${5-}
local extra_value=${6-}
local line
line="$(printf "\
${_BASHUNIT_COLOR_FAILED}✗ Failed${_BASHUNIT_COLOR_DEFAULT}: %s
${_BASHUNIT_COLOR_FAINT}Expected${_BASHUNIT_COLOR_DEFAULT} ${_BASHUNIT_COLOR_BOLD}'%s'${_BASHUNIT_COLOR_DEFAULT}
${_BASHUNIT_COLOR_FAINT}%s${_BASHUNIT_COLOR_DEFAULT} ${_BASHUNIT_COLOR_BOLD}'%s'${_BASHUNIT_COLOR_DEFAULT}\n" \
"${function_name}" "${expected}" "${failure_condition_message}" "${actual}")"
if [ -n "$extra_key" ]; then
line="$line$(printf "\
${_BASHUNIT_COLOR_FAINT}%s${_BASHUNIT_COLOR_DEFAULT} ${_BASHUNIT_COLOR_BOLD}'%s'${_BASHUNIT_COLOR_DEFAULT}\n" \
"${extra_key}" "${extra_value}")"
fi
bashunit::state::print_line "failed" "$line"
}
function bashunit::console_results::print_failed_snapshot_test() {
local function_name=$1
local snapshot_file=$2
local actual_content=${3-}
local line
line="$(printf "${_BASHUNIT_COLOR_FAILED}✗ Failed${_BASHUNIT_COLOR_DEFAULT}: %s
${_BASHUNIT_COLOR_FAINT}Expected to match the snapshot${_BASHUNIT_COLOR_DEFAULT}\n" "$function_name")"
if bashunit::dependencies::has_git; then
local actual_file="${snapshot_file}.tmp"
echo "$actual_content" >"$actual_file"
local git_diff_output
git_diff_output="$(git diff --no-index --word-diff --color=always \
"$snapshot_file" "$actual_file" 2>/dev/null |
tail -n +6 | sed "s/^/ /")"
line="$line$git_diff_output"
rm "$actual_file"
fi
bashunit::state::print_line "failed_snapshot" "$line"
}
function bashunit::console_results::print_skipped_test() {
local function_name=$1
local reason=${2-}
local line
line="$(printf "${_BASHUNIT_COLOR_SKIPPED}↷ Skipped${_BASHUNIT_COLOR_DEFAULT}: %s\n" "${function_name}")"
if [[ -n "$reason" ]]; then
line="$line$(printf "${_BASHUNIT_COLOR_FAINT} %s${_BASHUNIT_COLOR_DEFAULT}\n" "${reason}")"
fi
bashunit::state::print_line "skipped" "$line"
}
function bashunit::console_results::print_incomplete_test() {
local function_name=$1
local pending=${2-}
local line
line="$(printf "${_BASHUNIT_COLOR_INCOMPLETE}✒ Incomplete${_BASHUNIT_COLOR_DEFAULT}: %s\n" "${function_name}")"
if [[ -n "$pending" ]]; then
line="$line$(printf "${_BASHUNIT_COLOR_FAINT} %s${_BASHUNIT_COLOR_DEFAULT}\n" "${pending}")"
fi
bashunit::state::print_line "incomplete" "$line"
}
function bashunit::console_results::print_snapshot_test() {
local function_name=$1
local test_name
test_name=$(bashunit::helper::normalize_test_function_name "$function_name")
local line
line="$(printf "${_BASHUNIT_COLOR_SNAPSHOT}✎ Snapshot${_BASHUNIT_COLOR_DEFAULT}: %s\n" "${test_name}")"
bashunit::state::print_line "snapshot" "$line"
}
function bashunit::console_results::print_error_test() {
local function_name=$1
local error="$2"
local raw_output="${3:-}"
local test_name
test_name=$(bashunit::helper::normalize_test_function_name "$function_name")
local line
line="$(printf "${_BASHUNIT_COLOR_FAILED}✗ Error${_BASHUNIT_COLOR_DEFAULT}: %s
${_BASHUNIT_COLOR_FAINT}%s${_BASHUNIT_COLOR_DEFAULT}\n" "${test_name}" "${error}")"
if [[ -n "$raw_output" ]] && bashunit::env::is_show_output_on_failure_enabled; then
line="$line$(printf " %sOutput:%s\n" "${_BASHUNIT_COLOR_FAINT}" "${_BASHUNIT_COLOR_DEFAULT}")"
local output_line
while IFS= read -r output_line; do
line="$line$(printf " %s\n" "$output_line")"
done <<<"$raw_output"
fi
bashunit::state::print_line "error" "$line"
}
function bashunit::console_results::print_failing_tests_and_reset() {
if [[ -s "$FAILURES_OUTPUT_PATH" ]]; then
local total_failed
total_failed=$(bashunit::state::get_tests_failed)
if bashunit::env::is_simple_output_enabled; then
printf "\n\n"
fi
if [[ "$total_failed" -eq 1 ]]; then
echo -e "${_BASHUNIT_COLOR_BOLD}There was 1 failure:${_BASHUNIT_COLOR_DEFAULT}\n"
else
echo -e "${_BASHUNIT_COLOR_BOLD}There were $total_failed failures:${_BASHUNIT_COLOR_DEFAULT}\n"
fi
sed '${/^$/d;}' "$FAILURES_OUTPUT_PATH" | sed 's/^/|/'
rm "$FAILURES_OUTPUT_PATH"
echo ""
fi
}
function bashunit::console_results::print_skipped_tests_and_reset() {
if [[ -s "$SKIPPED_OUTPUT_PATH" ]] && bashunit::env::is_show_skipped_enabled; then
local total_skipped
total_skipped=$(bashunit::state::get_tests_skipped)
if bashunit::env::is_simple_output_enabled; then
printf "\n"
fi
if [[ "$total_skipped" -eq 1 ]]; then
echo -e "${_BASHUNIT_COLOR_BOLD}There was 1 skipped test:${_BASHUNIT_COLOR_DEFAULT}\n"
else
echo -e "${_BASHUNIT_COLOR_BOLD}There were $total_skipped skipped tests:${_BASHUNIT_COLOR_DEFAULT}\n"
fi
tr -d '\r' <"$SKIPPED_OUTPUT_PATH" | sed '/^[[:space:]]*$/d' | sed 's/^/|/'
rm "$SKIPPED_OUTPUT_PATH"
echo ""
fi
}
function bashunit::console_results::print_incomplete_tests_and_reset() {
if [[ -s "$INCOMPLETE_OUTPUT_PATH" ]] && bashunit::env::is_show_incomplete_enabled; then
local total_incomplete
total_incomplete=$(bashunit::state::get_tests_incomplete)
if bashunit::env::is_simple_output_enabled; then
printf "\n"
fi
if [[ "$total_incomplete" -eq 1 ]]; then
echo -e "${_BASHUNIT_COLOR_BOLD}There was 1 incomplete test:${_BASHUNIT_COLOR_DEFAULT}\n"
else
echo -e "${_BASHUNIT_COLOR_BOLD}There were $total_incomplete incomplete tests:${_BASHUNIT_COLOR_DEFAULT}\n"
fi
tr -d '\r' <"$INCOMPLETE_OUTPUT_PATH" | sed '/^[[:space:]]*$/d' | sed 's/^/|/'
rm "$INCOMPLETE_OUTPUT_PATH"
echo ""
fi
}