Skip to content

Commit

Permalink
Increase test coverage of the bundle command
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <[email protected]>
  • Loading branch information
jviotti committed Jul 14, 2024
1 parent d5c7c9a commit fcc1749
Show file tree
Hide file tree
Showing 27 changed files with 568 additions and 85 deletions.
9 changes: 8 additions & 1 deletion src/command_bundle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ auto intelligence::jsonschema::cli::bundle(
const std::span<const std::string> &arguments) -> int {
const auto options{
parse_options(arguments, {"h", "http", "w", "without-id"})};
CLI_ENSURE(!options.at("").empty(), "You must pass a JSON Schema as input");

if (options.at("").size() < 1) {
std::cerr
<< "error: This command expects a path to a schema. For example:\n\n"
<< " jsonschema bundle path/to/schema.json\n";
return EXIT_FAILURE;
}

auto schema{sourcemeta::jsontoolkit::from_file(options.at("").front())};

if (options.contains("w") || options.contains("without-id")) {
Expand Down
5 changes: 2 additions & 3 deletions src/command_validate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ auto intelligence::jsonschema::cli::validate(

if (options.at("").size() < 1) {
std::cerr
<< "error: This command expects to pass a path to a schema and a\n"
<< "path to an instance to validate against the schema. For "
"example:\n\n"
<< "error: This command expects a path to a schema and a path to an\n"
<< "instance to validate against the schema. For example:\n\n"
<< " jsonschema validate path/to/schema.json path/to/instance.json\n";
return EXIT_FAILURE;
}
Expand Down
3 changes: 3 additions & 0 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ auto main(int argc, char *argv[]) noexcept -> int {
} catch (const sourcemeta::jsontoolkit::SchemaResolutionError &error) {
std::cerr << "error: " << error.what() << "\n at " << error.id() << "\n";
return EXIT_FAILURE;
} catch (const sourcemeta::jsontoolkit::SchemaError &error) {
std::cerr << "error: " << error.what() << "\n";
return EXIT_FAILURE;
} catch (const sourcemeta::jsontoolkit::SchemaVocabularyError &error) {
std::cerr << "error: " << error.what() << "\n " << error.uri()
<< "\n\nTo request support for it, please open an issue "
Expand Down
29 changes: 22 additions & 7 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ 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(frame)
add_jsonschema_test_unix(bundle_non_remote)
add_jsonschema_test_unix(bundle_into_resolve_directory)
add_jsonschema_test_unix(bundle_remote_single_schema)
add_jsonschema_test_unix(bundle_remote_no_http)
add_jsonschema_test_unix(bundle_remote_directory)
add_jsonschema_test_unix(bundle_remote_directory_without_id)
add_jsonschema_test_unix(lint_pass)
add_jsonschema_test_unix(lint_fail)
add_jsonschema_test_unix(lint_fix)
Expand Down Expand Up @@ -98,8 +92,29 @@ add_jsonschema_test_unix(test/pass_single_no_test_description_verbose)
add_jsonschema_test_unix(test/pass_multi_directory_resolve)
add_jsonschema_test_unix(test/pass_multi_directory_resolve_verbose)

# Bundle
add_jsonschema_test_unix(bundle/pass_into_resolve_directory)
add_jsonschema_test_unix(bundle/pass_resolve_directory)
add_jsonschema_test_unix(bundle/pass_resolve_directory_verbose)
add_jsonschema_test_unix(bundle/pass_resolve_metaschema)
add_jsonschema_test_unix(bundle/pass_resolve_single)
add_jsonschema_test_unix(bundle/pass_resolve_with_ignore)
add_jsonschema_test_unix(bundle/pass_without_id)
add_jsonschema_test_unix(bundle/pass_without_id_verbose)
add_jsonschema_test_unix(bundle/pass_without_remote)
add_jsonschema_test_unix(bundle/fail_no_schema)
add_jsonschema_test_unix(bundle/fail_relative_external_ref_missing)
add_jsonschema_test_unix(bundle/fail_resolve_duplicate)
add_jsonschema_test_unix(bundle/fail_resolve_invalid_json)
add_jsonschema_test_unix(bundle/fail_schema_invalid_json)
add_jsonschema_test_unix(bundle/fail_unknown_metaschema)

# CI specific tests
add_jsonschema_test_unix_ci(bundle_remote_http)
add_jsonschema_test_unix_ci(pass_bundle_http)
add_jsonschema_test_unix_ci(fail_bundle_http_non_200)
add_jsonschema_test_unix_ci(fail_bundle_http_non_200_verbose)
add_jsonschema_test_unix_ci(fail_bundle_http_non_schema)
add_jsonschema_test_unix_ci(fail_bundle_http_non_schema_verbose)
add_jsonschema_test_unix_ci(fail_validate_http_non_200)
add_jsonschema_test_unix_ci(fail_validate_http_non_200_verbose)
add_jsonschema_test_unix_ci(fail_validate_http_non_schema)
Expand Down
19 changes: 19 additions & 0 deletions test/bundle/fail_no_schema.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/sh

set -o errexit
set -o nounset

TMP="$(mktemp -d)"
clean() { rm -rf "$TMP"; }
trap clean EXIT

"$1" bundle 2>"$TMP/stderr.txt" && CODE="$?" || CODE="$?"
test "$CODE" = "1" || exit 1

cat << 'EOF' > "$TMP/expected.txt"
error: This command expects a path to a schema. For example:
jsonschema bundle path/to/schema.json
EOF

diff "$TMP/stderr.txt" "$TMP/expected.txt"
26 changes: 26 additions & 0 deletions test/bundle/fail_relative_external_ref_missing.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/sh

set -o errexit
set -o nounset

TMP="$(mktemp -d)"
clean() { rm -rf "$TMP"; }
trap clean EXIT

cat << 'EOF' > "$TMP/schema.json"
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com",
"$ref": "nested"
}
EOF

"$1" bundle "$TMP/schema.json" 2>"$TMP/stderr.txt" && CODE="$?" || CODE="$?"
test "$CODE" = "1" || exit 1

cat << EOF > "$TMP/expected.txt"
error: Could not resolve the requested schema
at https://example.com/nested
EOF

diff "$TMP/stderr.txt" "$TMP/expected.txt"
46 changes: 46 additions & 0 deletions test/bundle/fail_resolve_duplicate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/sh

set -o errexit
set -o nounset

TMP="$(mktemp -d)"
clean() { rm -rf "$TMP"; }
trap clean EXIT

cat << 'EOF' > "$TMP/schema.json"
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com",
"$ref": "nested"
}
EOF

mkdir "$TMP/schemas"

cat << 'EOF' > "$TMP/schemas/remote.json"
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/nested",
"type": "string"
}
EOF

cat << 'EOF' > "$TMP/schemas/duplicated.json"
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/nested",
"type": "number"
}
EOF

