[diffoscope] 01/02: comparators/utils/file: diff container metadata centrally here. (Closes: #797759)
Ximin Luo
infinity0 at debian.org
Mon Oct 9 20:10:51 CEST 2017
This is an automated email from the git hooks/post-receive script.
infinity0 pushed a commit to branch master
in repository diffoscope.
commit ae3f2f05e36597ac777276ced63f7507b6214664
Author: Ximin Luo <infinity0 at debian.org>
Date: Mon Oct 9 19:47:37 2017 +0200
comparators/utils/file: diff container metadata centrally here. (Closes: #797759)
This fixes a last remaining bug in fuzzy-matching across containers.
---
diffoscope/comparators/gzip.py | 3 -
diffoscope/comparators/utils/file.py | 6 ++
tests/comparators/test_containers.py | 98 +++++++--------------
tests/data/containers/b.tar.gz | Bin 532 -> 532 bytes
...bzip2_xz_diff => different_files_expected_diff} | 0
.../different_files_expected_gzip_bzip2_diff | 45 ----------
.../different_files_expected_gzip_bzip2_meta | 3 -
.../different_files_expected_gzip_xz_diff | 45 ----------
.../different_files_expected_gzip_xz_meta | 3 -
.../containers/equal_files_expected_bzip_xz_diff | 3 -
.../equal_files_expected_gzip_bzip2_diff | 3 -
tests/data/containers/magic_bzip2 | 1 +
tests/data/containers/magic_gzip | 1 +
tests/data/containers/magic_xz | 1 +
14 files changed, 40 insertions(+), 172 deletions(-)
diff --git a/diffoscope/comparators/gzip.py b/diffoscope/comparators/gzip.py
index 31843d6..3ff131c 100644
--- a/diffoscope/comparators/gzip.py
+++ b/diffoscope/comparators/gzip.py
@@ -59,6 +59,3 @@ class GzipFile(File):
# Work around file(1) Debian bug #876316
FALLBACK_FILE_EXTENSION_SUFFIX = ".gz"
FALLBACK_FILE_TYPE_HEADER_PREFIX = b"\x1f\x8b"
-
- def compare_details(self, other, source=None):
- return [Difference.from_text(self.magic_file_type, other.magic_file_type, self, other, source='metadata')]
diff --git a/diffoscope/comparators/utils/file.py b/diffoscope/comparators/utils/file.py
index c4b20c4..f5653fc 100644
--- a/diffoscope/comparators/utils/file.py
+++ b/diffoscope/comparators/utils/file.py
@@ -296,6 +296,12 @@ class File(object, metaclass=abc.ABCMeta):
if hasattr(self, 'compare_details'):
details.extend(self.compare_details(other, source))
if self.as_container:
+ details.extend([
+ Difference.from_text(self.magic_file_type, other.magic_file_type, self, other,
+ source='filetype from file(1)'),
+ Difference.from_text(self.__class__.__name__, other.__class__.__name__, self, other,
+ source='filetype from diffoscope'),
+ ])
# Don't recursve forever on archive quines, etc.
depth = self._as_container.depth
no_recurse = (depth >= Config().max_container_depth)
diff --git a/tests/comparators/test_containers.py b/tests/comparators/test_containers.py
index 1d2eb70..5660681 100644
--- a/tests/comparators/test_containers.py
+++ b/tests/comparators/test_containers.py
@@ -19,6 +19,8 @@
import pytest
+import itertools
+
from ..utils.data import load_fixture, get_data
gzip1 = load_fixture('containers/a.tar.gz')
@@ -30,80 +32,42 @@ xz2 = load_fixture('containers/b.tar.xz')
bzip1 = load_fixture('containers/a.tar.bz2')
bzip2 = load_fixture('containers/b.tar.bz2')
+TYPES = "gzip bzip2 xz".split()
@pytest.fixture
-def differences_equal_gzip_xz(gzip1, xz1):
- return gzip1.compare(xz1).details
-
-
- at pytest.fixture
-def differences_equal_gzip_bzip2(gzip1, bzip1):
- return gzip1.compare(bzip1).details
-
-
- at pytest.fixture
-def differences_equal_bzip2_xz(bzip1, xz1):
- return bzip1.compare(xz1).details
-
+def set1(gzip1, bzip1, xz1):
+ return dict(zip(TYPES, [gzip1, bzip1, xz1]))
@pytest.fixture
-def differences_different_gzip_xz(gzip1, xz2):
- return gzip1.compare(xz2).details
-
+def set2(gzip2, bzip2, xz2):
+ return dict(zip(TYPES, [gzip2, bzip2, xz2]))
- at pytest.fixture
-def differences_different_gzip_bzip2(gzip1, bzip2):
- return gzip1.compare(bzip2).details
-
-
- at pytest.fixture
-def differences_different_bzip2_xz(bzip1, xz2):
- return bzip1.compare(xz2).details
+def expected_magic_diff(ext1, ext2):
+ meta1 = get_data('containers/magic_%s' % ext1)
+ meta2 = get_data('containers/magic_%s' % ext2)
+ return "@@ -1 +1 @@\n" + "-" + meta1 + "+" + meta2
+def expected_type_diff(ext1, ext2):
+ return "@@ -1 +1 @@\n-%sFile\n+%sFile\n" % (ext1.capitalize(), ext2.capitalize())
# Compares same content files, but with different extensions
-def test_equal_content_gzip_xz_diff(differences_equal_gzip_xz):
- expected_diff = get_data('containers/equal_files_expected_bzip_xz_diff')
- assert differences_equal_gzip_xz[0].unified_diff == expected_diff
-
-
-def test_equal_content_gzip_bzip2_diff(differences_equal_gzip_bzip2):
- expected_diff = get_data('containers/equal_files_expected_gzip_bzip2_diff')
- assert differences_equal_gzip_bzip2[0].unified_diff == expected_diff
-
-
- at pytest.mark.skip(reason="Behavior not matching previous containers. Check needed.")
-def test_equal_content_bzip2_xz_diff(differences_equal_bzip2_xz):
- assert differences_equal_bzip2_xz == []
-
+def test_equal(set1):
+ for x, y in itertools.permutations(TYPES, 2):
+ differences = set1[x].compare(set1[y]).details
+ if x == y:
+ assert differences is None
+ else:
+ assert differences[0].unified_diff == expected_magic_diff(x, y)
+ assert differences[1].unified_diff == expected_type_diff(x, y)
# Compares different content files with different extensions
-def test_different_content_gzip_xz_meta(differences_different_gzip_xz):
- expected_diff = get_data('containers/different_files_expected_gzip_xz_meta')
- assert differences_different_gzip_xz[0].unified_diff == expected_diff
-
-
-def test_different_content_gzip_xz_diff(differences_different_gzip_xz):
- expected_diff = get_data('containers/different_files_expected_gzip_xz_diff')
- assert differences_different_gzip_xz[1].details[1].unified_diff == expected_diff
-
-
-def test_different_content_gzip_bzip2_meta(differences_different_gzip_bzip2):
- expected_diff = get_data('containers/different_files_expected_gzip_bzip2_meta')
- assert differences_different_gzip_bzip2[0].unified_diff == expected_diff
-
-
-def test_different_content_gzip_bzip2_diff(differences_different_gzip_bzip2):
- expected_diff = get_data('containers/different_files_expected_gzip_bzip2_diff')
- assert differences_different_gzip_bzip2[1].details[1].unified_diff == expected_diff
-
-
- at pytest.mark.skip(reason="Behavior not matching previous containers. Check needed.")
-def test_different_content_bzip2_xz_meta(differences_different_bzip2_xz):
- expected_diff = get_data('containers/different_files_expected_bzip2_xz_meta')
- assert differences_different_bzip2_xz[0].unified_diff == expected_diff
-
-
-def test_different_content_bzip2_xz_diff(differences_different_bzip2_xz):
- expected_diff = get_data('containers/different_files_expected_bzip2_xz_diff')
- assert differences_different_bzip2_xz[0].details[1].unified_diff == expected_diff
+def test_different(set1, set2):
+ for x, y in itertools.permutations(TYPES, 2):
+ expected_diff = get_data('containers/different_files_expected_diff')
+ differences = set1[x].compare(set2[y]).details
+ if x == y:
+ assert differences is None
+ else:
+ assert differences[0].unified_diff == expected_magic_diff(x, y)
+ assert differences[1].unified_diff == expected_type_diff(x, y)
+ assert differences[2].details[1].unified_diff == expected_diff
diff --git a/tests/data/containers/b.tar.gz b/tests/data/containers/b.tar.gz
index e268938..7042ca1 100644
Binary files a/tests/data/containers/b.tar.gz and b/tests/data/containers/b.tar.gz differ
diff --git a/tests/data/containers/different_files_expected_bzip2_xz_diff b/tests/data/containers/different_files_expected_diff
similarity index 100%
rename from tests/data/containers/different_files_expected_bzip2_xz_diff
rename to tests/data/containers/different_files_expected_diff
diff --git a/tests/data/containers/different_files_expected_gzip_bzip2_diff b/tests/data/containers/different_files_expected_gzip_bzip2_diff
deleted file mode 100644
index e06800c..0000000
--- a/tests/data/containers/different_files_expected_gzip_bzip2_diff
+++ /dev/null
@@ -1,45 +0,0 @@
-@@ -1,14 +1,32 @@
--Cat's foot iron claw
--Neuro-surgeons scream for more
--At paranoia's poison door
--Twenty first century schizoid man
-+Living on a lighted stage
-+Approaches the unreal
-+For those who think and feel
-+In touch with some reality
-+Beyond the gilded cage
-
--Blood rack barbed wire
--Politicians' funeral pyre
--Innocents raped with napalm fire
--Twenty first century schizoid man
-+Cast in this unlikely role
-+Ill-equipped to act
-+With insufficient tact
-+One must put up barriers
-+To keep oneself intact
-
--Death seed blind man's greed
--Poets' starving children bleed
--Nothing he's got he really needs
--Twenty first century schizoid man
-+Living in the limelight
-+The universal dream
-+For those who wish to seem
-+Those who wish to be
-+Must put aside the alienation
-+Get on with the fascination
-+The real relation
-+The underlying theme
-+
-+Living in a fish eye lens
-+Caught in the camera eye
-+I have no heart to lie
-+I can't pretend a stranger
-+Is a long-awaited friend
-+
-+All the world's indeed a stage
-+And we are merely players
-+Performers and portrayers
-+Each another's audience
-+Outside the gilded cage
diff --git a/tests/data/containers/different_files_expected_gzip_bzip2_meta b/tests/data/containers/different_files_expected_gzip_bzip2_meta
deleted file mode 100644
index 33e2cce..0000000
--- a/tests/data/containers/different_files_expected_gzip_bzip2_meta
+++ /dev/null
@@ -1,3 +0,0 @@
-@@ -1 +1 @@
--gzip compressed data, last modified: Sun Sep 10 22:19:44 2017, from Unix
-+bzip2 compressed data, block size = 900k
diff --git a/tests/data/containers/different_files_expected_gzip_xz_diff b/tests/data/containers/different_files_expected_gzip_xz_diff
deleted file mode 100644
index e06800c..0000000
--- a/tests/data/containers/different_files_expected_gzip_xz_diff
+++ /dev/null
@@ -1,45 +0,0 @@
-@@ -1,14 +1,32 @@
--Cat's foot iron claw
--Neuro-surgeons scream for more
--At paranoia's poison door
--Twenty first century schizoid man
-+Living on a lighted stage
-+Approaches the unreal
-+For those who think and feel
-+In touch with some reality
-+Beyond the gilded cage
-
--Blood rack barbed wire
--Politicians' funeral pyre
--Innocents raped with napalm fire
--Twenty first century schizoid man
-+Cast in this unlikely role
-+Ill-equipped to act
-+With insufficient tact
-+One must put up barriers
-+To keep oneself intact
-
--Death seed blind man's greed
--Poets' starving children bleed
--Nothing he's got he really needs
--Twenty first century schizoid man
-+Living in the limelight
-+The universal dream
-+For those who wish to seem
-+Those who wish to be
-+Must put aside the alienation
-+Get on with the fascination
-+The real relation
-+The underlying theme
-+
-+Living in a fish eye lens
-+Caught in the camera eye
-+I have no heart to lie
-+I can't pretend a stranger
-+Is a long-awaited friend
-+
-+All the world's indeed a stage
-+And we are merely players
-+Performers and portrayers
-+Each another's audience
-+Outside the gilded cage
diff --git a/tests/data/containers/different_files_expected_gzip_xz_meta b/tests/data/containers/different_files_expected_gzip_xz_meta
deleted file mode 100644
index f281ee3..0000000
--- a/tests/data/containers/different_files_expected_gzip_xz_meta
+++ /dev/null
@@ -1,3 +0,0 @@
-@@ -1 +1 @@
--gzip compressed data, last modified: Sun Sep 10 22:19:44 2017, from Unix
-+XZ compressed data
diff --git a/tests/data/containers/equal_files_expected_bzip_xz_diff b/tests/data/containers/equal_files_expected_bzip_xz_diff
deleted file mode 100644
index f281ee3..0000000
--- a/tests/data/containers/equal_files_expected_bzip_xz_diff
+++ /dev/null
@@ -1,3 +0,0 @@
-@@ -1 +1 @@
--gzip compressed data, last modified: Sun Sep 10 22:19:44 2017, from Unix
-+XZ compressed data
diff --git a/tests/data/containers/equal_files_expected_gzip_bzip2_diff b/tests/data/containers/equal_files_expected_gzip_bzip2_diff
deleted file mode 100644
index 33e2cce..0000000
--- a/tests/data/containers/equal_files_expected_gzip_bzip2_diff
+++ /dev/null
@@ -1,3 +0,0 @@
-@@ -1 +1 @@
--gzip compressed data, last modified: Sun Sep 10 22:19:44 2017, from Unix
-+bzip2 compressed data, block size = 900k
diff --git a/tests/data/containers/magic_bzip2 b/tests/data/containers/magic_bzip2
new file mode 100644
index 0000000..422d0b4
--- /dev/null
+++ b/tests/data/containers/magic_bzip2
@@ -0,0 +1 @@
+bzip2 compressed data, block size = 900k
diff --git a/tests/data/containers/magic_gzip b/tests/data/containers/magic_gzip
new file mode 100644
index 0000000..8a2d8a2
--- /dev/null
+++ b/tests/data/containers/magic_gzip
@@ -0,0 +1 @@
+gzip compressed data, last modified: Sun Sep 10 22:19:44 2017, from Unix
diff --git a/tests/data/containers/magic_xz b/tests/data/containers/magic_xz
new file mode 100644
index 0000000..8ab73b6
--- /dev/null
+++ b/tests/data/containers/magic_xz
@@ -0,0 +1 @@
+XZ compressed data
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/diffoscope.git
More information about the diffoscope
mailing list