Skip to content

Commit ef627e1

Browse files
committed
refactor(compat): inline regex matching and remove bashunit::regex_match wrapper
1 parent eda74cc commit ef627e1

8 files changed

Lines changed: 70 additions & 50 deletions

File tree

src/assert.sh

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,12 @@ function bashunit::run_command_or_eval() {
8787
if [[ "$cmd" =~ $_re ]]; then
8888
eval "${cmd#eval }" &>/dev/null
8989
else
90-
_re='^alias'
91-
if [[ "$(command -v "$cmd")" =~ $_re ]]; then
92-
eval "$cmd" &>/dev/null
93-
else
94-
"$cmd" &>/dev/null
90+
_re='^alias'
91+
if [[ "$(command -v "$cmd")" =~ $_re ]]; then
92+
eval "$cmd" &>/dev/null
93+
else
94+
"$cmd" &>/dev/null
95+
fi
9596
fi
9697
return $?
9798
}

src/benchmark.sh

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,34 @@ function bashunit::benchmark::parse_annotations() {
1616
local annotation
1717
annotation=$(awk "/function[[:space:]]+${fn_name}[[:space:]]*\(/ {print prev; exit} {prev=\$0}" "$script")
1818

19-
if bashunit::regex_match "$annotation" '@revs=([0-9]+)'; then
20-
revs="${BASH_REMATCH[1]}"
21-
elif bashunit::regex_match "$annotation" '@revolutions=([0-9]+)'; then
19+
local _re='@revs=([0-9]+)'
20+
if [[ "$annotation" =~ $_re ]]; then
2221
revs="${BASH_REMATCH[1]}"
22+
else
23+
_re='@revolutions=([0-9]+)'
24+
if [[ "$annotation" =~ $_re ]]; then
25+
revs="${BASH_REMATCH[1]}"
26+
fi
2327
fi
2428

25-
if bashunit::regex_match "$annotation" '@its=([0-9]+)'; then
26-
its="${BASH_REMATCH[1]}"
27-
elif bashunit::regex_match "$annotation" '@iterations=([0-9]+)'; then
29+
_re='@its=([0-9]+)'
30+
if [[ "$annotation" =~ $_re ]]; then
2831
its="${BASH_REMATCH[1]}"
32+
else
33+
_re='@iterations=([0-9]+)'
34+
if [[ "$annotation" =~ $_re ]]; then
35+
its="${BASH_REMATCH[1]}"
36+
fi
2937
fi
3038

31-
if bashunit::regex_match "$annotation" '@max_ms=([0-9.]+)'; then
32-
max_ms="${BASH_REMATCH[1]}"
33-
elif bashunit::regex_match "$annotation" '@max_ms=([0-9.]+)'; then
39+
_re='@max_ms=([0-9.]+)'
40+
if [[ "$annotation" =~ $_re ]]; then
3441
max_ms="${BASH_REMATCH[1]}"
42+
else
43+
_re='@max_ms=([0-9.]+)'
44+
if [[ "$annotation" =~ $_re ]]; then
45+
max_ms="${BASH_REMATCH[1]}"
46+
fi
3547
fi
3648

3749
if [[ -n "$max_ms" ]]; then

src/console_header.sh

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,27 +63,27 @@ function bashunit::console_header::print_help() {
6363
Usage: bashunit <command> [arguments] [options]
6464
6565
Commands:
66-
test [path] Run tests (default command)
67-
bench [path] Run benchmarks
68-
assert <fn> <args> Run standalone assertion
69-
doc [filter] Display assertion documentation
70-
init [dir] Initialize a new test directory
71-
learn Start interactive tutorial
72-
upgrade Upgrade bashunit to latest version
66+
test [path] Run tests (default command)
67+
bench [path] Run benchmarks
68+
assert <fn> <args> Run standalone assertion
69+
doc [filter] Display assertion documentation
70+
init [dir] Initialize a new test directory
71+
learn Start interactive tutorial
72+
upgrade Upgrade bashunit to latest version
7373
7474
Global Options:
75-
-h, --help Show this help message
76-
-v, --version Display the current version
75+
-h, --help Show this help message
76+
-v, --version Display the current version
7777
7878
Run 'bashunit <command> --help' for command-specific options.
7979
8080
Examples:
81-
bashunit test tests/ Run all tests in directory
82-
bashunit tests/ Run all tests (shorthand)
83-
bashunit bench Run all benchmarks
84-
bashunit assert equals "foo" "foo" Run standalone assertion
85-
bashunit doc contains Show docs for 'contains' assertions
86-
bashunit init Initialize test directory
81+
bashunit test tests/ Run all tests in directory
82+
bashunit tests/ Run all tests (shorthand)
83+
bashunit bench Run all benchmarks
84+
bashunit assert equals "foo" "foo" Run standalone assertion
85+
bashunit doc contains Show docs for 'contains' assertions
86+
bashunit init Initialize test directory
8787
8888
More info: https://bashunit.typeddevs.com/command-line
8989
EOF

src/coverage.sh

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -358,22 +358,27 @@ function bashunit::coverage::is_executable_line() {
358358
[[ -z "${line// /}" ]] && return 1
359359

360360
# Skip comment-only lines (including shebang)
361-
bashunit::regex_match "$line" '^[[:space:]]*#' && return 1
361+
local _re='^[[:space:]]*#'
362+
[[ "$line" =~ $_re ]] && return 1
362363

363364
# Skip function declaration lines (but not single-line functions with body)
364-
bashunit::regex_match "$line" "$_BASHUNIT_COVERAGE_FUNC_PATTERN" && return 1
365+
[[ "$line" =~ $_BASHUNIT_COVERAGE_FUNC_PATTERN ]] && return 1
365366

366367
# Skip lines with only braces
367-
bashunit::regex_match "$line" '^[[:space:]]*[\{\}][[:space:]]*$' && return 1
368+
_re='^[[:space:]]*[\{\}][[:space:]]*$'
369+
[[ "$line" =~ $_re ]] && return 1
368370

369371
# Skip control flow keywords (then, else, fi, do, done, esac, in, ;;, ;&, ;;&)
370-
bashunit::regex_match "$line" '^[[:space:]]*(then|else|fi|do|done|esac|in|;;|;;&|;&)[[:space:]]*(#.*)?$' && return 1
372+
_re='^[[:space:]]*(then|else|fi|do|done|esac|in|;;|;;&|;&)[[:space:]]*(#.*)?$'
373+
[[ "$line" =~ $_re ]] && return 1
371374

372375
# Skip case patterns like "--option)" or "*)"
373-
bashunit::regex_match "$line" '^[[:space:]]*[^\)]+\)[[:space:]]*$' && return 1
376+
_re='^[[:space:]]*[^\)]+\)[[:space:]]*$'
377+
[[ "$line" =~ $_re ]] && return 1
374378

375379
# Skip standalone ) for arrays/subshells
376-
bashunit::regex_match "$line" '^[[:space:]]*\)[[:space:]]*(#.*)?$' && return 1
380+
_re='^[[:space:]]*\)[[:space:]]*(#.*)?$'
381+
[[ "$line" =~ $_re ]] && return 1
377382

378383
return 0
379384
}
@@ -494,10 +499,14 @@ function bashunit::coverage::extract_functions() {
494499
local fn_name=""
495500

496501
# Match: function name() or function name {
497-
if bashunit::regex_match "$line" '^[[:space:]]*(function[[:space:]]+)?([a-zA-Z_][a-zA-Z0-9_:]*)[[:space:]]*\(\)[[:space:]]*\{?[[:space:]]*(#.*)?$'; then
498-
fn_name="${BASH_REMATCH[2]}"
499-
elif bashunit::regex_match "$line" '^[[:space:]]*(function[[:space:]]+)([a-zA-Z_][a-zA-Z0-9_:]*)[[:space:]]*\{[[:space:]]*(#.*)?$'; then
502+
local _re='^[[:space:]]*(function[[:space:]]+)?([a-zA-Z_][a-zA-Z0-9_:]*)[[:space:]]*\(\)[[:space:]]*\{?[[:space:]]*(#.*)?$'
503+
if [[ "$line" =~ $_re ]]; then
500504
fn_name="${BASH_REMATCH[2]}"
505+
else
506+
_re='^[[:space:]]*(function[[:space:]]+)([a-zA-Z_][a-zA-Z0-9_:]*)[[:space:]]*\{[[:space:]]*(#.*)?$'
507+
if [[ "$line" =~ $_re ]]; then
508+
fn_name="${BASH_REMATCH[2]}"
509+
fi
501510
fi
502511

503512
if [[ -n "$fn_name" ]]; then
@@ -512,7 +521,8 @@ function bashunit::coverage::extract_functions() {
512521
brace_count=$((brace_count + ${#open_braces} - ${#close_braces}))
513522

514523
# Single-line function
515-
if [[ $brace_count -eq 0 ]] && bashunit::regex_match "$line" '\{' && bashunit::regex_match "$line" '\}'; then
524+
local _re_ob='\{' _re_cb='\}'
525+
if [[ $brace_count -eq 0 ]] && [[ "$line" =~ $_re_ob ]] && [[ "$line" =~ $_re_cb ]]; then
516526
echo "${current_fn}:${fn_start}:${lineno}"
517527
in_function=0
518528
current_fn=""

src/helpers.sh

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,6 @@
22

33
declare -r BASHUNIT_GIT_REPO="https://github.com/TypedDevs/bashunit"
44

5-
#
6-
# Helper function for regex matching that works correctly in Bash 3.0+
7-
# In Bash < 3.2, regex matching with literal patterns doesn't work properly,
8-
# so we need to use this function instead of direct [[ ... =~ ... ]] checks.
9-
#
10-
# @param $1 string The string to match
11-
# @param $2 string The regex pattern
125
#
136
# Walks up the call stack to find the first function that looks like a test function.
147
# A test function is one that starts with "test_" or "test" (camelCase).

src/main.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,8 @@ function bashunit::main::handle_assert_exit_code() {
694694
last_line=$(echo "$output" | tail -n 1)
695695
if echo "$last_line" | grep -q 'inner_exit_code:[0-9]*'; then
696696
inner_exit_code=$(echo "$last_line" | grep -o 'inner_exit_code:[0-9]*' | cut -d':' -f2)
697-
if ! bashunit::regex_match "$inner_exit_code" '^[0-9]+$'; then
697+
local _re='^[0-9]+$'
698+
if ! [[ "$inner_exit_code" =~ $_re ]]; then
698699
inner_exit_code=1
699700
fi
700701
output=$(echo "$output" | sed '$d')

src/runner.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,9 @@ function bashunit::runner::parse_data_provider_args() {
219219

220220
# Check for shell metacharacters that would break eval or cause globbing
221221
local has_metachar=false
222-
if bashunit::regex_match "$input" '[^\\][\|\&\;\*]' || bashunit::regex_match "$input" '^[\|\&\;\*]'; then
222+
local _re1='[^\\][\|\&\;\*]'
223+
local _re2='^[\|\&\;\*]'
224+
if [[ "$input" =~ $_re1 ]] || [[ "$input" =~ $_re2 ]]; then
223225
has_metachar=true
224226
fi
225227

@@ -807,7 +809,7 @@ function bashunit::runner::parse_result_sync() {
807809
local test_exit_code=0
808810

809811
# Use pre-compiled regex constant
810-
if bashunit::regex_match "$result_line" "$_BASHUNIT_RUNNER_PARSE_RESULT_REGEX"; then
812+
if [[ "$result_line" =~ $_BASHUNIT_RUNNER_PARSE_RESULT_REGEX ]]; then
811813
assertions_failed="${BASH_REMATCH[1]}"
812814
assertions_passed="${BASH_REMATCH[2]}"
813815
assertions_skipped="${BASH_REMATCH[3]}"

src/test_doubles.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ function assert_have_been_called_with() {
101101
shift
102102

103103
local index=""
104-
if bashunit::regex_match "${!#}" '^[0-9]+$'; then
104+
local _re='^[0-9]+$'
105+
if [[ "${!#}" =~ $_re ]]; then
105106
index=${!#}
106107
set -- "${@:1:$#-1}"
107108
fi

0 commit comments

Comments
 (0)