load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
"""Unit tests for jw.util.load_yaml."""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import pathlib
|
|
|
|
|
import tempfile
|
2026-08-16 12:15:47 +02:00
|
|
|
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
from typing import Callable, Generator
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
from jw.util.load_yaml import load_yaml
|
|
|
|
|
|
|
|
|
|
# -- Helpers ------------------------------------------------------------------
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
_YamlFile = Callable[[str], str]
|
|
|
|
|
_TempDir = Callable[[list[tuple[str, str]]], str]
|
|
|
|
|
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
@pytest.fixture
|
2026-09-11 17:00:03 +02:00
|
|
|
def yaml_file() -> _YamlFile:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
"""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
|
2026-09-11 17:00:03 +02:00
|
|
|
def temp_dir() -> Generator[_TempDir, None, None]:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
"""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."""
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_simple_dict(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
data = load_yaml(yaml_file('key: value\n'))
|
|
|
|
|
assert data == {'key': 'value'}
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_nested_dict(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
data = load_yaml(yaml_file('outer:\n inner: value\n number: 42\n'))
|
|
|
|
|
assert data == {'outer': {'inner': 'value', 'number': 42}}
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_list_value(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
data = load_yaml(yaml_file('items:\n - a\n - b\n - c\n'))
|
|
|
|
|
assert data == {'items': ['a', 'b', 'c']}
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_empty_file(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
data = load_yaml(yaml_file(''))
|
|
|
|
|
assert data == {}
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_none_root(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
data = load_yaml(yaml_file('---\n'))
|
|
|
|
|
assert data == {}
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_string_value(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
data = load_yaml(yaml_file('key: string value\n'))
|
|
|
|
|
assert data == {'key': 'string value'}
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_number_value(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
data = load_yaml(yaml_file('int: 42\nfloat: 3.14\n'))
|
|
|
|
|
assert data['int'] == 42
|
|
|
|
|
assert data['float'] == 3.14
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_boolean_value(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
data = load_yaml(yaml_file('enabled: true\ndisabled: false\n'))
|
|
|
|
|
assert data['enabled'] is True
|
|
|
|
|
assert data['disabled'] is False
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_null_value(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
data = load_yaml(yaml_file('key: null\n'))
|
|
|
|
|
assert data['key'] is None
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_multiline_string(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
data = load_yaml(yaml_file('key: |\n line1\n line2\n'))
|
|
|
|
|
assert data['key'] == 'line1\nline2\n'
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_deeply_nested(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
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)
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_present_optional(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
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."""
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_string_root_raises(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
with pytest.raises(TypeError, match = 'root must be a mapping'):
|
|
|
|
|
load_yaml(yaml_file('"just a string"\n'))
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_list_root_raises(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
with pytest.raises(TypeError, match = 'root must be a mapping'):
|
|
|
|
|
load_yaml(yaml_file('- item1\n- item2\n'))
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_number_root_raises(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
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."""
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_single_include(self, temp_dir: _TempDir) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
base = temp_dir(
|
|
|
|
|
[
|
|
|
|
|
('base.yaml', 'shared: from_base\n'),
|
2026-07-03 17:10:02 +02:00
|
|
|
('main.yaml', 'jw.include: base.yaml\ncurrent: here\n'),
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
data = load_yaml(pathlib.Path(base) / 'main.yaml')
|
|
|
|
|
assert data['shared'] == 'from_base'
|
|
|
|
|
assert data['current'] == 'here'
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_multiple_includes(self, temp_dir: _TempDir) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
base = temp_dir(
|
|
|
|
|
[
|
|
|
|
|
('a.yaml', 'from_a: 1\nshared: a\n'),
|
|
|
|
|
('b.yaml', 'from_b: 2\nshared: b\n'),
|
2026-07-03 17:10:02 +02:00
|
|
|
('main.yaml', 'jw.include:\n - a.yaml\n - b.yaml\nlocal: yes\n'),
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_optional_include(self, temp_dir: _TempDir) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
base = temp_dir(
|
|
|
|
|
[
|
2026-07-03 17:10:02 +02:00
|
|
|
('main.yaml', 'jw.include:\n - "- nonexist.yaml"\nok: true\n'),
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
data = load_yaml(pathlib.Path(base) / 'main.yaml')
|
|
|
|
|
assert data == {'ok': True}
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_deep_merge_dicts(self, temp_dir: _TempDir) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
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'),
|
2026-07-03 17:10:02 +02:00
|
|
|
(
|
|
|
|
|
'main.yaml',
|
|
|
|
|
'jw.include:\n'
|
|
|
|
|
' - override.yaml\n'
|
|
|
|
|
'config:\n'
|
|
|
|
|
' cache: true\n'
|
|
|
|
|
),
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_nested_includes(self, temp_dir: _TempDir) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
base = temp_dir(
|
|
|
|
|
[
|
|
|
|
|
('level1.yaml', 'from: level1\n'),
|
2026-07-03 17:10:02 +02:00
|
|
|
('level2.yaml', 'jw.include: level1.yaml\nfrom: level2\n'),
|
|
|
|
|
('main.yaml', 'jw.include: level2.yaml\n'),
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
data = load_yaml(pathlib.Path(base) / 'main.yaml')
|
|
|
|
|
assert data['from'] == 'level2'
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_include_circular_detect(self, temp_dir: _TempDir) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
base = temp_dir(
|
|
|
|
|
[
|
2026-07-03 17:10:02 +02:00
|
|
|
('a.yaml', 'jw.include: b.yaml\n'),
|
|
|
|
|
('b.yaml', 'jw.include: a.yaml\n'),
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
with pytest.raises(ValueError, match = 'Cyclic YAML include'):
|
|
|
|
|
load_yaml(pathlib.Path(base) / 'a.yaml')
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_include_relative_path(self, temp_dir: _TempDir) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
base = temp_dir(
|
|
|
|
|
[
|
|
|
|
|
('sub/inner.yaml', 'secret: found\n'),
|
2026-07-03 17:10:02 +02:00
|
|
|
('main.yaml', 'jw.include: sub/inner.yaml\n'),
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
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."""
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_inplace_absolute(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
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'
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_inplace_dict_target(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
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}
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_inplace_list_target(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
data = load_yaml(yaml_file('target:\n - x\n - y\nlink:\n jw.link: target\n'))
|
|
|
|
|
assert data['link'] == ['x', 'y']
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_inplace_in_list(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
data = load_yaml(
|
|
|
|
|
yaml_file(
|
|
|
|
|
'target: val\nitems:\n - plain\n - jw.link: target\n - more\n'
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
assert data['items'] == ['plain', 'val', 'more']
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_inplace_scalar_in_list(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
data = load_yaml(yaml_file('target: 42\nitems:\n - 1\n - jw.link: target\n'))
|
|
|
|
|
assert data['items'] == [1, 42]
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_inplace_in_nested_dict(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
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."""
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_named_absolute(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
data = load_yaml(
|
|
|
|
|
yaml_file('target:\n key: val\nlink:\n jw.link: name|target.key\n')
|
|
|
|
|
)
|
|
|
|
|
assert data['link'] == {'name': 'val'}
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_named_dict_target(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
data = load_yaml(yaml_file('target:\n a: 1\nlink:\n jw.link: alias|target\n'))
|
|
|
|
|
assert data['link'] == {'alias': {'a': 1}}
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_named_in_list(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
data = load_yaml(
|
|
|
|
|
yaml_file('target: val\nitems:\n - plain\n - jw.link: renamed|target\n')
|
|
|
|
|
)
|
|
|
|
|
assert data['items'] == ['plain', {'renamed': 'val'}]
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_named_with_dashes_and_underscores(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
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."""
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_relative_up_one(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
data = load_yaml(
|
|
|
|
|
yaml_file(
|
|
|
|
|
'parent:\n'
|
|
|
|
|
' child:\n'
|
|
|
|
|
' link:\n'
|
|
|
|
|
' jw.link: ..sibling\n'
|
|
|
|
|
' sibling: value\n'
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
assert data['parent']['child']['link'] == 'value'
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_relative_up_one_simple(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
data = load_yaml(
|
|
|
|
|
yaml_file('sibling: val\nnode:\n link:\n jw.link: ..sibling\n')
|
|
|
|
|
)
|
|
|
|
|
assert data['node']['link'] == 'val'
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_relative_up_two(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
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'
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_relative_up_two_simple(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
data = load_yaml(
|
|
|
|
|
yaml_file('base_val: my_base\nsub:\n link:\n jw.link: ..base_val\n')
|
|
|
|
|
)
|
|
|
|
|
assert data['sub']['link'] == 'my_base'
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_relative_up_then_descend(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
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'
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_relative_up_then_descend_simple(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
data = load_yaml(
|
|
|
|
|
yaml_file('shared:\n value: ok\nnode:\n link:\n jw.link: ..shared\n')
|
|
|
|
|
)
|
|
|
|
|
assert data['node']['link'] == {'value': 'ok'}
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_relative_same_level(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
data = load_yaml(yaml_file('a: 1\nb:\n link:\n jw.link: ..a\n'))
|
|
|
|
|
assert data['b']['link'] == 1
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_relative_same_level_simple(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
data = load_yaml(
|
|
|
|
|
yaml_file('sibling: val\nnode:\n link:\n jw.link: ..sibling\n')
|
|
|
|
|
)
|
|
|
|
|
assert data['node']['link'] == 'val'
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_relative_above_root_raises(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
with pytest.raises(ValueError, match = 'above root'):
|
|
|
|
|
load_yaml(yaml_file('link:\n jw.link: ....above\n'))
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_relative_deep_nesting(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
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'
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_relative_self_same_dict(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
# 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'))
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_relative_up_to_root_then_descend(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
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'
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_relative_up_from_deep(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
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'
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_relative_link_to_sibling_dict(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
data = load_yaml(yaml_file('a:\n x: 1\nb:\n link:\n jw.link: ..a\n'))
|
|
|
|
|
assert data['b']['link'] == {'x': 1}
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_relative_up_two_from_deep(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
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."""
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_chain_of_links(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
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'
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_chain_inplace(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
data = load_yaml(
|
|
|
|
|
yaml_file('value: 100\na:\n jw.link: b\nb:\n jw.link: value\n')
|
|
|
|
|
)
|
|
|
|
|
assert data['a'] == 100
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_chain_named(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
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."""
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_self_referencing_link(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
with pytest.raises(ValueError, match = 'Cyclic link'):
|
|
|
|
|
load_yaml(yaml_file('link:\n jw.link: link\n'))
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_mutual_cycle(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
with pytest.raises(ValueError, match = 'Cyclic link'):
|
|
|
|
|
load_yaml(yaml_file('a:\n jw.link: b\nb:\n jw.link: a\n'))
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_self_cycle_nested(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
with pytest.raises(ValueError, match = 'Cyclic link'):
|
|
|
|
|
load_yaml(
|
|
|
|
|
yaml_file('root:\n inner:\n link:\n jw.link: root.inner\n')
|
|
|
|
|
)
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_cycle_deep_chain(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
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."""
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_missing_absolute_target(self, yaml_file: _YamlFile) -> None:
|
2026-08-16 12:18:22 +02:00
|
|
|
with pytest.raises(ValueError, match = 'cannot resolve target path'):
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
load_yaml(yaml_file('link:\n jw.link: nonexistent\n'))
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_missing_relative_target(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
with pytest.raises(ValueError, match = 'above root'):
|
|
|
|
|
load_yaml(yaml_file('link:\n jw.link: ..missing\n'))
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_missing_deep_target(self, yaml_file: _YamlFile) -> None:
|
2026-08-16 12:18:22 +02:00
|
|
|
with pytest.raises(ValueError, match = 'cannot resolve target path'):
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
load_yaml(
|
|
|
|
|
yaml_file('exists:\n val: ok\nlink:\n jw.link: exists.missing\n')
|
|
|
|
|
)
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_missing_relative_deep_target(self, yaml_file: _YamlFile) -> None:
|
2026-08-16 12:18:22 +02:00
|
|
|
with pytest.raises(ValueError, match = 'cannot resolve target path'):
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
load_yaml(
|
|
|
|
|
yaml_file('a:\n b: val\nnode:\n link:\n jw.link: ..a.missing\n')
|
|
|
|
|
)
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_missing_target_in_list_raises(self, yaml_file: _YamlFile) -> None:
|
2026-08-16 12:18:22 +02:00
|
|
|
"""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'))
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_missing_target_error_names_link(self, yaml_file: _YamlFile) -> None:
|
2026-08-16 12:18:22 +02:00
|
|
|
"""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'))
|
|
|
|
|
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
# -- jw.link expansion - No links (regression) --------------------------------
|
|
|
|
|
|
|
|
|
|
class TestNoLinksRegression:
|
|
|
|
|
"""Ensure data without links passes through unchanged."""
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_simple_no_links(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
data = load_yaml(yaml_file('a: 1\nb:\n c: 2\n'))
|
|
|
|
|
assert data == {'a': 1, 'b': {'c': 2}}
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_complex_structure(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
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
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_jw_link_with_siblings_unchanged(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
"""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."""
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_include_then_link(self, temp_dir: _TempDir) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
base = temp_dir(
|
|
|
|
|
[
|
|
|
|
|
('base.yaml', 'shared: from_include\n'),
|
2026-07-03 17:10:02 +02:00
|
|
|
('main.yaml', 'jw.include: base.yaml\nlink:\n jw.link: shared\n'),
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
data = load_yaml(pathlib.Path(base) / 'main.yaml')
|
|
|
|
|
assert data['shared'] == 'from_include'
|
|
|
|
|
assert data['link'] == 'from_include'
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_link_in_included_file(self, temp_dir: _TempDir) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
base = temp_dir(
|
|
|
|
|
[
|
|
|
|
|
('base.yaml', 'target: included\ndecorator:\n jw.link: target\n'),
|
2026-07-03 17:10:02 +02:00
|
|
|
('main.yaml', 'jw.include: base.yaml\nlocal: here\n'),
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
data = load_yaml(pathlib.Path(base) / 'main.yaml')
|
|
|
|
|
assert data['target'] == 'included'
|
|
|
|
|
assert data['decorator'] == 'included'
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_relative_link_to_included(self, temp_dir: _TempDir) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
base = temp_dir(
|
|
|
|
|
[
|
|
|
|
|
('base.yaml', 'base_val: ok\n'),
|
|
|
|
|
(
|
|
|
|
|
'main.yaml',
|
2026-07-03 17:10:02 +02:00
|
|
|
'jw.include: base.yaml\nlocal:\n link:\n jw.link: ..base_val\n'
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
),
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
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."""
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_empty_string_value(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
data = load_yaml(yaml_file('target: ""\nlink:\n jw.link: target\n'))
|
|
|
|
|
assert data['link'] == ''
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_zero_value(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
data = load_yaml(yaml_file('target: 0\nlink:\n jw.link: target\n'))
|
|
|
|
|
assert data['link'] == 0
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_false_value(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
data = load_yaml(yaml_file('target: false\nlink:\n jw.link: target\n'))
|
|
|
|
|
assert data['link'] is False
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_none_target(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
data = load_yaml(yaml_file('target: null\nlink:\n jw.link: target\n'))
|
|
|
|
|
assert data['link'] is None
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_complex_nested_inplace(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
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'
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_link_to_list_of_dicts(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
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'}]
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_multiple_links_same_target(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
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'
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_cached_link_expansion_not_aliased(self, yaml_file: _YamlFile) -> None:
|
2026-08-16 12:21:47 +02:00
|
|
|
"""Link results shared via the memoisation cache are independent."""
|
|
|
|
|
data = load_yaml(
|
|
|
|
|
yaml_file(
|
|
|
|
|
'target:\n'
|
|
|
|
|
' x: 1\n'
|
|
|
|
|
'link_b:\n'
|
|
|
|
|
' jw.link: target\n'
|
|
|
|
|
'link_a:\n'
|
|
|
|
|
' jw.link: link_b\n'
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
data['link_a']['x'] = 99
|
|
|
|
|
assert data['link_b']['x'] == 1
|
|
|
|
|
assert data['target']['x'] == 1
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_named_link_to_list(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
data = load_yaml(
|
|
|
|
|
yaml_file('target:\n - x\n - y\nlink:\n jw.link: items|target\n')
|
|
|
|
|
)
|
|
|
|
|
assert data['link'] == {'items': ['x', 'y']}
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_link_value_with_pipe_in_target_key(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
"""Key with pipe in name — pipe triggers named link parsing."""
|
2026-08-16 12:18:22 +02:00
|
|
|
with pytest.raises(ValueError, match = 'cannot resolve target path'):
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
load_yaml(yaml_file('my|key: val\nlink:\n jw.link: my|key\n'))
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_named_link_with_numeric_prefix(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
data = load_yaml(yaml_file('target: ok\nlink:\n jw.link: 123target|target\n'))
|
|
|
|
|
assert data['link'] == {'123target': 'ok'}
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_non_string_link_value_raises(self, yaml_file: _YamlFile) -> None:
|
2026-08-16 12:18:55 +02:00
|
|
|
"""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'))
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_non_string_inplace_link_value_raises(self, yaml_file: _YamlFile) -> None:
|
2026-08-16 12:18:55 +02:00
|
|
|
"""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'))
|
|
|
|
|
|
load_yaml: Add jw.inplace-link with sibling merge
jw.inplace-link behaves like jw.link but can have sibling keys that
are deep-merged into the resolved target dict (siblings override target
on conflict).
Usage:
entry:
jw.inplace-link: path.to.target
sibling_key: value
Expands to:
entry:
<merged result of path.to.target + sibling_key: value>
This solves the common use case of pulling in a dict from elsewhere
while overriding specific keys inline.
Added 12 unit tests covering: no siblings, dict siblings, scalar
siblings, non-dict targets, relative paths, cycle detection, chains,
list siblings, list items, deep nesting, sibling override, and
integration with jw.include.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-08 12:55:05 +02:00
|
|
|
# -- jw.inplace-link expansion ------------------------------------------------
|
|
|
|
|
|
|
|
|
|
class TestInplaceLink:
|
|
|
|
|
"""Tests for jw.inplace-link expansion with sibling merging."""
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_inplace_link_no_siblings(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Add jw.inplace-link with sibling merge
jw.inplace-link behaves like jw.link but can have sibling keys that
are deep-merged into the resolved target dict (siblings override target
on conflict).
Usage:
entry:
jw.inplace-link: path.to.target
sibling_key: value
Expands to:
entry:
<merged result of path.to.target + sibling_key: value>
This solves the common use case of pulling in a dict from elsewhere
while overriding specific keys inline.
Added 12 unit tests covering: no siblings, dict siblings, scalar
siblings, non-dict targets, relative paths, cycle detection, chains,
list siblings, list items, deep nesting, sibling override, and
integration with jw.include.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-08 12:55:05 +02:00
|
|
|
"""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'
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_inplace_link_with_dict_siblings(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Add jw.inplace-link with sibling merge
jw.inplace-link behaves like jw.link but can have sibling keys that
are deep-merged into the resolved target dict (siblings override target
on conflict).
Usage:
entry:
jw.inplace-link: path.to.target
sibling_key: value
Expands to:
entry:
<merged result of path.to.target + sibling_key: value>
This solves the common use case of pulling in a dict from elsewhere
while overriding specific keys inline.
Added 12 unit tests covering: no siblings, dict siblings, scalar
siblings, non-dict targets, relative paths, cycle detection, chains,
list siblings, list items, deep nesting, sibling override, and
integration with jw.include.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-08 12:55:05 +02:00
|
|
|
"""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
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_inplace_link_with_scalar_sibling(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Add jw.inplace-link with sibling merge
jw.inplace-link behaves like jw.link but can have sibling keys that
are deep-merged into the resolved target dict (siblings override target
on conflict).
Usage:
entry:
jw.inplace-link: path.to.target
sibling_key: value
Expands to:
entry:
<merged result of path.to.target + sibling_key: value>
This solves the common use case of pulling in a dict from elsewhere
while overriding specific keys inline.
Added 12 unit tests covering: no siblings, dict siblings, scalar
siblings, non-dict targets, relative paths, cycle detection, chains,
list siblings, list items, deep nesting, sibling override, and
integration with jw.include.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-08 12:55:05 +02:00
|
|
|
"""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
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_inplace_link_non_dict_target(self, yaml_file: _YamlFile) -> None:
|
2026-08-16 12:16:57 +02:00
|
|
|
"""Non-dict target without siblings replaces the value."""
|
load_yaml: Add jw.inplace-link with sibling merge
jw.inplace-link behaves like jw.link but can have sibling keys that
are deep-merged into the resolved target dict (siblings override target
on conflict).
Usage:
entry:
jw.inplace-link: path.to.target
sibling_key: value
Expands to:
entry:
<merged result of path.to.target + sibling_key: value>
This solves the common use case of pulling in a dict from elsewhere
while overriding specific keys inline.
Added 12 unit tests covering: no siblings, dict siblings, scalar
siblings, non-dict targets, relative paths, cycle detection, chains,
list siblings, list items, deep nesting, sibling override, and
integration with jw.include.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-08 12:55:05 +02:00
|
|
|
data = load_yaml(yaml_file('target: val\nentry:\n jw.inplace-link: target\n'))
|
|
|
|
|
assert data['entry'] == 'val'
|
|
|
|
|
|
2026-08-16 12:16:57 +02:00
|
|
|
def test_inplace_link_non_dict_target_siblings_raise(
|
2026-09-11 17:00:03 +02:00
|
|
|
self, yaml_file: _YamlFile
|
2026-08-16 12:16:57 +02:00
|
|
|
) -> 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'
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_inplace_link_list_target_siblings_raise(
|
|
|
|
|
self, yaml_file: _YamlFile
|
|
|
|
|
) -> None:
|
2026-08-16 12:16:57 +02:00
|
|
|
"""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'
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_inplace_link_relative_path(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Add jw.inplace-link with sibling merge
jw.inplace-link behaves like jw.link but can have sibling keys that
are deep-merged into the resolved target dict (siblings override target
on conflict).
Usage:
entry:
jw.inplace-link: path.to.target
sibling_key: value
Expands to:
entry:
<merged result of path.to.target + sibling_key: value>
This solves the common use case of pulling in a dict from elsewhere
while overriding specific keys inline.
Added 12 unit tests covering: no siblings, dict siblings, scalar
siblings, non-dict targets, relative paths, cycle detection, chains,
list siblings, list items, deep nesting, sibling override, and
integration with jw.include.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-08 12:55:05 +02:00
|
|
|
"""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'
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_inplace_link_cyclic(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Add jw.inplace-link with sibling merge
jw.inplace-link behaves like jw.link but can have sibling keys that
are deep-merged into the resolved target dict (siblings override target
on conflict).
Usage:
entry:
jw.inplace-link: path.to.target
sibling_key: value
Expands to:
entry:
<merged result of path.to.target + sibling_key: value>
This solves the common use case of pulling in a dict from elsewhere
while overriding specific keys inline.
Added 12 unit tests covering: no siblings, dict siblings, scalar
siblings, non-dict targets, relative paths, cycle detection, chains,
list siblings, list items, deep nesting, sibling override, and
integration with jw.include.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-08 12:55:05 +02:00
|
|
|
"""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'))
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_inplace_link_chain(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Add jw.inplace-link with sibling merge
jw.inplace-link behaves like jw.link but can have sibling keys that
are deep-merged into the resolved target dict (siblings override target
on conflict).
Usage:
entry:
jw.inplace-link: path.to.target
sibling_key: value
Expands to:
entry:
<merged result of path.to.target + sibling_key: value>
This solves the common use case of pulling in a dict from elsewhere
while overriding specific keys inline.
Added 12 unit tests covering: no siblings, dict siblings, scalar
siblings, non-dict targets, relative paths, cycle detection, chains,
list siblings, list items, deep nesting, sibling override, and
integration with jw.include.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-08 12:55:05 +02:00
|
|
|
"""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'
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_inplace_link_with_list_sibling(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Add jw.inplace-link with sibling merge
jw.inplace-link behaves like jw.link but can have sibling keys that
are deep-merged into the resolved target dict (siblings override target
on conflict).
Usage:
entry:
jw.inplace-link: path.to.target
sibling_key: value
Expands to:
entry:
<merged result of path.to.target + sibling_key: value>
This solves the common use case of pulling in a dict from elsewhere
while overriding specific keys inline.
Added 12 unit tests covering: no siblings, dict siblings, scalar
siblings, non-dict targets, relative paths, cycle detection, chains,
list siblings, list items, deep nesting, sibling override, and
integration with jw.include.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-08 12:55:05 +02:00
|
|
|
"""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']
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_inplace_link_in_list(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Add jw.inplace-link with sibling merge
jw.inplace-link behaves like jw.link but can have sibling keys that
are deep-merged into the resolved target dict (siblings override target
on conflict).
Usage:
entry:
jw.inplace-link: path.to.target
sibling_key: value
Expands to:
entry:
<merged result of path.to.target + sibling_key: value>
This solves the common use case of pulling in a dict from elsewhere
while overriding specific keys inline.
Added 12 unit tests covering: no siblings, dict siblings, scalar
siblings, non-dict targets, relative paths, cycle detection, chains,
list siblings, list items, deep nesting, sibling override, and
integration with jw.include.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-08 12:55:05 +02:00
|
|
|
"""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'
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_inplace_link_deep_nesting(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Add jw.inplace-link with sibling merge
jw.inplace-link behaves like jw.link but can have sibling keys that
are deep-merged into the resolved target dict (siblings override target
on conflict).
Usage:
entry:
jw.inplace-link: path.to.target
sibling_key: value
Expands to:
entry:
<merged result of path.to.target + sibling_key: value>
This solves the common use case of pulling in a dict from elsewhere
while overriding specific keys inline.
Added 12 unit tests covering: no siblings, dict siblings, scalar
siblings, non-dict targets, relative paths, cycle detection, chains,
list siblings, list items, deep nesting, sibling override, and
integration with jw.include.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-08 12:55:05 +02:00
|
|
|
"""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'
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_inplace_link_override_deep(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Add jw.inplace-link with sibling merge
jw.inplace-link behaves like jw.link but can have sibling keys that
are deep-merged into the resolved target dict (siblings override target
on conflict).
Usage:
entry:
jw.inplace-link: path.to.target
sibling_key: value
Expands to:
entry:
<merged result of path.to.target + sibling_key: value>
This solves the common use case of pulling in a dict from elsewhere
while overriding specific keys inline.
Added 12 unit tests covering: no siblings, dict siblings, scalar
siblings, non-dict targets, relative paths, cycle detection, chains,
list siblings, list items, deep nesting, sibling override, and
integration with jw.include.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-08 12:55:05 +02:00
|
|
|
"""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'
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_inplace_link_with_include(self, temp_dir: _TempDir) -> None:
|
load_yaml: Add jw.inplace-link with sibling merge
jw.inplace-link behaves like jw.link but can have sibling keys that
are deep-merged into the resolved target dict (siblings override target
on conflict).
Usage:
entry:
jw.inplace-link: path.to.target
sibling_key: value
Expands to:
entry:
<merged result of path.to.target + sibling_key: value>
This solves the common use case of pulling in a dict from elsewhere
while overriding specific keys inline.
Added 12 unit tests covering: no siblings, dict siblings, scalar
siblings, non-dict targets, relative paths, cycle detection, chains,
list siblings, list items, deep nesting, sibling override, and
integration with jw.include.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-08 12:55:05 +02:00
|
|
|
"""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'
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_inplace_link_transitive_relative(self, yaml_file: _YamlFile) -> None:
|
2026-07-08 15:37:06 +02:00
|
|
|
"""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."""
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_include_at_root(self, temp_dir: _TempDir) -> None:
|
2026-07-08 15:37:06 +02:00
|
|
|
"""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'
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_include_at_nested_path(self, temp_dir: _TempDir) -> None:
|
2026-07-08 15:37:06 +02:00
|
|
|
"""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'
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_include_at_deep_path(self, temp_dir: _TempDir) -> None:
|
2026-07-08 15:37:06 +02:00
|
|
|
"""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'
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_include_merge_with_existing(self, temp_dir: _TempDir) -> None:
|
2026-07-08 15:37:06 +02:00
|
|
|
"""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'
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_include_override_existing(self, temp_dir: _TempDir) -> None:
|
2026-08-13 10:12:55 +02:00
|
|
|
"""Local content at target path overrides included content."""
|
2026-07-08 15:37:06 +02:00
|
|
|
base = temp_dir(
|
|
|
|
|
[
|
2026-08-13 10:12:55 +02:00
|
|
|
('base.yaml', 'existing: from_include\n'),
|
2026-07-08 15:37:06 +02:00
|
|
|
(
|
|
|
|
|
'main.yaml',
|
|
|
|
|
'entry:\n existing: original\njw.include: entry|base.yaml\n'
|
|
|
|
|
),
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
data = load_yaml(pathlib.Path(base) / 'main.yaml')
|
2026-08-13 10:12:55 +02:00
|
|
|
# Local content (original) wins over included (from_include)
|
|
|
|
|
assert data['entry']['existing'] == 'original'
|
2026-07-08 15:37:06 +02:00
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_include_non_dict_intermediate_raises(self, temp_dir: _TempDir) -> None:
|
2026-08-16 12:15:47 +02:00
|
|
|
"""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')
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_include_non_dict_leaf_raises(self, temp_dir: _TempDir) -> None:
|
2026-08-16 12:15:47 +02:00
|
|
|
"""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')
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_multiple_includes_different_paths(self, temp_dir: _TempDir) -> None:
|
2026-07-08 15:37:06 +02:00
|
|
|
"""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
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_include_with_optional(self, temp_dir: _TempDir) -> None:
|
2026-07-08 15:37:06 +02:00
|
|
|
"""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'
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_optional_include_missing_target(self, temp_dir: _TempDir) -> None:
|
2026-08-25 20:08:23 +02:00
|
|
|
"""A missing optional include at a target path creates no key."""
|
|
|
|
|
base = temp_dir(
|
|
|
|
|
[
|
|
|
|
|
('main.yaml', 'jw.include: -a.b|missing.yaml\nlocal: yes\n'),
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
data = load_yaml(pathlib.Path(base) / 'main.yaml')
|
|
|
|
|
assert data == {'local': True}
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_optional_include_missing_existing_target(self, temp_dir: _TempDir) -> None:
|
2026-08-25 20:08:23 +02:00
|
|
|
"""A missing optional include leaves an existing target untouched."""
|
|
|
|
|
base = temp_dir(
|
|
|
|
|
[
|
|
|
|
|
(
|
|
|
|
|
'main.yaml',
|
|
|
|
|
'jw.include: -section|missing.yaml\nsection:\n kept: yes\n',
|
|
|
|
|
),
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
data = load_yaml(pathlib.Path(base) / 'main.yaml')
|
|
|
|
|
assert data == {'section': {'kept': True}}
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_include_pipe_in_filename(self, temp_dir: _TempDir) -> None:
|
2026-08-16 12:19:23 +02:00
|
|
|
"""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'
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_include_plain_and_path_mixed(self, temp_dir: _TempDir) -> None:
|
2026-07-08 15:37:06 +02:00
|
|
|
"""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'
|
|
|
|
|
|
load_yaml: Add globbing to jw.include specs
The jw.include file spec is resolved as a single literal file name,
so a set of files under one directory has to be listed one by one.
Let the file part of a spec use glob characters (*, ?, [...]).
_normalize_include_specs() expands every match into its own
IncludeSpec entry, so matches are loaded and deep-merged in lexical
order: at the root, later files win; at a target path, the existing
content wins as with a single include. A non-optional glob that
matches no files raises ValueError; the leading - prefix keeps a
glob optional. Document the globbing in the module docstring,
including the YAML quoting requirement for specs that start with a
glob character, and add tests covering the glob syntax, lexical
order, the no-match and optional cases, target-path merging,
absolute paths, and self-inclusion.
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v0.84.2
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-08-16 13:03:32 +02:00
|
|
|
# -- jw.include globbing --------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
class TestIncludeGlobbing:
|
|
|
|
|
"""Tests for glob support in jw.include file specs."""
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_glob_multiple_files(self, temp_dir: _TempDir) -> None:
|
load_yaml: Add globbing to jw.include specs
The jw.include file spec is resolved as a single literal file name,
so a set of files under one directory has to be listed one by one.
Let the file part of a spec use glob characters (*, ?, [...]).
_normalize_include_specs() expands every match into its own
IncludeSpec entry, so matches are loaded and deep-merged in lexical
order: at the root, later files win; at a target path, the existing
content wins as with a single include. A non-optional glob that
matches no files raises ValueError; the leading - prefix keeps a
glob optional. Document the globbing in the module docstring,
including the YAML quoting requirement for specs that start with a
glob character, and add tests covering the glob syntax, lexical
order, the no-match and optional cases, target-path merging,
absolute paths, and self-inclusion.
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v0.84.2
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-08-16 13:03:32 +02:00
|
|
|
"""All glob matches are included."""
|
|
|
|
|
base = temp_dir(
|
|
|
|
|
[
|
|
|
|
|
('conf/a.yaml', 'from_a: 1\n'),
|
|
|
|
|
('conf/b.yaml', 'from_b: 2\n'),
|
|
|
|
|
('main.yaml', 'jw.include: conf/*.yaml\n'),
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
data = load_yaml(pathlib.Path(base) / 'main.yaml')
|
|
|
|
|
assert data['from_a'] == 1
|
|
|
|
|
assert data['from_b'] == 2
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_glob_lexical_order(self, temp_dir: _TempDir) -> None:
|
load_yaml: Add globbing to jw.include specs
The jw.include file spec is resolved as a single literal file name,
so a set of files under one directory has to be listed one by one.
Let the file part of a spec use glob characters (*, ?, [...]).
_normalize_include_specs() expands every match into its own
IncludeSpec entry, so matches are loaded and deep-merged in lexical
order: at the root, later files win; at a target path, the existing
content wins as with a single include. A non-optional glob that
matches no files raises ValueError; the leading - prefix keeps a
glob optional. Document the globbing in the module docstring,
including the YAML quoting requirement for specs that start with a
glob character, and add tests covering the glob syntax, lexical
order, the no-match and optional cases, target-path merging,
absolute paths, and self-inclusion.
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v0.84.2
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-08-16 13:03:32 +02:00
|
|
|
"""Matches are included in lexical order, later files win."""
|
|
|
|
|
base = temp_dir(
|
|
|
|
|
[
|
|
|
|
|
('conf/1.yaml', 'order: first\nshared: one\n'),
|
|
|
|
|
('conf/10.yaml', 'order: ten\n'),
|
|
|
|
|
('conf/2.yaml', 'order: second\nshared: two\n'),
|
|
|
|
|
('main.yaml', 'jw.include: conf/*.yaml\n'),
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
data = load_yaml(pathlib.Path(base) / 'main.yaml')
|
|
|
|
|
# Lexical order: 1.yaml, 10.yaml, 2.yaml
|
|
|
|
|
assert data['order'] == 'second'
|
|
|
|
|
assert data['shared'] == 'two'
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_glob_char_class(self, temp_dir: _TempDir) -> None:
|
load_yaml: Add globbing to jw.include specs
The jw.include file spec is resolved as a single literal file name,
so a set of files under one directory has to be listed one by one.
Let the file part of a spec use glob characters (*, ?, [...]).
_normalize_include_specs() expands every match into its own
IncludeSpec entry, so matches are loaded and deep-merged in lexical
order: at the root, later files win; at a target path, the existing
content wins as with a single include. A non-optional glob that
matches no files raises ValueError; the leading - prefix keeps a
glob optional. Document the globbing in the module docstring,
including the YAML quoting requirement for specs that start with a
glob character, and add tests covering the glob syntax, lexical
order, the no-match and optional cases, target-path merging,
absolute paths, and self-inclusion.
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v0.84.2
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-08-16 13:03:32 +02:00
|
|
|
"""Character classes match like other glob syntax."""
|
|
|
|
|
base = temp_dir(
|
|
|
|
|
[
|
|
|
|
|
('conf/g1.yaml', 'one: 1\n'),
|
|
|
|
|
('conf/g2.yaml', 'two: 2\n'),
|
|
|
|
|
('conf/g3.yaml', 'three: 3\n'),
|
|
|
|
|
('main.yaml', 'jw.include: conf/g[12].yaml\n'),
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
data = load_yaml(pathlib.Path(base) / 'main.yaml')
|
|
|
|
|
assert data['one'] == 1
|
|
|
|
|
assert data['two'] == 2
|
|
|
|
|
assert 'three' not in data
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_glob_question_mark(self, temp_dir: _TempDir) -> None:
|
load_yaml: Add globbing to jw.include specs
The jw.include file spec is resolved as a single literal file name,
so a set of files under one directory has to be listed one by one.
Let the file part of a spec use glob characters (*, ?, [...]).
_normalize_include_specs() expands every match into its own
IncludeSpec entry, so matches are loaded and deep-merged in lexical
order: at the root, later files win; at a target path, the existing
content wins as with a single include. A non-optional glob that
matches no files raises ValueError; the leading - prefix keeps a
glob optional. Document the globbing in the module docstring,
including the YAML quoting requirement for specs that start with a
glob character, and add tests covering the glob syntax, lexical
order, the no-match and optional cases, target-path merging,
absolute paths, and self-inclusion.
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v0.84.2
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-08-16 13:03:32 +02:00
|
|
|
"""A question mark matches exactly one character."""
|
|
|
|
|
base = temp_dir(
|
|
|
|
|
[
|
|
|
|
|
('conf/g1.yaml', 'one: 1\n'),
|
|
|
|
|
('conf/g12.yaml', 'twelve: 12\n'),
|
|
|
|
|
('main.yaml', 'jw.include: conf/g?.yaml\n'),
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
data = load_yaml(pathlib.Path(base) / 'main.yaml')
|
|
|
|
|
assert data['one'] == 1
|
|
|
|
|
assert 'twelve' not in data
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_glob_no_match_raises(self, temp_dir: _TempDir) -> None:
|
load_yaml: Add globbing to jw.include specs
The jw.include file spec is resolved as a single literal file name,
so a set of files under one directory has to be listed one by one.
Let the file part of a spec use glob characters (*, ?, [...]).
_normalize_include_specs() expands every match into its own
IncludeSpec entry, so matches are loaded and deep-merged in lexical
order: at the root, later files win; at a target path, the existing
content wins as with a single include. A non-optional glob that
matches no files raises ValueError; the leading - prefix keeps a
glob optional. Document the globbing in the module docstring,
including the YAML quoting requirement for specs that start with a
glob character, and add tests covering the glob syntax, lexical
order, the no-match and optional cases, target-path merging,
absolute paths, and self-inclusion.
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v0.84.2
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-08-16 13:03:32 +02:00
|
|
|
"""A non-optional glob matching no files raises."""
|
|
|
|
|
base = temp_dir([('main.yaml', 'jw.include: missing/*.yaml\n')])
|
|
|
|
|
with pytest.raises(ValueError, match = 'matches no files'):
|
|
|
|
|
load_yaml(pathlib.Path(base) / 'main.yaml')
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_glob_optional_no_match(self, temp_dir: _TempDir) -> None:
|
load_yaml: Add globbing to jw.include specs
The jw.include file spec is resolved as a single literal file name,
so a set of files under one directory has to be listed one by one.
Let the file part of a spec use glob characters (*, ?, [...]).
_normalize_include_specs() expands every match into its own
IncludeSpec entry, so matches are loaded and deep-merged in lexical
order: at the root, later files win; at a target path, the existing
content wins as with a single include. A non-optional glob that
matches no files raises ValueError; the leading - prefix keeps a
glob optional. Document the globbing in the module docstring,
including the YAML quoting requirement for specs that start with a
glob character, and add tests covering the glob syntax, lexical
order, the no-match and optional cases, target-path merging,
absolute paths, and self-inclusion.
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v0.84.2
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-08-16 13:03:32 +02:00
|
|
|
"""An optional glob matching no files contributes nothing."""
|
|
|
|
|
base = temp_dir([('main.yaml', 'jw.include: -missing/*.yaml\n')])
|
|
|
|
|
data = load_yaml(pathlib.Path(base) / 'main.yaml')
|
|
|
|
|
assert data == {}
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_glob_with_target_path(self, temp_dir: _TempDir) -> None:
|
load_yaml: Add globbing to jw.include specs
The jw.include file spec is resolved as a single literal file name,
so a set of files under one directory has to be listed one by one.
Let the file part of a spec use glob characters (*, ?, [...]).
_normalize_include_specs() expands every match into its own
IncludeSpec entry, so matches are loaded and deep-merged in lexical
order: at the root, later files win; at a target path, the existing
content wins as with a single include. A non-optional glob that
matches no files raises ValueError; the leading - prefix keeps a
glob optional. Document the globbing in the module docstring,
including the YAML quoting requirement for specs that start with a
glob character, and add tests covering the glob syntax, lexical
order, the no-match and optional cases, target-path merging,
absolute paths, and self-inclusion.
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v0.84.2
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-08-16 13:03:32 +02:00
|
|
|
"""Each match is merged at the target path, in lexical order."""
|
|
|
|
|
base = temp_dir(
|
|
|
|
|
[
|
|
|
|
|
('conf/a.yaml', 'first: a\nshared: from_a\n'),
|
|
|
|
|
('conf/b.yaml', 'shared: from_b\nextra: b\n'),
|
|
|
|
|
('main.yaml', 'jw.include: section|conf/*.yaml\n'),
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
data = load_yaml(pathlib.Path(base) / 'main.yaml')
|
|
|
|
|
assert data['section']['first'] == 'a'
|
|
|
|
|
# Existing content at the target path wins, so a.yaml's 'shared'
|
|
|
|
|
# beats b.yaml's; b.yaml still contributes its new key
|
|
|
|
|
assert data['section']['shared'] == 'from_a'
|
|
|
|
|
assert data['section']['extra'] == 'b'
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_glob_absolute_path(self, temp_dir: _TempDir) -> None:
|
load_yaml: Add globbing to jw.include specs
The jw.include file spec is resolved as a single literal file name,
so a set of files under one directory has to be listed one by one.
Let the file part of a spec use glob characters (*, ?, [...]).
_normalize_include_specs() expands every match into its own
IncludeSpec entry, so matches are loaded and deep-merged in lexical
order: at the root, later files win; at a target path, the existing
content wins as with a single include. A non-optional glob that
matches no files raises ValueError; the leading - prefix keeps a
glob optional. Document the globbing in the module docstring,
including the YAML quoting requirement for specs that start with a
glob character, and add tests covering the glob syntax, lexical
order, the no-match and optional cases, target-path merging,
absolute paths, and self-inclusion.
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v0.84.2
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-08-16 13:03:32 +02:00
|
|
|
"""Absolute glob paths work, not just relative ones."""
|
|
|
|
|
base = temp_dir(
|
|
|
|
|
[
|
|
|
|
|
('conf/a.yaml', 'from_a: 1\n'),
|
|
|
|
|
('conf/b.yaml', 'from_b: 2\n'),
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
main = pathlib.Path(base) / 'main.yaml'
|
|
|
|
|
main.write_text(f'jw.include: {base}/conf/*.yaml\n')
|
|
|
|
|
data = load_yaml(main)
|
|
|
|
|
assert data['from_a'] == 1
|
|
|
|
|
assert data['from_b'] == 2
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_glob_self_include_raises(self, temp_dir: _TempDir) -> None:
|
load_yaml: Add globbing to jw.include specs
The jw.include file spec is resolved as a single literal file name,
so a set of files under one directory has to be listed one by one.
Let the file part of a spec use glob characters (*, ?, [...]).
_normalize_include_specs() expands every match into its own
IncludeSpec entry, so matches are loaded and deep-merged in lexical
order: at the root, later files win; at a target path, the existing
content wins as with a single include. A non-optional glob that
matches no files raises ValueError; the leading - prefix keeps a
glob optional. Document the globbing in the module docstring,
including the YAML quoting requirement for specs that start with a
glob character, and add tests covering the glob syntax, lexical
order, the no-match and optional cases, target-path merging,
absolute paths, and self-inclusion.
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v0.84.2
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-08-16 13:03:32 +02:00
|
|
|
"""A glob matching the including file itself is a cycle."""
|
|
|
|
|
base = temp_dir([('main.yaml', 'jw.include: "*.yaml"\n')])
|
|
|
|
|
with pytest.raises(ValueError, match = 'Cyclic YAML include'):
|
|
|
|
|
load_yaml(pathlib.Path(base) / 'main.yaml')
|
|
|
|
|
|
2026-11-13 13:01:08 +01:00
|
|
|
class TestIncludeVarExpansion:
|
|
|
|
|
"""Tests for $(VAR) and ${VAR} expansion in include specs."""
|
|
|
|
|
|
|
|
|
|
def test_expand_braced_var(
|
|
|
|
|
self, temp_dir: _TempDir, monkeypatch: pytest.MonkeyPatch
|
|
|
|
|
) -> None:
|
|
|
|
|
"""${VAR} in a top-level include spec is expanded."""
|
|
|
|
|
base = temp_dir([('conf/a.yaml', 'from_a: 1\n')])
|
|
|
|
|
monkeypatch.setenv('LOAD_YAML_TEST_TOP', base)
|
|
|
|
|
main = pathlib.Path(base) / 'main.yaml'
|
|
|
|
|
main.write_text('jw.include: ${LOAD_YAML_TEST_TOP}/conf/a.yaml\n')
|
|
|
|
|
data = load_yaml(main)
|
|
|
|
|
assert data['from_a'] == 1
|
|
|
|
|
|
|
|
|
|
def test_expand_paren_var(
|
|
|
|
|
self, temp_dir: _TempDir, monkeypatch: pytest.MonkeyPatch
|
|
|
|
|
) -> None:
|
|
|
|
|
"""$(VAR) in a top-level include spec is expanded."""
|
|
|
|
|
base = temp_dir([('conf/a.yaml', 'from_a: 1\n')])
|
|
|
|
|
monkeypatch.setenv('LOAD_YAML_TEST_TOP', base)
|
|
|
|
|
main = pathlib.Path(base) / 'main.yaml'
|
|
|
|
|
main.write_text('jw.include: $(LOAD_YAML_TEST_TOP)/conf/a.yaml\n')
|
|
|
|
|
data = load_yaml(main)
|
|
|
|
|
assert data['from_a'] == 1
|
|
|
|
|
|
|
|
|
|
def test_expand_var_with_glob(
|
|
|
|
|
self, temp_dir: _TempDir, monkeypatch: pytest.MonkeyPatch
|
|
|
|
|
) -> None:
|
|
|
|
|
"""Expansion and glob expansion compose."""
|
|
|
|
|
base = temp_dir(
|
|
|
|
|
[
|
|
|
|
|
('conf/a.yaml', 'from_a: 1\n'),
|
|
|
|
|
('conf/b.yaml', 'from_b: 2\n'),
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
monkeypatch.setenv('LOAD_YAML_TEST_TOP', base)
|
|
|
|
|
main = pathlib.Path(base) / 'main.yaml'
|
|
|
|
|
main.write_text('jw.include: ${LOAD_YAML_TEST_TOP}/conf/*.yaml\n')
|
|
|
|
|
data = load_yaml(main)
|
|
|
|
|
assert data['from_a'] == 1
|
|
|
|
|
assert data['from_b'] == 2
|
|
|
|
|
|
|
|
|
|
def test_undefined_var_raises(self, temp_dir: _TempDir) -> None:
|
|
|
|
|
"""An undefined variable in a top-level spec raises."""
|
|
|
|
|
base = temp_dir([('main.yaml', 'jw.include: ${LOAD_YAML_TEST_NOPE}/x.yaml\n')])
|
|
|
|
|
with pytest.raises(ValueError, match = 'Undefined variable'):
|
|
|
|
|
load_yaml(pathlib.Path(base) / 'main.yaml')
|
|
|
|
|
|
|
|
|
|
def test_nested_spec_not_expanded(self, temp_dir: _TempDir) -> None:
|
|
|
|
|
"""Specs in included documents are never expanded."""
|
|
|
|
|
base = temp_dir(
|
|
|
|
|
[
|
|
|
|
|
('main.yaml', 'jw.include: nested/real.yaml\n'),
|
|
|
|
|
('nested/real.yaml', 'jw.include: ${HOME}/x.yaml\n'),
|
|
|
|
|
('nested/${HOME}/x.yaml', 'literal: yes\n'),
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
data = load_yaml(pathlib.Path(base) / 'main.yaml')
|
|
|
|
|
assert data['literal'] is True
|
|
|
|
|
|
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
2026-11-13 13:14:59 +01:00
|
|
|
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
|
|
|
|
|
|
2026-11-13 13:19:32 +01:00
|
|
|
class TestIncludeMapNoAbsolute:
|
|
|
|
|
"""Tests for the jw.include-map-no-absolute guard."""
|
|
|
|
|
|
|
|
|
|
def test_absolute_uncovered_raises(self, temp_dir: _TempDir) -> None:
|
|
|
|
|
"""An absolute spec with no matching entry raises."""
|
|
|
|
|
base = temp_dir(
|
|
|
|
|
[
|
|
|
|
|
(
|
|
|
|
|
'wrapper.yaml',
|
|
|
|
|
'jw.include-map: []\n'
|
|
|
|
|
'jw.include-map-no-absolute: true\n'
|
|
|
|
|
'jw.include: /etc/app.yaml\n'
|
|
|
|
|
),
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
with pytest.raises(ValueError, match = 'not covered by the include map'):
|
|
|
|
|
load_yaml(pathlib.Path(base) / 'wrapper.yaml')
|
|
|
|
|
|
|
|
|
|
def test_absolute_covered_ok(self, temp_dir: _TempDir) -> None:
|
|
|
|
|
"""An absolute spec covered by the map loads."""
|
|
|
|
|
base = temp_dir(
|
|
|
|
|
[
|
|
|
|
|
('app.yaml', 'ok: 1\n'),
|
|
|
|
|
(
|
|
|
|
|
'wrapper.yaml',
|
|
|
|
|
'jw.include-map:\n'
|
|
|
|
|
' - /etc/app.yaml: app.yaml\n'
|
|
|
|
|
'jw.include-map-no-absolute: true\n'
|
|
|
|
|
'jw.include: /etc/app.yaml\n'
|
|
|
|
|
),
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
data = load_yaml(pathlib.Path(base) / 'wrapper.yaml')
|
|
|
|
|
assert data['ok'] == 1
|
|
|
|
|
|
|
|
|
|
def test_nested_absolute_uncovered_raises(self, temp_dir: _TempDir) -> None:
|
|
|
|
|
"""The guard covers nested includes, not just the wrapper's own."""
|
|
|
|
|
base = temp_dir(
|
|
|
|
|
[
|
|
|
|
|
('prod.yaml', 'jw.include: /etc/app.yaml\n'),
|
|
|
|
|
(
|
|
|
|
|
'wrapper.yaml',
|
|
|
|
|
'jw.include-map: []\n'
|
|
|
|
|
'jw.include-map-no-absolute: true\n'
|
|
|
|
|
'jw.include: prod.yaml\n'
|
|
|
|
|
),
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
with pytest.raises(ValueError, match = 'not covered by the include map'):
|
|
|
|
|
load_yaml(pathlib.Path(base) / 'wrapper.yaml')
|
|
|
|
|
|
|
|
|
|
def test_relative_specs_pass(self, temp_dir: _TempDir) -> None:
|
|
|
|
|
"""Relative specs are unaffected by the guard."""
|
|
|
|
|
base = temp_dir(
|
|
|
|
|
[
|
|
|
|
|
('app.yaml', 'ok: 1\n'),
|
|
|
|
|
(
|
|
|
|
|
'wrapper.yaml',
|
|
|
|
|
'jw.include-map: []\n'
|
|
|
|
|
'jw.include-map-no-absolute: true\n'
|
|
|
|
|
'jw.include: app.yaml\n'
|
|
|
|
|
),
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
data = load_yaml(pathlib.Path(base) / 'wrapper.yaml')
|
|
|
|
|
assert data['ok'] == 1
|
|
|
|
|
|
|
|
|
|
def test_identity_entry_whitelists(self, temp_dir: _TempDir) -> None:
|
|
|
|
|
"""An identity entry whitelists an absolute path."""
|
|
|
|
|
base = temp_dir([('real.yaml', 'ok: 1\n')])
|
|
|
|
|
main = pathlib.Path(base) / 'wrapper.yaml'
|
|
|
|
|
main.write_text(
|
|
|
|
|
'jw.include-map:\n'
|
|
|
|
|
f' - {base}/real.yaml: {base}/real.yaml\n'
|
|
|
|
|
'jw.include-map-no-absolute: true\n'
|
|
|
|
|
f'jw.include: {base}/real.yaml\n'
|
|
|
|
|
)
|
|
|
|
|
data = load_yaml(main)
|
|
|
|
|
assert data['ok'] == 1
|
|
|
|
|
|
|
|
|
|
def test_default_is_false(self, temp_dir: _TempDir) -> None:
|
|
|
|
|
"""Without the flag, an uncovered absolute spec is read from disk."""
|
|
|
|
|
base = temp_dir(
|
|
|
|
|
[('wrapper.yaml', 'jw.include-map: []\n'
|
|
|
|
|
'jw.include: /etc/app.yaml\n')]
|
|
|
|
|
)
|
|
|
|
|
with pytest.raises(FileNotFoundError):
|
|
|
|
|
load_yaml(pathlib.Path(base) / 'wrapper.yaml')
|
|
|
|
|
|
|
|
|
|
def test_nested_flag_raises(self, temp_dir: _TempDir) -> None:
|
|
|
|
|
"""The flag in a non-top-level document raises."""
|
|
|
|
|
base = temp_dir(
|
|
|
|
|
[
|
|
|
|
|
('inner.yaml', 'jw.include-map-no-absolute: true\n'),
|
|
|
|
|
('wrapper.yaml', 'jw.include: inner.yaml\n'),
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
with pytest.raises(ValueError, match = 'top-level'):
|
|
|
|
|
load_yaml(pathlib.Path(base) / 'wrapper.yaml')
|
|
|
|
|
|
2026-07-08 15:37:06 +02:00
|
|
|
# -- jw.include with target path ------------------------------------------------
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
|
|
|
|
|
class TestPathResolution:
|
|
|
|
|
"""Tests for path resolution logic."""
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_absolute_single_key(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
data = load_yaml(yaml_file('key: val\nlink:\n jw.link: key\n'))
|
|
|
|
|
assert data['link'] == 'val'
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_absolute_multi_level(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
data = load_yaml(yaml_file('a:\n b:\n c: deep\nlink:\n jw.link: a.b.c\n'))
|
|
|
|
|
assert data['link'] == 'deep'
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_relative_from_list_index(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
"""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'
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_link_to_nested_value(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
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."""
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_string_preserved(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
data = load_yaml(yaml_file('target: "42"\nlink:\n jw.link: target\n'))
|
|
|
|
|
assert isinstance(data['link'], str)
|
|
|
|
|
assert data['link'] == '42'
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_int_preserved(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
data = load_yaml(yaml_file('target: 42\nlink:\n jw.link: target\n'))
|
|
|
|
|
assert isinstance(data['link'], int)
|
|
|
|
|
assert data['link'] == 42
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_float_preserved(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
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)
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_bool_preserved(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
data = load_yaml(yaml_file('target: true\nlink:\n jw.link: target\n'))
|
|
|
|
|
assert isinstance(data['link'], bool)
|
|
|
|
|
assert data['link'] is True
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_list_preserved(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
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]
|
|
|
|
|
|
2026-09-11 17:00:03 +02:00
|
|
|
def test_nested_types_preserved(self, yaml_file: _YamlFile) -> None:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
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
|