Statistics
| Branch: | Tag: | Revision:

root / devtools / devtools.version_unittest.py @ c9b113ac

History | View | Annotate | Download (4.7 kB)

1
#!/usr/bin/env python
2
#
3
# Copyright 2012 GRNET S.A. All rights reserved.
4
#
5
# Redistribution and use in source and binary forms, with or
6
# without modification, are permitted provided that the following
7
# conditions are met:
8
#
9
#   1. Redistributions of source code must retain the above
10
#      copyright notice, this list of conditions and the following
11
#      disclaimer.
12
#
13
#   2. Redistributions in binary form must reproduce the above
14
#      copyright notice, this list of conditions and the following
15
#      disclaimer in the documentation and/or other materials
16
#      provided with the distribution.
17
#
18
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
19
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
22
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
25
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
# POSSIBILITY OF SUCH DAMAGE.
30
#
31
# The views and conclusions contained in the software and
32
# documentation are those of the authors and should not be
33
# interpreted as representing official policies, either expressed
34
# or implied, of GRNET S.A.
35
#
36
#
37

    
38
"""Unit Tests for devtools.version
39

40
Provides unit tests for module devtools.version,
41
for automatic generation of version strings.
42

43
"""
44

    
45
import os
46
import unittest
47
from pkg_resources import parse_version
48
from version import debian_version_from_python_version
49

    
50

    
51
class DebianVersionObject(object):
52
    """Object representing a Debian Version."""
53
    def __init__(self, pyver):
54
        self.version = debian_version_from_python_version(pyver)
55

    
56
    def __str__(self):
57
        return self.version
58

    
59

    
60
def debian_compare_versions(a, op, b):
61
    i = os.system("dpkg --compare-versions %s %s %s" % (a, op, b))
62
    return i == 0
63

    
64
# Set ordering between DebianVersionObject objects, by adding
65
# debian_compare_versions
66
for op in ["lt", "le", "eq", "ne", "gt", "ge"]:
67
    def gen(op):
68
        def operator_func(self, other):
69
            return debian_compare_versions(self.version, op, other.version)
70
        return operator_func
71
    setattr(DebianVersionObject, "__%s__" % op, gen(op))
72

    
73

    
74
def _random_commit():
75
    import random
76
    import string
77
    return "".join(random.choice(string.hexdigits) for n in xrange(8)).lower()
78

    
79

    
80
# Add a random commit number at the end of snapshot versions
81
def version_with_commit(parse_func, v):
82
    if "_" in v:
83
        return parse_func(v + "_" + _random_commit())
84
    else:
85
        return parse_func(v)
86

    
87
V = lambda v: version_with_commit(parse_version, v)
88
D = lambda v: version_with_commit(DebianVersionObject, v)
89

    
90

    
91
class TestVersionFunctions(unittest.TestCase):
92
    def setUp(self):
93
        self.version_orderings = (
94
            ("0.14next", ">", "0.14"),
95
            ("0.14next", ">", "0.14rc7"),
96
            ("0.14next", "<", "0.14.1"),
97
            ("0.14rc6", "<", "0.14"),
98
            ("0.14.2rc6", ">", "0.14.1"),
99
            ("0.14next_150", "<", "0.14next"),
100
            ("0.14.1next_150", "<", "0.14.1next"),
101
            ("0.14.1_149", "<", "0.14.1"),
102
            ("0.14.1_149", "<", "0.14.1_150"),
103
            ("0.13next_102", "<", "0.13next"),
104
            ("0.13next", "<", "0.14rc5_120"),
105
            ("0.14rc3_120", "<", "0.14rc3"),
106
            # The following test fails, but version.python_version
107
            # will never try to produce such a version:
108
            # ("0.14rc3", "<", "0.14_1"),
109
            ("0.14_120", "<", "0.14"),
110
            ("0.14", "<", "0.14next_20"),
111
            ("0.14next_20", "<", "0.14next"),
112
        )
113

    
114
    def test_python_versions(self):
115
        for a, op, b in self.version_orderings:
116
            res = compare(V, a, op, b)
117
            self.assertTrue(res, "Python version: %s %s %s"
118
                                 " is not True" % (a, op, b))
119

    
120
    def test_debian_versions(self):
121
        for a, op, b in self.version_orderings:
122
            res = compare(D, a, op, b)
123
            self.assertTrue(res, "Debian version %s %s %s"
124
                                 " is not True" % (a, op, b))
125

    
126

    
127
def compare(function, a, op, b):
128
    import operator
129
    str_to_op = {"<": operator.lt,
130
            "<=": operator.le,
131
            "==": operator.eq,
132
            ">": operator.gt,
133
            ">=": operator.ge}
134
    try:
135
        return str_to_op[op](function(a), function(b))
136
    except KeyError:
137
        raise ValueError("Unknown operator '%s'" % op)
138

    
139
if __name__ == '__main__':
140
    unittest.main()