diff --git a/CHANGELOG.md b/CHANGELOG.md index 7abf3c78..d413cece 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## [Unreleased](https://github.com/TypedDevs/bashunit/compare/0.14.0...main) -- Nothing yet... +- Add assertions to log file ## [0.14.0](https://github.com/TypedDevs/bashunit/compare/0.13.0...0.14.0) - 2024-07-14 diff --git a/src/logger.sh b/src/logger.sh index 02042777..85bb03d7 100755 --- a/src/logger.sh +++ b/src/logger.sh @@ -1,34 +1,37 @@ #!/bin/bash +TEST_FILES=() TEST_NAMES=() TEST_STATUSES=() TEST_DURATIONS=() +TEST_ASSERTIONS=() function logger::test_snapshot() { - logger::log "$1" "$2" "$3" "snapshot" + logger::log "$1" "$2" "$3" "$4" "snapshot" } function logger::test_incomplete() { - logger::log "$1" "$2" "$3" "incomplete" + logger::log "$1" "$2" "$3" "$4" "incomplete" } function logger::test_skipped() { - logger::log "$1" "$2" "$3" "skipped" + logger::log "$1" "$2" "$3" "$4" "skipped" } function logger::test_passed() { - logger::log "$1" "$2" "$3" "passed" + logger::log "$1" "$2" "$3" "$4" "passed" } function logger::test_failed() { - logger::log "$1" "$2" "$3" "failed" + logger::log "$1" "$2" "$3" "$4" "failed" } function logger::log() { local file="$1" local test_name="$2" local start_time="$3" - local status="$4" + local assertions="$4" + local status="$5" local end_time end_time=$(clock::now) @@ -37,6 +40,7 @@ function logger::log() { TEST_FILES+=("$file") TEST_NAMES+=("$test_name") TEST_STATUSES+=("$status") + TEST_ASSERTIONS+=("$assertions") TEST_DURATIONS+=("$duration") } @@ -66,12 +70,14 @@ function logger::generate_junit_xml() { for i in "${!TEST_NAMES[@]}"; do local file="${TEST_FILES[$i]}" local name="${TEST_NAMES[$i]}" + local assertions="${TEST_ASSERTIONS[$i]}" local status="${TEST_STATUSES[$i]}" local test_time="${TEST_DURATIONS[$i]}" echo " " echo " " done diff --git a/src/runner.sh b/src/runner.sh index e67442f1..fb716a0e 100755 --- a/src/runner.sh +++ b/src/runner.sh @@ -189,17 +189,19 @@ function runner::run_test() { tail -n 1 |\ sed -E -e 's/(.*)##ASSERTIONS_FAILED=.*/\1/g'\ ) + local total_assertions + total_assertions="$(state::calculate_total_assertions "$test_execution_result")" if [[ -n $runtime_error ]]; then state::add_tests_failed console_results::print_error_test "$function_name" "$runtime_error" - logger::test_failed "$test_file" "$function_name" "$start_time" + logger::test_failed "$test_file" "$function_name" "$start_time" "$total_assertions" return fi if [[ "$current_assertions_failed" != "$(state::get_assertions_failed)" ]]; then state::add_tests_failed - logger::test_failed "$test_file" "$function_name" "$start_time" + logger::test_failed "$test_file" "$function_name" "$start_time" "$total_assertions" if [ "$STOP_ON_FAILURE" = true ]; then exit 1 @@ -211,19 +213,19 @@ function runner::run_test() { if [[ "$current_assertions_snapshot" != "$(state::get_assertions_snapshot)" ]]; then state::add_tests_snapshot console_results::print_snapshot_test "$function_name" - logger::test_snapshot "$test_file" "$function_name" "$start_time" + logger::test_snapshot "$test_file" "$function_name" "$start_time" "$total_assertions" return fi if [[ "$current_assertions_incomplete" != "$(state::get_assertions_incomplete)" ]]; then state::add_tests_incomplete - logger::test_incomplete "$test_file" "$function_name" "$start_time" + logger::test_incomplete "$test_file" "$function_name" "$start_time" "$total_assertions" return fi if [[ "$current_assertions_skipped" != "$(state::get_assertions_skipped)" ]]; then state::add_tests_skipped - logger::test_skipped "$test_file" "$function_name" "$start_time" + logger::test_skipped "$test_file" "$function_name" "$start_time" "$total_assertions" return fi @@ -232,7 +234,7 @@ function runner::run_test() { console_results::print_successful_test "${label}" "$@" state::add_tests_passed - logger::test_passed "$test_file" "$function_name" "$start_time" + logger::test_passed "$test_file" "$function_name" "$start_time" "$total_assertions" } function runner::run_set_up() { diff --git a/src/state.sh b/src/state.sh index 74a31186..d10c472b 100644 --- a/src/state.sh +++ b/src/state.sh @@ -141,3 +141,17 @@ function state::export_assertions_count() { ##ASSERTIONS_SNAPSHOT=$_ASSERTIONS_SNAPSHOT\ ##" } + +function state::calculate_total_assertions() { + local input="$1" + local total=0 + + local numbers + numbers=$(echo "$input" | grep -oE '##ASSERTIONS_\w+=[0-9]+' | grep -oE '[0-9]+') + + for number in $numbers; do + ((total += number)) + done + + echo $total +} diff --git a/tests/unit/state_test.sh b/tests/unit/state_test.sh index 096192de..9a7f236a 100644 --- a/tests/unit/state_test.sh +++ b/tests/unit/state_test.sh @@ -276,3 +276,13 @@ ASSERTIONS_INCOMPLETE=12##\ ASSERTIONS_SNAPSHOT=33##"\ "$export_assertions_count" } + +function test_calculate_total_assertions() { + local input="##ASSERTIONS_FAILED=1\ + ##ASSERTIONS_PASSED=2\ + ##ASSERTIONS_SKIPPED=3\ + ##ASSERTIONS_INCOMPLETE=4\ + ##ASSERTIONS_SNAPSHOT=5##" + + assert_equals 15 "$(state::calculate_total_assertions "$input")" +}