[Reproducible-commits] [diffoscope] 05/17: Implement truth value testing for Difference

Jérémy Bobbio lunar at moszumanska.debian.org
Tue Dec 8 18:15:20 UTC 2015


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

lunar pushed a commit to branch pu/parallel2
in repository diffoscope.

commit c8cc880af34f2a4e45660c0411bf6acf89ea802c
Author: Jérémy Bobbio <lunar at debian.org>
Date:   Tue Dec 8 18:07:08 2015 +0100

    Implement truth value testing for Difference
    
    A Difference is now defined true if it as either a unified diff,
    notifications or details. This allows to streamline some part of
    the code and will greatly simplify implementing parallel processing.
---
 diffoscope/comparators/binary.py    | 12 +++---------
 diffoscope/comparators/directory.py |  2 +-
 diffoscope/comparators/text.py      |  2 --
 diffoscope/difference.py            | 16 +++++-----------
 tests/comparators/test_binary.py    |  6 +++---
 tests/comparators/test_bzip2.py     |  2 +-
 tests/comparators/test_cbfs.py      |  2 +-
 tests/comparators/test_cpio.py      |  2 +-
 tests/comparators/test_debian.py    |  4 ++--
 tests/comparators/test_dex.py       |  2 +-
 tests/comparators/test_directory.py |  4 ++--
 tests/comparators/test_elf.py       |  4 ++--
 tests/comparators/test_epub.py      |  2 +-
 tests/comparators/test_fonts.py     |  2 +-
 tests/comparators/test_gettext.py   |  2 +-
 tests/comparators/test_gzip.py      |  2 +-
 tests/comparators/test_image.py     |  2 +-
 tests/comparators/test_ipk.py       |  2 +-
 tests/comparators/test_iso9660.py   |  2 +-
 tests/comparators/test_java.py      |  2 +-
 tests/comparators/test_mono.py      |  2 +-
 tests/comparators/test_pdf.py       |  2 +-
 tests/comparators/test_png.py       |  2 +-
 tests/comparators/test_ppu.py       |  2 +-
 tests/comparators/test_rpm.py       |  2 +-
 tests/comparators/test_sqlite.py    |  2 +-
 tests/comparators/test_squashfs.py  |  2 +-
 tests/comparators/test_tar.py       |  2 +-
 tests/comparators/test_text.py      |  4 ++--
 tests/comparators/test_xz.py        |  2 +-
 tests/comparators/test_zip.py       |  2 +-
 31 files changed, 42 insertions(+), 56 deletions(-)

diff --git a/diffoscope/comparators/binary.py b/diffoscope/comparators/binary.py
index 400b295..bc9ef03 100644
--- a/diffoscope/comparators/binary.py
+++ b/diffoscope/comparators/binary.py
@@ -162,9 +162,9 @@ class File(object, metaclass=ABCMeta):
     def _compare_using_details(self, other, source):
         details = []
         if hasattr(self, 'compare_details'):
-            details.extend(filter(None, self.compare_details(other, source)))
+            details.extend(self.compare_details(other, source))
         if self.as_container:
-            details.extend(filter(None, self.as_container.compare(other.as_container)))
+            details.extend(self.as_container.compare(other.as_container))
         return Difference.from_details(self.name, other.name, details, source=source)
 
     @tool_required('cmp')
@@ -187,10 +187,8 @@ class File(object, metaclass=ABCMeta):
             try:
                 difference = self._compare_using_details(other, source)
                 # no differences detected inside? let's at least do a binary diff
-                if difference is None:
+                if not difference:
                     difference = self.compare_bytes(other, source=source)
-                    if difference is None:
-                        return None
                     difference.add_comment("No differences found inside, yet data differs")
             except subprocess.CalledProcessError as e:
                 difference = self.compare_bytes(other, source=source)
@@ -199,14 +197,10 @@ class File(object, metaclass=ABCMeta):
                 else:
                     output = '<none>'
                 cmd = ' '.join(e.cmd)
