Skip to content
Snippets Groups Projects
Commit 94d08db4 authored by Chris Lamb's avatar Chris Lamb :eyes:
Browse files

Fix a gnarly regression when comparing directories against non-directories....

Fix a gnarly regression when comparing directories against non-directories. (Closes: #292)
parent fc46f9f1
No related branches found
No related tags found
No related merge requests found
Pipeline #352912 passed
...@@ -82,9 +82,11 @@ def compare_root_paths(path1, path2): ...@@ -82,9 +82,11 @@ def compare_root_paths(path1, path2):
# DESCRIPTION attributes either, as the specialize() call above may not # DESCRIPTION attributes either, as the specialize() call above may not
# have returned a specific instance. # have returned a specific instance.
if difference is None and str(type(file1)) != str(type(file2)): if difference is None and str(type(file1)) != str(type(file2)):
difference = Difference(file1.name, file2.name) return Difference.from_text(
difference.add_comment( "type: {}".format(file1.file_type),
"Types of files differ; human-readable metadata may match literal file contents." "type: {}".format(file2.file_type),
file1.name,
file2.name,
) )
return difference return difference
...@@ -136,6 +138,11 @@ def compare_files(file1, file2, source=None, diff_content_only=False): ...@@ -136,6 +138,11 @@ def compare_files(file1, file2, source=None, diff_content_only=False):
): ):
return file1.compare_bytes(file2, source) return file1.compare_bytes(file2, source)
with profile("compare_files (cumulative)", file1): with profile("compare_files (cumulative)", file1):
if file2.is_directory():
difference = file2.compare(file1, source)
if difference is not None:
difference = difference.get_reverse()
return difference
return file1.compare(file2, source) return file1.compare(file2, source)
......
...@@ -23,6 +23,7 @@ import pytest ...@@ -23,6 +23,7 @@ import pytest
from diffoscope.comparators.binary import FilesystemFile from diffoscope.comparators.binary import FilesystemFile
from diffoscope.comparators.directory import compare_directories from diffoscope.comparators.directory import compare_directories
from diffoscope.comparators.utils.compare import compare_root_paths
from diffoscope.comparators.utils.specialize import specialize from diffoscope.comparators.utils.specialize import specialize
from ..utils.data import data, get_data, assert_diff from ..utils.data import data, get_data, assert_diff
...@@ -113,14 +114,14 @@ def test_compare_to_dangling_symlink(tmpdir): ...@@ -113,14 +114,14 @@ def test_compare_to_dangling_symlink(tmpdir):
assert_diff(a.compare(b), "test_directory_symlink_diff") assert_diff(a.compare(b), "test_directory_symlink_diff")
@pytest.mark.xfail(strict=False)
def test_compare_both_ways(tmpdir): def test_compare_both_ways(tmpdir):
""" """
Comparing a directory with a file shouldn't crash, but nor should as Comparing a directory with a file shouldn't crash, but nor should as
comparing a file with a directory either. (Re: #292) comparing a file with a directory either. (Re: #292)
""" """
a = specialize(FilesystemFile(str(tmpdir))) a = str(tmpdir)
b = specialize(FilesystemFile(TEST_FILE1_PATH)) b = TEST_FILE1_PATH
a.compare(b)
b.compare(a) assert_diff(compare_root_paths(a, b), "test_directory_a_b_diff")
assert_diff(compare_root_paths(b, a), "test_directory_b_a_diff")
@@ -1 +1 @@
-type: directory
+type: file
@@ -1 +1 @@
+type: directory
-type: file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment