diff --git a/src/command_fmt.cc b/src/command_fmt.cc index 820273fd..f255cd1f 100644 --- a/src/command_fmt.cc +++ b/src/command_fmt.cc @@ -1,23 +1,46 @@ #include #include -#include // EXIT_SUCCESS +#include // EXIT_SUCCESS, EXIT_FAILURE #include // std::ofstream #include // std::cerr, std::endl +#include // std::ostringstream #include "command.h" #include "utils.h" -// TODO: Support a `--check` option to do an assert like Deno does auto intelligence::jsonschema::cli::fmt( const std::span &arguments) -> int { - const auto options{parse_options(arguments, {})}; + const auto options{parse_options(arguments, {"c", "check"})}; + for (const auto &entry : for_each_json(options.at(""))) { - log_verbose(options) << "Formatting: " << entry.first.string() << "\n"; - std::ofstream output{entry.first}; - sourcemeta::jsontoolkit::prettify( - entry.second, output, sourcemeta::jsontoolkit::schema_format_compare); - output << std::endl; + if (options.contains("c") || options.contains("check")) { + log_verbose(options) << "Checking: " << entry.first.string() << "\n"; + std::ifstream input{entry.first}; + std::ostringstream buffer; + buffer << input.rdbuf(); + std::ostringstream expected; + sourcemeta::jsontoolkit::prettify( + entry.second, expected, + sourcemeta::jsontoolkit::schema_format_compare); + expected << "\n"; + + if (buffer.str() == expected.str()) { + log_verbose(options) << "PASS: " << entry.first.string() << "\n"; + } else { + std::cerr << "FAIL: " << entry.first.string() << "\n"; + std::cerr << "Got: \n" + << buffer.str() << "\nBut expected:\n" + << expected.str() << "\n"; + return EXIT_FAILURE; + } + } else { + log_verbose(options) << "Formatting: " << entry.first.string() << "\n"; + std::ofstream output{entry.first}; + sourcemeta::jsontoolkit::prettify( + entry.second, output, sourcemeta::jsontoolkit::schema_format_compare); + output << std::endl; + } } return EXIT_SUCCESS; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index b3f36683..e30acfe2 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -11,6 +11,8 @@ add_jsonschema_test_unix(format_single) add_jsonschema_test_unix(format_invalid_path) add_jsonschema_test_unix(format_directory) add_jsonschema_test_unix(format_cwd) +add_jsonschema_test_unix(format_check_single_fail) +add_jsonschema_test_unix(format_check_single_pass) add_jsonschema_test_unix(frame) add_jsonschema_test_unix(validate_pass) add_jsonschema_test_unix(validate_fail) diff --git a/test/format_check_single_fail.sh b/test/format_check_single_fail.sh new file mode 100755 index 00000000..a16733e8 --- /dev/null +++ b/test/format_check_single_fail.sh @@ -0,0 +1,34 @@ +#!/bin/sh + +set -o errexit +set -o nounset + +TMP="$(mktemp -d)" +clean() { rm -rf "$TMP"; } +trap clean EXIT + +cat << 'EOF' > "$TMP/schema.json" +{ + "type": 1, + "$schema": "http://json-schema.org/draft-04/schema#" +} +EOF + +"$1" fmt "$TMP/schema.json" --check && CODE="$?" || CODE="$?" + +if [ "$CODE" = "0" ] +then + echo "FAIL" 1>&2 + exit 1 +else + echo "PASS" 1>&2 +fi + +cat << 'EOF' > "$TMP/expected.json" +{ + "type": 1, + "$schema": "http://json-schema.org/draft-04/schema#" +} +EOF + +diff "$TMP/schema.json" "$TMP/expected.json" diff --git a/test/format_check_single_pass.sh b/test/format_check_single_pass.sh new file mode 100755 index 00000000..d0b045ba --- /dev/null +++ b/test/format_check_single_pass.sh @@ -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": "http://json-schema.org/draft-04/schema#", + "type": 1 +} +EOF + +"$1" fmt "$TMP/schema.json" --check + +cat << 'EOF' > "$TMP/expected.json" +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "type": 1 +} +EOF + +diff "$TMP/schema.json" "$TMP/expected.json"