Skip to content

Commit

Permalink
Display seconds on time taken (#362)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chemaclass authored Oct 12, 2024
1 parent d9659bd commit 435cb11
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
- `LOG_PATH="out.log"`
- `LOAD_FILE="tests/bootstrap.sh"`
- Add check that git is installed to `install.sh`
- Display seconds on total time taken

## [0.17.0](https://github.com/TypedDevs/bashunit/compare/0.16.0...0.17.0) - 2024-10-01

Expand Down
15 changes: 14 additions & 1 deletion src/console_results.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/bash
# shellcheck disable=SC2155

_TOTAL_TESTS_COUNT=0

Expand Down Expand Up @@ -103,8 +104,20 @@ function console_results::print_execution_time() {
return
fi

local time=$(printf "%.0f" "$(clock::total_runtime_in_milliseconds)")

if [[ "$time" -lt 1000 ]]; then
printf "${_COLOR_BOLD}%s${_COLOR_DEFAULT}\n" \
"Time taken: $time ms"
return
fi

local time_in_seconds=$(( time / 1000 ))
local remainder_ms=$(( time % 1000 ))
local formatted_seconds=$(printf "%.2f" "$time_in_seconds.$remainder_ms")

printf "${_COLOR_BOLD}%s${_COLOR_DEFAULT}\n" \
"Time taken: $(clock::total_runtime_in_milliseconds) ms"
"Time taken: $formatted_seconds s"
}

function console_results::print_successful_test() {
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/console_results_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ function test_render_execution_time() {
console_results::render_result
)
assert_matches "Time taken: [[:digit:]]+(\.[[:digit:]]+)? ms" "$render_result"
assert_matches "Time taken: [[:digit:]]+(\.[[:digit:]]+)? (ms|s)" "$render_result"
}

function test_not_render_execution_time() {
Expand Down Expand Up @@ -304,7 +304,7 @@ function test_render_execution_time_on_osx_without_perl() {
console_results::render_result
)

assert_matches "Time taken: [[:digit:]]+(\.[[:digit:]]+)? ms" "$render_result"
assert_matches "Time taken: [[:digit:]]+(\.[[:digit:]]+)? (ms|s)" "$render_result"
}

function test_render_execution_time_on_osx_with_perl() {
Expand Down

0 comments on commit 435cb11

Please sign in to comment.