Skip to content

Commit d7cd950

Browse files
committed
fix: initialize all loop variables to prevent unbound variable errors in strict mode
1 parent 68941fc commit d7cd950

14 files changed

Lines changed: 42 additions & 42 deletions

src/benchmark.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ function bashunit::benchmark::print_results() {
126126
printf '%-40s %6s %6s %10s\n' "Name" "Revs" "Its" "Avg(ms)"
127127
fi
128128

129-
local i
129+
local i=0
130130
for i in "${!_BASHUNIT_BENCH_NAMES[@]}"; do
131131
local name="${_BASHUNIT_BENCH_NAMES[$i]}"
132132
local revs="${_BASHUNIT_BENCH_REVS[$i]}"

src/colors.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ bashunit::sgr() {
1010
local codes=${1:-0}
1111
shift
1212

13-
local c
13+
local c=""
1414
for c in "$@"; do
1515
codes="$codes;$c"
1616
done

src/console_results.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ function bashunit::console_results::print_error_test() {
351351

352352
if [[ -n "$raw_output" ]] && bashunit::env::is_show_output_on_failure_enabled; then
353353
line="$line$(printf " %sOutput:%s\n" "${_BASHUNIT_COLOR_FAINT}" "${_BASHUNIT_COLOR_DEFAULT}")"
354-
local output_line
354+
local output_line=""
355355
while IFS= read -r output_line; do
356356
line="$line$(printf " %s\n" "$output_line")"
357357
done <<< "$raw_output"

src/coverage.sh

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ function bashunit::coverage::get_hit_lines() {
416416
# Only count hits that correspond to executable lines
417417
# This prevents >100% coverage when DEBUG trap fires on non-executable lines
418418
local count=0
419-
local line_num
419+
local line_num=0
420420
for line_num in $hit_lines; do
421421
local line_content
422422
line_content=$(sed -n "${line_num}p" "$file" 2>/dev/null) || continue
@@ -437,7 +437,7 @@ function bashunit::coverage::get_line_hits() {
437437
return
438438
fi
439439

440-
local count
440+
local count=0
441441
count=$(grep -c "^${file}:${lineno}$" "$_BASHUNIT_COVERAGE_DATA_FILE" 2>/dev/null) || count=0
442442
echo "$count"
443443
}
@@ -562,7 +562,7 @@ function bashunit::coverage::get_function_coverage() {
562562

563563
local executable=0
564564
local hit=0
565-
local lineno
565+
local lineno=0
566566

567567
for ((lineno = fn_start; lineno <= fn_end; lineno++)); do
568568
local line_content
@@ -616,7 +616,7 @@ function bashunit::coverage::report_text() {
616616
echo "Coverage Report"
617617
echo "---------------"
618618

619-
local file
619+
local file=""
620620
while IFS= read -r file; do
621621
[[ -z "$file" || ! -f "$file" ]] && continue
622622
has_files=true
@@ -773,7 +773,7 @@ function bashunit::coverage::report_html() {
773773
local total_hit=0
774774
local -a file_data=()
775775
local file_data_count=0
776-
local file
776+
local file=""
777777

778778
while IFS= read -r file; do
779779
[[ -z "$file" || ! -f "$file" ]] && continue
@@ -787,7 +787,7 @@ function bashunit::coverage::report_html() {
787787
total_hit=$((total_hit + hit))
788788

789789
local display_file="${file#"$(pwd)"/}"
790-
local safe_filename
790+
local safe_filename=""
791791
safe_filename=$(bashunit::coverage::path_to_filename "$file")
792792

793793
file_data[file_data_count]="$display_file|$hit|$executable|$pct|$safe_filename"; file_data_count=$((file_data_count + 1))
@@ -1192,7 +1192,7 @@ function bashunit::coverage::generate_file_html() {
11921192
# Index: line number, Value: newline-separated list of "test_file:test_function"
11931193
# Using indexed array for Bash 3.0 compatibility (no associative arrays)
11941194
local -a tests_by_line=()
1195-
local _line_and_test
1195+
local _line_and_test=""
11961196
while IFS= read -r _line_and_test; do
11971197
[[ -z "$_line_and_test" ]] && continue
11981198
local _tln="${_line_and_test%%|*}"

src/doc.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ function bashunit::doc::get_embedded_docs() {
1111

1212
function bashunit::doc::print_asserts() {
1313
local filter="${1:-}"
14-
local line
14+
local line=""
1515
local docstring=""
1616
local fn=""
1717
local should_print=0
1818

1919
# Pattern stored in variable for Bash 3.0 compatibility
2020
local _doc_pattern='^## ([A-Za-z0-9_]+)'
21-
local line
21+
local line=""
2222
while IFS='' read -r line || [[ -n "$line" ]]; do
2323
if [[ $line =~ $_doc_pattern ]]; then
2424
fn="${BASH_REMATCH[1]}"

src/env.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ function bashunit::env::print_verbose() {
244244

245245
local max_length=0
246246

247-
local key
247+
local key=""
248248
for key in "${keys[@]}"; do
249249
if (( ${#key} > max_length )); then
250250
max_length=${#key}

src/globals.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function bashunit::random_str() {
3131
local length=${1:-6}
3232
local chars='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
3333
local str=''
34-
local i
34+
local i=0
3535
for (( i=0; i<length; i++ )); do
3636
str="$str${chars:RANDOM%${#chars}:1}"
3737
done

src/helpers.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function bashunit::regex_match() {
2727
#
2828
function bashunit::helper::find_test_function_name() {
2929
local fallback_depth="${1:-2}"
30-
local i
30+
local i=0
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
@@ -112,7 +112,7 @@ function bashunit::helper::interpolate_function_name() {
112112
[[ $# -gt 0 ]] && args=("$@")
113113
local result="$function_name"
114114

115-
local i
115+
local i=0
116116
for ((i=0; i<args_count; i++)); do
117117
local placeholder="::$((i+1))::"
118118
# shellcheck disable=SC2155

src/learn.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ function bashunit::learn::show_progress() {
123123
local total_lessons=10
124124
local completed=0
125125

126-
local i
126+
local i=0
127127
for i in $(seq 1 $total_lessons); do
128128
if bashunit::learn::is_completed "lesson_$i"; then
129129
echo " ${_BASHUNIT_COLOR_PASSED}${_BASHUNIT_COLOR_DEFAULT} Lesson $i completed"

src/main.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ function bashunit::main::cmd_test() {
179179
args_count="$raw_args_count"
180180
else
181181
# Test mode: process file paths and extract inline filters
182-
local arg
182+
local arg=""
183183
for arg in "${raw_args[@]}"; do
184184
local parsed_path parsed_filter
185185
{
@@ -193,7 +193,7 @@ function bashunit::main::cmd_test() {
193193
inline_filter_file="$parsed_path"
194194
fi
195195

196-
local file
196+
local file=""
197197
while IFS= read -r file; do
198198
args[args_count]="$file"; args_count=$((args_count + 1))
199199
done < <(bashunit::helper::find_files_recursive "$parsed_path" '*[tT]est.sh')

0 commit comments

Comments
 (0)