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