Skip to content

Commit

Permalink
Merge pull request #376 from TypedDevs/feat/improve-render-file-heade…
Browse files Browse the repository at this point in the history
…r-on-verbose

Improve render file header on verbose
  • Loading branch information
Chemaclass authored Oct 14, 2024
2 parents beb3799 + 238d570 commit 4c33046
Show file tree
Hide file tree
Showing 18 changed files with 55 additions and 56 deletions.
25 changes: 20 additions & 5 deletions src/runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,7 @@ function runner::call_test_functions() {
return
fi

if ! env::is_simple_output_enabled && ! parallel::is_enabled; then
echo "Running $script"
fi

runner::render_running_file_header
helper::check_duplicate_functions "$script" || true

for function_name in "${functions_to_run[@]}"; do
Expand Down Expand Up @@ -110,6 +107,22 @@ function runner::call_test_functions() {
done
}

function runner::render_running_file_header() {
if parallel::is_enabled; then
return
fi

if ! env::is_simple_output_enabled; then
if env::is_verbose_enabled; then
printf "\n${_COLOR_BOLD}%s${_COLOR_DEFAULT}\n" "Running $script"
else
printf "${_COLOR_BOLD}%s${_COLOR_DEFAULT}\n" "Running $script"
fi
elif env::is_verbose_enabled; then
printf "\n\n${_COLOR_BOLD}%s${_COLOR_DEFAULT}" "Running $script"
fi
}

function runner::run_test() {
local start_time
start_time=$(clock::now)
Expand Down Expand Up @@ -157,6 +170,8 @@ function runner::run_test() {
printf "%s\n" "File: $test_file"
printf "%s\n" "Function: $function_name"
printf "%s\n" "Duration: $duration ms"
local raw_text=${test_execution_result%%##ASSERTIONS_*}
[[ -n $raw_text ]] && printf "%s" "Raw text: ${test_execution_result%%##ASSERTIONS_*}"
printf "%s\n" "##ASSERTIONS_${test_execution_result#*##ASSERTIONS_}"
printf '%*s\n' "$TERMINAL_WIDTH" '' | tr ' ' '-'
fi
Expand All @@ -173,7 +188,7 @@ function runner::run_test() {
subshell_output=$line
fi

local runtime_output="${test_execution_result%%##ASSERTIONS_=*}"
local runtime_output="${test_execution_result%%##ASSERTIONS_*}"

local runtime_error=""
for error in "command not found" "unbound variable" "permission denied" \
Expand Down
45 changes: 15 additions & 30 deletions tests/acceptance/bashunit_execution_error_test.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/bash
# shellcheck disable=SC2155
set -euo pipefail

function set_up_before_script() {
Expand All @@ -7,25 +8,10 @@ function set_up_before_script() {

function test_bashunit_when_a_execution_error() {
local test_file=./tests/acceptance/fixtures/test_bashunit_when_a_execution_error.sh
local fixture_start fixture_end
local color_default color_red color_dim color_bold

color_default="$(sgr 0)"
color_bold="$(sgr 1)"
color_dim="$(sgr 2)"
color_red="$(sgr 31)"

function format_fail_title() {
printf "\n%s%s%s%s" "${color_red}" "$1" "${color_default}" "$2"
}

function format_expect_title() {
printf "\n %s%s%s" "${color_dim}" "$1" "${color_default}"
}

function format_expect_value() {
printf " %s%s%s" "${color_bold}" "$1" "${color_default}"
}
local color_default="$(sgr 0)"
local color_bold="$(sgr 1)"
local color_dim="$(sgr 2)"
local color_red="$(sgr 31)"

function format_summary_title() {
printf "\n%s%s%s" "${color_dim}" "$1" "${color_default}"
Expand All @@ -35,25 +21,24 @@ function test_bashunit_when_a_execution_error() {
printf " %s%s%s%s" "${color_red}" "$1" "${color_default}" "$2"
}

fixture_start=$(
printf "Running ./tests/acceptance/fixtures/test_bashunit_when_a_execution_error.sh"
format_fail_title "✗ Failed" ": Error"
format_expect_title "Expected"
format_expect_value "'127'"
format_expect_title "to be exactly"
format_expect_value "'1'"
local fixture_start=$(
printf "%sRunning ./tests/acceptance/fixtures/test_bashunit_when_a_execution_error.sh%s\n" \
"${color_bold}" "${color_default}"
printf "%s✗ Error%s: Error\n" "${color_red}" "${color_default}"
printf " %sline 4: invalid_function_name: command not found%s\n" "${color_dim}" "${color_default}"
)
fixture_end=$(
local fixture_end=$(
format_summary_title "Tests: "
format_summary_value "1 failed" ", 1 total"
format_summary_title "Assertions:"
format_summary_value "1 failed" ", 1 total"
format_summary_value "0 failed" ", 0 total"
)

todo "Add snapshots with regex to assert this test (part of the error message is localized)"
todo "Add snapshots with simple/verbose modes as in bashunit_pass_test and bashunit_fail_test"

assert_contains "$fixture_start" "$(./bashunit --no-parallel --env "$TEST_ENV_FILE" "$test_file")"
assert_contains "$fixture_end" "$(./bashunit --no-parallel --env "$TEST_ENV_FILE" "$test_file")"
local actual="$(./bashunit --no-parallel --detailed --env "$TEST_ENV_FILE" "$test_file")"
assert_contains "$fixture_start" "$actual"
assert_contains "$fixture_end" "$actual"
assert_general_error "$(./bashunit --no-parallel --env "$TEST_ENV_FILE" "$test_file")"
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/bin/bash

function test_error() {
invalid_function_name
assert_general_error
invalid_function_name arg1 arg2
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Running ./tests/acceptance/fixtures/test_bashunit_when_a_test_fail.sh
Running ./tests/acceptance/fixtures/test_bashunit_when_a_test_fail.sh
✓ Passed: Assert same
✓ Passed: Assert contains
✗ Failed: Assert failing
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Running ./tests/acceptance/fixtures/test_bashunit_when_a_test_fail.sh
Running ./tests/acceptance/fixtures/test_bashunit_when_a_test_fail.sh
✓ Passed: Assert same
✓ Passed: Assert contains
✗ Failed: Assert failing
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Running ./tests/acceptance/fixtures/tests_path/a_test.sh
Running ./tests/acceptance/fixtures/tests_path/a_test.sh
✓ Passed: Assert greater and less than
✓ Passed: Assert empty
Running ./tests/acceptance/fixtures/tests_path/other_test.sh
Running ./tests/acceptance/fixtures/tests_path/other_test.sh
✓ Passed: Assert same
✓ Passed: Assert contains

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Running ./tests/acceptance/fixtures/tests_path/a_test.sh
Running ./tests/acceptance/fixtures/tests_path/a_test.sh
✓ Passed: Assert greater and less than
✓ Passed: Assert empty
Running ./tests/acceptance/fixtures/tests_path/other_test.sh
Running ./tests/acceptance/fixtures/tests_path/other_test.sh
✓ Passed: Assert same
✓ Passed: Assert contains

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Running ./tests/acceptance/fixtures/tests_path/a_test.sh
Running ./tests/acceptance/fixtures/tests_path/a_test.sh
✓ Passed: Assert greater and less than
✓ Passed: Assert empty

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Running ./tests/acceptance/fixtures/test_bashunit_when_log_junit.sh
Running ./tests/acceptance/fixtures/test_bashunit_when_log_junit.sh
✓ Passed: Success
✗ Failed: Failure
Expected '2'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Running ./tests/acceptance/fixtures/test_bashunit_when_log_junit.sh
Running ./tests/acceptance/fixtures/test_bashunit_when_log_junit.sh
✓ Passed: Success
✗ Failed: Failure
Expected '2'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Running ./tests/acceptance/fixtures/test_bashunit_when_a_test_passes.sh
Running ./tests/acceptance/fixtures/test_bashunit_when_a_test_passes.sh
✓ Passed: Assert same
✓ Passed: Assert contains
✓ Passed: Assert greater and less than
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Running ./tests/acceptance/fixtures/test_bashunit_when_a_test_passes.sh
Running ./tests/acceptance/fixtures/test_bashunit_when_a_test_passes.sh
✓ Passed: Assert same
✓ Passed: Assert contains
✓ Passed: Assert greater and less than
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Running tests/acceptance/fixtures/tests_path/a_test.sh
Running tests/acceptance/fixtures/tests_path/a_test.sh
✓ Passed: Assert greater and less than
✓ Passed: Assert empty
Running tests/acceptance/fixtures/tests_path/other_test.sh
Running tests/acceptance/fixtures/tests_path/other_test.sh
✓ Passed: Assert same
✓ Passed: Assert contains

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Running tests/acceptance/fixtures/tests_path/a_test.sh
Running tests/acceptance/fixtures/tests_path/a_test.sh
✓ Passed: Assert greater and less than
✓ Passed: Assert empty
Running tests/acceptance/fixtures/tests_path/other_test.sh
Running tests/acceptance/fixtures/tests_path/other_test.sh
✓ Passed: Assert same
✓ Passed: Assert contains

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Running ./tests/acceptance/fixtures/test_bashunit_when_report_html.sh
Running ./tests/acceptance/fixtures/test_bashunit_when_report_html.sh
✓ Passed: Success
✗ Failed: Fail
Expected 'to be empty'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Running ./tests/acceptance/fixtures/test_bashunit_when_report_html.sh
Running ./tests/acceptance/fixtures/test_bashunit_when_report_html.sh
✓ Passed: Success
✗ Failed: Fail
Expected 'to be empty'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Running ./tests/acceptance/fixtures/test_bashunit_when_stop_on_failure.sh
Running ./tests/acceptance/fixtures/test_bashunit_when_stop_on_failure.sh
✓ Passed: A success
✗ Failed: B error
Expected '1'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Running ./tests/acceptance/fixtures/test_bashunit_when_stop_on_failure.sh
Running ./tests/acceptance/fixtures/test_bashunit_when_stop_on_failure.sh
✓ Passed: A success
✗ Failed: B error
Expected '1'
Expand Down

0 comments on commit 4c33046

Please sign in to comment.