summaryrefslogtreecommitdiffstats
path: root/test/units/utils/test_version.py
blob: 3c2cbaf4c14f5d9a637fc0ed93ed2a4bf7f33a47 (plain)
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# -*- coding: utf-8 -*-
# (c) 2020 Matt Martz <matt@sivel.net>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function
__metaclass__ = type

from ansible.module_utils.compat.version import LooseVersion, StrictVersion

import pytest

from ansible.utils.version import _Alpha, _Numeric, SemanticVersion


EQ = [
    ('1.0.0', '1.0.0', True),
    ('1.0.0', '1.0.0-beta', False),
    ('1.0.0-beta2+build1', '1.0.0-beta.2+build.1', False),
    ('1.0.0-beta+build', '1.0.0-beta+build', True),
    ('1.0.0-beta+build1', '1.0.0-beta+build2', True),
    ('1.0.0-beta+a', '1.0.0-alpha+bar', False),
]

NE = [
    ('1.0.0', '1.0.0', False),
    ('1.0.0', '1.0.0-beta', True),
    ('1.0.0-beta2+build1', '1.0.0-beta.2+build.1', True),
    ('1.0.0-beta+build', '1.0.0-beta+build', False),
    ('1.0.0-beta+a', '1.0.0-alpha+bar', True),
]

LT = [
    ('1.0.0', '2.0.0', True),
    ('1.0.0-beta', '2.0.0-alpha', True),
    ('1.0.0-alpha', '2.0.0-beta', True),
    ('1.0.0-alpha', '1.0.0', True),
    ('1.0.0-beta', '1.0.0-alpha3', False),
    ('1.0.0+foo', '1.0.0-alpha', False),
    ('1.0.0-beta.1', '1.0.0-beta.a', True),
    ('1.0.0-beta+a', '1.0.0-alpha+bar', False),
]

GT = [
    ('1.0.0', '2.0.0', False),
    ('1.0.0-beta', '2.0.0-alpha', False),
    ('1.0.0-alpha', '2.0.0-beta', False),
    ('1.0.0-alpha', '1.0.0', False),
    ('1.0.0-beta', '1.0.0-alpha3', True),
    ('1.0.0+foo', '1.0.0-alpha', True),
    ('1.0.0-beta.1', '1.0.0-beta.a', False),
    ('1.0.0-beta+a', '1.0.0-alpha+bar', True),
]

LE = [
    ('1.0.0', '1.0.0', True),
    ('1.0.0', '2.0.0', True),
    ('1.0.0-alpha', '1.0.0-beta', True),
    ('1.0.0-beta', '1.0.0-alpha', False),
]

GE = [
    ('1.0.0', '1.0.0', True),
    ('1.0.0', '2.0.0', False),
    ('1.0.0-alpha', '1.0.0-beta', False),
    ('1.0.0-beta', '1.0.0-alpha', True),
]

VALID = [
    "0.0.4",
    "1.2.3",
    "10.20.30",
    "1.1.2-prerelease+meta",
    "1.1.2+meta",
    "1.1.2+meta-valid",
    "1.0.0-alpha",
    "1.0.0-beta",
    "1.0.0-alpha.beta",
    "1.0.0-alpha.beta.1",
    "1.0.0-alpha.1",
    "1.0.0-alpha0.valid",
    "1.0.0-alpha.0valid",
    "1.0.0-alpha-a.b-c-somethinglong+build.1-aef.1-its-okay",
    "1.0.0-rc.1+build.1",
    "2.0.0-rc.1+build.123",
    "1.2.3-beta",
    "10.2.3-DEV-SNAPSHOT",
    "1.2.3-SNAPSHOT-123",
    "1.0.0",
    "2.0.0",
    "1.1.7",
    "2.0.0+build.1848",
    "2.0.1-alpha.1227",
    "1.0.0-alpha+beta",
    "1.2.3----RC-SNAPSHOT.12.9.1--.12+788",
    "1.2.3----R-S.12.9.1--.12+meta",
    "1.2.3----RC-SNAPSHOT.12.9.1--.12",
    "1.0.0+0.build.1-rc.10000aaa-kk-0.1",
    "99999999999999999999999.999999999999999999.99999999999999999",
    "1.0.0-0A.is.legal",
]

