-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_check_version_bump.py
More file actions
241 lines (185 loc) · 7.69 KB
/
Copy pathtest_check_version_bump.py
File metadata and controls
241 lines (185 loc) · 7.69 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
"""Tests for `.github/scripts/check_version_bump.py`.
Covers the parsing helpers and end-to-end `main()` behaviour by stubbing
the `git show` subprocess call and the GitHub Actions event payload.
"""
from __future__ import annotations
import importlib.util
import json
import os
import sys
from pathlib import Path
from typing import TYPE_CHECKING, Any
import pytest
if TYPE_CHECKING:
from collections.abc import Iterator
REPO_ROOT = Path(__file__).resolve().parent.parent
SCRIPT_PATH = REPO_ROOT / ".github" / "scripts" / "check_version_bump.py"
def _load_script() -> Any:
"""Load the check_version_bump module by file path (it lives outside src/)."""
spec = importlib.util.spec_from_file_location("check_version_bump", 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
cvb = _load_script()
# ---------- pyproject_version ----------
def test_pyproject_version_extracts_string() -> None:
text = '[project]\nname = "harness-python-react"\nversion = "1.6.5"\n'
assert cvb.pyproject_version(text) == "1.6.5"
def test_pyproject_version_missing_raises() -> None:
with pytest.raises(ValueError, match="version not found"):
cvb.pyproject_version('[project]\nname = "harness-python-react"\n')
def test_pyproject_version_empty_raises() -> None:
with pytest.raises(ValueError, match="version not found"):
cvb.pyproject_version('[project]\nversion = ""\n')
# ---------- uv_lock_self_version ----------
def test_uv_lock_self_version_extracts() -> None:
lock = (
'[[package]]\nname = "annotated-doc"\nversion = "0.0.4"\n\n'
'[[package]]\nname = "harness-python-react"\nversion = "1.6.5"\n'
'source = { editable = "." }\n'
)
assert cvb.uv_lock_self_version(lock) == "1.6.5"
def test_uv_lock_self_version_missing_raises() -> None:
with pytest.raises(ValueError, match="harness-python-react"):
cvb.uv_lock_self_version('[[package]]\nname = "fastapi"\nversion = "0.100"\n')
# ---------- is_release_pr ----------
@pytest.mark.parametrize(
"title,expected",
[
("release: v1.6.5 — harness rollout", True),
("Release: v1.6.5", True), # leading-cap accepted
("RELEASE: yes", True),
(" release: leading-spaces", True),
("feat: add a thing", False),
("fix: something", False),
("chore: release notes update", False), # 'release' substring, wrong prefix
("", False),
(None, False),
],
)
def test_is_release_pr(title: str | None, expected: bool) -> None:
assert cvb.is_release_pr(title) is expected
# ---------- main() end-to-end ----------
@pytest.fixture
def fake_repo(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Iterator[Path]:
"""Set up a fake repo root with pyproject.toml + uv.lock and chdir into it."""
(tmp_path / "pyproject.toml").write_text(
'[project]\nname = "harness-python-react"\nversion = "1.6.6"\n',
encoding="utf-8",
)
(tmp_path / "uv.lock").write_text(
'[[package]]\nname = "harness-python-react"\nversion = "1.6.6"\n'
'source = { editable = "." }\n',
encoding="utf-8",
)
monkeypatch.chdir(tmp_path)
yield tmp_path
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_git_show(monkeypatch: pytest.MonkeyPatch, base_pyproject: str) -> None:
def fake(path: Path, base_ref: str) -> str:
assert str(path) == "pyproject.toml"
assert base_ref == "develop"
return base_pyproject
monkeypatch.setattr(cvb, "git_show_at_base", fake)
def test_main_bumped_pass(
fake_repo: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
) -> None:
monkeypatch.setenv("GITHUB_BASE_REF", "develop")
_set_event_payload(monkeypatch, fake_repo, "feat: add thing")
_stub_git_show(
monkeypatch, '[project]\nname = "harness-python-react"\nversion = "1.6.5"\n'
)
assert cvb.main() == 0
out = capsys.readouterr().out
assert "Version bumped: 1.6.5 -> 1.6.6" in out
def test_main_unchanged_fails(
fake_repo: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
) -> None:
monkeypatch.setenv("GITHUB_BASE_REF", "develop")
_set_event_payload(monkeypatch, fake_repo, "feat: forgot to bump")
_stub_git_show(
monkeypatch, '[project]\nname = "harness-python-react"\nversion = "1.6.6"\n'
)
assert cvb.main() == 1
err_out = capsys.readouterr().out
assert "::error::" in err_out
assert "unchanged from develop" in err_out
def test_main_release_prefix_exempt(
fake_repo: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
) -> None:
monkeypatch.setenv("GITHUB_BASE_REF", "main")
_set_event_payload(monkeypatch, fake_repo, "release: v1.6.6 — harness")
# Even though pyproject == base, release: PRs short-circuit before we
# inspect git history.
_stub_git_show(
monkeypatch, '[project]\nname = "harness-python-react"\nversion = "1.6.6"\n'
)
assert cvb.main() == 0
assert "release: PR" in capsys.readouterr().out
def test_main_uv_lock_mismatch_fails(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
(tmp_path / "pyproject.toml").write_text(
'[project]\nname = "harness-python-react"\nversion = "1.6.6"\n',
encoding="utf-8",
)
(tmp_path / "uv.lock").write_text(
'[[package]]\nname = "harness-python-react"\nversion = "1.6.5"\n' # stale!
'source = { editable = "." }\n',
encoding="utf-8",
)
monkeypatch.chdir(tmp_path)
monkeypatch.setenv("GITHUB_BASE_REF", "develop")
_set_event_payload(monkeypatch, tmp_path, "feat: stale lock")
_stub_git_show(
monkeypatch, '[project]\nname = "harness-python-react"\nversion = "1.6.5"\n'
)
assert cvb.main() == 1
err = capsys.readouterr().out
assert "self-version disagree" in err
def test_main_no_base_ref_skips(
fake_repo: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
) -> None:
# Outside a PR run there is no GITHUB_BASE_REF; the gate degrades green.
monkeypatch.delenv("GITHUB_BASE_REF", raising=False)
monkeypatch.delenv("GITHUB_EVENT_PATH", raising=False)
assert cvb.main() == 0
assert "skipping" in capsys.readouterr().out
def test_main_parse_error_exits_2(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
(tmp_path / "pyproject.toml").write_text(
'[project]\nname = "harness-python-react"\n# version missing\n',
encoding="utf-8",
)
(tmp_path / "uv.lock").write_text(
'[[package]]\nname = "harness-python-react"\nversion = "1.6.6"\n',
encoding="utf-8",
)
monkeypatch.chdir(tmp_path)
monkeypatch.setenv("GITHUB_BASE_REF", "develop")
_set_event_payload(monkeypatch, tmp_path, "feat: x")
_stub_git_show(
monkeypatch, '[project]\nname = "harness-python-react"\nversion = "1.6.5"\n'
)
assert cvb.main() == 2
assert "::error::" in capsys.readouterr().out
# Ensure the loader didn't accidentally leave os.environ polluted.
def test_environ_unaffected_by_loader() -> None:
# Sanity: nothing the script does at import time touches os.environ.
assert "CHECK_VERSION_BUMP_LOADED" not in os.environ