test_index.py 14 KB

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