INVALID = [
    "1",
    "1.2",
    "1.2.3-0123",
    "1.2.3-0123.0123",
    "1.1.2+.123",
    "+invalid",
    "-invalid",
    "-invalid+invalid",
    "-invalid.01",
    "alpha",
    "alpha.beta",
    "alpha.beta.1",
    "alpha.1",
    "alpha+beta",
    "alpha_beta",
    "alpha.",
    "alpha..",
    "beta",
    "1.0.0-alpha_beta",
    "-alpha.",
    "1.0.0-alpha..",
    "1.0.0-alpha..1",
    "1.0.0-alpha...1",
    "1.0.0-alpha....1",
    "1.0.0-alpha.....1",
    "1.0.0-alpha......1",
    "1.0.0-alpha.......1",
    "01.1.1",
    "1.01.1",
    "1.1.01",
    "1.2",
    "1.2.3.DEV",
    "1.2-SNAPSHOT",
    "1.2.31.2.3----RC-SNAPSHOT.12.09.1--..12+788",
    "1.2-RC-SNAPSHOT",
    "-1.0.3-gamma+b7718",
    "+justmeta",
    "9.8.7+meta+meta",
    "9.8.7-whatever+meta+meta",
]

PRERELEASE = [
    ('1.0.0-alpha', True),
    ('1.0.0-alpha.1', True),
    ('1.0.0-0.3.7', True),
    ('1.0.0-x.7.z.92', True),
    ('0.1.2', False),
    ('0.1.2+bob', False),
    ('1.0.0', False),
]

STABLE = [
    ('1.0.0-alpha', False),
    ('1.0.0-alpha.1', False),
    ('1.0.0-0.3.7', False),
    ('1.0.0-x.7.z.92', False),
    ('0.1.2', False),
    ('0.1.2+bob', False),
    ('1.0.0', True),
    ('1.0.0+bob', True),
]

LOOSE_VERSION = [
    (LooseVersion('1'), SemanticVersion('1.0.0')),
    (LooseVersion('1-alpha'), SemanticVersion('1.0.0-alpha')),
    (LooseVersion('1.0.0-alpha+build'), SemanticVersion('1.0.0-alpha+build')),
]

LOOSE_VERSION_INVALID = [
    LooseVersion('1.a.3'),
    LooseVersion(),
    'bar',
    StrictVersion('1.2.3'),
]


def test_semanticversion_none():
    assert SemanticVersion().major is None


@pytest.mark.parametrize('left,right,expected', EQ)
def test_eq(left, right, expected):
    assert (SemanticVersion(left) == SemanticVersion(right)) is expected


@pytest.mark.parametrize('left,right,expected', NE)
def test_ne(left, right, expected):
    assert (SemanticVersion(left) != SemanticVersion(right)) is expected


@pytest.mark.parametrize('left,right,expected', LT)
def test_lt(left, right, expected):
    assert (SemanticVersion(left) < SemanticVersion(right)) is expected


@pytest.mark.parametrize('left,right,expected', LE)
def test_le(left, right, expected):
    assert (SemanticVersion(left) <= SemanticVersion(right)) is expected


@pytest.mark.parametrize('left,right,expected', GT)
def test_gt(left, right, expected):
    assert (SemanticVersion(left) > SemanticVersion(right)) is expected


@pytest.mark.parametrize('left,right,expected', GE)
def test_ge(left, right, expected):
    assert (SemanticVersion(left) >= SemanticVersion(right)) is expected


@pytest.mark.parametrize('value', VALID)
def test_valid(value):
    SemanticVersion(value)


@pytest.mark.parametrize('value', INVALID)
def test_invalid(value):
    pytest.raises(ValueError, SemanticVersion, value)


def test_example_precedence():
    # https://semver.org/#spec-item-11
    sv = SemanticVersion
    assert sv('1.0.0') < sv('2.0.0') < sv('2.1.0') < sv('2.1.1')
    assert sv('1.0.0-alpha') < sv('1.0.0')
    assert sv('1.0.0-alpha') < sv('1.0.0-alpha.1') < sv('1.0.0-alpha.beta')
    assert sv('1.0.0-beta') < sv('1.0.0-beta.2') < sv('1.0.0-beta.11') < sv('1.0.0-rc.1') < sv('1.0.0')


@pytest.mark.parametrize('value,expected', PRERELEASE)
def test_prerelease(value, expected):
    assert SemanticVersion(value).is_prerelease is expected


@pytest.mark.parametrize('value,expected', STABLE)
def test_stable(value, expected):
    assert SemanticVersion(value).is_stable is expected


@pytest.mark.parametrize('value,expected', LOOSE_VERSION)
def test_from_loose_version(value, expected):
    assert SemanticVersion.from_loose_version(value) == expected


