2
0

test_object_store.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. # test_object_store.py -- tests for object_store.py
  2. # Copyright (C) 2008 Jelmer Vernooij <jelmer@samba.org>
  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. # or (at your option) any later version 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. """Tests for the object store interface."""
  19. import os
  20. import shutil
  21. import tempfile
  22. from dulwich.index import (
  23. commit_tree,
  24. )
  25. from dulwich.objects import (
  26. object_class,
  27. Blob,
  28. Tag,
  29. )
  30. from dulwich.object_store import (
  31. DiskObjectStore,
  32. MemoryObjectStore,
  33. )
  34. from dulwich.pack import (
  35. write_pack_data,
  36. )
  37. from dulwich.tests import (
  38. TestCase,
  39. )
  40. from utils import (
  41. make_object,
  42. )
  43. testobject = make_object(Blob, data="yummy data")
  44. class ObjectStoreTests(object):
  45. def test_iter(self):
  46. self.assertEquals([], list(self.store))
  47. def test_get_nonexistant(self):
  48. self.assertRaises(KeyError, lambda: self.store["a" * 40])
  49. def test_contains_nonexistant(self):
  50. self.assertFalse(("a" * 40) in self.store)
  51. def test_add_objects_empty(self):
  52. self.store.add_objects([])
  53. def test_add_commit(self):
  54. # TODO: Argh, no way to construct Git commit objects without
  55. # access to a serialized form.
  56. self.store.add_objects([])
  57. def test_add_object(self):
  58. self.store.add_object(testobject)
  59. self.assertEquals(set([testobject.id]), set(self.store))
  60. self.assertTrue(testobject.id in self.store)
  61. r = self.store[testobject.id]
  62. self.assertEquals(r, testobject)
  63. def test_add_objects(self):
  64. data = [(testobject, "mypath")]
  65. self.store.add_objects(data)
  66. self.assertEquals(set([testobject.id]), set(self.store))
  67. self.assertTrue(testobject.id in self.store)
  68. r = self.store[testobject.id]
  69. self.assertEquals(r, testobject)
  70. def test_iter_tree_contents(self):
  71. blob_a = make_object(Blob, data='a')
  72. blob_b = make_object(Blob, data='b')
  73. blob_c = make_object(Blob, data='c')
  74. for blob in [blob_a, blob_b, blob_c]:
  75. self.store.add_object(blob)
  76. blobs = [
  77. ('a', blob_a.id, 0100644),
  78. ('ad/b', blob_b.id, 0100644),
  79. ('ad/bd/c', blob_c.id, 0100755),
  80. ('ad/c', blob_c.id, 0100644),
  81. ('c', blob_c.id, 0100644),
  82. ]
  83. tree_id = commit_tree(self.store, blobs)
  84. self.assertEquals([(p, m, h) for (p, h, m) in blobs],
  85. list(self.store.iter_tree_contents(tree_id)))
  86. def test_iter_tree_contents_include_trees(self):
  87. blob_a = make_object(Blob, data='a')
  88. blob_b = make_object(Blob, data='b')
  89. blob_c = make_object(Blob, data='c')
  90. for blob in [blob_a, blob_b, blob_c]:
  91. self.store.add_object(blob)
  92. blobs = [
  93. ('a', blob_a.id, 0100644),
  94. ('ad/b', blob_b.id, 0100644),
  95. ('ad/bd/c', blob_c.id, 0100755),
  96. ]
  97. tree_id = commit_tree(self.store, blobs)
  98. tree = self.store[tree_id]
  99. tree_ad = self.store[tree['ad'][1]]
  100. tree_bd = self.store[tree_ad['bd'][1]]
  101. expected = [
  102. ('', 0040000, tree_id),
  103. ('a', 0100644, blob_a.id),
  104. ('ad', 0040000, tree_ad.id),
  105. ('ad/b', 0100644, blob_b.id),
  106. ('ad/bd', 0040000, tree_bd.id),
  107. ('ad/bd/c', 0100755, blob_c.id),
  108. ]
  109. actual = self.store.iter_tree_contents(tree_id, include_trees=True)
  110. self.assertEquals(expected, list(actual))
  111. def make_tag(self, name, obj):
  112. tag = make_object(Tag, name=name, message='',
  113. tag_time=12345, tag_timezone=0,
  114. tagger='Test Tagger <test@example.com>',
  115. object=(object_class(obj.type_name), obj.id))
  116. self.store.add_object(tag)
  117. return tag
  118. def test_peel_sha(self):
  119. self.store.add_object(testobject)
  120. tag1 = self.make_tag('1', testobject)
  121. tag2 = self.make_tag('2', testobject)
  122. tag3 = self.make_tag('3', testobject)
  123. for obj in [testobject, tag1, tag2, tag3]:
  124. self.assertEqual(testobject, self.store.peel_sha(obj.id))
  125. class MemoryObjectStoreTests(ObjectStoreTests, TestCase):
  126. def setUp(self):
  127. TestCase.setUp(self)
  128. self.store = MemoryObjectStore()
  129. class PackBasedObjectStoreTests(ObjectStoreTests):
  130. def test_empty_packs(self):
  131. self.assertEquals([], self.store.packs)
  132. def test_pack_loose_objects(self):
  133. b1 = make_object(Blob, data="yummy data")
  134. self.store.add_object(b1)
  135. b2 = make_object(Blob, data="more yummy data")
  136. self.store.add_object(b2)
  137. self.assertEquals([], self.store.packs)
  138. self.assertEquals(2, self.store.pack_loose_objects())
  139. self.assertNotEquals([], self.store.packs)
  140. self.assertEquals(0, self.store.pack_loose_objects())
  141. class DiskObjectStoreTests(PackBasedObjectStoreTests, TestCase):
  142. def setUp(self):
  143. TestCase.setUp(self)
  144. self.store_dir = tempfile.mkdtemp()
  145. self.store = DiskObjectStore.init(self.store_dir)
  146. def tearDown(self):
  147. TestCase.tearDown(self)
  148. shutil.rmtree(self.store_dir)
  149. def test_pack_dir(self):
  150. o = DiskObjectStore(self.store_dir)
  151. self.assertEquals(os.path.join(self.store_dir, "pack"), o.pack_dir)
  152. def test_add_pack(self):
  153. o = DiskObjectStore(self.store_dir)
  154. f, commit = o.add_pack()
  155. b = make_object(Blob, data="more yummy data")
  156. write_pack_data(f, [(b, None)], 1)
  157. commit()
  158. def test_add_thin_pack(self):
  159. o = DiskObjectStore(self.store_dir)
  160. f, commit = o.add_thin_pack()
  161. b = make_object(Blob, data="more yummy data")
  162. write_pack_data(f, [(b, None)], 1)
  163. commit()
  164. # TODO: MissingObjectFinderTests