From 9fcc4860697dfa3c79d2ac1553ee604f6eb4d9a3 Mon Sep 17 00:00:00 2001 From: Brice Dutheil Date: Fri, 13 Feb 2026 12:52:25 +0100 Subject: [PATCH] fix: handle special characters in XML filenames and sanitize grep output The script was failing with arithmetic syntax errors when XML filenames contained special characters or when grep output included newlines. - Replace unsafe for loop with proper while/read loop using null-delimited find output - Strip newlines and carriage returns from grep counts before arithmetic operations --- .gitlab/count_tests.sh | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/.gitlab/count_tests.sh b/.gitlab/count_tests.sh index 9cdaa5a7e16..3e1de7e6bc4 100755 --- a/.gitlab/count_tests.sh +++ b/.gitlab/count_tests.sh @@ -47,19 +47,25 @@ echo "Counting tests in $RESULTS_DIR for $TEST_CATEGORY on JVM $JVM_VERSION" # Find all XML files and count tests log_verbose "Searching for XML files in $RESULTS_DIR" -for xml_file in $(find "$RESULTS_DIR" -name "*.xml" -type f 2>/dev/null); do +while IFS= read -r -d '' xml_file; do XML_FILE_COUNT=$((XML_FILE_COUNT + 1)) log_verbose "Processing file $XML_FILE_COUNT: $xml_file" if [ -f "$xml_file" ]; then # Count actual elements (more accurate than testsuite attributes) # This matches how datadog-ci counts tests tests=$(grep -c '/dev/null || echo "0") + tests="${tests//[$'\n\r']/}" # Strip any newlines/carriage returns # Count , , and tags # These are more reliable than trying to match testcase+failure in one grep failures=$(grep -c '/dev/null || echo "0") + failures="${failures//[$'\n\r']/}" # Strip any newlines/carriage returns + errors=$(grep -c '/dev/null || echo "0") + errors="${errors//[$'\n\r']/}" # Strip any newlines/carriage returns + skipped=$(grep -c '/dev/null || echo "0") + skipped="${skipped//[$'\n\r']/}" # Strip any newlines/carriage returns log_verbose " → tests=$tests, failures=$failures, errors=$errors, skipped=$skipped" @@ -68,7 +74,7 @@ for xml_file in $(find "$RESULTS_DIR" -name "*.xml" -type f 2>/dev/null); do TOTAL_ERRORS=$((TOTAL_ERRORS + errors)) TOTAL_SKIPPED=$((TOTAL_SKIPPED + skipped)) fi -done +done < <(find "$RESULTS_DIR" -name "*.xml" -type f -print0 2>/dev/null) log_verbose "Processed $XML_FILE_COUNT XML files"