gabriel / muse public
test_cli_coverage_gaps.py python
288 lines 11.2 KB
Raw
1 """Tests targeting specific coverage gaps in checkout, tag, commit, diff, stash, branch."""
2
3 import pathlib
4
5 import pytest
6 from tests.cli_test_helper import CliRunner
7
8 cli = None # argparse migration — CliRunner ignores this arg
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 # muse init creates .museignore / .museattributes which are tracked by default.
134 # Commit that initial state, then verify a second commit on a clean tree
135 # reports nothing to commit.
136 runner.invoke(cli, ["commit", "-m", "init files"])
137 result = runner.invoke(cli, ["commit", "-m", "nothing here"])
138 assert result.exit_code == 0
139 assert "Nothing to commit" in result.output or "nothing" in result.output.lower()
140
141 def test_empty_workdir_without_allow_empty_errors(self, repo: pathlib.Path) -> None:
142 # Same as above: commit init-created dotfiles first, then verify clean tree.
143 runner.invoke(cli, ["commit", "-m", "init files"])
144 result = runner.invoke(cli, ["commit", "-m", "empty"])
145 assert result.exit_code == 0
146 assert "Nothing to commit" in result.output or "nothing" in result.output.lower()
147
148 def test_nothing_to_commit_clean_tree(self, repo: pathlib.Path) -> None:
149 _write(repo, "beat.mid")
150 runner.invoke(cli, ["commit", "-m", "first"])
151 result = runner.invoke(cli, ["commit", "-m", "second"])
152 assert result.exit_code == 0
153 assert "Nothing to commit" in result.output or "clean" in result.output
154
155 def test_commit_with_pending_conflicts_errors(self, repo: pathlib.Path) -> None:
156 _write(repo, "beat.mid", "base")
157 runner.invoke(cli, ["commit", "-m", "base"])
158
159 runner.invoke(cli, ["branch", "feature"])
160 runner.invoke(cli, ["checkout", "feature"])
161 _write(repo, "beat.mid", "feature-v")
162 runner.invoke(cli, ["commit", "-m", "feature changes"])
163
164 runner.invoke(cli, ["checkout", "main"])
165 _write(repo, "beat.mid", "main-v")
166 runner.invoke(cli, ["commit", "-m", "main changes"])
167
168 runner.invoke(cli, ["merge", "feature"])
169
170 # Now try to commit — should fail because of unresolved conflicts
171 _write(repo, "new.mid")
172 result = runner.invoke(cli, ["commit", "-m", "during conflict"])
173 assert result.exit_code != 0
174 assert "conflict" in result.output.lower()
175
176
177 # ---------------------------------------------------------------------------
178 # diff gaps
179 # ---------------------------------------------------------------------------
180
181
182 class TestDiffGaps:
183 def test_diff_two_commits(self, repo: pathlib.Path) -> None:
184 _write(repo, "a.mid", "v1")
185 runner.invoke(cli, ["commit", "-m", "first"])
186 first_id = get_head_commit_id(repo, "main")
187
188 _write(repo, "a.mid", "v2")
189 runner.invoke(cli, ["commit", "-m", "second"])
190 second_id = get_head_commit_id(repo, "main")
191
192 assert first_id is not None
193 assert second_id is not None
194 result = runner.invoke(cli, ["diff", first_id, second_id])
195 assert result.exit_code == 0
196 assert "a.mid" in result.output
197
198 def test_diff_commit_vs_head(self, repo: pathlib.Path) -> None:
199 _write(repo, "a.mid", "v1")
200 runner.invoke(cli, ["commit", "-m", "first"])
201 first_id = get_head_commit_id(repo, "main")
202
203 _write(repo, "a.mid", "v2")
204 runner.invoke(cli, ["commit", "-m", "second"])
205 assert first_id is not None
206
207 result = runner.invoke(cli, ["diff", first_id])
208 assert result.exit_code == 0
209 assert "a.mid" in result.output
210
211 def test_diff_stat_flag(self, repo: pathlib.Path) -> None:
212 _write(repo, "a.mid")
213 runner.invoke(cli, ["commit", "-m", "base"])
214 _write(repo, "b.mid")
215 result = runner.invoke(cli, ["diff", "--stat"])
216 assert result.exit_code == 0
217 # --stat prints the structured delta summary line.
218 assert "added" in result.output or "No differences" in result.output
219
220
221 # ---------------------------------------------------------------------------
222 # stash gaps
223 # ---------------------------------------------------------------------------
224
225
226 class TestStashGaps:
227 def test_stash_pop_restores_files(self, repo: pathlib.Path) -> None:
228 _write(repo, "beat.mid")
229 runner.invoke(cli, ["commit", "-m", "base"])
230 _write(repo, "unsaved.mid", "wip")
231
232 runner.invoke(cli, ["stash"])
233 assert not (repo / "unsaved.mid").exists()
234
235 runner.invoke(cli, ["stash", "pop"])
236 assert (repo / "unsaved.mid").exists()
237
238 def test_stash_drop_removes_entry(self, repo: pathlib.Path) -> None:
239 _write(repo, "beat.mid")
240 runner.invoke(cli, ["commit", "-m", "base"])
241 _write(repo, "unsaved.mid", "wip")
242
243 runner.invoke(cli, ["stash"])
244 result = runner.invoke(cli, ["stash", "drop"])
245 assert result.exit_code == 0
246
247 result = runner.invoke(cli, ["stash", "list"])
248 assert "No stash entries" in result.output
249
250 def test_stash_pop_empty_errors(self, repo: pathlib.Path) -> None:
251 result = runner.invoke(cli, ["stash", "pop"])
252 assert result.exit_code != 0
253 assert "No stash" in result.output
254
255 def test_stash_drop_empty_errors(self, repo: pathlib.Path) -> None:
256 result = runner.invoke(cli, ["stash", "drop"])
257 assert result.exit_code != 0
258
259
260
261 # ---------------------------------------------------------------------------
262 # branch gaps
263 # ---------------------------------------------------------------------------
264
265
266 class TestBranchGaps:
267 def test_delete_branch_with_d_flag(self, repo: pathlib.Path) -> None:
268 runner.invoke(cli, ["branch", "to-delete"])
269 result = runner.invoke(cli, ["branch", "-D", "to-delete"])
270 assert result.exit_code == 0
271
272 def test_delete_current_branch_errors(self, repo: pathlib.Path) -> None:
273 result = runner.invoke(cli, ["branch", "-d", "main"])
274 assert result.exit_code != 0
275 assert "current" in result.output.lower() or "main" in result.output
276
277 def test_delete_nonexistent_branch_errors(self, repo: pathlib.Path) -> None:
278 result = runner.invoke(cli, ["branch", "-d", "no-such-branch"])
279 assert result.exit_code != 0
280
281 def test_create_branch_with_b_flag(self, repo: pathlib.Path) -> None:
282 result = runner.invoke(cli, ["branch", "new-feature"])
283 assert result.exit_code == 0
284
285 def test_branch_with_slash_shown_in_list(self, repo: pathlib.Path) -> None:
286 runner.invoke(cli, ["branch", "feature/my-thing"])
287 result = runner.invoke(cli, ["branch"])
288 assert "feature/my-thing" in result.output
File History 1 commit