test_index.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. import os
  19. import stat
  20. from unittest import TestCase
  21. from dulwich.index import (
  22. Index,
  23. commit_tree,
  24. read_index,
  25. write_index,
  26. )
  27. from dulwich.object_store import (
  28. MemoryObjectStore,
  29. )
  30. from dulwich.objects import (
  31. Blob,
  32. )
  33. class IndexTestCase(TestCase):
  34. datadir = os.path.join(os.path.dirname(__file__), 'data/indexes')
  35. def get_simple_index(self, name):
  36. return Index(os.path.join(self.datadir, name))
  37. class SimpleIndexTestcase(IndexTestCase):
  38. def test_len(self):
  39. self.assertEquals(1, len(self.get_simple_index("index")))
  40. def test_iter(self):
  41. self.assertEquals(['bla'], list(self.get_simple_index("index")))
  42. def test_getitem(self):
  43. self.assertEquals( ((1230680220, 0), (1230680220, 0), 2050, 3761020, 33188, 1000, 1000, 0, 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391', 3)
  44. ,
  45. self.get_simple_index("index")["bla"])
  46. class SimpleIndexWriterTestCase(IndexTestCase):
  47. def test_simple_write(self):
  48. entries = [('barbla', (1230680220, 0), (1230680220, 0), 2050, 3761020, 33188, 1000, 1000, 0, 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391', 3)]
  49. x = open('test-simple-write-index', 'w+')
  50. try:
  51. write_index(x, entries)
  52. finally:
  53. x.close()
  54. x = open('test-simple-write-index', 'r')
  55. try:
  56. self.assertEquals(entries, list(read_index(x)))
  57. finally:
  58. x.close()
  59. class CommitTreeTests(TestCase):
  60. def setUp(self):
  61. super(CommitTreeTests, self).setUp()
  62. self.store = MemoryObjectStore()
  63. def test_single_blob(self):
  64. blob = Blob()
  65. blob.data = "foo"
  66. self.store.add_object(blob)
  67. blobs = [("bla", blob.id, stat.S_IFREG)]
  68. rootid = commit_tree(self.store, blobs)
  69. self.assertEquals(rootid, "1a1e80437220f9312e855c37ac4398b68e5c1d50")
  70. self.assertEquals(blob.id, self.store[rootid]["bla"][1])
  71. self.assertEquals(set([rootid, blob.id]), set(self.store._data.keys()))