From f0a36d0dc91730e117061e849be2117a3fa3b195 Mon Sep 17 00:00:00 2001 From: Chemaclass Date: Sun, 3 May 2026 13:30:14 +0200 Subject: [PATCH] refactor(runner): extract subshell output type and formatter Pull the `[type]line` decode block out of `bashunit::runner::run_test` into two pure helpers (`extract_subshell_type`, `format_subshell_output`). The `print_line` side effect stays at the call site so command substitution does not capture the user-facing output. --- CHANGELOG.md | 1 + src/runner.sh | 32 +++++++++++++++++++------------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e8009ba4..148de752 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ - Extract `bashunit::runner::source_login_shell_profiles` and `bashunit::runner::print_verbose_test_summary` from the 320-line `run_test` body so the hot path reads top-down without inline `~/.profile` sourcing or printf scaffolding - Further trim `bashunit::runner::run_test`: extract `export_test_identity` (test ID + coverage env exports) and `apply_interpolated_title` (data-provider title interpolation) so the function opens with five named one-liners instead of an inline export/branch block - Extract `bashunit::runner::detect_runtime_error` so the 23-pattern runtime-error scan in `run_test` becomes a single named call +- Extract `bashunit::runner::extract_subshell_type` and `bashunit::runner::format_subshell_output` so the encoded-output decode block in `run_test` is two pure transforms (the `print_line` side effect stays at the call site) - Centralize all ANSI escape emission through the existing `_BASHUNIT_COLOR_*` constants. `src/coverage.sh` and the `--watch` screen-clear in `src/main.sh` no longer hardcode escape sequences (#247) - Speed up coverage report generation by collapsing the per-line non-executable pattern checks in `bashunit::coverage::is_executable_line` into a single combined `grep` invocation (#636) - Speed up coverage report generation further by combining executable + hit counting into a single source-file pass (`bashunit::coverage::compute_file_coverage`) shared across text/lcov/html reporters, removing per-line `get_line_hits` scans of the coverage data file (#636) diff --git a/src/runner.sh b/src/runner.sh index e24632dc..538b1b05 100755 --- a/src/runner.sh +++ b/src/runner.sh @@ -46,6 +46,21 @@ function bashunit::runner::apply_interpolated_title() { printf '%s' "$interpolated" } +function bashunit::runner::extract_subshell_type() { + local subshell_output=$1 + local type="${subshell_output%%]*}" + printf '%s' "${type#[}" +} + +function bashunit::runner::format_subshell_output() { + local subshell_output=$1 + local line="${subshell_output#*]}" + line=${line//\[failed\]/$'\n'} + line=${line//\[skipped\]/$'\n'} + line=${line//\[incomplete\]/$'\n'} + printf '%s' "$line" +} + function bashunit::runner::detect_runtime_error() { local runtime_output=$1 local error @@ -739,21 +754,12 @@ function bashunit::runner::run_test() { local subshell_output=$(bashunit::runner::decode_subshell_output "$test_execution_result") if [ -n "$subshell_output" ]; then - # Formatted as "[type]line" @see `bashunit::state::print_line()` - local type="${subshell_output%%]*}" # Remove everything after "]" - type="${type#[}" # Remove the leading "[" - local line="${subshell_output#*]}" # Remove everything before and including "]" - - # Replace [type] with a newline to split the messages - line=${line//\[failed\]/$'\n'} # Replace [failed] with newline - line=${line//\[skipped\]/$'\n'} # Replace [skipped] with newline - line=${line//\[incomplete\]/$'\n'} # Replace [incomplete] with newline - + local type + type=$(bashunit::runner::extract_subshell_type "$subshell_output") + subshell_output=$(bashunit::runner::format_subshell_output "$subshell_output") if ! bashunit::env::is_failures_only_enabled; then - bashunit::state::print_line "$type" "$line" + bashunit::state::print_line "$type" "$subshell_output" fi - - subshell_output=$line fi local runtime_output="${test_execution_result%%##ASSERTIONS_*}"