Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions tests/claude-code/test-helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,21 @@ assert_order() {
if [ "$line_a" -lt "$line_b" ]; then
echo " [PASS] $test_name (A at line $line_a, B at line $line_b)"
return 0
elif [ "$line_a" -eq "$line_b" ]; then
# Same line - check column positions
local the_line=$(echo "$output" | sed -n "${line_a}p")
local col_a=$(echo "$the_line" | grep -bo "$pattern_a" | head -1 | cut -d: -f1)
local col_b=$(echo "$the_line" | grep -bo "$pattern_b" | head -1 | cut -d: -f1)

if [ "$col_a" -lt "$col_b" ]; then
echo " [PASS] $test_name (both at line $line_a, A at col $col_a, B at col $col_b)"
return 0
else
echo " [FAIL] $test_name"
echo " Expected '$pattern_a' before '$pattern_b' on line $line_a"
echo " But found A at col $col_a, B at col $col_b"
return 1
fi
Comment on lines +117 to +131
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Missing validation for empty column values will cause arithmetic error.

If grep -bo fails to find the pattern (possible with certain regex metacharacters that behave differently between grep -n and grep -bo), col_a or col_b will be empty, causing the arithmetic comparison on line 123 to fail with a bash error like integer expression expected.

Additionally, as flagged by ShellCheck (SC2155), declaring and assigning in one statement masks the return value, making it harder to detect failures.

🛡️ Proposed fix with validation and separated declarations
     elif [ "$line_a" -eq "$line_b" ]; then
         # Same line - check column positions
-        local the_line=$(echo "$output" | sed -n "${line_a}p")
-        local col_a=$(echo "$the_line" | grep -bo "$pattern_a" | head -1 | cut -d: -f1)
-        local col_b=$(echo "$the_line" | grep -bo "$pattern_b" | head -1 | cut -d: -f1)
+        local the_line
+        the_line=$(echo "$output" | sed -n "${line_a}p")
+        local col_a
+        col_a=$(echo "$the_line" | grep -bo "$pattern_a" | head -1 | cut -d: -f1)
+        local col_b
+        col_b=$(echo "$the_line" | grep -bo "$pattern_b" | head -1 | cut -d: -f1)
+
+        if [ -z "$col_a" ] || [ -z "$col_b" ]; then
+            echo "  [FAIL] $test_name: could not determine column positions on line $line_a"
+            return 1
+        fi
 
         if [ "$col_a" -lt "$col_b" ]; then
🧰 Tools
🪛 Shellcheck (0.11.0)

[warning] 119-119: Declare and assign separately to avoid masking return values.

(SC2155)


[warning] 120-120: Declare and assign separately to avoid masking return values.

(SC2155)


[warning] 121-121: Declare and assign separately to avoid masking return values.

(SC2155)

🤖 Prompt for AI Agents
In `@tests/claude-code/test-helpers.sh` around lines 117 - 131, The code compares
numeric columns col_a and col_b but doesn't handle when grep -bo fails and
returns empty (causing "integer expression expected") and also triggers
ShellCheck SC2155 by combining declaration+assignment; fix by declaring locals
first (local the_line col_a col_b) then assign them separately (the_line=$(echo
... | sed -n "${line_a}p"); col_a=$(echo "$the_line" | grep -bo "$pattern_a" |
head -1 | cut -d: -f1) etc.), validate each col (if [ -z "$col_a" ] || [ -z
"$col_b" ] ) then print a clear [FAIL] $test_name message showing the_line and
indicate which pattern was not found (include patterns $pattern_a/$pattern_b and
the_line or full output) and return 1; only if both are non-empty perform the
numeric comparison using -lt.

else
echo " [FAIL] $test_name"
echo " Expected '$pattern_a' before '$pattern_b'"
Expand Down