-                if difference is None:
-                    return None
                 difference.add_notification("Command `%s` exited with %d. Output:\n%s"
                                             % (cmd, e.returncode, output))
             except RequiredToolNotFound as e:
                 difference = self.compare_bytes(other, source=source)
-                if difference is None:
-                    return None
                 difference.add_comment(
                     "'%s' not available in path. Falling back to binary comparison." % e.command)
                 package = e.get_package()
diff --git a/diffoscope/comparators/directory.py b/diffoscope/comparators/directory.py
index c538fb9..4f31637 100644
--- a/diffoscope/comparators/directory.py
+++ b/diffoscope/comparators/directory.py
@@ -88,7 +88,7 @@ def compare_meta(path1, path2):
         differences.append(Difference.from_command(Getfacl, path1, path2))
     except RequiredToolNotFound:
         logger.info("Unable to find 'getfacl'.")
-    return [d for d in differences if d is not None]
+    return filter(None, differences)
 
 
 def compare_directories(path1, path2, source=None):
diff --git a/diffoscope/comparators/text.py b/diffoscope/comparators/text.py
index 1ac4718..42cf1fc 100644
--- a/diffoscope/comparators/text.py
+++ b/diffoscope/comparators/text.py
@@ -44,8 +44,6 @@ class TextFile(File):
                  codecs.open(other.path, 'r', encoding=other_encoding) as other_content:
                 difference = Difference.from_text_readers(my_content, other_content, self.name, other.name, source)
                 if my_encoding != other_encoding:
-                    if difference is None:
-                        difference = Difference(self.path, other.path, source)
                     difference.add_details([Difference.from_text(my_encoding, other_encoding, None, None, source='encoding')])
                 return difference
         except (LookupError, UnicodeDecodeError):
diff --git a/diffoscope/difference.py b/diffoscope/difference.py
index 9562b03..f5a18a9 100644
--- a/diffoscope/difference.py
+++ b/diffoscope/difference.py
@@ -287,14 +287,14 @@ class Difference(object):
     def __repr__(self):
         return '<Difference %s -- %s %s>' % (self._source1, self._source2, self._details)
 
+    def __bool__(self):
+        return self._unified_diff is not None or len(self._notifications) > 0 or len(self._details) > 0
+
     @staticmethod
     def from_feeder(feeder1, feeder2, path1, path2, source=None, comment=None):
         difference = Difference(path1, path2, source)
         try:
-            unified_diff = diff(feeder1, feeder2)
-            if not unified_diff:
-                return None
-            difference.unified_diff = unified_diff
+            difference.unified_diff = diff(feeder1, feeder2)
         except RequiredToolNotFound:
             difference.add_comment('diff is not available!')
         difference.add_comment(comment)
@@ -340,8 +340,6 @@ class Difference(object):
             source_cmd = command1 or command2
             kwargs['source'] = ' '.join(map(lambda x: '{}' if x == source_cmd.path else x, source_cmd.cmdline()))
         difference = Difference.from_feeder(feeder1, feeder2, path1, path2, *args, **kwargs)
-        if not difference:
-            return None
         if command1 and command1.stderr_content:
             difference.add_comment('stderr from `%s`:' % ' '.join(command1.cmdline()))
             difference.add_comment(command1.stderr_content)
@@ -352,8 +350,6 @@ class Difference(object):
 
     @staticmethod
     def from_details(path1, path2, details, source=None):
-        if not details:
-            return None
         d = Difference(path1, path2, source)
         d.add_details(details)
         return d
@@ -406,9 +402,7 @@ class Difference(object):
         return self._details
 
     def add_details(self, differences):
-        if len([d for d in differences if type(d) is not Difference]) > 0:
-            raise TypeError("'differences' must contains Difference objects'")
-        self._details.extend(differences)
+        self._details.extend(filter(None, differences))
 
     def get_reverse(self):
         difference = Difference(None, None, source=[self._source2, self._source1], comment=self._comments)
