Skip to content

Commit 987d76a

Browse files
committed
refactor(runner): extract test identity and title interpolation
Pull the test ID/coverage env exports and the data-provider title interpolation block out of `bashunit::runner::run_test` into named helpers (`export_test_identity`, `apply_interpolated_title`). The helper returns the interpolated name so subsequent code in `run_test` keeps the same local. Pure extraction, no behavior change.
1 parent 6cec81b commit 987d76a

2 files changed

Lines changed: 27 additions & 16 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- Guard root `package.json` against accidental regression of the docs-split: assert no `dependencies`/`devDependencies`/`peerDependencies`/`scripts` blocks ever return to the published manifest
1313
- `release.sh` now treats `docs/package.json` as a first-class `RELEASE_FILES` entry, so the release flow backs it up, restores it on rollback, and stages it without inline duplication. Backup/restore preserves nested directory paths
1414
- 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
15+
- 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
1516
- 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)
1617
- 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)
1718
- 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)

src/runner.sh

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,29 @@ function bashunit::runner::source_login_shell_profiles() {
2323
[ -f ~/.profile ] && source ~/.profile 2>/dev/null || true
2424
}
2525

26+
function bashunit::runner::export_test_identity() {
27+
local test_file=$1
28+
local fn_name=$2
29+
export BASHUNIT_CURRENT_TEST_ID="$(bashunit::helper::generate_id "$fn_name")"
30+
if bashunit::env::is_coverage_enabled; then
31+
export _BASHUNIT_COVERAGE_CURRENT_TEST_FILE="$test_file"
32+
export _BASHUNIT_COVERAGE_CURRENT_TEST_FN="$fn_name"
33+
fi
34+
}
35+
36+
function bashunit::runner::apply_interpolated_title() {
37+
local fn_name=$1
38+
shift
39+
local interpolated
40+
interpolated="$(bashunit::helper::interpolate_function_name "$fn_name" "$@")"
41+
if [ "$interpolated" != "$fn_name" ]; then
42+
bashunit::state::set_current_test_interpolated_function_name "$interpolated"
43+
else
44+
bashunit::state::reset_current_test_interpolated_function_name
45+
fi
46+
printf '%s' "$interpolated"
47+
}
48+
2649
function bashunit::runner::print_verbose_test_summary() {
2750
local test_file=$1
2851
local fn_name=$2
@@ -620,24 +643,11 @@ function bashunit::runner::run_test() {
620643
shift
621644

622645
bashunit::internal_log "Running test" "$fn_name" "$*"
623-
# Export a unique test identifier so that test doubles can
624-
# create temporary files scoped per test run. This prevents
625-
# race conditions when running tests in parallel.
626-
export BASHUNIT_CURRENT_TEST_ID="$(bashunit::helper::generate_id "$fn_name")"
627-
# Export current test file and function for coverage tracking (only when coverage enabled)
628-
if bashunit::env::is_coverage_enabled; then
629-
export _BASHUNIT_COVERAGE_CURRENT_TEST_FILE="$test_file"
630-
export _BASHUNIT_COVERAGE_CURRENT_TEST_FN="$fn_name"
631-
fi
646+
bashunit::runner::export_test_identity "$test_file" "$fn_name"
632647

633648
bashunit::state::reset_test_title
634-
635-
local interpolated_fn_name="$(bashunit::helper::interpolate_function_name "$fn_name" "$@")"
636-
if [ "$interpolated_fn_name" != "$fn_name" ]; then
637-
bashunit::state::set_current_test_interpolated_function_name "$interpolated_fn_name"
638-
else
639-
bashunit::state::reset_current_test_interpolated_function_name
640-
fi
649+
local interpolated_fn_name
650+
interpolated_fn_name=$(bashunit::runner::apply_interpolated_title "$fn_name" "$@")
641651
local current_assertions_failed="$_BASHUNIT_ASSERTIONS_FAILED"
642652
local current_assertions_snapshot="$_BASHUNIT_ASSERTIONS_SNAPSHOT"
643653
local current_assertions_incomplete="$_BASHUNIT_ASSERTIONS_INCOMPLETE"

0 commit comments

Comments
 (0)