File: test_containers.py

package info (click to toggle)
diffoscope 177
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,340 kB
  • sloc: python: 14,727; makefile: 97; sh: 81; xml: 26; javascript: 2
file content (109 lines) | stat: -rw-r--r-- 3,451 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#
# diffoscope: in-depth comparison of files, archives, and directories
#
# Copyright © 2017 Juliana Oliveira R <juliana.orod@gmail.org>
# Copyright © 2017-2020 Chris Lamb <lamby@debian.org>
#
# diffoscope is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# diffoscope is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with diffoscope.  If not, see <https://www.gnu.org/licenses/>.

import pytest

import itertools

from ..utils.data import load_fixture, get_data
from ..utils.tools import (
    skip_unless_tools_exist,
    file_version_is_ge,
    skip_unless_file_version_is_at_least,
)

gzip1 = load_fixture("containers/a.tar.gz")
gzip2 = load_fixture("containers/b.tar.gz")

xz1 = load_fixture("containers/a.tar.xz")
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 set1(gzip1, bzip1, xz1):
    return dict(zip(TYPES, [gzip1, bzip1, xz1]))


@pytest.fixture
def set2(gzip2, bzip2, xz2):
    return dict(zip(TYPES, [gzip2, bzip2, xz2]))


def expected_magic_diff(ext1, ext2):
    magic = {
        "bzip2": "bzip2 compressed data, block size = 900k\n",
        "gzip": "gzip compressed data, last modified: Sun Sep 10 22:19:44 2017, from Unix\n",
        "xz": "XZ compressed data",
    }
    if "xz" in (ext1, ext2):
        if file_version_is_ge("5.40"):
            magic["xz"] += ", checksum CRC64\n"
        else:
            magic["xz"] += "\n"

    return "@@ -1 +1 @@\n" + "-" + magic[ext1] + "+" + magic[ext2]


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


@skip_unless_tools_exist("xz")
@skip_unless_file_version_is_at_least("5.37")
def test_equal(set1):
    for x, y in itertools.product(TYPES, TYPES):
        diff = set1[x].compare(set1[y])
        if x == y:
            assert diff is None
        else:
            differences = diff.details
            assert differences[0].unified_diff == expected_magic_diff(
                x, y
            ), "{} {}".format(x, y)
            assert differences[1].unified_diff == expected_type_diff(x, y)


# Compares different content files with different extensions


@skip_unless_tools_exist("xz")
@skip_unless_file_version_is_at_least("5.37")
def test_different(set1, set2):
    for x, y in itertools.product(TYPES, TYPES):
        expected_diff = get_data("containers/different_files_expected_diff")
        differences = set1[x].compare(set2[y]).details
        if x == y:
            assert differences[0].details[1].unified_diff == expected_diff
        else:
            assert differences[0].unified_diff == expected_magic_diff(
                x, y
            ), "{} {}".format(x, y)
            assert differences[1].unified_diff == expected_type_diff(x, y)
            assert differences[2].details[1].unified_diff == expected_diff