diff --git a/tests/comparators/test_binary.py b/tests/comparators/test_binary.py
index af0cdbb..d1273f4 100644
--- a/tests/comparators/test_binary.py
+++ b/tests/comparators/test_binary.py
@@ -64,7 +64,7 @@ def test_guess_encoding_iso8859():
 
 def test_no_differences_with_xxd(binary1):
     difference = binary1.compare_bytes(binary1)
-    assert difference is None
+    assert not difference
 
 @pytest.mark.skipif(tool_missing('xxd'), reason='missing xxd')
 def test_compare_with_xxd(binary1, binary2):
@@ -84,7 +84,7 @@ def xxd_not_found(monkeypatch):
 
 def test_no_differences_without_xxd(xxd_not_found, binary1):
     difference = binary1.compare_bytes(binary1)
-    assert difference is None
+    assert not difference
 
 def test_compare_without_xxd(xxd_not_found, binary1, binary2):
     difference = binary1.compare(binary2)
@@ -114,7 +114,7 @@ def test_with_compare_details_and_no_actual_differences():
         def compare_details(self, other, source=None):
             return []
     difference = MockFile(TEST_FILE1_PATH).compare(MockFile(TEST_FILE1_PATH))
-    assert difference is None
+    assert not difference
 
 @pytest.mark.skipif(tool_missing('xxd'), reason='missing xxd')
 def test_with_compare_details_and_failed_process():
diff --git a/tests/comparators/test_bzip2.py b/tests/comparators/test_bzip2.py
index 6b0a9c2..946b444 100644
--- a/tests/comparators/test_bzip2.py
+++ b/tests/comparators/test_bzip2.py
@@ -44,7 +44,7 @@ def test_identification(bzip1):
 
 def test_no_differences(bzip1):
     difference = bzip1.compare(bzip1)
-    assert difference is None
+    assert not difference
 
 @pytest.fixture
 def differences(bzip1, bzip2):
diff --git a/tests/comparators/test_cbfs.py b/tests/comparators/test_cbfs.py
index 1850f76..f9b8646 100644
--- a/tests/comparators/test_cbfs.py
+++ b/tests/comparators/test_cbfs.py
@@ -65,7 +65,7 @@ def test_identification_without_offset(rom2):
 @pytest.mark.skipif(tool_missing('cbfstool'), reason='missing cbfstool')
 def test_no_differences(rom1):
     difference = rom1.compare(rom1)
-    assert difference is None
+    assert not difference
 
 @pytest.fixture
 def differences(rom1, rom2):
diff --git a/tests/comparators/test_cpio.py b/tests/comparators/test_cpio.py
index 7fc0853..869855a 100644
--- a/tests/comparators/test_cpio.py
+++ b/tests/comparators/test_cpio.py
@@ -41,7 +41,7 @@ def test_identification(cpio1):
 
 def test_no_differences(cpio1):
     difference = cpio1.compare(cpio1)
-    assert difference is None
+    assert not difference
 
 @pytest.fixture
 def differences(cpio1, cpio2):
diff --git a/tests/comparators/test_debian.py b/tests/comparators/test_debian.py
index f839930..07d89d0 100644
--- a/tests/comparators/test_debian.py
+++ b/tests/comparators/test_debian.py
@@ -66,7 +66,7 @@ def test_dot_changes_invalid(tmpdir):
 
 def test_dot_changes_no_differences(dot_changes1):
     difference = dot_changes1.compare(dot_changes1)
-    assert difference is None
+    assert not difference
 
 @pytest.fixture
 def dot_changes_differences(dot_changes1, dot_changes2):
@@ -127,7 +127,7 @@ def test_dot_dsc_invalid(tmpdir, dot_dsc2):
 
 def test_dot_dsc_no_differences(dot_dsc1):
     difference = dot_dsc1.compare(dot_dsc1)
-    assert difference is None
+    assert not difference
 
 @pytest.fixture
 def dot_dsc_differences(dot_dsc1, dot_dsc2):