"$1" bundle "$TMP/schema.json" \
--resolve "$TMP/schemas" 2>"$TMP/stderr.txt" \
&& CODE="$?" || CODE="$?"
test "$CODE" = "1" || exit 1

cat << EOF > "$TMP/expected.txt"
error: Cannot register the same identifier twice: https://example.com/nested
EOF

diff "$TMP/stderr.txt" "$TMP/expected.txt"

32 changes: 32 additions & 0 deletions test/bundle/fail_resolve_invalid_json.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/sh

set -o errexit
set -o nounset

TMP="$(mktemp -d)"
clean() { rm -rf "$TMP"; }
trap clean EXIT

cat << 'EOF' > "$TMP/schema.json"
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com",
"$ref": "nested"
}
EOF

cat << 'EOF' > "$TMP/invalid.json"
{ xxx }
EOF

"$1" bundle "$TMP/schema.json" \
--resolve "$TMP/invalid.json" 2>"$TMP/stderr.txt" \
&& CODE="$?" || CODE="$?"
test "$CODE" = "1" || exit 1

cat << EOF > "$TMP/expected.txt"
error: Failed to parse the JSON document at line 1 and column 3
$(realpath "$TMP")/invalid.json
EOF

diff "$TMP/stderr.txt" "$TMP/expected.txt"
25 changes: 25 additions & 0 deletions test/bundle/fail_schema_invalid_json.sh
Original file line number Diff line number Diff line change
@@ -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"
{
"type" string
}
EOF

"$1" bundle "$TMP/schema.json" 2>"$TMP/stderr.txt" \
&& CODE="$?" || CODE="$?"
test "$CODE" = "1" || exit 1

cat << EOF > "$TMP/expected.txt"
error: Failed to parse the JSON document at line 2 and column 10
$(realpath "$TMP")/schema.json
EOF

diff "$TMP/stderr.txt" "$TMP/expected.txt"
26 changes: 26 additions & 0 deletions test/bundle/fail_unknown_metaschema.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/sh

