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>
This commit is contained in:
Jan Lindemann 2026-08-16 13:03:32 +02:00
commit d5da1599ad
Signed by: Jan Lindemann
GPG key ID: 3750640C9E25DD61

View file

@ -1016,6 +1016,115 @@ class TestIncludeWithPath:
assert data['plain_key'] == 'plain_val'
assert data['section']['target_key'] == 'target_val'
# -- jw.include globbing --------------------------------------------------------
class TestIncludeGlobbing:
"""Tests for glob support in jw.include file specs."""
def test_glob_multiple_files(self, temp_dir: Callable) -> None:
"""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
def test_glob_lexical_order(self, temp_dir: Callable) -> None:
"""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'
def test_glob_char_class(self, temp_dir: Callable) -> None:
"""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
def test_glob_question_mark(self, temp_dir: Callable) -> None:
"""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
def test_glob_no_match_raises(self, temp_dir: Callable) -> None:
"""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')
def test_glob_optional_no_match(self, temp_dir: Callable) -> None:
"""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 == {}
def test_glob_with_target_path(self, temp_dir: Callable) -> None:
"""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'
def test_glob_absolute_path(self, temp_dir: Callable) -> None:
"""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
def test_glob_self_include_raises(self, temp_dir: Callable) -> None:
"""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')
# -- jw.include with target path ------------------------------------------------
class TestPathResolution: