Skip to content

Commit 834bd32

Browse files
committed
fix(compat): convert all literal regex patterns to variables
Store all regex patterns in variables before use to ensure Bash 3.0 compatibility. In Bash < 3.2, regex matching with literal patterns doesn't work properly. Fixed patterns in: - src/doc.sh: code fence pattern - src/main.sh: digit validation pattern - src/helpers.sh: test name patterns and variable name validation - src/runner.sh: metacharacter detection patterns - src/clock.sh: digit validation pattern - src/assert.sh: eval and alias detection patterns All patterns now follow the safe approach: local _pattern='...' [[ $var =~ $_pattern ]]
1 parent e9be71b commit 834bd32

6 files changed

Lines changed: 24 additions & 8 deletions

File tree

src/assert.sh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,12 @@ function assert_false() {
8383
function bashunit::run_command_or_eval() {
8484
local cmd="$1"
8585

86-
if [[ "$cmd" =~ ^eval ]]; then
86+
# Patterns stored in variable for Bash 3.0 compatibility
87+
local _eval_pattern='^eval'
88+
local _alias_pattern='^alias'
89+
if [[ "$cmd" =~ $_eval_pattern ]]; then
8790
eval "${cmd#eval }" &>/dev/null
88-
elif [[ "$(command -v "$cmd")" =~ ^alias ]]; then
91+
elif [[ "$(command -v "$cmd")" =~ $_alias_pattern ]]; then
8992
eval "$cmd" &>/dev/null
9093
else
9194
"$cmd" &>/dev/null

src/clock.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ function bashunit::clock::_choose_impl() {
4545
if ! bashunit::check_os::is_macos && ! bashunit::check_os::is_alpine; then
4646
local result
4747
result=$(date +%s%N 2>/dev/null)
48-
if [[ "$result" != *N && "$result" =~ ^[0-9]+$ ]]; then
48+
# Pattern stored in variable for Bash 3.0 compatibility
49+
local _digit_pattern='^[0-9]+$'
50+
if [[ "$result" != *N && "$result" =~ $_digit_pattern ]]; then
4951
_BASHUNIT_CLOCK_NOW_IMPL="date"
5052
return 0
5153
fi

src/doc.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ function bashunit::doc::print_asserts() {
3232
fi
3333

3434
if ((should_print)); then
35-
if [[ "$line" =~ ^\`\`\` ]]; then
35+
# Pattern stored in variable for Bash 3.0 compatibility
36+
local _code_fence_pattern='^\`\`\`'
37+
if [[ "$line" =~ $_code_fence_pattern ]]; then
3638
echo "--------------"
3739
echo "$docstring"
3840
should_print=0

src/helpers.sh

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ function bashunit::helper::find_test_function_name() {
3131
for ((i = 0; i < ${#FUNCNAME[@]}; i++)); do
3232
local fn="${FUNCNAME[$i]}"
3333
# Check if function starts with "test_" or "test" followed by uppercase
34-
if [[ "$fn" == test_* ]] || [[ "$fn" =~ ^test[A-Z] ]]; then
34+
# Pattern stored in variable for Bash 3.0 compatibility
35+
local _test_camel_pattern='^test[A-Z]'
36+
if [[ "$fn" == test_* ]] || [[ "$fn" =~ $_test_camel_pattern ]]; then
3537
echo "$fn"
3638
return
3739
fi
@@ -270,7 +272,9 @@ function bashunit::helper::normalize_variable_name() {
270272

271273
normalized_string="${input_string//[^a-zA-Z0-9_]/_}"
272274

273-
if [[ ! $normalized_string =~ ^[a-zA-Z_] ]]; then
275+
# Pattern stored in variable for Bash 3.0 compatibility
276+
local _valid_start_pattern='^[a-zA-Z_]'
277+
if [[ ! $normalized_string =~ $_valid_start_pattern ]]; then
274278
normalized_string="_$normalized_string"
275279
fi
276280

src/main.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,9 @@ 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 ! [[ $inner_exit_code =~ ^[0-9]+$ ]]; then
697+
# Pattern stored in variable for Bash 3.0 compatibility
698+
local _digit_pattern='^[0-9]+$'
699+
if ! [[ $inner_exit_code =~ $_digit_pattern ]]; then
698700
inner_exit_code=1
699701
fi
700702
output=$(echo "$output" | sed '$d')

src/runner.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,11 @@ function bashunit::runner::parse_data_provider_args() {
215215
local args_count=0
216216

217217
# Check for shell metacharacters that would break eval or cause globbing
218+
# Patterns stored in variable for Bash 3.0 compatibility
219+
local _metachar_mid_pattern='[^\\][\|\&\;\*]'
220+
local _metachar_start_pattern='^[\|\&\;\*]'
218221
local has_metachar=false
219-
if [[ "$input" =~ [^\\][\|\&\;\*] ]] || [[ "$input" =~ ^[\|\&\;\*] ]]; then
222+
if [[ "$input" =~ $_metachar_mid_pattern ]] || [[ "$input" =~ $_metachar_start_pattern ]]; then
220223
has_metachar=true
221224
fi
222225

0 commit comments

Comments
 (0)