diff --git a/tests/comparators/test_dex.py b/tests/comparators/test_dex.py
index b815096..038e6ca 100644
--- a/tests/comparators/test_dex.py
+++ b/tests/comparators/test_dex.py
@@ -41,7 +41,7 @@ def test_identification(dex1):
 
 def test_no_differences(dex1):
     difference = dex1.compare(dex1)
-    assert difference is None
+    assert not difference
 
 @pytest.fixture
 def differences(dex1, dex2):
diff --git a/tests/comparators/test_directory.py b/tests/comparators/test_directory.py
index d1008f7..3eb9503 100644
--- a/tests/comparators/test_directory.py
+++ b/tests/comparators/test_directory.py
@@ -29,11 +29,11 @@ TEST_FILE2_PATH = os.path.join(os.path.dirname(__file__), '../data/text_ascii2')
 
 def test_no_differences():
     difference = compare_directories(os.path.dirname(__file__), os.path.dirname(__file__))
-    assert difference is None
+    assert not difference
 
 def test_no_differences_with_extra_slash():
     difference = compare_directories(os.path.dirname(__file__) + '/', os.path.dirname(__file__))
-    assert difference is None
+    assert not difference
 
 @pytest.fixture
 def differences(tmpdir):
diff --git a/tests/comparators/test_elf.py b/tests/comparators/test_elf.py
index 33d6261..a8ca280 100644
--- a/tests/comparators/test_elf.py
+++ b/tests/comparators/test_elf.py
@@ -41,7 +41,7 @@ def test_obj_identification(obj1):
 
 def test_obj_no_differences(obj1):
     difference = obj1.compare(obj1)
-    assert difference is None
+    assert not difference
 
 @pytest.fixture
 def obj_differences(obj1, obj2):
@@ -75,7 +75,7 @@ def test_lib_identification(lib1):
 
 def test_lib_no_differences(lib1):
     difference = lib1.compare(lib1)
-    assert difference is None
+    assert not difference
 
 @pytest.fixture
 def lib_differences(lib1, lib2):
diff --git a/tests/comparators/test_epub.py b/tests/comparators/test_epub.py
index 80134e1..b7e72e5 100644
--- a/tests/comparators/test_epub.py
+++ b/tests/comparators/test_epub.py
@@ -41,7 +41,7 @@ def test_identification(epub1):
 
 def test_no_differences(epub1):
     difference = epub1.compare(epub1)
-    assert difference is None
+    assert not difference
 
 @pytest.fixture
 def differences(epub1, epub2):
diff --git a/tests/comparators/test_fonts.py b/tests/comparators/test_fonts.py
index 317eb82..9b9de87 100644
--- a/tests/comparators/test_fonts.py
+++ b/tests/comparators/test_fonts.py
@@ -41,7 +41,7 @@ def test_identification(ttf1):
 
 def test_no_differences(ttf1):
     difference = ttf1.compare(ttf1)
-    assert difference is None
+    assert not difference
 
 @pytest.fixture
 def differences(ttf1, ttf2):
diff --git a/tests/comparators/test_gettext.py b/tests/comparators/test_gettext.py
index e6fa8b7..32e4654 100644
--- a/tests/comparators/test_gettext.py
+++ b/tests/comparators/test_gettext.py
@@ -42,7 +42,7 @@ def test_identification(mo1):
 
 def test_no_differences(mo1):
     difference = mo1.compare(mo1)
-    assert difference is None
+    assert not difference
 
 @pytest.fixture
 def differences(mo1, mo2):
diff --git a/tests/comparators/test_gzip.py b/tests/comparators/test_gzip.py
index 038398f..b6aab9b 100644
--- a/tests/comparators/test_gzip.py
+++ b/tests/comparators/test_gzip.py
@@ -41,7 +41,7 @@ def test_identification(gzip1):
 
 def test_no_differences(gzip1):
     difference = gzip1.compare(gzip1)
