Skip to content

Commit 205da41

Browse files
committed
chore: progress bar
1 parent 3f1fa02 commit 205da41

25 files changed

Lines changed: 272 additions & 37 deletions

bashunit

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ source "$BASHUNIT_ROOT_DIR/src/parallel.sh"
1919
source "$BASHUNIT_ROOT_DIR/src/env.sh"
2020
source "$BASHUNIT_ROOT_DIR/src/clock.sh"
2121
source "$BASHUNIT_ROOT_DIR/src/state.sh"
22+
source "$BASHUNIT_ROOT_DIR/src/progress.sh"
2223
source "$BASHUNIT_ROOT_DIR/src/colors.sh"
2324
source "$BASHUNIT_ROOT_DIR/src/console_header.sh"
2425
source "$BASHUNIT_ROOT_DIR/src/console_results.sh"

build.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ function build::dependencies() {
9494
"src/env.sh"
9595
"src/clock.sh"
9696
"src/state.sh"
97+
"src/progress.sh"
9798
"src/colors.sh"
9899
"src/console_header.sh"
99100
"src/console_results.sh"

src/assert_snapshot.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,17 @@ function snapshot::match_with_placeholder() {
3434
fi
3535
}
3636

37+
# Remove progress bar output from a given string. Progress bar sequences are
38+
# wrapped between ESC7 and ESC8 control codes when a TTY is present.
39+
function snapshot::strip_progress_line() {
40+
local input="$1"
41+
echo -n "$input" | sed $'s/\x1b7.*\x1b8//'
42+
}
43+
3744
function assert_match_snapshot() {
3845
local actual
3946
actual=$(echo -n "$1" | tr -d '\r')
47+
actual=$(snapshot::strip_progress_line "$actual")
4048
local directory
4149
directory="./$(dirname "${BASH_SOURCE[1]}")/snapshots"
4250
local test_file
@@ -73,6 +81,7 @@ function assert_match_snapshot() {
7381
function assert_match_snapshot_ignore_colors() {
7482
local actual
7583
actual=$(echo -n "$1" | sed -r 's/\x1B\[[0-9;]*[mK]//g' | tr -d '\r')
84+
actual=$(snapshot::strip_progress_line "$actual")
7685

7786
local directory
7887
directory="./$(dirname "${BASH_SOURCE[1]}")/snapshots"

src/benchmark.sh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,13 @@ function benchmark::print_results() {
9494
fi
9595

9696
if env::is_simple_output_enabled; then
97-
printf "\n"
97+
progress::blank_line
9898
fi
9999

100-
printf "\nBenchmark Results (avg ms)\n"
100+
progress::blank_line
101+
printf "Benchmark Results (avg ms)\n"
101102
print_line 80 "="
102-
printf "\n"
103+
progress::blank_line
103104

104105
local has_threshold=false
105106
for val in "${_BENCH_MAX_MILLIS[@]}"; do

src/console_results.sh

Lines changed: 82 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ function console_results::render_result() {
1313
fi
1414

1515
if env::is_simple_output_enabled; then
16-
printf "\n\n"
16+
progress::blank_line
17+
progress::blank_line
1718
fi
1819

1920
local total_tests=0
@@ -67,36 +68,42 @@ function console_results::render_result() {
6768
printf " %s total\n" "$total_assertions"
6869

6970
if [[ "$(state::get_tests_failed)" -gt 0 ]]; then
70-
printf "\n%s%s%s\n" "$_COLOR_RETURN_ERROR" " Some tests failed " "$_COLOR_DEFAULT"
71+
progress::blank_line
72+
printf "%s%s%s\n" "$_COLOR_RETURN_ERROR" " Some tests failed " "$_COLOR_DEFAULT"
7173
console_results::print_execution_time
7274
return 1
7375
fi
7476

7577
if [[ "$(state::get_tests_incomplete)" -gt 0 ]]; then
76-
printf "\n%s%s%s\n" "$_COLOR_RETURN_INCOMPLETE" " Some tests incomplete " "$_COLOR_DEFAULT"
78+
progress::blank_line
79+
printf "%s%s%s\n" "$_COLOR_RETURN_INCOMPLETE" " Some tests incomplete " "$_COLOR_DEFAULT"
7780
console_results::print_execution_time
7881
return 0
7982
fi
8083

8184
if [[ "$(state::get_tests_skipped)" -gt 0 ]]; then
82-
printf "\n%s%s%s\n" "$_COLOR_RETURN_SKIPPED" " Some tests skipped " "$_COLOR_DEFAULT"
85+
progress::blank_line
86+
printf "%s%s%s\n" "$_COLOR_RETURN_SKIPPED" " Some tests skipped " "$_COLOR_DEFAULT"
8387
console_results::print_execution_time
8488
return 0
8589
fi
8690

8791
if [[ "$(state::get_tests_snapshot)" -gt 0 ]]; then
88-
printf "\n%s%s%s\n" "$_COLOR_RETURN_SNAPSHOT" " Some snapshots created " "$_COLOR_DEFAULT"
92+
progress::blank_line
93+
printf "%s%s%s\n" "$_COLOR_RETURN_SNAPSHOT" " Some snapshots created " "$_COLOR_DEFAULT"
8994
console_results::print_execution_time
9095
return 0
9196
fi
9297

9398
if [[ $total_tests -eq 0 ]]; then
94-
printf "\n%s%s%s\n" "$_COLOR_RETURN_ERROR" " No tests found " "$_COLOR_DEFAULT"
99+
progress::blank_line
100+
printf "%s%s%s\n" "$_COLOR_RETURN_ERROR" " No tests found " "$_COLOR_DEFAULT"
95101
console_results::print_execution_time
96102
return 1
97103
fi
98104

99-
printf "\n%s%s%s\n" "$_COLOR_RETURN_SUCCESS" " All tests passed " "$_COLOR_DEFAULT"
105+
progress::blank_line
106+
printf "%s%s%s\n" "$_COLOR_RETURN_SUCCESS" " All tests passed " "$_COLOR_DEFAULT"
100107
console_results::print_execution_time
101108
return 0
102109
}
@@ -143,17 +150,39 @@ function console_results::print_successful_test() {
143150
state::print_line "successful" "$full_line"
144151
}
145152

153+
# Print a summary line for a failed test with its duration.
154+
function console_results::print_failed_test_summary() {
155+
local test_name=$1
156+
local duration=${2:-"0"}
157+
158+
local line
159+
line=$(printf "%s✗ Failed%s: %s" "$_COLOR_FAILED" "$_COLOR_DEFAULT" "$test_name")
160+
161+
local full_line=$line
162+
if env::is_show_execution_time_enabled; then
163+
full_line="$(printf "%s\n" "$(str::rpad "$line" "$duration ms")")"
164+
fi
165+
166+
state::print_line "failed" "$full_line"
167+
}
168+
146169
function console_results::print_failure_message() {
147170
local test_name=$1
148171
local failure_message=$2
172+
local duration=${3-}
149173

150174
local line
151175
line="$(printf "\
152176
${_COLOR_FAILED}✗ Failed${_COLOR_DEFAULT}: %s
153177
${_COLOR_FAINT}Message:${_COLOR_DEFAULT} ${_COLOR_BOLD}'%s'${_COLOR_DEFAULT}\n"\
154178
"${test_name}" "${failure_message}")"
155179

156-
state::print_line "failure" "$line"
180+
local full_line=$line
181+
if [[ -n "$duration" ]] && env::is_show_execution_time_enabled; then
182+
full_line="$(printf "%s\n" "$(str::rpad "$line" "$duration ms")")"
183+
fi
184+
185+
state::print_line "failure" "$full_line"
157186
}
158187

159188
function console_results::print_failed_test() {
@@ -163,6 +192,7 @@ function console_results::print_failed_test() {
163192
local actual=$4
164193
local extra_key=${5-}
165194
local extra_value=${6-}
195+
local duration=${7-}
166196

167197
local line
168198
line="$(printf "\
@@ -178,13 +208,19 @@ ${_COLOR_FAILED}✗ Failed${_COLOR_DEFAULT}: %s
178208
"${extra_key}" "${extra_value}")"
179209
fi
180210

181-
state::print_line "failed" "$line"
211+
local full_line=$line
212+
if [[ -n "$duration" ]] && env::is_show_execution_time_enabled; then
213+
full_line="$(printf "%s\n" "$(str::rpad "$line" "$duration ms")")"
214+
fi
215+
216+
state::print_line "failed" "$full_line"
182217
}
183218

184219

185220
function console_results::print_failed_snapshot_test() {
186221
local function_name=$1
187222
local snapshot_file=$2
223+
local duration=${3-}
188224

189225
local line
190226
line="$(printf "${_COLOR_FAILED}✗ Failed${_COLOR_DEFAULT}: %s
@@ -203,12 +239,18 @@ function console_results::print_failed_snapshot_test() {
203239
rm "$actual_file"
204240
fi
205241

206-
state::print_line "failed_snapshot" "$line"
242+
local full_line=$line
243+
if [[ -n "$duration" ]] && env::is_show_execution_time_enabled; then
244+
full_line="$(printf "%s\n" "$(str::rpad "$line" "$duration ms")")"
245+
fi
246+
247+
state::print_line "failed_snapshot" "$full_line"
207248
}
208249

209250
function console_results::print_skipped_test() {
210251
local function_name=$1
211252
local reason=${2-}
253+
local duration=${3-}
212254

213255
local line
214256
line="$(printf "${_COLOR_SKIPPED}↷ Skipped${_COLOR_DEFAULT}: %s\n" "${function_name}")"
@@ -217,12 +259,18 @@ function console_results::print_skipped_test() {
217259
line+="$(printf "${_COLOR_FAINT} %s${_COLOR_DEFAULT}\n" "${reason}")"
218260
fi
219261

220-
state::print_line "skipped" "$line"
262+
local full_line=$line
263+
if [[ -n "$duration" ]] && env::is_show_execution_time_enabled; then
264+
full_line="$(printf "%s\n" "$(str::rpad "$line" "$duration ms")")"
265+
fi
266+
267+
state::print_line "skipped" "$full_line"
221268
}
222269

223270
function console_results::print_incomplete_test() {
224271
local function_name=$1
225272
local pending=${2-}
273+
local duration=${3-}
226274

227275
local line
228276
line="$(printf "${_COLOR_INCOMPLETE}✒ Incomplete${_COLOR_DEFAULT}: %s\n" "${function_name}")"
@@ -231,23 +279,35 @@ function console_results::print_incomplete_test() {
231279
line+="$(printf "${_COLOR_FAINT} %s${_COLOR_DEFAULT}\n" "${pending}")"
232280
fi
233281

234-
state::print_line "incomplete" "$line"
282+
local full_line=$line
283+
if [[ -n "$duration" ]] && env::is_show_execution_time_enabled; then
284+
full_line="$(printf "%s\n" "$(str::rpad "$line" "$duration ms")")"
285+
fi
286+
287+
state::print_line "incomplete" "$full_line"
235288
}
236289

237290
function console_results::print_snapshot_test() {
238291
local function_name=$1
292+
local duration=${2-}
239293
local test_name
240294
test_name=$(helper::normalize_test_function_name "$function_name")
241295

242296
local line
243297
line="$(printf "${_COLOR_SNAPSHOT}✎ Snapshot${_COLOR_DEFAULT}: %s\n" "${test_name}")"
244298

245-
state::print_line "snapshot" "$line"
299+
local full_line=$line
300+
if [[ -n "$duration" ]] && env::is_show_execution_time_enabled; then
301+
full_line="$(printf "%s\n" "$(str::rpad "$line" "$duration ms")")"
302+
fi
303+
304+
state::print_line "snapshot" "$full_line"
246305
}
247306

248307
function console_results::print_error_test() {
249308
local function_name=$1
250309
local error="$2"
310+
local duration=${3-}
251311

252312
local test_name
253313
test_name=$(helper::normalize_test_function_name "$function_name")
@@ -256,7 +316,12 @@ function console_results::print_error_test() {
256316
line="$(printf "${_COLOR_FAILED}✗ Error${_COLOR_DEFAULT}: %s
257317
${_COLOR_FAINT}%s${_COLOR_DEFAULT}\n" "${test_name}" "${error}")"
258318

259-
state::print_line "error" "$line"
319+
local full_line=$line
320+
if [[ -n "$duration" ]] && env::is_show_execution_time_enabled; then
321+
full_line="$(printf "%s\n" "$(str::rpad "$line" "$duration ms")")"
322+
fi
323+
324+
state::print_line "error" "$full_line"
260325
}
261326

262327
function console_results::print_failing_tests_and_reset() {
@@ -265,7 +330,8 @@ function console_results::print_failing_tests_and_reset() {
265330
total_failed=$(state::get_tests_failed)
266331

267332
if env::is_simple_output_enabled; then
268-
printf "\n\n"
333+
progress::blank_line
334+
progress::blank_line
269335
fi
270336

271337
if [[ "$total_failed" -eq 1 ]]; then
@@ -277,6 +343,6 @@ function console_results::print_failing_tests_and_reset() {
277343
sed '${/^$/d;}' "$FAILURES_OUTPUT_PATH" | sed 's/^/|/'
278344
rm "$FAILURES_OUTPUT_PATH"
279345

280-
echo ""
346+
progress::blank_line
281347
fi
282348
}

src/env.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ _DEFAULT_STOP_ON_FAILURE="false"
2828
_DEFAULT_SHOW_EXECUTION_TIME="true"
2929
_DEFAULT_VERBOSE="false"
3030
_DEFAULT_BENCH_MODE="false"
31+
_DEFAULT_PROGRESS="true"
32+
33+
# Controls if the progress bar should render during execution
34+
_PROGRESS_ENABLED=true
3135

3236
: "${BASHUNIT_PARALLEL_RUN:=${PARALLEL_RUN:=$_DEFAULT_PARALLEL_RUN}}"
3337
: "${BASHUNIT_SHOW_HEADER:=${SHOW_HEADER:=$_DEFAULT_SHOW_HEADER}}"
@@ -37,6 +41,7 @@ _DEFAULT_BENCH_MODE="false"
3741
: "${BASHUNIT_SHOW_EXECUTION_TIME:=${SHOW_EXECUTION_TIME:=$_DEFAULT_SHOW_EXECUTION_TIME}}"
3842
: "${BASHUNIT_VERBOSE:=${VERBOSE:=$_DEFAULT_VERBOSE}}"
3943
: "${BASHUNIT_BENCH_MODE:=${BENCH_MODE:=$_DEFAULT_BENCH_MODE}}"
44+
: "${BASHUNIT_PROGRESS:=${PROGRESS:=$_DEFAULT_PROGRESS}}"
4045

4146
function env::is_parallel_run_enabled() {
4247
[[ "$BASHUNIT_PARALLEL_RUN" == "true" ]]
@@ -74,6 +79,10 @@ function env::is_bench_mode_enabled() {
7479
[[ "$BASHUNIT_BENCH_MODE" == "true" ]]
7580
}
7681

82+
function env::is_progress_enabled() {
83+
[[ "$BASHUNIT_PROGRESS" == "true" ]]
84+
}
85+
7786
function env::active_internet_connection() {
7887
if ping -c 1 -W 3 google.com &> /dev/null; then
7988
return 0

src/main.sh

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,14 @@ function main::exec_tests() {
3232

3333
console_header::print_version_with_env "$filter" "${test_files[@]}"
3434

35+
local total_tests
36+
total_tests=$(helpers::find_total_tests "$filter" "${test_files[@]}")
37+
state::set_total_tests_to_run "$total_tests"
38+
progress::init "$total_tests"
39+
3540
if env::is_verbose_enabled; then
3641
if env::is_simple_output_enabled; then
37-
echo ""
42+
progress::blank_line
3843
fi
3944
printf '%*s\n' "$TERMINAL_WIDTH" '' | tr ' ' '#'
4045
printf "%s\n" "Filter: ${filter:-None}"
@@ -59,6 +64,7 @@ function main::exec_tests() {
5964
console_results::print_failing_tests_and_reset
6065
console_results::render_result
6166
exit_code=$?
67+
progress::finish
6268

6369
if [[ -n "$BASHUNIT_LOG_JUNIT" ]]; then
6470
reports::generate_junit_xml "$BASHUNIT_LOG_JUNIT"
@@ -105,7 +111,8 @@ function main::cleanup() {
105111
}
106112

107113
function main::handle_stop_on_failure_sync() {
108-
printf "\n%sStop on failure enabled...%s\n" "${_COLOR_SKIPPED}" "${_COLOR_DEFAULT}"
114+
progress::blank_line
115+
printf "%sStop on failure enabled...%s\n" "${_COLOR_SKIPPED}" "${_COLOR_DEFAULT}"
109116
console_results::print_failing_tests_and_reset
110117
console_results::render_result
111118
cleanup_temp_files

0 commit comments

Comments
 (0)