2
0

test_archive.py 3.8 KB

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