load_yaml: Add jw.include-map
This commit adds support for interpreting a new syntax:
jw.include-map:
- /etc/some/file.conf: ../../../file.conf
- /opt/blah/another/config.yaml: ../../../config.yaml
Production configs include absolute paths outside the repository tree, so a
test that runs a production-style document against repository-local copies
must redirect those includes, ideally without modifying the
files meant to be installed as is.
Add a jw.include-map key to the top-level document: an ordered list
of single-key mappings from LHS pattern to RHS pattern or list of RHS
patterns. Before resolution, the file part of each include spec is
qualified against the including file's directory and matched against the
LHS patterns in table order; the first match rewrites the spec to every RHS
pattern of that entry. Relative LHS and RHS values are interpreted against
the directory of the document that declares the map, so matching runs on
canonical paths and same-named includes from different directories stay
distinguishable. The optional marker and the target path are decomposed
before the rewrite: every rewritten source inherits the spec's optionality,
and the target path applies to the merged result.
- _parse_include_map(): parse and validate the table; only '*' is supported
in an LHS, a RHS may not have more '*' than its LHS, and RHS values
expand $(VAR) and ${VAR} from the environment
- _canonicalize_path(): join a relative path with a base directory and
normalize, without disk access
- _compile_lhs_pattern(): compile a canonical LHS into an anchored regex
whose '*' groups match runs that are not '/'
- _apply_include_map(): match the first entry and fill the LHS captures
into every RHS pattern; log the rewrite at DEBUG
- _load_yaml(): parse the map once in the top-level document, reject it in
included documents, thread it through the include tree
Signed-off-by: Jan Lindemann <jan@janware.com>
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v1.6.0
This commit is contained in:
parent
911c78ff99
commit
5d94a1fcee
1 changed files with 454 additions and 9 deletions
|
|
@ -1213,6 +1213,280 @@ class TestIncludeVarExpansion:
|
|||
data = load_yaml(pathlib.Path(base) / 'main.yaml')
|
||||
assert data['literal'] is True
|
||||
|
||||
class TestIncludeMap:
|
||||
"""Tests for the jw.include-map include spec rewrite table."""
|
||||
|
||||
def test_exact_rewrite(self, temp_dir: _TempDir) -> None:
|
||||
"""An exact LHS rewrites the spec to the RHS."""
|
||||
base = temp_dir(
|
||||
[
|
||||
(
|
||||
'wrapper.yaml',
|
||||
'jw.include-map:\n'
|
||||
' - /prod/etc/app.yaml: test/app.yaml\n'
|
||||
'jw.include: /prod/etc/app.yaml\n'
|
||||
),
|
||||
('test/app.yaml', 'from_test: 1\n'),
|
||||
]
|
||||
)
|
||||
data = load_yaml(pathlib.Path(base) / 'wrapper.yaml')
|
||||
assert data['from_test'] == 1
|
||||
|
||||
def test_glob_rewrite(self, temp_dir: _TempDir) -> None:
|
||||
"""A glob LHS captures into a glob RHS."""
|
||||
base = temp_dir(
|
||||
[
|
||||
('prod.d/one.yaml', 'one: prod\n'),
|
||||
('prod.d/two.yaml', 'two: prod\n'),
|
||||
('test.d/one.yaml', 'one: test\n'),
|
||||
('test.d/two.yaml', 'two: test\n'),
|
||||
(
|
||||
'wrapper.yaml',
|
||||
'jw.include-map:\n'
|
||||
' - prod.d/*.yaml: test.d/*.yaml\n'
|
||||
'jw.include: prod.d/*.yaml\n'
|
||||
),
|
||||
]
|
||||
)
|
||||
data = load_yaml(pathlib.Path(base) / 'wrapper.yaml')
|
||||
assert data['one'] == 'test'
|
||||
assert data['two'] == 'test'
|
||||
|
||||
def test_rhs_list(self, temp_dir: _TempDir) -> None:
|
||||
"""A list RHS expands to multiple sources in list order."""
|
||||
base = temp_dir(
|
||||
[
|
||||
('t1/a.yaml', 'one: 1\nshared: t1a\n'),
|
||||
('t1/b.yaml', 'shared: t1b\ntwo: 2\n'),
|
||||
('t2/a.yaml', 'shared: t2a\nthree: 3\n'),
|
||||
(
|
||||
'wrapper.yaml',
|
||||
'jw.include-map:\n'
|
||||
' - prod.d/*.yaml: [t1/*.yaml, t2/*.yaml]\n'
|
||||
'jw.include: prod.d/*.yaml\n'
|
||||
),
|
||||
]
|
||||
)
|
||||
data = load_yaml(pathlib.Path(base) / 'wrapper.yaml')
|
||||
assert data['one'] == 1
|
||||
assert data['two'] == 2
|
||||
assert data['three'] == 3
|
||||
# Sources merge in RHS list order, later files win
|
||||
assert data['shared'] == 't2a'
|
||||
|
||||
def test_rewrite_before_disk_lookup(self, temp_dir: _TempDir) -> None:
|
||||
"""The LHS pattern need not match anything on disk."""
|
||||
base = temp_dir(
|
||||
[
|
||||
('test.d/one.yaml', 'one: 1\n'),
|
||||
(
|
||||
'wrapper.yaml',
|
||||
'jw.include-map:\n'
|
||||
' - prod.d/*.yaml: test.d/*.yaml\n'
|
||||
'jw.include: prod.d/one.yaml\n'
|
||||
),
|
||||
]
|
||||
)
|
||||
data = load_yaml(pathlib.Path(base) / 'wrapper.yaml')
|
||||
assert data['one'] == 1
|
||||
|
||||
def test_first_match_wins(self, temp_dir: _TempDir) -> None:
|
||||
"""The first matching entry wins; later entries are ignored."""
|
||||
base = temp_dir(
|
||||
[
|
||||
('first.yaml', 'source: first\n'),
|
||||
('second.yaml', 'source: second\n'),
|
||||
(
|
||||
'wrapper.yaml',
|
||||
'jw.include-map:\n'
|
||||
' - /prod/etc/app.yaml: first.yaml\n'
|
||||
' - /prod/etc/app.yaml: second.yaml\n'
|
||||
'jw.include: /prod/etc/app.yaml\n'
|
||||
),
|
||||
]
|
||||
)
|
||||
data = load_yaml(pathlib.Path(base) / 'wrapper.yaml')
|
||||
assert data['source'] == 'first'
|
||||
|
||||
def test_relative_lhs_collision(self, temp_dir: _TempDir) -> None:
|
||||
"""Same-named relative includes are disambiguated by directory."""
|
||||
base = temp_dir(
|
||||
[
|
||||
('prod/a/doc.yaml', 'jw.include: x.yaml\n'),
|
||||
('prod/b/doc.yaml', 'jw.include: x.yaml\n'),
|
||||
('ax.yaml', 'from_ax: 1\n'),
|
||||
('bx.yaml', 'from_bx: 2\n'),
|
||||
(
|
||||
'wrapper.yaml',
|
||||
'jw.include-map:\n'
|
||||
' - prod/a/x.yaml: ax.yaml\n'
|
||||
' - prod/b/x.yaml: bx.yaml\n'
|
||||
'jw.include:\n'
|
||||
' - prod/a/doc.yaml\n'
|
||||
' - prod/b/doc.yaml\n'
|
||||
),
|
||||
]
|
||||
)
|
||||
data = load_yaml(pathlib.Path(base) / 'wrapper.yaml')
|
||||
assert data['from_ax'] == 1
|
||||
assert data['from_bx'] == 2
|
||||
|
||||
def test_relative_rhs_against_wrapper(self, temp_dir: _TempDir) -> None:
|
||||
"""A relative RHS resolves against the wrapper's directory."""
|
||||
base = temp_dir(
|
||||
[
|
||||
('prod/app.yaml', 'jw.include: deep.yaml\n'),
|
||||
('copies/deep.yaml', 'deep: ok\n'),
|
||||
(
|
||||
'wrapper.yaml',
|
||||
'jw.include-map:\n'
|
||||
' - prod/deep.yaml: copies/deep.yaml\n'
|
||||
'jw.include: prod/app.yaml\n'
|
||||
),
|
||||
]
|
||||
)
|
||||
data = load_yaml(pathlib.Path(base) / 'wrapper.yaml')
|
||||
assert data['deep'] == 'ok'
|
||||
|
||||
def test_absolute_lhs(self, temp_dir: _TempDir) -> None:
|
||||
"""Absolute LHS patterns match production-style includes."""
|
||||
base = temp_dir(
|
||||
[
|
||||
('prod.yaml', 'jw.include: /etc/jw/app/*.yaml\n'),
|
||||
('test/app.yaml', 'from_test: 1\n'),
|
||||
(
|
||||
'wrapper.yaml',
|
||||
'jw.include-map:\n'
|
||||
' - /etc/jw/app/*.yaml: test/app.yaml\n'
|
||||
'jw.include: prod.yaml\n'
|
||||
),
|
||||
]
|
||||
)
|
||||
data = load_yaml(pathlib.Path(base) / 'wrapper.yaml')
|
||||
assert data['from_test'] == 1
|
||||
|
||||
def test_optional_flag_inherited(self, temp_dir: _TempDir) -> None:
|
||||
"""Rewritten sources inherit the original spec's optionality."""
|
||||
base = temp_dir(
|
||||
[
|
||||
(
|
||||
'wrapper.yaml',
|
||||
'jw.include-map:\n'
|
||||
' - prod/*.yaml: nowhere/*.yaml\n'
|
||||
'jw.include: -prod/missing.yaml\n'
|
||||
),
|
||||
]
|
||||
)
|
||||
data = load_yaml(pathlib.Path(base) / 'wrapper.yaml')
|
||||
assert data == {}
|
||||
|
||||
def test_non_optional_rewrite_missing_raises(self, temp_dir: _TempDir) -> None:
|
||||
"""A rewritten plain path that does not exist raises."""
|
||||
base = temp_dir(
|
||||
[
|
||||
(
|
||||
'wrapper.yaml',
|
||||
'jw.include-map:\n'
|
||||
' - prod/*.yaml: nowhere/*.yaml\n'
|
||||
'jw.include: prod/missing.yaml\n'
|
||||
),
|
||||
]
|
||||
)
|
||||
with pytest.raises(FileNotFoundError):
|
||||
load_yaml(pathlib.Path(base) / 'wrapper.yaml')
|
||||
|
||||
def test_target_path_preserved(self, temp_dir: _TempDir) -> None:
|
||||
"""The target path prefix survives the rewrite."""
|
||||
base = temp_dir(
|
||||
[
|
||||
('t/one.yaml', 'a: 1\n'),
|
||||
('t/two.yaml', 'b: 2\n'),
|
||||
(
|
||||
'wrapper.yaml',
|
||||
'jw.include-map:\n'
|
||||
' - prod/*.yaml: t/*.yaml\n'
|
||||
'jw.include: section|prod/*.yaml\n'
|
||||
),
|
||||
]
|
||||
)
|
||||
data = load_yaml(pathlib.Path(base) / 'wrapper.yaml')
|
||||
assert data['section'] == {'a': 1, 'b': 2}
|
||||
|
||||
def test_nested_map_raises(self, temp_dir: _TempDir) -> None:
|
||||
"""jw.include-map in a non-top-level document raises."""
|
||||
base = temp_dir(
|
||||
[
|
||||
('inner.yaml', 'jw.include-map:\n - a.yaml: b.yaml\n'),
|
||||
('wrapper.yaml', 'jw.include: inner.yaml\n'),
|
||||
]
|
||||
)
|
||||
with pytest.raises(ValueError, match = 'top-level'):
|
||||
load_yaml(pathlib.Path(base) / 'wrapper.yaml')
|
||||
|
||||
def test_empty_map_is_noop(self, temp_dir: _TempDir) -> None:
|
||||
"""An empty map leaves all includes untouched."""
|
||||
base = temp_dir(
|
||||
[
|
||||
('app.yaml', 'ok: 1\n'),
|
||||
('wrapper.yaml', 'jw.include-map: []\n'
|
||||
'jw.include: app.yaml\n'),
|
||||
]
|
||||
)
|
||||
data = load_yaml(pathlib.Path(base) / 'wrapper.yaml')
|
||||
assert data['ok'] == 1
|
||||
|
||||
def test_map_not_a_list_raises(self, temp_dir: _TempDir) -> None:
|
||||
"""A non-list jw.include-map value raises."""
|
||||
base = temp_dir([('wrapper.yaml', 'jw.include-map: {}\n')])
|
||||
with pytest.raises(TypeError, match = 'must be a list'):
|
||||
load_yaml(pathlib.Path(base) / 'wrapper.yaml')
|
||||
|
||||
def test_multi_key_entry_raises(self, temp_dir: _TempDir) -> None:
|
||||
"""A map entry with two keys raises."""
|
||||
base = temp_dir(
|
||||
[
|
||||
(
|
||||
'wrapper.yaml',
|
||||
'jw.include-map:\n'
|
||||
' - a.yaml: b.yaml\n'
|
||||
' c.yaml: d.yaml\n'
|
||||
),
|
||||
]
|
||||
)
|
||||
with pytest.raises(ValueError, match = 'single-key'):
|
||||
load_yaml(pathlib.Path(base) / 'wrapper.yaml')
|
||||
|
||||
def test_rhs_star_overflow_raises(self, temp_dir: _TempDir) -> None:
|
||||
"""A RHS with more stars than the LHS raises."""
|
||||
base = temp_dir(
|
||||
[
|
||||
('wrapper.yaml', 'jw.include-map:\n - a/*.yaml: b/*/*.yaml\n'),
|
||||
]
|
||||
)
|
||||
with pytest.raises(ValueError, match = r'more "\*"'):
|
||||
load_yaml(pathlib.Path(base) / 'wrapper.yaml')
|
||||
|
||||
def test_lhs_question_mark_raises(self, temp_dir: _TempDir) -> None:
|
||||
"""A '?' in the LHS raises; only '*' is supported."""
|
||||
base = temp_dir([('wrapper.yaml', 'jw.include-map:\n - a/?.yaml: b.yaml\n')])
|
||||
with pytest.raises(ValueError, match = r'only "\*"'):
|
||||
load_yaml(pathlib.Path(base) / 'wrapper.yaml')
|
||||
|
||||
def test_var_in_rhs(
|
||||
self, temp_dir: _TempDir, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
"""RHS values expand environment variables."""
|
||||
base = temp_dir([('app.yaml', 'ok: 1\n')])
|
||||
monkeypatch.setenv('LOAD_YAML_TEST_TOP', base)
|
||||
main = pathlib.Path(base) / 'wrapper.yaml'
|
||||
main.write_text(
|
||||
'jw.include-map:\n'
|
||||
' - /etc/app.yaml: ${LOAD_YAML_TEST_TOP}/app.yaml\n'
|
||||
'jw.include: /etc/app.yaml\n'
|
||||
)
|
||||
data = load_yaml(main)
|
||||
assert data['ok'] == 1
|
||||
|
||||
# -- jw.include with target path ------------------------------------------------
|
||||
|
||||
class TestPathResolution:
|
||||
|
|
|
|||
Loading…
Reference in a new issue