test_archive.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. from io import BytesIO
  22. import sys
  23. import tarfile
  24. from dulwich.archive import tar_stream
  25. from dulwich.object_store import (
  26. MemoryObjectStore,
  27. )
  28. from dulwich.objects import (
  29. Blob,
  30. Tree,
  31. )
  32. from dulwich.tests import (
  33. TestCase,
  34. )
  35. from dulwich.tests.utils import (
  36. build_commit_graph,
  37. )
  38. class ArchiveTests(TestCase):
  39. def test_empty(self):
  40. store = MemoryObjectStore()
  41. c1, c2, c3 = build_commit_graph(store, [[1], [2, 1], [3, 1, 2]])
  42. tree = store[c3.tree]
  43. stream = b''.join(tar_stream(store, tree, 10))
  44. out = BytesIO(stream)
  45. tf = tarfile.TarFile(fileobj=out)
  46. self.addCleanup(tf.close)
  47. self.assertEqual([], tf.getnames())
  48. def test_simple(self):
  49. store = MemoryObjectStore()
  50. b1 = Blob.from_string(b"somedata")
  51. store.add_object(b1)
  52. t1 = Tree()
  53. t1.add(b"somename", 0o100644, b1.id)
  54. store.add_object(t1)
  55. stream = b''.join(tar_stream(store, t1, 10))
  56. out = BytesIO(stream)
  57. tf = tarfile.TarFile(fileobj=out)
  58. self.addCleanup(tf.close)
  59. self.assertEqual(["somename"], tf.getnames())