-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_check_tests_present.py
More file actions
219 lines (166 loc) · 6.76 KB
/
test_check_tests_present.py
File metadata and controls
219 lines (166 loc) · 6.76 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
"""Tests for `.github/scripts/check_tests_present.py`."""
from __future__ import annotations
import importlib.util
import json
import sys
from pathlib import Path
from typing import TYPE_CHECKING, Any
import pytest
if TYPE_CHECKING:
from collections.abc import Iterable
REPO_ROOT = Path(__file__).resolve().parent.parent
SCRIPT_PATH = REPO_ROOT / ".github" / "scripts" / "check_tests_present.py"
def _load_script() -> Any:
spec = importlib.util.spec_from_file_location("check_tests_present", SCRIPT_PATH)
if spec is None or spec.loader is None:
msg = f"Could not load script at {SCRIPT_PATH}"
raise RuntimeError(msg)
module = importlib.util.module_from_spec(spec)
sys.modules[spec.name] = module
spec.loader.exec_module(module)
return module
ctp = _load_script()
# ---------- commit_type_prefix ----------
@pytest.mark.parametrize(
"title,expected",
[
("feat: add tool", "feat"),
("fix: handle empty list", "fix"),
("chore(api): rename module", "chore"),
("docs: update readme", "docs"),
("test: add coverage", "test"),
("refactor: split module", "refactor"),
("release: v1.2.3", "release"),
("feat!: breaking change", "feat"),
("FEAT: case-insensitive", "feat"),
(" feat: leading-spaces", "feat"),
("no-prefix subject only", None),
("", None),
(None, None),
],
)
def test_commit_type_prefix(title: str | None, expected: str | None) -> None:
assert ctp.commit_type_prefix(title) == expected
# ---------- changed_files (via stub) ----------
def test_changed_files_filters_blank_lines(monkeypatch: pytest.MonkeyPatch) -> None:
"""Empty lines in git output are filtered out."""
def fake_check_output(*_: Any, **__: Any) -> str:
return "src/api/main.py\n\ntests/test_api.py\n"
monkeypatch.setattr(ctp.subprocess, "check_output", fake_check_output)
assert ctp.changed_files("develop") == ["src/api/main.py", "tests/test_api.py"]
def test_changed_files_propagates_git_failure(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""A git error becomes a RuntimeError with a context-rich message."""
def fake_check_output(*_: Any, **__: Any) -> str:
raise ctp.subprocess.CalledProcessError(
128, ["git"], stderr="bad object origin/develop"
)
monkeypatch.setattr(ctp.subprocess, "check_output", fake_check_output)
with pytest.raises(RuntimeError, match="bad object origin/develop"):
ctp.changed_files("develop")
# ---------- main() end-to-end ----------
def _set_event_payload(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path, title: str
) -> None:
event_file = tmp_path / "event.json"
event_file.write_text(
json.dumps({"pull_request": {"title": title}}), encoding="utf-8"
)
monkeypatch.setenv("GITHUB_EVENT_PATH", str(event_file))
def _stub_changed_files(monkeypatch: pytest.MonkeyPatch, files: Iterable[str]) -> None:
monkeypatch.setattr(ctp, "changed_files", lambda _base_ref: list(files))
def test_main_no_src_changes_passes(
monkeypatch: pytest.MonkeyPatch,
tmp_path: Path,
capsys: pytest.CaptureFixture[str],
) -> None:
monkeypatch.setenv("GITHUB_BASE_REF", "develop")
_set_event_payload(monkeypatch, tmp_path, "feat: docs-only change")
_stub_changed_files(monkeypatch, ["docs/README.md", ".github/workflows/ci.yml"])
assert ctp.main() == 0
assert "gate not applicable" in capsys.readouterr().out
def test_main_src_and_tests_changed_passes(
monkeypatch: pytest.MonkeyPatch,
tmp_path: Path,
capsys: pytest.CaptureFixture[str],
) -> None:
monkeypatch.setenv("GITHUB_BASE_REF", "develop")
_set_event_payload(monkeypatch, tmp_path, "feat: new tool")
_stub_changed_files(monkeypatch, ["src/tools/new.py", "tests/test_new.py"])
assert ctp.main() == 0
out = capsys.readouterr().out
assert "tests/ also touched" in out
@pytest.mark.parametrize("prefix_title", ["feat: x", "fix: y", "FIX: z"])
def test_main_feat_or_fix_without_tests_blocks(
prefix_title: str,
monkeypatch: pytest.MonkeyPatch,
tmp_path: Path,
capsys: pytest.CaptureFixture[str],
) -> None:
monkeypatch.setenv("GITHUB_BASE_REF", "develop")
_set_event_payload(monkeypatch, tmp_path, prefix_title)
_stub_changed_files(monkeypatch, ["src/api/routes.py"])
assert ctp.main() == 1
err = capsys.readouterr().out
assert "::error::" in err
assert "Testing Policy" in err
@pytest.mark.parametrize(
"prefix_title", ["chore: x", "docs: y", "refactor: z", "test: a", "release: b"]
)
def test_main_warn_only_prefixes_pass(
prefix_title: str,
monkeypatch: pytest.MonkeyPatch,
tmp_path: Path,
capsys: pytest.CaptureFixture[str],
) -> None:
monkeypatch.setenv("GITHUB_BASE_REF", "develop")
_set_event_payload(monkeypatch, tmp_path, prefix_title)
_stub_changed_files(monkeypatch, ["src/api/routes.py"])
assert ctp.main() == 0
out = capsys.readouterr().out
assert "::warning::" in out
def test_main_unknown_prefix_warns(
monkeypatch: pytest.MonkeyPatch,
tmp_path: Path,
capsys: pytest.CaptureFixture[str],
) -> None:
"""No conventional prefix → warn-only (Lint PR title is the prefix gate)."""
monkeypatch.setenv("GITHUB_BASE_REF", "develop")
_set_event_payload(monkeypatch, tmp_path, "no prefix at all")
_stub_changed_files(monkeypatch, ["src/api/routes.py"])
assert ctp.main() == 0
out = capsys.readouterr().out
assert "prefix not recognised" in out
def test_main_excludes_non_python_src_changes(
monkeypatch: pytest.MonkeyPatch,
tmp_path: Path,
capsys: pytest.CaptureFixture[str],
) -> None:
"""Non-Python files under src/ shouldn't trigger the gate."""
monkeypatch.setenv("GITHUB_BASE_REF", "develop")
_set_event_payload(monkeypatch, tmp_path, "feat: copy a fixture")
_stub_changed_files(monkeypatch, ["src/api/README.md", "src/data/sample.csv"])
assert ctp.main() == 0
assert "gate not applicable" in capsys.readouterr().out
def test_main_no_base_ref_skips(
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
monkeypatch.delenv("GITHUB_BASE_REF", raising=False)
monkeypatch.delenv("GITHUB_EVENT_PATH", raising=False)
assert ctp.main() == 0
assert "skipping" in capsys.readouterr().out
def test_main_git_failure_exits_2(
monkeypatch: pytest.MonkeyPatch,
tmp_path: Path,
capsys: pytest.CaptureFixture[str],
) -> None:
monkeypatch.setenv("GITHUB_BASE_REF", "develop")
_set_event_payload(monkeypatch, tmp_path, "feat: x")
def boom(_base_ref: str) -> list[str]:
msg = "git fetch failed"
raise RuntimeError(msg)
monkeypatch.setattr(ctp, "changed_files", boom)
assert ctp.main() == 2
assert "::error::" in capsys.readouterr().out