-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathtest_set_version.py
More file actions
195 lines (164 loc) · 8.12 KB
/
Copy pathtest_set_version.py
File metadata and controls
195 lines (164 loc) · 8.12 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
# Tests for "set_version.py" module.
# !! Don't forget to run this test in case you change "set_version.py" to make sure the asserts still pass !!
import pytest
import set_version
from set_version import (
_get_build_version,
_version_sort_key,
get_build_version,
resolve_build_version,
validate_specified_version,
VERSION_RE,
)
def test_set_version():
assert "1.0.0" == _get_build_version("major", "stable", [])
assert "2.0.0" == _get_build_version("major", "stable", ["1.1.0"])
assert "1.0.0" == _get_build_version("major", "stable", ["0.1.1.rc1", "0.1.1.rc0", "0.1.0", "0.0.1"])
assert "1.0.0" == _get_build_version("major", "stable", ["0.1.1.dev0", "0.1.1.rc0", "0.1.0", "0.0.1"])
assert "0.1.0" == _get_build_version("minor", "stable", ["0.0.2", "0.0.1"])
assert "0.2.0" == _get_build_version("minor", "stable", ["0.1.1.dev0", "0.1.1.rc0", "0.1.0", "0.0.1"])
assert "0.1.2" == _get_build_version("patch", "stable", ["0.1.1", "0.0.1"])
assert "0.1.1" == _get_build_version("patch", "stable", ["0.1.1.rc1", "0.1.1.rc0", "0.1.0", "0.0.1"])
assert "1.0.0.rc0" == _get_build_version("major", "rc", [])
assert "2.0.0.rc0" == _get_build_version("major", "rc", ["3.0.0.dev0", "1.1.0"])
assert "3.0.0.rc1" == _get_build_version("major", "rc", ["3.0.0.rc0", "2.1.0", "1.1.0"])
assert "0.1.0.rc0" == _get_build_version("minor", "rc", ["0.0.2", "0.0.1"])
assert "0.1.2.rc0" == _get_build_version("patch", "rc", ["0.1.1", "0.0.1"])
assert "0.1.1.rc1" == _get_build_version("patch", "rc", ["1.0.0.dev0", "0.1.1.rc0", "0.1.0", "0.0.1"])
assert "1.0.0.dev0" == _get_build_version("major", "dev", [])
assert "2.0.0.dev0" == _get_build_version("major", "dev", ["3.0.0.rc0", "1.1.0"])
assert "3.0.0.dev1" == _get_build_version("major", "dev", ["3.0.0.dev0", "2.1.0", "1.1.0"])
assert "0.1.0.dev0" == _get_build_version("minor", "dev", ["0.0.2", "0.0.1"])
assert "0.2.0.dev0" == _get_build_version("minor", "dev", ["1.0.0.rc0", "1.0.0.dev0", "0.1.0", "0.0.1"])
assert "0.1.2.dev0" == _get_build_version("patch", "dev", ["0.1.1", "0.0.1"])
assert "0.1.1.dev0" == _get_build_version("patch", "dev", ["1.0.0.rc0", "0.1.0.dev0", "0.1.0", "0.0.1"])
assert "0.1.1.dev1" == _get_build_version("patch", "dev", ["1.0.0.rc0", "0.1.1.dev0", "0.1.0", "0.0.1"])
def test_version_regex_matches_wheel_and_sdist_filenames():
# Representative distribution filenames as they appear on a PEP 503 simple index,
# covering wheels, sdists (.tar.gz / .zip), the "-"/"_" name normalization, and
# the .devN / .rcN pre-release suffixes.
html = (
'<a href="x">azure_quantum-3.10.0-py3-none-any.whl</a>'
'<a href="x">azure-quantum-3.10.0.tar.gz</a>'
'<a href="x">azure_quantum-1.1.0.dev0-py3-none-any.whl</a>'
'<a href="x">azure-quantum-2.0.0.rc0.tar.gz</a>'
'<a href="x">azure-quantum-1.0.0.zip</a>'
)
assert sorted(set(VERSION_RE.findall(html))) == [
"1.0.0",
"1.1.0.dev0",
"2.0.0.rc0",
"3.10.0",
]
def test_version_regex_skips_unsupported_filenames():
# Legacy 4-part and alpha/beta ("bN") versions must be skipped entirely rather
# than silently truncated to a bogus 3-part version, and unrelated tokens on the
# page must never match.
html = (
'<a href="x">azure_quantum-0.11.2004.2825-py3-none-any.whl</a>'
'<a href="x">azure-quantum-0.11.2004.2825.tar.gz</a>'
'<a href="x">azure_quantum-0.13.2011.119705b1-py3-none-any.whl</a>'
'<a href="x">some-other-package-9.9.9-py3-none-any.whl</a>'
'<a href="/download/azure-quantum/1.2.3/">1.2.3</a>'
)
assert VERSION_RE.findall(html) == []
def test_version_sort_key_orders_prereleases_before_final():
# Within the same major.minor.patch: dev < rc < final, with numeric (not
# lexical) ordering of the pre-release number.
unsorted = [
"1.1.0",
"1.1.0.rc0",
"1.1.0.dev10",
"1.1.0.dev2",
"1.0.0",
]
assert sorted(unsorted, key=_version_sort_key) == [
"1.0.0",
"1.1.0.dev2",
"1.1.0.dev10",
"1.1.0.rc0",
"1.1.0",
]
def test_version_sort_key_rejects_unsupported_segment():
with pytest.raises(ValueError):
_version_sort_key("0.13.2011.119705b1")
def test_get_build_version_sorts_before_selecting(monkeypatch):
# _fetch_versions returns versions in arbitrary order; get_build_version must
# sort them (descending) before picking the latest stable to bump from. If the
# sort were skipped, the first 3-part entry ("1.0.0") would be chosen instead of
# the true latest stable ("1.1.0").
unsorted = [
"1.0.0.dev0",
"1.0.0",
"1.1.0",
"1.0.0.rc0",
"2.0.0.dev1",
"1.1.0.dev0",
]
monkeypatch.setattr(set_version, "_fetch_versions", lambda index_url: list(unsorted))
# The latest *stable* (3-part final) version is 1.1.0. 2.0.0 exists only as a
# pre-release ("2.0.0.dev1"), so it is not a release that can be patched from.
assert get_build_version("patch", "stable") == "1.1.1"
assert get_build_version("patch", "dev") == "1.1.1.dev0"
assert get_build_version("minor", "dev") == "1.2.0.dev0"
# 2.0.0.dev1 exists but 2.0.0.dev0 does not, so dev0 is the next major dev build.
assert get_build_version("major", "dev") == "2.0.0.dev0"
def test_get_build_version_empty_list_raises(monkeypatch):
monkeypatch.setattr(set_version, "_fetch_versions", lambda index_url: [])
with pytest.raises(RuntimeError):
get_build_version("patch", "dev")
def test_get_build_version_existing_version_raises(monkeypatch):
# Guard: if the computed version already exists in the published list, abort
# rather than risk republishing an existing version.
monkeypatch.setattr(set_version, "_fetch_versions", lambda index_url: ["1.0.0"])
monkeypatch.setattr(set_version, "_get_build_version", lambda *args: "1.0.0")
with pytest.raises(RuntimeError):
get_build_version("patch", "stable")
def test_resolve_build_version_uses_specified_version(monkeypatch):
# When a valid version is specified, it is returned as-is and the automated
# computation is skipped entirely (so the package index is never contacted).
def _should_not_be_called(*args, **kwargs):
raise AssertionError("get_build_version must not be called when a version is given")
monkeypatch.setattr(set_version, "get_build_version", _should_not_be_called)
assert resolve_build_version("patch", "dev", "1.2.3.dev0") == "1.2.3.dev0"
def test_resolve_build_version_falls_back_when_blank(monkeypatch):
# A blank version falls back to the automated computation.
monkeypatch.setattr(
set_version, "get_build_version", lambda vt, bt: f"computed-{vt}-{bt}"
)
assert resolve_build_version("minor", "rc", "") == "computed-minor-rc"
@pytest.mark.parametrize("blank", ["", " ", None])
def test_validate_specified_version_blank_returns_empty(blank):
# A blank/whitespace/None version returns "" (signalling automatic computation)
# and never touches the network.
assert validate_specified_version("dev", blank) == ""
@pytest.mark.parametrize(
"build_type,version",
[
("dev", "1.2.3.dev0"),
("rc", "1.2.3.rc7"),
("stable", "1.2.3"),
# Surrounding whitespace is stripped.
("dev", " 1.2.3.dev0 "),
],
)
def test_validate_specified_version_accepts_valid(build_type, version):
# A valid version that agrees with the build type is accepted and returned
# stripped of surrounding whitespace.
assert validate_specified_version(build_type, version) == version.strip()
@pytest.mark.parametrize(
"build_type,version",
[
# Malformed versions.
("dev", "1.2"),
("dev", "v1.2.3"),
# Build type disagrees with the version's suffix (or lack of one).
("dev", "1.2.3"),
("rc", "1.2.3.dev0"),
("stable", "1.2.3.rc0"),
],
)
def test_validate_specified_version_rejects_invalid(build_type, version):
# Malformed or build-type-mismatched versions fail loud.
with pytest.raises(ValueError):
validate_specified_version(build_type, version)