Skip to content

Commit 6e5ba27

Browse files
fix: address Copilot review feedback on Claude integration
- Remove unused `import re` - Skip injection if argument-hint already exists in frontmatter - Add found_description assertion to test_hint_appears_after_description - Add test_inject_argument_hint_skips_if_already_present test Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent ffa6946 commit 6e5ba27

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

src/specify_cli/integrations/claude/__init__.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from __future__ import annotations
44

5-
import re
65
from pathlib import Path
76
from typing import Any, TYPE_CHECKING
87

@@ -45,8 +44,25 @@ class ClaudeIntegration(MarkdownIntegration):
4544

4645
@staticmethod
4746
def inject_argument_hint(content: str, hint: str) -> str:
48-
"""Insert ``argument-hint`` after the first ``description:`` in YAML frontmatter."""
47+
"""Insert ``argument-hint`` after the first ``description:`` in YAML frontmatter.
48+
49+
Skips injection if ``argument-hint:`` already exists in the
50+
frontmatter to avoid duplicate keys.
51+
"""
4952
lines = content.splitlines(keepends=True)
53+
54+
# Pre-scan: bail out if argument-hint already present in frontmatter
55+
dash_count = 0
56+
for line in lines:
57+
stripped = line.rstrip("\n\r")
58+
if stripped == "---":
59+
dash_count += 1
60+
if dash_count == 2:
61+
break
62+
continue
63+
if dash_count == 1 and stripped.startswith("argument-hint:"):
64+
return content # already present
65+
5066
out: list[str] = []
5167
in_fm = False
5268
dash_count = 0

tests/integrations/test_integration_claude.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,20 @@ def test_hint_appears_after_description(self, tmp_path):
7777
for f in cmd_files:
7878
content = f.read_text(encoding="utf-8")
7979
lines = content.splitlines()
80+
found_description = False
8081
for idx, line in enumerate(lines):
8182
if line.startswith("description:"):
83+
found_description = True
8284
assert idx + 1 < len(lines), (
8385
f"{f.name}: description is last line"
8486
)
8587
assert lines[idx + 1].startswith("argument-hint:"), (
8688
f"{f.name}: argument-hint does not follow description"
8789
)
8890
break
91+
assert found_description, (
92+
f"{f.name}: no description: line found in output"
93+
)
8994

9095
def test_inject_argument_hint_only_in_frontmatter(self):
9196
"""inject_argument_hint must not modify description: lines in the body."""
@@ -100,7 +105,25 @@ def test_inject_argument_hint_only_in_frontmatter(self):
100105
)
101106
result = ClaudeIntegration.inject_argument_hint(content, "Test hint")
102107
lines = result.splitlines()
103-
hint_count = sum(1 for l in lines if l.startswith("argument-hint:"))
108+
hint_count = sum(1 for ln in lines if ln.startswith("argument-hint:"))
104109
assert hint_count == 1, (
105110
f"Expected exactly 1 argument-hint line, found {hint_count}"
106111
)
112+
113+
def test_inject_argument_hint_skips_if_already_present(self):
114+
"""inject_argument_hint must not duplicate if argument-hint already exists."""
115+
from specify_cli.integrations.claude import ClaudeIntegration
116+
117+
content = (
118+
"---\n"
119+
"description: My command\n"
120+
"argument-hint: Existing hint\n"
121+
"---\n"
122+
"\n"
123+
"Body text\n"
124+
)
125+
result = ClaudeIntegration.inject_argument_hint(content, "New hint")
126+
assert result == content, "Content should be unchanged when hint already exists"
127+
lines = result.splitlines()
128+
hint_count = sum(1 for ln in lines if ln.startswith("argument-hint:"))
129+
assert hint_count == 1

0 commit comments

Comments
 (0)