test_index.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 cStringIO import (
  20. StringIO,
  21. )
  22. import os
  23. import shutil
  24. import stat
  25. import struct
  26. import tempfile
  27. from dulwich.index import (
  28. Index,
  29. cleanup_mode,
  30. commit_tree,
  31. index_entry_from_stat,
  32. read_index,
  33. write_cache_time,
  34. write_index,
  35. build_index_from_tree,
  36. )
  37. from dulwich.object_store import (
  38. MemoryObjectStore,
  39. )
  40. from dulwich.objects import (
  41. Blob,
  42. )
  43. from dulwich.tests import TestCase
  44. from dulwich.repo import Repo
  45. from dulwich.tests.utils import (
  46. open_repo,
  47. tear_down_repo,
  48. )
  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. class SimpleIndexTestCase(IndexTestCase):
  54. def test_len(self):
  55. self.assertEquals(1, len(self.get_simple_index("index")))
  56. def test_iter(self):
  57. self.assertEquals(['bla'], list(self.get_simple_index("index")))
  58. def test_getitem(self):
  59. self.assertEquals(((1230680220, 0), (1230680220, 0), 2050, 3761020,
  60. 33188, 1000, 1000, 0,
  61. 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391', 0),
  62. self.get_simple_index("index")["bla"])
  63. def test_empty(self):
  64. i = self.get_simple_index("notanindex")
  65. self.assertEquals(0, len(i))
  66. self.assertFalse(os.path.exists(i._filename))
  67. class SimpleIndexWriterTestCase(IndexTestCase):
  68. def setUp(self):
  69. IndexTestCase.setUp(self)
  70. self.tempdir = tempfile.mkdtemp()
  71. def tearDown(self):
  72. IndexTestCase.tearDown(self)
  73. shutil.rmtree(self.tempdir)
  74. def test_simple_write(self):
  75. entries = [('barbla', (1230680220, 0), (1230680220, 0), 2050, 3761020,
  76. 33188, 1000, 1000, 0,
  77. 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391', 0)]
  78. filename = os.path.join(self.tempdir, 'test-simple-write-index')
  79. x = open(filename, 'w+')
  80. try:
  81. write_index(x, entries)
  82. finally:
  83. x.close()
  84. x = open(filename, 'r')
  85. try:
  86. self.assertEquals(entries, list(read_index(x)))
  87. finally:
  88. x.close()
  89. class CommitTreeTests(TestCase):
  90. def setUp(self):
  91. super(CommitTreeTests, self).setUp()
  92. self.store = MemoryObjectStore()
  93. def test_single_blob(self):
  94. blob = Blob()
  95. blob.data = "foo"
  96. self.store.add_object(blob)
  97. blobs = [("bla", blob.id, stat.S_IFREG)]
  98. rootid = commit_tree(self.store, blobs)
  99. self.assertEquals(rootid, "1a1e80437220f9312e855c37ac4398b68e5c1d50")
  100. self.assertEquals((stat.S_IFREG, blob.id), self.store[rootid]["bla"])
  101. self.assertEquals(set([rootid, blob.id]), set(self.store._data.keys()))
  102. def test_nested(self):
  103. blob = Blob()
  104. blob.data = "foo"
  105. self.store.add_object(blob)
  106. blobs = [("bla/bar", blob.id, stat.S_IFREG)]
  107. rootid = commit_tree(self.store, blobs)
  108. self.assertEquals(rootid, "d92b959b216ad0d044671981196781b3258fa537")
  109. dirid = self.store[rootid]["bla"][1]
  110. self.assertEquals(dirid, "c1a1deb9788150829579a8b4efa6311e7b638650")
  111. self.assertEquals((stat.S_IFDIR, dirid), self.store[rootid]["bla"])
  112. self.assertEquals((stat.S_IFREG, blob.id), self.store[dirid]["bar"])
  113. self.assertEquals(set([rootid, dirid, blob.id]),
  114. set(self.store._data.keys()))
  115. class CleanupModeTests(TestCase):
  116. def test_file(self):
  117. self.assertEquals(0100644, cleanup_mode(0100000))
  118. def test_executable(self):
  119. self.assertEquals(0100755, cleanup_mode(0100711))
  120. def test_symlink(self):
  121. self.assertEquals(0120000, cleanup_mode(0120711))
  122. def test_dir(self):
  123. self.assertEquals(0040000, cleanup_mode(040531))
  124. def test_submodule(self):
  125. self.assertEquals(0160000, cleanup_mode(0160744))
  126. class WriteCacheTimeTests(TestCase):
  127. def test_write_string(self):
  128. f = StringIO()
  129. self.assertRaises(TypeError, write_cache_time, f, "foo")
  130. def test_write_int(self):
  131. f = StringIO()
  132. write_cache_time(f, 434343)
  133. self.assertEquals(struct.pack(">LL", 434343, 0), f.getvalue())
  134. def test_write_tuple(self):
  135. f = StringIO()
  136. write_cache_time(f, (434343, 21))
  137. self.assertEquals(struct.pack(">LL", 434343, 21), f.getvalue())
  138. def test_write_float(self):
  139. f = StringIO()
  140. write_cache_time(f, 434343.000000021)
  141. self.assertEquals(struct.pack(">LL", 434343, 21), f.getvalue())
  142. class IndexEntryFromStatTests(TestCase):
  143. def test_simple(self):
  144. st = os.stat_result((16877, 131078, 64769L,
  145. 154, 1000, 1000, 12288,
  146. 1323629595, 1324180496, 1324180496))
  147. entry = index_entry_from_stat(st, "22" * 20, 0)
  148. self.assertEquals(entry, (
  149. 1324180496,
  150. 1324180496,
  151. 64769L,
  152. 131078,
  153. 16384,
  154. 1000,
  155. 1000,
  156. 12288,
  157. '2222222222222222222222222222222222222222',
  158. 0))
  159. def test_override_mode(self):
  160. st = os.stat_result((stat.S_IFREG + 0644, 131078, 64769L,
  161. 154, 1000, 1000, 12288,
  162. 1323629595, 1324180496, 1324180496))
  163. entry = index_entry_from_stat(st, "22" * 20, 0,
  164. mode=stat.S_IFREG + 0755)
  165. self.assertEquals(entry, (
  166. 1324180496,
  167. 1324180496,
  168. 64769L,
  169. 131078,
  170. 33261,
  171. 1000,
  172. 1000,
  173. 12288,
  174. '2222222222222222222222222222222222222222',
  175. 0))
  176. class BuildIndexTests(TestCase):
  177. def test_nonempty(self):
  178. repo = open_repo('a.git')
  179. target_repo_dir = tempfile.mkdtemp()
  180. target_repo = Repo.init(target_repo_dir)
  181. repo.fetch(target_repo)
  182. target_repo.refs.add_if_new(
  183. 'refs/heads/master',
  184. repo.refs['refs/heads/master'])
  185. self.assertFalse(os.path.exists(target_repo.index_path()))
  186. build_index_from_tree(target_repo.path, target_repo.index_path(),
  187. target_repo.object_store, target_repo['refs/heads/master'].tree)
  188. self.assertTrue(os.path.exists(target_repo.index_path()))
  189. index = target_repo.open_index()
  190. for entry in repo.object_store.iter_tree_contents(repo['refs/heads/master'].tree):
  191. full_path = os.path.join(target_repo.path, entry.path)
  192. self.assertTrue(os.path.exists(full_path))
  193. st = os.stat(full_path)
  194. f = open(full_path, 'rb')
  195. blob = Blob()
  196. try:
  197. blob.data = f.read()
  198. finally:
  199. f.close()
  200. index_entry = index_entry_from_stat(st, blob.id, 0)
  201. index_entry_cmp = ((int(index_entry[0]),0), (int(index_entry[1]),0)) + (int(index_entry[2]),) + index_entry[3:]
  202. self.assertEquals(index_entry_cmp, index[entry.path])
  203. shutil.rmtree(target_repo.path)
  204. tear_down_repo(repo)