Skip to content

Commit c2c414b

Browse files
authored
Merge pull request #317 from TypedDevs/feat/291-display-total-tests
Display total tests upfront
2 parents 64f82c5 + 0d5ef67 commit c2c414b

7 files changed

+85
-30
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
- Change `-v` as shortcut for `--version`
1414
- Add `-vvv` as shortcut for `--verbose`
1515
- Fix wrong commit id when installing beta
16+
- Add display total tests upfront when running bashunit
1617
- Add `BASHUNIT_` suffix to all .env config keys
1718
- BASHUNIT_SHOW_HEADER
1819
- BASHUNIT_HEADER_ASCII_ART

docs/quickstart.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,13 @@ Once **bashunit** is installed, you're ready to get started.
4545

4646
4. If everything works correctly, you should see an output similar to the following:
4747
```
48-
Running tests/example_test.sh
48+
bashunit - 0.14.0 | Total tests: 1
49+
Running local/example_test.sh
4950
✓ Passed: Bashunit is working
5051

5152
Tests: 1 passed, 1 total
5253
Assertions: 1 passed, 1 total
54+
5355
All tests passed
5456
Time taken: 100 ms
5557
```

src/console_header.sh

+36-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
#!/bin/bash
22

3+
function console_header::print_version_with_env() {
4+
local files=("${@:-}")
5+
local should_print_ascii="true"
6+
7+
if [[ "$BASHUNIT_SHOW_HEADER" != "$should_print_ascii" ]]; then
8+
return
9+
fi
10+
11+
console_header::print_version "${files[@]}"
12+
}
13+
314
function console_header::print_version() {
15+
local files=("${@:-}")
16+
local total_tests
17+
total_tests=$(helpers::find_total_tests "${files[@]}")
18+
419
if [[ $BASHUNIT_HEADER_ASCII_ART == true ]]; then
520
cat <<EOF
621
_ _ _
@@ -9,18 +24,21 @@ function console_header::print_version() {
924
| |_) | (_| \__ \ | | | |_| | | | | | |_
1025
|_.__/ \__,_|___/_| |_|\___/|_| |_|_|\__|
1126
EOF
12-
printf "%s\n\n" "$BASHUNIT_VERSION"
13-
else
14-
printf "${_COLOR_BOLD}${_COLOR_PASSED}bashunit${_COLOR_DEFAULT} - %s\n" "$BASHUNIT_VERSION"
27+
if [ "$total_tests" -eq 0 ]; then
28+
printf "%s\n" "$BASHUNIT_VERSION"
29+
else
30+
printf "%s | Total tests: %s\n" "$BASHUNIT_VERSION" "$total_tests"
31+
fi
32+
return
1533
fi
16-
}
1734

18-
function console_header::print_version_with_env() {
19-
local should_print_ascii="true"
20-
if [[ "$BASHUNIT_SHOW_HEADER" != "$should_print_ascii" ]]; then
21-
return
22-
fi
23-
console_header::print_version
35+
if [ "$total_tests" -eq 0 ]; then
36+
printf "${_COLOR_BOLD}${_COLOR_PASSED}bashunit${_COLOR_DEFAULT} - %s\n" "$BASHUNIT_VERSION"
37+
else
38+
printf "${_COLOR_BOLD}${_COLOR_PASSED}bashunit${_COLOR_DEFAULT} - %s | Total tests: %s\n"\
39+
"$BASHUNIT_VERSION"\
40+
"$total_tests"
41+
fi
2442
}
2543

2644
function console_header::print_help() {
@@ -69,3 +87,11 @@ Options:
6987
See more: https://bashunit.typeddevs.com/command-line
7088
EOF
7189
}
90+
91+
function console_header::print_total_tests() {
92+
local files=("${@}")
93+
94+
local total_tests
95+
total_tests=$(helpers::find_total_tests "${files[@]}")
96+
printf "\rTotal tests: %s\n" "$total_tests"
97+
}

src/helpers.sh

+32
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,35 @@ function helpers::get_latest_tag() {
149149
sort -Vr |
150150
head -n 1
151151
}
152+
153+
function helpers::find_total_tests() {
154+
local files=("$@")
155+
local total_count=0
156+
157+
for file in "${files[@]}"; do
158+
local count
159+
count=$(grep -r -E '^\s*function\s+test' "$file" --include=\*.sh 2>/dev/null | wc -l)
160+
total_count=$((total_count + count))
161+
done
162+
163+
echo "$total_count"
164+
}
165+
166+
function helper::load_test_files() {
167+
local filter=$1
168+
local files=("${@:2}")
169+
170+
local test_files=()
171+
172+
if [[ "${#files[@]}" -eq 0 ]]; then
173+
if [[ -n "${BASHUNIT_DEFAULT_PATH}" ]]; then
174+
while IFS='' read -r line; do
175+
test_files+=("$line")
176+
done < <(helper::find_files_recursive "$BASHUNIT_DEFAULT_PATH")
177+
fi
178+
else
179+
test_files=("${files[@]}")
180+
fi
181+
182+
printf "%s\n" "${test_files[@]}"
183+
}

src/main.sh

+13-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,19 @@ function main::exec_tests() {
44
local filter=$1
55
local files=("${@:2}")
66

7-
console_header::print_version_with_env
8-
runner::load_test_files "$filter" "${files[@]}"
7+
local test_files=()
8+
while IFS= read -r line; do
9+
test_files+=("$line")
10+
done < <(helper::load_test_files "$filter" "${files[@]}")
11+
12+
if [[ ${#test_files[@]} -eq 0 || -z "${test_files[0]}" ]]; then
13+
printf "%sError: At least one file path is required.%s\n" "${_COLOR_FAILED}" "${_COLOR_DEFAULT}"
14+
console_header::print_help
15+
exit 1
16+
fi
17+
18+
console_header::print_version_with_env "${test_files[@]}"
19+
runner::load_test_files "$filter" "${test_files[@]}"
920
console_results::render_result
1021
exit_code=$?
1122

src/runner.sh

-12
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,6 @@ function runner::load_test_files() {
44
local filter=$1
55
local files=("${@:2}") # Store all arguments starting from the second as an array
66

7-
if [[ "${#files[@]}" == 0 ]]; then
8-
if [[ -n "${BASHUNIT_DEFAULT_PATH}" ]]; then
9-
while IFS='' read -r line; do
10-
files+=("$line");
11-
done < <(helper::find_files_recursive "$BASHUNIT_DEFAULT_PATH")
12-
else
13-
printf "%sError: At least one file path is required.%s\n" "${_COLOR_FAILED}" "${_COLOR_DEFAULT}"
14-
console_header::print_help
15-
exit 1
16-
fi
17-
fi
18-
197
for test_file in "${files[@]}"; do
208
if [[ ! -f $test_file ]]; then
219
continue

tests/acceptance/snapshots/bashunit_log_junit_test_sh.test_bashunit_when_stop_on_failure_env.snapshot

-5
This file was deleted.

0 commit comments

Comments
 (0)