Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/specify_cli/presets/_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -916,9 +916,12 @@ def _parse_fm_yaml(fm_block: str) -> dict:
else:
yaml_lines = []
try:
return yaml.safe_load("\n".join(yaml_lines)) or {}
parsed = yaml.safe_load("\n".join(yaml_lines)) or {}
except yaml.YAMLError:
return {}
# Valid YAML that is not a mapping is treated like invalid
# frontmatter, matching CommandRegistrar.parse_frontmatter.
return parsed if isinstance(parsed, dict) else {}

top_fm = _parse_fm_yaml(top_frontmatter_text)

Expand Down
47 changes: 47 additions & 0 deletions tests/specify_cli/presets/test_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,53 @@ def test_resolve_content_nonexistent(self, project_dir):
content = resolver.resolve_content("nonexistent")
assert content is None

def test_resolve_content_non_mapping_command_frontmatter(
self, project_dir, temp_dir, valid_pack_data
):
"""Valid YAML that is not a mapping must not crash composition.

``_parse_fm_yaml`` returned whatever ``yaml.safe_load`` produced, and the
caller then called ``.pop`` on it, so ``preset add`` surfaced a raw
``AttributeError``. ``CommandRegistrar.parse_frontmatter`` already treats
a non-dict result as ``{}``; the resolver now agrees.
"""
pack_data = {**valid_pack_data}
pack_data["preset"] = {
**valid_pack_data["preset"],
"id": "odd-frontmatter-pack",
"name": "Odd Frontmatter",
}
pack_data["provides"] = {
"templates": [
{
"type": "command",
"name": "speckit.demo",
"file": "commands/speckit.demo.md",
"strategy": "append",
}
]
}
pack_dir = temp_dir / "odd-frontmatter-pack"
pack_dir.mkdir()
with open(pack_dir / "preset.yml", "w") as f:
yaml.dump(pack_data, f)
(pack_dir / "commands").mkdir()
(pack_dir / "commands" / "speckit.demo.md").write_text(
"---\njust a note\n---\nAPPENDED\n", encoding="utf-8"
)
core_command = project_dir / ".specify" / "templates" / "commands" / "speckit.demo.md"
core_command.parent.mkdir(parents=True, exist_ok=True)
core_command.write_text("---\ndescription: core\n---\nCORE BODY\n", encoding="utf-8")

PresetManager(project_dir).install_from_directory(pack_dir, "0.1.5")

content = PresetResolver(project_dir).resolve_content("speckit.demo", "command")

assert content is not None
assert "CORE BODY" in content
assert "APPENDED" in content
assert "just a note" not in content

def test_resolve_content_unreadable_winning_layer_returns_none(self, project_dir):
"""An undecodable winning layer must yield None, not a raw traceback.

Expand Down
Loading