forked from python/release-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_run_release.py
More file actions
248 lines (206 loc) · 7.47 KB
/
test_run_release.py
File metadata and controls
248 lines (206 loc) · 7.47 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
import builtins
import contextlib
import io
import tarfile
from contextlib import nullcontext as does_not_raise
from pathlib import Path
from typing import cast
import pytest
import run_release
from release import ReleaseShelf, Tag
from run_release import ReleaseException
@pytest.mark.parametrize(
"version",
["sigstore 3.6.2", "sigstore 3.6.6"],
)
def test_check_sigstore_version_success(version) -> None:
# Verify runs with no exceptions
run_release.check_sigstore_version(version)
@pytest.mark.parametrize(
"version",
["sigstore 3.4.0", "sigstore 3.6.0", "sigstore 4.0.0", ""],
)
def test_check_sigstore_version_exception(version) -> None:
with pytest.raises(
ReleaseException, match="Sigstore version not detected or not valid"
):
run_release.check_sigstore_version(version)
@pytest.mark.parametrize(
["url", "expected"],
[
("github.com/hugovk/cpython.git", "hugovk"),
("[email protected]:hugovk/cpython.git", "hugovk"),
("https://github.com/hugovk/cpython.git", "hugovk"),
],
)
def test_extract_github_owner(url: str, expected: str) -> None:
assert run_release.extract_github_owner(url) == expected
def test_invalid_extract_github_owner() -> None:
with pytest.raises(
ReleaseException,
match="Could not parse GitHub owner from 'origin' remote URL: "
"https://example.com",
):
run_release.extract_github_owner("https://example.com")
@pytest.mark.parametrize(
["release_tag", "git_current_branch", "expectation"],
[
# Success cases
("3.15.0rc1", "3.15\n", does_not_raise()),
("3.15.0b1", "3.15\n", does_not_raise()),
("3.15.0a6", "main\n", does_not_raise()),
("3.14.3", "3.14\n", does_not_raise()),
("3.13.12", "3.13\n", does_not_raise()),
# Failure cases
(
"3.15.0rc1",
"main\n",
pytest.raises(ReleaseException, match="on main branch, expected 3.15"),
),
(
"3.15.0b1",
"main\n",
pytest.raises(ReleaseException, match="on main branch, expected 3.15"),
),
(
"3.15.0a6",
"3.14\n",
pytest.raises(ReleaseException, match="on 3.14 branch, expected main"),
),
(
"3.14.3",
"main\n",
pytest.raises(ReleaseException, match="on main branch, expected 3.14"),
),
],
)
def test_check_cpython_repo_branch(
monkeypatch, release_tag: str, git_current_branch: str, expectation
) -> None:
# Arrange
db = {"release": Tag(release_tag), "git_repo": "/fake/repo"}
monkeypatch.setattr(
run_release.subprocess,
"check_output",
lambda *args, **kwargs: git_current_branch,
)
# Act / Assert
with expectation:
run_release.check_cpython_repo_branch(cast(ReleaseShelf, db))
@pytest.mark.parametrize(
["age_seconds", "user_continues", "expectation"],
[
# Recent repo (< 1 day) - no question asked
(3600, None, does_not_raise()),
# Old repo (> 1 day) + user says yes
(90000, True, does_not_raise()),
# Old repo (> 1 day) + user says no
(90000, False, pytest.raises(ReleaseException, match="repository is old")),
],
)
def test_check_cpython_repo_age(
monkeypatch, age_seconds: int, user_continues: bool | None, expectation
) -> None:
# Arrange
db = {"release": Tag("3.15.0a6"), "git_repo": "/fake/repo"}
current_time = 1700000000
commit_timestamp = current_time - age_seconds
def fake_check_output(cmd, **kwargs):
cmd_str = " ".join(cmd)
if "%ct" in cmd_str:
return f"{commit_timestamp}\n"
if "%cr" in cmd_str:
return "some time ago\n"
return ""
monkeypatch.setattr(run_release.subprocess, "check_output", fake_check_output)
monkeypatch.setattr(run_release.time, "time", lambda: current_time)
if user_continues is not None:
monkeypatch.setattr(run_release, "ask_question", lambda _: user_continues)
# Act / Assert
with expectation:
run_release.check_cpython_repo_age(cast(ReleaseShelf, db))
def test_check_magic_number() -> None:
db = {
"release": Tag("3.14.0rc1"),
"git_repo": str(Path(__file__).parent / "magicdata"),
}
with pytest.raises(ReleaseException, match="Magic numbers in .* don't match"):
run_release.check_magic_number(cast(ReleaseShelf, db))
def prepare_fake_docs(tmp_path: Path, content: str) -> None:
docs_path = tmp_path / "3.13.0rc1/docs"
docs_path.mkdir(parents=True)
tarball = tarfile.open(docs_path / "python-3.13.0rc1-docs-html.tar.bz2", "w:bz2")
with tarball:
tarinfo = tarfile.TarInfo("index.html")
tarinfo.size = len(content)
tarball.addfile(tarinfo, io.BytesIO(content.encode()))
@contextlib.contextmanager
def fake_answers(monkeypatch: pytest.MonkeyPatch, answers: list[str]) -> None:
"""Monkey-patch input() to give the given answers. All must be consumed."""
answers_left = list(answers)
def fake_input(question):
print(question, "--", answers_left[0])
return answers_left.pop(0)
with monkeypatch.context() as ctx:
ctx.setattr(builtins, "input", fake_input)
yield
assert answers_left == []
def test_check_doc_unreleased_version_no_file(tmp_path: Path) -> None:
db = {
"release": Tag("3.13.0rc1"),
"git_repo": str(tmp_path),
}
with pytest.raises(AssertionError):
# There should be a docs artefact available
run_release.check_doc_unreleased_version(cast(ReleaseShelf, db))
def test_check_doc_unreleased_version_no_file_alpha(tmp_path: Path) -> None:
db = {
"release": Tag("3.13.0a1"),
"git_repo": str(tmp_path),
}
# No docs artefact needed for alphas
run_release.check_doc_unreleased_version(cast(ReleaseShelf, db))
def test_check_doc_unreleased_version_ok(tmp_path: Path) -> None:
prepare_fake_docs(
tmp_path,
"<div>New in 3.13</div>",
)
db = {
"release": Tag("3.13.0rc1"),
"git_repo": str(tmp_path),
}
run_release.check_doc_unreleased_version(cast(ReleaseShelf, db))
def test_check_doc_unreleased_version_not_ok(monkeypatch, tmp_path: Path) -> None:
prepare_fake_docs(
tmp_path,
"<div>New in 3.13.0rc1 (unreleased)</div>",
)
db = {
"release": Tag("3.13.0rc1"),
"git_repo": str(tmp_path),
}
with fake_answers(monkeypatch, ["no"]), pytest.raises(AssertionError):
run_release.check_doc_unreleased_version(cast(ReleaseShelf, db))
def test_check_doc_unreleased_version_waived(monkeypatch, tmp_path: Path) -> None:
prepare_fake_docs(
tmp_path,
"<div>New in 3.13.0rc1 (unreleased)</div>",
)
db = {
"release": Tag("3.13.0rc1"),
"git_repo": str(tmp_path),
}
with fake_answers(monkeypatch, ["yes"]):
run_release.check_doc_unreleased_version(cast(ReleaseShelf, db))
def test_update_whatsnew_toctree(tmp_path: Path) -> None:
# Arrange
# Only first beta triggers update
db = {"release": Tag("3.14.0b1")}
original_toctree_file = Path(__file__).parent / "whatsnew_index.rst"
toctree__file = tmp_path / "patchlevel.h"
toctree__file.write_text(original_toctree_file.read_text())
# Act
run_release.update_whatsnew_toctree(cast(ReleaseShelf, db), str(toctree__file))
# Assert
new_contents = toctree__file.read_text()
assert " 3.15.rst\n 3.14.rst\n" in new_contents