-    assert difference is None
+    assert not difference
 
 @pytest.fixture
 def differences(gzip1, gzip2):
diff --git a/tests/comparators/test_image.py b/tests/comparators/test_image.py
index abab26e..c9d7c93 100644
--- a/tests/comparators/test_image.py
+++ b/tests/comparators/test_image.py
@@ -42,7 +42,7 @@ def test_identification(image1):
 
 def test_no_differences(image1):
     difference = image1.compare(image1)
-    assert difference is None
+    assert not difference
 
 @pytest.fixture
 def differences(image1, image2):
diff --git a/tests/comparators/test_ipk.py b/tests/comparators/test_ipk.py
index 1cf6c12..276fe56 100644
--- a/tests/comparators/test_ipk.py
+++ b/tests/comparators/test_ipk.py
@@ -40,7 +40,7 @@ def test_identification(ipk1):
 
 def test_no_differences(ipk1):
     difference = ipk1.compare(ipk1)
-    assert difference is None
+    assert not difference
 
 @pytest.fixture
 def differences(ipk1, ipk2):
diff --git a/tests/comparators/test_iso9660.py b/tests/comparators/test_iso9660.py
index 5d5fa6d..07dcb12 100644
--- a/tests/comparators/test_iso9660.py
+++ b/tests/comparators/test_iso9660.py
@@ -41,7 +41,7 @@ def test_identification(iso1):
 
 def test_no_differences(iso1):
     difference = iso1.compare(iso1)
-    assert difference is None
+    assert not difference
 
 @pytest.fixture
 def differences(iso1, iso2):
diff --git a/tests/comparators/test_java.py b/tests/comparators/test_java.py
index 29f5949..3b46585 100644
--- a/tests/comparators/test_java.py
+++ b/tests/comparators/test_java.py
@@ -41,7 +41,7 @@ def test_identification(class1):
 
 def test_no_differences(class1):
     difference = class1.compare(class1)
-    assert difference is None
+    assert not difference
 
 @pytest.fixture
 def differences(class1, class2):
diff --git a/tests/comparators/test_mono.py b/tests/comparators/test_mono.py
index d7933c9..04680fd 100644
--- a/tests/comparators/test_mono.py
+++ b/tests/comparators/test_mono.py
@@ -46,7 +46,7 @@ def test_identification(exe1):
 
 def test_no_differences(exe1):
     difference = exe1.compare(exe1)
-    assert difference is None
+    assert not difference
 
 @pytest.fixture
 def differences(exe1, exe2):
diff --git a/tests/comparators/test_pdf.py b/tests/comparators/test_pdf.py
index 1dc3972..67f1af3 100644
--- a/tests/comparators/test_pdf.py
+++ b/tests/comparators/test_pdf.py
@@ -41,7 +41,7 @@ def test_identification(pdf1):
 
 def test_no_differences(pdf1):
     difference = pdf1.compare(pdf1)
-    assert difference is None
+    assert not difference
 
 @pytest.fixture
 def differences(pdf1, pdf2):
diff --git a/tests/comparators/test_png.py b/tests/comparators/test_png.py
index 6cf4089..d3051b2 100644
--- a/tests/comparators/test_png.py
+++ b/tests/comparators/test_png.py
@@ -41,7 +41,7 @@ def test_identification(png1):
 
 def test_no_differences(png1):
     difference = png1.compare(png1)
-    assert difference is None
+    assert not difference
 
 @pytest.fixture
 def differences(png1, png2):
diff --git a/tests/comparators/test_ppu.py b/tests/comparators/test_ppu.py
index 6326946..40dbd95 100644
--- a/tests/comparators/test_ppu.py
+++ b/tests/comparators/test_ppu.py
@@ -49,7 +49,7 @@ def test_identification(file1):
 
 def test_no_differences(file1):
     difference = file1.compare(file1)
-    assert difference is None
+    assert not difference
 
 @pytest.fixture
 def differences(file1, file2):
