cgcardona / muse public
test_cli_coverage_gaps.py python
286 lines 11.0 KB
59a915a4 refactor: repo root is the working tree — remove state/ subdirectory Gabriel Cardona <gabriel@tellurstori.com> 6h ago
1 """Tests targeting specific coverage gaps in checkout, tag, commit, diff, stash, branch."""
2
3 import pathlib
4
5 import pytest
6 from typer.testing import CliRunner
7
8 from muse.cli.app import cli
9 from muse.core.store import get_head_commit_id
10
11 runner = CliRunner()
12
13
14 @pytest.fixture
15 def repo(tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> pathlib.Path:
16 monkeypatch.chdir(tmp_path)
17 monkeypatch.setenv("MUSE_REPO_ROOT", str(tmp_path))
18 result = runner.invoke(cli, ["init"])
19 assert result.exit_code == 0, result.output
20 return tmp_path
21
22
23 def _write(repo: pathlib.Path, filename: str, content: str = "data") -> None:
24 (repo / filename).write_text(content)
25
26
27 def _commit(msg: str = "commit") -> str | None:
28 result = runner.invoke(cli, ["commit", "-m", msg])
29 assert result.exit_code == 0, result.output
30 return get_head_commit_id(pathlib.Path("."), "main")
31
32
33 # ---------------------------------------------------------------------------
34 # checkout gaps
35 # ---------------------------------------------------------------------------
36
37
38 class TestCheckoutGaps:
39 def test_create_branch_already_exists_errors(self, repo: pathlib.Path) -> None:
40 result = runner.invoke(cli, ["checkout", "-b", "main"])
41 assert result.exit_code != 0
42 assert "already exists" in result.output
43
44 def test_unknown_ref_errors(self, repo: pathlib.Path) -> None:
45 result = runner.invoke(cli, ["checkout", "no-such-branch-or-commit"])
46 assert result.exit_code != 0
47 assert "not a branch" in result.output.lower() or "not found" in result.output.lower() or "no-such" in result.output
48
49 def test_checkout_by_commit_id_detaches_head(self, repo: pathlib.Path) -> None:
50 _write(repo, "beat.mid")
51 result = runner.invoke(cli, ["commit", "-m", "root"])
52 assert result.exit_code == 0
53 commit_id = get_head_commit_id(repo, "main")
54 assert commit_id is not None
55
56 _write(repo, "lead.mid")
57 runner.invoke(cli, ["commit", "-m", "second"])
58
59 result = runner.invoke(cli, ["checkout", commit_id])
60 assert result.exit_code == 0
61 assert "HEAD is now at" in result.output
62
63 def test_checkout_restores_workdir_to_target_snapshot(self, repo: pathlib.Path) -> None:
64 _write(repo, "beat.mid", "v1")
65 result = runner.invoke(cli, ["commit", "-m", "first"])
66 assert result.exit_code == 0
67 first_id = get_head_commit_id(repo, "main")
68 assert first_id is not None
69
70 _write(repo, "lead.mid", "new")
71 runner.invoke(cli, ["commit", "-m", "second"])
72
73 runner.invoke(cli, ["checkout", first_id])
74 assert (repo / "beat.mid").exists()
75 assert not (repo / "lead.mid").exists()
76
77
78 # ---------------------------------------------------------------------------
79 # tag gaps
80 # ---------------------------------------------------------------------------
81
82
83 class TestTagGaps:
84 def test_tag_add_unknown_ref_errors(self, repo: pathlib.Path) -> None:
85 _write(repo, "beat.mid")
86 runner.invoke(cli, ["commit", "-m", "base"])
87 result = runner.invoke(cli, ["tag", "add", "emotion:joyful", "deadbeef"])
88 assert result.exit_code != 0
89
90 def test_tag_list_for_specific_commit(self, repo: pathlib.Path) -> None:
91 _write(repo, "beat.mid")
92 runner.invoke(cli, ["commit", "-m", "base"])
93 commit_id = get_head_commit_id(repo, "main")
94 assert commit_id is not None
95
96 runner.invoke(cli, ["tag", "add", "emotion:joyful", commit_id[:8]])
97 result = runner.invoke(cli, ["tag", "list", commit_id[:8]])
98 assert result.exit_code == 0
99 assert "joyful" in result.output
100
101 def test_tag_list_for_unknown_ref_errors(self, repo: pathlib.Path) -> None:
102 _write(repo, "beat.mid")
103 runner.invoke(cli, ["commit", "-m", "base"])
104 result = runner.invoke(cli, ["tag", "list", "deadbeef"])
105 assert result.exit_code != 0
106
107 def test_tag_list_all_shows_all_tags(self, repo: pathlib.Path) -> None:
108 _write(repo, "beat.mid")
109 runner.invoke(cli, ["commit", "-m", "base"])
110 commit_id = get_head_commit_id(repo, "main")
111 assert commit_id is not None
112
113 runner.invoke(cli, ["tag", "add", "section:verse", commit_id[:8]])
114 runner.invoke(cli, ["tag", "add", "emotion:joyful", commit_id[:8]])
115 result = runner.invoke(cli, ["tag", "list"])
116 assert result.exit_code == 0
117 assert "section:verse" in result.output
118 assert "emotion:joyful" in result.output
119
120
121 # ---------------------------------------------------------------------------
122 # commit gaps
123 # ---------------------------------------------------------------------------
124
125
126 class TestCommitGaps:
127 def test_no_message_without_allow_empty_errors(self, repo: pathlib.Path) -> None:
128 _write(repo, "beat.mid")
129 result = runner.invoke(cli, ["commit"])
130 assert result.exit_code != 0
131
132 def test_empty_repo_without_allow_empty_errors(self, repo: pathlib.Path) -> None:
133 # Working tree IS the repo root — always exists. An empty tree without
134 # --allow-empty should exit non-zero (nothing tracked to commit).
135 result = runner.invoke(cli, ["commit", "-m", "nothing here"])
136 assert result.exit_code != 0
137
138 def test_empty_workdir_without_allow_empty_errors(self, repo: pathlib.Path) -> None:
139 result = runner.invoke(cli, ["commit", "-m", "empty"])
140 assert result.exit_code != 0
141
142 def test_nothing_to_commit_clean_tree(self, repo: pathlib.Path) -> None:
143 _write(repo, "beat.mid")
144 runner.invoke(cli, ["commit", "-m", "first"])
145 result = runner.invoke(cli, ["commit", "-m", "second"])
146 assert result.exit_code == 0
147 assert "Nothing to commit" in result.output or "clean" in result.output
148
149 def test_commit_with_pending_conflicts_errors(self, repo: pathlib.Path) -> None:
150 _write(repo, "beat.mid", "base")
151 runner.invoke(cli, ["commit", "-m", "base"])
152
153 runner.invoke(cli, ["branch", "feature"])
154 runner.invoke(cli, ["checkout", "feature"])
155 _write(repo, "beat.mid", "feature-v")
156 runner.invoke(cli, ["commit", "-m", "feature changes"])
157
158 runner.invoke(cli, ["checkout", "main"])
159 _write(repo, "beat.mid", "main-v")
160 runner.invoke(cli, ["commit", "-m", "main changes"])
161
162 runner.invoke(cli, ["merge", "feature"])
163
164 # Now try to commit — should fail because of unresolved conflicts
165 _write(repo, "new.mid")
166 result = runner.invoke(cli, ["commit", "-m", "during conflict"])
167 assert result.exit_code != 0
168 assert "conflict" in result.output.lower()
169
170
171 # ---------------------------------------------------------------------------
172 # diff gaps
173 # ---------------------------------------------------------------------------
174
175
176 class TestDiffGaps:
177 def test_diff_two_commits(self, repo: pathlib.Path) -> None:
178 _write(repo, "a.mid", "v1")
179 runner.invoke(cli, ["commit", "-m", "first"])
180 first_id = get_head_commit_id(repo, "main")
181
182 _write(repo, "a.mid", "v2")
183 runner.invoke(cli, ["commit", "-m", "second"])
184 second_id = get_head_commit_id(repo, "main")
185
186 assert first_id is not None
187 assert second_id is not None
188 result = runner.invoke(cli, ["diff", first_id, second_id])
189 assert result.exit_code == 0
190 assert "a.mid" in result.output
191
192 def test_diff_commit_vs_head(self, repo: pathlib.Path) -> None:
193 _write(repo, "a.mid", "v1")
194 runner.invoke(cli, ["commit", "-m", "first"])
195 first_id = get_head_commit_id(repo, "main")
196
197 _write(repo, "a.mid", "v2")
198 runner.invoke(cli, ["commit", "-m", "second"])
199 assert first_id is not None
200
201 result = runner.invoke(cli, ["diff", first_id])
202 assert result.exit_code == 0
203 assert "a.mid" in result.output
204
205 def test_diff_stat_flag(self, repo: pathlib.Path) -> None:
206 _write(repo, "a.mid")
207 runner.invoke(cli, ["commit", "-m", "base"])
208 _write(repo, "b.mid")
209 result = runner.invoke(cli, ["diff", "--stat"])
210 assert result.exit_code == 0
211 # --stat prints the structured delta summary line.
212 assert "added" in result.output or "No differences" in result.output
213
214
215 # ---------------------------------------------------------------------------
216 # stash gaps
217 # ---------------------------------------------------------------------------
218
219
220 class TestStashGaps:
221 def test_stash_pop_restores_files(self, repo: pathlib.Path) -> None:
222 _write(repo, "beat.mid")
223 runner.invoke(cli, ["commit", "-m", "base"])
224 _write(repo, "unsaved.mid", "wip")
225
226 runner.invoke(cli, ["stash"])
227 assert not (repo / "unsaved.mid").exists()
228
229 runner.invoke(cli, ["stash", "pop"])
230 assert (repo / "unsaved.mid").exists()
231
232 def test_stash_drop_removes_entry(self, repo: pathlib.Path) -> None:
233 _write(repo, "beat.mid")
234 runner.invoke(cli, ["commit", "-m", "base"])
235 _write(repo, "unsaved.mid", "wip")
236
237 runner.invoke(cli, ["stash"])
238 result = runner.invoke(cli, ["stash", "drop"])
239 assert result.exit_code == 0
240
241 result = runner.invoke(cli, ["stash", "list"])
242 assert "No stash entries" in result.output
243
244 def test_stash_pop_empty_errors(self, repo: pathlib.Path) -> None:
245 result = runner.invoke(cli, ["stash", "pop"])
246 assert result.exit_code != 0
247 assert "No stash" in result.output
248
249 def test_stash_drop_empty_errors(self, repo: pathlib.Path) -> None:
250 result = runner.invoke(cli, ["stash", "drop"])
251 assert result.exit_code != 0
252
253 def test_stash_nothing_to_stash(self, repo: pathlib.Path) -> None:
254 result = runner.invoke(cli, ["stash"])
255 assert result.exit_code == 0
256 assert "Nothing to stash" in result.output
257
258
259 # ---------------------------------------------------------------------------
260 # branch gaps
261 # ---------------------------------------------------------------------------
262
263
264 class TestBranchGaps:
265 def test_delete_branch_with_d_flag(self, repo: pathlib.Path) -> None:
266 runner.invoke(cli, ["branch", "to-delete"])
267 result = runner.invoke(cli, ["branch", "-d", "to-delete"])
268 assert result.exit_code == 0
269
270 def test_delete_current_branch_errors(self, repo: pathlib.Path) -> None:
271 result = runner.invoke(cli, ["branch", "-d", "main"])
272 assert result.exit_code != 0
273 assert "current" in result.output.lower() or "main" in result.output
274
275 def test_delete_nonexistent_branch_errors(self, repo: pathlib.Path) -> None:
276 result = runner.invoke(cli, ["branch", "-d", "no-such-branch"])
277 assert result.exit_code != 0
278
279 def test_create_branch_with_b_flag(self, repo: pathlib.Path) -> None:
280 result = runner.invoke(cli, ["branch", "new-feature"])
281 assert result.exit_code == 0
282
283 def test_branch_with_slash_shown_in_list(self, repo: pathlib.Path) -> None:
284 runner.invoke(cli, ["branch", "feature/my-thing"])
285 result = runner.invoke(cli, ["branch"])
286 assert "feature/my-thing" in result.output