2
0

test_index.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. read_index,
  32. write_cache_time,
  33. write_index,
  34. )
  35. from dulwich.object_store import (
  36. MemoryObjectStore,
  37. )
  38. from dulwich.objects import (
  39. Blob,
  40. )
  41. from dulwich.tests import TestCase
  42. class IndexTestCase(TestCase):
  43. datadir = os.path.join(os.path.dirname(__file__), 'data/indexes')
  44. def get_simple_index(self, name):
  45. return Index(os.path.join(self.datadir, name))
  46. class SimpleIndexTestCase(IndexTestCase):
  47. def test_len(self):
  48. self.assertEquals(1, len(self.get_simple_index("index")))
  49. def test_iter(self):
  50. self.assertEquals(['bla'], list(self.get_simple_index("index")))
  51. def test_getitem(self):
  52. self.assertEquals(((1230680220, 0), (1230680220, 0), 2050, 3761020,
  53. 33188, 1000, 1000, 0,
  54. 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391', 0),
  55. self.get_simple_index("index")["bla"])
  56. def test_empty(self):
  57. i = self.get_simple_index("notanindex")
  58. self.assertEquals(0, len(i))
  59. self.assertFalse(os.path.exists(i._filename))
  60. class SimpleIndexWriterTestCase(IndexTestCase):
  61. def setUp(self):
  62. IndexTestCase.setUp(self)
  63. self.tempdir = tempfile.mkdtemp()
  64. def tearDown(self):
  65. IndexTestCase.tearDown(self)
  66. shutil.rmtree(self.tempdir)
  67. def test_simple_write(self):
  68. entries = [('barbla', (1230680220, 0), (1230680220, 0), 2050, 3761020,
  69. 33188, 1000, 1000, 0,
  70. 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391', 0)]
  71. filename = os.path.join(self.tempdir, 'test-simple-write-index')
  72. x = open(filename, 'w+')
  73. try:
  74. write_index(x, entries)
  75. finally:
  76. x.close()
  77. x = open(filename, 'r')
  78. try:
  79. self.assertEquals(entries, list(read_index(x)))
  80. finally:
  81. x.close()
  82. class CommitTreeTests(TestCase):
  83. def setUp(self):
  84. super(CommitTreeTests, self).setUp()
  85. self.store = MemoryObjectStore()
  86. def test_single_blob(self):
  87. blob = Blob()
  88. blob.data = "foo"
  89. self.store.add_object(blob)
  90. blobs = [("bla", blob.id, stat.S_IFREG)]
  91. rootid = commit_tree(self.store, blobs)
  92. self.assertEquals(rootid, "1a1e80437220f9312e855c37ac4398b68e5c1d50")
  93. self.assertEquals((stat.S_IFREG, blob.id), self.store[rootid]["bla"])
  94. self.assertEquals(set([rootid, blob.id]), set(self.store._data.keys()))
  95. def test_nested(self):
  96. blob = Blob()
  97. blob.data = "foo"
  98. self.store.add_object(blob)
  99. blobs = [("bla/bar", blob.id, stat.S_IFREG)]
  100. rootid = commit_tree(self.store, blobs)
  101. self.assertEquals(rootid, "d92b959b216ad0d044671981196781b3258fa537")
  102. dirid = self.store[rootid]["bla"][1]
  103. self.assertEquals(dirid, "c1a1deb9788150829579a8b4efa6311e7b638650")
  104. self.assertEquals((stat.S_IFDIR, dirid), self.store[rootid]["bla"])
  105. self.assertEquals((stat.S_IFREG, blob.id), self.store[dirid]["bar"])
  106. self.assertEquals(set([rootid, dirid, blob.id]),
  107. set(self.store._data.keys()))
  108. class CleanupModeTests(TestCase):
  109. def test_file(self):
  110. self.assertEquals(0100644, cleanup_mode(0100000))
  111. def test_executable(self):
  112. self.assertEquals(0100755, cleanup_mode(0100711))
  113. def test_symlink(self):
  114. self.assertEquals(0120000, cleanup_mode(0120711))
  115. def test_dir(self):
  116. self.assertEquals(0040000, cleanup_mode(040531))
  117. def test_submodule(self):
  118. self.assertEquals(0160000, cleanup_mode(0160744))
  119. class WriteCacheTimeTests(TestCase):
  120. def test_write_string(self):
  121. f = StringIO()
  122. self.assertRaises(TypeError, write_cache_time, f, "foo")
  123. def test_write_int(self):
  124. f = StringIO()
  125. write_cache_time(f, 434343)
  126. self.assertEquals(struct.pack(">LL", 434343, 0), f.getvalue())
  127. def test_write_tuple(self):
  128. f = StringIO()
  129. write_cache_time(f, (434343, 21))
  130. self.assertEquals(struct.pack(">LL", 434343, 21), f.getvalue())
  131. def test_write_float(self):
  132. f = StringIO()
  133. write_cache_time(f, 434343.000000021)
  134. self.assertEquals(struct.pack(">LL", 434343, 21), f.getvalue())