[Reproducible-commits] [diffoscope] 01/17: Remove unified_diff from Difference constructor

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 a8ac3d1056ddeb5a1d66dd3b3b6d9725f052e911
Author: Jérémy Bobbio <lunar at debian.org>
Date:   Tue Dec 8 16:32:49 2015 +0100

    Remove unified_diff from Difference constructor
    
    It's been a while since we have Difference objects existing without an
    associated unified diff: when they solely carry details. Let's clean the
    code by removing `unified_diff` from the constructor. Instead, it can now be
    recorded through a regular setter.
    
    In the future, we might want to represent differences in more ways that
    unified diffs (e.g. images or direct byte comparisons).
---
 diffoscope/comparators/__init__.py  |  2 +-
 diffoscope/comparators/binary.py    |  8 ++------
 diffoscope/comparators/deb.py       |  2 +-
 diffoscope/comparators/directory.py |  4 ++--
 diffoscope/comparators/text.py      |  2 +-
 diffoscope/difference.py            | 32 ++++++++++++++++++++++----------
 tests/comparators/test_binary.py    |  2 +-
 7 files changed, 30 insertions(+), 22 deletions(-)

diff --git a/diffoscope/comparators/__init__.py b/diffoscope/comparators/__init__.py
index 61d673f..4add2af 100644
--- a/diffoscope/comparators/__init__.py
+++ b/diffoscope/comparators/__init__.py
@@ -111,7 +111,7 @@ def compare_commented_files(file1, file2, comment=None, source=None):
     difference = compare_files(file1, file2, source=source)
     if comment:
         if difference is None:
-            difference = Difference(None, file1.name, file2.name)
+            difference = Difference(file1.name, file2.name)
         difference.add_comment(comment)
     return difference
 
diff --git a/diffoscope/comparators/binary.py b/diffoscope/comparators/binary.py
index 733f1ba..aad13d6 100644
--- a/diffoscope/comparators/binary.py
+++ b/diffoscope/comparators/binary.py
@@ -165,11 +165,7 @@ class File(object, metaclass=ABCMeta):
             details.extend(filter(None, self.compare_details(other, source)))
         if self.as_container:
             details.extend(filter(None, self.as_container.compare(other.as_container)))
-        if not details:
-            return None
-        difference = Difference(None, self.name, other.name, source=source)
-        difference.add_details(details)
-        return difference
+        return Difference.from_details(self.name, other.name, details, source=source)
 
     @tool_required('cmp')
     def has_same_content_as(self, other):
@@ -282,7 +278,7 @@ class NonExistingFile(File):
         # perform a meaningful comparison right here. So we are good do the comparison backward
         # (where knowledge of the file format lies) and and then reverse it.
         if isinstance(other, NonExistingFile):
-            return Difference(None, self.name, other.name, comment='Trying to compare two non-existing files.')
+            return Difference(self.name, other.name, comment='Trying to compare two non-existing files.')
         logger.debug('Performing backward comparison')
         backward_diff = other.compare(self, source)
         if not backward_diff:
diff --git a/diffoscope/comparators/deb.py b/diffoscope/comparators/deb.py
index 8448d4b..9ebe557 100644
--- a/diffoscope/comparators/deb.py
+++ b/diffoscope/comparators/deb.py
@@ -80,7 +80,7 @@ class Md5sumsFile(File):
             return set()
 
     def compare(self, other, source=None):
-        return Difference(None, self.path, other.path, source='md5sums',
+        return Difference(self.path, other.path, source='md5sums',
                           comment="Files in package differs")
 
 
diff --git a/diffoscope/comparators/directory.py b/diffoscope/comparators/directory.py
index cfabff8..c538fb9 100644
--- a/diffoscope/comparators/directory.py
+++ b/diffoscope/comparators/directory.py
@@ -140,13 +140,13 @@ class FilesystemDirectory(object):
                                    my_file, other_file, source=name)
             meta_differences = compare_meta(my_file.name, other_file.name)
             if meta_differences and not inner_difference:
