test_archive.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # test_archive.py -- tests for archive
  2. # Copyright (C) 2015 Jelmer Vernooij <jelmer@jelmer.uk>
  3. #
  4. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  5. # General Public License as public by the Free Software Foundation; version 2.0
  6. # or (at your option) any later version. You can redistribute it and/or
  7. # modify it under the terms of either of these two licenses.
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15. # You should have received a copy of the licenses; if not, see
  16. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  17. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  18. # License, Version 2.0.
  19. #
  20. """Tests for archive support."""
  21. import struct
  22. import tarfile
  23. from io import BytesIO
  24. from unittest import skipUnless
  25. from dulwich.archive import tar_stream
  26. from dulwich.object_store import MemoryObjectStore
  27. from dulwich.objects import Blob, Tree
  28. from dulwich.tests.utils import build_commit_graph
  29. from . import TestCase
  30. try:
  31. from unittest.mock import patch
  32. except ImportError:
  33. patch = None # type: ignore
  34. class ArchiveTests(TestCase):
  35. def test_empty(self):
  36. store = MemoryObjectStore()
  37. c1, c2, c3 = build_commit_graph(store, [[1], [2, 1], [3, 1, 2]])
  38. tree = store[c3.tree]
  39. stream = b"".join(tar_stream(store, tree, 10))
  40. out = BytesIO(stream)
  41. tf = tarfile.TarFile(fileobj=out)
  42. self.addCleanup(tf.close)
  43. self.assertEqual([], tf.getnames())
  44. def _get_example_tar_stream(self, *tar_stream_args, **tar_stream_kwargs):
  45. store = MemoryObjectStore()
  46. b1 = Blob.from_string(b"somedata")
  47. store.add_object(b1)
  48. t1 = Tree()
  49. t1.add(b"somename", 0o100644, b1.id)
  50. store.add_object(t1)
  51. stream = b"".join(tar_stream(store, t1, *tar_stream_args, **tar_stream_kwargs))
  52. return BytesIO(stream)
  53. def test_simple(self):
  54. stream = self._get_example_tar_stream(mtime=0)
  55. tf = tarfile.TarFile(fileobj=stream)
  56. self.addCleanup(tf.close)
  57. self.assertEqual(["somename"], tf.getnames())
  58. def test_unicode(self):
  59. store = MemoryObjectStore()
  60. b1 = Blob.from_string(b"somedata")
  61. store.add_object(b1)
  62. t1 = Tree()
  63. t1.add("ő".encode(), 0o100644, b1.id)
  64. store.add_object(t1)
  65. stream = b"".join(tar_stream(store, t1, mtime=0))
  66. tf = tarfile.TarFile(fileobj=BytesIO(stream))
  67. self.addCleanup(tf.close)
  68. self.assertEqual(["ő"], tf.getnames())
  69. def test_prefix(self):
  70. stream = self._get_example_tar_stream(mtime=0, prefix=b"blah")
  71. tf = tarfile.TarFile(fileobj=stream)
  72. self.addCleanup(tf.close)
  73. self.assertEqual(["blah/somename"], tf.getnames())
  74. def test_gzip_mtime(self):
  75. stream = self._get_example_tar_stream(mtime=1234, format="gz")
  76. expected_mtime = struct.pack("<L", 1234)
  77. self.assertEqual(stream.getvalue()[4:8], expected_mtime)
  78. @skipUnless(patch, "Required mock.patch")
  79. def test_same_file(self):
  80. contents = [None, None]
  81. for format in ["", "gz", "bz2"]:
  82. for i in [0, 1]:
  83. with patch("time.time", return_value=i):
  84. stream = self._get_example_tar_stream(mtime=0, format=format)
  85. contents[i] = stream.getvalue()
  86. self.assertEqual(
  87. contents[0],
  88. contents[1],
  89. f"Different file contents for format {format!r}",
  90. )