Skip to content

Commit 5cb99eb

Browse files
committed
Fix macOS script errors with empty arrays and sed
- Fix unbound variable error when FAILED_TESTS array is empty - Check array length before expanding with [@] to avoid set -u errors - Use conditional checks for empty arrays in JSON generation - Fix sed 'first RE may not be empty' errors - Use printf to ensure strings are properly formatted before sed - Simplify sed escaping logic - Fix empty array handling in summary log generation - Add conditional checks for empty arrays in heredoc Fixes macOS CI failures: - 'FAILED_TESTS[@]: unbound variable' error - 'sed: first RE may not be empty' errors
1 parent c043269 commit 5cb99eb

1 file changed

Lines changed: 21 additions & 5 deletions

File tree

scripts/cicd/run_vader_tests_direct.sh

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -313,16 +313,32 @@ format_json_array() {
313313
result+=","
314314
fi
315315
# Escape JSON special characters: ", \, and control characters
316-
local escaped=$(echo "$item" | sed 's/\\/\\\\/g' | sed 's/"/\\"/g' | sed 's/\x00//g')
316+
# Use printf to ensure we have a string, then escape
317+
local escaped=$(printf '%s' "$item")
318+
# Escape backslashes first, then quotes
319+
escaped=$(printf '%s' "$escaped" | sed 's/\\/\\\\/g')
320+
escaped=$(printf '%s' "$escaped" | sed 's/"/\\"/g')
321+
# Remove null bytes
322+
escaped=$(printf '%s' "$escaped" | tr -d '\000')
317323
result+="\"${escaped}\""
318324
done
319325
result+="]"
320326
echo "$result"
321327
}
322328

323329
TEST_RESULTS_JSON="${PROJECT_ROOT}/test-results.json"
324-
PASSED_ARRAY_JSON=$(format_json_array "${PASSED_TESTS[@]}")
325-
FAILED_ARRAY_JSON=$(format_json_array "${FAILED_TESTS[@]}")
330+
# Handle empty arrays properly with set -u (unbound variable check)
331+
# Use parameter expansion to provide empty string if array is unset
332+
if [ ${#PASSED_TESTS[@]} -eq 0 ]; then
333+
PASSED_ARRAY_JSON="[]"
334+
else
335+
PASSED_ARRAY_JSON=$(format_json_array "${PASSED_TESTS[@]}")
336+
fi
337+
if [ ${#FAILED_TESTS[@]} -eq 0 ]; then
338+
FAILED_ARRAY_JSON="[]"
339+
else
340+
FAILED_ARRAY_JSON=$(format_json_array "${FAILED_TESTS[@]}")
341+
fi
326342

327343
cat > "${TEST_RESULTS_JSON}" << EOF
328344
{
@@ -372,10 +388,10 @@ Total Assertions: ${TOTAL_ASSERTIONS}
372388
Passed Assertions: ${PASSED_ASSERTIONS}
373389
374390
Passed Tests:
375-
$(for test in "${PASSED_TESTS[@]}"; do echo "${test}"; done)
391+
$(if [ ${#PASSED_TESTS[@]} -gt 0 ]; then for test in "${PASSED_TESTS[@]}"; do echo "${test}"; done; else echo " (none)"; fi)
376392
377393
Failed Tests:
378-
$(for test in "${FAILED_TESTS[@]}"; do echo "${test}"; done)
394+
$(if [ ${#FAILED_TESTS[@]} -gt 0 ]; then for test in "${FAILED_TESTS[@]}"; do echo "${test}"; done; else echo " (none)"; fi)
379395
EOF
380396

381397
# Print summary

0 commit comments

Comments
 (0)