Skip to content

Commit

Permalink
Implement --check/-c for fmt (#28)
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <[email protected]>
  • Loading branch information
jviotti authored Jun 3, 2024
1 parent defa5c3 commit 00f0ef3
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 8 deletions.
39 changes: 31 additions & 8 deletions src/command_fmt.cc
Original file line number Diff line number Diff line change
@@ -1,23 +1,46 @@
#include <sourcemeta/jsontoolkit/json.h>
#include <sourcemeta/jsontoolkit/jsonschema.h>

#include <cstdlib> // EXIT_SUCCESS
#include <cstdlib> // EXIT_SUCCESS, EXIT_FAILURE
#include <fstream> // std::ofstream
#include <iostream> // std::cerr, std::endl
#include <sstream> // 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<const std::string> &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;
Expand Down
2 changes: 2 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
34 changes: 34 additions & 0 deletions test/format_check_single_fail.sh
Original file line number Diff line number Diff line change
@@ -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"
26 changes: 26 additions & 0 deletions test/format_check_single_pass.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": "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"

0 comments on commit 00f0ef3

Please sign in to comment.