-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cli.py
More file actions
148 lines (116 loc) · 4.37 KB
/
test_cli.py
File metadata and controls
148 lines (116 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import json
from pathlib import Path
import pytest
from scripts.drift_check import cli
from tests.conftest import FIXTURES
@pytest.fixture
def meta_repo(tmp_path: Path) -> Path:
"""Isolated meta-repo for CLI tests. Pins meta VERSION to 1.6.3 so that
the on-disk fixtures (which still carry 1.6.3 signals) read as clean."""
root = tmp_path / "meta"
root.mkdir()
(root / "VERSION").write_text("1.6.3", encoding="utf-8")
(root / "standards").mkdir()
(root / "standards" / "required-refs.json").write_text(
'{"version": 1, "requirements": {"cursor-plugin": {}, "mcp-server": {}}}',
encoding="utf-8",
)
return root
def _meta_args(meta_repo: Path) -> list[str]:
return ["--meta-repo", str(meta_repo), "--config", "nonexistent.json"]
def test_missing_local_returns_2(capsys):
rc = cli.main([])
assert rc == 2
err = capsys.readouterr().err
assert "--local" in err and "--remote" in err
def test_bad_path_returns_2(capsys, tmp_path: Path, meta_repo: Path):
rc = cli.main(["--local", str(tmp_path / "nope"), *_meta_args(meta_repo)])
assert rc == 2
assert "not a directory" in capsys.readouterr().err
def test_clean_repo_exit_zero(capsys, meta_repo: Path):
rc = cli.main(["--local", str(FIXTURES / "clean_repo"), *_meta_args(meta_repo)])
out = capsys.readouterr().out
assert rc == 0
assert "0 errors, 0 warnings" in out
def test_drifted_repo_exit_one(capsys, meta_repo: Path):
rc = cli.main(["--local", str(FIXTURES / "drifted_repo"), *_meta_args(meta_repo)])
out = capsys.readouterr().out
assert rc == 1
assert "Summary:" in out
def test_broken_repo_exit_one(capsys, meta_repo: Path):
rc = cli.main(["--local", str(FIXTURES / "broken_repo"), *_meta_args(meta_repo)])
assert rc == 1
def test_ignored_repo_exit_zero_infos_suppressed(capsys, meta_repo: Path):
rc = cli.main(["--local", str(FIXTURES / "ignored_repo"), *_meta_args(meta_repo)])
assert rc == 0
def test_json_format(capsys, meta_repo: Path):
rc = cli.main([
"--local", str(FIXTURES / "drifted_repo"),
"--format", "json",
*_meta_args(meta_repo),
])
assert rc == 1
payload = json.loads(capsys.readouterr().out)
assert "summary" in payload
assert "meta_version" in payload
def test_output_to_file(tmp_path: Path, meta_repo: Path):
out = tmp_path / "report.md"
rc = cli.main([
"--local", str(FIXTURES / "clean_repo"),
"--output", str(out),
*_meta_args(meta_repo),
])
assert rc == 0
text = out.read_text(encoding="utf-8")
assert "Meta-repo version:" in text
def test_malformed_config_returns_2(tmp_path: Path, capsys, meta_repo: Path):
bad = tmp_path / "bad.json"
bad.write_text("{not json", encoding="utf-8")
rc = cli.main([
"--local", str(FIXTURES / "clean_repo"),
"--meta-repo", str(meta_repo),
"--config", str(bad),
])
assert rc == 2
assert "malformed JSON" in capsys.readouterr().err
def test_multiple_local_paths(capsys, meta_repo: Path):
rc = cli.main([
"--local", str(FIXTURES / "clean_repo"),
"--local", str(FIXTURES / "drifted_repo"),
*_meta_args(meta_repo),
])
assert rc == 1
out = capsys.readouterr().out
assert "clean_repo" in out
assert "drifted_repo" in out
def test_verbose_flag_shows_infos(capsys, meta_repo: Path):
rc = cli.main([
"--local", str(FIXTURES / "drifted_repo"),
"--verbose",
*_meta_args(meta_repo),
])
assert rc == 1
out = capsys.readouterr().out
assert "infos" in out
def test_gh_summary_requires_env(capsys, meta_repo: Path, monkeypatch):
monkeypatch.delenv("GITHUB_STEP_SUMMARY", raising=False)
rc = cli.main([
"--local", str(FIXTURES / "clean_repo"),
"--format", "gh-summary",
*_meta_args(meta_repo),
])
assert rc == 2
assert "GITHUB_STEP_SUMMARY" in capsys.readouterr().err
def test_gh_summary_writes_file(tmp_path: Path, meta_repo: Path, monkeypatch):
target = tmp_path / "summary.md"
monkeypatch.setenv("GITHUB_STEP_SUMMARY", str(target))
rc = cli.main([
"--local", str(FIXTURES / "clean_repo"),
"--format", "gh-summary",
*_meta_args(meta_repo),
])
assert rc == 0
assert target.is_file()
text = target.read_text(encoding="utf-8")
assert "Drift report" in text
assert "clean" in text.lower()