|
| 1 | +""" |
| 2 | +Tests for Cursor .mdc frontmatter generation (issue #669). |
| 3 | +
|
| 4 | +Verifies that update-agent-context.sh properly prepends YAML frontmatter |
| 5 | +to .mdc files so that Cursor IDE auto-includes the rules. |
| 6 | +""" |
| 7 | + |
| 8 | +import os |
| 9 | +import shutil |
| 10 | +import subprocess |
| 11 | +import textwrap |
| 12 | + |
| 13 | +import pytest |
| 14 | + |
| 15 | +SCRIPT_PATH = os.path.join( |
| 16 | + os.path.dirname(__file__), |
| 17 | + os.pardir, |
| 18 | + "scripts", |
| 19 | + "bash", |
| 20 | + "update-agent-context.sh", |
| 21 | +) |
| 22 | + |
| 23 | +EXPECTED_FRONTMATTER_LINES = [ |
| 24 | + "---", |
| 25 | + "description: Project Development Guidelines", |
| 26 | + 'globs: ["**/*"]', |
| 27 | + "alwaysApply: true", |
| 28 | + "---", |
| 29 | +] |
| 30 | + |
| 31 | +requires_git = pytest.mark.skipif( |
| 32 | + shutil.which("git") is None, |
| 33 | + reason="git is not installed", |
| 34 | +) |
| 35 | + |
| 36 | + |
| 37 | +class TestScriptFrontmatterPattern: |
| 38 | + """Static analysis — no git required.""" |
| 39 | + |
| 40 | + def test_create_new_has_mdc_frontmatter_logic(self): |
| 41 | + """create_new_agent_file() must contain .mdc frontmatter logic.""" |
| 42 | + with open(SCRIPT_PATH, encoding="utf-8") as f: |
| 43 | + content = f.read() |
| 44 | + assert 'if [[ "$target_file" == *.mdc ]]' in content |
| 45 | + assert "alwaysApply: true" in content |
| 46 | + |
| 47 | + def test_update_existing_has_mdc_frontmatter_logic(self): |
| 48 | + """update_existing_agent_file() must also handle .mdc frontmatter.""" |
| 49 | + with open(SCRIPT_PATH, encoding="utf-8") as f: |
| 50 | + content = f.read() |
| 51 | + # There should be two occurrences of the .mdc check — one per function |
| 52 | + occurrences = content.count('if [[ "$target_file" == *.mdc ]]') |
| 53 | + assert occurrences >= 2, ( |
| 54 | + f"Expected at least 2 .mdc frontmatter checks, found {occurrences}" |
| 55 | + ) |
| 56 | + |
| 57 | + def test_powershell_script_has_mdc_frontmatter_logic(self): |
| 58 | + """PowerShell script must also handle .mdc frontmatter.""" |
| 59 | + ps_path = os.path.join( |
| 60 | + os.path.dirname(__file__), |
| 61 | + os.pardir, |
| 62 | + "scripts", |
| 63 | + "powershell", |
| 64 | + "update-agent-context.ps1", |
| 65 | + ) |
| 66 | + with open(ps_path, encoding="utf-8") as f: |
| 67 | + content = f.read() |
| 68 | + assert "alwaysApply: true" in content |
| 69 | + occurrences = content.count(r"\.mdc$") |
| 70 | + assert occurrences >= 2, ( |
| 71 | + f"Expected at least 2 .mdc frontmatter checks in PS script, found {occurrences}" |
| 72 | + ) |
| 73 | + |
| 74 | + |
| 75 | +@requires_git |
| 76 | +class TestCursorFrontmatterIntegration: |
| 77 | + """Integration tests using a real git repo.""" |
| 78 | + |
| 79 | + @pytest.fixture |
| 80 | + def git_repo(self, tmp_path): |
| 81 | + """Create a minimal git repo with the spec-kit structure.""" |
| 82 | + repo = tmp_path / "repo" |
| 83 | + repo.mkdir() |
| 84 | + |
| 85 | + # Init git repo |
| 86 | + subprocess.run( |
| 87 | + ["git", "init"], cwd=str(repo), capture_output=True, check=True |
| 88 | + ) |
| 89 | + subprocess.run( |
| 90 | + ["git", "config", "user.email", "test@test.com"], |
| 91 | + cwd=str(repo), |
| 92 | + capture_output=True, |
| 93 | + check=True, |
| 94 | + ) |
| 95 | + subprocess.run( |
| 96 | + ["git", "config", "user.name", "Test"], |
| 97 | + cwd=str(repo), |
| 98 | + capture_output=True, |
| 99 | + check=True, |
| 100 | + ) |
| 101 | + |
| 102 | + # Create .specify dir with config |
| 103 | + specify_dir = repo / ".specify" |
| 104 | + specify_dir.mkdir() |
| 105 | + (specify_dir / "config.yaml").write_text( |
| 106 | + textwrap.dedent("""\ |
| 107 | + project_type: webapp |
| 108 | + language: python |
| 109 | + framework: fastapi |
| 110 | + database: N/A |
| 111 | + """) |
| 112 | + ) |
| 113 | + |
| 114 | + # Create template |
| 115 | + templates_dir = specify_dir / "templates" |
| 116 | + templates_dir.mkdir() |
| 117 | + (templates_dir / "agent-file-template.md").write_text( |
| 118 | + "# [PROJECT NAME] Development Guidelines\n\n" |
| 119 | + "Auto-generated from all feature plans. Last updated: [DATE]\n\n" |
| 120 | + "## Active Technologies\n\n" |
| 121 | + "[EXTRACTED FROM ALL PLAN.MD FILES]\n\n" |
| 122 | + "## Project Structure\n\n" |
| 123 | + "[ACTUAL STRUCTURE FROM PLANS]\n\n" |
| 124 | + "## Development Commands\n\n" |
| 125 | + "[ONLY COMMANDS FOR ACTIVE TECHNOLOGIES]\n\n" |
| 126 | + "## Coding Conventions\n\n" |
| 127 | + "[LANGUAGE-SPECIFIC, ONLY FOR LANGUAGES IN USE]\n\n" |
| 128 | + "## Recent Changes\n\n" |
| 129 | + "[LAST 3 FEATURES AND WHAT THEY ADDED]\n" |
| 130 | + ) |
| 131 | + |
| 132 | + # Create initial commit |
| 133 | + subprocess.run( |
| 134 | + ["git", "add", "-A"], cwd=str(repo), capture_output=True, check=True |
| 135 | + ) |
| 136 | + subprocess.run( |
| 137 | + ["git", "commit", "-m", "init"], |
| 138 | + cwd=str(repo), |
| 139 | + capture_output=True, |
| 140 | + check=True, |
| 141 | + ) |
| 142 | + |
| 143 | + # Create a feature branch so CURRENT_BRANCH detection works |
| 144 | + subprocess.run( |
| 145 | + ["git", "checkout", "-b", "001-test-feature"], |
| 146 | + cwd=str(repo), |
| 147 | + capture_output=True, |
| 148 | + check=True, |
| 149 | + ) |
| 150 | + |
| 151 | + # Create a spec so the script detects the feature |
| 152 | + spec_dir = repo / "specs" / "001-test-feature" |
| 153 | + spec_dir.mkdir(parents=True) |
| 154 | + (spec_dir / "plan.md").write_text( |
| 155 | + "# Test Feature Plan\n\n" |
| 156 | + "## Technology Stack\n\n" |
| 157 | + "- Language: Python\n" |
| 158 | + "- Framework: FastAPI\n" |
| 159 | + ) |
| 160 | + |
| 161 | + return repo |
| 162 | + |
| 163 | + def _run_update(self, repo, agent_type="cursor-agent"): |
| 164 | + """Run update-agent-context.sh for a specific agent type.""" |
| 165 | + script = os.path.abspath(SCRIPT_PATH) |
| 166 | + result = subprocess.run( |
| 167 | + ["bash", script, agent_type], |
| 168 | + cwd=str(repo), |
| 169 | + capture_output=True, |
| 170 | + text=True, |
| 171 | + timeout=30, |
| 172 | + ) |
| 173 | + return result |
| 174 | + |
| 175 | + def test_new_mdc_file_has_frontmatter(self, git_repo): |
| 176 | + """Creating a new .mdc file must include YAML frontmatter.""" |
| 177 | + result = self._run_update(git_repo) |
| 178 | + assert result.returncode == 0, f"Script failed: {result.stderr}" |
| 179 | + |
| 180 | + mdc_file = git_repo / ".cursor" / "rules" / "specify-rules.mdc" |
| 181 | + assert mdc_file.exists(), "Cursor .mdc file was not created" |
| 182 | + |
| 183 | + content = mdc_file.read_text() |
| 184 | + lines = content.splitlines() |
| 185 | + |
| 186 | + # First line must be the opening --- |
| 187 | + assert lines[0] == "---", f"Expected frontmatter start, got: {lines[0]}" |
| 188 | + |
| 189 | + # Check all frontmatter lines are present |
| 190 | + for expected in EXPECTED_FRONTMATTER_LINES: |
| 191 | + assert expected in content, f"Missing frontmatter line: {expected}" |
| 192 | + |
| 193 | + # Content after frontmatter should be the template content |
| 194 | + assert "Development Guidelines" in content |
| 195 | + |
| 196 | + def test_existing_mdc_without_frontmatter_gets_it_added(self, git_repo): |
| 197 | + """Updating an existing .mdc file that lacks frontmatter must add it.""" |
| 198 | + # First, create the file WITHOUT frontmatter (simulating pre-fix state) |
| 199 | + cursor_dir = git_repo / ".cursor" / "rules" |
| 200 | + cursor_dir.mkdir(parents=True, exist_ok=True) |
| 201 | + mdc_file = cursor_dir / "specify-rules.mdc" |
| 202 | + mdc_file.write_text( |
| 203 | + "# repo Development Guidelines\n\n" |
| 204 | + "Auto-generated from all feature plans. Last updated: 2025-01-01\n\n" |
| 205 | + "## Active Technologies\n\n" |
| 206 | + "- Python + FastAPI (main)\n\n" |
| 207 | + "## Recent Changes\n\n" |
| 208 | + "- main: Added Python + FastAPI\n" |
| 209 | + ) |
| 210 | + |
| 211 | + result = self._run_update(git_repo) |
| 212 | + assert result.returncode == 0, f"Script failed: {result.stderr}" |
| 213 | + |
| 214 | + content = mdc_file.read_text() |
| 215 | + lines = content.splitlines() |
| 216 | + |
| 217 | + assert lines[0] == "---", f"Expected frontmatter start, got: {lines[0]}" |
| 218 | + for expected in EXPECTED_FRONTMATTER_LINES: |
| 219 | + assert expected in content, f"Missing frontmatter line: {expected}" |
| 220 | + |
| 221 | + def test_existing_mdc_with_frontmatter_not_duplicated(self, git_repo): |
| 222 | + """Updating an .mdc file that already has frontmatter must not duplicate it.""" |
| 223 | + cursor_dir = git_repo / ".cursor" / "rules" |
| 224 | + cursor_dir.mkdir(parents=True, exist_ok=True) |
| 225 | + mdc_file = cursor_dir / "specify-rules.mdc" |
| 226 | + |
| 227 | + frontmatter = ( |
| 228 | + "---\n" |
| 229 | + "description: Project Development Guidelines\n" |
| 230 | + 'globs: ["**/*"]\n' |
| 231 | + "alwaysApply: true\n" |
| 232 | + "---\n\n" |
| 233 | + ) |
| 234 | + body = ( |
| 235 | + "# repo Development Guidelines\n\n" |
| 236 | + "Auto-generated from all feature plans. Last updated: 2025-01-01\n\n" |
| 237 | + "## Active Technologies\n\n" |
| 238 | + "- Python + FastAPI (main)\n\n" |
| 239 | + "## Recent Changes\n\n" |
| 240 | + "- main: Added Python + FastAPI\n" |
| 241 | + ) |
| 242 | + mdc_file.write_text(frontmatter + body) |
| 243 | + |
| 244 | + result = self._run_update(git_repo) |
| 245 | + assert result.returncode == 0, f"Script failed: {result.stderr}" |
| 246 | + |
| 247 | + content = mdc_file.read_text() |
| 248 | + # Count occurrences of the frontmatter delimiter |
| 249 | + assert content.count("alwaysApply: true") == 1, ( |
| 250 | + "Frontmatter was duplicated" |
| 251 | + ) |
| 252 | + |
| 253 | + def test_non_mdc_file_has_no_frontmatter(self, git_repo): |
| 254 | + """Non-.mdc agent files (e.g., Claude) must NOT get frontmatter.""" |
| 255 | + result = self._run_update(git_repo, agent_type="claude") |
| 256 | + assert result.returncode == 0, f"Script failed: {result.stderr}" |
| 257 | + |
| 258 | + claude_file = git_repo / ".claude" / "CLAUDE.md" |
| 259 | + if claude_file.exists(): |
| 260 | + content = claude_file.read_text() |
| 261 | + assert not content.startswith("---"), ( |
| 262 | + "Non-mdc file should not have frontmatter" |
| 263 | + ) |
0 commit comments