Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions src/command_fmt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ auto sourcemeta::jsonschema::fmt(const sourcemeta::core::Options &options)
const auto configuration_path{find_configuration(current_path)};
const auto &configuration{
read_configuration(options, configuration_path, current_path)};
const auto dialect{default_dialect(options, configuration)};
const auto &custom_resolver{
resolver(options, options.contains("http"), dialect, configuration)};
const auto display_path{stdin_path()};

std::string raw_stdin;
Expand All @@ -39,6 +36,17 @@ auto sourcemeta::jsonschema::fmt(const sourcemeta::core::Options &options)
}

const auto &document{parsed.document};
const auto dialect{default_dialect(options, configuration)};
const auto is_test_document =
dialect.empty() && looks_like_test_document(document);
const auto effective_dialect =
is_test_document ? TEST_DOCUMENT_DEFAULT_DIALECT : dialect;
if (is_test_document) {
std::cerr << "Interpreting as a test file: " << display_path.string()
<< "\n";
}
const auto &custom_resolver{resolver(options, options.contains("http"),
Comment thread
jviotti marked this conversation as resolved.
effective_dialect, configuration)};
const auto stdin_label{display_path.string()};

try {
Expand All @@ -49,7 +57,7 @@ auto sourcemeta::jsonschema::fmt(const sourcemeta::core::Options &options)
} else {
auto copy = document;
sourcemeta::core::format(copy, sourcemeta::core::schema_walker,
custom_resolver, dialect);
custom_resolver, effective_dialect);
sourcemeta::core::prettify(copy, expected, indentation);
}
expected << "\n";
Expand All @@ -69,7 +77,7 @@ auto sourcemeta::jsonschema::fmt(const sourcemeta::core::Options &options)
} else {
auto copy = document;
sourcemeta::core::format(copy, sourcemeta::core::schema_walker,
custom_resolver, dialect);
custom_resolver, effective_dialect);
sourcemeta::core::prettify(copy, std::cout, indentation);
}
std::cout << "\n";
Expand Down Expand Up @@ -114,16 +122,23 @@ auto sourcemeta::jsonschema::fmt(const sourcemeta::core::Options &options)
const auto &configuration{read_configuration(options, configuration_path,
entry.resolution_base)};
const auto dialect{default_dialect(options, configuration)};
const auto &custom_resolver{
resolver(options, options.contains("http"), dialect, configuration)};
const auto is_test_document =
dialect.empty() && looks_like_test_document(entry.second);
const auto effective_dialect =
is_test_document ? TEST_DOCUMENT_DEFAULT_DIALECT : dialect;
if (is_test_document) {
std::cerr << "Interpreting as a test file: " << entry.first << "\n";
}
const auto &custom_resolver{resolver(options, options.contains("http"),
effective_dialect, configuration)};

std::ostringstream expected;
if (options.contains("keep-ordering")) {
sourcemeta::core::prettify(entry.second, expected, indentation);
} else {
auto copy = entry.second;
sourcemeta::core::format(copy, sourcemeta::core::schema_walker,
custom_resolver, dialect);
custom_resolver, effective_dialect);
sourcemeta::core::prettify(copy, expected, indentation);
}
expected << "\n";
Expand Down
10 changes: 10 additions & 0 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ inline auto resolve_entrypoint(const sourcemeta::core::SchemaFrame &frame,
}
}

constexpr std::string_view TEST_DOCUMENT_DEFAULT_DIALECT{
"https://json-schema.org/draft/2020-12/schema"};

inline auto looks_like_test_document(const sourcemeta::core::JSON &document)
-> bool {
return document.is_object() && !document.defines("$schema") &&
Comment thread
jviotti marked this conversation as resolved.
document.defines("target") && document.at("target").is_string() &&
document.defines("tests") && document.at("tests").is_array();
}

inline auto default_dialect(
const sourcemeta::core::Options &options,
const std::optional<sourcemeta::blaze::Configuration> &configuration)
Expand Down
8 changes: 8 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ add_jsonschema_test_unix(format/fail_stdin_invalid_json)
add_jsonschema_test_unix(format/pass_stdin_keep_ordering)
add_jsonschema_test_unix(format/fail_stdin_missing_newline)
add_jsonschema_test_unix(format/pass_stdin_crlf)
add_jsonschema_test_unix(format/pass_test_document)
add_jsonschema_test_unix(format/pass_check_test_document)
add_jsonschema_test_unix(format/fail_check_test_document)
add_jsonschema_test_unix(format/pass_stdin_test_document)
add_jsonschema_test_unix(format/pass_test_document_with_comment)
add_jsonschema_test_unix(format/pass_test_document_explicit_dialect)
add_jsonschema_test_unix(format/pass_test_document_directory)
add_jsonschema_test_unix(format/pass_test_document_schema_keyword)

