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
1 change: 1 addition & 0 deletions src/configuration/include/sourcemeta/blaze/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ struct SOURCEMETA_BLAZE_CONFIGURATION_EXPORT Configuration {
bool absolute_path_explicit{false};
std::filesystem::path base_path;
sourcemeta::core::JSON::String base;
sourcemeta::core::URI base_uri;
Comment thread
jviotti marked this conversation as resolved.
Comment thread
jviotti marked this conversation as resolved.
Comment thread
jviotti marked this conversation as resolved.
std::optional<sourcemeta::core::JSON::String> default_dialect;
std::unordered_set<sourcemeta::core::JSON::String> extension{".json", ".yml",
".yaml"};
Expand Down
12 changes: 6 additions & 6 deletions src/configuration/parse.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,23 +92,23 @@ auto Configuration::from_json(const sourcemeta::core::JSON &value,

if (value.defines("baseUri")) {
try {
sourcemeta::core::URI base{value.at("baseUri").to_string()};
base.canonicalize();
if (!base.is_absolute()) {
result.base_uri = sourcemeta::core::URI{value.at("baseUri").to_string()};
result.base_uri.canonicalize();
if (!result.base_uri.is_absolute()) {
CONFIGURATION_ENSURE(
false, "The baseUri property must be an absolute URI", {"baseUri"});
}

result.base = base.recompose();
result.base = result.base_uri.recompose();
} catch (const sourcemeta::core::URIParseError &) {
CONFIGURATION_ENSURE(false,
"The baseUri property must represent a valid URI",
{"baseUri"});
}
} else {
// Otherwise the base is the directory
result.base =
sourcemeta::core::URI::from_path(result.absolute_path).recompose();
result.base_uri = sourcemeta::core::URI::from_path(result.absolute_path);
result.base = result.base_uri.recompose();
}

result.default_dialect =
Expand Down
6 changes: 6 additions & 0 deletions test/configuration/configuration_add_dependency_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ TEST(Configuration_add_dependency, single) {
sourcemeta::blaze::Configuration config;
config.absolute_path = "/test/schemas";
config.base = "https://example.com";
config.base_uri = sourcemeta::core::URI{config.base};

config.add_dependency("https://json-schema.org/draft/2020-12/schema",
std::filesystem::path{"/test/vendor/2020-12.json"});
Expand All @@ -26,6 +27,7 @@ TEST(Configuration_add_dependency, multiple) {
sourcemeta::blaze::Configuration config;
config.absolute_path = "/test/schemas";
config.base = "https://example.com";
config.base_uri = sourcemeta::core::URI{config.base};

config.add_dependency("https://json-schema.org/draft/2020-12/schema",
std::filesystem::path{"/test/vendor/2020-12.json"});
Expand All @@ -47,6 +49,7 @@ TEST(Configuration_add_dependency, duplicate_uri) {
sourcemeta::blaze::Configuration config;
config.absolute_path = "/test/schemas";
config.base = "https://example.com";
config.base_uri = sourcemeta::core::URI{config.base};

config.add_dependency("https://json-schema.org/draft/2020-12/schema",
std::filesystem::path{"/test/vendor/2020-12.json"});
Expand All @@ -69,6 +72,7 @@ TEST(Configuration_add_dependency, uri_is_canonicalised) {
sourcemeta::blaze::Configuration config;
config.absolute_path = "/test/schemas";
config.base = "https://example.com";
config.base_uri = sourcemeta::core::URI{config.base};

config.add_dependency("HTTP://Example.COM:80/draft/2020-12/schema",
std::filesystem::path{"/test/vendor/2020-12.json"});
Expand All @@ -84,6 +88,7 @@ TEST(Configuration_add_dependency, duplicate_uri_after_canonicalisation) {
sourcemeta::blaze::Configuration config;
config.absolute_path = "/test/schemas";
config.base = "https://example.com";
config.base_uri = sourcemeta::core::URI{config.base};

config.add_dependency("http://example.com/schema.json",
std::filesystem::path{"/test/vendor/first.json"});
Expand All @@ -105,6 +110,7 @@ TEST(Configuration_add_dependency, duplicate_path) {
sourcemeta::blaze::Configuration config;
config.absolute_path = "/test/schemas";
config.base = "https://example.com";
config.base_uri = sourcemeta::core::URI{config.base};

config.add_dependency("https://json-schema.org/draft/2020-12/schema",
std::filesystem::path{"/test/vendor/schema.json"});
Expand Down
27 changes: 27 additions & 0 deletions test/configuration/configuration_json_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <sourcemeta/blaze/configuration.h>
#include <sourcemeta/core/json.h>
#include <sourcemeta/core/uri.h>

#include "configuration_test_utils.h"

Expand Down Expand Up @@ -39,6 +40,7 @@ TEST(Configuration_json, read_json_valid_1) {
EXPECT_EQ(manifest.absolute_path, std::filesystem::path{"/test"} / "schemas");
EXPECT_TRUE(manifest.absolute_path_explicit);
EXPECT_EQ(manifest.base, "https://schemas.sourcemeta.com");
EXPECT_EQ(manifest.base_uri.recompose(), "https://schemas.sourcemeta.com");
EXPECT_TRUE(manifest.default_dialect.has_value());
EXPECT_EQ(manifest.default_dialect.value(),
"http://json-schema.org/draft-07/schema#");
Expand Down Expand Up @@ -71,11 +73,27 @@ TEST(Configuration_json, read_json_valid_without_path) {
EXPECT_EQ(manifest.absolute_path, std::filesystem::path{"/test"});
EXPECT_FALSE(manifest.absolute_path_explicit);
EXPECT_EQ(manifest.base, "https://example.com");
EXPECT_EQ(manifest.base_uri.recompose(), "https://example.com");
EXPECT_FALSE(manifest.default_dialect.has_value());
EXPECT_EQ(manifest.resolve.size(), 0);
EXPECT_EQ(manifest.extra.size(), 0);
}

TEST(Configuration_json, read_json_base_uri_defaults_to_absolute_path) {
std::unordered_map<std::string, std::string> files;
files["/test/blaze.json"] = R"JSON({
"path": "./schemas"
})JSON";

const auto manifest{sourcemeta::blaze::Configuration::read_json(
"/test/blaze.json", MAKE_READER(files))};

EXPECT_EQ(manifest.absolute_path, std::filesystem::path{"/test"} / "schemas");
EXPECT_TRUE(manifest.absolute_path_explicit);
EXPECT_EQ(manifest.base, "file:///test/schemas");
EXPECT_EQ(manifest.base_uri.recompose(), "file:///test/schemas");
}

TEST(Configuration_json, to_json_all_fields) {
sourcemeta::blaze::Configuration config;
config.title = "Sourcemeta";
Expand All @@ -87,6 +105,7 @@ TEST(Configuration_json, to_json_all_fields) {
config.absolute_path_explicit = true;
config.base_path = "/test/schemas";
config.base = "https://schemas.sourcemeta.com";
config.base_uri = sourcemeta::core::URI{config.base};
config.default_dialect = "http://json-schema.org/draft-07/schema#";
config.extension = {".json", ".yaml"};
config.resolve.emplace("https://other.com/single.json", "../single.json");
Expand Down Expand Up @@ -122,6 +141,7 @@ TEST(Configuration_json, to_json_minimal) {
config.absolute_path = "/test";
config.absolute_path_explicit = true;
config.base = "https://example.com";
config.base_uri = sourcemeta::core::URI{config.base};

const auto result{config.to_json()};

Expand All @@ -148,6 +168,7 @@ TEST(Configuration_json, to_json_with_extra) {
config.absolute_path = "/test";
config.absolute_path_explicit = true;
config.base = "https://example.com";
config.base_uri = sourcemeta::core::URI{config.base};
config.extra.assign("x-foo", sourcemeta::core::JSON{"bar"});

const auto result{config.to_json()};
Expand Down Expand Up @@ -286,6 +307,7 @@ TEST(Configuration_json, to_json_with_lint_rules) {
config.absolute_path_explicit = true;
config.base_path = "/test";
config.base = "https://example.com";
config.base_uri = sourcemeta::core::URI{config.base};
config.lint.rules.emplace_back("/test/rules/my-rule.json");
config.lint.rules.emplace_back("/test/rules/other-rule.json");

Expand All @@ -308,6 +330,7 @@ TEST(Configuration_json, to_json_with_lint_rules_parent) {
config.absolute_path_explicit = true;
config.base_path = "/test";
config.base = "https://example.com";
config.base_uri = sourcemeta::core::URI{config.base};
config.lint.rules.emplace_back("/other/rules/my-rule.json");

const auto result{config.to_json()};
Expand All @@ -327,6 +350,7 @@ TEST(Configuration_json, to_json_empty_lint) {
sourcemeta::blaze::Configuration config;
config.absolute_path = "/test";
config.base = "https://example.com";
config.base_uri = sourcemeta::core::URI{config.base};

const auto result{config.to_json()};

Expand Down Expand Up @@ -374,6 +398,7 @@ TEST(Configuration_json, to_json_with_ignore) {
config.absolute_path_explicit = true;
config.base_path = "/test";
config.base = "https://example.com";
config.base_uri = sourcemeta::core::URI{config.base};
config.ignore.emplace_back("/test/vendor");
config.ignore.emplace_back("/test/build");

Expand All @@ -394,6 +419,7 @@ TEST(Configuration_json, to_json_with_ignore_parent) {
config.absolute_path_explicit = true;
config.base_path = "/test";
config.base = "https://example.com";
config.base_uri = sourcemeta::core::URI{config.base};
config.ignore.emplace_back("/other/vendor");

const auto result{config.to_json()};
Expand All @@ -411,6 +437,7 @@ TEST(Configuration_json, to_json_empty_ignore) {
sourcemeta::blaze::Configuration config;
config.absolute_path = "/test";
config.base = "https://example.com";
config.base_uri = sourcemeta::core::URI{config.base};

const auto result{config.to_json()};

Expand Down
Loading