Copilot commented on code in PR #13237:
URL: https://github.com/apache/trafficserver/pull/13237#discussion_r3353264809


##########
tests/tools/condwait:
##########
@@ -55,24 +61,35 @@ if [[ "$X" = "$1" ]] ; then
 fi
 
 if [[ "$1" = "" ]] ; then
-    echo "usage: condwait [ MAX-WAIT [ POST-WAIT ] ] TEST-CONDTION" >&2
+    usage
     exit 1
 fi
 
+if [[ "$1" = "--contains" ]]; then
+    if [[ $# -ne 4 ]] || ! [[ "$4" =~ ^[0-9]+$ ]]; then
+        usage
+        exit 1
+    fi
+fi
+
 # Check if this is a simple file existence test (-f, -e, -d). If so, use ls -d
 # which handles glob patterns properly. Otherwise, use test for the condition.
 check_condition() {
-    if [[ "$1" = "-f" || "$1" = "-e" || "$1" = "-d" ]] && [[ $# -eq 2 ]]; then
+    if [[ "$1" = "--contains" ]]; then
+        [[ -f "$2" ]] || return 1
+        matches=$(awk -v pattern="$3" 'index($0, pattern) { count++ } END { 
print count + 0 }' "$2")
+        (( matches >= $4 ))
+    elif [[ "$1" = "-f" || "$1" = "-e" || "$1" = "-d" ]] && [[ $# -eq 2 ]]; 
then
         # Use ls -d for file existence tests to support glob patterns.
         ls -d $2 >/dev/null 2>&1
     else

Review Comment:
   For `-f/-e/-d` existence checks, `ls -d $2` performs word-splitting. This 
makes `condwait ... -f '/path with spaces/file'` fail even though the script’s 
docs say the condition may contain whitespace. You can preserve glob support 
while handling whitespace by only leaving `$2` unquoted when it contains glob 
metacharacters.



##########
tests/tools/condwait:
##########
@@ -55,24 +61,35 @@ if [[ "$X" = "$1" ]] ; then
 fi
 
 if [[ "$1" = "" ]] ; then
-    echo "usage: condwait [ MAX-WAIT [ POST-WAIT ] ] TEST-CONDTION" >&2
+    usage
     exit 1
 fi
 
+if [[ "$1" = "--contains" ]]; then
+    if [[ $# -ne 4 ]] || ! [[ "$4" =~ ^[0-9]+$ ]]; then
+        usage
+        exit 1
+    fi
+fi
+
 # Check if this is a simple file existence test (-f, -e, -d). If so, use ls -d
 # which handles glob patterns properly. Otherwise, use test for the condition.
 check_condition() {
-    if [[ "$1" = "-f" || "$1" = "-e" || "$1" = "-d" ]] && [[ $# -eq 2 ]]; then
+    if [[ "$1" = "--contains" ]]; then
+        [[ -f "$2" ]] || return 1
+        matches=$(awk -v pattern="$3" 'index($0, pattern) { count++ } END { 
print count + 0 }' "$2")
+        (( matches >= $4 ))

Review Comment:
   In `--contains` mode, `matches` is assigned without being scoped, so it 
leaks into the script’s global namespace and can be accidentally 
reused/overwritten if `check_condition` grows additional logic. Declare it as a 
local variable inside the function to keep the helper self-contained.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to