dulwich-users team mailing list archive
-
dulwich-users team
-
Mailing list archive
-
Message #00171
[PATCH 5/7] More flexible version checking for compat tests.
From: Dave Borowitz <dborowitz@xxxxxxxxxx>
We now check up to 4 components of the version number (e.g. 1.7.0.2).
Reorganized to make future changes easier. Added tests.
Change-Id: Id2f0bd5ba01cb77724d399d9c3a11af695eb2707
---
NEWS | 2 +
dulwich/tests/compat/test_utils.py | 91 ++++++++++++++++++++++++++++++++++++
dulwich/tests/compat/utils.py | 46 +++++++++++++-----
3 files changed, 126 insertions(+), 13 deletions(-)
create mode 100644 dulwich/tests/compat/test_utils.py
diff --git a/NEWS b/NEWS
index d29171c..d010150 100644
--- a/NEWS
+++ b/NEWS
@@ -73,6 +73,8 @@
* Quiet logging output from web tests. (Dave Borowitz)
+ * More flexible version checking for compat tests. (Dave Borowitz)
+
CLEANUP
* Clean up file headers. (Dave Borowitz)
diff --git a/dulwich/tests/compat/test_utils.py b/dulwich/tests/compat/test_utils.py
new file mode 100644
index 0000000..5e77d98
--- /dev/null
+++ b/dulwich/tests/compat/test_utils.py
@@ -0,0 +1,91 @@
+# test_utils.py -- Tests for git compatibility utilities
+# Copyright (C) 2010 Google, Inc.
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor,
+# Boston, MA 02110-1301, USA.
+
+"""Tests for git compatibility utilities."""
+
+from unittest import TestCase
+
+from dulwich.tests import (
+ TestSkipped,
+ )
+import utils
+
+
+class GitVersionTests(TestCase):
+
+ def setUp(self):
+ self._orig_run_git = utils.run_git
+ self._version_str = None # tests can override to set stub version
+
+ def run_git(args, **unused_kwargs):
+ self.assertEqual(['--version'], args)
+ return 0, self._version_str
+ utils.run_git = run_git
+
+ def tearDown(self):
+ utils.run_git = self._orig_run_git
+
+ def test_git_version_none(self):
+ self._version_str = 'not a git version'
+ self.assertEqual(None, utils.git_version())
+
+ def test_git_version_3(self):
+ self._version_str = 'git version 1.6.6'
+ self.assertEqual((1, 6, 6, 0), utils.git_version())
+
+ def test_git_version_4(self):
+ self._version_str = 'git version 1.7.0.2'
+ self.assertEqual((1, 7, 0, 2), utils.git_version())
+
+ def test_git_version_extra(self):
+ self._version_str = 'git version 1.7.0.3.295.gd8fa2'
+ self.assertEqual((1, 7, 0, 3), utils.git_version())
+
+ def assertRequireSucceeds(self, required_version):
+ try:
+ utils.require_git_version(required_version)
+ except TestSkipped:
+ self.fail()
+
+ def assertRequireFails(self, required_version):
+ self.assertRaises(TestSkipped, utils.require_git_version,
+ required_version)
+
+ def test_require_git_version(self):
+ try:
+ self._version_str = 'git version 1.6.6'
+ self.assertRequireSucceeds((1, 6, 6))
+ self.assertRequireSucceeds((1, 6, 6, 0))
+ self.assertRequireSucceeds((1, 6, 5))
+ self.assertRequireSucceeds((1, 6, 5, 99))
+ self.assertRequireFails((1, 7, 0))
+ self.assertRequireFails((1, 7, 0, 2))
+ self.assertRaises(ValueError, utils.require_git_version,
+ (1, 6, 6, 0, 0))
+
+ self._version_str = 'git version 1.7.0.2'
+ self.assertRequireSucceeds((1, 6, 6))
+ self.assertRequireSucceeds((1, 6, 6, 0))
+ self.assertRequireSucceeds((1, 7, 0))
+ self.assertRequireSucceeds((1, 7, 0, 2))
+ self.assertRequireFails((1, 7, 0, 3))
+ self.assertRequireFails((1, 7, 1))
+ except TestSkipped, e:
+ # This test is designed to catch all TestSkipped exceptions.
+ self.fail('Test unexpectedly skipped: %s' % e)
diff --git a/dulwich/tests/compat/utils.py b/dulwich/tests/compat/utils.py
index 50f1064..0fcb23f 100644
--- a/dulwich/tests/compat/utils.py
+++ b/dulwich/tests/compat/utils.py
@@ -35,6 +35,7 @@ from dulwich.tests import (
)
_DEFAULT_GIT = 'git'
+_VERSION_LEN = 4
def git_version(git_path=_DEFAULT_GIT):
@@ -42,8 +43,8 @@ def git_version(git_path=_DEFAULT_GIT):
:param git_path: Path to the git executable; defaults to the version in
the system path.
- :return: A tuple of ints of the form (major, minor, point), or None if no
- git installation was found.
+ :return: A tuple of ints of the form (major, minor, point, sub-point), or
+ None if no git installation was found.
"""
try:
_, output = run_git(['--version'], git_path=git_path,
@@ -53,21 +54,40 @@ def git_version(git_path=_DEFAULT_GIT):
version_prefix = 'git version '
if not output.startswith(version_prefix):
return None
- output = output[len(version_prefix):]
- nums = output.split('.')
- if len(nums) == 2:
- nums.add('0')
- else:
- nums = nums[:3]
- try:
- return tuple(int(x) for x in nums)
- except ValueError:
- return None
+
+ parts = output[len(version_prefix):].split('.')
+ nums = []
+ for part in parts:
+ try:
+ nums.append(int(part))
+ except ValueError:
+ break
+
+ while len(nums) < _VERSION_LEN:
+ nums.append(0)
+ return tuple(nums[:_VERSION_LEN])
def require_git_version(required_version, git_path=_DEFAULT_GIT):
- """Require git version >= version, or skip the calling test."""
+ """Require git version >= version, or skip the calling test.
+
+ :param required_version: A tuple of ints of the form (major, minor, point,
+ sub-point); ommitted components default to 0.
+ :param git_path: Path to the git executable; defaults to the version in
+ the system path.
+ :raise ValueError: if the required version tuple has too many parts.
+ :raise TestSkipped: if no suitable git version was found at the given path.
+ """
found_version = git_version(git_path=git_path)
+ if len(required_version) > _VERSION_LEN:
+ raise ValueError('Invalid version tuple %s, expected %i parts' %
+ (required_version, _VERSION_LEN))
+
+ required_version = list(required_version)
+ while len(found_version) < len(required_version):
+ required_version.append(0)
+ required_version = tuple(required_version)
+
if found_version < required_version:
required_version = '.'.join(map(str, required_version))
found_version = '.'.join(map(str, found_version))
--
1.7.1
References