test_pack.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # test_pack.py -- Tests for the handling of git packs.
  2. # Copyright (C) 2007 James Westby <jw+debian@jameswestby.net>
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; version 2
  7. # of the License.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. # MA 02110-1301, USA.
  18. import os
  19. import unittest
  20. from dulwich.pack import (
  21. PackIndex,
  22. PackData,
  23. hex_to_sha,
  24. )
  25. pack1_sha = 'bc63ddad95e7321ee734ea11a7a62d314e0d7481'
  26. a_sha = '6f670c0fb53f9463760b7295fbb814e965fb20c8'
  27. tree_sha = 'b2a2766a2879c209ab1176e7e778b81ae422eeaa'
  28. commit_sha = 'f18faa16531ac570a3fdc8c7ca16682548dafd12'
  29. class PackTests(unittest.TestCase):
  30. """Base class for testing packs"""
  31. datadir = os.path.join(os.path.dirname(__file__), 'data/packs')
  32. def get_pack_index(self, sha):
  33. """Returns a PackIndex from the datadir with the given sha"""
  34. return PackIndex(os.path.join(self.datadir, 'pack-%s.idx' % sha))
  35. def get_pack_data(self, sha):
  36. """Returns a PackData object from the datadir with the given sha"""
  37. return PackData(os.path.join(self.datadir, 'pack-%s.pack' % sha))
  38. class PackIndexTests(PackTests):
  39. """Class that tests the index of packfiles"""
  40. def test_object_index(self):
  41. """Tests that the correct object offset is returned from the index."""
  42. p = self.get_pack_index(pack1_sha)
  43. self.assertEqual(p.object_index(pack1_sha), None)
  44. self.assertEqual(p.object_index(a_sha), 178)
  45. self.assertEqual(p.object_index(tree_sha), 138)
  46. self.assertEqual(p.object_index(commit_sha), 12)
  47. class TestPackData(PackTests):
  48. """Tests getting the data from the packfile."""
  49. def test_create_pack(self):
  50. p = self.get_pack_data(pack1_sha)
  51. def test_get_object_at(self):
  52. """Tests random access for non-delta objects"""
  53. p = self.get_pack_data(pack1_sha)
  54. idx = self.get_pack_index(pack1_sha)
  55. obj = p.get_object_at(idx.object_index(a_sha))
  56. self.assertEqual(obj._type, 'blob')
  57. self.assertEqual(obj.sha().hexdigest(), a_sha)
  58. obj = p.get_object_at(idx.object_index(tree_sha))
  59. self.assertEqual(obj._type, 'tree')
  60. self.assertEqual(obj.sha().hexdigest(), tree_sha)
  61. obj = p.get_object_at(idx.object_index(commit_sha))
  62. self.assertEqual(obj._type, 'commit')
  63. self.assertEqual(obj.sha().hexdigest(), commit_sha)
  64. class TestHexToSha(unittest.TestCase):
  65. def test_simple(self):
  66. self.assertEquals(703710, hex_to_sha("abcde"))