2
0

test_object_store.py 9.0 KB

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