From: Eric Hawicz Date: Sun, 21 Jun 2026 17:43:11 +0000 (-0400) Subject: Save stderr from test output, allow for non-zero exit code from tests. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bb9c1238adb88c81c92c405ee83d43e1430b4172;p=thirdparty%2Fjson-c.git Save stderr from test output, allow for non-zero exit code from tests. --- diff --git a/tests/test-defs.sh b/tests/test-defs.sh index 8c5b521..7984309 100755 --- a/tests/test-defs.sh +++ b/tests/test-defs.sh @@ -83,10 +83,14 @@ fi # run_output_test() { + EXPECTED_EXITVAL=0 if [ "$1" = "-o" ] ; then TEST_OUTPUT="$2" - shift - shift + shift 2 + fi + if [ "$1" = "--exit" ] ; then + EXPECTED_EXITVAL="$2" + shift 2 fi TEST_COMMAND="$1" shift @@ -94,9 +98,9 @@ run_output_test() TEST_OUTPUT=${TEST_COMMAND} fi - REDIR_OUTPUT="> \"${TEST_OUTPUT}.out\"" + REDIR_OUTPUT="> \"${TEST_OUTPUT}.out\" 2>&1" if [ $VERBOSE -gt 1 ] ; then - REDIR_OUTPUT="| tee \"${TEST_OUTPUT}.out\"" + REDIR_OUTPUT="2>&1 | tee \"${TEST_OUTPUT}.out\"" fi if [ $use_valgrind -eq 1 ] ; then @@ -108,15 +112,16 @@ run_output_test() --show-reachable=yes \ --run-libc-freeres=yes \ "\"${top_builddir}/${TEST_COMMAND}\"" \"\$@\" ${REDIR_OUTPUT} - err=$? + exitval=$? else eval "\"${top_builddir}/${TEST_COMMAND}"\" \"\$@\" ${REDIR_OUTPUT} - err=$? + exitval=$? fi - if [ $err -ne 0 ] ; then - echo "ERROR: \"${TEST_COMMAND} $@\" exited with non-zero exit status: $err" 1>&2 + if [ $exitval -ne $EXPECTED_EXITVAL ] ; then + echo "ERROR: \"${TEST_COMMAND} $@\" exited with incorrect exit status: $err != $EXPECTED_EXITVAL" 1>&2 + err=1 fi if [ $use_valgrind -eq 1 ] ; then diff --git a/tests/test_set_value.c b/tests/test_set_value.c index a8ebbfe..dac2577 100644 --- a/tests/test_set_value.c +++ b/tests/test_set_value.c @@ -4,6 +4,7 @@ #include #include #include +#include #include "json.h" @@ -106,7 +107,9 @@ int main(int argc, char **argv) tmp = json_tokener_parse("1.234"); json_object_set_double(tmp, 12.3); const char *serialized = json_object_to_json_string(tmp); - fprintf(stderr, "%s\n", serialized); + if (getenv("JSONC_TEST_TRACE") != NULL) + // This output might be different on different systems + fprintf(stderr, "%s\n", serialized); assert(strncmp(serialized, "12.3", 4) == 0); json_object_put(tmp); printf("PARSE AND SET PASSED\n"); diff --git a/tests/test_visit.c b/tests/test_visit.c index 3283a55..48fb60e 100644 --- a/tests/test_visit.c +++ b/tests/test_visit.c @@ -77,6 +77,7 @@ static int emit_object(json_object *jso, int flags, json_object *parent_jso, con printf("flags: 0x%x, key: %s, index: %ld, value: %s\n", flags, (jso_key ? jso_key : "(null)"), (jso_index ? (long)*jso_index : -1L), json_object_to_json_string(jso)); + fflush(stdout); return JSON_C_VISIT_RETURN_CONTINUE; } @@ -148,5 +149,6 @@ static int err_return(json_object *jso, int flags, json_object *parent_jso, cons printf("flags: 0x%x, key: %s, index: %ld, value: %s\n", flags, (jso_key ? jso_key : "(null)"), (jso_index ? (long)*jso_index : -1L), json_object_to_json_string(jso)); + fflush(stdout); return 100; } diff --git a/tests/test_visit.expected b/tests/test_visit.expected index 5f32317..34da92a 100644 --- a/tests/test_visit.expected +++ b/tests/test_visit.expected @@ -84,6 +84,7 @@ json_c_visit(stop_array)=0 ================================ flags: 0x0, key: (null), index: -1, value: { "obj1": 123, "obj2": { "subobj1": "aaa", "subobj2": "bbb", "subobj3": [ "elem1", "elem2", true ] }, "obj3": 1.234, "obj4": [ true, false, null ] } +ERROR: invalid return value from json_c_visit userfunc: 100 json_c_visit(err_return)=-1 ================================