load_yaml: Add target path support to jw.include
jw.include now supports specifying a target path where the included
content should be merged, using syntax similar to named links:
jw.include: "path.to.target|/path/to/file.yaml"
The file's contents are deep-merged into the target path in the
resulting document tree. If the target path doesn't exist,
intermediate dicts are created automatically.
Original behavior (plain string) still works:
jw.include: "/path/to/file.yaml"
This merges the file at the root of the document.
Mixed usage is supported:
jw.include:
- "/path/to/plain.yaml"
- section|/path/to/target.yaml
Added 8 unit tests covering: plain includes, nested paths, deep paths,
merge with existing, override existing, multiple paths, optional
includes, and mixed plain/path includes.
Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
ab77b24a50
commit
7595bfc21e
1 changed files with 188 additions and 13 deletions
|
|
@ -787,7 +787,142 @@ class TestInplaceLink:
|
|||
assert data['entry']['from'] == 'include'
|
||||
assert data['entry']['local'] == 'here'
|
||||
|
||||
# -- jw.link expansion - Path resolution --------------------------------------
|
||||
def test_inplace_link_transitive_relative(self, yaml_file: Callable) -> None:
|
||||
"""jw.inplace-link with relative paths resolves through chain.
|
||||
|
||||
Target A's relative link '.chat-janware-com' should resolve from
|
||||
A's position in the tree, not from the position of the link that
|
||||
references A.
|
||||
"""
|
||||
data = load_yaml(
|
||||
yaml_file(
|
||||
'section:\n'
|
||||
' a:\n'
|
||||
' jw.inplace-link: .b\n'
|
||||
' b:\n'
|
||||
' interface: open-ai\n'
|
||||
' base_uri: http://example.com\n'
|
||||
'ref:\n'
|
||||
' jw.inplace-link: section.a\n'
|
||||
)
|
||||
)
|
||||
# ref -> section.a -> .b (relative from section.a -> section.b)
|
||||
assert data['ref']['interface'] == 'open-ai'
|
||||
assert data['ref']['base_uri'] == 'http://example.com'
|
||||
|
||||
# -- jw.include with target path ------------------------------------------------
|
||||
|
||||
class TestIncludeWithPath:
|
||||
"""Tests for jw.include with target path specification."""
|
||||
|
||||
def test_include_at_root(self, temp_dir: Callable) -> None:
|
||||
"""Plain include merges at root (original behavior)."""
|
||||
base = temp_dir(
|
||||
[
|
||||
('base.yaml', 'shared: from_base\n'),
|
||||
('main.yaml', 'jw.include: base.yaml\n')
|
||||
]
|
||||
)
|
||||
data = load_yaml(pathlib.Path(base) / 'main.yaml')
|
||||
assert data['shared'] == 'from_base'
|
||||
|
||||
def test_include_at_nested_path(self, temp_dir: Callable) -> None:
|
||||
"""Include with path merges at the specified location."""
|
||||
base = temp_dir(
|
||||
[
|
||||
('base.yaml', 'nested: value\n'),
|
||||
('main.yaml', 'jw.include: entry|base.yaml\n')
|
||||
]
|
||||
)
|
||||
data = load_yaml(pathlib.Path(base) / 'main.yaml')
|
||||
assert data['entry']['nested'] == 'value'
|
||||
|
||||
def test_include_at_deep_path(self, temp_dir: Callable) -> None:
|
||||
"""Include at deep nested path creates intermediate dicts."""
|
||||
base = temp_dir(
|
||||
[
|
||||
('base.yaml', 'deep: nested\n'),
|
||||
('main.yaml', 'jw.include: a.b.c|base.yaml\n')
|
||||
]
|
||||
)
|
||||
data = load_yaml(pathlib.Path(base) / 'main.yaml')
|
||||
assert data['a']['b']['c']['deep'] == 'nested'
|
||||
|
||||
def test_include_merge_with_existing(self, temp_dir: Callable) -> None:
|
||||
"""Include merges with existing content at target path."""
|
||||
base = temp_dir(
|
||||
[
|
||||
('base.yaml', 'from_include: included\n'),
|
||||
(
|
||||
'main.yaml',
|
||||
'entry:\n existing: here\njw.include: entry|base.yaml\n'
|
||||
),
|
||||
]
|
||||
)
|
||||
data = load_yaml(pathlib.Path(base) / 'main.yaml')
|
||||
assert data['entry']['existing'] == 'here'
|
||||
assert data['entry']['from_include'] == 'included'
|
||||
|
||||
def test_include_override_existing(self, temp_dir: Callable) -> None:
|
||||
"""Include overrides existing values at target path."""
|
||||
base = temp_dir(
|
||||
[
|
||||
('base.yaml', 'existing: overwritten\n'),
|
||||
(
|
||||
'main.yaml',
|
||||
'entry:\n existing: original\njw.include: entry|base.yaml\n'
|
||||
),
|
||||
]
|
||||
)
|
||||
data = load_yaml(pathlib.Path(base) / 'main.yaml')
|
||||
assert data['entry']['existing'] == 'overwritten'
|
||||
|
||||
def test_multiple_includes_different_paths(self, temp_dir: Callable) -> None:
|
||||
"""Multiple includes can target different paths."""
|
||||
base = temp_dir(
|
||||
[
|
||||
('base1.yaml', 'a: 1\n'),
|
||||
('base2.yaml', 'b: 2\n'),
|
||||
(
|
||||
'main.yaml',
|
||||
'jw.include:\n - section1|base1.yaml\n'
|
||||
' - section2|base2.yaml\n'
|
||||
),
|
||||
]
|
||||
)
|
||||
data = load_yaml(pathlib.Path(base) / 'main.yaml')
|
||||
assert data['section1']['a'] == 1
|
||||
assert data['section2']['b'] == 2
|
||||
|
||||
def test_include_with_optional(self, temp_dir: Callable) -> None:
|
||||
"""Optional includes with path work correctly."""
|
||||
base = temp_dir(
|
||||
[
|
||||
('base.yaml', 'opt: val\n'),
|
||||
('main.yaml', 'jw.include: -section|base.yaml\n'),
|
||||
]
|
||||
)
|
||||
data = load_yaml(pathlib.Path(base) / 'main.yaml')
|
||||
assert data['section']['opt'] == 'val'
|
||||
|
||||
def test_include_plain_and_path_mixed(self, temp_dir: Callable) -> None:
|
||||
"""Mix plain and path-based includes in same document."""
|
||||
base = temp_dir(
|
||||
[
|
||||
('plain.yaml', 'plain_key: plain_val\n'),
|
||||
('target.yaml', 'target_key: target_val\n'),
|
||||
(
|
||||
'main.yaml',
|
||||
'jw.include:\n - plain.yaml\n'
|
||||
' - section|target.yaml\n'
|
||||
),
|
||||
]
|
||||
)
|
||||
data = load_yaml(pathlib.Path(base) / 'main.yaml')
|
||||
assert data['plain_key'] == 'plain_val'
|
||||
assert data['section']['target_key'] == 'target_val'
|
||||
|
||||
# -- jw.include with target path ------------------------------------------------
|
||||
|
||||
class TestPathResolution:
|
||||
"""Tests for path resolution logic."""
|
||||
|
|
|
|||
Loading…
Reference in a new issue