Skip to content

Commit 7862985

Browse files
committed
fix: Use jq for proper JSON stringification in proc parity tests
The proc parity tests need to match JS engine's JSON.stringify() behavior. Updated to: - Use jq when available for proper JSON string encoding - Provide basic sed-based fallback for environments without jq - Simplify response parity test to echo responses directly This ensures the proc programs output JSON in the same format as the JS engine, fixing CI failures on Linux.
1 parent 99727cf commit 7862985

File tree

1 file changed

+20
-23
lines changed

1 file changed

+20
-23
lines changed

tests/weak_integration.rs

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -355,12 +355,22 @@ fn test_proc_js_json_parity() {
355355
// Create a proc program that echoes back the JSON it receives
356356
// Use sh instead of Python for better portability
357357
let mut proc_program = NamedTempFile::new().unwrap();
358+
// Use jq if available for proper JSON string encoding, otherwise basic escaping
358359
let program_content = r#"#!/bin/sh
359360
# Read lines from stdin and echo back the JSON as a deny message
360361
while IFS= read -r line; do
361-
# Echo the line back wrapped in a deny_message, with the JSON as a string value
362-
# The line is already JSON, so we wrap it as a string in the deny_message
363-
printf '{"deny_message": "%s"}\n' "$(printf '%s' "$line" | sed 's/"/\\"/g')"
362+
# The JS engine does: {deny_message: JSON.stringify(r)}
363+
# We need to output the JSON as a string value in deny_message
364+
if command -v jq >/dev/null 2>&1; then
365+
# Use jq to properly stringify the JSON
366+
deny_msg=$(printf "%s" "$line" | jq -Rs .)
367+
echo "{\"deny_message\": $deny_msg}"
368+
else
369+
# Fallback: basic escaping (may not handle all edge cases)
370+
# Escape backslashes first, then quotes
371+
escaped=$(printf "%s" "$line" | sed 's/\\/\\\\/g; s/"/\\"/g')
372+
printf '{"deny_message":"%s"}\n' "$escaped"
373+
fi
364374
done
365375
"#;
366376
proc_program.write_all(program_content.as_bytes()).unwrap();
@@ -478,29 +488,16 @@ fn test_proc_js_response_parity() {
478488
// Create proc program that returns the test response
479489
// Use sh for portability
480490
let mut proc_program = NamedTempFile::new().unwrap();
481-
let program_content = if response.starts_with('{') && response.ends_with('}') {
482-
// For JSON responses, echo directly with proper escaping
483-
format!(
484-
r#"#!/bin/sh
491+
// All responses should be echoed directly as-is
492+
let program_content = format!(
493+
r#"#!/bin/sh
494+
# Echo the exact response for each line of input
485495
while IFS= read -r line; do
486-
cat <<'EOF'
487-
{}
488-
EOF
496+
echo '{}'
489497
done
490498
"#,
491-
response
492-
)
493-
} else {
494-
// For simple responses (true, false, arbitrary text)
495-
format!(
496-
r#"#!/bin/sh
497-
while IFS= read -r line; do
498-
printf '%s\n' "{}"
499-
done
500-
"#,
501-
response
502-
)
503-
};
499+
response
500+
);
504501

505502
proc_program.write_all(program_content.as_bytes()).unwrap();
506503
proc_program.flush().unwrap();

0 commit comments

Comments
 (0)