From 457bff1e3cda550c7e83f1c5c7a3b6da341b3c04 Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Mon, 26 Aug 2024 15:58:57 -0400 Subject: [PATCH] Improve linter end-to-end tests Signed-off-by: Juan Cruz Viotti --- src/command_lint.cc | 7 +++-- test/CMakeLists.txt | 8 ++++-- test/lint/fail_lint.sh | 27 ++++++++++++++++++ test/{lint_fix.sh => lint/pass_lint_fix.sh} | 7 ++++- test/lint/pass_lint_no_fix.sh | 31 +++++++++++++++++++++ test/lint_fail.sh | 26 ----------------- test/lint_pass.sh | 17 ----------- 7 files changed, 73 insertions(+), 50 deletions(-) create mode 100755 test/lint/fail_lint.sh rename test/{lint_fix.sh => lint/pass_lint_fix.sh} (74%) create mode 100755 test/lint/pass_lint_no_fix.sh delete mode 100755 test/lint_fail.sh delete mode 100755 test/lint_pass.sh diff --git a/src/command_lint.cc b/src/command_lint.cc index 07da4ac0..58a0b4fa 100644 --- a/src/command_lint.cc +++ b/src/command_lint.cc @@ -48,10 +48,11 @@ auto sourcemeta::jsonschema::cli::lint( entry.second, sourcemeta::jsontoolkit::default_schema_walker, resolver(options), [&entry](const auto &pointer, const auto &name, const auto &message) { - std::cout << entry.first.string() << "\n"; - std::cout << " "; + std::cout << entry.first.string() << ":\n"; + std::cout << " " << message << " (" << name << ")\n"; + std::cout << " at schema location \""; sourcemeta::jsontoolkit::stringify(pointer, std::cout); - std::cout << " " << message << " (" << name << ")\n"; + std::cout << "\"\n"; }); if (subresult) { diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 4da6ec16..418e20a3 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -25,9 +25,6 @@ add_jsonschema_test_unix(format_check_single_pass) add_jsonschema_test_unix(format_directory_ignore_directory) add_jsonschema_test_unix(format_directory_ignore_file) add_jsonschema_test_unix(format_check_single_invalid) -add_jsonschema_test_unix(lint_pass) -add_jsonschema_test_unix(lint_fail) -add_jsonschema_test_unix(lint_fix) add_jsonschema_test_unix(metaschema_fail_directory) add_jsonschema_test_unix(metaschema_fail_single) add_jsonschema_test_unix(metaschema_fail_non_schema) @@ -162,6 +159,11 @@ add_jsonschema_test_unix(identify/pass_with_resolve_from_verbose) add_jsonschema_test_unix(identify/pass_with_unknown_dialect) add_jsonschema_test_unix(identify/pass_with_unknown_dialect_verbose) +# Lint +add_jsonschema_test_unix(lint/pass_lint_fix) +add_jsonschema_test_unix(lint/pass_lint_no_fix) +add_jsonschema_test_unix(lint/fail_lint) + # CI specific tests add_jsonschema_test_unix_ci(pass_bundle_http) add_jsonschema_test_unix_ci(fail_bundle_http_non_200) diff --git a/test/lint/fail_lint.sh b/test/lint/fail_lint.sh new file mode 100755 index 00000000..6fb2ad45 --- /dev/null +++ b/test/lint/fail_lint.sh @@ -0,0 +1,27 @@ +#!/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": "string", + "enum": [ "foo" ] +} +EOF + +"$1" lint "$TMP/schema.json" >"$TMP/stderr.txt" 2>&1 && CODE="$?" || CODE="$?" +test "$CODE" = "1" || exit 1 + +cat << EOF > "$TMP/expected.txt" +$(realpath "$TMP")/schema.json: + Setting \`type\` alongside \`enum\` is considered an anti-pattern, as the enumeration choices already imply their respective types (enum_with_type) + at schema location "" +EOF + +diff "$TMP/stderr.txt" "$TMP/expected.txt" diff --git a/test/lint_fix.sh b/test/lint/pass_lint_fix.sh similarity index 74% rename from test/lint_fix.sh rename to test/lint/pass_lint_fix.sh index 13fd5214..7c5f5c2e 100755 --- a/test/lint_fix.sh +++ b/test/lint/pass_lint_fix.sh @@ -15,7 +15,12 @@ cat << 'EOF' > "$TMP/schema.json" } EOF -"$1" lint "$TMP/schema.json" --fix +"$1" lint "$TMP/schema.json" --fix > "$TMP/result.txt" 2>&1 + +cat << 'EOF' > "$TMP/output.txt" +EOF + +diff "$TMP/result.txt" "$TMP/output.txt" cat << 'EOF' > "$TMP/expected.json" { diff --git a/test/lint/pass_lint_no_fix.sh b/test/lint/pass_lint_no_fix.sh new file mode 100755 index 00000000..b1830b8f --- /dev/null +++ b/test/lint/pass_lint_no_fix.sh @@ -0,0 +1,31 @@ +#!/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": "string" +} +EOF + +"$1" lint "$TMP/schema.json" > "$TMP/result.txt" 2>&1 + +cat << 'EOF' > "$TMP/output.txt" +EOF + +diff "$TMP/result.txt" "$TMP/output.txt" + +cat << 'EOF' > "$TMP/expected.json" +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "type": "string" +} +EOF + +diff "$TMP/schema.json" "$TMP/expected.json" diff --git a/test/lint_fail.sh b/test/lint_fail.sh deleted file mode 100755 index 224057b5..00000000 --- a/test/lint_fail.sh +++ /dev/null @@ -1,26 +0,0 @@ -#!/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": "string", - "enum": [ "foo" ] -} -EOF - -"$1" lint "$TMP/schema.json" && CODE="$?" || CODE="$?" - -if [ "$CODE" = "0" ] -then - echo "FAIL" 1>&2 - exit 1 -else - echo "PASS" 1>&2 -fi diff --git a/test/lint_pass.sh b/test/lint_pass.sh deleted file mode 100755 index 3eadfbf9..00000000 --- a/test/lint_pass.sh +++ /dev/null @@ -1,17 +0,0 @@ -#!/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": "string" -} -EOF - -"$1" lint "$TMP/schema.json"