Skip to content

Commit 61ce0ba

Browse files
authored
Handle configuration base paths correctly on multi-file setups (#651)
Signed-off-by: Juan Cruz Viotti <[email protected]>
1 parent aa7af9d commit 61ce0ba

File tree

13 files changed

+93
-13
lines changed

13 files changed

+93
-13
lines changed

.github/actions/sandbox/action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ inputs:
2222
runs:
2323
using: 'composite'
2424
steps:
25+
# Work around BuildKit snapshot corruption on GitHub Actions runners
26+
- name: Prune BuildKit cache (${{ inputs.configuration }})
27+
run: docker builder prune --all --force
28+
shell: bash
2529
- name: Build Sandbox (${{ inputs.configuration }})
2630
run: make docker-sandbox-build
2731
shell: bash

.github/workflows/ci.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ jobs:
3131

3232
- run: make docker PRESET=Release ${{ matrix.edition.options }}
3333

34-
# Work around BuildKit snapshot corruption on GitHub Actions runners
35-
- run: docker builder prune --all --force
36-
3734
- name: Sandbox (headless)
3835
uses: ./.github/actions/sandbox
3936
with:

collections/self/v1/schemas/configuration/collection.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
"x-sourcemeta-one:evaluate": {
2525
"type": "boolean"
2626
},
27+
"x-sourcemeta-one:path": {
28+
"$ref": "https://schemas.sourcemeta.com/sourcemeta/std/v0/ieee/posix/2017/path"
29+
},
2730
"x-sourcemeta-one:provenance": {
2831
"$ref": "./rpath.json"
2932
},

src/configuration/include/sourcemeta/one/configuration.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ struct Configuration {
1818
const std::filesystem::path &collections_path)
1919
-> sourcemeta::core::JSON;
2020
static auto parse(const sourcemeta::core::JSON &data,
21-
const std::filesystem::path &base_path) -> Configuration;
21+
const std::filesystem::path &default_base_path)
22+
-> Configuration;
2223

2324
sourcemeta::core::JSON::String url;
2425

src/configuration/parse.cc

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,29 @@ auto page_from_json(const sourcemeta::core::JSON &input)
3232
template <typename T>
3333
auto entries_from_json(T &result, const std::filesystem::path &location,
3434
const sourcemeta::core::JSON &input,
35-
const std::filesystem::path &base_path) -> void {
35+
const std::filesystem::path &default_base_path) -> void {
3636
// A heuristic to check if we are at the root or not
3737
if (input.defines("url")) {
3838
if (input.defines("contents")) {
3939
for (const auto &entry : input.at("contents").as_object()) {
4040
entries_from_json<T>(result, location / entry.first, entry.second,
41-
base_path);
41+
default_base_path);
4242
}
4343
}
4444
} else {
4545
assert(!result.contains(location));
4646
if (input.defines("path")) {
47-
auto collection{
48-
sourcemeta::blaze::Configuration::from_json(input, base_path)};
47+
auto collection_input{input};
48+
const auto base_path{
49+
collection_input.defines("x-sourcemeta-one:path")
50+
? std::filesystem::path{collection_input
51+
.at("x-sourcemeta-one:path")
52+
.to_string()}
53+
.parent_path()
54+
: default_base_path};
55+
collection_input.erase("x-sourcemeta-one:path");
56+
auto collection{sourcemeta::blaze::Configuration::from_json(
57+
collection_input, base_path)};
4958
// Filesystems behave differently with regards to casing. To unify
5059
// them, assume they are case-insensitive and just go for lowercase
5160
std::ranges::transform(
@@ -62,7 +71,7 @@ auto entries_from_json(T &result, const std::filesystem::path &location,
6271
if (input.defines("contents")) {
6372
for (const auto &entry : input.at("contents").as_object()) {
6473
entries_from_json<T>(result, location / entry.first, entry.second,
65-
base_path);
74+
default_base_path);
6675
}
6776
}
6877
}
@@ -74,7 +83,7 @@ auto entries_from_json(T &result, const std::filesystem::path &location,
7483
namespace sourcemeta::one {
7584

7685
auto Configuration::parse(const sourcemeta::core::JSON &data,
77-
const std::filesystem::path &base_path)
86+
const std::filesystem::path &default_base_path)
7887
-> Configuration {
7988
const auto compiled_schema{sourcemeta::blaze::from_json(
8089
sourcemeta::core::parse_json(std::string{CONFIGURATION}))};
@@ -112,7 +121,7 @@ auto Configuration::parse(const sourcemeta::core::JSON &data,
112121
}
113122
}
114123

115-
entries_from_json(result.entries, "", data, base_path);
124+
entries_from_json(result.entries, "", data, default_base_path);
116125

117126
return result;
118127
}

src/configuration/read.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ auto dereference(const std::filesystem::path &collections_path,
101101
current_path.is_relative() ? base.parent_path() / current_path
102102
: current_path)};
103103
input.at("path").into(sourcemeta::core::JSON{absolute_path});
104+
if (!input.defines("x-sourcemeta-one:path")) {
105+
input.assign("x-sourcemeta-one:path",
106+
sourcemeta::core::JSON{base.string()});
107+
}
104108

105109
// Recurse on children, if any
106110
} else if (input.defines("contents") && input.at("contents").is_object()) {

test/cli/index/common/configuration-long.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ cat << EOF > "$TMP/expected.txt"
4141
"contents": {
4242
"schemas": {
4343
"baseUri": "https://example.com/",
44-
"path": "$(realpath "$TMP")/schemas"
44+
"path": "$(realpath "$TMP")/schemas",
45+
"x-sourcemeta-one:path": "$(realpath "$TMP")/one.json"
4546
}
4647
}
4748
}

