load_yaml: Expand variables in top-level include specs
Test wrappers that mimic production invocations redirect the includes of
production documents to tree-local copies; the target location depends on
where the tree is checked out, so it would be nice if the wrapper could
take TOPDIR from the environment instead of hardcoding it.
Allow the file part of a top-level include spec to reference environment
variables as $(VAR) or ${VAR}. _load_yaml() receives a depth counter,
zero at the top level and one per include level, and threads it to
_normalize_include_specs() and _parse_include_spec(); the file part passes
through _expand_include_vars() only at depth zero, after the optional
marker and the target path have been split off. Specs in included documents
keep their literal behaviour. An undefined variable raises ValueError,
naming the reference and the file it came from.
Signed-off-by: Jan Lindemann <jan@janware.com>
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v1.6.0
This commit is contained in:
parent
3d4e49f047
commit
911c78ff99
1 changed files with 113 additions and 11 deletions
|
|
@ -1153,6 +1153,66 @@ class TestIncludeGlobbing:
|
|||
with pytest.raises(ValueError, match = 'Cyclic YAML include'):
|
||||
load_yaml(pathlib.Path(base) / 'main.yaml')
|
||||
|
||||
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
|
||||
|
||||
# -- jw.include with target path ------------------------------------------------
|
||||
|
||||
class TestPathResolution:
|
||||
|
|
|
|||
Loading…
Reference in a new issue