diff --git a/tests/comparators/test_rpm.py b/tests/comparators/test_rpm.py
index 5e9a715..501ce85 100644
--- a/tests/comparators/test_rpm.py
+++ b/tests/comparators/test_rpm.py
@@ -47,7 +47,7 @@ def test_identification(rpm1):
 @pytest.mark.skipif(miss_rpm_module, reason='rpm module is not installed')
 def test_no_differences(rpm1):
     difference = rpm1.compare(rpm1)
-    assert difference is None
+    assert not difference
 
 @pytest.fixture
 def differences(rpm1, rpm2):
diff --git a/tests/comparators/test_sqlite.py b/tests/comparators/test_sqlite.py
index 4ff3a63..1076cc8 100644
--- a/tests/comparators/test_sqlite.py
+++ b/tests/comparators/test_sqlite.py
@@ -41,7 +41,7 @@ def test_identification(sqlite3db1):
 
 def test_no_differences(sqlite3db1):
     difference = sqlite3db1.compare(sqlite3db1)
-    assert difference is None
+    assert not difference
 
 @pytest.fixture
 def differences(sqlite3db1, sqlite3db2):
diff --git a/tests/comparators/test_squashfs.py b/tests/comparators/test_squashfs.py
index 25a3fc8..c735c0d 100644
--- a/tests/comparators/test_squashfs.py
+++ b/tests/comparators/test_squashfs.py
@@ -42,7 +42,7 @@ def test_identification(squashfs1):
 
 def test_no_differences(squashfs1):
     difference = squashfs1.compare(squashfs1)
-    assert difference is None
+    assert not difference
 
 def test_no_warnings(capfd, squashfs1, squashfs2):
     _ = squashfs1.compare(squashfs2)
diff --git a/tests/comparators/test_tar.py b/tests/comparators/test_tar.py
index 78a7ff6..e432cce 100644
--- a/tests/comparators/test_tar.py
+++ b/tests/comparators/test_tar.py
@@ -40,7 +40,7 @@ def test_identification(tar1):
 
 def test_no_differences(tar1):
     difference = tar1.compare(tar1)
-    assert difference is None
+    assert not difference
 
 @pytest.fixture
 def differences(tar1, tar2):
diff --git a/tests/comparators/test_text.py b/tests/comparators/test_text.py
index 147a906..ab47045 100644
--- a/tests/comparators/test_text.py
+++ b/tests/comparators/test_text.py
@@ -34,11 +34,11 @@ def ascii2():
 
 def test_no_differences(ascii1):
     difference = ascii1.compare(ascii1)
-    assert difference is None
+    assert not difference
 
 def test_difference_in_ascii(ascii1, ascii2):
     difference = ascii1.compare(ascii2)
-    assert difference is not None
+    assert difference
     expected_diff = open(os.path.join(os.path.dirname(__file__), '../data/text_ascii_expected_diff')).read()
     assert difference.unified_diff == expected_diff
     assert not difference.comments
diff --git a/tests/comparators/test_xz.py b/tests/comparators/test_xz.py
index a940ffd..494cc33 100644
--- a/tests/comparators/test_xz.py
+++ b/tests/comparators/test_xz.py
@@ -42,7 +42,7 @@ def test_identification(xz1):
 
 def test_no_differences(xz1):
     difference = xz1.compare(xz1)
-    assert difference is None
+    assert not difference
 
 @pytest.fixture
 def differences(xz1, xz2):
diff --git a/tests/comparators/test_zip.py b/tests/comparators/test_zip.py
index d921b79..3396037 100644
--- a/tests/comparators/test_zip.py
+++ b/tests/comparators/test_zip.py
@@ -41,7 +41,7 @@ def test_identification(zip1):
 
 def test_no_differences(zip1):
     difference = zip1.compare(zip1)
-    assert difference is None
+    assert not difference
 
 @pytest.fixture
 def differences(zip1, zip2):

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



More information about the Reproducible-commits mailing list