gabriel / muse public
test_plumbing_check_ref_format.py python
141 lines 5.4 KB
86000da9 fix: replace typer CliRunner with argparse-compatible test helper Gabriel Cardona <gabriel@tellurstori.com> 1d ago
1 """Tests for muse plumbing check-ref-format."""
2
3 from __future__ import annotations
4
5 import json
6 import pathlib
7
8 import pytest
9 from tests.cli_test_helper import CliRunner
10
11 cli = None # argparse migration — CliRunner ignores this arg
12
13 runner = CliRunner()
14
15
16 def _env(repo: pathlib.Path) -> dict[str, str]:
17 return {"MUSE_REPO_ROOT": str(repo)}
18
19
20 def _init_repo(repo: pathlib.Path) -> None:
21 muse = repo / ".muse"
22 (muse / "commits").mkdir(parents=True)
23 (muse / "snapshots").mkdir(parents=True)
24 (muse / "objects").mkdir(parents=True)
25 (muse / "refs" / "heads").mkdir(parents=True)
26 (muse / "HEAD").write_text("ref: refs/heads/main", encoding="utf-8")
27 (muse / "repo.json").write_text(
28 json.dumps({"repo_id": "test-repo", "domain": "midi"}), encoding="utf-8"
29 )
30
31
32 class TestCheckRefFormat:
33 def test_valid_simple_name(self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> None:
34 monkeypatch.chdir(tmp_path)
35 _init_repo(tmp_path)
36 result = runner.invoke(
37 cli, ["plumbing", "check-ref-format", "main"], env=_env(tmp_path)
38 )
39 assert result.exit_code == 0
40 data = json.loads(result.output)
41 assert data["all_valid"] is True
42 assert data["results"][0]["valid"] is True
43
44 def test_valid_namespaced_name(self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> None:
45 monkeypatch.chdir(tmp_path)
46 _init_repo(tmp_path)
47 result = runner.invoke(
48 cli, ["plumbing", "check-ref-format", "feat/my-branch"], env=_env(tmp_path)
49 )
50 assert result.exit_code == 0
51 data = json.loads(result.output)
52 assert data["all_valid"] is True
53
54 def test_consecutive_dots_invalid(self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> None:
55 monkeypatch.chdir(tmp_path)
56 _init_repo(tmp_path)
57 result = runner.invoke(
58 cli, ["plumbing", "check-ref-format", "bad..name"], env=_env(tmp_path)
59 )
60 assert result.exit_code != 0
61 data = json.loads(result.output)
62 assert data["all_valid"] is False
63 assert data["results"][0]["valid"] is False
64 assert data["results"][0]["error"] is not None
65
66 def test_leading_dot_invalid(self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> None:
67 monkeypatch.chdir(tmp_path)
68 _init_repo(tmp_path)
69 result = runner.invoke(
70 cli, ["plumbing", "check-ref-format", ".hidden"], env=_env(tmp_path)
71 )
72 assert result.exit_code != 0
73 data = json.loads(result.output)
74 assert data["all_valid"] is False
75
76 def test_leading_slash_invalid(self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> None:
77 monkeypatch.chdir(tmp_path)
78 _init_repo(tmp_path)
79 result = runner.invoke(
80 cli, ["plumbing", "check-ref-format", "/bad"], env=_env(tmp_path)
81 )
82 assert result.exit_code != 0
83
84 def test_multiple_names_mixed_validity(self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> None:
85 monkeypatch.chdir(tmp_path)
86 _init_repo(tmp_path)
87 result = runner.invoke(
88 cli, ["plumbing", "check-ref-format", "good-name", "bad..name"], env=_env(tmp_path)
89 )
90 assert result.exit_code != 0
91 data = json.loads(result.output)
92 assert data["all_valid"] is False
93 valid_map = {r["name"]: r["valid"] for r in data["results"]}
94 assert valid_map["good-name"] is True
95 assert valid_map["bad..name"] is False
96
97 def test_all_valid_exits_zero(self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> None:
98 monkeypatch.chdir(tmp_path)
99 _init_repo(tmp_path)
100 result = runner.invoke(
101 cli, ["plumbing", "check-ref-format", "feat/a", "fix/b", "dev"], env=_env(tmp_path)
102 )
103 assert result.exit_code == 0
104 data = json.loads(result.output)
105 assert data["all_valid"] is True
106
107 def test_quiet_mode_exit_zero(self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> None:
108 monkeypatch.chdir(tmp_path)
109 _init_repo(tmp_path)
110 result = runner.invoke(
111 cli, ["plumbing", "check-ref-format", "--quiet", "main"], env=_env(tmp_path)
112 )
113 assert result.exit_code == 0
114 assert result.output.strip() == ""
115
116 def test_quiet_mode_exit_one_on_invalid(self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> None:
117 monkeypatch.chdir(tmp_path)
118 _init_repo(tmp_path)
119 result = runner.invoke(
120 cli, ["plumbing", "check-ref-format", "-q", "bad..name"], env=_env(tmp_path)
121 )
122 assert result.exit_code != 0
123 assert result.output.strip() == ""
124
125 def test_text_format(self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> None:
126 monkeypatch.chdir(tmp_path)
127 _init_repo(tmp_path)
128 result = runner.invoke(
129 cli,
130 ["plumbing", "check-ref-format", "--format", "text", "good", "bad..name"],
131 env=_env(tmp_path),
132 )
133 assert result.exit_code != 0
134 assert "ok" in result.output
135 assert "FAIL" in result.output
136
137 def test_no_args_errors(self, tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> None:
138 monkeypatch.chdir(tmp_path)
139 _init_repo(tmp_path)
140 result = runner.invoke(cli, ["plumbing", "check-ref-format"], env=_env(tmp_path))
141 assert result.exit_code != 0