[Reproducible-commits] [debbindiff] 05/07: Comparators now return a single Difference
Jérémy Bobbio
lunar at moszumanska.debian.org
Sat Jun 27 08:59:11 UTC 2015
This is an automated email from the git hooks/post-receive script.
lunar pushed a commit to branch master
in repository debbindiff.
commit 2d3625057d18310f056935dd41aa27b8cb4215c2
Author: Jérémy Bobbio <lunar at debian.org>
Date: Fri Jun 26 16:22:28 2015 +0000
Comparators now return a single Difference
The forest approach was often clumsy and ill-specified. Now
comparators are expected to return a single Difference, or None.
To make it easy for comparators who are producing details, a new decorator
`returns_details` will create a wrapping Difference object for free. This
was previously a side-effect of using the `binary_fallback` decorator.
This new decorator will filter None from the list of differences,
removing some boilerplate from the comparators.
---
debbindiff.py | 8 +++---
debbindiff/comparators/__init__.py | 14 +++++-----
debbindiff/comparators/binary.py | 7 ++---
debbindiff/comparators/bzip2.py | 7 +++--
debbindiff/comparators/cpio.py | 11 ++++----
debbindiff/comparators/deb.py | 19 ++++++-------
debbindiff/comparators/directory.py | 25 +++++++----------
debbindiff/comparators/elf.py | 22 ++++++---------
debbindiff/comparators/fonts.py | 8 ++----
debbindiff/comparators/gettext.py | 8 ++----
debbindiff/comparators/gzip.py | 11 ++++----
debbindiff/comparators/haskell.py | 8 ++----
debbindiff/comparators/ipk.py | 11 ++++----
debbindiff/comparators/iso9660.py | 13 ++++-----
debbindiff/comparators/java.py | 8 ++----
debbindiff/comparators/pdf.py | 11 +++-----
debbindiff/comparators/png.py | 8 ++----
debbindiff/comparators/rpm.py | 11 ++++----
debbindiff/comparators/squashfs.py | 13 ++++-----
debbindiff/comparators/tar.py | 11 ++++----
debbindiff/comparators/text.py | 5 +---
debbindiff/comparators/utils.py | 29 ++++++++++++-------
debbindiff/comparators/xz.py | 7 +++--
debbindiff/comparators/zip.py | 8 +++---
debbindiff/difference.py | 5 ++++
debbindiff/presenters/html.py | 5 ++--
debbindiff/presenters/text.py | 11 ++++----
tests/comparators/test_binary.py | 18 ++++++------
tests/comparators/test_bzip2.py | 8 +++---
tests/comparators/test_cpio.py | 6 ++--
tests/comparators/test_deb.py | 6 ++--
tests/comparators/test_directory.py | 17 ++++++------
tests/comparators/test_elf.py | 12 ++++----
tests/comparators/test_fonts.py | 6 ++--
tests/comparators/test_gettext.py | 11 ++++----
tests/comparators/test_gzip.py | 12 ++++----
tests/comparators/test_ipk.py | 6 ++--
tests/comparators/test_iso9660.py | 6 ++--
tests/comparators/test_java.py | 6 ++--
tests/comparators/test_pdf.py | 6 ++--
tests/comparators/test_png.py | 6 ++--
tests/comparators/test_rpm.py | 6 ++--
tests/comparators/test_squashfs.py | 8 +++---
tests/comparators/test_text.py | 25 ++++++++---------
tests/comparators/test_utils.py | 55 +++++++++++++++++++++++--------------
tests/comparators/test_xz.py | 12 ++++----
tests/comparators/test_zip.py | 10 +++----
47 files changed, 263 insertions(+), 283 deletions(-)
diff --git a/debbindiff.py b/debbindiff.py
index 20a6412..01b14f5 100755
--- a/debbindiff.py
+++ b/debbindiff.py
@@ -104,16 +104,16 @@ def main():
if parsed_args.debug:
logger.setLevel(logging.DEBUG)
set_locale()
- differences = debbindiff.comparators.compare_files(
+ difference = debbindiff.comparators.compare_files(
parsed_args.file1, parsed_args.file2)
- if len(differences) > 0:
+ if difference:
if parsed_args.html_output:
with make_printer(parsed_args.html_output) as print_func:
- output_html(differences, css_url=parsed_args.css_url, print_func=print_func,
+ output_html(difference, css_url=parsed_args.css_url, print_func=print_func,
max_page_size=parsed_args.max_report_size)
if (parsed_args.text_output and parsed_args.text_output != parsed_args.html_output) or not parsed_args.html_output:
with make_printer(parsed_args.text_output or '-') as print_func:
- output_text(differences, print_func=print_func)
+ output_text(difference, print_func=print_func)
return 1
return 0
diff --git a/debbindiff/comparators/__init__.py b/debbindiff/comparators/__init__.py
index c50608e..f95743e 100644
--- a/debbindiff/comparators/__init__.py
+++ b/debbindiff/comparators/__init__.py
@@ -49,11 +49,11 @@ except ImportError as ex:
raise
def compare_rpm_files(path1, path2, source=None):
logger.info("Python rpm module not found.")
- differences = compare_binary_files(path1, path2, source)
- if differences:
- differences[0].comment = (differences[0].comment or '') + \
+ difference = compare_binary_files(path1, path2, source)
+ if difference:
+ difference.comment = (difference.comment or '') + \
'\nUnable to find Python rpm module. Falling back to binary comparison.'
- return differences
+ return difference
from debbindiff.comparators.squashfs import compare_squashfs_files
from debbindiff.comparators.text import compare_text_files
from debbindiff.comparators.tar import compare_tar_files
@@ -137,8 +137,8 @@ def compare_files(path1, path2, source=None):
text2 = "[ No symlink ]"
if dest1 and dest2 and dest1 == dest2:
- return []
- return [Difference.from_unicode(text1, text2, path1, path2, source=source, comment="symlink")]
+ return None
+ return Difference.from_unicode(text1, text2, path1, path2, source=source, comment="symlink")
if os.path.isdir(path1) and os.path.isdir(path2):
return compare_directories(path1, path2, source)
@@ -153,7 +153,7 @@ def compare_files(path1, path2, source=None):
size2 = os.path.getsize(path2)
if size1 == size2 and size1 <= SMALL_FILE_THRESHOLD:
if file(path1).read() == file(path2).read():
- return []
+ return None
# ok, let's do the full thing
mime_type1 = guess_mime_type(path1)
mime_type2 = guess_mime_type(path2)
diff --git a/debbindiff/comparators/binary.py b/debbindiff/comparators/binary.py
index 572762e..c085f9e 100644
--- a/debbindiff/comparators/binary.py
+++ b/debbindiff/comparators/binary.py
@@ -49,15 +49,12 @@ def compare_binary_files(path1, path2, source=None):
try:
with xxd(path1) as xxd1:
with xxd(path2) as xxd2:
- difference = Difference.from_file(xxd1, xxd2, path1, path2, source)
+ return Difference.from_file(xxd1, xxd2, path1, path2, source)
except RequiredToolNotFound:
hexdump1 = hexdump_fallback(path1)
hexdump2 = hexdump_fallback(path2)
comment = 'xxd not available in path. Falling back to Python hexlify.\n'
- difference = Difference.from_unicode(hexdump1, hexdump2, path1, path2, source, comment)
- if not difference:
- return []
- return [difference]
+ return Difference.from_unicode(hexdump1, hexdump2, path1, path2, source, comment)
@tool_required('cmp')
diff --git a/debbindiff/comparators/bzip2.py b/debbindiff/comparators/bzip2.py
index 842bb7a..d77a408 100644
--- a/debbindiff/comparators/bzip2.py
+++ b/debbindiff/comparators/bzip2.py
@@ -21,7 +21,7 @@ from contextlib import contextmanager
import os.path
import subprocess
import debbindiff.comparators
-from debbindiff.comparators.utils import binary_fallback, make_temp_directory
+from debbindiff.comparators.utils import binary_fallback, returns_details, make_temp_directory
from debbindiff.difference import get_source
from debbindiff import tool_required
@@ -42,9 +42,10 @@ def decompress_bzip2(path):
@binary_fallback
+ at returns_details
def compare_bzip2_files(path1, path2, source=None):
with decompress_bzip2(path1) as new_path1:
with decompress_bzip2(path2) as new_path2:
- return debbindiff.comparators.compare_files(
+ return [debbindiff.comparators.compare_files(
new_path1, new_path2,
- source=[os.path.basename(new_path1), os.path.basename(new_path2)])
+ source=[os.path.basename(new_path1), os.path.basename(new_path2)])]
diff --git a/debbindiff/comparators/cpio.py b/debbindiff/comparators/cpio.py
index 128aa2c..ccf187c 100644
--- a/debbindiff/comparators/cpio.py
+++ b/debbindiff/comparators/cpio.py
@@ -21,7 +21,7 @@ import subprocess
import os.path
import debbindiff.comparators
from debbindiff import logger, tool_required
-from debbindiff.comparators.utils import binary_fallback, make_temp_directory, Command
+from debbindiff.comparators.utils import binary_fallback, returns_details, make_temp_directory, Command
from debbindiff.difference import Difference
class CpioContent(Command):
@@ -49,13 +49,12 @@ def extract_cpio_archive(path, destdir):
@binary_fallback
+ at returns_details
def compare_cpio_files(path1, path2, source=None):
differences = []
- difference = Difference.from_command(
- CpioContent, path1, path2, source="file list")
- if difference:
- differences.append(difference)
+ differences.append(Difference.from_command(
+ CpioContent, path1, path2, source="file list"))
# compare files contained in archive
content1 = get_cpio_names(path1)
@@ -69,7 +68,7 @@ def compare_cpio_files(path1, path2, source=None):
in_path2 = os.path.join(temp_dir2, member)
if not os.path.isfile(in_path1) or not os.path.isfile(in_path2):
continue
- differences.extend(debbindiff.comparators.compare_files(
+ differences.append(debbindiff.comparators.compare_files(
in_path1, in_path2, source=member))
return differences
diff --git a/debbindiff/comparators/deb.py b/debbindiff/comparators/deb.py
index e6b4d09..3130827 100644
--- a/debbindiff/comparators/deb.py
+++ b/debbindiff/comparators/deb.py
@@ -26,10 +26,11 @@ from debbindiff.difference import Difference, get_source
import debbindiff.comparators
from debbindiff.comparators.binary import are_same_binaries
from debbindiff.comparators.utils import \
- binary_fallback, make_temp_directory, get_ar_content
+ binary_fallback, returns_details, make_temp_directory, get_ar_content
@binary_fallback
+ at returns_details
def compare_deb_files(path1, path2, source=None):
differences = []
# look up differences in content
@@ -50,7 +51,7 @@ def compare_deb_files(path1, path2, source=None):
f1.write(member1.read())
with open(in_path2, 'w') as f2:
f2.write(member2.read())
- differences.extend(
+ differences.append(
debbindiff.comparators.compare_files(
in_path1, in_path2, source=name))
os.unlink(in_path1)
@@ -58,16 +59,14 @@ def compare_deb_files(path1, path2, source=None):
# look up differences in file list and file metadata
content1 = get_ar_content(path1)
content2 = get_ar_content(path2)
- difference = Difference.from_unicode(
- content1, content2, path1, path2, source="metadata")
- if difference:
- differences.append(difference)
+ differences.append(Difference.from_unicode(
+ content1, content2, path1, path2, source="metadata"))
return differences
def compare_md5sums_files(path1, path2, source=None):
if are_same_binaries(path1, path2):
- return []
- return [Difference(None, path1, path2,
- source=get_source(path1, path2),
- comment="Files in package differs")]
+ return None
+ return Difference(None, path1, path2,
+ source=get_source(path1, path2),
+ comment="Files in package differs")
diff --git a/debbindiff/comparators/directory.py b/debbindiff/comparators/directory.py
index 5953d9f..93a41aa 100644
--- a/debbindiff/comparators/directory.py
+++ b/debbindiff/comparators/directory.py
@@ -23,7 +23,7 @@ import subprocess
from debbindiff import logger, tool_required, RequiredToolNotFound
from debbindiff.difference import Difference
import debbindiff.comparators
-from debbindiff.comparators.utils import Command
+from debbindiff.comparators.utils import returns_details, Command
def ls(path):
@@ -96,6 +96,7 @@ def compare_meta(path1, path2):
@tool_required('ls')
+ at returns_details
def compare_directories(path1, path2, source=None):
differences = []
logger.debug('path1 files: %s' % sorted(set(os.listdir(path1))))
@@ -104,26 +105,20 @@ def compare_directories(path1, path2, source=None):
logger.debug('compare %s' % name)
in_path1 = os.path.join(path1, name)
in_path2 = os.path.join(path2, name)
- in_differences = debbindiff.comparators.compare_files(
- in_path1, in_path2, source=name)
+ in_difference = debbindiff.comparators.compare_files(
+ in_path1, in_path2, source=name)
if not os.path.isdir(in_path1):
- if in_differences:
- in_differences[0].add_details(compare_meta(in_path1, in_path2))
+ if in_difference:
+ in_difference.add_details(compare_meta(in_path1, in_path2))
else:
details = compare_meta(in_path1, in_path2)
if details:
d = Difference(None, path1, path2, source=name)
d.add_details(details)
- in_differences = [d]
- differences.extend(in_differences)
+ in_difference = d
+ differences.append(in_difference)
ls1 = ls(path1)
ls2 = ls(path2)
- difference = Difference.from_unicode(ls1, ls2, path1, path2, source="ls")
- if difference:
- differences.append(difference)
+ differences.append(Difference.from_unicode(ls1, ls2, path1, path2, source="ls"))
differences.extend(compare_meta(path1, path2))
- if differences:
- d = Difference(None, path1, path2, source=source)
- d.add_details(differences)
- return [d]
- return []
+ return differences
diff --git a/debbindiff/comparators/elf.py b/debbindiff/comparators/elf.py
index 01f345b..bff635d 100644
--- a/debbindiff/comparators/elf.py
+++ b/debbindiff/comparators/elf.py
@@ -21,7 +21,7 @@ import os.path
import re
import subprocess
from debbindiff import tool_required
-from debbindiff.comparators.utils import binary_fallback, get_ar_content, Command
+from debbindiff.comparators.utils import binary_fallback, returns_details, get_ar_content, Command
from debbindiff.difference import Difference
@@ -62,32 +62,26 @@ class ObjdumpDisassemble(Command):
# by both compare_elf_files and compare_static_lib_files
def _compare_elf_data(path1, path2, source=None):
differences = []
- difference = Difference.from_command(ReadelfAll, path1, path2)
- if difference:
- differences.append(difference)
- difference = Difference.from_command(ReadelfDebugDump, path1, path2)
- if difference:
- differences.append(difference)
- difference = Difference.from_command(ObjdumpDisassemble, path1, path2)
- if difference:
- differences.append(difference)
+ differences.append(Difference.from_command(ReadelfAll, path1, path2))
+ differences.append(Difference.from_command(ReadelfDebugDump, path1, path2))
+ differences.append(Difference.from_command(ObjdumpDisassemble, path1, path2))
return differences
@binary_fallback
+ at returns_details
def compare_elf_files(path1, path2, source=None):
return _compare_elf_data(path1, path2, source=None)
@binary_fallback
+ at returns_details
def compare_static_lib_files(path1, path2, source=None):
differences = []
# look up differences in metadata
content1 = get_ar_content(path1)
content2 = get_ar_content(path2)
- difference = Difference.from_unicode(
- content1, content2, path1, path2, source="metadata")
- if difference:
- differences.append(difference)
+ differences.append(Difference.from_unicode(
+ content1, content2, path1, path2, source="metadata"))
differences.extend(_compare_elf_data(path1, path2, source))
return differences
diff --git a/debbindiff/comparators/fonts.py b/debbindiff/comparators/fonts.py
index 5481a1e..0deafcb 100644
--- a/debbindiff/comparators/fonts.py
+++ b/debbindiff/comparators/fonts.py
@@ -20,7 +20,7 @@
import locale
import subprocess
from debbindiff import tool_required
-from debbindiff.comparators.utils import binary_fallback, Command
+from debbindiff.comparators.utils import binary_fallback, returns_details, Command
from debbindiff.difference import Difference
@@ -33,8 +33,6 @@ class Showttf(Command):
return line.decode('latin-1').encode('utf-8')
@binary_fallback
+ at returns_details
def compare_ttf_files(path1, path2, source=None):
- difference = Difference.from_command(Showttf, path1, path2)
- if not difference:
- return []
- return [difference]
+ return [Difference.from_command(Showttf, path1, path2)]
diff --git a/debbindiff/comparators/gettext.py b/debbindiff/comparators/gettext.py
index 98304f8..6e68702 100644
--- a/debbindiff/comparators/gettext.py
+++ b/debbindiff/comparators/gettext.py
@@ -21,7 +21,7 @@ import re
import subprocess
from StringIO import StringIO
from debbindiff import tool_required
-from debbindiff.comparators.utils import binary_fallback, Command
+from debbindiff.comparators.utils import binary_fallback, returns_details, Command
from debbindiff.difference import Difference
from debbindiff import logger
@@ -57,8 +57,6 @@ class Msgunfmt(Command):
@binary_fallback
+ at returns_details
def compare_mo_files(path1, path2, source=None):
- difference = Difference.from_command(Msgunfmt, path1, path2)
- if not difference:
- return []
- return [difference]
+ return [Difference.from_command(Msgunfmt, path1, path2)]
diff --git a/debbindiff/comparators/gzip.py b/debbindiff/comparators/gzip.py
index e0405b9..7326409 100644
--- a/debbindiff/comparators/gzip.py
+++ b/debbindiff/comparators/gzip.py
@@ -22,7 +22,7 @@ import subprocess
import os.path
import debbindiff.comparators
from debbindiff import tool_required
-from debbindiff.comparators.utils import binary_fallback, make_temp_directory
+from debbindiff.comparators.utils import binary_fallback, returns_details, make_temp_directory
from debbindiff.difference import Difference, get_source
@@ -47,19 +47,18 @@ def get_gzip_metadata(path):
@binary_fallback
+ at returns_details
def compare_gzip_files(path1, path2, source=None):
differences = []
# check metadata
metadata1 = get_gzip_metadata(path1)
metadata2 = get_gzip_metadata(path2)
- difference = Difference.from_unicode(
- metadata1, metadata2, path1, path2, source='metadata')
- if difference:
- differences.append(difference)
+ differences.append(Difference.from_unicode(
+ metadata1, metadata2, path1, path2, source='metadata'))
# check content
with decompress_gzip(path1) as new_path1:
with decompress_gzip(path2) as new_path2:
- differences.extend(debbindiff.comparators.compare_files(
+ differences.append(debbindiff.comparators.compare_files(
new_path1, new_path2,
source=[os.path.basename(new_path1), os.path.basename(new_path2)]))
return differences
diff --git a/debbindiff/comparators/haskell.py b/debbindiff/comparators/haskell.py
index 74771bb..e3c0ad5 100644
--- a/debbindiff/comparators/haskell.py
+++ b/debbindiff/comparators/haskell.py
@@ -19,7 +19,7 @@
import subprocess
from debbindiff import tool_required
-from debbindiff.comparators.utils import binary_fallback, Command
+from debbindiff.comparators.utils import binary_fallback, returns_details, Command
from debbindiff.difference import Difference
@@ -30,8 +30,6 @@ class ShowIface(Command):
@binary_fallback
+ at returns_details
def compare_hi_files(path1, path2, source=None):
- difference = Difference.from_command(ShowIface, path1, path2)
- if not difference:
- return []
- return [difference]
+ return [Difference.from_command(ShowIface, path1, path2)]
diff --git a/debbindiff/comparators/ipk.py b/debbindiff/comparators/ipk.py
index 240e2f7..7a95b77 100644
--- a/debbindiff/comparators/ipk.py
+++ b/debbindiff/comparators/ipk.py
@@ -18,12 +18,13 @@
# along with debbindiff. If not, see <http://www.gnu.org/licenses/>.
import os.path
-from debbindiff.comparators.utils import binary_fallback
+from debbindiff.comparators.utils import binary_fallback, returns_details
from debbindiff.comparators.tar import compare_tar_files
from debbindiff.comparators.gzip import decompress_gzip, get_gzip_metadata
from debbindiff.difference import Difference
@binary_fallback
+ at returns_details
# ipk packages are just .tar.gz archives
def compare_ipk_files(path1, path2, source=None):
differences = []
@@ -31,15 +32,13 @@ def compare_ipk_files(path1, path2, source=None):
# metadata
metadata1 = get_gzip_metadata(path1)
metadata2 = get_gzip_metadata(path2)
- difference = Difference.from_unicode(
- metadata1, metadata2, path1, path2, source='metadata')
- if difference:
- differences.append(difference)
+ differences.append(Difference.from_unicode(
+ metadata1, metadata2, path1, path2, source='metadata'))
# content
with decompress_gzip(path1) as tar1:
with decompress_gzip(path2) as tar2:
- differences.extend(compare_tar_files(tar1, tar2,
+ differences.append(compare_tar_files(tar1, tar2,
source=[os.path.basename(tar1), os.path.basename(tar2)]))
return differences
diff --git a/debbindiff/comparators/iso9660.py b/debbindiff/comparators/iso9660.py
index e327ec2..04d0356 100644
--- a/debbindiff/comparators/iso9660.py
+++ b/debbindiff/comparators/iso9660.py
@@ -21,7 +21,7 @@ import os.path
import subprocess
import debbindiff.comparators
from debbindiff import logger, tool_required
-from debbindiff.comparators.utils import binary_fallback, make_temp_directory, Command
+from debbindiff.comparators.utils import binary_fallback, returns_details, make_temp_directory, Command
from debbindiff.difference import Difference
@@ -67,17 +67,14 @@ def extract_from_iso9660(image_path, in_path, dest):
@binary_fallback
+ at returns_details
def compare_iso9660_files(path1, path2, source=None):
differences = []
# compare metadata
- difference = Difference.from_command(ISO9660PVD, path1, path2)
- if difference:
- differences.append(difference)
+ differences.append(Difference.from_command(ISO9660PVD, path1, path2))
for extension in (None, 'joliet', 'rockridge'):
- difference = Difference.from_command(ISO9660Listing, path1, path2, command_args=(extension,))
- if difference:
- differences.append(difference)
+ differences.append(Difference.from_command(ISO9660Listing, path1, path2, command_args=(extension,)))
# compare files contained in image
files1 = get_iso9660_names(path1)
@@ -92,7 +89,7 @@ def compare_iso9660_files(path1, path2, source=None):
extract_from_iso9660(path1, name, dest)
with open(in_path2, 'w') as dest:
extract_from_iso9660(path2, name, dest)
- differences.extend(debbindiff.comparators.compare_files(
+ differences.append(debbindiff.comparators.compare_files(
in_path1, in_path2, source=name))
return differences
diff --git a/debbindiff/comparators/java.py b/debbindiff/comparators/java.py
index 24b864e..de8bb59 100644
--- a/debbindiff/comparators/java.py
+++ b/debbindiff/comparators/java.py
@@ -20,7 +20,7 @@
import os.path
import re
from debbindiff import tool_required
-from debbindiff.comparators.utils import binary_fallback, Command
+from debbindiff.comparators.utils import binary_fallback, returns_details, Command
from debbindiff.difference import Difference
@@ -39,8 +39,6 @@ class Javap(Command):
return line
@binary_fallback
+ at returns_details
def compare_class_files(path1, path2, source=None):
- difference = Difference.from_command(Javap, path1, path2)
- if not difference:
- return []
- return [difference]
+ return [Difference.from_command(Javap, path1, path2)]
diff --git a/debbindiff/comparators/pdf.py b/debbindiff/comparators/pdf.py
index 81f5b3c..8a35ad5 100644
--- a/debbindiff/comparators/pdf.py
+++ b/debbindiff/comparators/pdf.py
@@ -19,7 +19,7 @@
import subprocess
from debbindiff import tool_required
-from debbindiff.comparators.utils import binary_fallback, Command
+from debbindiff.comparators.utils import binary_fallback, returns_details, Command
from debbindiff.difference import Difference, get_source
@@ -39,12 +39,9 @@ class Pdftk(Command):
@binary_fallback
+ at returns_details
def compare_pdf_files(path1, path2, source=None):
differences = []
- difference = Difference.from_command(Pdftotext, path1, path2)
- if difference:
- differences.append(difference)
- difference = Difference.from_command(Pdftk, path1, path2)
- if difference:
- differences.append(difference)
+ differences.append(Difference.from_command(Pdftotext, path1, path2))
+ differences.append(Difference.from_command(Pdftk, path1, path2))
return differences
diff --git a/debbindiff/comparators/png.py b/debbindiff/comparators/png.py
index 482ff37..64fc104 100644
--- a/debbindiff/comparators/png.py
+++ b/debbindiff/comparators/png.py
@@ -20,7 +20,7 @@
from functools import partial
import subprocess
from debbindiff import tool_required
-from debbindiff.comparators.utils import binary_fallback, Command
+from debbindiff.comparators.utils import binary_fallback, returns_details, Command
from debbindiff.difference import Difference
@@ -39,9 +39,7 @@ class Sng(Command):
@binary_fallback
+ at returns_details
def compare_png_files(path1, path2, source=None):
- difference = Difference.from_command(Sng, path1, path2, source='sng')
- if not difference:
- return []
- return [difference]
+ return [Difference.from_command(Sng, path1, path2, source='sng')]
diff --git a/debbindiff/comparators/rpm.py b/debbindiff/comparators/rpm.py
index b9e9c13..52b92c8 100644
--- a/debbindiff/comparators/rpm.py
+++ b/debbindiff/comparators/rpm.py
@@ -24,7 +24,7 @@ from contextlib import contextmanager
import rpm
import debbindiff.comparators
from debbindiff import logger, tool_required
-from debbindiff.comparators.utils import binary_fallback, make_temp_directory
+from debbindiff.comparators.utils import binary_fallback, returns_details, make_temp_directory
from debbindiff.difference import Difference, get_source
def get_rpm_header(path, ts):
@@ -65,6 +65,7 @@ def extract_rpm_payload(path):
@binary_fallback
+ at returns_details
def compare_rpm_files(path1, path2, source=None):
differences = []
@@ -75,15 +76,13 @@ def compare_rpm_files(path1, path2, source=None):
ts.setVSFlags(-1)
header1 = get_rpm_header(path1, ts)
header2 = get_rpm_header(path2, ts)
- difference = Difference.from_unicode(
- header1, header2, path1, path2, source="header")
- if difference:
- differences.append(difference)
+ differences.append(Difference.from_unicode(
+ header1, header2, path1, path2, source="header"))
# extract cpio archive
with extract_rpm_payload(path1) as archive1:
with extract_rpm_payload(path2) as archive2:
- differences.extend(debbindiff.comparators.compare_files(
+ differences.append(debbindiff.comparators.compare_files(
archive1, archive2, source=get_source(archive1, archive2)))
return differences
diff --git a/debbindiff/comparators/squashfs.py b/debbindiff/comparators/squashfs.py
index ffe252b..e09df27 100644
--- a/debbindiff/comparators/squashfs.py
+++ b/debbindiff/comparators/squashfs.py
@@ -22,7 +22,7 @@ import subprocess
import os.path
import debbindiff.comparators
from debbindiff import logger, tool_required
-from debbindiff.comparators.utils import binary_fallback, make_temp_directory, Command
+from debbindiff.comparators.utils import binary_fallback, returns_details, make_temp_directory, Command
from debbindiff.difference import Difference
@@ -61,16 +61,13 @@ def extract_squashfs(path, destdir):
@binary_fallback
+ at returns_details
def compare_squashfs_files(path1, path2, source=None):
differences = []
# compare metadata
- difference = Difference.from_command(SquashfsSuperblock, path1, path2)
- if difference:
- differences.append(difference)
- difference = Difference.from_command(SquashfsListing, path1, path2)
- if difference:
- differences.append(difference)
+ differences.append(Difference.from_command(SquashfsSuperblock, path1, path2))
+ differences.append(Difference.from_command(SquashfsListing, path1, path2))
# compare files contained in archive
files1 = get_squashfs_names(path1)
@@ -84,7 +81,7 @@ def compare_squashfs_files(path1, path2, source=None):
in_path2 = os.path.join(temp_dir2, member)
if not os.path.isfile(in_path1) or not os.path.isfile(in_path2):
continue
- differences.extend(debbindiff.comparators.compare_files(
+ differences.append(debbindiff.comparators.compare_files(
in_path1, in_path2, source=member))
return differences
diff --git a/debbindiff/comparators/tar.py b/debbindiff/comparators/tar.py
index de40c08..74b6994 100644
--- a/debbindiff/comparators/tar.py
+++ b/debbindiff/comparators/tar.py
@@ -24,7 +24,7 @@ import tarfile
from debbindiff import logger
from debbindiff.difference import Difference
import debbindiff.comparators
-from debbindiff.comparators.utils import binary_fallback, make_temp_directory
+from debbindiff.comparators.utils import binary_fallback, returns_details, make_temp_directory
def get_tar_content(tar):
@@ -39,6 +39,7 @@ def get_tar_content(tar):
@binary_fallback
+ at returns_details
def compare_tar_files(path1, path2, source=None):
differences = []
with tarfile.open(path1, 'r') as tar1:
@@ -59,7 +60,7 @@ def compare_tar_files(path1, path2, source=None):
tar2.extract(name, temp_dir2)
in_path1 = os.path.join(temp_dir1, name).decode('utf-8')
in_path2 = os.path.join(temp_dir2, name).decode('utf-8')
- differences.extend(
+ differences.append(
debbindiff.comparators.compare_files(
in_path1, in_path2,
source=name.decode('utf-8')))
@@ -68,8 +69,6 @@ def compare_tar_files(path1, path2, source=None):
# look up differences in file list and file metadata
content1 = get_tar_content(tar1).decode('utf-8')
content2 = get_tar_content(tar2).decode('utf-8')
- difference = Difference.from_unicode(
- content1, content2, path1, path2, source="metadata")
- if difference:
- differences.append(difference)
+ differences.append(Difference.from_unicode(
+ content1, content2, path1, path2, source="metadata"))
return differences
diff --git a/debbindiff/comparators/text.py b/debbindiff/comparators/text.py
index ca73ba2..caf9480 100644
--- a/debbindiff/comparators/text.py
+++ b/debbindiff/comparators/text.py
@@ -28,10 +28,7 @@ def compare_text_files(path1, path2, encoding=None, source=None):
try:
file1 = codecs.open(path1, 'r', encoding=encoding)
file2 = codecs.open(path2, 'r', encoding=encoding)
- difference = Difference.from_file(file1, file2, path1, path2, source)
+ return Difference.from_file(file1, file2, path1, path2, source)
except (LookupError, UnicodeDecodeError):
# unknown or misdetected encoding
return compare_binary_files(path1, path2, source)
- if not difference:
- return []
- return [difference]
diff --git a/debbindiff/comparators/utils.py b/debbindiff/comparators/utils.py
index d848468..0d0ae32 100644
--- a/debbindiff/comparators/utils.py
+++ b/debbindiff/comparators/utils.py
@@ -39,35 +39,44 @@ from debbindiff import logger, RequiredToolNotFound
def binary_fallback(original_function):
def with_fallback(path1, path2, source=None):
if are_same_binaries(path1, path2):
- return []
+ return None
try:
- inside_differences = original_function(path1, path2, source)
+ difference = original_function(path1, path2, source)
# no differences detected inside? let's at least do a binary diff
- if len(inside_differences) == 0:
- difference = compare_binary_files(path1, path2, source=source)[0]
+ if difference is None:
+ difference = compare_binary_files(path1, path2, source=source)
difference.comment = (difference.comment or '') + \
"No differences found inside, yet data differs"
- else:
- difference = Difference(None, path1, path2, source=source)
- difference.add_details(inside_differences)
except subprocess.CalledProcessError as e:
- difference = compare_binary_files(path1, path2, source=source)[0]
+ difference = compare_binary_files(path1, path2, source=source)
output = re.sub(r'^', ' ', e.output, flags=re.MULTILINE)
cmd = ' '.join(e.cmd)
difference.comment = (difference.comment or '') + \
"Command `%s` exited with %d. Output:\n%s" \
% (cmd, e.returncode, output)
except RequiredToolNotFound as e:
- difference = compare_binary_files(path1, path2, source=source)[0]
+ difference = compare_binary_files(path1, path2, source=source)
difference.comment = (difference.comment or '') + \
"'%s' not available in path. Falling back to binary comparison." % e.command
package = e.get_package()
if package:
difference.comment += "\nInstall '%s' to get a better output." % package
- return [difference]
+ return difference
return with_fallback
+# decorator that will group multiple differences as details of an empty Difference
+# are detected or if an external tool fails
+def returns_details(original_function):
+ def wrap_details(path1, path2, source=None):
+ details = [d for d in original_function(path1, path2, source) if d is not None]
+ if len(details) == 0:
+ return None
+ difference = Difference(None, path1, path2, source=source)
+ difference.add_details(details)
+ return difference
+ return wrap_details
+
@contextmanager
def make_temp_directory():
temp_dir = tempfile.mkdtemp(suffix='debbindiff')
diff --git a/debbindiff/comparators/xz.py b/debbindiff/comparators/xz.py
index 11ccd14..fe987f7 100644
--- a/debbindiff/comparators/xz.py
+++ b/debbindiff/comparators/xz.py
@@ -22,7 +22,7 @@ import os.path
import subprocess
import debbindiff.comparators
from debbindiff import tool_required
-from debbindiff.comparators.utils import binary_fallback, make_temp_directory
+from debbindiff.comparators.utils import binary_fallback, returns_details, make_temp_directory
from debbindiff.difference import get_source
@@ -42,9 +42,10 @@ def decompress_xz(path):
@binary_fallback
+ at returns_details
def compare_xz_files(path1, path2, source=None):
with decompress_xz(path1) as new_path1:
with decompress_xz(path2) as new_path2:
- return debbindiff.comparators.compare_files(
+ return [debbindiff.comparators.compare_files(
new_path1, new_path2,
- source=[os.path.basename(new_path1), os.path.basename(new_path2)])
+ source=[os.path.basename(new_path1), os.path.basename(new_path2)])]
diff --git a/debbindiff/comparators/zip.py b/debbindiff/comparators/zip.py
index eb5f71f..6c52390 100644
--- a/debbindiff/comparators/zip.py
+++ b/debbindiff/comparators/zip.py
@@ -25,7 +25,7 @@ from debbindiff import logger
from debbindiff.difference import Difference
import debbindiff.comparators
from debbindiff import tool_required
-from debbindiff.comparators.utils import binary_fallback, make_temp_directory, Command
+from debbindiff.comparators.utils import binary_fallback, returns_details, make_temp_directory, Command
class Zipinfo(Command):
@@ -47,6 +47,7 @@ class ZipinfoVerbose(Zipinfo):
@binary_fallback
+ at returns_details
def compare_zip_files(path1, path2, source=None):
differences = []
try:
@@ -63,7 +64,7 @@ def compare_zip_files(path1, path2, source=None):
logger.debug('extract member %s', name)
in_path1 = zip1.extract(name, temp_dir1)
in_path2 = zip2.extract(name, temp_dir2)
- differences.extend(
+ differences.append(
debbindiff.comparators.compare_files(
in_path1, in_path2,
source=name))
@@ -74,8 +75,7 @@ def compare_zip_files(path1, path2, source=None):
if not difference:
# search harder
difference = Difference.from_command(ZipinfoVerbose, path1, path2)
- if difference:
- differences.append(difference)
+ differences.append(difference)
except BadZipfile:
logger.debug('Either %s or %s is not a zip file.' % (path1, path2))
# we'll fallback on binary comparison
diff --git a/debbindiff/difference.py b/debbindiff/difference.py
index 375138f..e895aa4 100644
--- a/debbindiff/difference.py
+++ b/debbindiff/difference.py
@@ -280,6 +280,9 @@ class Difference(object):
self._source2 = path2
self._details = []
+ def __repr__(self):
+ return '<Difference %s -- %s %s>' % (self._source1, self._source2, self._details)
+
@staticmethod
def from_feeder(feeder1, feeder2, path1, path2, source=None,
comment=None):
@@ -358,6 +361,8 @@ 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)
diff --git a/debbindiff/presenters/html.py b/debbindiff/presenters/html.py
index f2e74ef..1330849 100644
--- a/debbindiff/presenters/html.py
+++ b/debbindiff/presenters/html.py
@@ -512,7 +512,7 @@ def output_header(css_url, print_func):
})
-def output_html(differences, css_url=None, print_func=None, max_page_size=None):
+def output_html(difference, css_url=None, print_func=None, max_page_size=None):
if print_func is None:
print_func = print
if max_page_size is None:
@@ -520,8 +520,7 @@ def output_html(differences, css_url=None, print_func=None, max_page_size=None):
print_func = create_limited_print_func(print_func, max_page_size)
try:
output_header(css_url, print_func)
- for difference in differences:
- output_difference(difference, print_func, [])
+ output_difference(difference, print_func, [])
except PrintLimitReached:
logger.debug('print limit reached')
print_func(u"<div class='error'>Max output size reached.</div>",
diff --git a/debbindiff/presenters/text.py b/debbindiff/presenters/text.py
index 447a76b..eb7ad60 100644
--- a/debbindiff/presenters/text.py
+++ b/debbindiff/presenters/text.py
@@ -48,13 +48,12 @@ def print_details(difference, print_func):
print_details(detail, new_print_func)
print_func(u'╵')
-def output_text(differences, print_func):
+def output_text(difference, print_func):
try:
- for difference in differences:
- print_func("--- %s" % (difference.source1))
- print_func("+++ %s" % (difference.source2))
- print_difference(difference, print_func)
- print_details(difference, print_func)
+ print_func("--- %s" % (difference.source1))
+ print_func("+++ %s" % (difference.source2))
+ print_difference(difference, print_func)
+ print_details(difference, print_func)
except UnicodeEncodeError:
logger.critical('Console is unable to print Unicode characters. Set LC_CTYPE=C.UTF-8')
sys.exit(2)
diff --git a/tests/comparators/test_binary.py b/tests/comparators/test_binary.py
index b188a70..7e5832a 100644
--- a/tests/comparators/test_binary.py
+++ b/tests/comparators/test_binary.py
@@ -37,14 +37,13 @@ def test_are_not_same_binaries(tmpdir):
assert not are_same_binaries(TEST_FILE1_PATH, TEST_FILE2_PATH)
def test_no_differences_with_xxd():
- differences = compare_binary_files(TEST_FILE1_PATH, TEST_FILE1_PATH)
- assert len(differences) == 0
+ difference = compare_binary_files(TEST_FILE1_PATH, TEST_FILE1_PATH)
+ assert difference is None
def test_compare_with_xxd():
- differences = compare_binary_files(TEST_FILE1_PATH, TEST_FILE2_PATH)
- assert len(differences) == 1
+ difference = compare_binary_files(TEST_FILE1_PATH, TEST_FILE2_PATH)
expected_diff = open(os.path.join(os.path.dirname(__file__), '../data/binary_expected_diff')).read()
- assert differences[0].unified_diff == expected_diff
+ assert difference.unified_diff == expected_diff
@pytest.fixture
def xxd_not_found(monkeypatch):
@@ -53,11 +52,10 @@ def xxd_not_found(monkeypatch):
monkeypatch.setattr(debbindiff.comparators.binary, 'xxd', mock_xxd)
def test_no_differences_without_xxd(xxd_not_found):
- differences = compare_binary_files(TEST_FILE1_PATH, TEST_FILE1_PATH)
- assert len(differences) == 0
+ difference = compare_binary_files(TEST_FILE1_PATH, TEST_FILE1_PATH)
+ assert difference is None
def test_compare_without_xxd(xxd_not_found):
- differences = compare_binary_files(TEST_FILE1_PATH, TEST_FILE2_PATH)
- assert len(differences) == 1
+ difference = compare_binary_files(TEST_FILE1_PATH, TEST_FILE2_PATH)
expected_diff = open(os.path.join(os.path.dirname(__file__), '../data/binary_hexdump_expected_diff')).read()
- assert differences[0].unified_diff == expected_diff
+ assert difference.unified_diff == expected_diff
diff --git a/tests/comparators/test_bzip2.py b/tests/comparators/test_bzip2.py
index 7a04bc6..6b5bf70 100644
--- a/tests/comparators/test_bzip2.py
+++ b/tests/comparators/test_bzip2.py
@@ -27,12 +27,12 @@ TEST_FILE1_PATH = os.path.join(os.path.dirname(__file__), '../data/test1.bz2')
TEST_FILE2_PATH = os.path.join(os.path.dirname(__file__), '../data/test2.bz2')
def test_no_differences():
- differences = compare_bzip2_files(TEST_FILE1_PATH, TEST_FILE1_PATH)
- assert len(differences) == 0
+ difference = compare_bzip2_files(TEST_FILE1_PATH, TEST_FILE1_PATH)
+ assert difference is None
@pytest.fixture
def differences():
- return compare_bzip2_files(TEST_FILE1_PATH, TEST_FILE2_PATH)[0].details # skip container with path
+ return compare_bzip2_files(TEST_FILE1_PATH, TEST_FILE2_PATH).details
def test_content_source(differences):
assert differences[0].source1 == 'test1'
@@ -43,7 +43,7 @@ def test_content_source_without_extension(tmpdir):
path2 = str(tmpdir.join('test2'))
shutil.copy(TEST_FILE1_PATH, path1)
shutil.copy(TEST_FILE2_PATH, path2)
- differences = compare_bzip2_files(path1, path2)[0].details # skip container
+ differences = compare_bzip2_files(path1, path2).details
assert differences[0].source1 == 'test1-content'
assert differences[0].source2 == 'test2-content'
diff --git a/tests/comparators/test_cpio.py b/tests/comparators/test_cpio.py
index 3e99e05..b73ab6c 100644
--- a/tests/comparators/test_cpio.py
+++ b/tests/comparators/test_cpio.py
@@ -28,12 +28,12 @@ TEST_FILE1_PATH = os.path.join(os.path.dirname(__file__), '../data/test1.cpio')
TEST_FILE2_PATH = os.path.join(os.path.dirname(__file__), '../data/test2.cpio')
def test_no_differences():
- differences = compare_cpio_files(TEST_FILE1_PATH, TEST_FILE1_PATH)
- assert len(differences) == 0
+ difference = compare_cpio_files(TEST_FILE1_PATH, TEST_FILE1_PATH)
+ assert difference is None
@pytest.fixture
def differences():
- return compare_cpio_files(TEST_FILE1_PATH, TEST_FILE2_PATH)[0].details # skip container with path
+ return compare_cpio_files(TEST_FILE1_PATH, TEST_FILE2_PATH).details
def test_listing(differences):
expected_diff = open(os.path.join(os.path.dirname(__file__), '../data/cpio_listing_expected_diff')).read()
diff --git a/tests/comparators/test_deb.py b/tests/comparators/test_deb.py
index 4f0d872..11bd2cf 100644
--- a/tests/comparators/test_deb.py
+++ b/tests/comparators/test_deb.py
@@ -28,12 +28,12 @@ TEST_FILE1_PATH = os.path.join(os.path.dirname(__file__), '../data/test1.deb')
TEST_FILE2_PATH = os.path.join(os.path.dirname(__file__), '../data/test2.deb')
def test_no_differences():
- differences = compare_deb_files(TEST_FILE1_PATH, TEST_FILE1_PATH)
- assert len(differences) == 0
+ difference = compare_deb_files(TEST_FILE1_PATH, TEST_FILE1_PATH)
+ assert difference is None
@pytest.fixture
def differences():
- return compare_deb_files(TEST_FILE1_PATH, TEST_FILE2_PATH)[0].details # skip container with path
+ return compare_deb_files(TEST_FILE1_PATH, TEST_FILE2_PATH).details
def test_compressed_files(differences):
assert differences[0].source1 == 'control.tar.gz'
diff --git a/tests/comparators/test_directory.py b/tests/comparators/test_directory.py
index bce2e9f..4a587b1 100644
--- a/tests/comparators/test_directory.py
+++ b/tests/comparators/test_directory.py
@@ -32,9 +32,8 @@ TEST_FILE1_PATH = os.path.join(os.path.dirname(__file__), '../data/text_ascii1')
TEST_FILE2_PATH = os.path.join(os.path.dirname(__file__), '../data/text_ascii2')
def test_no_differences():
- differences = compare_directories(os.path.dirname(__file__), os.path.dirname(__file__))
- output_text(differences, print_func=print)
- assert len(differences) == 0
+ difference = compare_directories(os.path.dirname(__file__), os.path.dirname(__file__))
+ assert difference is None
@pytest.fixture
def differences(tmpdir):
@@ -46,14 +45,14 @@ def differences(tmpdir):
shutil.copy(TEST_FILE2_PATH, str(tmpdir.join('b/dir/text')))
os.utime(str(tmpdir.join('a/dir/text')), (0, 0))
os.utime(str(tmpdir.join('b/dir/text')), (0, 0))
- return compare_directories(str(tmpdir.join('a')), str(tmpdir.join('b')))
+ return compare_directories(str(tmpdir.join('a')), str(tmpdir.join('b'))).details
def test_content(differences):
- assert differences[0].details[0].source1 == 'dir'
- assert differences[0].details[0].details[0].source1 == 'text'
+ assert differences[0].source1 == 'dir'
+ assert differences[0].details[0].source1 == 'text'
expected_diff = open(os.path.join(os.path.dirname(__file__), '../data/text_ascii_expected_diff')).read()
- assert differences[0].details[0].details[0].unified_diff == expected_diff
+ assert differences[0].details[0].unified_diff == expected_diff
def test_stat(differences):
- output_text(differences, print_func=print)
- assert 'stat' in differences[0].details[0].details[0].details[0].source1
+ output_text(differences[0], print_func=print)
+ assert 'stat' in differences[0].details[0].details[0].source1
diff --git a/tests/comparators/test_elf.py b/tests/comparators/test_elf.py
index 718f1fe..5764ac6 100644
--- a/tests/comparators/test_elf.py
+++ b/tests/comparators/test_elf.py
@@ -27,12 +27,12 @@ TEST_OBJ1_PATH = os.path.join(os.path.dirname(__file__), '../data/test1.o')
TEST_OBJ2_PATH = os.path.join(os.path.dirname(__file__), '../data/test2.o')
def test_obj_no_differences():
- differences = compare_elf_files(TEST_OBJ1_PATH, TEST_OBJ1_PATH)
- assert len(differences) == 0
+ difference = compare_elf_files(TEST_OBJ1_PATH, TEST_OBJ1_PATH)
+ assert difference is None
@pytest.fixture
def obj_differences():
- return compare_elf_files(TEST_OBJ1_PATH, TEST_OBJ2_PATH)[0].details # skip container with path
+ return compare_elf_files(TEST_OBJ1_PATH, TEST_OBJ2_PATH).details
def test_diff(obj_differences):
assert len(obj_differences) == 1
@@ -43,12 +43,12 @@ TEST_LIB1_PATH = os.path.join(os.path.dirname(__file__), '../data/test1.a')
TEST_LIB2_PATH = os.path.join(os.path.dirname(__file__), '../data/test2.a')
def test_lib_no_differences():
- differences = compare_elf_files(TEST_LIB1_PATH, TEST_LIB1_PATH)
- assert len(differences) == 0
+ difference = compare_elf_files(TEST_LIB1_PATH, TEST_LIB1_PATH)
+ assert difference is None
@pytest.fixture
def lib_differences():
- return compare_static_lib_files(TEST_LIB1_PATH, TEST_LIB2_PATH)[0].details # skip container with path
+ return compare_static_lib_files(TEST_LIB1_PATH, TEST_LIB2_PATH).details
def test_lib_differences(lib_differences):
assert len(lib_differences) == 2
diff --git a/tests/comparators/test_fonts.py b/tests/comparators/test_fonts.py
index 6b7d8e7..ab9ec83 100644
--- a/tests/comparators/test_fonts.py
+++ b/tests/comparators/test_fonts.py
@@ -27,12 +27,12 @@ TEST_FILE1_PATH = os.path.join(os.path.dirname(__file__), '../data/Samyak-Malaya
TEST_FILE2_PATH = os.path.join(os.path.dirname(__file__), '../data/Samyak-Malayalam2.ttf')
def test_no_differences():
- differences = compare_ttf_files(TEST_FILE1_PATH, TEST_FILE1_PATH)
- assert len(differences) == 0
+ difference = compare_ttf_files(TEST_FILE1_PATH, TEST_FILE1_PATH)
+ assert difference is None
@pytest.fixture
def differences():
- return compare_ttf_files(TEST_FILE1_PATH, TEST_FILE2_PATH)[0].details # skip container with path
+ return compare_ttf_files(TEST_FILE1_PATH, TEST_FILE2_PATH).details
def test_diff(differences):
expected_diff = open(os.path.join(os.path.dirname(__file__), '../data/ttf_expected_diff')).read()
diff --git a/tests/comparators/test_gettext.py b/tests/comparators/test_gettext.py
index 1b1b70d..521c3c5 100644
--- a/tests/comparators/test_gettext.py
+++ b/tests/comparators/test_gettext.py
@@ -28,20 +28,19 @@ TEST_FILE1_PATH = os.path.join(os.path.dirname(__file__), '../data/test1.mo')
TEST_FILE2_PATH = os.path.join(os.path.dirname(__file__), '../data/test2.mo')
def test_no_differences():
- differences = compare_mo_files(TEST_FILE1_PATH, TEST_FILE1_PATH)
- assert len(differences) == 0
+ difference = compare_mo_files(TEST_FILE1_PATH, TEST_FILE1_PATH)
+ assert difference is None
@pytest.fixture
def differences():
- return compare_mo_files(TEST_FILE1_PATH, TEST_FILE2_PATH)[0].details # skip container with path
+ return compare_mo_files(TEST_FILE1_PATH, TEST_FILE2_PATH).details
def test_diff(differences):
expected_diff = open(os.path.join(os.path.dirname(__file__), '../data/mo_expected_diff')).read()
assert differences[0].unified_diff == expected_diff
def test_charsets():
- differences = compare_mo_files(os.path.join(os.path.dirname(__file__), '../data/test_no_charset.mo'),
+ difference = compare_mo_files(os.path.join(os.path.dirname(__file__), '../data/test_no_charset.mo'),
os.path.join(os.path.dirname(__file__), '../data/test_iso8859-1.mo'))
- differences = differences[0].details # skip container with path
expected_diff = codecs.open(os.path.join(os.path.dirname(__file__), '../data/mo_charsets_expected_diff'), encoding='utf-8').read()
- assert differences[0].unified_diff == expected_diff
+ assert difference.details[0].unified_diff == expected_diff
diff --git a/tests/comparators/test_gzip.py b/tests/comparators/test_gzip.py
index 143a8f8..53d147b 100644
--- a/tests/comparators/test_gzip.py
+++ b/tests/comparators/test_gzip.py
@@ -27,12 +27,12 @@ TEST_FILE1_PATH = os.path.join(os.path.dirname(__file__), '../data/test1.gz')
TEST_FILE2_PATH = os.path.join(os.path.dirname(__file__), '../data/test2.gz')
def test_no_differences():
- differences = compare_gzip_files(TEST_FILE1_PATH, TEST_FILE1_PATH)
- assert len(differences) == 0
+ difference = compare_gzip_files(TEST_FILE1_PATH, TEST_FILE1_PATH)
+ assert difference is None
@pytest.fixture
def differences():
- return compare_gzip_files(TEST_FILE1_PATH, TEST_FILE2_PATH)[0].details # skip container with path
+ return compare_gzip_files(TEST_FILE1_PATH, TEST_FILE2_PATH).details
def test_metadata(differences):
assert differences[0].source1 == 'metadata'
@@ -49,9 +49,9 @@ def test_content_source_without_extension(tmpdir):
path2 = str(tmpdir.join('test2'))
shutil.copy(TEST_FILE1_PATH, path1)
shutil.copy(TEST_FILE2_PATH, path2)
- differences = compare_gzip_files(path1, path2)[0].details # skip container
- assert differences[1].source1 == 'test1-content'
- assert differences[1].source2 == 'test2-content'
+ difference = compare_gzip_files(path1, path2).details
+ assert difference[1].source1 == 'test1-content'
+ assert difference[1].source2 == 'test2-content'
def test_content_diff(differences):
expected_diff = open(os.path.join(os.path.dirname(__file__), '../data/text_ascii_expected_diff')).read()
diff --git a/tests/comparators/test_ipk.py b/tests/comparators/test_ipk.py
index 18dd26c..ea5c55d 100644
--- a/tests/comparators/test_ipk.py
+++ b/tests/comparators/test_ipk.py
@@ -28,12 +28,12 @@ TEST_FILE1_PATH = os.path.join(os.path.dirname(__file__), '../data/base-files_15
TEST_FILE2_PATH = os.path.join(os.path.dirname(__file__), '../data/base-files_157-r45918_ar71xx.ipk')
def test_no_differences():
- differences = compare_ipk_files(TEST_FILE1_PATH, TEST_FILE1_PATH)
- assert len(differences) == 0
+ difference = compare_ipk_files(TEST_FILE1_PATH, TEST_FILE1_PATH)
+ assert difference is None
@pytest.fixture
def differences():
- return compare_ipk_files(TEST_FILE1_PATH, TEST_FILE2_PATH)[0].details # skip container with path
+ return compare_ipk_files(TEST_FILE1_PATH, TEST_FILE2_PATH).details
def test_metadata(differences):
expected_diff = open(os.path.join(os.path.dirname(__file__), '../data/ipk_metadata_expected_diff')).read()
diff --git a/tests/comparators/test_iso9660.py b/tests/comparators/test_iso9660.py
index 8693f39..fb4b19b 100644
--- a/tests/comparators/test_iso9660.py
+++ b/tests/comparators/test_iso9660.py
@@ -28,12 +28,12 @@ TEST_FILE1_PATH = os.path.join(os.path.dirname(__file__), '../data/test1.iso')
TEST_FILE2_PATH = os.path.join(os.path.dirname(__file__), '../data/test2.iso')
def test_no_differences():
- differences = compare_iso9660_files(TEST_FILE1_PATH, TEST_FILE1_PATH)
- assert len(differences) == 0
+ difference = compare_iso9660_files(TEST_FILE1_PATH, TEST_FILE1_PATH)
+ assert difference is None
@pytest.fixture
def differences():
- return compare_iso9660_files(TEST_FILE1_PATH, TEST_FILE2_PATH)[0].details # skip container with path
+ return compare_iso9660_files(TEST_FILE1_PATH, TEST_FILE2_PATH).details
def test_iso9660_content(differences):
expected_diff = open(os.path.join(os.path.dirname(__file__), '../data/iso9660_content_expected_diff')).read()
diff --git a/tests/comparators/test_java.py b/tests/comparators/test_java.py
index c95f6b4..5c119f4 100644
--- a/tests/comparators/test_java.py
+++ b/tests/comparators/test_java.py
@@ -27,12 +27,12 @@ TEST_FILE1_PATH = os.path.join(os.path.dirname(__file__), '../data/Test1.class')
TEST_FILE2_PATH = os.path.join(os.path.dirname(__file__), '../data/Test2.class')
def test_no_differences():
- differences = compare_class_files(TEST_FILE1_PATH, TEST_FILE1_PATH)
- assert len(differences) == 0
+ difference = compare_class_files(TEST_FILE1_PATH, TEST_FILE1_PATH)
+ assert difference is None
@pytest.fixture
def differences():
- return compare_class_files(TEST_FILE1_PATH, TEST_FILE2_PATH)[0].details # skip container with path
+ return compare_class_files(TEST_FILE1_PATH, TEST_FILE2_PATH).details
def test_diff(differences):
expected_diff = open(os.path.join(os.path.dirname(__file__), '../data/class_expected_diff')).read()
diff --git a/tests/comparators/test_pdf.py b/tests/comparators/test_pdf.py
index 2f5d659..7e75272 100644
--- a/tests/comparators/test_pdf.py
+++ b/tests/comparators/test_pdf.py
@@ -27,12 +27,12 @@ TEST_FILE1_PATH = os.path.join(os.path.dirname(__file__), '../data/test1.pdf')
TEST_FILE2_PATH = os.path.join(os.path.dirname(__file__), '../data/test2.pdf')
def test_no_differences():
- differences = compare_pdf_files(TEST_FILE1_PATH, TEST_FILE1_PATH)
- assert len(differences) == 0
+ difference = compare_pdf_files(TEST_FILE1_PATH, TEST_FILE1_PATH)
+ assert difference is None
@pytest.fixture
def differences():
- return compare_pdf_files(TEST_FILE1_PATH, TEST_FILE2_PATH)[0].details # skip container with path
+ return compare_pdf_files(TEST_FILE1_PATH, TEST_FILE2_PATH).details
def test_text_diff(differences):
expected_diff = open(os.path.join(os.path.dirname(__file__), '../data/pdf_text_expected_diff')).read()
diff --git a/tests/comparators/test_png.py b/tests/comparators/test_png.py
index 28dc2ec..3ef669c 100644
--- a/tests/comparators/test_png.py
+++ b/tests/comparators/test_png.py
@@ -27,12 +27,12 @@ TEST_FILE1_PATH = os.path.join(os.path.dirname(__file__), '../data/test1.png')
TEST_FILE2_PATH = os.path.join(os.path.dirname(__file__), '../data/test2.png')
def test_no_differences():
- differences = compare_png_files(TEST_FILE1_PATH, TEST_FILE1_PATH)
- assert len(differences) == 0
+ difference = compare_png_files(TEST_FILE1_PATH, TEST_FILE1_PATH)
+ assert difference is None
@pytest.fixture
def differences():
- return compare_png_files(TEST_FILE1_PATH, TEST_FILE2_PATH)[0].details # skip container with path
+ return compare_png_files(TEST_FILE1_PATH, TEST_FILE2_PATH).details
def test_diff(differences):
expected_diff = open(os.path.join(os.path.dirname(__file__), '../data/png_expected_diff')).read()
diff --git a/tests/comparators/test_rpm.py b/tests/comparators/test_rpm.py
index a8b5394..21eaf68 100644
--- a/tests/comparators/test_rpm.py
+++ b/tests/comparators/test_rpm.py
@@ -28,12 +28,12 @@ TEST_FILE1_PATH = os.path.join(os.path.dirname(__file__), '../data/test1.rpm')
TEST_FILE2_PATH = os.path.join(os.path.dirname(__file__), '../data/test2.rpm')
def test_no_differences():
- differences = compare_rpm_files(TEST_FILE1_PATH, TEST_FILE1_PATH)
- assert len(differences) == 0
+ difference = compare_rpm_files(TEST_FILE1_PATH, TEST_FILE1_PATH)
+ assert difference is None
@pytest.fixture
def differences():
- return compare_rpm_files(TEST_FILE1_PATH, TEST_FILE2_PATH)[0].details # skip container with path
+ return compare_rpm_files(TEST_FILE1_PATH, TEST_FILE2_PATH).details
def test_header(differences):
assert differences[0].source1 == 'header'
diff --git a/tests/comparators/test_squashfs.py b/tests/comparators/test_squashfs.py
index b1392ec..10b335e 100644
--- a/tests/comparators/test_squashfs.py
+++ b/tests/comparators/test_squashfs.py
@@ -28,18 +28,18 @@ TEST_FILE1_PATH = os.path.join(os.path.dirname(__file__), '../data/test1.squashf
TEST_FILE2_PATH = os.path.join(os.path.dirname(__file__), '../data/test2.squashfs')
def test_no_differences():
- differences = compare_squashfs_files(TEST_FILE1_PATH, TEST_FILE1_PATH)
- assert len(differences) == 0
+ difference = compare_squashfs_files(TEST_FILE1_PATH, TEST_FILE1_PATH)
+ assert difference is None
@pytest.mark.xfail
def test_no_warnings(capfd, differences):
- compare_squashfs_files(TEST_FILE1_PATH, TEST_FILE2_PATH)[0].details # skip container with path
+ compare_squashfs_files(TEST_FILE1_PATH, TEST_FILE2_PATH).details
_, err = capfd.readouterr()
assert err == ''
@pytest.fixture
def differences():
- return compare_squashfs_files(TEST_FILE1_PATH, TEST_FILE2_PATH)[0].details # skip container with path
+ return compare_squashfs_files(TEST_FILE1_PATH, TEST_FILE2_PATH).details
def test_superblock(differences):
expected_diff = open(os.path.join(os.path.dirname(__file__), '../data/squashfs_superblock_expected_diff')).read()
diff --git a/tests/comparators/test_text.py b/tests/comparators/test_text.py
index ef8059a..2ba5110 100644
--- a/tests/comparators/test_text.py
+++ b/tests/comparators/test_text.py
@@ -26,37 +26,36 @@ from debbindiff.comparators.text import compare_text_files
def test_no_differences():
text1 = os.path.join(os.path.dirname(__file__), '../data/text_ascii1')
text2 = os.path.join(os.path.dirname(__file__), '../data/text_ascii1')
- differences = compare_text_files(text1, text2)
- assert len(differences) == 0
+ assert compare_text_files(text1, text2) is None
def test_difference_in_ascii():
text1 = os.path.join(os.path.dirname(__file__), '../data/text_ascii1')
text2 = os.path.join(os.path.dirname(__file__), '../data/text_ascii2')
- differences = compare_text_files(text1, text2)
- assert len(differences) == 1
+ difference = compare_text_files(text1, text2)
+ assert difference is not None
expected_diff = open(os.path.join(os.path.dirname(__file__), '../data/text_ascii_expected_diff')).read()
- assert differences[0].unified_diff == expected_diff
- assert differences[0].comment is None
- assert len(differences[0].details) == 0
+ assert difference.unified_diff == expected_diff
+ assert difference.comment is None
+ assert len(difference.details) == 0
def test_difference_in_unicode():
text1 = os.path.join(os.path.dirname(__file__), '../data/text_unicode1')
text2 = os.path.join(os.path.dirname(__file__), '../data/text_unicode2')
- differences = compare_text_files(text1, text2)
+ difference = compare_text_files(text1, text2)
expected_diff = codecs.open(os.path.join(os.path.dirname(__file__), '../data/text_unicode_expected_diff'), encoding='utf-8').read()
- assert differences[0].unified_diff == expected_diff
+ assert difference.unified_diff == expected_diff
def test_fallback_to_binary():
text1 = os.path.join(os.path.dirname(__file__), '../data/text_unicode1')
text2 = os.path.join(os.path.dirname(__file__), '../data/text_unicode2')
- differences = compare_text_files(text1, text2, encoding='ascii')
+ difference = compare_text_files(text1, text2, encoding='ascii')
expected_diff = open(os.path.join(os.path.dirname(__file__), '../data/text_unicode_binary_fallback')).read()
- assert differences[0].unified_diff == expected_diff
+ assert difference.unified_diff == expected_diff
@pytest.mark.xfail
def test_difference_between_iso88591_and_unicode():
text1 = os.path.join(os.path.dirname(__file__), '../data/text_unicode1')
text2 = os.path.join(os.path.dirname(__file__), '../data/text_iso8859')
- differences = compare_text_files(text1, text2)
+ difference = compare_text_files(text1, text2)
expected_diff = codecs.open(os.path.join(os.path.dirname(__file__), '../data/text_iso8859_expected_diff'), encoding='utf-8').read()
- assert differences[0].unified_diff == expected_diff
+ assert differences.unified_diff == expected_diff
diff --git a/tests/comparators/test_utils.py b/tests/comparators/test_utils.py
index 722202f..80f706e 100644
--- a/tests/comparators/test_utils.py
+++ b/tests/comparators/test_utils.py
@@ -23,14 +23,14 @@ import shutil
import subprocess
import pytest
from debbindiff import tool_required
-from debbindiff.comparators.utils import binary_fallback
+from debbindiff.comparators.utils import binary_fallback, returns_details
from debbindiff.difference import Difference
def test_same_binaries():
@binary_fallback
def mock_comparator(path1, path2, source=None):
raise Exception('should not be run')
- assert mock_comparator(__file__, __file__) == []
+ assert mock_comparator(__file__, __file__) is None
TEST_FILE1_PATH = os.path.join(os.path.dirname(__file__), '../data/text_unicode1')
TEST_FILE2_PATH = os.path.join(os.path.dirname(__file__), '../data/text_unicode2')
@@ -39,20 +39,18 @@ def test_return_differences():
d = Difference('diff', TEST_FILE1_PATH, TEST_FILE2_PATH, source='source')
@binary_fallback
def mock_comparator(path1, path2, source=None):
- return [d]
- differences = mock_comparator(TEST_FILE1_PATH, TEST_FILE2_PATH)
- assert len(differences) == 1
- assert differences[0].details == [d]
+ return d
+ difference = mock_comparator(TEST_FILE1_PATH, TEST_FILE2_PATH)
+ assert difference == d
def test_fallback():
@binary_fallback
def mock_comparator(path1, path2, source=None):
- return []
- differences = mock_comparator(TEST_FILE1_PATH, TEST_FILE2_PATH)
+ return None
+ difference = mock_comparator(TEST_FILE1_PATH, TEST_FILE2_PATH)
expected_diff = open(os.path.join(os.path.dirname(__file__), '../data/text_unicode_binary_fallback')).read()
- assert len(differences) == 1
- assert 'yet data differs' in differences[0].comment
- assert differences[0].unified_diff == expected_diff
+ assert 'yet data differs' in difference.comment
+ assert difference.unified_diff == expected_diff
def test_process_failed():
output = 'Free Jeremy Hammond'
@@ -60,12 +58,11 @@ def test_process_failed():
def mock_comparator(path1, path2, source=None):
subprocess.check_output(['sh', '-c', 'echo "%s"; exit 42' % output], shell=False)
raise Exception('should not be run')
- differences = mock_comparator(TEST_FILE1_PATH, TEST_FILE2_PATH)
+ difference = mock_comparator(TEST_FILE1_PATH, TEST_FILE2_PATH)
expected_diff = open(os.path.join(os.path.dirname(__file__), '../data/text_unicode_binary_fallback')).read()
- assert len(differences) == 1
- assert output in differences[0].comment
- assert '42' in differences[0].comment
- assert differences[0].unified_diff == expected_diff
+ assert output in difference.comment
+ assert '42' in difference.comment
+ assert difference.unified_diff == expected_diff
def test_tool_not_found(monkeypatch):
monkeypatch.setattr('debbindiff.RequiredToolNotFound.get_package', lambda _: 'some-package')
@@ -73,9 +70,25 @@ def test_tool_not_found(monkeypatch):
@tool_required('nonexistent')
def mock_comparator(path1, path2, source=None):
raise Exception('should not be run')
- differences = mock_comparator(TEST_FILE1_PATH, TEST_FILE2_PATH)
+ difference = mock_comparator(TEST_FILE1_PATH, TEST_FILE2_PATH)
expected_diff = open(os.path.join(os.path.dirname(__file__), '../data/text_unicode_binary_fallback')).read()
- assert len(differences) == 1
- assert 'nonexistent' in differences[0].comment
- assert 'some-package' in differences[0].comment
- assert differences[0].unified_diff == expected_diff
+ assert 'nonexistent' in difference.comment
+ assert 'some-package' in difference.comment
+ assert difference.unified_diff == expected_diff
+
+def test_no_details():
+ @returns_details
+ def mock_comparator(path1, path2, source=None):
+ return []
+ difference = mock_comparator(TEST_FILE1_PATH, TEST_FILE2_PATH)
+ assert difference is None
+
+def test_details():
+ d = Difference('diff', TEST_FILE1_PATH, TEST_FILE2_PATH)
+ @returns_details
+ def mock_comparator(path1, path2, source=None):
+ return [d]
+ difference = mock_comparator(TEST_FILE1_PATH, TEST_FILE2_PATH)
+ assert difference.source1 == TEST_FILE1_PATH
+ assert difference.source2 == TEST_FILE2_PATH
+ assert difference.details == [d]
diff --git a/tests/comparators/test_xz.py b/tests/comparators/test_xz.py
index cf51d61..e262b53 100644
--- a/tests/comparators/test_xz.py
+++ b/tests/comparators/test_xz.py
@@ -27,12 +27,12 @@ TEST_FILE1_PATH = os.path.join(os.path.dirname(__file__), '../data/test1.xz')
TEST_FILE2_PATH = os.path.join(os.path.dirname(__file__), '../data/test2.xz')
def test_no_differences():
- differences = compare_xz_files(TEST_FILE1_PATH, TEST_FILE1_PATH)
- assert len(differences) == 0
+ difference = compare_xz_files(TEST_FILE1_PATH, TEST_FILE1_PATH)
+ assert difference is None
@pytest.fixture
def differences():
- return compare_xz_files(TEST_FILE1_PATH, TEST_FILE2_PATH)[0].details # skip container with path
+ return compare_xz_files(TEST_FILE1_PATH, TEST_FILE2_PATH).details
def test_content_source(differences):
assert differences[0].source1 == 'test1'
@@ -43,9 +43,9 @@ def test_content_source_without_extension(tmpdir):
path2 = str(tmpdir.join('test2'))
shutil.copy(TEST_FILE1_PATH, path1)
shutil.copy(TEST_FILE2_PATH, path2)
- differences = compare_xz_files(path1, path2)[0].details # skip container
- assert differences[0].source1 == 'test1-content'
- assert differences[0].source2 == 'test2-content'
+ difference = compare_xz_files(path1, path2).details
+ assert difference[0].source1 == 'test1-content'
+ assert difference[0].source2 == 'test2-content'
def test_content_diff(differences):
expected_diff = open(os.path.join(os.path.dirname(__file__), '../data/text_ascii_expected_diff')).read()
diff --git a/tests/comparators/test_zip.py b/tests/comparators/test_zip.py
index 9713593..913dc4c 100644
--- a/tests/comparators/test_zip.py
+++ b/tests/comparators/test_zip.py
@@ -28,12 +28,12 @@ TEST_FILE1_PATH = os.path.join(os.path.dirname(__file__), '../data/test1.zip')
TEST_FILE2_PATH = os.path.join(os.path.dirname(__file__), '../data/test2.zip')
def test_no_differences():
- differences = compare_zip_files(TEST_FILE1_PATH, TEST_FILE1_PATH)
- assert len(differences) == 0
+ difference = compare_zip_files(TEST_FILE1_PATH, TEST_FILE1_PATH)
+ assert difference is None
@pytest.fixture
def differences():
- return compare_zip_files(TEST_FILE1_PATH, TEST_FILE2_PATH)[0].details # skip container with path
+ return compare_zip_files(TEST_FILE1_PATH, TEST_FILE2_PATH).details
def test_compressed_files(differences):
assert differences[0].source1 == 'dir/text'
@@ -46,7 +46,7 @@ def test_metadata(differences):
assert differences[-1].unified_diff == expected_diff
def test_bad_zip():
- differences = compare_zip_files(os.path.join(os.path.dirname(__file__), '../data/text_unicode1'),
+ difference = compare_zip_files(os.path.join(os.path.dirname(__file__), '../data/text_unicode1'),
os.path.join(os.path.dirname(__file__), '../data/text_unicode2'))
expected_diff = open(os.path.join(os.path.dirname(__file__), '../data/text_unicode_binary_fallback')).read()
- assert differences[0].unified_diff == expected_diff
+ assert difference.unified_diff == expected_diff
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/debbindiff.git
More information about the Reproducible-commits
mailing list