[Git][reproducible-builds/diffoscope][master] 2 commits: Use our assert_diff utility where we can within test_directory.py

Chris Lamb (@lamby) gitlab at salsa.debian.org
Fri Feb 25 11:47:32 UTC 2022



Chris Lamb pushed to branch master at Reproducible Builds / diffoscope


Commits:
fc46f9f1 by Chris Lamb at 2022-02-25T11:10:53+00:00
Use our assert_diff utility where we can within test_directory.py

- - - - -
94d08db4 by Chris Lamb at 2022-02-25T11:46:02+00:00
Fix a gnarly regression when comparing directories against non-directories. (Closes: reproducible-builds/diffoscope#292)

- - - - -


4 changed files:

- diffoscope/comparators/utils/compare.py
- tests/comparators/test_directory.py
- + tests/data/test_directory_a_b_diff
- + tests/data/test_directory_b_a_diff


Changes:

=====================================
diffoscope/comparators/utils/compare.py
=====================================
@@ -82,9 +82,11 @@ def compare_root_paths(path1, path2):
     # DESCRIPTION attributes either, as the specialize() call above may not
     # have returned a specific instance.
     if difference is None and str(type(file1)) != str(type(file2)):
-        difference = Difference(file1.name, file2.name)
-        difference.add_comment(
-            "Types of files differ; human-readable metadata may match literal file contents."
+        return Difference.from_text(
+            "type: {}".format(file1.file_type),
+            "type: {}".format(file2.file_type),
+            file1.name,
+            file2.name,
         )
 
     return difference
@@ -136,6 +138,11 @@ def compare_files(file1, file2, source=None, diff_content_only=False):
     ):
         return file1.compare_bytes(file2, source)
     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)
 
 


=====================================
tests/comparators/test_directory.py
=====================================
@@ -23,9 +23,10 @@ import pytest
 
 from diffoscope.comparators.binary import FilesystemFile
 from diffoscope.comparators.directory import compare_directories
+from diffoscope.comparators.utils.compare import compare_root_paths
 from diffoscope.comparators.utils.specialize import specialize
 
-from ..utils.data import data, get_data
+from ..utils.data import data, get_data, assert_diff
 
 
 TEST_FILE1_PATH = data("text_ascii1")
@@ -83,14 +84,14 @@ def test_compare_to_file(tmpdir):
     a = specialize(FilesystemFile(str(tmpdir.mkdir("dir"))))
     b = specialize(FilesystemFile(path))
 
-    assert a.compare(b).unified_diff == get_data("test_directory_file_diff")
+    assert_diff(a.compare(b), "test_directory_file_diff")
 
 
 def test_compare_to_device(tmpdir):
     a = specialize(FilesystemFile(str(tmpdir.mkdir("dir"))))
     b = specialize(FilesystemFile("/dev/null"))
 
-    assert a.compare(b).unified_diff == get_data("test_directory_device_diff")
+    assert_diff(a.compare(b), "test_directory_device_diff")
 
 
 def test_compare_to_symlink(tmpdir):
@@ -100,7 +101,7 @@ def test_compare_to_symlink(tmpdir):
     a = specialize(FilesystemFile(str(tmpdir.mkdir("dir"))))
     b = specialize(FilesystemFile(path))
 
-    assert a.compare(b).unified_diff == get_data("test_directory_symlink_diff")
+    assert_diff(a.compare(b), "test_directory_symlink_diff")
 
 
 def test_compare_to_dangling_symlink(tmpdir):
@@ -110,17 +111,17 @@ def test_compare_to_dangling_symlink(tmpdir):
     a = specialize(FilesystemFile(str(tmpdir.mkdir("dir"))))
     b = specialize(FilesystemFile(path))
 
-    assert a.compare(b).unified_diff == get_data("test_directory_symlink_diff")
+    assert_diff(a.compare(b), "test_directory_symlink_diff")
 
 
- at pytest.mark.xfail(strict=False)
 def test_compare_both_ways(tmpdir):
     """
     Comparing a directory with a file shouldn't crash, but nor should as
     comparing a file with a directory either. (Re: #292)
     """
 
-    a = specialize(FilesystemFile(str(tmpdir)))
-    b = specialize(FilesystemFile(TEST_FILE1_PATH))
-    a.compare(b)
-    b.compare(a)
+    a = str(tmpdir)
+    b = TEST_FILE1_PATH
+
+    assert_diff(compare_root_paths(a, b), "test_directory_a_b_diff")
+    assert_diff(compare_root_paths(b, a), "test_directory_b_a_diff")


=====================================
tests/data/test_directory_a_b_diff
=====================================
@@ -0,0 +1,3 @@
+@@ -1 +1 @@
+-type: directory
++type: file


=====================================
tests/data/test_directory_b_a_diff
=====================================
@@ -0,0 +1,3 @@
+@@ -1 +1 @@
++type: directory
+-type: file



View it on GitLab: https://salsa.debian.org/reproducible-builds/diffoscope/-/compare/9c4dfee66fe1ff6d54b05db4e8fd95508fc5429e...94d08db4d27c82d1584c21e56bdda1142793221a

-- 
View it on GitLab: https://salsa.debian.org/reproducible-builds/diffoscope/-/compare/9c4dfee66fe1ff6d54b05db4e8fd95508fc5429e...94d08db4d27c82d1584c21e56bdda1142793221a
You're receiving this email because of your account on salsa.debian.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.reproducible-builds.org/pipermail/rb-commits/attachments/20220225/ece51070/attachment.htm>


More information about the rb-commits mailing list