[diffoscope] 02/03: tests: rewrite tool_older_than() into skip_unless_tool_is_older_than()

Mattia Rizzolo mattia at debian.org
Mon Aug 22 15:23:54 CEST 2016


This is an automated email from the git hooks/post-receive script.

mattia pushed a commit to branch master
in repository diffoscope.

commit cf4a4c3b09dbf9a875dee94047fa7bb8a53cdceb
Author: Mattia Rizzolo <mattia at debian.org>
Date:   Mon Aug 22 12:51:12 2016 +0000

    tests: rewrite tool_older_than() into skip_unless_tool_is_older_than()
---
 tests/comparators/test_rlib.py  | 10 +++++++---
 tests/comparators/test_utils.py | 13 ++++++++++++-
 tests/comparators/utils.py      | 19 +++++++++++--------
 3 files changed, 30 insertions(+), 12 deletions(-)

diff --git a/tests/comparators/test_rlib.py b/tests/comparators/test_rlib.py
index 22ff362..1ed07cb 100644
--- a/tests/comparators/test_rlib.py
+++ b/tests/comparators/test_rlib.py
@@ -19,16 +19,20 @@
 # along with diffoscope.  If not, see <http://www.gnu.org/licenses/>.
 
 import pytest
+import subprocess
 
 from diffoscope.comparators.ar import ArFile
 from diffoscope.comparators.utils import diff_ignore_line_numbers
 
-from utils import skip_unless_tools_exist, tool_older_than, data, \
-    load_fixture, assert_non_existing
+from utils import skip_unless_tools_exist, skip_unless_tool_is_older_than, \
+    data, load_fixture, assert_non_existing
 
 rlib1 = load_fixture(data('test1.rlib'))
 rlib2 = load_fixture(data('test2.rlib'))
 
+def llvm_version():
+    return subprocess.check_output(['llvm-config', '--version']).decode("utf-8").strip()
+
 def test_identification(rlib1):
     assert isinstance(rlib1, ArFile)
 
@@ -60,7 +64,7 @@ def test_item2_rust_metadata_bin(differences):
     assert differences[2].source2 == 'rust.metadata.bin'
 
 @skip_unless_tools_exist('llvm-dis')
- at pytest.mark.skipif(tool_older_than(['llvm-config', '--version'], '3.8'), reason='llvm version too low')
+ at skip_unless_tool_is_older_than('llvm-config', llvm_version, '3.8')
 def test_item3_deflate_llvm_bitcode(differences):
     assert differences[3].source1 == 'alloc_system-d16b8f0e.0.bytecode.deflate'
     assert differences[3].source2 == 'alloc_system-d16b8f0e.0.bytecode.deflate'
diff --git a/tests/comparators/test_utils.py b/tests/comparators/test_utils.py
index d723e7e..2cbb4ed 100644
--- a/tests/comparators/test_utils.py
+++ b/tests/comparators/test_utils.py
@@ -24,7 +24,8 @@ from diffoscope.config import Config
 from diffoscope.difference import Difference
 from diffoscope.comparators.utils import Command
 
-from utils import tools_missing, skip_unless_tools_exist, data, load_fixture
+from utils import tools_missing, skip_unless_tools_exist, data, load_fixture, \
+    skip_unless_tool_is_older_than
 
 try:
     import tlsh # noqa
@@ -50,6 +51,16 @@ def test_skip_unless_tools_exist_empty():
 def test_skip_unless_tools_exist_missing():
     pytest.xfail("Test should always be skipped")
 
+def skip_unless_tool_is_older_than():
+    func = skip_unless_tool_is_older_than
+    assert func('/missing', 1, 1).name is 'skip'
+    # pytest.skipif().args[0] contains the evaluated statement
+    assert func('cat', 1, 1).args[0] is False
+    assert func('cat', 1, '1.2d.45+b8').args[0] is True
+    def version():
+        return '4.3-git'
+    assert func('cat', version, '4.3').args[0] is False
+
 @pytest.mark.skipif(miss_tlsh, reason='tlsh is missing')
 def test_fuzzy_matching(fuzzy_tar1, fuzzy_tar2):
     differences = fuzzy_tar1.compare(fuzzy_tar2).details
diff --git a/tests/comparators/utils.py b/tests/comparators/utils.py
index d9e01a7..f8f6399 100644
--- a/tests/comparators/utils.py
+++ b/tests/comparators/utils.py
@@ -20,10 +20,9 @@
 import os
 import pytest
 import diffoscope
-import subprocess
 
 from distutils.spawn import find_executable
-from distutils.version import StrictVersion
+from distutils.version import LooseVersion
 
 from diffoscope.config import Config
 from diffoscope.comparators import specialize
@@ -45,6 +44,16 @@ def skip_unless_tools_exist(*required):
         reason="requires {}".format(" and ".join(required)),
     )
 
+def skip_unless_tool_is_older_than(tool, actual_ver, min_ver, vcls=LooseVersion):
+    if tools_missing(tool):
+        return pytest.mark.skip(reason="requires {}".format(tool))
+    if callable(actual_ver):
+        actual_ver = actual_ver()
+    return pytest.mark.skipif(
+        vcls(str(actual_ver)) < vcls(str(min_ver)),
+        reason="requires {} >= {}".format(tool, min_ver)
+    )
+
 def load_fixture(filename):
     return pytest.fixture(
         lambda: specialize(FilesystemFile(filename))
@@ -57,12 +66,6 @@ def data(filename):
         filename,
     )
 
-def tool_older_than(cmdline, min_ver, vcls=StrictVersion):
-    if find_executable(cmdline[0]) is None:
-        return True
-    actual_ver = subprocess.check_output(cmdline).decode("utf-8").strip()
-    return vcls(actual_ver) < vcls(min_ver)
-
 def assert_non_existing(monkeypatch, fixture, has_null_source=True, has_details=True):
     monkeypatch.setattr(Config.general, 'new_file', True)
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/diffoscope.git


More information about the diffoscope mailing list