test_index.py 12 KB

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