test_object_store.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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.errors import (
  26. NotTreeError,
  27. )
  28. from dulwich.objects import (
  29. object_class,
  30. Blob,
  31. Tag,
  32. Tree,
  33. TreeEntry,
  34. )
  35. from dulwich.object_store import (
  36. DiskObjectStore,
  37. MemoryObjectStore,
  38. tree_lookup_path,
  39. )
  40. from dulwich.pack import (
  41. write_pack_data,
  42. )
  43. from dulwich.tests import (
  44. TestCase,
  45. )
  46. from dulwich.tests.utils import (
  47. make_object,
  48. )
  49. testobject = make_object(Blob, data="yummy data")
  50. class ObjectStoreTests(object):
  51. def test_determine_wants_all(self):
  52. self.assertEquals(["1" * 40],
  53. self.store.determine_wants_all({"refs/heads/foo": "1" * 40}))
  54. def test_determine_wants_all_zero(self):
  55. self.assertEquals([],
  56. self.store.determine_wants_all({"refs/heads/foo": "0" * 40}))
  57. def test_iter(self):
  58. self.assertEquals([], list(self.store))
  59. def test_get_nonexistant(self):
  60. self.assertRaises(KeyError, lambda: self.store["a" * 40])
  61. def test_contains_nonexistant(self):
  62. self.assertFalse(("a" * 40) in self.store)
  63. def test_add_objects_empty(self):
  64. self.store.add_objects([])
  65. def test_add_commit(self):
  66. # TODO: Argh, no way to construct Git commit objects without
  67. # access to a serialized form.
  68. self.store.add_objects([])
  69. def test_add_object(self):
  70. self.store.add_object(testobject)
  71. self.assertEquals(set([testobject.id]), set(self.store))
  72. self.assertTrue(testobject.id in self.store)
  73. r = self.store[testobject.id]
  74. self.assertEquals(r, testobject)
  75. def test_add_objects(self):
  76. data = [(testobject, "mypath")]
  77. self.store.add_objects(data)
  78. self.assertEquals(set([testobject.id]), set(self.store))
  79. self.assertTrue(testobject.id in self.store)
  80. r = self.store[testobject.id]
  81. self.assertEquals(r, testobject)
  82. def test_tree_changes(self):
  83. blob_a1 = make_object(Blob, data='a1')
  84. blob_a2 = make_object(Blob, data='a2')
  85. blob_b = make_object(Blob, data='b')
  86. for blob in [blob_a1, blob_a2, blob_b]:
  87. self.store.add_object(blob)
  88. blobs_1 = [('a', blob_a1.id, 0100644), ('b', blob_b.id, 0100644)]
  89. tree1_id = commit_tree(self.store, blobs_1)
  90. blobs_2 = [('a', blob_a2.id, 0100644), ('b', blob_b.id, 0100644)]
  91. tree2_id = commit_tree(self.store, blobs_2)
  92. change_a = (('a', 'a'), (0100644, 0100644), (blob_a1.id, blob_a2.id))
  93. self.assertEquals([change_a],
  94. list(self.store.tree_changes(tree1_id, tree2_id)))
  95. self.assertEquals(
  96. [change_a, (('b', 'b'), (0100644, 0100644), (blob_b.id, blob_b.id))],
  97. list(self.store.tree_changes(tree1_id, tree2_id,
  98. want_unchanged=True)))
  99. def test_iter_tree_contents(self):
  100. blob_a = make_object(Blob, data='a')
  101. blob_b = make_object(Blob, data='b')
  102. blob_c = make_object(Blob, data='c')
  103. for blob in [blob_a, blob_b, blob_c]:
  104. self.store.add_object(blob)
  105. blobs = [
  106. ('a', blob_a.id, 0100644),
  107. ('ad/b', blob_b.id, 0100644),
  108. ('ad/bd/c', blob_c.id, 0100755),
  109. ('ad/c', blob_c.id, 0100644),
  110. ('c', blob_c.id, 0100644),
  111. ]
  112. tree_id = commit_tree(self.store, blobs)
  113. self.assertEquals([TreeEntry(p, m, h) for (p, h, m) in blobs],
  114. list(self.store.iter_tree_contents(tree_id)))
  115. def test_iter_tree_contents_include_trees(self):
  116. blob_a = make_object(Blob, data='a')
  117. blob_b = make_object(Blob, data='b')
  118. blob_c = make_object(Blob, data='c')
  119. for blob in [blob_a, blob_b, blob_c]:
  120. self.store.add_object(blob)
  121. blobs = [
  122. ('a', blob_a.id, 0100644),
  123. ('ad/b', blob_b.id, 0100644),
  124. ('ad/bd/c', blob_c.id, 0100755),
  125. ]
  126. tree_id = commit_tree(self.store, blobs)
  127. tree = self.store[tree_id]
  128. tree_ad = self.store[tree['ad'][1]]
  129. tree_bd = self.store[tree_ad['bd'][1]]
  130. expected = [
  131. TreeEntry('', 0040000, tree_id),
  132. TreeEntry('a', 0100644, blob_a.id),
  133. TreeEntry('ad', 0040000, tree_ad.id),
  134. TreeEntry('ad/b', 0100644, blob_b.id),
  135. TreeEntry('ad/bd', 0040000, tree_bd.id),
  136. TreeEntry('ad/bd/c', 0100755, blob_c.id),
  137. ]
  138. actual = self.store.iter_tree_contents(tree_id, include_trees=True)
  139. self.assertEquals(expected, list(actual))
  140. def make_tag(self, name, obj):
  141. tag = make_object(Tag, name=name, message='',
  142. tag_time=12345, tag_timezone=0,
  143. tagger='Test Tagger <test@example.com>',
  144. object=(object_class(obj.type_name), obj.id))
  145. self.store.add_object(tag)
  146. return tag
  147. def test_peel_sha(self):
  148. self.store.add_object(testobject)
  149. tag1 = self.make_tag('1', testobject)
  150. tag2 = self.make_tag('2', testobject)
  151. tag3 = self.make_tag('3', testobject)
  152. for obj in [testobject, tag1, tag2, tag3]:
  153. self.assertEqual(testobject, self.store.peel_sha(obj.id))
  154. class MemoryObjectStoreTests(ObjectStoreTests, TestCase):
  155. def setUp(self):
  156. TestCase.setUp(self)
  157. self.store = MemoryObjectStore()
  158. class PackBasedObjectStoreTests(ObjectStoreTests):
  159. def tearDown(self):
  160. for pack in self.store.packs:
  161. pack.close()
  162. def test_empty_packs(self):
  163. self.assertEquals([], self.store.packs)
  164. def test_pack_loose_objects(self):
  165. b1 = make_object(Blob, data="yummy data")
  166. self.store.add_object(b1)
  167. b2 = make_object(Blob, data="more yummy data")
  168. self.store.add_object(b2)
  169. self.assertEquals([], self.store.packs)
  170. self.assertEquals(2, self.store.pack_loose_objects())
  171. self.assertNotEquals([], self.store.packs)
  172. self.assertEquals(0, self.store.pack_loose_objects())
  173. class DiskObjectStoreTests(PackBasedObjectStoreTests, TestCase):
  174. def setUp(self):
  175. TestCase.setUp(self)
  176. self.store_dir = tempfile.mkdtemp()
  177. self.store = DiskObjectStore.init(self.store_dir)
  178. def tearDown(self):
  179. TestCase.tearDown(self)
  180. PackBasedObjectStoreTests.tearDown(self)
  181. shutil.rmtree(self.store_dir)
  182. def test_pack_dir(self):
  183. o = DiskObjectStore(self.store_dir)
  184. self.assertEquals(os.path.join(self.store_dir, "pack"), o.pack_dir)
  185. def test_add_pack(self):
  186. o = DiskObjectStore(self.store_dir)
  187. f, commit = o.add_pack()
  188. b = make_object(Blob, data="more yummy data")
  189. write_pack_data(f, [(b, None)])
  190. commit()
  191. def test_add_thin_pack(self):
  192. o = DiskObjectStore(self.store_dir)
  193. f, commit = o.add_thin_pack()
  194. b = make_object(Blob, data="more yummy data")
  195. write_pack_data(f, [(b, None)])
  196. commit()
  197. class TreeLookupPathTests(TestCase):
  198. def setUp(self):
  199. TestCase.setUp(self)
  200. self.store = MemoryObjectStore()
  201. blob_a = make_object(Blob, data='a')
  202. blob_b = make_object(Blob, data='b')
  203. blob_c = make_object(Blob, data='c')
  204. for blob in [blob_a, blob_b, blob_c]:
  205. self.store.add_object(blob)
  206. blobs = [
  207. ('a', blob_a.id, 0100644),
  208. ('ad/b', blob_b.id, 0100644),
  209. ('ad/bd/c', blob_c.id, 0100755),
  210. ('ad/c', blob_c.id, 0100644),
  211. ('c', blob_c.id, 0100644),
  212. ]
  213. self.tree_id = commit_tree(self.store, blobs)
  214. def get_object(self, sha):
  215. return self.store[sha]
  216. def test_lookup_blob(self):
  217. o_id = tree_lookup_path(self.get_object, self.tree_id, 'a')[1]
  218. self.assertTrue(isinstance(self.store[o_id], Blob))
  219. def test_lookup_tree(self):
  220. o_id = tree_lookup_path(self.get_object, self.tree_id, 'ad')[1]
  221. self.assertTrue(isinstance(self.store[o_id], Tree))
  222. o_id = tree_lookup_path(self.get_object, self.tree_id, 'ad/bd')[1]
  223. self.assertTrue(isinstance(self.store[o_id], Tree))
  224. o_id = tree_lookup_path(self.get_object, self.tree_id, 'ad/bd/')[1]
  225. self.assertTrue(isinstance(self.store[o_id], Tree))
  226. def test_lookup_nonexistent(self):
  227. self.assertRaises(KeyError, tree_lookup_path, self.get_object, self.tree_id, 'j')
  228. def test_lookup_not_tree(self):
  229. self.assertRaises(NotTreeError, tree_lookup_path, self.get_object, self.tree_id, 'ad/b/j')
  230. # TODO: MissingObjectFinderTests