Skip to content

Commit

Permalink
Improve linter end-to-end tests
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <[email protected]>
  • Loading branch information
jviotti committed Aug 26, 2024
1 parent fc1939c commit 457bff1
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 50 deletions.
7 changes: 4 additions & 3 deletions src/command_lint.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 5 additions & 3 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
27 changes: 27 additions & 0 deletions test/lint/fail_lint.sh
Original file line number Diff line number Diff line change
@@ -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"
7 changes: 6 additions & 1 deletion test/lint_fix.sh → test/lint/pass_lint_fix.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
{
Expand Down
31 changes: 31 additions & 0 deletions test/lint/pass_lint_no_fix.sh
Original file line number Diff line number Diff line change
@@ -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"
26 changes: 0 additions & 26 deletions test/lint_fail.sh

This file was deleted.

17 changes: 0 additions & 17 deletions test/lint_pass.sh

This file was deleted.

0 comments on commit 457bff1

Please sign in to comment.