diff --git a/src/command_fmt.cc b/src/command_fmt.cc index 8803266a..cf31ea2a 100644 --- a/src/command_fmt.cc +++ b/src/command_fmt.cc @@ -2,8 +2,10 @@ #include #include // std::ofstream -#include // std::cerr +#include // std::cerr, std::cout #include // std::ostringstream +#include // std::move +#include // std::vector #include "command.h" #include "error.h" @@ -14,6 +16,9 @@ auto sourcemeta::jsonschema::fmt(const sourcemeta::core::Options &options) -> void { + const bool output_json{options.contains("json")}; + bool result{true}; + std::vector failed_files; const auto indentation{parse_indentation(options)}; for (const auto &entry : for_each_json(options)) { if (entry.first.extension() == ".yaml" || @@ -51,13 +56,13 @@ auto sourcemeta::jsonschema::fmt(const sourcemeta::core::Options &options) if (options.contains("check")) { if (current.str() == expected.str()) { - LOG_VERBOSE(options) << "PASS: " << entry.first.string() << "\n"; + LOG_VERBOSE(options) << "ok: " << entry.first.string() << "\n"; + } else if (output_json) { + failed_files.push_back(entry.first.string()); + result = false; } else { - std::cerr << "FAIL: " << entry.first.string() << "\n"; - std::cerr << "Got:\n" - << current.str() << "\nBut expected:\n" - << expected.str() << "\n"; - throw Fail{EXIT_FAILURE}; + std::cerr << "fail: " << entry.first.string() << "\n"; + result = false; } } else { if (current.str() != expected.str()) { @@ -66,4 +71,31 @@ auto sourcemeta::jsonschema::fmt(const sourcemeta::core::Options &options) } } } + + if (options.contains("check") && output_json) { + auto output_json_object{sourcemeta::core::JSON::make_object()}; + output_json_object.assign("valid", sourcemeta::core::JSON{result}); + + if (!result) { + auto errors_array{sourcemeta::core::JSON::make_array()}; + for (auto &file_path : failed_files) { + errors_array.push_back(sourcemeta::core::JSON{std::move(file_path)}); + } + + output_json_object.assign("errors", sourcemeta::core::JSON{errors_array}); + } + + sourcemeta::core::prettify(output_json_object, std::cout, indentation); + std::cout << "\n"; + } + + if (!result) { + if (!output_json) { + std::cerr << "\nRun the `fmt` command without `--check/-c` to fix the " + "formatting" + << "\n"; + } + + throw Fail{EXIT_FAILURE}; + } } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index d4641188..57b221d1 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -37,7 +37,9 @@ add_jsonschema_test_unix(format/pass_multi_extension) add_jsonschema_test_unix(format/pass_yaml) add_jsonschema_test_unix(format/pass_check_yaml) add_jsonschema_test_unix(format/fail_check_single) +add_jsonschema_test_unix(format/fail_check_many) add_jsonschema_test_unix(format/pass_check_single) +add_jsonschema_test_unix(format/pass_check_single_verbose) add_jsonschema_test_unix(format/pass_check_single_keep_ordering) add_jsonschema_test_unix(format/pass_directory_ignore_directory) add_jsonschema_test_unix(format/pass_directory_ignore_file) @@ -53,6 +55,10 @@ add_jsonschema_test_unix(format/pass_default_dialect) add_jsonschema_test_unix(format/pass_default_dialect_config) add_jsonschema_test_unix(format/pass_default_dialect_config_relative) add_jsonschema_test_unix(format/pass_bignum_real) +add_jsonschema_test_unix(format/pass_check_single_json) +add_jsonschema_test_unix(format/fail_check_single_json) +add_jsonschema_test_unix(format/fail_check_many_json) +add_jsonschema_test_unix(format/fail_check_single_json_indentation) # Validate add_jsonschema_test_unix(validate/fail_instance_enoent) diff --git a/test/format/fail_check_many.sh b/test/format/fail_check_many.sh new file mode 100755 index 00000000..b4825781 --- /dev/null +++ b/test/format/fail_check_many.sh @@ -0,0 +1,42 @@ +#!/bin/sh + +set -o errexit +set -o nounset + +TMP="$(mktemp -d)" +clean() { rm -rf "$TMP"; } +trap clean EXIT + +mkdir "$TMP/schemas" + +cat << 'EOF' > "$TMP/schemas/1.json" +{ + "type": 1, + "$schema": "http://json-schema.org/draft-04/schema#" +} +EOF + +cat << 'EOF' > "$TMP/schemas/2.json" +{ + "type": 1, + "$schema": "http://json-schema.org/draft-04/schema#" +} +EOF + +cat << 'EOF' > "$TMP/schemas/3.json" +{ + "$schema": "http://json-schema.org/draft-04/schema#" +} +EOF + +"$1" fmt "$TMP/schemas" --check >"$TMP/output.txt" 2>&1 && CODE="$?" || CODE="$?" +test "$CODE" = "1" || exit 1 + +cat << EOF > "$TMP/expected.txt" +fail: $(realpath "$TMP")/schemas/1.json +fail: $(realpath "$TMP")/schemas/2.json + +Run the \`fmt\` command without \`--check/-c\` to fix the formatting +EOF + +diff "$TMP/output.txt" "$TMP/expected.txt" diff --git a/test/format/fail_check_many_json.sh b/test/format/fail_check_many_json.sh new file mode 100755 index 00000000..335bc427 --- /dev/null +++ b/test/format/fail_check_many_json.sh @@ -0,0 +1,45 @@ +#!/bin/sh + +set -o errexit +set -o nounset + +TMP="$(mktemp -d)" +clean() { rm -rf "$TMP"; } +trap clean EXIT + +mkdir "$TMP/schemas" + +cat << 'EOF' > "$TMP/schemas/1.json" +{ + "type": 1, + "$schema": "http://json-schema.org/draft-04/schema#" +} +EOF + +cat << 'EOF' > "$TMP/schemas/2.json" +{ + "type": 1, + "$schema": "http://json-schema.org/draft-04/schema#" +} +EOF + +cat << 'EOF' > "$TMP/schemas/3.json" +{ + "$schema": "http://json-schema.org/draft-04/schema#" +} +EOF + +"$1" fmt "$TMP/schemas" --check --json >"$TMP/output.json" 2>&1 && CODE="$?" || CODE="$?" +test "$CODE" = "1" || exit 1 + +cat << EOF > "$TMP/expected.json" +{ + "valid": false, + "errors": [ + "$(realpath "$TMP")/schemas/1.json", + "$(realpath "$TMP")/schemas/2.json" + ] +} +EOF + +diff "$TMP/output.json" "$TMP/expected.json" diff --git a/test/format/fail_check_single.sh b/test/format/fail_check_single.sh index 229bbebb..ff5440fc 100755 --- a/test/format/fail_check_single.sh +++ b/test/format/fail_check_single.sh @@ -14,26 +14,16 @@ cat << 'EOF' > "$TMP/schema.json" } EOF -"$1" fmt "$TMP/schema.json" --check 2>"$TMP/stderr.txt" && CODE="$?" || CODE="$?" +"$1" fmt "$TMP/schema.json" --check >"$TMP/output.txt" 2>&1 && CODE="$?" || CODE="$?" test "$CODE" = "1" || exit 1 -cat << EOF > "$TMP/error.txt" -FAIL: $(realpath "$TMP")/schema.json -Got: -{ - "type": 1, - "\$schema": "http://json-schema.org/draft-04/schema#" -} - -But expected: -{ - "\$schema": "http://json-schema.org/draft-04/schema#", - "type": 1 -} +cat << EOF > "$TMP/expected.txt" +fail: $(realpath "$TMP")/schema.json +Run the \`fmt\` command without \`--check/-c\` to fix the formatting EOF -diff "$TMP/stderr.txt" "$TMP/error.txt" +diff "$TMP/output.txt" "$TMP/expected.txt" cat << 'EOF' > "$TMP/expected.json" { diff --git a/test/format/fail_check_single_indentation.sh b/test/format/fail_check_single_indentation.sh index 502e73ef..bf7578df 100755 --- a/test/format/fail_check_single_indentation.sh +++ b/test/format/fail_check_single_indentation.sh @@ -14,26 +14,16 @@ cat << 'EOF' > "$TMP/schema.json" } EOF -"$1" fmt "$TMP/schema.json" --indentation 4 --check 2>"$TMP/stderr.txt" && CODE="$?" || CODE="$?" +"$1" fmt "$TMP/schema.json" --indentation 4 --check >"$TMP/output.txt" 2>&1 && CODE="$?" || CODE="$?" test "$CODE" = "1" || exit 1 -cat << EOF > "$TMP/error.txt" -FAIL: $(realpath "$TMP")/schema.json -Got: -{ - "\$schema": "http://json-schema.org/draft-04/schema#", - "type": 1 -} - -But expected: -{ - "\$schema": "http://json-schema.org/draft-04/schema#", - "type": 1 -} +cat << EOF > "$TMP/expected.txt" +fail: $(realpath "$TMP")/schema.json +Run the \`fmt\` command without \`--check/-c\` to fix the formatting EOF -diff "$TMP/stderr.txt" "$TMP/error.txt" +diff "$TMP/output.txt" "$TMP/expected.txt" cat << 'EOF' > "$TMP/expected.json" { diff --git a/test/format/fail_check_single_invalid.sh b/test/format/fail_check_single_invalid.sh index 05fa77cf..eaa6bcf7 100755 --- a/test/format/fail_check_single_invalid.sh +++ b/test/format/fail_check_single_invalid.sh @@ -14,5 +14,14 @@ cat << 'EOF' > "$TMP/schema.json" } EOF -"$1" fmt "$TMP/schema.json" --check 2>"$TMP/output.txt" && CODE="$?" || CODE="$?" +"$1" fmt "$TMP/schema.json" --check >"$TMP/output.txt" 2>&1 && CODE="$?" || CODE="$?" test "$CODE" = "1" || exit 1 + +cat << EOF > "$TMP/expected.txt" +error: Failed to parse the JSON document + at line 3 + at column 3 + at file path $(realpath "$TMP")/schema.json +EOF + +diff "$TMP/output.txt" "$TMP/expected.txt" diff --git a/test/format/fail_check_single_json.sh b/test/format/fail_check_single_json.sh new file mode 100755 index 00000000..64156e2b --- /dev/null +++ b/test/format/fail_check_single_json.sh @@ -0,0 +1,32 @@ +#!/bin/sh + +set -o errexit +set -o nounset + +TMP="$(mktemp -d)" +clean() { rm -rf "$TMP"; } +trap clean EXIT + +mkdir -p "$TMP/this/is/a/very/very/very/long/path" + +cat << 'EOF' > "$TMP/this/is/a/very/very/very/long/path/schema.json" +{ + "type": 1, + "$schema": "http://json-schema.org/draft-04/schema#" +} +EOF + +"$1" fmt "$TMP/this/is/a/very/very/very/long/path/schema.json" \ + --check --json >"$TMP/output.json" 2>&1 && CODE="$?" || CODE="$?" +test "$CODE" = "1" || exit 1 + +cat << EOF > "$TMP/expected.json" +{ + "valid": false, + "errors": [ + "$(realpath "$TMP")/this/is/a/very/very/very/long/path/schema.json" + ] +} +EOF + +diff "$TMP/output.json" "$TMP/expected.json" diff --git a/test/format/fail_check_single_json_indentation.sh b/test/format/fail_check_single_json_indentation.sh new file mode 100755 index 00000000..bd776ece --- /dev/null +++ b/test/format/fail_check_single_json_indentation.sh @@ -0,0 +1,32 @@ +#!/bin/sh + +set -o errexit +set -o nounset + +TMP="$(mktemp -d)" +clean() { rm -rf "$TMP"; } +trap clean EXIT + +mkdir -p "$TMP/this/is/a/very/very/very/long/path" + +cat << 'EOF' > "$TMP/this/is/a/very/very/very/long/path/schema.json" +{ + "type": 1, + "$schema": "http://json-schema.org/draft-04/schema#" +} +EOF + +"$1" fmt "$TMP/this/is/a/very/very/very/long/path/schema.json" \ + --check --json --indentation 4 >"$TMP/output.json" 2>&1 && CODE="$?" || CODE="$?" +test "$CODE" = "1" || exit 1 + +cat << EOF > "$TMP/expected.json" +{ + "valid": false, + "errors": [ + "$(realpath "$TMP")/this/is/a/very/very/very/long/path/schema.json" + ] +} +EOF + +diff "$TMP/output.json" "$TMP/expected.json" diff --git a/test/format/fail_invalid_path.sh b/test/format/fail_invalid_path.sh index a147ca2d..5d2dc415 100755 --- a/test/format/fail_invalid_path.sh +++ b/test/format/fail_invalid_path.sh @@ -7,5 +7,12 @@ TMP="$(mktemp -d)" clean() { rm -rf "$TMP"; } trap clean EXIT -"$1" fmt "$TMP/does_not_exist.json" && CODE="$?" || CODE="$?" +"$1" fmt "$TMP/does_not_exist.json" >"$TMP/output.txt" 2>&1 && CODE="$?" || CODE="$?" test "$CODE" = "1" || exit 1 + +cat << EOF > "$TMP/expected.txt" +error: No such file or directory + at file path $(realpath "$TMP")/does_not_exist.json +EOF + +diff "$TMP/output.txt" "$TMP/expected.txt" diff --git a/test/format/fail_no_dialect.sh b/test/format/fail_no_dialect.sh index c55ae527..150f2508 100755 --- a/test/format/fail_no_dialect.sh +++ b/test/format/fail_no_dialect.sh @@ -17,7 +17,7 @@ cat << 'EOF' > "$TMP/schema.json" } EOF -"$1" fmt "$TMP/schema.json" 2>"$TMP/stderr.txt" && CODE="$?" || CODE="$?" +"$1" fmt "$TMP/schema.json" >"$TMP/output.txt" 2>&1 && CODE="$?" || CODE="$?" test "$CODE" = "1" || exit 1 cat << EOF > "$TMP/expected.txt" @@ -28,16 +28,16 @@ If the input does not declare the \`\$schema\` keyword, you might want to explicitly declare a default dialect using \`--default-dialect/-d\` EOF -diff "$TMP/stderr.txt" "$TMP/expected.txt" +diff "$TMP/output.txt" "$TMP/expected.txt" # JSON error -"$1" fmt "$TMP/schema.json" --json >"$TMP/stdout.txt" && CODE="$?" || CODE="$?" +"$1" fmt "$TMP/schema.json" --json >"$TMP/output_json.txt" 2>&1 && CODE="$?" || CODE="$?" test "$CODE" = "1" || exit 1 -cat << 'EOF' > "$TMP/expected.txt" +cat << 'EOF' > "$TMP/expected_json.txt" { "error": "Could not determine the base dialect of the schema" } EOF -diff "$TMP/stdout.txt" "$TMP/expected.txt" +diff "$TMP/output_json.txt" "$TMP/expected_json.txt" diff --git a/test/format/fail_unknown_metaschema.sh b/test/format/fail_unknown_metaschema.sh index 6d565fdf..e1b15c7d 100755 --- a/test/format/fail_unknown_metaschema.sh +++ b/test/format/fail_unknown_metaschema.sh @@ -18,7 +18,7 @@ cat << 'EOF' > "$TMP/schema.json" } EOF -"$1" fmt "$TMP/schema.json" 2>"$TMP/stderr.txt" && CODE="$?" || CODE="$?" +"$1" fmt "$TMP/schema.json" >"$TMP/output.txt" 2>&1 && CODE="$?" || CODE="$?" test "$CODE" = "1" || exit 1 cat << EOF > "$TMP/expected.txt" @@ -28,17 +28,17 @@ error: Could not resolve the metaschema of the schema This is likely because you forgot to import such schema using \`--resolve/-r\` EOF -diff "$TMP/stderr.txt" "$TMP/expected.txt" +diff "$TMP/output.txt" "$TMP/expected.txt" # JSON error -"$1" fmt "$TMP/schema.json" --json >"$TMP/stdout.txt" && CODE="$?" || CODE="$?" +"$1" fmt "$TMP/schema.json" --json >"$TMP/output_json.txt" 2>&1 && CODE="$?" || CODE="$?" test "$CODE" = "1" || exit 1 -cat << EOF > "$TMP/expected.txt" +cat << EOF > "$TMP/expected_json.txt" { "error": "Could not resolve the metaschema of the schema", "identifier": "https://example.com/unknown" } EOF -diff "$TMP/stdout.txt" "$TMP/expected.txt" +diff "$TMP/output_json.txt" "$TMP/expected_json.txt" diff --git a/test/format/pass_bignum_real.sh b/test/format/pass_bignum_real.sh index 047ec531..3a037d24 100755 --- a/test/format/pass_bignum_real.sh +++ b/test/format/pass_bignum_real.sh @@ -14,7 +14,12 @@ cat << 'EOF' > "$TMP/schema.json" } EOF -"$1" fmt "$TMP/schema.json" +"$1" fmt "$TMP/schema.json" >"$TMP/output.txt" 2>&1 + +cat << 'EOF' > "$TMP/expected_output.txt" +EOF + +diff "$TMP/output.txt" "$TMP/expected_output.txt" cat << 'EOF' > "$TMP/expected.json" { diff --git a/test/format/pass_check_single.sh b/test/format/pass_check_single.sh index d0b045ba..f1242d42 100755 --- a/test/format/pass_check_single.sh +++ b/test/format/pass_check_single.sh @@ -14,7 +14,12 @@ cat << 'EOF' > "$TMP/schema.json" } EOF -"$1" fmt "$TMP/schema.json" --check +"$1" fmt "$TMP/schema.json" --check >"$TMP/output.txt" 2>&1 + +cat << 'EOF' > "$TMP/expected_output.txt" +EOF + +diff "$TMP/output.txt" "$TMP/expected_output.txt" cat << 'EOF' > "$TMP/expected.json" { diff --git a/test/format/pass_check_single_indentation.sh b/test/format/pass_check_single_indentation.sh index ffab28ff..8a075d07 100755 --- a/test/format/pass_check_single_indentation.sh +++ b/test/format/pass_check_single_indentation.sh @@ -14,7 +14,12 @@ cat << 'EOF' > "$TMP/schema.json" } EOF -"$1" fmt "$TMP/schema.json" --check --indentation 4 +"$1" fmt "$TMP/schema.json" --check --indentation 4 >"$TMP/output.txt" 2>&1 + +cat << 'EOF' > "$TMP/expected_output.txt" +EOF + +diff "$TMP/output.txt" "$TMP/expected_output.txt" cat << 'EOF' > "$TMP/expected.json" { diff --git a/test/format/pass_check_single_json.sh b/test/format/pass_check_single_json.sh new file mode 100755 index 00000000..9003789d --- /dev/null +++ b/test/format/pass_check_single_json.sh @@ -0,0 +1,25 @@ +#!/bin/sh + +set -o errexit +set -o nounset + +TMP="$(mktemp -d)" +clean() { rm -rf "$TMP"; } +trap clean EXIT + +cat << 'EOF' > "$TMP/schema.json" +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "type": 1 +} +EOF + +"$1" fmt "$TMP/schema.json" --check --json >"$TMP/output.json" 2>&1 + +cat << 'EOF' > "$TMP/expected.json" +{ + "valid": true +} +EOF + +diff "$TMP/output.json" "$TMP/expected.json" diff --git a/test/format/pass_check_single_keep_ordering.sh b/test/format/pass_check_single_keep_ordering.sh index c98e7f86..8a432d16 100755 --- a/test/format/pass_check_single_keep_ordering.sh +++ b/test/format/pass_check_single_keep_ordering.sh @@ -14,7 +14,12 @@ cat << 'EOF' > "$TMP/schema.json" } EOF -"$1" fmt "$TMP/schema.json" --check --keep-ordering +"$1" fmt "$TMP/schema.json" --check --keep-ordering >"$TMP/output.txt" 2>&1 + +cat << 'EOF' > "$TMP/expected_output.txt" +EOF + +diff "$TMP/output.txt" "$TMP/expected_output.txt" cat << 'EOF' > "$TMP/expected.json" { diff --git a/test/format/pass_check_single_verbose.sh b/test/format/pass_check_single_verbose.sh new file mode 100755 index 00000000..78e13849 --- /dev/null +++ b/test/format/pass_check_single_verbose.sh @@ -0,0 +1,33 @@ +#!/bin/sh + +set -o errexit +set -o nounset + +TMP="$(mktemp -d)" +clean() { rm -rf "$TMP"; } +trap clean EXIT + +cat << 'EOF' > "$TMP/schema.json" +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "type": 1 +} +EOF + +"$1" fmt "$TMP/schema.json" --check --verbose >"$TMP/output.txt" 2>&1 + +cat << EOF > "$TMP/expected_output.txt" +Checking: $(realpath "$TMP")/schema.json +ok: $(realpath "$TMP")/schema.json +EOF + +diff "$TMP/output.txt" "$TMP/expected_output.txt" + +cat << 'EOF' > "$TMP/expected.json" +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "type": 1 +} +EOF + +diff "$TMP/schema.json" "$TMP/expected.json" diff --git a/test/format/pass_check_yaml.sh b/test/format/pass_check_yaml.sh index 14bdead5..bbd6d7c7 100755 --- a/test/format/pass_check_yaml.sh +++ b/test/format/pass_check_yaml.sh @@ -12,12 +12,12 @@ type: 1 $schema: http://json-schema.org/draft-04/schema# EOF -"$1" fmt "$TMP/schema.yaml" --check 2>"$TMP/stderr.txt" && CODE="$?" || CODE="$?" +"$1" fmt "$TMP/schema.yaml" --check >"$TMP/output.txt" 2>&1 && CODE="$?" || CODE="$?" test "$CODE" = "1" || exit 1 -cat << EOF > "$TMP/error.txt" +cat << EOF > "$TMP/expected.txt" error: This command does not support YAML input files yet at file path $(realpath "$TMP/schema.yaml") EOF -diff "$TMP/stderr.txt" "$TMP/error.txt" +diff "$TMP/output.txt" "$TMP/expected.txt" diff --git a/test/format/pass_config_path.sh b/test/format/pass_config_path.sh index 1700363c..ebeb20d1 100755 --- a/test/format/pass_config_path.sh +++ b/test/format/pass_config_path.sh @@ -26,7 +26,7 @@ cat << 'EOF' > "$TMP/jsonschema.json" EOF cd "$TMP/bar" -"$1" fmt --verbose 2> "$TMP/output.txt" +"$1" fmt --verbose >"$TMP/output.txt" 2>&1 cat << EOF > "$TMP/expected.txt" Using configuration file: $(realpath "$TMP")/jsonschema.json diff --git a/test/format/pass_cwd.sh b/test/format/pass_cwd.sh index 9888eafa..2347b398 100755 --- a/test/format/pass_cwd.sh +++ b/test/format/pass_cwd.sh @@ -21,7 +21,12 @@ cat << 'EOF' > "$TMP/schema_2.json" EOF cd "$TMP" -"$1" fmt +"$1" fmt >"$TMP/output.txt" 2>&1 + +cat << 'EOF' > "$TMP/expected_output.txt" +EOF + +diff "$TMP/output.txt" "$TMP/expected_output.txt" cat << 'EOF' > "$TMP/expected_1.json" { diff --git a/test/format/pass_default_dialect.sh b/test/format/pass_default_dialect.sh index ce75267a..d2d5b869 100755 --- a/test/format/pass_default_dialect.sh +++ b/test/format/pass_default_dialect.sh @@ -15,7 +15,12 @@ cat << 'EOF' > "$TMP/schema.json" } EOF -"$1" fmt "$TMP/schema.json" --default-dialect "https://json-schema.org/draft/2020-12/schema" +"$1" fmt "$TMP/schema.json" --default-dialect "https://json-schema.org/draft/2020-12/schema" >"$TMP/output.txt" 2>&1 + +cat << 'EOF' > "$TMP/expected_output.txt" +EOF + +diff "$TMP/output.txt" "$TMP/expected_output.txt" cat << 'EOF' > "$TMP/expected.json" { diff --git a/test/format/pass_default_dialect_config.sh b/test/format/pass_default_dialect_config.sh index e61a2e5c..55c0f18c 100755 --- a/test/format/pass_default_dialect_config.sh +++ b/test/format/pass_default_dialect_config.sh @@ -21,7 +21,7 @@ cat << 'EOF' > "$TMP/jsonschema.json" } EOF -"$1" fmt "$TMP/schema.json" --verbose 2> "$TMP/output.txt" +"$1" fmt "$TMP/schema.json" --verbose >"$TMP/output.txt" 2>&1 cat << 'EOF' > "$TMP/expected.json" { @@ -36,9 +36,9 @@ EOF diff "$TMP/schema.json" "$TMP/expected.json" -cat << EOF > "$TMP/expected_log.txt" +cat << EOF > "$TMP/expected_output.txt" Formatting: $(realpath "$TMP")/schema.json Using configuration file: $(realpath "$TMP")/jsonschema.json EOF -diff "$TMP/output.txt" "$TMP/expected_log.txt" +diff "$TMP/output.txt" "$TMP/expected_output.txt" diff --git a/test/format/pass_default_dialect_config_relative.sh b/test/format/pass_default_dialect_config_relative.sh index cf3b5a92..513018e6 100755 --- a/test/format/pass_default_dialect_config_relative.sh +++ b/test/format/pass_default_dialect_config_relative.sh @@ -23,7 +23,7 @@ EOF cd "$TMP" -"$1" fmt schema.json --verbose 2> "$TMP/output.txt" +"$1" fmt schema.json --verbose >"$TMP/output.txt" 2>&1 cat << 'EOF' > "$TMP/expected.json" { @@ -38,9 +38,9 @@ EOF diff "$TMP/schema.json" "$TMP/expected.json" -cat << EOF > "$TMP/expected_log.txt" +cat << EOF > "$TMP/expected_output.txt" Formatting: $(realpath "$TMP")/schema.json Using configuration file: $(realpath "$TMP")/jsonschema.json EOF -diff "$TMP/output.txt" "$TMP/expected_log.txt" +diff "$TMP/output.txt" "$TMP/expected_output.txt" diff --git a/test/format/pass_directory.sh b/test/format/pass_directory.sh index 57b8ae11..aa9a43d7 100755 --- a/test/format/pass_directory.sh +++ b/test/format/pass_directory.sh @@ -20,7 +20,12 @@ cat << 'EOF' > "$TMP/schema_2.json" {"$schema": "https://json-schema.org/draft/2020-12/schema", "type": "string", "title": "My String"} EOF -"$1" fmt "$TMP" +"$1" fmt "$TMP" >"$TMP/output.txt" 2>&1 + +cat << 'EOF' > "$TMP/expected_output.txt" +EOF + +diff "$TMP/output.txt" "$TMP/expected_output.txt" cat << 'EOF' > "$TMP/expected_1.json" { diff --git a/test/format/pass_directory_ignore_directory.sh b/test/format/pass_directory_ignore_directory.sh index ac2afd81..bbf3f85a 100755 --- a/test/format/pass_directory_ignore_directory.sh +++ b/test/format/pass_directory_ignore_directory.sh @@ -21,7 +21,12 @@ cat << 'EOF' > "$TMP/nested/schema_2.json" {"type": "string", "title": "My String"} EOF -"$1" fmt "$TMP" --ignore "$TMP/nested" +"$1" fmt "$TMP" --ignore "$TMP/nested" >"$TMP/output.txt" 2>&1 + +cat << 'EOF' > "$TMP/expected_output.txt" +EOF + +diff "$TMP/output.txt" "$TMP/expected_output.txt" cat << 'EOF' > "$TMP/expected_1.json" { diff --git a/test/format/pass_directory_ignore_file.sh b/test/format/pass_directory_ignore_file.sh index 2216dd37..703c21ec 100755 --- a/test/format/pass_directory_ignore_file.sh +++ b/test/format/pass_directory_ignore_file.sh @@ -20,7 +20,12 @@ cat << 'EOF' > "$TMP/schema_2.json" {"type": "string", "title": "My String"} EOF -"$1" fmt "$TMP" --ignore "$TMP/schema_2.json" +"$1" fmt "$TMP" --ignore "$TMP/schema_2.json" >"$TMP/output.txt" 2>&1 + +cat << 'EOF' > "$TMP/expected_output.txt" +EOF + +diff "$TMP/output.txt" "$TMP/expected_output.txt" cat << 'EOF' > "$TMP/expected_1.json" { diff --git a/test/format/pass_multi_extension.sh b/test/format/pass_multi_extension.sh index 6b51cbff..cfb17c5e 100755 --- a/test/format/pass_multi_extension.sh +++ b/test/format/pass_multi_extension.sh @@ -20,7 +20,14 @@ cat << 'EOF' > "$TMP/schema_2.schema.json" EOF cd "$TMP" -"$1" fmt --extension .schema.json -v +"$1" fmt --extension .schema.json -v >"$TMP/output.txt" 2>&1 + +cat << EOF > "$TMP/expected_output.txt" +Using extension: .schema.json +Formatting: $(realpath "$TMP")/schema_2.schema.json +EOF + +diff "$TMP/output.txt" "$TMP/expected_output.txt" cat << 'EOF' > "$TMP/expected_1.json" { diff --git a/test/format/pass_single.sh b/test/format/pass_single.sh index 7c6e9c94..46f9c744 100755 --- a/test/format/pass_single.sh +++ b/test/format/pass_single.sh @@ -16,7 +16,12 @@ cat << 'EOF' > "$TMP/schema.json" } EOF -"$1" fmt "$TMP/schema.json" +"$1" fmt "$TMP/schema.json" >"$TMP/output.txt" 2>&1 + +cat << 'EOF' > "$TMP/expected_output.txt" +EOF + +diff "$TMP/output.txt" "$TMP/expected_output.txt" cat << 'EOF' > "$TMP/expected.json" { diff --git a/test/format/pass_single_indentation.sh b/test/format/pass_single_indentation.sh index 29f04f1e..a6278c55 100755 --- a/test/format/pass_single_indentation.sh +++ b/test/format/pass_single_indentation.sh @@ -16,7 +16,12 @@ cat << 'EOF' > "$TMP/schema.json" } EOF -"$1" fmt "$TMP/schema.json" --indentation 4 +"$1" fmt "$TMP/schema.json" --indentation 4 >"$TMP/output.txt" 2>&1 + +cat << 'EOF' > "$TMP/expected_output.txt" +EOF + +diff "$TMP/output.txt" "$TMP/expected_output.txt" cat << 'EOF' > "$TMP/expected.json" { diff --git a/test/format/pass_single_intact_mtime.sh b/test/format/pass_single_intact_mtime.sh index 4e705cb7..e397c3ef 100755 --- a/test/format/pass_single_intact_mtime.sh +++ b/test/format/pass_single_intact_mtime.sh @@ -26,9 +26,14 @@ EOF mtime_old="$(mtime "$TMP/schema.json")" sleep 1 -"$1" fmt "$TMP/schema.json" +"$1" fmt "$TMP/schema.json" >"$TMP/output.txt" 2>&1 mtime_new="$(mtime "$TMP/schema.json")" +cat << 'EOF' > "$TMP/expected_output.txt" +EOF + +diff "$TMP/output.txt" "$TMP/expected_output.txt" + if [ "$mtime_old" != "$mtime_new" ] then echo "The file was unexpectedly modified" 1>&2 diff --git a/test/format/pass_single_keep_ordering.sh b/test/format/pass_single_keep_ordering.sh index c22ee47a..d563b5c0 100755 --- a/test/format/pass_single_keep_ordering.sh +++ b/test/format/pass_single_keep_ordering.sh @@ -15,7 +15,12 @@ cat << 'EOF' > "$TMP/schema.json" } EOF -"$1" fmt --keep-ordering "$TMP/schema.json" +"$1" fmt --keep-ordering "$TMP/schema.json" >"$TMP/output.txt" 2>&1 + +cat << 'EOF' > "$TMP/expected_output.txt" +EOF + +diff "$TMP/output.txt" "$TMP/expected_output.txt" cat << 'EOF' > "$TMP/expected.json" { diff --git a/test/format/pass_yaml.sh b/test/format/pass_yaml.sh index 5db77a8b..047da5fc 100755 --- a/test/format/pass_yaml.sh +++ b/test/format/pass_yaml.sh @@ -12,15 +12,15 @@ additionalProperties: false title: Hello World EOF -"$1" fmt "$TMP/schema.yaml" 2>"$TMP/stderr.txt" && CODE="$?" || CODE="$?" +"$1" fmt "$TMP/schema.yaml" >"$TMP/output.txt" 2>&1 && CODE="$?" || CODE="$?" test "$CODE" = "1" || exit 1 -cat << EOF > "$TMP/error.txt" +cat << EOF > "$TMP/expected.txt" error: This command does not support YAML input files yet at file path $(realpath "$TMP/schema.yaml") EOF -diff "$TMP/stderr.txt" "$TMP/error.txt" +diff "$TMP/output.txt" "$TMP/expected.txt" cat << EOF > "$TMP/expected.yaml" additionalProperties: false