|
@@ -22,6 +22,8 @@
|
|
|
|
|
|
from io import BytesIO
|
|
|
import tarfile
|
|
|
+import struct
|
|
|
+from unittest import skipUnless
|
|
|
|
|
|
from dulwich.archive import tar_stream
|
|
|
from dulwich.object_store import (
|
|
@@ -38,6 +40,11 @@ from dulwich.tests.utils import (
|
|
|
build_commit_graph,
|
|
|
)
|
|
|
|
|
|
+try:
|
|
|
+ from mock import patch
|
|
|
+except ImportError:
|
|
|
+ patch = None
|
|
|
+
|
|
|
|
|
|
class ArchiveTests(TestCase):
|
|
|
|
|
@@ -51,15 +58,33 @@ class ArchiveTests(TestCase):
|
|
|
self.addCleanup(tf.close)
|
|
|
self.assertEqual([], tf.getnames())
|
|
|
|
|
|
- def test_simple(self):
|
|
|
+ def _get_example_tar_stream(self, *tar_stream_args, **tar_stream_kwargs):
|
|
|
store = MemoryObjectStore()
|
|
|
b1 = Blob.from_string(b"somedata")
|
|
|
store.add_object(b1)
|
|
|
t1 = Tree()
|
|
|
t1.add(b"somename", 0o100644, b1.id)
|
|
|
store.add_object(t1)
|
|
|
- stream = b''.join(tar_stream(store, t1, 10))
|
|
|
- out = BytesIO(stream)
|
|
|
- tf = tarfile.TarFile(fileobj=out)
|
|
|
+ stream = b''.join(tar_stream(store, t1, *tar_stream_args, **tar_stream_kwargs))
|
|
|
+ return BytesIO(stream)
|
|
|
+
|
|
|
+ def test_simple(self):
|
|
|
+ stream = self._get_example_tar_stream(mtime=0)
|
|
|
+ tf = tarfile.TarFile(fileobj=stream)
|
|
|
self.addCleanup(tf.close)
|
|
|
self.assertEqual(["somename"], tf.getnames())
|
|
|
+
|
|
|
+ def test_gzip_mtime(self):
|
|
|
+ stream = self._get_example_tar_stream(mtime=1234, format='gz')
|
|
|
+ expected_mtime = struct.pack('<L', 1234)
|
|
|
+ self.assertEqual(stream.getvalue()[4:8], expected_mtime)
|
|
|
+
|
|
|
+ @skipUnless(patch, "Required mock.patch")
|
|
|
+ def test_same_file(self):
|
|
|
+ contents = [None, None]
|
|
|
+ for format in ['', 'gz', 'bz2']:
|
|
|
+ for i in [0, 1]:
|
|
|
+ with patch('time.time', return_value=i):
|
|
|
+ stream = self._get_example_tar_stream(mtime=0, format=format)
|
|
|
+ contents[i] = stream.getvalue()
|
|
|
+ self.assertEqual(contents[0], contents[1], "Different file contents for format %r" % format)
|