Skip to content

Commit 4a31f9f

Browse files
authored
refactor(runner): extract runtime error pattern scan (#655)
1 parent d5e85da commit 4a31f9f

2 files changed

Lines changed: 26 additions & 19 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
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
1515
- 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
16+
- Extract `bashunit::runner::detect_runtime_error` so the 23-pattern runtime-error scan in `run_test` becomes a single named call
1617
- 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)
1718
- 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)
1819
- 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: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,29 @@ function bashunit::runner::apply_interpolated_title() {
4646
printf '%s' "$interpolated"
4747
}
4848

49+
function bashunit::runner::detect_runtime_error() {
50+
local runtime_output=$1
51+
local error
52+
for error in "command not found" "unbound variable" "permission denied" \
53+
"no such file or directory" "syntax error" "bad substitution" \
54+
"division by 0" "cannot allocate memory" "bad file descriptor" \
55+
"segmentation fault" "illegal option" "argument list too long" \
56+
"readonly variable" "missing keyword" "killed" \
57+
"cannot execute binary file" "invalid arithmetic operator" \
58+
"ambiguous redirect" "integer expression expected" \
59+
"too many arguments" "value too great" \
60+
"not a valid identifier" "unexpected EOF"; do
61+
case "$runtime_output" in
62+
*"$error"*)
63+
local runtime_error="${runtime_output#*: }"
64+
printf '%s' "${runtime_error//$'\n'/}"
65+
return 0
66+
;;
67+
esac
68+
done
69+
printf ''
70+
}
71+
4972
function bashunit::runner::print_verbose_test_summary() {
5073
local test_file=$1
5174
local fn_name=$2
@@ -735,25 +758,8 @@ function bashunit::runner::run_test() {
735758

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

738-
local runtime_error=""
739-
local error=""
740-
for error in "command not found" "unbound variable" "permission denied" \
741-
"no such file or directory" "syntax error" "bad substitution" \
742-
"division by 0" "cannot allocate memory" "bad file descriptor" \
743-
"segmentation fault" "illegal option" "argument list too long" \
744-
"readonly variable" "missing keyword" "killed" \
745-
"cannot execute binary file" "invalid arithmetic operator" \
746-
"ambiguous redirect" "integer expression expected" \
747-
"too many arguments" "value too great" \
748-
"not a valid identifier" "unexpected EOF"; do
749-
case "$runtime_output" in
750-
*"$error"*)
751-
runtime_error="${runtime_output#*: }" # Remove everything up to and including ": "
752-
runtime_error=${runtime_error//$'\n'/} # Remove all newlines using parameter expansion
753-
break
754-
;;
755-
esac
756-
done
761+
local runtime_error
762+
runtime_error=$(bashunit::runner::detect_runtime_error "$runtime_output")
757763

758764
bashunit::runner::parse_result "$fn_name" "$test_execution_result" "$@"
759765

0 commit comments

Comments
 (0)