test_bundle.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # test_bundle.py -- tests for bundle
  2. # Copyright (C) 2020 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 bundle support."""
  21. from io import BytesIO
  22. import os
  23. import tempfile
  24. from dulwich.tests import (
  25. TestCase,
  26. )
  27. from dulwich.bundle import (
  28. Bundle,
  29. read_bundle,
  30. write_bundle,
  31. )
  32. from dulwich.pack import (
  33. PackData,
  34. write_pack_objects,
  35. )
  36. class BundleTests(TestCase):
  37. def test_roundtrip_bundle(self):
  38. origbundle = Bundle()
  39. origbundle.version = 3
  40. origbundle.capabilities = {"foo": None}
  41. origbundle.references = {b"refs/heads/master": b"ab" * 20}
  42. origbundle.prerequisites = [(b"cc" * 20, "comment")]
  43. b = BytesIO()
  44. write_pack_objects(b.write, [])
  45. b.seek(0)
  46. origbundle.pack_data = PackData.from_file(b)
  47. with tempfile.TemporaryDirectory() as td:
  48. with open(os.path.join(td, "foo"), "wb") as f:
  49. write_bundle(f, origbundle)
  50. with open(os.path.join(td, "foo"), "rb") as f:
  51. newbundle = read_bundle(f)
  52. self.assertEqual(origbundle, newbundle)