@pytest.mark.parametrize('value', LOOSE_VERSION_INVALID)
def test_from_loose_version_invalid(value):
    pytest.raises((AttributeError, ValueError), SemanticVersion.from_loose_version, value)


def test_comparison_with_string():
    assert SemanticVersion('1.0.0') > '0.1.0'


def test_alpha():
    assert _Alpha('a') == _Alpha('a')
    assert _Alpha('a') == 'a'
    assert _Alpha('a') != _Alpha('b')
    assert _Alpha('a') != 1
    assert _Alpha('a') < _Alpha('b')
    assert _Alpha('a') < 'c'
    assert _Alpha('a') > _Numeric(1)
    with pytest.raises(ValueError):
        _Alpha('a') < None
    assert _Alpha('a') <= _Alpha('a')
    assert _Alpha('a') <= _Alpha('b')
    assert _Alpha('b') >= _Alpha('a')
    assert _Alpha('b') >= _Alpha('b')

    # The following 3*6 tests check that all comparison operators perform
    # as expected. DO NOT remove any of them, or reformulate them (to remove
    # the explicit `not`)!

    assert _Alpha('a') == _Alpha('a')
    assert not _Alpha('a') != _Alpha('a')  # pylint: disable=unneeded-not
    assert not _Alpha('a') < _Alpha('a')  # pylint: disable=unneeded-not
    assert _Alpha('a') <= _Alpha('a')
    assert not _Alpha('a') > _Alpha('a')  # pylint: disable=unneeded-not
    assert _Alpha('a') >= _Alpha('a')

    assert not _Alpha('a') == _Alpha('b')  # pylint: disable=unneeded-not
    assert _Alpha('a') != _Alpha('b')
    assert _Alpha('a') < _Alpha('b')
    assert _Alpha('a') <= _Alpha('b')
    assert not _Alpha('a') > _Alpha('b')  # pylint: disable=unneeded-not
    assert not _Alpha('a') >= _Alpha('b')  # pylint: disable=unneeded-not

    assert not _Alpha('b') == _Alpha('a')  # pylint: disable=unneeded-not
    assert _Alpha('b') != _Alpha('a')
    assert not _Alpha('b') < _Alpha('a')  # pylint: disable=unneeded-not
    assert not _Alpha('b') <= _Alpha('a')  # pylint: disable=unneeded-not
    assert _Alpha('b') > _Alpha('a')
    assert _Alpha('b') >= _Alpha('a')


def test_numeric():
    assert _Numeric(1) == _Numeric(1)
    assert _Numeric(1) == 1
    assert _Numeric(1) != _Numeric(2)
    assert _Numeric(1) != 'a'
    assert _Numeric(1) < _Numeric(2)
    assert _Numeric(1) < 3
    assert _Numeric(1) < _Alpha('b')
    with pytest.raises(ValueError):
        _Numeric(1) < None
    assert _Numeric(1) <= _Numeric(1)
    assert _Numeric(1) <= _Numeric(2)
    assert _Numeric(2) >= _Numeric(1)
    assert _Numeric(2) >= _Numeric(2)

    # The following 3*6 tests check that all comparison operators perform
    # as expected. DO NOT remove any of them, or reformulate them (to remove
    # the explicit `not`)!

    assert _Numeric(1) == _Numeric(1)
    assert not _Numeric(1) != _Numeric(1)  # pylint: disable=unneeded-not
    assert not _Numeric(1) < _Numeric(1)  # pylint: disable=unneeded-not
    assert _Numeric(1) <= _Numeric(1)
    assert not _Numeric(1) > _Numeric(1)  # pylint: disable=unneeded-not
    assert _Numeric(1) >= _Numeric(1)

    assert not _Numeric(1) == _Numeric(2)  # pylint: disable=unneeded-not
    assert _Numeric(1) != _Numeric(2)
    assert _Numeric(1) < _Numeric(2)
    assert _Numeric(1) <= _Numeric(2)
    assert not _Numeric(1) > _Numeric(2)  # pylint: disable=unneeded-not
    assert not _Numeric(1) >= _Numeric(2)  # pylint: disable=unneeded-not

    assert not _Numeric(2) == _Numeric(1)  # pylint: disable=unneeded-not
    assert _Numeric(2) != _Numeric(1)
    assert not _Numeric(2) < _Numeric(1)  # pylint: disable=unneeded-not
    assert not _Numeric(2) <= _Numeric(1)  # pylint: disable=unneeded-not
    assert _Numeric(2) > _Numeric(1)
    assert _Numeric(2) >= _Numeric(1)