test_index.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. class SimpleIndexWriterTestCase(IndexTestCase):
  65. def setUp(self):
  66. IndexTestCase.setUp(self)
  67. self.tempdir = tempfile.mkdtemp()
  68. def tearDown(self):
  69. IndexTestCase.tearDown(self)
  70. shutil.rmtree(self.tempdir)
  71. def test_simple_write(self):
  72. entries = [('barbla', (1230680220, 0), (1230680220, 0), 2050, 3761020,
  73. 33188, 1000, 1000, 0,
  74. 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391', 0)]
  75. filename = os.path.join(self.tempdir, 'test-simple-write-index')
  76. x = open(filename, 'w+')
  77. try:
  78. write_index(x, entries)
  79. finally:
  80. x.close()
  81. x = open(filename, 'r')
  82. try:
  83. self.assertEqual(entries, list(read_index(x)))
  84. finally:
  85. x.close()
  86. class CommitTreeTests(TestCase):
  87. def setUp(self):
  88. super(CommitTreeTests, self).setUp()
  89. self.store = MemoryObjectStore()
  90. def test_single_blob(self):
  91. blob = Blob()
  92. blob.data = "foo"
  93. self.store.add_object(blob)
  94. blobs = [("bla", blob.id, stat.S_IFREG)]
  95. rootid = commit_tree(self.store, blobs)
  96. self.assertEqual(rootid, "1a1e80437220f9312e855c37ac4398b68e5c1d50")
  97. self.assertEqual((stat.S_IFREG, blob.id), self.store[rootid]["bla"])
  98. self.assertEqual(set([rootid, blob.id]), set(self.store._data.keys()))
  99. def test_nested(self):
  100. blob = Blob()
  101. blob.data = "foo"
  102. self.store.add_object(blob)
  103. blobs = [("bla/bar", blob.id, stat.S_IFREG)]
  104. rootid = commit_tree(self.store, blobs)
  105. self.assertEqual(rootid, "d92b959b216ad0d044671981196781b3258fa537")
  106. dirid = self.store[rootid]["bla"][1]
  107. self.assertEqual(dirid, "c1a1deb9788150829579a8b4efa6311e7b638650")
  108. self.assertEqual((stat.S_IFDIR, dirid), self.store[rootid]["bla"])
  109. self.assertEqual((stat.S_IFREG, blob.id), self.store[dirid]["bar"])
  110. self.assertEqual(set([rootid, dirid, blob.id]),
  111. set(self.store._data.keys()))
  112. class CleanupModeTests(TestCase):
  113. def test_file(self):
  114. self.assertEqual(0100644, cleanup_mode(0100000))
  115. def test_executable(self):
  116. self.assertEqual(0100755, cleanup_mode(0100711))
  117. def test_symlink(self):
  118. self.assertEqual(0120000, cleanup_mode(0120711))
  119. def test_dir(self):
  120. self.assertEqual(0040000, cleanup_mode(040531))
  121. def test_submodule(self):
  122. self.assertEqual(0160000, cleanup_mode(0160744))
  123. class WriteCacheTimeTests(TestCase):
  124. def test_write_string(self):
  125. f = StringIO()
  126. self.assertRaises(TypeError, write_cache_time, f, "foo")
  127. def test_write_int(self):
  128. f = StringIO()
  129. write_cache_time(f, 434343)
  130. self.assertEqual(struct.pack(">LL", 434343, 0), f.getvalue())
  131. def test_write_tuple(self):
  132. f = StringIO()
  133. write_cache_time(f, (434343, 21))
  134. self.assertEqual(struct.pack(">LL", 434343, 21), f.getvalue())
  135. def test_write_float(self):
  136. f = StringIO()
  137. write_cache_time(f, 434343.000000021)
  138. self.assertEqual(struct.pack(">LL", 434343, 21), f.getvalue())
  139. class IndexEntryFromStatTests(TestCase):
  140. def test_simple(self):
  141. st = os.stat_result((16877, 131078, 64769L,
  142. 154, 1000, 1000, 12288,
  143. 1323629595, 1324180496, 1324180496))
  144. entry = index_entry_from_stat(st, "22" * 20, 0)
  145. self.assertEqual(entry, (
  146. 1324180496,
  147. 1324180496,
  148. 64769L,
  149. 131078,
  150. 16384,
  151. 1000,
  152. 1000,
  153. 12288,
  154. '2222222222222222222222222222222222222222',
  155. 0))
  156. def test_override_mode(self):
  157. st = os.stat_result((stat.S_IFREG + 0644, 131078, 64769L,
  158. 154, 1000, 1000, 12288,
  159. 1323629595, 1324180496, 1324180496))
  160. entry = index_entry_from_stat(st, "22" * 20, 0,
  161. mode=stat.S_IFREG + 0755)
  162. self.assertEqual(entry, (
  163. 1324180496,
  164. 1324180496,
  165. 64769L,
  166. 131078,
  167. 33261,
  168. 1000,
  169. 1000,
  170. 12288,
  171. '2222222222222222222222222222222222222222',
  172. 0))
  173. class BuildIndexTests(TestCase):
  174. def assertReasonableIndexEntry(self, index_entry, values):
  175. delta = 1000000
  176. self.assertEquals(index_entry[0], index_entry[1]) # ctime and atime
  177. self.assertTrue(index_entry[0] > values[0] - delta)
  178. self.assertEquals(index_entry[4], values[4]) # mode
  179. self.assertEquals(index_entry[5], values[5]) # uid
  180. self.assertTrue(index_entry[6] in values[6]) # gid
  181. self.assertEquals(index_entry[7], values[7]) # filesize
  182. self.assertEquals(index_entry[8], values[8]) # 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, ctime,
  235. None, None,
  236. stat.S_IFREG | 0644,
  237. os.getuid(), os.getgroups(),
  238. 6,
  239. filea.id,
  240. None))
  241. self.assertFileContents(apath, 'file a')
  242. # fileb
  243. bpath = os.path.join(repo.path, 'b')
  244. self.assertTrue(os.path.exists(bpath))
  245. self.assertReasonableIndexEntry(index['b'], (
  246. ctime, ctime,
  247. None, None,
  248. stat.S_IFREG | 0644,
  249. os.getuid(), os.getgroups(),
  250. 6,
  251. fileb.id,
  252. None))
  253. self.assertFileContents(bpath, 'file b')
  254. # filed
  255. dpath = os.path.join(repo.path, 'c', 'd')
  256. self.assertTrue(os.path.exists(dpath))
  257. self.assertReasonableIndexEntry(index['c/d'], (
  258. ctime, ctime,
  259. None, None,
  260. stat.S_IFREG | 0644,
  261. os.getuid(), os.getgroups(),
  262. 6,
  263. filed.id,
  264. None))
  265. self.assertFileContents(dpath, 'file d')
  266. # symlink to d
  267. epath = os.path.join(repo.path, 'c', 'e')
  268. self.assertTrue(os.path.exists(epath))
  269. self.assertReasonableIndexEntry(index['c/e'], (
  270. ctime, ctime,
  271. None, None,
  272. stat.S_IFLNK,
  273. os.getuid(), os.getgroups(),
  274. 1,
  275. filee.id,
  276. None))
  277. self.assertFileContents(epath, 'd', symlink=True)
  278. # Verify no extra files
  279. self.assertEquals(['.git', 'a', 'b', 'c'],
  280. sorted(os.listdir(repo.path)))
  281. self.assertEquals(['d', 'e'],
  282. sorted(os.listdir(os.path.join(repo.path, 'c'))))