Skip to content

Commit 76db26f

Browse files
committed
chore: adjust code for bash3.0 compatibility
1 parent baef9bf commit 76db26f

File tree

3 files changed

+47
-17
lines changed

3 files changed

+47
-17
lines changed

src/helpers.sh

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ function helper::get_functions_to_run() {
138138
filtered_functions+=" $fn"
139139
fi
140140
done
141+
unset fn
141142

142143
echo "${filtered_functions# }"
143144
}
@@ -282,23 +283,27 @@ function helper::find_total_tests() {
282283
for fn_name in "${functions_to_run[@]}"; do
283284
local provider_data
284285
provider_data=()
286+
local provider_count=0
285287
local provider_output
286288
provider_output="$(helper::get_provider_data "$fn_name" "$file")"
287289
if [[ -n "$provider_output" ]]; then
288290
local line
289291
while IFS=" " read -r line; do
290-
provider_data+=("$line")
292+
provider_data[$provider_count]="$line"
293+
provider_count=$((provider_count + 1))
291294
done << EOF
292295
$provider_output
293296
EOF
297+
unset line
294298
fi
295299
296-
if [[ "${#provider_data[@]}" -eq 0 ]]; then
300+
if [[ $provider_count -eq 0 ]]; then
297301
count=$((count + 1))
298302
else
299-
count=$((count + ${#provider_data[@]}))
303+
count=$((count + provider_count))
300304
fi
301305
done
306+
unset fn_name
302307
fi
303308
304309
echo "$count"
@@ -320,8 +325,9 @@ function helper::load_test_files() {
320325
if [[ "${#files[@]}" -eq 0 ]]; then
321326
if [[ -n "${BASHUNIT_DEFAULT_PATH}" ]]; then
322327
while IFS='' read -r line; do
323-
test_files+=("$line")
328+
test_files=("${test_files[@]}" "$line")
324329
done < <(helper::find_files_recursive "$BASHUNIT_DEFAULT_PATH")
330+
unset line
325331
fi
326332
else
327333
test_files=("${files[@]}")
@@ -340,8 +346,9 @@ function helper::load_bench_files() {
340346
if [[ "${#files[@]}" -eq 0 ]]; then
341347
if [[ -n "${BASHUNIT_DEFAULT_PATH}" ]]; then
342348
while IFS='' read -r line; do
343-
bench_files+=("$line")
349+
bench_files=("${bench_files[@]}" "$line")
344350
done < <(helper::find_files_recursive "$BASHUNIT_DEFAULT_PATH" '*[bB]ench.sh')
351+
unset line
345352
fi
346353
else
347354
bench_files=("${files[@]}")

src/runner.sh

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function runner::load_test_files() {
1414
fi
1515
unset BASHUNIT_CURRENT_TEST_ID
1616
export BASHUNIT_CURRENT_SCRIPT_ID="$(helper::generate_id "${test_file}")"
17-
scripts_ids+=("${BASHUNIT_CURRENT_SCRIPT_ID}")
17+
scripts_ids=("${scripts_ids[@]}" "${BASHUNIT_CURRENT_SCRIPT_ID}")
1818
internal_log "Loading file" "$test_file"
1919
# shellcheck source=/dev/null
2020
source "$test_file"
@@ -37,6 +37,7 @@ function runner::load_test_files() {
3737
fi
3838
internal_log "Finished file" "$test_file"
3939
done
40+
unset test_file
4041

4142
if parallel::is_enabled; then
4243
wait
@@ -50,6 +51,7 @@ function runner::load_test_files() {
5051
export BASHUNIT_CURRENT_SCRIPT_ID="${script_id}"
5152
cleanup_script_temp_files
5253
done
54+
unset script_id
5355
fi
5456
}
5557

@@ -74,6 +76,7 @@ function runner::load_bench_files() {
7476
runner::clean_set_up_and_tear_down_after_script
7577
cleanup_script_temp_files
7678
done
79+
unset bench_file
7780
}
7881

7982
function runner::spinner() {
@@ -135,10 +138,12 @@ function runner::parse_data_provider_args() {
135138
encoded_arg="$(helper::encode_base64 "${arg}")"
136139
printf '%s\n' "$encoded_arg"
137140
done
141+
unset arg encoded_arg
138142
return
139143
fi
140144

141145
# Fallback: parse args from the input string into an array, respecting quotes and escapes
146+
local i
142147
for ((i=0; i<${#input}; i++)); do
143148
local char="${input:$i:1}"
144149
if [ "$escaped" = true ]; then
@@ -168,8 +173,10 @@ function runner::parse_data_provider_args() {
168173
quote_char="$char"
169174
;;
170175
" " | $'\t')
171-
args+=("$current_arg")
172-
current_arg=""
176+
if [[ -n "$current_arg" ]]; then
177+
args=("${args[@]}" "$current_arg")
178+
current_arg=""
179+
fi
173180
;;
174181
*)
175182
current_arg+="$char"
@@ -182,12 +189,16 @@ function runner::parse_data_provider_args() {
182189
current_arg+="$char"
183190
fi
184191
done
185-
args+=("$current_arg")
192+
if [[ -n "$current_arg" ]]; then
193+
args=("${args[@]}" "$current_arg")
194+
fi
195+
unset char escaped quote_char i
186196
# Print one arg per line to stdout, base64-encoded to preserve newlines in the data
187197
for arg in "${args[@]}"; do
188198
encoded_arg="$(helper::encode_base64 "${arg}")"
189199
printf '%s\n' "$encoded_arg"
190200
done
201+
unset arg encoded_arg
191202
}
192203

193204
function runner::call_test_functions() {
@@ -214,39 +225,47 @@ function runner::call_test_functions() {
214225

215226
local provider_data
216227
provider_data=()
228+
local provider_count=0
217229
local provider_output
218230
provider_output="$(helper::get_provider_data "$fn_name" "$script")"
219231
if [[ -n "$provider_output" ]]; then
220232
local line
221233
while IFS=" " read -r line; do
222-
provider_data+=("$line")
234+
provider_data[$provider_count]="$line"
235+
provider_count=$((provider_count + 1))
223236
done << EOF
224237
$provider_output
225238
EOF
239+
unset line
226240
fi
227241

228242
# No data provider found
229-
if [[ "${#provider_data[@]}" -eq 0 ]]; then
243+
if [[ $provider_count -eq 0 ]]; then
230244
runner::run_test "$script" "$fn_name"
231245
unset fn_name
232246
continue
233247
fi
234248

235249
# Execute the test function for each line of data
236-
for data in "${provider_data[@]}"; do
250+
local i
251+
for ((i=0; i<provider_count; i++)); do
252+
local data="${provider_data[$i]}"
237253
local parsed_data
238254
parsed_data=()
255+
local parsed_count=0
239256
local args_output
240257
args_output="$(runner::parse_data_provider_args "$data")"
241258
local line
242259
while IFS= read -r line; do
243-
parsed_data+=( "$(helper::decode_base64 "${line}")" )
260+
parsed_data[$parsed_count]="$(helper::decode_base64 "${line}")"
261+
parsed_count=$((parsed_count + 1))
244262
done << EOF
245263
$args_output
246264
EOF
265+
unset line
247266
runner::run_test "$script" "$fn_name" "${parsed_data[@]}"
248267
done
249-
unset fn_name
268+
unset i data fn_name
250269
done
251270

252271
if ! env::is_simple_output_enabled; then
@@ -275,7 +294,7 @@ function runner::call_bench_functions() {
275294
for fn_name in "${functions_to_run[@]}"; do
276295
local annotation_result
277296
annotation_result="$(benchmark::parse_annotations "$fn_name" "$script")"
278-
set -- "$annotation_result"
297+
set -- $annotation_result
279298
revs="$1"
280299
its="$2"
281300
max_ms="$3"
@@ -410,6 +429,7 @@ function runner::run_test() {
410429
break
411430
fi
412431
done
432+
unset error
413433

414434
runner::parse_result "$fn_name" "$test_execution_result" "$@"
415435

src/test_doubles.sh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env bash
22

33
MOCKED_FUNCTIONS=()
4+
MOCKED_FUNCTIONS_COUNT=0
45

56
function unmock() {
67
local command=$1
@@ -34,7 +35,8 @@ function mock() {
3435

3536
export -f "${command?}"
3637

37-
MOCKED_FUNCTIONS+=("$command")
38+
MOCKED_FUNCTIONS[$MOCKED_FUNCTIONS_COUNT]="$command"
39+
MOCKED_FUNCTIONS_COUNT=$((MOCKED_FUNCTIONS_COUNT + 1))
3840
}
3941

4042
function spy() {
@@ -67,7 +69,8 @@ function spy() {
6769

6870
export -f "${command?}"
6971

70-
MOCKED_FUNCTIONS+=("$command")
72+
MOCKED_FUNCTIONS[$MOCKED_FUNCTIONS_COUNT]="$command"
73+
MOCKED_FUNCTIONS_COUNT=$((MOCKED_FUNCTIONS_COUNT + 1))
7174
}
7275

7376
function assert_have_been_called() {

0 commit comments

Comments
 (0)