forked from python-semver/python-semver
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tests.py
298 lines (214 loc) · 8.78 KB
/
tests.py
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import pytest # noqa
from semver import compare
from semver import match
from semver import parse
from semver import format_version
from semver import bump_major
from semver import bump_minor
from semver import bump_patch
from semver import bump_prerelease
from semver import bump_build
from semver import min_ver
from semver import max_ver
SEMVERFUNCS = [
compare, match, parse, format_version,
bump_major, bump_minor, bump_patch, bump_prerelease, bump_build,
max_ver, min_ver,
]
@pytest.mark.parametrize("func", SEMVERFUNCS,
ids=[func.__name__ for func in SEMVERFUNCS])
def test_fordocstrings(func):
assert func.__doc__, "Need a docstring for function %r" % func.__name
def test_should_parse_version():
result = parse("1.2.3-alpha.1.2+build.11.e0f985a")
assert result == {
'major': 1,
'minor': 2,
'patch': 3,
'prerelease': 'alpha.1.2',
'build': 'build.11.e0f985a',
}
result = parse("1.2.3-alpha-1+build.11.e0f985a")
assert result == {
'major': 1,
'minor': 2,
'patch': 3,
'prerelease': 'alpha-1',
'build': 'build.11.e0f985a',
}
def test_should_parse_zero_prerelease():
result = parse("1.2.3-rc.0+build.0")
assert result == {
'major': 1,
'minor': 2,
'patch': 3,
'prerelease': 'rc.0',
'build': 'build.0',
}
result = parse("1.2.3-rc.0.0+build.0")
assert result == {
'major': 1,
'minor': 2,
'patch': 3,
'prerelease': 'rc.0.0',
'build': 'build.0',
}
def test_should_get_less():
assert compare("1.0.0", "2.0.0") == -1
def test_should_get_greater():
assert compare("2.0.0", "1.0.0") == 1
def test_should_match_simple():
assert match("2.3.7", ">=2.3.6") is True
def test_should_no_match_simple():
assert match("2.3.7", ">=2.3.8") is False
def test_should_match_not_equal():
assert match("2.3.7", "!=2.3.8") is True
assert match("2.3.7", "!=2.3.6") is True
assert match("2.3.7", "!=2.3.7") is False
def test_should_not_raise_value_error_for_expected_match_expression():
assert match("2.3.7", "<2.4.0") is True
assert match("2.3.7", ">2.3.5") is True
assert match("2.3.7", "<=2.3.9") is True
assert match("2.3.7", ">=2.3.5") is True
assert match("2.3.7", "==2.3.7") is True
assert match("2.3.7", "!=2.3.7") is False
def test_should_raise_value_error_for_unexpected_match_expression():
with pytest.raises(ValueError):
match("2.3.7", "=2.3.7")
with pytest.raises(ValueError):
match("2.3.7", "~2.3.7")
with pytest.raises(ValueError):
match("2.3.7", "^2.3.7")
def test_should_raise_value_error_for_zero_prefixed_versions():
with pytest.raises(ValueError):
parse("01.2.3")
with pytest.raises(ValueError):
parse("1.02.3")
with pytest.raises(ValueError):
parse("1.2.03")
def test_should_raise_value_error_for_invalid_value():
with pytest.raises(ValueError):
compare('foo', 'bar')
with pytest.raises(ValueError):
compare('1.x', '1.0.0')
def test_should_raise_value_error_for_invalid_match_expression():
with pytest.raises(ValueError):
match('1.0.0', '')
with pytest.raises(ValueError):
match('1.0.0', '!')
with pytest.raises(ValueError):
match('1.0.0', '1.0.0')
def test_should_follow_specification_comparison():
"""
produce comparison chain:
1.0.0-alpha < 1.0.0-alpha.1 < 1.0.0-beta.2 < 1.0.0-beta.11
< 1.0.0-rc.1 < 1.0.0-rc.1+build.1 < 1.0.0 < 1.0.0+0.3.7 < 1.3.7+build
< 1.3.7+build.2.b8f12d7 < 1.3.7+build.11.e0f985a
and in backward too.
"""
chain = [
'1.0.0-alpha', '1.0.0-alpha.1', '1.0.0-beta.2', '1.0.0-beta.11',
'1.0.0-rc.1', '1.0.0', '1.3.7+build',
]
versions = zip(chain[:-1], chain[1:])
for low_version, high_version in versions:
assert compare(low_version, high_version) == -1, \
'%s should be lesser than %s' % (low_version, high_version)
assert compare(high_version, low_version) == 1, \
'%s should be higher than %s' % (high_version, low_version)
def test_should_compare_rc_builds():
assert compare('1.0.0-beta.2', '1.0.0-beta.11') == -1
def test_should_compare_release_candidate_with_release():
assert compare('1.0.0-rc.1', '1.0.0') == -1
assert compare('1.0.0-rc.1+build.1', '1.0.0') == -1
def test_should_say_equal_versions_are_equal():
assert compare('2.0.0', '2.0.0') == 0
assert compare('1.1.9-rc.1', '1.1.9-rc.1') == 0
assert compare('1.1.9+build.1', '1.1.9+build.1') == 0
assert compare('1.1.9-rc.1+build.1', '1.1.9-rc.1+build.1') == 0
def test_should_compare_versions_with_build_and_release():
assert compare('1.1.9-rc.1', '1.1.9-rc.1+build.1') == 0
assert compare('1.1.9-rc.1', '1.1.9+build.1') == -1
def test_should_ignore_builds_on_compare():
assert compare('1.0.0+build.1', '1.0.0') == 0
assert compare('1.0.0-alpha.1+build.1', '1.0.0-alpha.1') == 0
assert compare('1.0.0+build.1', '1.0.0-alpha.1') == 1
assert compare('1.0.0+build.1', '1.0.0-alpha.1+build.1') == 1
def test_should_correctly_format_version():
assert format_version(3, 4, 5) == '3.4.5'
assert format_version(3, 4, 5, 'rc.1') == '3.4.5-rc.1'
assert format_version(3, 4, 5, prerelease='rc.1') == '3.4.5-rc.1'
assert format_version(3, 4, 5, build='build.4') == '3.4.5+build.4'
assert format_version(3, 4, 5, 'rc.1', 'build.4') == '3.4.5-rc.1+build.4'
def test_should_bump_major():
assert bump_major('3.4.5') == '4.0.0'
def test_should_bump_minor():
assert bump_minor('3.4.5') == '3.5.0'
def test_should_bump_patch():
assert bump_patch('3.4.5') == '3.4.6'
def test_should_ignore_extensions_for_bump():
assert bump_patch('3.4.5-rc1+build4') == '3.4.6'
def test_should_get_max():
assert max_ver('3.4.5', '4.0.2') == '4.0.2'
def test_should_get_min():
assert min_ver('3.4.5', '4.0.2') == '3.4.5'
def test_should_get_min_same():
assert min_ver('3.4.5', '3.4.5') == '3.4.5'
def test_should_get_more_rc1():
assert compare("1.0.0-rc1", "1.0.0-rc0") == 1
def test_prerelease_order():
assert min_ver('1.2.3-rc.2', '1.2.3-rc.10') == '1.2.3-rc.2'
assert min_ver('1.2.3-rc2', '1.2.3-rc10') == '1.2.3-rc10'
# identifiers with letters or hyphens are compared lexically in ASCII sort
# order.
assert min_ver('1.2.3-Rc10', '1.2.3-rc10') == '1.2.3-Rc10'
# Numeric identifiers always have lower precedence than non-numeric
# identifiers.
assert min_ver('1.2.3-2', '1.2.3-rc') == '1.2.3-rc'
# A larger set of pre-release fields has a higher precedence than a
# smaller set, if all of the preceding identifiers are equal.
assert min_ver('1.2.3-rc.2.1', '1.2.3-rc.2') == '1.2.3-rc.2'
# When major, minor, and patch are equal, a pre-release version has lower
# precedence than a normal version.
assert min_ver('1.2.3', '1.2.3-1') == '1.2.3-1'
assert min_ver('1.0.0-alpha', '1.0.0-alpha.1') == '1.0.0-alpha'
def test_should_bump_prerelease():
assert bump_prerelease('3.4.5-rc.9') == '3.4.5-rc.10'
assert bump_prerelease('3.4.5') == '3.4.5-rc.1'
def test_should_ignore_build_on_prerelease_bump():
assert bump_prerelease('3.4.5-rc.1+build.4') == '3.4.5-rc.2'
def test_should_bump_build():
assert bump_build('3.4.5-rc.1+build.9') == '3.4.5-rc.1+build.10'
assert bump_build('3.4.5-rc.1+0009.dev') == '3.4.5-rc.1+0010.dev'
assert bump_build('3.4.5-rc.1') == '3.4.5-rc.1+build.1'
assert bump_build('3.4.5') == '3.4.5+build.1'
def test_parse_version_has_major_and_minor_only():
version_info = parse('1.0')
assert version_info['major'] == 1
assert version_info['minor'] == 0
def test_parse_version_with_patch_has_plus():
version_info = parse('3.4.5+Final')
assert version_info['major'] == 3
assert version_info['minor'] == 4
assert version_info['patch'] == 5
assert version_info['build'] == 'Final'
def test_parse_version_with_patch_has_dot():
version_info = parse('3.4.5.Final')
assert version_info['major'] == 3
assert version_info['minor'] == 4
assert version_info['patch'] == 5
assert version_info['build'] == 'Final'
def test_compare_versions_having_major_and_minor_only():
assert compare('1.0', '2.0') == -1
assert compare('2.0', '1.0') == 1
assert compare('2.0', '2.0') == 0
def test_compare_versions_with_patch_has_plus():
assert compare('3.4.5+Final', '3.4.5') == 0
assert compare('3.4.5+Final', '3.4.6') == -1
assert compare('3.4.5+Final', '3.4.4') == 1
assert compare('3.4.5+Final', '3.4.5.Final') == 0
def test_compare_versions_with_patch_has_dot():
assert compare('3.4.5.Final', '3.4.5') == 0
assert compare('3.4.5.Final', '3.4.6') == -1
assert compare('3.4.5.Final', '3.4.4') == 1
assert compare('3.4.5.Final', '3.4.5.Final') == 0