test_index.py 11 KB

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