[diffoscope] 01/01: Catch an error when we can't read files in has_same_content_as()

Mattia Rizzolo mattia at debian.org
Mon Sep 26 00:45:01 CEST 2016


This is an automated email from the git hooks/post-receive script.

mattia pushed a commit to branch master
in repository diffoscope.

commit ea750f17d3e2f3e248294e05d8b66cc901e5fa5c
Author: Maria Glukhova <siamezzze at gmail.com>
Date:   Sun Sep 25 19:07:22 2016 +0300

    Catch an error when we can't read files in has_same_content_as()
    
    Closes: #835642
    Signed-off-by: Mattia Rizzolo <mattia at debian.org>
---
 diffoscope/comparators/binary.py            | 9 +++++++--
 tests/comparators/test_binary.py            | 7 +++++++
 tests/data/dir_and_symlink_a/dir/.gitignore | 0
 tests/data/dir_and_symlink_a/lnk            | 1 +
 tests/data/dir_and_symlink_b/dir/.gitignore | 0
 tests/data/dir_and_symlink_b/lnk            | 1 +
 6 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/diffoscope/comparators/binary.py b/diffoscope/comparators/binary.py
index a72ad6f..4c6a6c4 100644
--- a/diffoscope/comparators/binary.py
+++ b/diffoscope/comparators/binary.py
@@ -199,8 +199,13 @@ class File(object, metaclass=abc.ABCMeta):
             # just assume they are different
             return False
         if my_size == other_size and my_size <= SMALL_FILE_THRESHOLD:
-            if open(self.path, 'rb').read() == open(other.path, 'rb').read():
-                return True
+            try:
+                with open(self.path, 'rb') as file1, open(other.path, 'rb') as file2:
+                    return file1.read() == file2.read()
+            except OSError:
+                # one or both files could not be opened for some reason,
+                # assume they are different
+                return False
 
         return 0 == subprocess.call(['cmp', '-s', self.path, other.path],
                                     shell=False, close_fds=True)
diff --git a/tests/comparators/test_binary.py b/tests/comparators/test_binary.py
index b160f03..79ee1fb 100644
--- a/tests/comparators/test_binary.py
+++ b/tests/comparators/test_binary.py
@@ -34,6 +34,8 @@ TEST_FILE2_PATH = data('binary2')
 TEST_ASCII_PATH = data('text_ascii1')
 TEST_UNICODE_PATH = data('text_unicode1')
 TEST_ISO8859_PATH = data('text_iso8859')
+TEST_SYMLINK2DIR1_PATH = data('dir_and_symlink_a')
+TEST_SYMLINK2DIR2_PATH = data('dir_and_symlink_b')
 
 
 binary1 = load_fixture(TEST_FILE1_PATH)
@@ -149,3 +151,8 @@ def test_compare_two_nonexisting_files():
     file2 = NonExistingFile('/nonexisting2')
     difference = file1.compare(file2)
     assert 'non-existing' in difference.comment
+
+def test_symlink_to_dir():
+    file1 = FilesystemFile(TEST_SYMLINK2DIR1_PATH)
+    file2 = FilesystemFile(TEST_SYMLINK2DIR2_PATH)
+    assert file1.has_same_content_as(file2) is False
diff --git a/tests/data/dir_and_symlink_a/dir/.gitignore b/tests/data/dir_and_symlink_a/dir/.gitignore
new file mode 100644
index 0000000..e69de29
diff --git a/tests/data/dir_and_symlink_a/lnk b/tests/data/dir_and_symlink_a/lnk
new file mode 120000
index 0000000..8724519
--- /dev/null
+++ b/tests/data/dir_and_symlink_a/lnk
@@ -0,0 +1 @@
+dir
\ No newline at end of file
diff --git a/tests/data/dir_and_symlink_b/dir/.gitignore b/tests/data/dir_and_symlink_b/dir/.gitignore
new file mode 100644
index 0000000..e69de29
diff --git a/tests/data/dir_and_symlink_b/lnk b/tests/data/dir_and_symlink_b/lnk
new file mode 120000
index 0000000..8724519
--- /dev/null
+++ b/tests/data/dir_and_symlink_b/lnk
@@ -0,0 +1 @@
+dir
\ No newline at end of file

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/diffoscope.git


More information about the diffoscope mailing list