Explorar o código

Added tests for write_object_diff with binary files

Added tests for patch.write_object_diff's binary comparison
behaviour.

Signed-off-by: Jelmer Vernooij <jelmer@samba.org>
Takeshi Kanemoto %!s(int64=12) %!d(string=hai) anos
pai
achega
8f12310613
Modificáronse 1 ficheiros con 51 adicións e 0 borrados
  1. 51 0
      dulwich/tests/test_patch.py

+ 51 - 0
dulwich/tests/test_patch.py

@@ -369,6 +369,57 @@ class DiffTests(TestCase):
             '-same'
             ], f.getvalue().splitlines())
 
+    def test_object_diff_bin_blob(self):
+        f = StringIO()
+        # Prepare two slightly different PNG headers
+        b1 = Blob.from_string(
+            "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52"
+            "\x00\x00\x01\xd5\x00\x00\x00\x9f\x08\x04\x00\x00\x00\x05\x04\x8b")
+        b2 = Blob.from_string(
+            "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52"
+            "\x00\x00\x01\xd5\x00\x00\x00\x9f\x08\x03\x00\x00\x00\x98\xd3\xb3")
+        store = MemoryObjectStore()
+        store.add_objects([(b1, None), (b2, None)])
+        write_object_diff(f, store, ('foo.png', 0644, b1.id),
+                                    ('bar.png', 0644, b2.id))
+        self.assertEqual([
+            'diff --git a/foo.png b/bar.png',
+            'index f73e47d..06364b7 644',
+            'Binary files a/foo.png and b/bar.png differ'
+            ], f.getvalue().splitlines())
+
+    def test_object_diff_add_bin_blob(self):
+        f = StringIO()
+        b2 = Blob.from_string(
+            '\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52'
+            '\x00\x00\x01\xd5\x00\x00\x00\x9f\x08\x03\x00\x00\x00\x98\xd3\xb3')
+        store = MemoryObjectStore()
+        store.add_object(b2)
+        write_object_diff(f, store, (None, None, None),
+                                    ('bar.png', 0644, b2.id))
+        self.assertEqual([
+            'diff --git /dev/null b/bar.png',
+            'new mode 644',
+            'index 0000000..06364b7 644',
+            'Binary files /dev/null and b/bar.png differ'
+            ], f.getvalue().splitlines())
+
+    def test_object_diff_remove_bin_blob(self):
+        f = StringIO()
+        b1 = Blob.from_string(
+            '\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52'
+            '\x00\x00\x01\xd5\x00\x00\x00\x9f\x08\x04\x00\x00\x00\x05\x04\x8b')
+        store = MemoryObjectStore()
+        store.add_object(b1)
+        write_object_diff(f, store, ('foo.png', 0644, b1.id),
+                                    (None, None, None))
+        self.assertEqual([
+            'diff --git a/foo.png /dev/null',
+            'deleted mode 644',
+            'index f73e47d..0000000',
+            'Binary files a/foo.png and /dev/null differ'
+            ], f.getvalue().splitlines())
+
     def test_object_diff_kind_change(self):
         f = StringIO()
         b1 = Blob.from_string("new\nsame\n")