_INCLUDE_PATH_RE allows the pipe character in the target-path part of a jw.include spec such as 'target.path|file.yaml'. The greedy match then consumes further pipes, so a file name that itself contains a pipe is misparsed: the target swallows part of the file name and the remainder becomes the (nonexistent) file to open. Dots are needed in the target part for nested paths, but a pipe can never be part of a target path. Drop the pipe from the character class so that the first pipe always separates the target path from the file name. Add a regression test with a pipe in the file name. Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v0.84.2 Signed-off-by: Jan Lindemann <jan@janware.com>
1072 lines
40 KiB
Python
1072 lines
40 KiB
Python
"""Unit tests for jw.util.load_yaml."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pathlib
|
|
import tempfile
|
|
|
|
from typing import Callable, Generator
|
|
|
|
import pytest
|
|
|
|
from jw.util.load_yaml import load_yaml
|
|
|
|
# -- Helpers ------------------------------------------------------------------
|
|
|
|
@pytest.fixture
|
|
def yaml_file() -> Callable[[str], str]:
|
|
"""Create a temporary YAML file and return its path."""
|
|
|
|
def _make(content: str) -> str:
|
|
fh = tempfile.NamedTemporaryFile(mode = 'w', suffix = '.yaml', delete = False)
|
|
fh.write(content)
|
|
fh.flush()
|
|
fh.close()
|
|
return fh.name
|
|
|
|
return _make
|
|
|
|
@pytest.fixture
|
|
def temp_dir() -> Generator:
|
|
"""Create a temporary directory with files, return the directory."""
|
|
import shutil
|
|
|
|
base_dir: str | None = None
|
|
|
|
def _make(files: list[tuple[str, str]]) -> str:
|
|
nonlocal base_dir
|
|
base_dir = tempfile.mkdtemp()
|
|
for name, content in files:
|
|
path = pathlib.Path(base_dir) / name
|
|
path.parent.mkdir(parents = True, exist_ok = True)
|
|
path.write_text(content)
|
|
return base_dir
|
|
|
|
yield _make
|
|
if base_dir:
|
|
shutil.rmtree(base_dir, ignore_errors = True)
|
|
|
|
# -- Basic YAML loading -------------------------------------------------------
|
|
|
|
class TestBasicLoading:
|
|
"""Tests for basic YAML file loading without links or includes."""
|
|
|
|
def test_simple_dict(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(yaml_file('key: value\n'))
|
|
assert data == {'key': 'value'}
|
|
|
|
def test_nested_dict(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(yaml_file('outer:\n inner: value\n number: 42\n'))
|
|
assert data == {'outer': {'inner': 'value', 'number': 42}}
|
|
|
|
def test_list_value(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(yaml_file('items:\n - a\n - b\n - c\n'))
|
|
assert data == {'items': ['a', 'b', 'c']}
|
|
|
|
def test_empty_file(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(yaml_file(''))
|
|
assert data == {}
|
|
|
|
def test_none_root(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(yaml_file('---\n'))
|
|
assert data == {}
|
|
|
|
def test_string_value(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(yaml_file('key: string value\n'))
|
|
assert data == {'key': 'string value'}
|
|
|
|
def test_number_value(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(yaml_file('int: 42\nfloat: 3.14\n'))
|
|
assert data['int'] == 42
|
|
assert data['float'] == 3.14
|
|
|
|
def test_boolean_value(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(yaml_file('enabled: true\ndisabled: false\n'))
|
|
assert data['enabled'] is True
|
|
assert data['disabled'] is False
|
|
|
|
def test_null_value(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(yaml_file('key: null\n'))
|
|
assert data['key'] is None
|
|
|
|
def test_multiline_string(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(yaml_file('key: |\n line1\n line2\n'))
|
|
assert data['key'] == 'line1\nline2\n'
|
|
|
|
def test_deeply_nested(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(yaml_file('a:\n b:\n c:\n d: deep\n'))
|
|
assert data == {'a': {'b': {'c': {'d': 'deep'}}}}
|
|
|
|
# -- Optional loading ---------------------------------------------------------
|
|
|
|
class TestOptionalLoading:
|
|
"""Tests for the optional parameter."""
|
|
|
|
def test_missing_optional(self) -> None:
|
|
data = load_yaml('nonexistent_file.yaml', optional = True)
|
|
assert data == {}
|
|
|
|
def test_missing_required(self) -> None:
|
|
with pytest.raises(FileNotFoundError):
|
|
load_yaml('nonexistent_file.yaml', optional = False)
|
|
|
|
def test_present_optional(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(yaml_file('key: val\n'), optional = True)
|
|
assert data == {'key': 'val'}
|
|
|
|
# -- Non-dict root detection --------------------------------------------------
|
|
|
|
class TestRootTypeCheck:
|
|
"""Tests that non-dict YAML roots are rejected."""
|
|
|
|
def test_string_root_raises(self, yaml_file: Callable) -> None:
|
|
with pytest.raises(TypeError, match = 'root must be a mapping'):
|
|
load_yaml(yaml_file('"just a string"\n'))
|
|
|
|
def test_list_root_raises(self, yaml_file: Callable) -> None:
|
|
with pytest.raises(TypeError, match = 'root must be a mapping'):
|
|
load_yaml(yaml_file('- item1\n- item2\n'))
|
|
|
|
def test_number_root_raises(self, yaml_file: Callable) -> None:
|
|
with pytest.raises(TypeError, match = 'root must be a mapping'):
|
|
load_yaml(yaml_file('42\n'))
|
|
|
|
# -- Include directive --------------------------------------------------------
|
|
|
|
class TestIncludeDirective:
|
|
"""Tests for the include directive."""
|
|
|
|
def test_single_include(self, temp_dir: Callable) -> None:
|
|
base = temp_dir(
|
|
[
|
|
('base.yaml', 'shared: from_base\n'),
|
|
('main.yaml', 'jw.include: base.yaml\ncurrent: here\n'),
|
|
]
|
|
)
|
|
data = load_yaml(pathlib.Path(base) / 'main.yaml')
|
|
assert data['shared'] == 'from_base'
|
|
assert data['current'] == 'here'
|
|
|
|
def test_multiple_includes(self, temp_dir: Callable) -> None:
|
|
base = temp_dir(
|
|
[
|
|
('a.yaml', 'from_a: 1\nshared: a\n'),
|
|
('b.yaml', 'from_b: 2\nshared: b\n'),
|
|
('main.yaml', 'jw.include:\n - a.yaml\n - b.yaml\nlocal: yes\n'),
|
|
]
|
|
)
|
|
data = load_yaml(pathlib.Path(base) / 'main.yaml')
|
|
assert data['from_a'] == 1
|
|
assert data['from_b'] == 2
|
|
# Later includes override earlier ones
|
|
assert data['shared'] == 'b'
|
|
assert data['local'] is True
|
|
|
|
def test_optional_include(self, temp_dir: Callable) -> None:
|
|
base = temp_dir(
|
|
[
|
|
('main.yaml', 'jw.include:\n - "- nonexist.yaml"\nok: true\n'),
|
|
]
|
|
)
|
|
data = load_yaml(pathlib.Path(base) / 'main.yaml')
|
|
assert data == {'ok': True}
|
|
|
|
def test_deep_merge_dicts(self, temp_dir: Callable) -> None:
|
|
base = temp_dir(
|
|
[
|
|
('base.yaml', 'config:\n db:\n host: localhost\n port: 5432\n'),
|
|
('override.yaml', 'config:\n db:\n port: 3306\n name: mydb\n'),
|
|
(
|
|
'main.yaml',
|
|
'jw.include:\n'
|
|
' - override.yaml\n'
|
|
'config:\n'
|
|
' cache: true\n'
|
|
),
|
|
]
|
|
)
|
|
data = load_yaml(pathlib.Path(base) / 'main.yaml')
|
|
assert data['config']['db']['port'] == 3306
|
|
assert data['config']['db']['name'] == 'mydb'
|
|
assert data['config']['cache'] is True
|
|
|
|
def test_nested_includes(self, temp_dir: Callable) -> None:
|
|
base = temp_dir(
|
|
[
|
|
('level1.yaml', 'from: level1\n'),
|
|
('level2.yaml', 'jw.include: level1.yaml\nfrom: level2\n'),
|
|
('main.yaml', 'jw.include: level2.yaml\n'),
|
|
]
|
|
)
|
|
data = load_yaml(pathlib.Path(base) / 'main.yaml')
|
|
assert data['from'] == 'level2'
|
|
|
|
def test_include_circular_detect(self, temp_dir: Callable) -> None:
|
|
base = temp_dir(
|
|
[
|
|
('a.yaml', 'jw.include: b.yaml\n'),
|
|
('b.yaml', 'jw.include: a.yaml\n'),
|
|
]
|
|
)
|
|
with pytest.raises(ValueError, match = 'Cyclic YAML include'):
|
|
load_yaml(pathlib.Path(base) / 'a.yaml')
|
|
|
|
def test_include_relative_path(self, temp_dir: Callable) -> None:
|
|
base = temp_dir(
|
|
[
|
|
('sub/inner.yaml', 'secret: found\n'),
|
|
('main.yaml', 'jw.include: sub/inner.yaml\n'),
|
|
]
|
|
)
|
|
data = load_yaml(pathlib.Path(base) / 'main.yaml')
|
|
assert data['secret'] == 'found'
|
|
|
|
# -- jw.link expansion - Inplace links ----------------------------------------
|
|
|
|
class TestInplaceLinks:
|
|
"""Tests for inplace jw.link expansion."""
|
|
|
|
def test_inplace_absolute(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(
|
|
yaml_file('target:\n key: val\nlink:\n jw.link: target.key\n')
|
|
)
|
|
assert data['link'] == 'val'
|
|
# Original target should still be there
|
|
assert data['target']['key'] == 'val'
|
|
|
|
def test_inplace_dict_target(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(
|
|
yaml_file('target:\n a: 1\n b: 2\nlink:\n jw.link: target\n')
|
|
)
|
|
assert data['link'] == {'a': 1, 'b': 2}
|
|
|
|
def test_inplace_list_target(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(yaml_file('target:\n - x\n - y\nlink:\n jw.link: target\n'))
|
|
assert data['link'] == ['x', 'y']
|
|
|
|
def test_inplace_in_list(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(
|
|
yaml_file(
|
|
'target: val\nitems:\n - plain\n - jw.link: target\n - more\n'
|
|
)
|
|
)
|
|
assert data['items'] == ['plain', 'val', 'more']
|
|
|
|
def test_inplace_scalar_in_list(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(yaml_file('target: 42\nitems:\n - 1\n - jw.link: target\n'))
|
|
assert data['items'] == [1, 42]
|
|
|
|
def test_inplace_in_nested_dict(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(
|
|
yaml_file(
|
|
'target: deep\nnested:\n level:\n link:\n jw.link: target\n'
|
|
)
|
|
)
|
|
assert data['nested']['level']['link'] == 'deep'
|
|
|
|
# -- jw.link expansion - Named links ------------------------------------------
|
|
|
|
class TestNamedLinks:
|
|
"""Tests for named jw.link expansion."""
|
|
|
|
def test_named_absolute(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(
|
|
yaml_file('target:\n key: val\nlink:\n jw.link: name|target.key\n')
|
|
)
|
|
assert data['link'] == {'name': 'val'}
|
|
|
|
def test_named_dict_target(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(yaml_file('target:\n a: 1\nlink:\n jw.link: alias|target\n'))
|
|
assert data['link'] == {'alias': {'a': 1}}
|
|
|
|
def test_named_in_list(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(
|
|
yaml_file('target: val\nitems:\n - plain\n - jw.link: renamed|target\n')
|
|
)
|
|
assert data['items'] == ['plain', {'renamed': 'val'}]
|
|
|
|
def test_named_with_dashes_and_underscores(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(yaml_file('target: ok\nlink:\n jw.link: my-name|target\n'))
|
|
assert data['link'] == {'my-name': 'ok'}
|
|
|
|
# -- jw.link expansion - Relative paths ---------------------------------------
|
|
|
|
class TestRelativePaths:
|
|
"""Tests for relative jw.link paths."""
|
|
|
|
def test_relative_up_one(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(
|
|
yaml_file(
|
|
'parent:\n'
|
|
' child:\n'
|
|
' link:\n'
|
|
' jw.link: ..sibling\n'
|
|
' sibling: value\n'
|
|
)
|
|
)
|
|
assert data['parent']['child']['link'] == 'value'
|
|
|
|
def test_relative_up_one_simple(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(
|
|
yaml_file('sibling: val\nnode:\n link:\n jw.link: ..sibling\n')
|
|
)
|
|
assert data['node']['link'] == 'val'
|
|
|
|
def test_relative_up_two(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(
|
|
yaml_file(
|
|
'root_val: my_root\n'
|
|
'level1:\n'
|
|
' level2:\n'
|
|
' link:\n'
|
|
' jw.link: ...root_val\n'
|
|
)
|
|
)
|
|
assert data['level1']['level2']['link'] == 'my_root'
|
|
|
|
def test_relative_up_two_simple(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(
|
|
yaml_file('base_val: my_base\nsub:\n link:\n jw.link: ..base_val\n')
|
|
)
|
|
assert data['sub']['link'] == 'my_base'
|
|
|
|
def test_relative_up_then_descend(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(
|
|
yaml_file(
|
|
'shared:\n'
|
|
' target: found\n'
|
|
'nested:\n'
|
|
' a:\n'
|
|
' b:\n'
|
|
' link:\n'
|
|
' jw.link: ....shared.target\n'
|
|
)
|
|
)
|
|
assert data['nested']['a']['b']['link'] == 'found'
|
|
|
|
def test_relative_up_then_descend_simple(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(
|
|
yaml_file('shared:\n value: ok\nnode:\n link:\n jw.link: ..shared\n')
|
|
)
|
|
assert data['node']['link'] == {'value': 'ok'}
|
|
|
|
def test_relative_same_level(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(yaml_file('a: 1\nb:\n link:\n jw.link: ..a\n'))
|
|
assert data['b']['link'] == 1
|
|
|
|
def test_relative_same_level_simple(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(
|
|
yaml_file('sibling: val\nnode:\n link:\n jw.link: ..sibling\n')
|
|
)
|
|
assert data['node']['link'] == 'val'
|
|
|
|
def test_relative_above_root_raises(self, yaml_file: Callable) -> None:
|
|
with pytest.raises(ValueError, match = 'above root'):
|
|
load_yaml(yaml_file('link:\n jw.link: ....above\n'))
|
|
|
|
def test_relative_deep_nesting(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(
|
|
yaml_file(
|
|
'shared: value\n'
|
|
'depth:\n'
|
|
' l1:\n'
|
|
' l2:\n'
|
|
' l3:\n'
|
|
' link:\n'
|
|
' jw.link: .....shared\n'
|
|
)
|
|
)
|
|
assert data['depth']['l1']['l2']['l3']['link'] == 'value'
|
|
|
|
def test_relative_self_same_dict(self, yaml_file: Callable) -> None:
|
|
# Link pointing to ancestor dict containing itself is a cycle
|
|
with pytest.raises(ValueError, match = 'Cyclic link'):
|
|
load_yaml(yaml_file('a:\n x: 1\n b:\n c:\n jw.link: ...a\n'))
|
|
|
|
def test_relative_up_to_root_then_descend(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(
|
|
yaml_file(
|
|
'root_data: ok\na:\n b:\n link:\n jw.link: ...root_data\n'
|
|
)
|
|
)
|
|
assert data['a']['b']['link'] == 'ok'
|
|
|
|
def test_relative_up_from_deep(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(
|
|
yaml_file(
|
|
'root_val: here\n'
|
|
'top:\n'
|
|
' middle:\n'
|
|
' bot:\n'
|
|
' link:\n'
|
|
' jw.link: ....root_val\n'
|
|
)
|
|
)
|
|
assert data['top']['middle']['bot']['link'] == 'here'
|
|
|
|
def test_relative_link_to_sibling_dict(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(yaml_file('a:\n x: 1\nb:\n link:\n jw.link: ..a\n'))
|
|
assert data['b']['link'] == {'x': 1}
|
|
|
|
def test_relative_up_two_from_deep(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(
|
|
yaml_file(
|
|
'root_val: here\n'
|
|
'a:\n'
|
|
' b:\n'
|
|
' c:\n'
|
|
' link:\n'
|
|
' jw.link: ....root_val\n'
|
|
)
|
|
)
|
|
assert data['a']['b']['c']['link'] == 'here'
|
|
|
|
# -- jw.link expansion - Transitive links -------------------------------------
|
|
|
|
class TestTransitiveLinks:
|
|
"""Tests for transitive link resolution."""
|
|
|
|
def test_chain_of_links(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(
|
|
yaml_file(
|
|
'target: final\n'
|
|
'link_a:\n'
|
|
' jw.link: link_b\n'
|
|
'link_b:\n'
|
|
' jw.link: target\n'
|
|
)
|
|
)
|
|
assert data['link_a'] == 'final'
|
|
assert data['link_b'] == 'final'
|
|
|
|
def test_chain_inplace(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(
|
|
yaml_file('value: 100\na:\n jw.link: b\nb:\n jw.link: value\n')
|
|
)
|
|
assert data['a'] == 100
|
|
|
|
def test_chain_named(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(yaml_file('value: ok\nlink:\n jw.link: copy|value\n'))
|
|
assert data['link'] == {'copy': 'ok'}
|
|
|
|
# -- jw.link expansion - Cycle detection --------------------------------------
|
|
|
|
class TestCycleDetection:
|
|
"""Tests for cycle detection in links."""
|
|
|
|
def test_self_referencing_link(self, yaml_file: Callable) -> None:
|
|
with pytest.raises(ValueError, match = 'Cyclic link'):
|
|
load_yaml(yaml_file('link:\n jw.link: link\n'))
|
|
|
|
def test_mutual_cycle(self, yaml_file: Callable) -> None:
|
|
with pytest.raises(ValueError, match = 'Cyclic link'):
|
|
load_yaml(yaml_file('a:\n jw.link: b\nb:\n jw.link: a\n'))
|
|
|
|
def test_self_cycle_nested(self, yaml_file: Callable) -> None:
|
|
with pytest.raises(ValueError, match = 'Cyclic link'):
|
|
load_yaml(
|
|
yaml_file('root:\n inner:\n link:\n jw.link: root.inner\n')
|
|
)
|
|
|
|
def test_cycle_deep_chain(self, yaml_file: Callable) -> None:
|
|
with pytest.raises(ValueError, match = 'Cyclic link'):
|
|
load_yaml(
|
|
yaml_file('a:\n jw.link: c\nc:\n jw.link: b\nb:\n jw.link: a\n')
|
|
)
|
|
|
|
# -- jw.link expansion - Missing targets --------------------------------------
|
|
|
|
class TestMissingTargets:
|
|
"""Tests for missing link targets."""
|
|
|
|
def test_missing_absolute_target(self, yaml_file: Callable) -> None:
|
|
with pytest.raises(ValueError, match = 'cannot resolve target path'):
|
|
load_yaml(yaml_file('link:\n jw.link: nonexistent\n'))
|
|
|
|
def test_missing_relative_target(self, yaml_file: Callable) -> None:
|
|
with pytest.raises(ValueError, match = 'above root'):
|
|
load_yaml(yaml_file('link:\n jw.link: ..missing\n'))
|
|
|
|
def test_missing_deep_target(self, yaml_file: Callable) -> None:
|
|
with pytest.raises(ValueError, match = 'cannot resolve target path'):
|
|
load_yaml(
|
|
yaml_file('exists:\n val: ok\nlink:\n jw.link: exists.missing\n')
|
|
)
|
|
|
|
def test_missing_relative_deep_target(self, yaml_file: Callable) -> None:
|
|
with pytest.raises(ValueError, match = 'cannot resolve target path'):
|
|
load_yaml(
|
|
yaml_file('a:\n b: val\nnode:\n link:\n jw.link: ..a.missing\n')
|
|
)
|
|
|
|
def test_missing_target_in_list_raises(self, yaml_file: Callable) -> None:
|
|
"""Navigating into a list by key name raises with context."""
|
|
with pytest.raises(ValueError, match = 'cannot resolve target path'):
|
|
load_yaml(yaml_file('items:\n - a\nlink:\n jw.link: items.x\n'))
|
|
|
|
def test_missing_target_error_names_link(self, yaml_file: Callable) -> None:
|
|
"""The error message names the failing link and its location."""
|
|
with pytest.raises(ValueError, match = "jw.link 'nope' at link"):
|
|
load_yaml(yaml_file('link:\n jw.link: nope\n'))
|
|
|
|
# -- jw.link expansion - No links (regression) --------------------------------
|
|
|
|
class TestNoLinksRegression:
|
|
"""Ensure data without links passes through unchanged."""
|
|
|
|
def test_simple_no_links(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(yaml_file('a: 1\nb:\n c: 2\n'))
|
|
assert data == {'a': 1, 'b': {'c': 2}}
|
|
|
|
def test_complex_structure(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(
|
|
yaml_file('list:\n - a\n - b\nnested:\n deep:\n value: 42\n')
|
|
)
|
|
assert data['list'] == ['a', 'b']
|
|
assert data['nested']['deep']['value'] == 42
|
|
|
|
def test_jw_link_with_siblings_unchanged(self, yaml_file: Callable) -> None:
|
|
"""jw.link with sibling keys should NOT be expanded."""
|
|
data = load_yaml(
|
|
yaml_file('target: val\nlink:\n jw.link: target\n other: kept\n')
|
|
)
|
|
# The jw.link marker with siblings is left as-is
|
|
assert 'jw.link' in data['link']
|
|
assert data['link']['other'] == 'kept'
|
|
|
|
# -- Combined: Include + Links ------------------------------------------------
|
|
|
|
class TestIncludePlusLinks:
|
|
"""Tests combining include directives and link expansion."""
|
|
|
|
def test_include_then_link(self, temp_dir: Callable) -> None:
|
|
base = temp_dir(
|
|
[
|
|
('base.yaml', 'shared: from_include\n'),
|
|
('main.yaml', 'jw.include: base.yaml\nlink:\n jw.link: shared\n'),
|
|
]
|
|
)
|
|
data = load_yaml(pathlib.Path(base) / 'main.yaml')
|
|
assert data['shared'] == 'from_include'
|
|
assert data['link'] == 'from_include'
|
|
|
|
def test_link_in_included_file(self, temp_dir: Callable) -> None:
|
|
base = temp_dir(
|
|
[
|
|
('base.yaml', 'target: included\ndecorator:\n jw.link: target\n'),
|
|
('main.yaml', 'jw.include: base.yaml\nlocal: here\n'),
|
|
]
|
|
)
|
|
data = load_yaml(pathlib.Path(base) / 'main.yaml')
|
|
assert data['target'] == 'included'
|
|
assert data['decorator'] == 'included'
|
|
|
|
def test_relative_link_to_included(self, temp_dir: Callable) -> None:
|
|
base = temp_dir(
|
|
[
|
|
('base.yaml', 'base_val: ok\n'),
|
|
(
|
|
'main.yaml',
|
|
'jw.include: base.yaml\nlocal:\n link:\n jw.link: ..base_val\n'
|
|
),
|
|
]
|
|
)
|
|
data = load_yaml(pathlib.Path(base) / 'main.yaml')
|
|
assert data['local']['link'] == 'ok'
|
|
|
|
# -- jw.link expansion - Edge cases -------------------------------------------
|
|
|
|
class TestEdgeCases:
|
|
"""Tests for edge cases in link expansion."""
|
|
|
|
def test_empty_string_value(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(yaml_file('target: ""\nlink:\n jw.link: target\n'))
|
|
assert data['link'] == ''
|
|
|
|
def test_zero_value(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(yaml_file('target: 0\nlink:\n jw.link: target\n'))
|
|
assert data['link'] == 0
|
|
|
|
def test_false_value(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(yaml_file('target: false\nlink:\n jw.link: target\n'))
|
|
assert data['link'] is False
|
|
|
|
def test_none_target(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(yaml_file('target: null\nlink:\n jw.link: target\n'))
|
|
assert data['link'] is None
|
|
|
|
def test_complex_nested_inplace(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(
|
|
yaml_file(
|
|
'target:\n'
|
|
' nested:\n'
|
|
' dict:\n'
|
|
' deep: value\n'
|
|
'link:\n'
|
|
' jw.link: target.nested.dict\n'
|
|
)
|
|
)
|
|
assert data['link']['deep'] == 'value'
|
|
|
|
def test_link_to_list_of_dicts(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(
|
|
yaml_file('target:\n - key: a\n - key: b\nlink:\n jw.link: target\n')
|
|
)
|
|
assert data['link'] == [{'key': 'a'}, {'key': 'b'}]
|
|
|
|
def test_multiple_links_same_target(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(
|
|
yaml_file('target: val\na:\n jw.link: target\nb:\n jw.link: target\n')
|
|
)
|
|
assert data['a'] == 'val'
|
|
assert data['b'] == 'val'
|
|
|
|
def test_named_link_to_list(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(
|
|
yaml_file('target:\n - x\n - y\nlink:\n jw.link: items|target\n')
|
|
)
|
|
assert data['link'] == {'items': ['x', 'y']}
|
|
|
|
def test_link_value_with_pipe_in_target_key(self, yaml_file: Callable) -> None:
|
|
"""Key with pipe in name — pipe triggers named link parsing."""
|
|
with pytest.raises(ValueError, match = 'cannot resolve target path'):
|
|
load_yaml(yaml_file('my|key: val\nlink:\n jw.link: my|key\n'))
|
|
|
|
def test_named_link_with_numeric_prefix(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(yaml_file('target: ok\nlink:\n jw.link: 123target|target\n'))
|
|
assert data['link'] == {'123target': 'ok'}
|
|
|
|
def test_non_string_link_value_raises(self, yaml_file: Callable) -> None:
|
|
"""Non-string jw.link values raise TypeError naming the value."""
|
|
with pytest.raises(TypeError, match = 'must be a string'):
|
|
load_yaml(yaml_file('target: 42\nlink:\n jw.link: 42\n'))
|
|
|
|
def test_non_string_inplace_link_value_raises(self, yaml_file: Callable) -> None:
|
|
"""Non-string jw.inplace-link values raise TypeError."""
|
|
with pytest.raises(TypeError, match = 'must be a string'):
|
|
load_yaml(yaml_file('target:\n a: 1\nentry:\n jw.inplace-link: 42\n'))
|
|
|
|
# -- jw.inplace-link expansion ------------------------------------------------
|
|
|
|
class TestInplaceLink:
|
|
"""Tests for jw.inplace-link expansion with sibling merging."""
|
|
|
|
def test_inplace_link_no_siblings(self, yaml_file: Callable) -> None:
|
|
"""jw.inplace-link alone replaces the value (like jw.link)."""
|
|
data = load_yaml(yaml_file('target: val\nlink:\n jw.inplace-link: target\n'))
|
|
assert data['link'] == 'val'
|
|
|
|
def test_inplace_link_with_dict_siblings(self, yaml_file: Callable) -> None:
|
|
"""jw.inplace-link with sibling keys deep-merges into resolved dict."""
|
|
data = load_yaml(
|
|
yaml_file(
|
|
'target:\n'
|
|
' a: 1\n'
|
|
' b: 2\n'
|
|
'entry:\n'
|
|
' jw.inplace-link: target\n'
|
|
' c: 3\n'
|
|
' b: overridden\n'
|
|
)
|
|
)
|
|
# a and b from target, c from siblings, b overridden by sibling
|
|
assert data['entry']['a'] == 1
|
|
assert data['entry']['b'] == 'overridden'
|
|
assert data['entry']['c'] == 3
|
|
|
|
def test_inplace_link_with_scalar_sibling(self, yaml_file: Callable) -> None:
|
|
"""Scalar siblings merge into resolved dict."""
|
|
data = load_yaml(
|
|
yaml_file(
|
|
'target:\n'
|
|
' nested: dict\n'
|
|
'entry:\n'
|
|
' jw.inplace-link: target\n'
|
|
' flag: true\n'
|
|
)
|
|
)
|
|
assert data['entry']['nested'] == 'dict'
|
|
assert data['entry']['flag'] is True
|
|
|
|
def test_inplace_link_non_dict_target(self, yaml_file: Callable) -> None:
|
|
"""Non-dict target without siblings replaces the value."""
|
|
data = load_yaml(yaml_file('target: val\nentry:\n jw.inplace-link: target\n'))
|
|
assert data['entry'] == 'val'
|
|
|
|
def test_inplace_link_non_dict_target_siblings_raise(
|
|
self, yaml_file: Callable
|
|
) -> None:
|
|
"""Non-dict target with sibling keys raises instead of dropping them."""
|
|
with pytest.raises(ValueError, match = 'not a mapping'):
|
|
load_yaml(
|
|
yaml_file(
|
|
'target: val\nentry:\n jw.inplace-link: target\n extra: kept\n'
|
|
)
|
|
)
|
|
|
|
def test_inplace_link_list_target_siblings_raise(self, yaml_file: Callable) -> None:
|
|
"""List target with sibling keys raises instead of dropping them."""
|
|
with pytest.raises(ValueError, match = 'not a mapping'):
|
|
load_yaml(
|
|
yaml_file(
|
|
'target:\n - a\nentry:\n jw.inplace-link: target\n extra: kept\n'
|
|
)
|
|
)
|
|
|
|
def test_inplace_link_relative_path(self, yaml_file: Callable) -> None:
|
|
"""jw.inplace-link supports relative paths."""
|
|
data = load_yaml(
|
|
yaml_file(
|
|
'sibling:\n'
|
|
' x: resolved\n'
|
|
'root:\n'
|
|
' entry:\n'
|
|
' jw.inplace-link: ..sibling\n'
|
|
' y: extra\n'
|
|
)
|
|
)
|
|
assert data['root']['entry']['x'] == 'resolved'
|
|
assert data['root']['entry']['y'] == 'extra'
|
|
|
|
def test_inplace_link_cyclic(self, yaml_file: Callable) -> None:
|
|
"""jw.inplace-link detects cycles."""
|
|
with pytest.raises(ValueError, match = 'Cyclic link'):
|
|
load_yaml(yaml_file('a:\n jw.inplace-link: b\nb:\n jw.inplace-link: a\n'))
|
|
|
|
def test_inplace_link_chain(self, yaml_file: Callable) -> None:
|
|
"""jw.inplace-link resolves through transitive chains."""
|
|
data = load_yaml(
|
|
yaml_file(
|
|
'target: final\n'
|
|
'a:\n'
|
|
' jw.inplace-link: b\n'
|
|
'b:\n'
|
|
' jw.inplace-link: target\n'
|
|
)
|
|
)
|
|
assert data['a'] == 'final'
|
|
|
|
def test_inplace_link_with_list_sibling(self, yaml_file: Callable) -> None:
|
|
"""List sibling values merge into resolved dict."""
|
|
data = load_yaml(
|
|
yaml_file(
|
|
'target:\n'
|
|
' key: val\n'
|
|
'entry:\n'
|
|
' jw.inplace-link: target\n'
|
|
' items:\n'
|
|
' - a\n'
|
|
' - b\n'
|
|
)
|
|
)
|
|
assert data['entry']['key'] == 'val'
|
|
assert data['entry']['items'] == ['a', 'b']
|
|
|
|
def test_inplace_link_in_list(self, yaml_file: Callable) -> None:
|
|
"""jw.inplace-link works inside list items."""
|
|
data = load_yaml(
|
|
yaml_file(
|
|
'target:\n'
|
|
' x: 1\n'
|
|
'items:\n'
|
|
' - jw.inplace-link: target\n'
|
|
' y: 2\n'
|
|
' - plain\n'
|
|
)
|
|
)
|
|
assert data['items'][0] == {'x': 1, 'y': 2}
|
|
assert data['items'][1] == 'plain'
|
|
|
|
def test_inplace_link_deep_nesting(self, yaml_file: Callable) -> None:
|
|
"""jw.inplace-link merges deeply nested dicts."""
|
|
data = load_yaml(
|
|
yaml_file(
|
|
'target:\n'
|
|
' a:\n'
|
|
' b:\n'
|
|
' c: original\n'
|
|
'entry:\n'
|
|
' jw.inplace-link: target\n'
|
|
' a:\n'
|
|
' b:\n'
|
|
' d: added\n'
|
|
' e: new\n'
|
|
)
|
|
)
|
|
assert data['entry']['a']['b']['c'] == 'original'
|
|
assert data['entry']['a']['b']['d'] == 'added'
|
|
assert data['entry']['a']['e'] == 'new'
|
|
|
|
def test_inplace_link_override_deep(self, yaml_file: Callable) -> None:
|
|
"""Sibling values override target values in deep merge."""
|
|
data = load_yaml(
|
|
yaml_file(
|
|
'target:\n'
|
|
' a:\n'
|
|
' b: original\n'
|
|
'entry:\n'
|
|
' jw.inplace-link: target\n'
|
|
' a:\n'
|
|
' b: overridden\n'
|
|
)
|
|
)
|
|
assert data['entry']['a']['b'] == 'overridden'
|
|
|
|
def test_inplace_link_with_include(self, temp_dir: Callable) -> None:
|
|
"""jw.inplace-link works with included files."""
|
|
base = temp_dir(
|
|
[
|
|
('base.yaml', 'shared:\n from: include\n'),
|
|
(
|
|
'main.yaml',
|
|
'jw.include: base.yaml\nentry:\n'
|
|
' jw.inplace-link: shared\n local: here\n'
|
|
),
|
|
]
|
|
)
|
|
data = load_yaml(pathlib.Path(base) / 'main.yaml')
|
|
assert data['entry']['from'] == 'include'
|
|
assert data['entry']['local'] == 'here'
|
|
|
|
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:
|
|
"""Local content at target path overrides included content."""
|
|
base = temp_dir(
|
|
[
|
|
('base.yaml', 'existing: from_include\n'),
|
|
(
|
|
'main.yaml',
|
|
'entry:\n existing: original\njw.include: entry|base.yaml\n'
|
|
),
|
|
]
|
|
)
|
|
data = load_yaml(pathlib.Path(base) / 'main.yaml')
|
|
# Local content (original) wins over included (from_include)
|
|
assert data['entry']['existing'] == 'original'
|
|
|
|
def test_include_non_dict_intermediate_raises(self, temp_dir: Callable) -> None:
|
|
"""Including through an existing non-dict value raises."""
|
|
base = temp_dir(
|
|
[
|
|
('base.yaml', 'inc: from_include\n'),
|
|
('main.yaml', 'a:\n b: scalar\njw.include: a.b.c|base.yaml\n'),
|
|
]
|
|
)
|
|
with pytest.raises(ValueError, match = 'not a mapping'):
|
|
load_yaml(pathlib.Path(base) / 'main.yaml')
|
|
|
|
def test_include_non_dict_leaf_raises(self, temp_dir: Callable) -> None:
|
|
"""Including at an existing non-dict leaf raises."""
|
|
base = temp_dir(
|
|
[
|
|
('base.yaml', 'inc: from_include\n'),
|
|
(
|
|
'main.yaml',
|
|
'entry:\n existing: local\njw.include: entry.existing|base.yaml\n',
|
|
),
|
|
]
|
|
)
|
|
with pytest.raises(ValueError, match = 'not a mapping'):
|
|
load_yaml(pathlib.Path(base) / 'main.yaml')
|
|
|
|
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_pipe_in_filename(self, temp_dir: Callable) -> None:
|
|
"""The first pipe separates target and file, pipes stay in the file."""
|
|
base = temp_dir(
|
|
[
|
|
('weird|name.yaml', 'pip: ed\n'),
|
|
('main.yaml', 'jw.include: section|weird|name.yaml\n'),
|
|
]
|
|
)
|
|
data = load_yaml(pathlib.Path(base) / 'main.yaml')
|
|
assert data['section']['pip'] == 'ed'
|
|
|
|
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."""
|
|
|
|
def test_absolute_single_key(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(yaml_file('key: val\nlink:\n jw.link: key\n'))
|
|
assert data['link'] == 'val'
|
|
|
|
def test_absolute_multi_level(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(yaml_file('a:\n b:\n c: deep\nlink:\n jw.link: a.b.c\n'))
|
|
assert data['link'] == 'deep'
|
|
|
|
def test_relative_from_list_index(self, yaml_file: Callable) -> None:
|
|
"""Links inside list items should resolve correctly."""
|
|
data = load_yaml(
|
|
yaml_file(
|
|
'target: val\nitems:\n - plain\n - jw.link: target\n - more\n'
|
|
)
|
|
)
|
|
assert data['items'][1] == 'val'
|
|
|
|
def test_link_to_nested_value(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(
|
|
yaml_file(
|
|
'a:\n nested:\n deep: value\nlink:\n jw.link: a.nested.deep\n'
|
|
)
|
|
)
|
|
assert data['link'] == 'value'
|
|
|
|
# -- jw.link expansion - Type preservation ------------------------------------
|
|
|
|
class TestTypePreservation:
|
|
"""Ensure linked values preserve their original types."""
|
|
|
|
def test_string_preserved(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(yaml_file('target: "42"\nlink:\n jw.link: target\n'))
|
|
assert isinstance(data['link'], str)
|
|
assert data['link'] == '42'
|
|
|
|
def test_int_preserved(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(yaml_file('target: 42\nlink:\n jw.link: target\n'))
|
|
assert isinstance(data['link'], int)
|
|
assert data['link'] == 42
|
|
|
|
def test_float_preserved(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(yaml_file('target: 3.14\nlink:\n jw.link: target\n'))
|
|
assert isinstance(data['link'], float)
|
|
assert data['link'] == pytest.approx(3.14)
|
|
|
|
def test_bool_preserved(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(yaml_file('target: true\nlink:\n jw.link: target\n'))
|
|
assert isinstance(data['link'], bool)
|
|
assert data['link'] is True
|
|
|
|
def test_list_preserved(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(
|
|
yaml_file('target:\n - 1\n - 2\n - 3\nlink:\n jw.link: target\n')
|
|
)
|
|
assert isinstance(data['link'], list)
|
|
assert data['link'] == [1, 2, 3]
|
|
|
|
def test_nested_types_preserved(self, yaml_file: Callable) -> None:
|
|
data = load_yaml(
|
|
yaml_file(
|
|
'target:\n - num: 1\n flag: true\nlink:\n jw.link: target\n'
|
|
)
|
|
)
|
|
assert data['link'][0]['num'] == 1
|
|
assert data['link'][0]['flag'] is True
|