2
0

test_pack.py 3.1 KB

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