1# Copyright (C) 2010 Chris Jerdonek (cjerdonek@webkit.org)
2#
3# Redistribution and use in source and binary forms, with or without
4# modification, are permitted provided that the following conditions
5# are met:
6# 1.  Redistributions of source code must retain the above copyright
7#     notice, this list of conditions and the following disclaimer.
8# 2.  Redistributions in binary form must reproduce the above copyright
9#     notice, this list of conditions and the following disclaimer in the
10#     documentation and/or other materials provided with the distribution.
11#
12# THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND
13# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
14# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15# DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR
16# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
17# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
18# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
19# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
20# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
21# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22
23"""Contains unit tests for versioning.py."""
24
25import logging
26import unittest
27
28from webkitpy.common.system.logtesting import LogTesting
29from webkitpy.python24.versioning import check_version
30from webkitpy.python24.versioning import compare_version
31
32class MockSys(object):
33
34    """A mock sys module for passing to version-checking methods."""
35
36    def __init__(self, current_version):
37        """Create an instance.
38
39        current_version: A version string with major, minor, and micro
40                         version parts.
41
42        """
43        version_info = current_version.split(".")
44        version_info = map(int, version_info)
45
46        self.version = current_version + " Version details."
47        self.version_info = version_info
48
49
50class CompareVersionTest(unittest.TestCase):
51
52    """Tests compare_version()."""
53
54    def _mock_sys(self, current_version):
55        return MockSys(current_version)
56
57    def test_default_minimum_version(self):
58        """Test the configured minimum version that webkitpy supports."""
59        (comparison, current_version, min_version) = compare_version()
60        self.assertEquals(min_version, "2.5")
61
62    def compare_version(self, target_version, current_version=None):
63        """Call compare_version()."""
64        if current_version is None:
65            current_version = "2.5.3"
66        mock_sys = self._mock_sys(current_version)
67        return compare_version(mock_sys, target_version)
68
69    def compare(self, target_version, current_version=None):
70        """Call compare_version(), and return the comparison."""
71        return self.compare_version(target_version, current_version)[0]
72
73    def test_returned_current_version(self):
74        """Test the current_version return value."""
75        current_version = self.compare_version("2.5")[1]
76        self.assertEquals(current_version, "2.5.3")
77
78    def test_returned_target_version(self):
79        """Test the current_version return value."""
80        target_version = self.compare_version("2.5")[2]
81        self.assertEquals(target_version, "2.5")
82
83    def test_target_version_major(self):
84        """Test major version for target."""
85        self.assertEquals(-1, self.compare("3"))
86        self.assertEquals(0, self.compare("2"))
87        self.assertEquals(1, self.compare("2", "3.0.0"))
88
89    def test_target_version_minor(self):
90        """Test minor version for target."""
91        self.assertEquals(-1, self.compare("2.6"))
92        self.assertEquals(0, self.compare("2.5"))
93        self.assertEquals(1, self.compare("2.4"))
94
95    def test_target_version_micro(self):
96        """Test minor version for target."""
97        self.assertEquals(-1, self.compare("2.5.4"))
98        self.assertEquals(0, self.compare("2.5.3"))
99        self.assertEquals(1, self.compare("2.5.2"))
100
101
102class CheckVersionTest(unittest.TestCase):
103
104    """Tests check_version()."""
105
106    def setUp(self):
107        self._log = LogTesting.setUp(self)
108
109    def tearDown(self):
110        self._log.tearDown()
111
112    def _check_version(self, minimum_version):
113        """Call check_version()."""
114        mock_sys = MockSys("2.5.3")
115        return check_version(sysmodule=mock_sys, target_version=minimum_version)
116
117    def test_true_return_value(self):
118        """Test the configured minimum version that webkitpy supports."""
119        is_current = self._check_version("2.4")
120        self.assertEquals(True, is_current)
121        self._log.assertMessages([])  # No warning was logged.
122
123    def test_false_return_value(self):
124        """Test the configured minimum version that webkitpy supports."""
125        is_current = self._check_version("2.6")
126        self.assertEquals(False, is_current)
127        expected_message = ('WARNING: WebKit Python scripts do not support '
128                            'your current Python version (2.5.3).  '
129                            'The minimum supported version is 2.6.\n  '
130                            'See the following page to upgrade your Python '
131                            'version:\n\n    '
132                            'http://trac.webkit.org/wiki/PythonGuidelines\n\n')
133        self._log.assertMessages([expected_message])
134
135