2
0

test_index.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # test_index.py -- Tests for the git index cache
  2. # Copyright (C) 2008 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. import os
  20. import stat
  21. from unittest import TestCase
  22. from dulwich.index import (
  23. Index,
  24. cleanup_mode,
  25. commit_tree,
  26. read_index,
  27. write_index,
  28. )
  29. from dulwich.object_store import (
  30. MemoryObjectStore,
  31. )
  32. from dulwich.objects import (
  33. Blob,
  34. )
  35. class IndexTestCase(TestCase):
  36. datadir = os.path.join(os.path.dirname(__file__), 'data/indexes')
  37. def get_simple_index(self, name):
  38. return Index(os.path.join(self.datadir, name))
  39. class SimpleIndexTestcase(IndexTestCase):
  40. def test_len(self):
  41. self.assertEquals(1, len(self.get_simple_index("index")))
  42. def test_iter(self):
  43. self.assertEquals(['bla'], list(self.get_simple_index("index")))
  44. def test_getitem(self):
  45. self.assertEquals( ((1230680220, 0), (1230680220, 0), 2050, 3761020, 33188, 1000, 1000, 0, 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391', 0)
  46. ,
  47. self.get_simple_index("index")["bla"])
  48. class SimpleIndexWriterTestCase(IndexTestCase):
  49. def test_simple_write(self):
  50. entries = [('barbla', (1230680220, 0), (1230680220, 0), 2050, 3761020, 33188, 1000, 1000, 0, 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391', 0)]
  51. x = open('test-simple-write-index', 'w+')
  52. try:
  53. write_index(x, entries)
  54. finally:
  55. x.close()
  56. x = open('test-simple-write-index', 'r')
  57. try:
  58. self.assertEquals(entries, list(read_index(x)))
  59. finally:
  60. x.close()
  61. class CommitTreeTests(TestCase):
  62. def setUp(self):
  63. super(CommitTreeTests, self).setUp()
  64. self.store = MemoryObjectStore()
  65. def test_single_blob(self):
  66. blob = Blob()
  67. blob.data = "foo"
  68. self.store.add_object(blob)
  69. blobs = [("bla", blob.id, stat.S_IFREG)]
  70. rootid = commit_tree(self.store, blobs)
  71. self.assertEquals(rootid, "1a1e80437220f9312e855c37ac4398b68e5c1d50")
  72. self.assertEquals((stat.S_IFREG, blob.id), self.store[rootid]["bla"])
  73. self.assertEquals(set([rootid, blob.id]), set(self.store._data.keys()))
  74. def test_nested(self):
  75. blob = Blob()
  76. blob.data = "foo"
  77. self.store.add_object(blob)
  78. blobs = [("bla/bar", blob.id, stat.S_IFREG)]
  79. rootid = commit_tree(self.store, blobs)
  80. self.assertEquals(rootid, "d92b959b216ad0d044671981196781b3258fa537")
  81. dirid = self.store[rootid]["bla"][1]
  82. self.assertEquals(dirid, "c1a1deb9788150829579a8b4efa6311e7b638650")
  83. self.assertEquals((stat.S_IFDIR, dirid), self.store[rootid]["bla"])
  84. self.assertEquals((stat.S_IFREG, blob.id), self.store[dirid]["bar"])
  85. self.assertEquals(set([rootid, dirid, blob.id]),
  86. set(self.store._data.keys()))
  87. class CleanupModeTests(TestCase):
  88. def test_file(self):
  89. self.assertEquals(0100644, cleanup_mode(0100000))
  90. def test_executable(self):
  91. self.assertEquals(0100755, cleanup_mode(0100711))
  92. def test_symlink(self):
  93. self.assertEquals(0120000, cleanup_mode(0120711))
  94. def test_dir(self):
  95. self.assertEquals(0040000, cleanup_mode(040531))
  96. def test_submodule(self):
  97. self.assertEquals(0160000, cleanup_mode(0160744))