-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_check_commit_types.py
More file actions
253 lines (217 loc) · 9.33 KB
/
test_check_commit_types.py
File metadata and controls
253 lines (217 loc) · 9.33 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
"""Tests for ``.github/scripts/check_commit_types.py``.
The script runs as the ``Commit-type sync`` CI job and guards against drift
between commitizen's ``schema_pattern`` and pr-title.yml's ``types`` list.
These tests cover the two improvements landed in #91 and #92:
- #91: the alternation regex accepts digits and hyphens in type names
(forward-proof against future types like ``rc2`` or ``post-release``).
- #92: both extractors raise on empty input rather than returning
``set()``, which would silently trip the ``cz == pr`` check when both
configs are broken.
Tests live under ``tests/`` so they run in the regular pytest suite and
contribute to coverage. The script is loaded via ``importlib`` because
``.github/scripts`` isn't on the Python path.
"""
from __future__ import annotations
import importlib.util
from pathlib import Path
from typing import TYPE_CHECKING
from unittest.mock import patch
import pytest
if TYPE_CHECKING:
from types import ModuleType
def _load_script() -> ModuleType:
"""Load the standalone script as an importable module."""
script_path = (
Path(__file__).parent.parent / ".github" / "scripts" / "check_commit_types.py"
)
spec = importlib.util.spec_from_file_location("check_commit_types", script_path)
assert spec is not None, f"Could not create spec for {script_path}"
assert spec.loader is not None
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
cct = _load_script()
# ---------------------------------------------------------------------------
# #91 — regex widening
# ---------------------------------------------------------------------------
class TestSchemaAlternationRegex:
"""The widened character class accepts digits and hyphens in type names."""
@pytest.mark.parametrize(
"schema,expected",
[
# Current production schema — must still work after widening.
(
r"^(feat|fix|docs|test|refactor|chore|release)(\([\w\-]+\))?!?:\s.+",
{"feat", "fix", "docs", "test", "refactor", "chore", "release"},
),
# Types with digits (e.g. future release-candidate prefix).
(
r"^(feat|rc2|v2hotfix)(\([\w\-]+\))?!?:\s.+",
{"feat", "rc2", "v2hotfix"},
),
# Types with hyphens (compound prefixes).
(
r"^(fix|hot-fix|post-release)(\([\w\-]+\))?!?:\s.+",
{"fix", "hot-fix", "post-release"},
),
# Mixed: digits and hyphens together.
(
r"^(feat|rc2|post-release|hot-fix-v3)(\([\w\-]+\))?!?:\s.+",
{"feat", "rc2", "post-release", "hot-fix-v3"},
),
# Single type — edge of the alternation range.
(
r"^(feat)(\([\w\-]+\))?!?:\s.+",
{"feat"},
),
],
)
def test_extraction(self, schema: str, expected: set[str]) -> None:
match = cct._SCHEMA_ALTERNATION_RE.search(schema)
assert match is not None, f"regex failed to match: {schema!r}"
actual = {t for t in match.group(1).split("|") if t}
assert actual == expected
def test_rejects_uppercase_types(self) -> None:
# The regex only matches lowercase + digit + hyphen. An uppercase
# type in the schema would fall through; we'd never mistake it for
# a valid alternation. (Commitizen schemas use lowercase by
# convention.)
schema = r"^(FEAT|FIX)(\([\w\-]+\))?!?:\s.+"
match = cct._SCHEMA_ALTERNATION_RE.search(schema)
assert match is None
# ---------------------------------------------------------------------------
# #92 — empty-set guards
# ---------------------------------------------------------------------------
class TestCommitizenTypesEmptyGuard:
"""commitizen_types() must raise, not return an empty set."""
def test_raises_when_schema_pattern_missing(
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
pyproject = tmp_path / "pyproject.toml"
pyproject.write_text(
'[tool.commitizen.customize]\nexample = "no schema_pattern here"\n',
encoding="utf-8",
)
monkeypatch.setattr(cct, "PYPROJECT", pyproject)
with pytest.raises(ValueError, match="schema_pattern not found"):
cct.commitizen_types()
def test_raises_when_schema_pattern_unparseable(
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
pyproject = tmp_path / "pyproject.toml"
pyproject.write_text(
'[tool.commitizen.customize]\nschema_pattern = "no group here"\n',
encoding="utf-8",
)
monkeypatch.setattr(cct, "PYPROJECT", pyproject)
with pytest.raises(ValueError, match="Could not extract"):
cct.commitizen_types()
def test_raises_when_alternation_group_empty(
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
# `^(|)` — a malformed pattern whose group matches the regex but
# produces only empty strings after split+filter. #92 guards this.
pyproject = tmp_path / "pyproject.toml"
pyproject.write_text(
"[tool.commitizen.customize]\n"
'schema_pattern = "^(|)(\\\\(...\\\\))?!?:\\\\s.+"\n',
encoding="utf-8",
)
monkeypatch.setattr(cct, "PYPROJECT", pyproject)
with pytest.raises(ValueError, match="Empty type alternation"):
cct.commitizen_types()
class TestPrTitleTypesEmptyGuard:
"""pr_title_types() must raise, not return an empty set."""
def test_raises_when_action_step_missing(
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
workflow = tmp_path / "pr-title.yml"
workflow.write_text(
"name: PR title\njobs:\n foo:\n steps:\n - run: echo hi\n",
encoding="utf-8",
)
monkeypatch.setattr(cct, "PR_TITLE_YML", workflow)
with pytest.raises(ValueError, match="Could not find"):
cct.pr_title_types()
def test_raises_when_types_block_empty(
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
workflow = tmp_path / "pr-title.yml"
workflow.write_text(
"name: PR title\n"
"jobs:\n"
" lint:\n"
" steps:\n"
" - uses: amannn/action-semantic-pull-request@abc123\n"
" with:\n"
' types: ""\n',
encoding="utf-8",
)
monkeypatch.setattr(cct, "PR_TITLE_YML", workflow)
with pytest.raises(ValueError, match="empty or whitespace-only"):
cct.pr_title_types()
def test_raises_when_types_block_whitespace_only(
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
workflow = tmp_path / "pr-title.yml"
workflow.write_text(
"name: PR title\n"
"jobs:\n"
" lint:\n"
" steps:\n"
" - uses: amannn/action-semantic-pull-request@abc123\n"
" with:\n"
" types: |\n"
" \n"
" \n",
encoding="utf-8",
)
monkeypatch.setattr(cct, "PR_TITLE_YML", workflow)
with pytest.raises(ValueError, match="empty or whitespace-only"):
cct.pr_title_types()
def test_parses_valid_types_block(
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
# Positive path: confirm the happy case still works after the guard.
workflow = tmp_path / "pr-title.yml"
workflow.write_text(
"name: PR title\n"
"jobs:\n"
" lint:\n"
" steps:\n"
" - uses: amannn/action-semantic-pull-request@abc123\n"
" with:\n"
" types: |\n"
" feat\n"
" fix\n"
" chore\n",
encoding="utf-8",
)
monkeypatch.setattr(cct, "PR_TITLE_YML", workflow)
assert cct.pr_title_types() == {"feat", "fix", "chore"}
# ---------------------------------------------------------------------------
# main() belt-and-braces safety net (#92)
# ---------------------------------------------------------------------------
class TestMainEmptySafetyNet:
"""Even if a future refactor strips the raises, main() catches empties."""
def test_exits_nonzero_when_commitizen_returns_empty(
self, capsys: pytest.CaptureFixture[str]
) -> None:
with (
patch.object(cct, "commitizen_types", return_value=set()),
patch.object(cct, "pr_title_types", return_value={"feat"}),
):
assert cct.main() == 1
captured = capsys.readouterr()
assert "cannot proceed" in captured.out
assert "commitizen_types() empty: True" in captured.out
def test_exits_nonzero_when_pr_title_returns_empty(
self, capsys: pytest.CaptureFixture[str]
) -> None:
with (
patch.object(cct, "commitizen_types", return_value={"feat"}),
patch.object(cct, "pr_title_types", return_value=set()),
):
assert cct.main() == 1
captured = capsys.readouterr()
assert "pr_title_types() empty: True" in captured.out