test/cli/index/common/configuration-short.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ cat << EOF > "$TMP/expected.txt"
4141
"contents": {
4242
"schemas": {
4343
"baseUri": "https://example.com/",
44-
"path": "$(realpath "$TMP")/schemas"
44+
"path": "$(realpath "$TMP")/schemas",
45+
"x-sourcemeta-one:path": "$(realpath "$TMP")/one.json"
4546
}
4647
}
4748
}

test/cli/index/enterprise/sourcemeta-std.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ cat << EOF > "$TMP/expected.txt"
3636
"v0": {
3737
"x-sourcemeta-one:provenance": "@sourcemeta/std/v0",
3838
"path": "$ONE_PREFIX/share/sourcemeta/one/collections/sourcemeta/std/v0/schemas/2020-12",
39+
"x-sourcemeta-one:path": "$ONE_PREFIX/share/sourcemeta/one/collections/sourcemeta/std/v0/jsonschema.json",
3940
"baseUri": "https://example.com/"
4041
}
4142
}

test/unit/configuration/configuration_read_test.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ TEST(Configuration_read, read_valid_001) {
3636
"title": "Test",
3737
"baseUri": "https://example.com/extension",
3838
"path": "STUB_DIRECTORY/schemas/example/extension",
39+
"x-sourcemeta-one:path": "STUB_DIRECTORY/read_valid_001.json",
3940
"defaultDialect": "http://json-schema.org/draft-07/schema#",
4041
"resolve": {
4142
"https://other.com/single.json": "/foo.json"
@@ -75,6 +76,7 @@ TEST(Configuration_read, read_valid_002) {
7576
"nested": {
7677
"baseUri": "https://example.com/extension",
7778
"path": "STUB_DIRECTORY/folder/schemas/example/extension",
79+
"x-sourcemeta-one:path": "STUB_DIRECTORY/folder/jsonschema.json",
7880
"defaultDialect": "http://json-schema.org/draft-07/schema#"
7981
}
8082
}
@@ -106,6 +108,7 @@ TEST(Configuration_read, read_valid_003) {
106108
"nested": {
107109
"baseUri": "https://example.com/extension",
108110
"path": "STUB_DIRECTORY/folder/schemas/example/extension",
111+
"x-sourcemeta-one:path": "STUB_DIRECTORY/folder/jsonschema.json",
109112
"defaultDialect": "http://json-schema.org/draft-07/schema#"
110113
}
111114
}
@@ -140,6 +143,7 @@ TEST(Configuration_read, read_valid_004) {
140143
"nested": {
141144
"baseUri": "https://example.com/extension",
142145
"path": "STUB_DIRECTORY/folder/schemas/example/extension",
146+
"x-sourcemeta-one:path": "STUB_DIRECTORY/folder/jsonschema.json",
143147
"defaultDialect": "http://json-schema.org/draft-07/schema#"
144148
}
145149
}
@@ -174,6 +178,7 @@ TEST(Configuration_read, read_valid_005) {
174178
"nested": {
175179
"baseUri": "https://example.com/extension",
176180
"path": "STUB_DIRECTORY/folder/schemas/example/extension",
181+
"x-sourcemeta-one:path": "STUB_DIRECTORY/folder/jsonschema.json",
177182
"defaultDialect": "http://json-schema.org/draft-07/schema#"
178183
}
179184
}
@@ -372,6 +377,7 @@ TEST(Configuration_read, read_valid_013) {
372377
"title": "Test",
373378
"baseUri": "https://example.com/extension",
374379
"path": "STUB_DIRECTORY/schemas/example/extension",
380+
"x-sourcemeta-one:path": "STUB_DIRECTORY/read_valid_013.json",
375381
"defaultDialect": "http://json-schema.org/draft-07/schema#",
376382
"resolve": {
377383
"https://other.com/single.json": "/foo.json"
@@ -403,6 +409,7 @@ TEST(Configuration_read, read_valid_014) {
403409
"title": "Test",
404410
"baseUri": "https://example.com/extension",
405411
"path": "STUB_DIRECTORY/schemas/example/extension",
412+
"x-sourcemeta-one:path": "STUB_DIRECTORY/read_valid_014.json",
406413
"defaultDialect": "http://json-schema.org/draft-07/schema#",
407414
"resolve": {
408415
"https://other.com/single.json": "/foo.json"
@@ -436,6 +443,7 @@ TEST(Configuration_read, read_valid_015) {
436443
"contents": {
437444
"example": {
438445
"path": "STUB_DIRECTORY/schemas/example/extension",
446+
"x-sourcemeta-one:path": "STUB_DIRECTORY/read_valid_015.json",
439447
"baseUri": "http://localhost:8000"
440448
}
441449
}

0 commit comments

Comments
 (0)