# Validate
add_jsonschema_test_unix(validate/fail_instance_enoent)
Expand Down
37 changes: 37 additions & 0 deletions test/format/fail_check_test_document.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/sh

set -o errexit
set -o nounset

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

cat << 'EOF' > "$TMP/test.json"
{
"tests": [
{
"description": "I expect to pass",
"valid": true,
"data": {
"bar": 1,
"foo": 1
}
}
],
"target": "https://example.com/my-schema"
}
EOF

"$1" fmt "$TMP/test.json" --check > "$TMP/output.txt" 2>&1 \
&& EXIT_CODE="$?" || EXIT_CODE="$?"
test "$EXIT_CODE" = "2"

cat << EOF > "$TMP/expected.txt"
Interpreting as a test file: $(realpath "$TMP")/test.json
fail: $(realpath "$TMP")/test.json

Run the \`fmt\` command without \`--check/-c\` to fix the formatting
EOF

diff "$TMP/output.txt" "$TMP/expected.txt"
32 changes: 32 additions & 0 deletions test/format/pass_check_test_document.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/test.json"
{
"target": "https://example.com/my-schema",
"tests": [
{
"description": "I expect to pass",
"valid": true,
"data": {
"bar": 1,
"foo": 1
}
}
]
}
EOF

"$1" fmt "$TMP/test.json" --check > "$TMP/output.txt" 2>&1

cat << EOF > "$TMP/expected_output.txt"
Interpreting as a test file: $(realpath "$TMP")/test.json
EOF

diff "$TMP/output.txt" "$TMP/expected_output.txt"
41 changes: 41 additions & 0 deletions test/format/pass_stdin_test_document.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/sh

set -o errexit
set -o nounset

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

cat << 'EOF' > "$TMP/test.json"
{
"tests": [
{
"description": "I expect to pass",
"valid": true,
"data": { "foo": 1 }
}
],
"target": "https://example.com/my-schema"
}
EOF

"$1" fmt - < "$TMP/test.json" > "$TMP/output.txt" 2>&1

cat << 'EOF' > "$TMP/expected.txt"
Interpreting as a test file: /dev/stdin
{
"target": "https://example.com/my-schema",
"tests": [
{
"description": "I expect to pass",
"valid": true,
"data": {
"foo": 1
}
}
]
}
EOF

diff "$TMP/output.txt" "$TMP/expected.txt"
50 changes: 50 additions & 0 deletions test/format/pass_test_document.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/sh

set -o errexit
set -o nounset

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

cat << 'EOF' > "$TMP/test.json"
{
"tests": [
{
"description": "I expect to pass",
"valid": true,
"data": {
"bar": 1,
"foo": 1
}
}
],
"target": "https://example.com/my-schema"
}
EOF

"$1" fmt "$TMP/test.json" > "$TMP/output.txt" 2>&1

cat << EOF > "$TMP/expected_output.txt"
Interpreting as a test file: $(realpath "$TMP")/test.json
EOF

diff "$TMP/output.txt" "$TMP/expected_output.txt"

cat << 'EOF' > "$TMP/expected.json"
{
"target": "https://example.com/my-schema",
"tests": [
{
"description": "I expect to pass",
"valid": true,
"data": {
"bar": 1,
"foo": 1
}
}
]
}
EOF

diff "$TMP/test.json" "$TMP/expected.json"
52 changes: 52 additions & 0 deletions test/format/pass_test_document_directory.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/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",
"properties": { "foo": {} },
"type": "object"
}
EOF

cat << 'EOF' > "$TMP/test.json"
{
"tests": [],
"target": "https://example.com/my-schema"
}
EOF

"$1" fmt "$TMP" > "$TMP/output.txt" 2>&1

cat << EOF > "$TMP/expected_output.txt"
Interpreting as a test file: $(realpath "$TMP")/test.json
EOF

diff "$TMP/output.txt" "$TMP/expected_output.txt"

cat << 'EOF' > "$TMP/expected_test.json"
{
"target": "https://example.com/my-schema",
"tests": []
}
EOF

diff "$TMP/test.json" "$TMP/expected_test.json"

cat << 'EOF' > "$TMP/expected_schema.json"
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"foo": {}
}
}
EOF

diff "$TMP/schema.json" "$TMP/expected_schema.json"
33 changes: 33 additions & 0 deletions test/format/pass_test_document_explicit_dialect.sh
Original file line number Diff line number Diff line change
@@ -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/test.json"
{
"tests": [],
"target": "https://example.com/my-schema"
}
EOF

"$1" fmt "$TMP/test.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"
{
"target": "https://example.com/my-schema",
"tests": []
}
EOF

diff "$TMP/test.json" "$TMP/expected.json"
Loading
Loading