1 # test_index.py -- Tests for the git index
2 # Copyright (C) 2008-2009 Jelmer Vernooij <jelmer@samba.org>
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.
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.
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,
20 """Tests for the index."""
23 from cStringIO import (
31 from unittest import TestCase
33 from dulwich.index import (
41 from dulwich.object_store import (
44 from dulwich.objects import (
49 class IndexTestCase(TestCase):
51 datadir = os.path.join(os.path.dirname(__file__), 'data/indexes')
53 def get_simple_index(self, name):
54 return Index(os.path.join(self.datadir, name))
57 class SimpleIndexTestCase(IndexTestCase):
60 self.assertEquals(1, len(self.get_simple_index("index")))
63 self.assertEquals(['bla'], list(self.get_simple_index("index")))
65 def test_getitem(self):
66 self.assertEquals(((1230680220, 0), (1230680220, 0), 2050, 3761020,
68 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391', 0),
69 self.get_simple_index("index")["bla"])
72 i = self.get_simple_index("notanindex")
73 self.assertEquals(0, len(i))
74 self.assertFalse(os.path.exists(i._filename))
77 class SimpleIndexWriterTestCase(IndexTestCase):
80 IndexTestCase.setUp(self)
81 self.tempdir = tempfile.mkdtemp()
84 IndexTestCase.tearDown(self)
85 shutil.rmtree(self.tempdir)
87 def test_simple_write(self):
88 entries = [('barbla', (1230680220, 0), (1230680220, 0), 2050, 3761020,
90 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391', 0)]
91 filename = os.path.join(self.tempdir, 'test-simple-write-index')
92 x = open(filename, 'w+')
94 write_index(x, entries)
97 x = open(filename, 'r')
99 self.assertEquals(entries, list(read_index(x)))
104 class CommitTreeTests(TestCase):
107 super(CommitTreeTests, self).setUp()
108 self.store = MemoryObjectStore()
110 def test_single_blob(self):
113 self.store.add_object(blob)
114 blobs = [("bla", blob.id, stat.S_IFREG)]
115 rootid = commit_tree(self.store, blobs)
116 self.assertEquals(rootid, "1a1e80437220f9312e855c37ac4398b68e5c1d50")
117 self.assertEquals((stat.S_IFREG, blob.id), self.store[rootid]["bla"])
118 self.assertEquals(set([rootid, blob.id]), set(self.store._data.keys()))
120 def test_nested(self):
123 self.store.add_object(blob)
124 blobs = [("bla/bar", blob.id, stat.S_IFREG)]
125 rootid = commit_tree(self.store, blobs)
126 self.assertEquals(rootid, "d92b959b216ad0d044671981196781b3258fa537")
127 dirid = self.store[rootid]["bla"][1]
128 self.assertEquals(dirid, "c1a1deb9788150829579a8b4efa6311e7b638650")
129 self.assertEquals((stat.S_IFDIR, dirid), self.store[rootid]["bla"])
130 self.assertEquals((stat.S_IFREG, blob.id), self.store[dirid]["bar"])
131 self.assertEquals(set([rootid, dirid, blob.id]),
132 set(self.store._data.keys()))
135 class CleanupModeTests(TestCase):
138 self.assertEquals(0100644, cleanup_mode(0100000))
140 def test_executable(self):
141 self.assertEquals(0100755, cleanup_mode(0100711))
143 def test_symlink(self):
144 self.assertEquals(0120000, cleanup_mode(0120711))
147 self.assertEquals(0040000, cleanup_mode(040531))
149 def test_submodule(self):
150 self.assertEquals(0160000, cleanup_mode(0160744))
153 class WriteCacheTimeTests(TestCase):
155 def test_write_string(self):
157 self.assertRaises(TypeError, write_cache_time, f, "foo")
159 def test_write_int(self):
161 write_cache_time(f, 434343)
162 self.assertEquals(struct.pack(">LL", 434343, 0), f.getvalue())
164 def test_write_tuple(self):
166 write_cache_time(f, (434343, 21))
167 self.assertEquals(struct.pack(">LL", 434343, 21), f.getvalue())
169 def test_write_float(self):
171 write_cache_time(f, 434343.000000021)
172 self.assertEquals(struct.pack(">LL", 434343, 21), f.getvalue())