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
7 changes: 5 additions & 2 deletions src/specify_cli/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@ def parse_frontmatter(content: str) -> tuple[dict, str]:
Returns:
Tuple of (frontmatter_dict, body_content)
"""
if not content.startswith("---"):
# A UTF-8 BOM before the opening delimiter is an encoding marker, not
# content. Inputs without frontmatter are still returned unchanged.
text = content.removeprefix("\ufeff")
if not text.startswith("---"):
return {}, content

# The closing delimiter is a line that is exactly ``---`` (a YAML
Expand All @@ -113,7 +116,7 @@ def parse_frontmatter(content: str) -> tuple[dict, str]:
# which truncates the frontmatter and spills the remainder into the
# body. Match on line boundaries instead, mirroring the line-anchored
# scan in ``VibeIntegration._inject_frontmatter_flag``.
lines = content.splitlines(keepends=True)
lines = text.splitlines(keepends=True)
end_line = next(
(i for i in range(1, len(lines)) if lines[i].rstrip() == "---"),
None,
Expand Down
92 changes: 92 additions & 0 deletions tests/test_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3467,6 +3467,60 @@ def test_parse_frontmatter_no_frontmatter(self):
assert frontmatter == {}
assert body == content

@pytest.mark.parametrize(
("content", "expected_frontmatter", "expected_body"),
[
(
"\ufeff---\ndescription: Test command\n---\n\n# Command body\n",
{"description": "Test command"},
"# Command body",
),
(
"\ufeff---\r\ndescription: Test command\r\n---\r\n\r\n# Command body\r\n",
{"description": "Test command"},
"# Command body",
),
(
"\ufeff# Just a command\n$ARGUMENTS",
{},
"\ufeff# Just a command\n$ARGUMENTS",
),
(
"\ufeff\ufeff---\ndescription: Test command\n---\n\n# Command body\n",
{},
"\ufeff\ufeff---\ndescription: Test command\n---\n\n# Command body\n",
),
(
"\ufeff---\ndescription: Test command\n",
{},
"\ufeff---\ndescription: Test command\n",
),
(
"# Command body\n\ufeffKeep this marker",
{},
"# Command body\n\ufeffKeep this marker",
),
],
ids=(
"lf",
"crlf",
"bom-without-frontmatter",
"double-bom",
"bom-unterminated-frontmatter",
"non-leading-bom",
),
)
def test_parse_frontmatter_handles_leading_bom(
self, content, expected_frontmatter, expected_body
):
"""Strip one leading BOM only when YAML frontmatter follows it."""
registrar = CommandRegistrar()

frontmatter, body = registrar.parse_frontmatter(content)

assert frontmatter == expected_frontmatter
assert body == expected_body

def test_parse_frontmatter_non_mapping_returns_empty_dict(self):
"""Non-mapping YAML frontmatter should not crash downstream renderers."""
content = """---
Expand Down Expand Up @@ -3948,6 +4002,44 @@ def test_register_commands_for_claude(self, extension_dir, project_dir):
assert "description: Test hello command" in content
assert "test-ext" in content

def test_register_commands_handles_bom_prefixed_frontmatter(
self, extension_dir, project_dir
):
"""A BOM-prefixed command keeps its metadata when registered."""
from specify_cli.agents import CommandRegistrar as AgentCommandRegistrar

source_file = extension_dir / "commands" / "hello.md"
source_file.write_text(
"\ufeff---\ndescription: BOM command\n---\n\n# Command body\n",
encoding="utf-8",
)
manifest = ExtensionManifest(extension_dir / "extension.yml")
registrar = AgentCommandRegistrar()

registered = registrar.register_commands(
"claude",
manifest.commands,
manifest.id,
extension_dir,
project_dir,
extension_id=manifest.id,
author=manifest.data["extension"].get("author"),
version=manifest.version,
)

assert registered == ["speckit.test-ext.hello"]
output_file = (
project_dir
/ ".claude"
/ "skills"
/ "speckit-test-ext-hello"
/ "SKILL.md"
)
output = output_file.read_text(encoding="utf-8")
rendered_frontmatter, _ = registrar.parse_frontmatter(output)
assert rendered_frontmatter["description"] == "BOM command"
assert "\ufeff" not in output

def test_command_with_aliases(self, project_dir, temp_dir):
"""Test registering a command with aliases."""
import yaml
Expand Down
Loading