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: 3 additions & 2 deletions src/specify_cli/presets/_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ def _find_in_subdirs(base_dir: Path) -> Optional[Path]:
try:
cmd_content = candidate.read_text(encoding="utf-8")
lines = cmd_content.splitlines(keepends=True)
if lines and lines[0].rstrip("\r\n") == "---":
if lines and lines[0].removeprefix("\ufeff").rstrip("\r\n") == "---":
fence_end = -1
for fi, fline in enumerate(lines[1:], start=1):
if fline.rstrip("\r\n") == "---":
Expand Down Expand Up @@ -883,7 +883,8 @@ def _split_frontmatter(text: str) -> tuple:
Uses line-based fence detection (fence must be ``---`` on its
own line) to avoid false matches on ``---`` inside YAML values.
"""
lines = text.splitlines(keepends=True)
frontmatter_text = text.removeprefix("\ufeff")
lines = frontmatter_text.splitlines(keepends=True)
if not lines or lines[0].rstrip("\r\n") != "---":
return None, text

Expand Down
132 changes: 131 additions & 1 deletion tests/specify_cli/presets/test_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
PresetResolver,
PresetValidationError,
)
from specify_cli.presets._manifest import PresetSectionValidationError
from tests.specify_cli.presets._helpers import (
CORE_TEMPLATE_NAMES,
install_self_test_preset,
Expand Down Expand Up @@ -1670,6 +1671,135 @@ def test_resolve_content_command_frontmatter_stripping(self, project_dir, temp_d
# Core frontmatter should NOT appear in the body
assert content.count("---") == 2 # only one frontmatter block (opening + closing)

@pytest.mark.parametrize("newline", ["\n", "\r\n"], ids=["lf", "crlf"])
def test_resolve_content_legacy_bom_frontmatter_strategy(
self, project_dir, temp_dir, valid_pack_data, newline
):
"""A frontmatter strategy after a UTF-8 BOM is still honoured."""
commands_dir = project_dir / ".specify" / "templates" / "commands"
commands_dir.mkdir(parents=True, exist_ok=True)
(commands_dir / "bom-legacy.md").write_text(
"Core body content\n", encoding="utf-8"
)
preset_content = (
"\ufeff---\ndescription: BOM preset\nstrategy: append\n---\n"
"Appended body content\n"
).replace("\n", newline)
pack_dir = _create_pack(
temp_dir,
valid_pack_data,
"bom-legacy-pack",
preset_content,
Comment thread
Copilot marked this conversation as resolved.
Resolved
template_type="command",
template_name="speckit.bom-legacy",
)
PresetManager(project_dir).install_from_directory(pack_dir, "0.1.5")

resolver = PresetResolver(project_dir)
layers = resolver.collect_all_layers("speckit.bom-legacy", "command")
content = resolver.resolve_content("speckit.bom-legacy", "command")

assert layers[0]["strategy"] == "append"
assert content is not None
assert "Core body content" in content
assert "Appended body content" in content

def test_resolve_content_strips_bom_frontmatter_from_composed_command(
self, project_dir, temp_dir, valid_pack_data
):
"""A BOM-prefixed layer's frontmatter must not leak into the body."""
commands_dir = project_dir / ".specify" / "templates" / "commands"
commands_dir.mkdir(parents=True, exist_ok=True)
(commands_dir / "bom-compose.md").write_text(
"---\ndescription: Core command\n---\nCore body content\n",
encoding="utf-8",
)
pack_dir = _create_pack(
temp_dir,
valid_pack_data,
"bom-compose-pack",
"\ufeff---\ndescription: BOM preset\nstrategy: append\n---\n"
"Appended body content\n",
strategy="append",
template_type="command",
template_name="speckit.bom-compose",
)
PresetManager(project_dir).install_from_directory(pack_dir, "0.1.5")

content = PresetResolver(project_dir).resolve_content(
"speckit.bom-compose", "command"
)

assert content is not None
assert "\ufeff" not in content
assert "strategy: append" not in content
assert "Appended body content" in content
frontmatter = yaml.safe_load(content.split("---", 2)[1])
assert frontmatter == {"description": "BOM preset"}

@pytest.mark.parametrize(
"preset_content",
[
"\ufeffPlain appended content\n",
"\ufeff---\ndescription: Unterminated frontmatter\nAppended body content\n",
],
ids=["no-frontmatter", "unterminated-frontmatter"],
)
def test_resolve_content_keeps_bom_without_complete_frontmatter(
self, project_dir, temp_dir, valid_pack_data, preset_content
):
"""Without complete frontmatter the layer is appended unchanged."""
commands_dir = project_dir / ".specify" / "templates" / "commands"
commands_dir.mkdir(parents=True, exist_ok=True)
core_content = "Core body content\n"
(commands_dir / "bom-guard.md").write_text(
core_content, encoding="utf-8"
)
pack_dir = _create_pack(
temp_dir,
valid_pack_data,
"bom-guard-pack",
preset_content,
strategy="append",
template_type="command",
template_name="speckit.bom-guard",
)
PresetManager(project_dir).install_from_directory(pack_dir, "0.1.5")

content = PresetResolver(project_dir).resolve_content(
"speckit.bom-guard", "command"
)

# No complete frontmatter means content stays unchanged, matching #5562.
assert content == core_content + "\n\n" + preset_content

@pytest.mark.parametrize("prefix", ["", "\ufeff"], ids=["plain", "bom"])
def test_legacy_frontmatter_replace_section_requires_manifest_declaration(
self, project_dir, valid_pack_data, prefix
):
"""replace-section in command frontmatter is rejected, BOM or not."""
presets_dir = project_dir / ".specify" / "presets"
presets_dir.mkdir(parents=True)
_create_pack(
presets_dir,
valid_pack_data,
"replace-section-pack",
prefix + "---\nstrategy: replace-section\n---\nReplacement body\n",
template_type="command",
template_name="speckit.replace-section",
)
PresetRegistry(presets_dir).add(
"replace-section-pack", {"version": "1.0.0", "priority": 10}
)

with pytest.raises(
PresetSectionValidationError,
match="Declare replace-section and selector",
):
PresetResolver(project_dir).collect_all_layers(
"speckit.replace-section", "command"
)

def test_resolve_content_blank_line_separator(self, project_dir, temp_dir, valid_pack_data):
"""Test that prepend/append use blank line separator."""
pack_data = {**valid_pack_data}
Expand Down Expand Up @@ -1952,7 +2082,7 @@ def _create_pack(temp_dir, valid_pack_data, pack_id, content,
elif template_type == "command":
subdir = pack_dir / "commands"
subdir.mkdir(exist_ok=True)
(subdir / f"{template_name}.md").write_text(content)
(subdir / f"{template_name}.md").write_text(content, encoding="utf-8")
else:
subdir = pack_dir / "templates"
subdir.mkdir(exist_ok=True)
Expand Down
Loading