]> git.ipfire.org Git - thirdparty/json-c.git/commitdiff
Save stderr from test output, allow for non-zero exit code from tests.
authorEric Hawicz <erh+git@nimenees.com>
Sun, 21 Jun 2026 17:43:11 +0000 (13:43 -0400)
committerEric Hawicz <erh+git@nimenees.com>
Sun, 21 Jun 2026 17:43:11 +0000 (13:43 -0400)
tests/test-defs.sh
tests/test_set_value.c
tests/test_visit.c
tests/test_visit.expected

index 8c5b521f01fddfc609e0cf4bda8a0f0b357ffd20..79843095e53043456fe326aa3a973d666d2f0ae2 100755 (executable)
@@ -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
index a8ebbfec79626d708b64518dff4fdce83d5e8d06..dac25770abb7f105229df7cfaaea1f3a90bd0881 100644 (file)
@@ -4,6 +4,7 @@
 #include <assert.h>
 #include <stdio.h>
 #include <string.h>
+#include <stdlib.h>
 
 #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");
index 3283a559a4342c902dbc53639ff534e21ce3706d..48fb60e8ffca3d1f98a26348cc482e1e6103105f 100644 (file)
@@ -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;
 }
index 5f323174bba6da8fef8666f1c5b183b78633f01a..34da92a462df7cfc3bb92909e3426d3f4039d7d9 100644 (file)
@@ -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
 ================================