test_index.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 unittest import TestCase
  28. from dulwich.index import (
  29. Index,
  30. cleanup_mode,
  31. commit_tree,
  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. )
  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. class SimpleIndexWriterTestCase(IndexTestCase):
  57. def setUp(self):
  58. IndexTestCase.setUp(self)
  59. self.tempdir = tempfile.mkdtemp()
  60. def tearDown(self):
  61. IndexTestCase.tearDown(self)
  62. shutil.rmtree(self.tempdir)
  63. def test_simple_write(self):
  64. entries = [('barbla', (1230680220, 0), (1230680220, 0), 2050, 3761020,
  65. 33188, 1000, 1000, 0,
  66. 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391', 0)]
  67. filename = os.path.join(self.tempdir, 'test-simple-write-index')
  68. x = open(filename, 'w+')
  69. try:
  70. write_index(x, entries)
  71. finally:
  72. x.close()
  73. x = open(filename, 'r')
  74. try:
  75. self.assertEquals(entries, list(read_index(x)))
  76. finally:
  77. x.close()
  78. class CommitTreeTests(TestCase):
  79. def setUp(self):
  80. super(CommitTreeTests, self).setUp()
  81. self.store = MemoryObjectStore()
  82. def test_single_blob(self):
  83. blob = Blob()
  84. blob.data = "foo"
  85. self.store.add_object(blob)
  86. blobs = [("bla", blob.id, stat.S_IFREG)]
  87. rootid = commit_tree(self.store, blobs)
  88. self.assertEquals(rootid, "1a1e80437220f9312e855c37ac4398b68e5c1d50")
  89. self.assertEquals((stat.S_IFREG, blob.id), self.store[rootid]["bla"])
  90. self.assertEquals(set([rootid, blob.id]), set(self.store._data.keys()))
  91. def test_nested(self):
  92. blob = Blob()
  93. blob.data = "foo"
  94. self.store.add_object(blob)
  95. blobs = [("bla/bar", blob.id, stat.S_IFREG)]
  96. rootid = commit_tree(self.store, blobs)
  97. self.assertEquals(rootid, "d92b959b216ad0d044671981196781b3258fa537")
  98. dirid = self.store[rootid]["bla"][1]
  99. self.assertEquals(dirid, "c1a1deb9788150829579a8b4efa6311e7b638650")
  100. self.assertEquals((stat.S_IFDIR, dirid), self.store[rootid]["bla"])
  101. self.assertEquals((stat.S_IFREG, blob.id), self.store[dirid]["bar"])
  102. self.assertEquals(set([rootid, dirid, blob.id]),
  103. set(self.store._data.keys()))
  104. class CleanupModeTests(TestCase):
  105. def test_file(self):
  106. self.assertEquals(0100644, cleanup_mode(0100000))
  107. def test_executable(self):
  108. self.assertEquals(0100755, cleanup_mode(0100711))
  109. def test_symlink(self):
  110. self.assertEquals(0120000, cleanup_mode(0120711))
  111. def test_dir(self):
  112. self.assertEquals(0040000, cleanup_mode(040531))
  113. def test_submodule(self):
  114. self.assertEquals(0160000, cleanup_mode(0160744))
  115. class WriteCacheTimeTests(TestCase):
  116. def test_write_string(self):
  117. f = StringIO()
  118. self.assertRaises(TypeError, write_cache_time, f, "foo")
  119. def test_write_int(self):
  120. f = StringIO()
  121. write_cache_time(f, 434343)
  122. self.assertEquals(struct.pack(">LL", 434343, 0), f.getvalue())
  123. def test_write_tuple(self):
  124. f = StringIO()
  125. write_cache_time(f, (434343, 21))
  126. self.assertEquals(struct.pack(">LL", 434343, 21), f.getvalue())
  127. def test_write_float(self):
  128. f = StringIO()
  129. write_cache_time(f, 434343.000000021)
  130. self.assertEquals(struct.pack(">LL", 434343, 21), f.getvalue())