test_index.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. # test_index.py -- Tests for the git index
  2. # Copyright (C) 2008-2009 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 index."""
  19. from cStringIO import (
  20. StringIO,
  21. )
  22. import os
  23. import shutil
  24. import stat
  25. import struct
  26. import tempfile
  27. from dulwich.index import (
  28. Index,
  29. build_index_from_tree,
  30. cleanup_mode,
  31. commit_tree,
  32. index_entry_from_stat,
  33. read_index,
  34. write_cache_time,
  35. write_index,
  36. )
  37. from dulwich.object_store import (
  38. MemoryObjectStore,
  39. )
  40. from dulwich.objects import (
  41. Blob,
  42. Tree,
  43. )
  44. from dulwich.repo import Repo
  45. from dulwich.tests import TestCase
  46. class IndexTestCase(TestCase):
  47. datadir = os.path.join(os.path.dirname(__file__), 'data/indexes')
  48. def get_simple_index(self, name):
  49. return Index(os.path.join(self.datadir, name))
  50. class SimpleIndexTestCase(IndexTestCase):
  51. def test_len(self):
  52. self.assertEqual(1, len(self.get_simple_index("index")))
  53. def test_iter(self):
  54. self.assertEqual(['bla'], list(self.get_simple_index("index")))
  55. def test_getitem(self):
  56. self.assertEqual(((1230680220, 0), (1230680220, 0), 2050, 3761020,
  57. 33188, 1000, 1000, 0,
  58. 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391', 0),
  59. self.get_simple_index("index")["bla"])
  60. def test_empty(self):
  61. i = self.get_simple_index("notanindex")
  62. self.assertEqual(0, len(i))
  63. self.assertFalse(os.path.exists(i._filename))
  64. def test_against_empty_tree(self):
  65. i = self.get_simple_index("index")
  66. changes = list(i.changes_from_tree(MemoryObjectStore(), None))
  67. self.assertEqual(1, len(changes))
  68. (oldname, newname), (oldmode, newmode), (oldsha, newsha) = changes[0]
  69. self.assertEqual('bla', newname)
  70. self.assertEqual('e69de29bb2d1d6434b8b29ae775ad8c2e48c5391', newsha)
  71. class SimpleIndexWriterTestCase(IndexTestCase):
  72. def setUp(self):
  73. IndexTestCase.setUp(self)
  74. self.tempdir = tempfile.mkdtemp()
  75. def tearDown(self):
  76. IndexTestCase.tearDown(self)
  77. shutil.rmtree(self.tempdir)
  78. def test_simple_write(self):
  79. entries = [('barbla', (1230680220, 0), (1230680220, 0), 2050, 3761020,
  80. 33188, 1000, 1000, 0,
  81. 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391', 0)]
  82. filename = os.path.join(self.tempdir, 'test-simple-write-index')
  83. x = open(filename, 'w+')
  84. try:
  85. write_index(x, entries)
  86. finally:
  87. x.close()
  88. x = open(filename, 'r')
  89. try:
  90. self.assertEqual(entries, list(read_index(x)))
  91. finally:
  92. x.close()
  93. class CommitTreeTests(TestCase):
  94. def setUp(self):
  95. super(CommitTreeTests, self).setUp()
  96. self.store = MemoryObjectStore()
  97. def test_single_blob(self):
  98. blob = Blob()
  99. blob.data = "foo"
  100. self.store.add_object(blob)
  101. blobs = [("bla", blob.id, stat.S_IFREG)]
  102. rootid = commit_tree(self.store, blobs)
  103. self.assertEqual(rootid, "1a1e80437220f9312e855c37ac4398b68e5c1d50")
  104. self.assertEqual((stat.S_IFREG, blob.id), self.store[rootid]["bla"])
  105. self.assertEqual(set([rootid, blob.id]), set(self.store._data.keys()))
  106. def test_nested(self):
  107. blob = Blob()
  108. blob.data = "foo"
  109. self.store.add_object(blob)
  110. blobs = [("bla/bar", blob.id, stat.S_IFREG)]
  111. rootid = commit_tree(self.store, blobs)
  112. self.assertEqual(rootid, "d92b959b216ad0d044671981196781b3258fa537")
  113. dirid = self.store[rootid]["bla"][1]
  114. self.assertEqual(dirid, "c1a1deb9788150829579a8b4efa6311e7b638650")
  115. self.assertEqual((stat.S_IFDIR, dirid), self.store[rootid]["bla"])
  116. self.assertEqual((stat.S_IFREG, blob.id), self.store[dirid]["bar"])
  117. self.assertEqual(set([rootid, dirid, blob.id]),
  118. set(self.store._data.keys()))
  119. class CleanupModeTests(TestCase):
  120. def test_file(self):
  121. self.assertEqual(0100644, cleanup_mode(0100000))
  122. def test_executable(self):
  123. self.assertEqual(0100755, cleanup_mode(0100711))
  124. def test_symlink(self):
  125. self.assertEqual(0120000, cleanup_mode(0120711))
  126. def test_dir(self):
  127. self.assertEqual(0040000, cleanup_mode(040531))
  128. def test_submodule(self):
  129. self.assertEqual(0160000, cleanup_mode(0160744))
  130. class WriteCacheTimeTests(TestCase):
  131. def test_write_string(self):
  132. f = StringIO()
  133. self.assertRaises(TypeError, write_cache_time, f, "foo")
  134. def test_write_int(self):
  135. f = StringIO()
  136. write_cache_time(f, 434343)
  137. self.assertEqual(struct.pack(">LL", 434343, 0), f.getvalue())
  138. def test_write_tuple(self):
  139. f = StringIO()
  140. write_cache_time(f, (434343, 21))
  141. self.assertEqual(struct.pack(">LL", 434343, 21), f.getvalue())
  142. def test_write_float(self):
  143. f = StringIO()
  144. write_cache_time(f, 434343.000000021)
  145. self.assertEqual(struct.pack(">LL", 434343, 21), f.getvalue())
  146. class IndexEntryFromStatTests(TestCase):
  147. def test_simple(self):
  148. st = os.stat_result((16877, 131078, 64769L,
  149. 154, 1000, 1000, 12288,
  150. 1323629595, 1324180496, 1324180496))
  151. entry = index_entry_from_stat(st, "22" * 20, 0)
  152. self.assertEqual(entry, (
  153. 1324180496,
  154. 1324180496,
  155. 64769L,
  156. 131078,
  157. 16384,
  158. 1000,
  159. 1000,
  160. 12288,
  161. '2222222222222222222222222222222222222222',
  162. 0))
  163. def test_override_mode(self):
  164. st = os.stat_result((stat.S_IFREG + 0644, 131078, 64769L,
  165. 154, 1000, 1000, 12288,
  166. 1323629595, 1324180496, 1324180496))
  167. entry = index_entry_from_stat(st, "22" * 20, 0,
  168. mode=stat.S_IFREG + 0755)
  169. self.assertEqual(entry, (
  170. 1324180496,
  171. 1324180496,
  172. 64769L,
  173. 131078,
  174. 33261,
  175. 1000,
  176. 1000,
  177. 12288,
  178. '2222222222222222222222222222222222222222',
  179. 0))
  180. class BuildIndexTests(TestCase):
  181. def assertReasonableIndexEntry(self, index_entry, mode, filesize, sha):
  182. self.assertEquals(index_entry[4], mode) # mode
  183. self.assertEquals(index_entry[7], filesize) # filesize
  184. self.assertEquals(index_entry[8], sha) # sha
  185. def assertFileContents(self, path, contents, symlink=False):
  186. if symlink:
  187. self.assertEquals(os.readlink(path), contents)
  188. else:
  189. f = open(path, 'rb')
  190. try:
  191. self.assertEquals(f.read(), contents)
  192. finally:
  193. f.close()
  194. def test_empty(self):
  195. repo_dir = tempfile.mkdtemp()
  196. repo = Repo.init(repo_dir)
  197. self.addCleanup(shutil.rmtree, repo_dir)
  198. tree = Tree()
  199. repo.object_store.add_object(tree)
  200. build_index_from_tree(repo.path, repo.index_path(),
  201. repo.object_store, tree.id)
  202. # Verify index entries
  203. index = repo.open_index()
  204. self.assertEquals(len(index), 0)
  205. # Verify no files
  206. self.assertEquals(['.git'], os.listdir(repo.path))
  207. def test_nonempty(self):
  208. if os.name != 'posix':
  209. self.skipTest("test depends on POSIX shell")
  210. repo_dir = tempfile.mkdtemp()
  211. repo = Repo.init(repo_dir)
  212. self.addCleanup(shutil.rmtree, repo_dir)
  213. # Populate repo
  214. filea = Blob.from_string('file a')
  215. fileb = Blob.from_string('file b')
  216. filed = Blob.from_string('file d')
  217. filee = Blob.from_string('d')
  218. tree = Tree()
  219. tree['a'] = (stat.S_IFREG | 0644, filea.id)
  220. tree['b'] = (stat.S_IFREG | 0644, fileb.id)
  221. tree['c/d'] = (stat.S_IFREG | 0644, filed.id)
  222. tree['c/e'] = (stat.S_IFLNK, filee.id) # symlink
  223. repo.object_store.add_objects([(o, None)
  224. for o in [filea, fileb, filed, filee, tree]])
  225. build_index_from_tree(repo.path, repo.index_path(),
  226. repo.object_store, tree.id)
  227. # Verify index entries
  228. index = repo.open_index()
  229. self.assertEquals(len(index), 4)
  230. # filea
  231. apath = os.path.join(repo.path, 'a')
  232. self.assertTrue(os.path.exists(apath))
  233. self.assertReasonableIndexEntry(index['a'],
  234. stat.S_IFREG | 0644, 6, filea.id)
  235. self.assertFileContents(apath, 'file a')
  236. # fileb
  237. bpath = os.path.join(repo.path, 'b')
  238. self.assertTrue(os.path.exists(bpath))
  239. self.assertReasonableIndexEntry(index['b'],
  240. stat.S_IFREG | 0644, 6, fileb.id)
  241. self.assertFileContents(bpath, 'file b')
  242. # filed
  243. dpath = os.path.join(repo.path, 'c', 'd')
  244. self.assertTrue(os.path.exists(dpath))
  245. self.assertReasonableIndexEntry(index['c/d'],
  246. stat.S_IFREG | 0644, 6, filed.id)
  247. self.assertFileContents(dpath, 'file d')
  248. # symlink to d
  249. epath = os.path.join(repo.path, 'c', 'e')
  250. self.assertTrue(os.path.exists(epath))
  251. self.assertReasonableIndexEntry(index['c/e'],
  252. stat.S_IFLNK, 1, filee.id)
  253. self.assertFileContents(epath, 'd', symlink=True)
  254. # Verify no extra files
  255. self.assertEquals(['.git', 'a', 'b', 'c'],
  256. sorted(os.listdir(repo.path)))
  257. self.assertEquals(['d', 'e'],
  258. sorted(os.listdir(os.path.join(repo.path, 'c'))))