Commit ae3f2f05 authored by Ximin Luo's avatar Ximin Luo

comparators/utils/file: diff container metadata centrally here. (Closes: #797759)

This fixes a last remaining bug in fuzzy-matching across containers.
parent 70cb725d
......@@ -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')]
......@@ -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)
......
......@@ -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
@pytest.fixture
def differences_equal_gzip_bzip2(gzip1, bzip1):
return gzip1.compare(bzip1).details
@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]))
@pytest.fixture
def differences_different_gzip_bzip2(gzip1, bzip2):
return gzip1.compare(bzip2).details
@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
@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
@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
@@ -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
@@ -1 +1 @@
-gzip compressed data, last modified: Sun Sep 10 22:19:44 2017, from Unix
+bzip2 compressed data, block size = 900k
@@ -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
@@ -1 +1 @@
-gzip compressed data, last modified: Sun Sep 10 22:19:44 2017, from Unix
+XZ compressed data
@@ -1 +1 @@
-gzip compressed data, last modified: Sun Sep 10 22:19:44 2017, from Unix
+XZ compressed data
@@ -1 +1 @@
-gzip compressed data, last modified: Sun Sep 10 22:19:44 2017, from Unix
+bzip2 compressed data, block size = 900k
bzip2 compressed data, block size = 900k
gzip compressed data, last modified: Sun Sep 10 22:19:44 2017, from Unix
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment