diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index a0bc485d..822c665b 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -54,7 +54,7 @@ jobs: CC: ${{matrix.cc}} run: cmake . - run: cmake --build . - - run: ctest + - run: ctest --output-on-failure valgrind: runs-on: ubuntu-latest @@ -63,4 +63,4 @@ jobs: - run: sudo apt update && sudo apt install valgrind - run: cmake -DJANSSON_TEST_WITH_VALGRIND=ON . - run: cmake --build . - - run: ctest \ No newline at end of file + - run: ctest --output-on-failure diff --git a/CHANGES b/CHANGES index c5853c7e..cf3e4ef5 100644 --- a/CHANGES +++ b/CHANGES @@ -9,6 +9,10 @@ Work in progress is used to switch locales inside the threads (#674, #675, #677. Thanks to Bruno Haible the report and help with fixing.) +* Build: + + - Make test output nicer in CMake based builds (#683) + Version 2.14 ============ diff --git a/scripts/clang-format b/scripts/clang-format index d46056c9..65cb7a69 100755 --- a/scripts/clang-format +++ b/scripts/clang-format @@ -1,3 +1,3 @@ #!/bin/bash -find . -type f -a '(' -name '*.c' -o -name '*.h' ')' | xargs clang-format -i +git ls-files | grep '\.[ch]$' | xargs clang-format -i diff --git a/scripts/clang-format-check b/scripts/clang-format-check index 983e55d9..a75e6b72 100755 --- a/scripts/clang-format-check +++ b/scripts/clang-format-check @@ -12,13 +12,16 @@ fi errors=0 paths=$(git ls-files | grep '\.[ch]$') for path in $paths; do + echo "Checking $path" + $CLANG_FORMAT $path > $path.formatted in=$(cat $path) - out=$($CLANG_FORMAT $path) + out=$(cat $path.formatted) if [ "$in" != "$out" ]; then - diff -u -L $path -L "$path.formatted" $path - <<<$out + diff -u $path $path.formatted errors=1 fi + rm $path.formatted done if [ $errors -ne 0 ]; then diff --git a/test/.gitignore b/test/.gitignore index 6a33dea3..93cc8da4 100644 --- a/test/.gitignore +++ b/test/.gitignore @@ -7,6 +7,7 @@ suites/api/test_cpp suites/api/test_dump suites/api/test_dump_callback suites/api/test_equal +suites/api/test_fixed_size suites/api/test_load suites/api/test_load_callback suites/api/test_loadb diff --git a/test/bin/json_process.c b/test/bin/json_process.c index fc98543f..3fa926b9 100644 --- a/test/bin/json_process.c +++ b/test/bin/json_process.c @@ -35,7 +35,6 @@ struct config { int ensure_ascii; int sort_keys; int strip; - int use_env; int have_hashseed; int hashseed; int precision; @@ -138,10 +137,16 @@ static int cmpfile(const char *str, const char *path, const char *fname) { } buffer = loadfile(file); - if (strcmp(buffer, str) != 0) + if (strcmp(buffer, str) != 0) { + fprintf(stderr, "=== Expected %s ===\n", fname); + fprintf(stderr, "%s\n", buffer); + fprintf(stderr, "=== Actual %s ===\n", fname); + fprintf(stderr, "%s\n", str); ret = 1; - else + } else { ret = 0; + } + free(buffer); fclose(file); @@ -206,8 +211,9 @@ int use_conf(char *test_path) { buffer = loadfile(infile); json = json_loads(strip(buffer), 0, &error); free(buffer); - } else + } else { json = json_loadf(infile, 0, &error); + } fclose(infile); @@ -227,108 +233,6 @@ int use_conf(char *test_path) { return ret; } -static int getenv_int(const char *name) { - char *value, *end; - long result; - - value = getenv(name); - if (!value) - return 0; - - result = strtol(value, &end, 10); - if (*end != '\0') - return 0; - - return (int)result; -} - -int use_env() { - int indent, precision; - size_t flags = 0; - json_t *json; - json_error_t error; - -#ifdef _WIN32 - /* On Windows, set stdout and stderr to binary mode to avoid - outputting DOS line terminators */ - _setmode(_fileno(stdout), _O_BINARY); - _setmode(_fileno(stderr), _O_BINARY); -#endif - - indent = getenv_int("JSON_INDENT"); - if (indent < 0 || indent > 31) { - fprintf(stderr, "invalid value for JSON_INDENT: %d\n", indent); - return 2; - } - if (indent > 0) - flags |= JSON_INDENT(indent); - - if (getenv_int("JSON_COMPACT") > 0) - flags |= JSON_COMPACT; - - if (getenv_int("JSON_ENSURE_ASCII")) - flags |= JSON_ENSURE_ASCII; - - if (getenv_int("JSON_PRESERVE_ORDER")) - flags |= JSON_PRESERVE_ORDER; - - if (getenv_int("JSON_SORT_KEYS")) - flags |= JSON_SORT_KEYS; - - precision = getenv_int("JSON_REAL_PRECISION"); - if (precision < 0 || precision > 31) { - fprintf(stderr, "invalid value for JSON_REAL_PRECISION: %d\n", precision); - return 2; - } - - if (getenv("HASHSEED")) - json_object_seed(getenv_int("HASHSEED")); - - if (precision > 0) - flags |= JSON_REAL_PRECISION(precision); - - if (getenv_int("STRIP")) { - /* Load to memory, strip leading and trailing whitespace */ - size_t size = 0, used = 0; - char *buffer = NULL, *buf_ck = NULL; - - while (1) { - size_t count; - - size = (size == 0 ? 128 : size * 2); - buf_ck = realloc(buffer, size); - if (!buf_ck) { - fprintf(stderr, "Unable to allocate %d bytes\n", (int)size); - free(buffer); - return 1; - } - buffer = buf_ck; - - count = fread(buffer + used, 1, size - used, stdin); - if (count < size - used) { - buffer[used + count] = '\0'; - break; - } - used += count; - } - - json = json_loads(strip(buffer), 0, &error); - free(buffer); - } else - json = json_loadf(stdin, 0, &error); - - if (!json) { - fprintf(stderr, "%d %d %d\n%s\n", error.line, error.column, error.position, - error.text); - return 1; - } - - json_dumpf(json, stdout, flags); - json_decref(json); - - return 0; -} - int main(int argc, char *argv[]) { int i; char *test_path = NULL; @@ -344,23 +248,17 @@ int main(int argc, char *argv[]) { for (i = 1; i < argc; i++) { if (!strcmp(argv[i], "--strip")) conf.strip = 1; - else if (!strcmp(argv[i], "--env")) - conf.use_env = 1; else test_path = argv[i]; } - if (conf.use_env) - return use_env(); - else { - if (!test_path) - goto usage; - - return use_conf(test_path); + if (!test_path) { + goto usage; } + return use_conf(test_path); + usage: - fprintf(stderr, "argc =%d\n", argc); - fprintf(stderr, "usage: %s [--strip] [--env] test_dir\n", argv[0]); + fprintf(stderr, "usage: %s [--strip] test_dir\n", argv[0]); return 2; } diff --git a/test/suites/encoding-flags/run b/test/suites/encoding-flags/run index 5db7d5e5..a6f21115 100755 --- a/test/suites/encoding-flags/run +++ b/test/suites/encoding-flags/run @@ -10,23 +10,13 @@ is_test() { } run_test() { - ( - if [ -f $test_path/env ]; then - . $test_path/env - fi - $json_process --env <$test_path/input >$test_log/stdout 2>$test_log/stderr - ) + $json_process $test_path >$test_log/stdout 2>$test_log/stderr || return 1 valgrind_check $test_log/stderr || return 1 - cmp -s $test_path/output $test_log/stdout } show_error() { valgrind_show_error && return - - echo "EXPECTED OUTPUT:" - nl -bn $test_path/output - echo "ACTUAL OUTPUT:" - nl -bn $test_log/stdout + cat $test_log/stderr } . $top_srcdir/test/scripts/run-tests.sh diff --git a/test/suites/invalid-unicode/run b/test/suites/invalid-unicode/run index 369c43da..5b542a43 100755 --- a/test/suites/invalid-unicode/run +++ b/test/suites/invalid-unicode/run @@ -10,18 +10,13 @@ is_test() { } run_test() { - $json_process --env <$test_path/input >$test_log/stdout 2>$test_log/stderr - valgrind_check $test_log/stderr || return 1 - cmp -s $test_path/error $test_log/stderr + $json_process $test_path >$test_log/stdout 2>$test_log/stderr || return 1 + valgrind_check $test_log/stderr$s || return 1 } show_error() { valgrind_show_error && return - - echo "EXPECTED ERROR:" - nl -bn $test_path/error - echo "ACTUAL ERROR:" - nl -bn $test_log/stderr + cat $test_log/stderr } . $top_srcdir/test/scripts/run-tests.sh diff --git a/test/suites/invalid/run b/test/suites/invalid/run index d1d490c6..ee1ee1aa 100755 --- a/test/suites/invalid/run +++ b/test/suites/invalid/run @@ -13,24 +13,18 @@ do_run() { variant=$1 s=".$1" - strip=0 + strip="" if [ "$variant" = "strip" ]; then # This test should not be stripped [ -f $test_path/nostrip ] && return - strip=1 + strip="--strip" fi - STRIP=$strip $json_process --env \ - <$test_path/input >$test_log/stdout$s 2>$test_log/stderr$s - valgrind_check $test_log/stderr$s || return 1 - - ref=error - [ -f $test_path/error$s ] && ref=error$s - - if ! cmp -s $test_path/$ref $test_log/stderr$s; then - echo $variant > $test_log/variant + if ! $json_process $strip $test_path >$test_log/stdout$s 2>$test_log/stderr$s; then + echo $variant >$test_log/variant return 1 fi + valgrind_check $test_log/stderr$s || return 1 } run_test() { @@ -44,14 +38,7 @@ show_error() { s=".$variant" echo "VARIANT: $variant" - - echo "EXPECTED ERROR:" - ref=error - [ -f $test_path/error$s ] && ref=error$s - nl -bn $test_path/$ref - - echo "ACTUAL ERROR:" - nl -bn $test_log/stderr$s + cat $test_log/stderr$s } . $top_srcdir/test/scripts/run-tests.sh diff --git a/test/suites/valid/run b/test/suites/valid/run index d9954896..afc488c9 100755 --- a/test/suites/valid/run +++ b/test/suites/valid/run @@ -16,20 +16,14 @@ do_run() { variant=$1 s=".$1" - strip=0 - [ "$variant" = "strip" ] && strip=1 + strip="" + [ "$variant" = "strip" ] && strip="--strip" - STRIP=$strip $json_process --env \ - <$test_path/input >$test_log/stdout$s 2>$test_log/stderr$s - valgrind_check $test_log/stderr$s || return 1 - - ref=output - [ -f $test_path/output$s ] && ref=output$s - - if ! cmp -s $test_path/$ref $test_log/stdout$s; then - echo $variant > $test_log/variant + if ! $json_process $strip $test_path >$test_log/stdout$s 2>$test_log/stderr$s; then + echo $variant >$test_log/variant return 1 fi + valgrind_check $test_log/stderr$s || return 1 } run_test() { @@ -43,14 +37,7 @@ show_error() { s=".$variant" echo "VARIANT: $variant" - - echo "EXPECTED OUTPUT:" - ref=output - [ -f $test_path/output$s ] && ref=output$s - nl -bn $test_path/$ref - - echo "ACTUAL OUTPUT:" - nl -bn $test_log/stdout$s + cat $test_log/stderr$s } . $top_srcdir/test/scripts/run-tests.sh