set -o errexit
set -o nounset

TMP="$(mktemp -d)"
clean() { rm -rf "$TMP"; }
trap clean EXIT

cat << 'EOF' > "$TMP/schema.json"
{
"$schema": "https://example.com/unknown",
"$id": "https://example.com",
"$ref": "nested"
}
EOF

"$1" bundle "$TMP/schema.json" 2>"$TMP/stderr.txt" && CODE="$?" || CODE="$?"
test "$CODE" = "1" || exit 1

cat << EOF > "$TMP/expected.txt"
error: Could not resolve the requested schema
at https://example.com/unknown
EOF

diff "$TMP/stderr.txt" "$TMP/expected.txt"
File renamed without changes.
File renamed without changes.
45 changes: 45 additions & 0 deletions test/bundle/pass_resolve_directory_verbose.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/sh

set -o errexit
set -o nounset

TMP="$(mktemp -d)"
clean() { rm -rf "$TMP"; }
trap clean EXIT

cat << 'EOF' > "$TMP/schema.json"
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com",
"$ref": "nested"
}
EOF

mkdir "$TMP/schemas"
cat << 'EOF' > "$TMP/schemas/remote.json"
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/nested",
"type": "string"
}
EOF

"$1" bundle "$TMP/schema.json" --resolve "$TMP/schemas" --verbose 1> "$TMP/result.json" 2>&1

cat << EOF > "$TMP/expected.json"
Importing schema into the resolution context: $(realpath "$TMP")/schemas/remote.json
{
"\$schema": "https://json-schema.org/draft/2020-12/schema",
"\$id": "https://example.com",
"\$ref": "nested",
"\$defs": {
"https://example.com/nested": {
"\$schema": "https://json-schema.org/draft/2020-12/schema",
"\$id": "https://example.com/nested",
"type": "string"
}
}
}
EOF

diff "$TMP/result.json" "$TMP/expected.json"
77 changes: 77 additions & 0 deletions test/bundle/pass_resolve_metaschema.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/bin/sh

set -o errexit
set -o nounset

TMP="$(mktemp -d)"
clean() { rm -rf "$TMP"; }
trap clean EXIT

cat << 'EOF' > "$TMP/schema.json"
{
"$schema": "https://example.com/meta",
"$id": "https://example.com",
"$ref": "nested"
}
EOF

mkdir "$TMP/schemas"

cat << 'EOF' > "$TMP/schemas/meta.json"
{
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$id": "https://example.com/meta",
"$vocabulary": {
"https://json-schema.org/draft/2019-09/vocab/core": true,
"https://json-schema.org/draft/2019-09/vocab/applicator": true,
"https://json-schema.org/draft/2019-09/vocab/validation": true,
"https://json-schema.org/draft/2019-09/vocab/meta-data": true,
"https://json-schema.org/draft/2019-09/vocab/format": false,
"https://json-schema.org/draft/2019-09/vocab/content": true
},
"$ref": "https://json-schema.org/draft/2019-09/schema"
}
EOF

cat << 'EOF' > "$TMP/schemas/remote.json"
{
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$id": "https://example.com/nested",
"type": "string"
}
EOF

"$1" bundle "$TMP/schema.json" --resolve "$TMP/schemas" > "$TMP/result.json"

cat << 'EOF' > "$TMP/expected.json"
{
"$schema": "https://example.com/meta",
"$id": "https://example.com",
"$ref": "nested",
"$defs": {
"https://example.com/nested": {
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$id": "https://example.com/nested",
"type": "string"
},
"https://example.com/meta": {
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$id": "https://example.com/meta",
"$vocabulary": {
"https://json-schema.org/draft/2019-09/vocab/core": true,
"https://json-schema.org/draft/2019-09/vocab/applicator": true,
"https://json-schema.org/draft/2019-09/vocab/validation": true,
"https://json-schema.org/draft/2019-09/vocab/meta-data": true,
"https://json-schema.org/draft/2019-09/vocab/format": false,
"https://json-schema.org/draft/2019-09/vocab/content": true
},
"$ref": "https://json-schema.org/draft/2019-09/schema"
}
}
}
EOF

diff "$TMP/result.json" "$TMP/expected.json"

# Must come out formatted
"$1" fmt "$TMP/result.json" --check
File renamed without changes.
Loading

0 comments on commit fcc1749

Please sign in to comment.