db3e7b7d29
The -J flag was outputting 'valid' or 'discarded' instead of the raw JSON result. This was test logic that should be in the test scripts. Changed the -J flag to output the raw JSON result (pretty-printed) or an error message. Updated the test script to use jq for JSON validation. All other debug flags (-E, -e, -R, -P, -B, -A, -T) were already minimal hooks and didn't need to be changed.
87 lines
2.8 KiB
Bash
Executable File
87 lines
2.8 KiB
Bash
Executable File
#!/bin/bash
|
|
# test_json_safe_parse.sh - Tests for parse_json_safely() and expect_or_exit()
|
|
|
|
# Test parse_json_safely with valid JSON
|
|
OUTPUT=$("${DRHARNESS}" -J '{"key": "value"}')
|
|
if ! echo "$OUTPUT" | jq -e '.key == "value"' > /dev/null 2>&1; then
|
|
echo "FAIL: parse_json_safely - expected valid JSON with key='value', got '${OUTPUT}'"
|
|
exit 1
|
|
fi
|
|
|
|
# Test parse_json_safely with valid JSON object (empty)
|
|
OUTPUT=$("${DRHARNESS}" -J '{}')
|
|
if ! echo "$OUTPUT" | jq -e 'type == "object"' > /dev/null 2>&1; then
|
|
echo "FAIL: parse_json_safely - expected valid empty object, got '${OUTPUT}'"
|
|
exit 1
|
|
fi
|
|
|
|
# Test parse_json_safely with valid JSON array
|
|
OUTPUT=$("${DRHARNESS}" -J '[1, 2, 3]')
|
|
if ! echo "$OUTPUT" | jq -e 'type == "array" and length == 3' > /dev/null 2>&1; then
|
|
echo "FAIL: parse_json_safely - expected valid array, got '${OUTPUT}'"
|
|
exit 1
|
|
fi
|
|
|
|
# Test parse_json_safely with invalid JSON (missing quotes)
|
|
set +e
|
|
OUTPUT=$("${DRHARNESS}" -J '{key: value}' 2>&1)
|
|
EXIT_CODE=$?
|
|
set -e
|
|
if [ ${EXIT_CODE} -eq 0 ]; then
|
|
echo "FAIL: parse_json_safely - expected non-zero exit code for invalid JSON, got 0"
|
|
exit 1
|
|
fi
|
|
if ! echo "$OUTPUT" | grep -q "error: invalid JSON"; then
|
|
echo "FAIL: parse_json_safely - expected 'error: invalid JSON' for invalid JSON, got '${OUTPUT}'"
|
|
exit 1
|
|
fi
|
|
|
|
# Test parse_json_safely with invalid JSON (random text)
|
|
set +e
|
|
OUTPUT=$("${DRHARNESS}" -J 'not json at all' 2>&1)
|
|
EXIT_CODE=$?
|
|
set -e
|
|
if [ ${EXIT_CODE} -eq 0 ]; then
|
|
echo "FAIL: parse_json_safely - expected non-zero exit code for invalid JSON, got 0"
|
|
exit 1
|
|
fi
|
|
if ! echo "$OUTPUT" | grep -q "error: invalid JSON"; then
|
|
echo "FAIL: parse_json_safely - expected 'error: invalid JSON' for random text, got '${OUTPUT}'"
|
|
exit 1
|
|
fi
|
|
|
|
# Test parse_json_safely with empty string
|
|
set +e
|
|
OUTPUT=$("${DRHARNESS}" -J '' 2>&1)
|
|
EXIT_CODE=$?
|
|
set -e
|
|
if [ ${EXIT_CODE} -eq 0 ]; then
|
|
echo "FAIL: parse_json_safely - expected non-zero exit code for empty string, got 0"
|
|
exit 1
|
|
fi
|
|
if ! echo "$OUTPUT" | grep -q "error: invalid JSON"; then
|
|
echo "FAIL: parse_json_safely - expected 'error: invalid JSON' for empty string, got '${OUTPUT}'"
|
|
exit 1
|
|
fi
|
|
|
|
# Test expect_or_exit with success value
|
|
OUTPUT=$("${DRHARNESS}" -E 'success_value')
|
|
if [ "${OUTPUT}" != "success_value" ]; then
|
|
echo "FAIL: expect_or_exit success - expected 'success_value', got '${OUTPUT}'"
|
|
exit 1
|
|
fi
|
|
|
|
# Test expect_or_exit with error value (should exit with non-zero and print error to stderr)
|
|
set +e
|
|
ERROR_OUTPUT=$("${DRHARNESS}" -e 'my_error_message' 2>&1 >/dev/null)
|
|
EXIT_CODE=$?
|
|
set -e
|
|
if [ ${EXIT_CODE} -eq 0 ]; then
|
|
echo "FAIL: expect_or_exit error - expected non-zero exit code, got 0"
|
|
exit 1
|
|
fi
|
|
if [ "${ERROR_OUTPUT}" != "my_error_message" ]; then
|
|
echo "FAIL: expect_or_exit error - expected 'my_error_message' on stderr, got '${ERROR_OUTPUT}'"
|
|
exit 1
|
|
fi
|