Skip to content

Commit

Permalink
Merge pull request #317 from TypedDevs/feat/291-display-total-tests
Browse files Browse the repository at this point in the history
Display total tests upfront
  • Loading branch information
Chemaclass authored Sep 1, 2024
2 parents 64f82c5 + 0d5ef67 commit c2c414b
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 30 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- Change `-v` as shortcut for `--version`
- Add `-vvv` as shortcut for `--verbose`
- Fix wrong commit id when installing beta
- Add display total tests upfront when running bashunit
- Add `BASHUNIT_` suffix to all .env config keys
- BASHUNIT_SHOW_HEADER
- BASHUNIT_HEADER_ASCII_ART
Expand Down
4 changes: 3 additions & 1 deletion docs/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ Once **bashunit** is installed, you're ready to get started.

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

Tests: 1 passed, 1 total
Assertions: 1 passed, 1 total

All tests passed
Time taken: 100 ms
```
Expand Down
46 changes: 36 additions & 10 deletions src/console_header.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
#!/bin/bash

function console_header::print_version_with_env() {
local files=("${@:-}")
local should_print_ascii="true"

if [[ "$BASHUNIT_SHOW_HEADER" != "$should_print_ascii" ]]; then
return
fi

console_header::print_version "${files[@]}"
}

function console_header::print_version() {
local files=("${@:-}")
local total_tests
total_tests=$(helpers::find_total_tests "${files[@]}")

if [[ $BASHUNIT_HEADER_ASCII_ART == true ]]; then
cat <<EOF
_ _ _
Expand All @@ -9,18 +24,21 @@ function console_header::print_version() {
| |_) | (_| \__ \ | | | |_| | | | | | |_
|_.__/ \__,_|___/_| |_|\___/|_| |_|_|\__|
EOF
printf "%s\n\n" "$BASHUNIT_VERSION"
else
printf "${_COLOR_BOLD}${_COLOR_PASSED}bashunit${_COLOR_DEFAULT} - %s\n" "$BASHUNIT_VERSION"
if [ "$total_tests" -eq 0 ]; then
printf "%s\n" "$BASHUNIT_VERSION"
else
printf "%s | Total tests: %s\n" "$BASHUNIT_VERSION" "$total_tests"
fi
return
fi
}

function console_header::print_version_with_env() {
local should_print_ascii="true"
if [[ "$BASHUNIT_SHOW_HEADER" != "$should_print_ascii" ]]; then
return
fi
console_header::print_version
if [ "$total_tests" -eq 0 ]; then
printf "${_COLOR_BOLD}${_COLOR_PASSED}bashunit${_COLOR_DEFAULT} - %s\n" "$BASHUNIT_VERSION"
else
printf "${_COLOR_BOLD}${_COLOR_PASSED}bashunit${_COLOR_DEFAULT} - %s | Total tests: %s\n"\
"$BASHUNIT_VERSION"\
"$total_tests"
fi
}

function console_header::print_help() {
Expand Down Expand Up @@ -69,3 +87,11 @@ Options:
See more: https://bashunit.typeddevs.com/command-line
EOF
}

function console_header::print_total_tests() {
local files=("${@}")

local total_tests
total_tests=$(helpers::find_total_tests "${files[@]}")
printf "\rTotal tests: %s\n" "$total_tests"
}
32 changes: 32 additions & 0 deletions src/helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,35 @@ function helpers::get_latest_tag() {
sort -Vr |
head -n 1
}

function helpers::find_total_tests() {
local files=("$@")
local total_count=0

for file in "${files[@]}"; do
local count
count=$(grep -r -E '^\s*function\s+test' "$file" --include=\*.sh 2>/dev/null | wc -l)
total_count=$((total_count + count))
done

echo "$total_count"
}

function helper::load_test_files() {
local filter=$1
local files=("${@:2}")

local test_files=()

if [[ "${#files[@]}" -eq 0 ]]; then
if [[ -n "${BASHUNIT_DEFAULT_PATH}" ]]; then
while IFS='' read -r line; do
test_files+=("$line")
done < <(helper::find_files_recursive "$BASHUNIT_DEFAULT_PATH")
fi
else
test_files=("${files[@]}")
fi

printf "%s\n" "${test_files[@]}"
}
15 changes: 13 additions & 2 deletions src/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,19 @@ function main::exec_tests() {
local filter=$1
local files=("${@:2}")

console_header::print_version_with_env
runner::load_test_files "$filter" "${files[@]}"
local test_files=()
while IFS= read -r line; do
test_files+=("$line")
done < <(helper::load_test_files "$filter" "${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

console_header::print_version_with_env "${test_files[@]}"
runner::load_test_files "$filter" "${test_files[@]}"
console_results::render_result
exit_code=$?

Expand Down
12 changes: 0 additions & 12 deletions src/runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,6 @@ function runner::load_test_files() {
local filter=$1
local files=("${@:2}") # Store all arguments starting from the second as an array

if [[ "${#files[@]}" == 0 ]]; then
if [[ -n "${BASHUNIT_DEFAULT_PATH}" ]]; then
while IFS='' read -r line; do
files+=("$line");
done < <(helper::find_files_recursive "$BASHUNIT_DEFAULT_PATH")
else
printf "%sError: At least one file path is required.%s\n" "${_COLOR_FAILED}" "${_COLOR_DEFAULT}"
console_header::print_help
exit 1
fi
fi

for test_file in "${files[@]}"; do
if [[ ! -f $test_file ]]; then
continue
Expand Down

This file was deleted.

0 comments on commit c2c414b

Please sign in to comment.