summaryrefslogtreecommitdiff
path: root/setuptools/_distutils/tests/test_version.py
blob: ff52ea468308417479a7d48b5704608267d1d884 (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
"""Tests for distutils.version."""
import pytest

import distutils
from distutils.version import LooseVersion
from distutils.version import StrictVersion


@pytest.fixture(autouse=True)
def suppress_deprecation():
    with distutils.version.suppress_known_deprecation():
        yield


class TestVersion:
    def test_prerelease(self):
        version = StrictVersion('1.2.3a1')
        assert version.version == (1, 2, 3)
        assert version.prerelease == ('a', 1)
        assert str(version) == '1.2.3a1'

        version = StrictVersion('1.2.0')
        assert str(version) == '1.2'

    def test_cmp_strict(self):
        versions = (
            ('1.5.1', '1.5.2b2', -1),
            ('161', '3.10a', ValueError),
            ('8.02', '8.02', 0),
            ('3.4j', '1996.07.12', ValueError),
            ('3.2.pl0', '3.1.1.6', ValueError),
            ('2g6', '11g', ValueError),
            ('0.9', '2.2', -1),
            ('1.2.1', '1.2', 1),
            ('1.1', '1.2.2', -1),
            ('1.2', '1.1', 1),
            ('1.2.1', '1.2.2', -1),
            ('1.2.2', '1.2', 1),
            ('1.2', '1.2.2', -1),
            ('0.4.0', '0.4', 0),
            ('1.13++', '5.5.kw', ValueError),
        )

        for v1, v2, wanted in versions:
            try:
                res = StrictVersion(v1)._cmp(StrictVersion(v2))
            except ValueError:
                if wanted is ValueError:
                    continue
                else:
                    raise AssertionError(
                        ("cmp(%s, %s) " "shouldn't raise ValueError") % (v1, v2)
                    )
            assert res == wanted, 'cmp({}, {}) should be {}, got {}'.format(
                v1, v2, wanted, res
            )
            res = StrictVersion(v1)._cmp(v2)
            assert res == wanted, 'cmp({}, {}) should be {}, got {}'.format(
                v1, v2, wanted, res
            )
            res = StrictVersion(v1)._cmp(object())
            assert (
                res is NotImplemented
            ), 'cmp({}, {}) should be NotImplemented, got {}'.format(v1, v2, res)

    def test_cmp(self):
        versions = (
            ('1.5.1', '1.5.2b2', -1),
            ('161', '3.10a', 1),
            ('8.02', '8.02', 0),
            ('3.4j', '1996.07.12', -1),
            ('3.2.pl0', '3.1.1.6', 1),
            ('2g6', '11g', -1),
            ('0.960923', '2.2beta29', -1),
            ('1.13++', '5.5.kw', -1),
        )

        for v1, v2, wanted in versions:
            res = LooseVersion(v1)._cmp(LooseVersion(v2))
            assert res == wanted, 'cmp({}, {}) should be {}, got {}'.format(
                v1, v2, wanted, res
            )
            res = LooseVersion(v1)._cmp(v2)
            assert res == wanted, 'cmp({}, {}) should be {}, got {}'.format(
                v1, v2, wanted, res
            )
            res = LooseVersion(v1)._cmp(object())
            assert (
                res is NotImplemented
            ), 'cmp({}, {}) should be NotImplemented, got {}'.format(v1, v2, res)