-                inner_difference = Difference(None, my_file.path, other_file.path)
+                inner_difference = Difference(my_file.path, other_file.path)
             if inner_difference:
                 inner_difference.add_details(meta_differences)
                 differences.append(inner_difference)
         if not differences:
             return None
-        difference = Difference(None, self.path, other.path, source)
+        difference = Difference(self.path, other.path, source)
         difference.add_details(differences)
         return difference
 
diff --git a/diffoscope/comparators/text.py b/diffoscope/comparators/text.py
index a48a50e..1ac4718 100644
--- a/diffoscope/comparators/text.py
+++ b/diffoscope/comparators/text.py
@@ -45,7 +45,7 @@ class TextFile(File):
                 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(None, self.path, other.path, source)
+                        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 554e29e..870cb63 100644
--- a/diffoscope/difference.py
+++ b/diffoscope/difference.py
@@ -261,14 +261,14 @@ def diff(feeder1, feeder2):
 
 
 class Difference(object):
-    def __init__(self, unified_diff, path1, path2, source=None, comment=None):
+    def __init__(self, path1, path2, source=None, comment=None):
         self._comments = []
         if comment:
             if type(comment) is list:
                 self._comments.extend(comment)
             else:
                 self._comments.append(comment)
-        self._unified_diff = unified_diff
+        self._unified_diff = None
         # allow to override declared file paths, useful when comparing
         # tempfiles
         if source:
@@ -287,17 +287,17 @@ class Difference(object):
 
     @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
-            return Difference(unified_diff, path1, path2, source, comment)
+            difference.unified_diff = unified_diff
         except RequiredToolNotFound:
-            difference = Difference(None, path1, path2, source)
             difference.add_comment('diff is not available!')
-            if comment:
-                difference.add_comment(comment)
-            return difference
+        if comment:
+            difference.add_comment(comment)
+        return difference
 
     @staticmethod
     def from_text(content1, content2, *args, **kwargs):
@@ -349,6 +349,14 @@ class Difference(object):
             difference.add_comment(command2.stderr_content)
         return difference
 
+    @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
+
     @property
     def comment(self):
         return '\n'.join(self._comments)
@@ -373,6 +381,10 @@ class Difference(object):
     def unified_diff(self):
         return self._unified_diff
 
+    @unified_diff.setter
+    def unified_diff(self, value):
+        self._unified_diff = value
+
     @property
     def details(self):
         return self._details
@@ -383,12 +395,12 @@ class Difference(object):
         self._details.extend(differences)
 
     def get_reverse(self):
+        difference = Difference(None, None, source=[self._source2, self._source1], comment=self._comments)
         if self._unified_diff is None:
-            unified_diff = None
+            difference.unified_diff = None
         else:
-            unified_diff = reverse_unified_diff(self._unified_diff)
+            difference.unified_diff = reverse_unified_diff(self._unified_diff)
         logger.debug('reverse orig %s %s', self._source1, self._source2)
-        difference = Difference(unified_diff, None, None, source=[self._source2, self._source1], comment=self._comments)
         difference.add_details([d.get_reverse() for d in self._details])
         return difference
 
diff --git a/tests/comparators/test_binary.py b/tests/comparators/test_binary.py
index 34031b2..e2ffd47 100644
--- a/tests/comparators/test_binary.py
+++ b/tests/comparators/test_binary.py
@@ -92,7 +92,7 @@ def test_compare_without_xxd(xxd_not_found, binary1, binary2):
     assert difference.unified_diff == expected_diff
 
 def test_with_compare_details():
-    d = Difference('diff', TEST_FILE1_PATH, TEST_FILE2_PATH, source='source')
+    d = Difference(TEST_FILE1_PATH, TEST_FILE2_PATH, source='source', comment='mock')
     class MockFile(FilesystemFile):
         def compare_details(self, other, source=None):
             return [d]

-- 
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