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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
#
# diffoscope: in-depth comparison of files, archives, and directories
#
# Copyright © 2015 Jérémy Bobbio <lunar@debian.org>
# Copyright © 2015-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 codecs
import os
import pytest
import threading
from diffoscope.config import Config
from diffoscope.difference import Difference
from diffoscope.comparators.utils.command import Command
from ..utils.data import data, load_fixture
from ..utils.tools import (
tools_missing,
skip_unless_tools_exist,
skip_unless_module_exists,
)
fuzzy_tar1 = load_fixture("fuzzy1.tar")
fuzzy_tar2 = load_fixture("fuzzy2.tar")
fuzzy_tar3 = load_fixture("fuzzy3.tar")
def test_tools_missing():
assert tools_missing() is True
assert tools_missing("/missing") is True
for x in ["cat", "sh"]:
assert tools_missing(x) is False
@skip_unless_tools_exist()
def test_skip_unless_tools_exist_empty():
pytest.xfail("Test should always be skipped")
@skip_unless_tools_exist("/missing")
def test_skip_unless_tools_exist_missing():
pytest.xfail("Test should always be skipped")
def skip_unless_tool_is_at_least():
func = skip_unless_tool_is_at_least
assert func("/missing", 1, 1).name == "skip"
# pytest.skipif().args[0] contains the evaluated statement
assert func("cat", 1, 1).args[0] is False
assert func("cat", 1, "1.2d.45+b8").args[0] is True
def version():
return "4.3-git"
assert func("cat", version, "4.3").args[0] is False
@skip_unless_module_exists("tlsh")
def test_fuzzy_matching(fuzzy_tar1, fuzzy_tar2):
differences = fuzzy_tar1.compare(fuzzy_tar2).details
expected_diff = codecs.open(
data("text_iso8859_expected_diff"), encoding="utf-8"
).read()
assert differences[1].source1 == "./matching"
assert differences[1].source2 == "./fuzzy"
assert "similar" in differences[1].comment
assert differences[1].unified_diff == expected_diff
@skip_unless_module_exists("tlsh")
def test_fuzzy_matching_only_once(fuzzy_tar1, fuzzy_tar3):
differences = fuzzy_tar1.compare(fuzzy_tar3).details
assert len(differences) == 2
fuzzy_tar_in_tar1 = load_fixture("fuzzy-tar-in-tar1.tar")
fuzzy_tar_in_tar2 = load_fixture("fuzzy-tar-in-tar2.tar")
@skip_unless_module_exists("tlsh")
def test_no_fuzzy_matching(monkeypatch, fuzzy_tar_in_tar1, fuzzy_tar_in_tar2):
monkeypatch.setattr(Config(), "fuzzy_threshold", 0)
difference = fuzzy_tar_in_tar1.compare(fuzzy_tar_in_tar2)
assert len(difference.details) == 1
assert difference.details[0].source1 == "file list"
@skip_unless_module_exists("tlsh")
def test_no_fuzzy_matching_new_file(
monkeypatch, fuzzy_tar_in_tar1, fuzzy_tar_in_tar2
):
monkeypatch.setattr(Config(), "fuzzy_threshold", 0)
monkeypatch.setattr(Config(), "new_file", True)
difference = fuzzy_tar_in_tar1.compare(fuzzy_tar_in_tar2)
assert len(difference.details) == 3
assert difference.details[1].source2 == "/dev/null"
assert difference.details[2].source1 == "/dev/null"
@skip_unless_tools_exist("tee")
def test_trim_stderr_in_command():
class FillStderr(Command):
def cmdline(self):
return ["tee", "/dev/stderr"]
def stdin(self):
r, w = os.pipe()
r, w = os.fdopen(r), os.fdopen(w, "w")
def write():
for _ in range(0, Command.MAX_STDERR_LINES + 1):
w.write("error {}\n".format(self.path))
threading.Thread(target=write).start()
return r
difference = Difference.from_operation(FillStderr, "dummy1", "dummy2")
assert (
"[ truncated after {} lines; 1 ignored ]".format(
Command.MAX_STDERR_LINES
)
in difference.comment
)
|