test_index.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 io import BytesIO
  20. import os
  21. import shutil
  22. import stat
  23. import struct
  24. import tempfile
  25. from dulwich.index import (
  26. Index,
  27. build_index_from_tree,
  28. cleanup_mode,
  29. commit_tree,
  30. get_unstaged_changes,
  31. index_entry_from_stat,
  32. read_index,
  33. read_index_dict,
  34. write_cache_time,
  35. write_index,
  36. write_index_dict,
  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. def test_against_empty_tree(self):
  66. i = self.get_simple_index("index")
  67. changes = list(i.changes_from_tree(MemoryObjectStore(), None))
  68. self.assertEqual(1, len(changes))
  69. (oldname, newname), (oldmode, newmode), (oldsha, newsha) = changes[0]
  70. self.assertEqual('bla', newname)
  71. self.assertEqual('e69de29bb2d1d6434b8b29ae775ad8c2e48c5391', newsha)
  72. class SimpleIndexWriterTestCase(IndexTestCase):
  73. def setUp(self):
  74. IndexTestCase.setUp(self)
  75. self.tempdir = tempfile.mkdtemp()
  76. def tearDown(self):
  77. IndexTestCase.tearDown(self)
  78. shutil.rmtree(self.tempdir)
  79. def test_simple_write(self):
  80. entries = [('barbla', (1230680220, 0), (1230680220, 0), 2050, 3761020,
  81. 33188, 1000, 1000, 0,
  82. 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391', 0)]
  83. filename = os.path.join(self.tempdir, 'test-simple-write-index')
  84. x = open(filename, 'w+')
  85. try:
  86. write_index(x, entries)
  87. finally:
  88. x.close()
  89. x = open(filename, 'r')
  90. try:
  91. self.assertEqual(entries, list(read_index(x)))
  92. finally:
  93. x.close()
  94. class ReadIndexDictTests(IndexTestCase):
  95. def setUp(self):
  96. IndexTestCase.setUp(self)
  97. self.tempdir = tempfile.mkdtemp()
  98. def tearDown(self):
  99. IndexTestCase.tearDown(self)
  100. shutil.rmtree(self.tempdir)
  101. def test_simple_write(self):
  102. entries = {'barbla': ((1230680220, 0), (1230680220, 0), 2050, 3761020,
  103. 33188, 1000, 1000, 0,
  104. 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391', 0)}
  105. filename = os.path.join(self.tempdir, 'test-simple-write-index')
  106. x = open(filename, 'w+')
  107. try:
  108. write_index_dict(x, entries)
  109. finally:
  110. x.close()
  111. x = open(filename, 'r')
  112. try:
  113. self.assertEqual(entries, read_index_dict(x))
  114. finally:
  115. x.close()
  116. class CommitTreeTests(TestCase):
  117. def setUp(self):
  118. super(CommitTreeTests, self).setUp()
  119. self.store = MemoryObjectStore()
  120. def test_single_blob(self):
  121. blob = Blob()
  122. blob.data = "foo"
  123. self.store.add_object(blob)
  124. blobs = [("bla", blob.id, stat.S_IFREG)]
  125. rootid = commit_tree(self.store, blobs)
  126. self.assertEqual(rootid, "1a1e80437220f9312e855c37ac4398b68e5c1d50")
  127. self.assertEqual((stat.S_IFREG, blob.id), self.store[rootid]["bla"])
  128. self.assertEqual(set([rootid, blob.id]), set(self.store._data.keys()))
  129. def test_nested(self):
  130. blob = Blob()
  131. blob.data = "foo"
  132. self.store.add_object(blob)
  133. blobs = [("bla/bar", blob.id, stat.S_IFREG)]
  134. rootid = commit_tree(self.store, blobs)
  135. self.assertEqual(rootid, "d92b959b216ad0d044671981196781b3258fa537")
  136. dirid = self.store[rootid]["bla"][1]
  137. self.assertEqual(dirid, "c1a1deb9788150829579a8b4efa6311e7b638650")
  138. self.assertEqual((stat.S_IFDIR, dirid), self.store[rootid]["bla"])
  139. self.assertEqual((stat.S_IFREG, blob.id), self.store[dirid]["bar"])
  140. self.assertEqual(set([rootid, dirid, blob.id]),
  141. set(self.store._data.keys()))
  142. class CleanupModeTests(TestCase):
  143. def test_file(self):
  144. self.assertEqual(0o100644, cleanup_mode(0o100000))
  145. def test_executable(self):
  146. self.assertEqual(0o100755, cleanup_mode(0o100711))
  147. def test_symlink(self):
  148. self.assertEqual(0o120000, cleanup_mode(0o120711))
  149. def test_dir(self):
  150. self.assertEqual(0o040000, cleanup_mode(0o40531))
  151. def test_submodule(self):
  152. self.assertEqual(0o160000, cleanup_mode(0o160744))
  153. class WriteCacheTimeTests(TestCase):
  154. def test_write_string(self):
  155. f = BytesIO()
  156. self.assertRaises(TypeError, write_cache_time, f, "foo")
  157. def test_write_int(self):
  158. f = BytesIO()
  159. write_cache_time(f, 434343)
  160. self.assertEqual(struct.pack(">LL", 434343, 0), f.getvalue())
  161. def test_write_tuple(self):
  162. f = BytesIO()
  163. write_cache_time(f, (434343, 21))
  164. self.assertEqual(struct.pack(">LL", 434343, 21), f.getvalue())
  165. def test_write_float(self):
  166. f = BytesIO()
  167. write_cache_time(f, 434343.000000021)
  168. self.assertEqual(struct.pack(">LL", 434343, 21), f.getvalue())
  169. class IndexEntryFromStatTests(TestCase):
  170. def test_simple(self):
  171. st = os.stat_result((16877, 131078, 64769,
  172. 154, 1000, 1000, 12288,
  173. 1323629595, 1324180496, 1324180496))
  174. entry = index_entry_from_stat(st, "22" * 20, 0)
  175. self.assertEqual(entry, (
  176. 1324180496,
  177. 1324180496,
  178. 64769,
  179. 131078,
  180. 16384,
  181. 1000,
  182. 1000,
  183. 12288,
  184. '2222222222222222222222222222222222222222',
  185. 0))
  186. def test_override_mode(self):
  187. st = os.stat_result((stat.S_IFREG + 0o644, 131078, 64769,
  188. 154, 1000, 1000, 12288,
  189. 1323629595, 1324180496, 1324180496))
  190. entry = index_entry_from_stat(st, "22" * 20, 0,
  191. mode=stat.S_IFREG + 0o755)
  192. self.assertEqual(entry, (
  193. 1324180496,
  194. 1324180496,
  195. 64769,
  196. 131078,
  197. 33261,
  198. 1000,
  199. 1000,
  200. 12288,
  201. '2222222222222222222222222222222222222222',
  202. 0))
  203. class BuildIndexTests(TestCase):
  204. def assertReasonableIndexEntry(self, index_entry, mode, filesize, sha):
  205. self.assertEqual(index_entry[4], mode) # mode
  206. self.assertEqual(index_entry[7], filesize) # filesize
  207. self.assertEqual(index_entry[8], sha) # sha
  208. def assertFileContents(self, path, contents, symlink=False):
  209. if symlink:
  210. self.assertEqual(os.readlink(path), contents)
  211. else:
  212. f = open(path, 'rb')
  213. try:
  214. self.assertEqual(f.read(), contents)
  215. finally:
  216. f.close()
  217. def test_empty(self):
  218. repo_dir = tempfile.mkdtemp()
  219. repo = Repo.init(repo_dir)
  220. self.addCleanup(shutil.rmtree, repo_dir)
  221. tree = Tree()
  222. repo.object_store.add_object(tree)
  223. build_index_from_tree(repo.path, repo.index_path(),
  224. repo.object_store, tree.id)
  225. # Verify index entries
  226. index = repo.open_index()
  227. self.assertEqual(len(index), 0)
  228. # Verify no files
  229. self.assertEqual(['.git'], os.listdir(repo.path))
  230. def test_nonempty(self):
  231. if os.name != 'posix':
  232. self.skipTest("test depends on POSIX shell")
  233. repo_dir = tempfile.mkdtemp()
  234. repo = Repo.init(repo_dir)
  235. self.addCleanup(shutil.rmtree, repo_dir)
  236. # Populate repo
  237. filea = Blob.from_string('file a')
  238. fileb = Blob.from_string('file b')
  239. filed = Blob.from_string('file d')
  240. filee = Blob.from_string('d')
  241. tree = Tree()
  242. tree['a'] = (stat.S_IFREG | 0o644, filea.id)
  243. tree['b'] = (stat.S_IFREG | 0o644, fileb.id)
  244. tree['c/d'] = (stat.S_IFREG | 0o644, filed.id)
  245. tree['c/e'] = (stat.S_IFLNK, filee.id) # symlink
  246. repo.object_store.add_objects([(o, None)
  247. for o in [filea, fileb, filed, filee, tree]])
  248. build_index_from_tree(repo.path, repo.index_path(),
  249. repo.object_store, tree.id)
  250. # Verify index entries
  251. index = repo.open_index()
  252. self.assertEqual(len(index), 4)
  253. # filea
  254. apath = os.path.join(repo.path, 'a')
  255. self.assertTrue(os.path.exists(apath))
  256. self.assertReasonableIndexEntry(index['a'],
  257. stat.S_IFREG | 0o644, 6, filea.id)
  258. self.assertFileContents(apath, 'file a')
  259. # fileb
  260. bpath = os.path.join(repo.path, 'b')
  261. self.assertTrue(os.path.exists(bpath))
  262. self.assertReasonableIndexEntry(index['b'],
  263. stat.S_IFREG | 0o644, 6, fileb.id)
  264. self.assertFileContents(bpath, 'file b')
  265. # filed
  266. dpath = os.path.join(repo.path, 'c', 'd')
  267. self.assertTrue(os.path.exists(dpath))
  268. self.assertReasonableIndexEntry(index['c/d'],
  269. stat.S_IFREG | 0o644, 6, filed.id)
  270. self.assertFileContents(dpath, 'file d')
  271. # symlink to d
  272. epath = os.path.join(repo.path, 'c', 'e')
  273. self.assertTrue(os.path.exists(epath))
  274. self.assertReasonableIndexEntry(index['c/e'],
  275. stat.S_IFLNK, 1, filee.id)
  276. self.assertFileContents(epath, 'd', symlink=True)
  277. # Verify no extra files
  278. self.assertEqual(['.git', 'a', 'b', 'c'],
  279. sorted(os.listdir(repo.path)))
  280. self.assertEqual(['d', 'e'],
  281. sorted(os.listdir(os.path.join(repo.path, 'c'))))
  282. class GetUnstagedChangesTests(TestCase):
  283. def test_get_unstaged_changes(self):
  284. """Unit test for get_unstaged_changes."""
  285. repo_dir = tempfile.mkdtemp()
  286. repo = Repo.init(repo_dir)
  287. self.addCleanup(shutil.rmtree, repo_dir)
  288. # Commit a dummy file then modify it
  289. foo1_fullpath = os.path.join(repo_dir, 'foo1')
  290. with open(foo1_fullpath, 'w') as f:
  291. f.write('origstuff')
  292. foo2_fullpath = os.path.join(repo_dir, 'foo2')
  293. with open(foo2_fullpath, 'w') as f:
  294. f.write('origstuff')
  295. repo.stage(['foo1', 'foo2'])
  296. repo.do_commit('test status', author='', committer='')
  297. with open(foo1_fullpath, 'w') as f:
  298. f.write('newstuff')
  299. # modify access and modify time of path
  300. os.utime(foo1_fullpath, (0, 0))
  301. changes = get_unstaged_changes(repo.open_index(), repo_dir)
  302. self.assertEqual(list(changes), ['foo1'])