test_pack.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 git.pack import (PackIndex,
  21. PackData,
  22. )
  23. pack1_sha = 'bc63ddad95e7321ee734ea11a7a62d314e0d7481'
  24. a_sha = '6f670c0fb53f9463760b7295fbb814e965fb20c8'
  25. tree_sha = 'b2a2766a2879c209ab1176e7e778b81ae422eeaa'
  26. commit_sha = 'f18faa16531ac570a3fdc8c7ca16682548dafd12'
  27. class PackTests(unittest.TestCase):
  28. """Base class for testing packs"""
  29. datadir = os.path.join(os.path.dirname(__file__), 'data/packs')
  30. def get_pack_index(self, sha):
  31. """Returns a PackIndex from the datadir with the given sha"""
  32. return PackIndex(os.path.join(self.datadir, 'pack-%s.idx' % sha))
  33. def get_pack_data(self, sha):
  34. """Returns a PackData object from the datadir with the given sha"""
  35. return PackData(os.path.join(self.datadir, 'pack-%s.pack' % sha))
  36. class PackIndexTests(PackTests):
  37. """Class that tests the index of packfiles"""
  38. def test_object_index(self):
  39. """Tests that the correct object offset is returned from the index."""
  40. p = self.get_pack_index(pack1_sha)
  41. self.assertEqual(p.object_index(pack1_sha), None)
  42. self.assertEqual(p.object_index(a_sha), 178)
  43. self.assertEqual(p.object_index(tree_sha), 138)
  44. self.assertEqual(p.object_index(commit_sha), 12)
  45. class TestPackData(PackTests):
  46. """Tests getting the data from the packfile."""
  47. def test_create_pack(self):
  48. p = self.get_pack_data(pack1_sha)
  49. def test_get_object_at(self):
  50. """Tests random access for non-delta objects"""
  51. p = self.get_pack_data(pack1_sha)
  52. idx = self.get_pack_index(pack1_sha)
  53. obj = p.get_object_at(idx.object_index(a_sha))
  54. self.assertEqual(obj._type, 'blob')
  55. self.assertEqual(obj.sha().hexdigest(), a_sha)
  56. obj = p.get_object_at(idx.object_index(tree_sha))
  57. self.assertEqual(obj._type, 'tree')
  58. self.assertEqual(obj.sha().hexdigest(), tree_sha)
  59. obj = p.get_object_at(idx.object_index(commit_sha))
  60. self.assertEqual(obj._type, 'commit')
  61. self.assertEqual(obj.sha().hexdigest(), commit_sha)