test_index.py 13 KB

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