Treat non-existent index files as empty.
[jelmer/dulwich-libgit2.git] / dulwich / tests / test_index.py
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
19
20 """Tests for the index."""
21
22
23 from cStringIO import (
24     StringIO,
25     )
26 import os
27 import shutil
28 import stat
29 import struct
30 import tempfile
31 from unittest import TestCase
32
33 from dulwich.index import (
34     Index,
35     cleanup_mode,
36     commit_tree,
37     read_index,
38     write_cache_time,
39     write_index,
40     )
41 from dulwich.object_store import (
42     MemoryObjectStore,
43     )
44 from dulwich.objects import (
45     Blob,
46     )
47
48
49 class IndexTestCase(TestCase):
50
51     datadir = os.path.join(os.path.dirname(__file__), 'data/indexes')
52
53     def get_simple_index(self, name):
54         return Index(os.path.join(self.datadir, name))
55
56
57 class SimpleIndexTestCase(IndexTestCase):
58
59     def test_len(self):
60         self.assertEquals(1, len(self.get_simple_index("index")))
61
62     def test_iter(self):
63         self.assertEquals(['bla'], list(self.get_simple_index("index")))
64
65     def test_getitem(self):
66         self.assertEquals(((1230680220, 0), (1230680220, 0), 2050, 3761020,
67                            33188, 1000, 1000, 0,
68                            'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391', 0),
69                           self.get_simple_index("index")["bla"])
70
71     def test_empty(self):
72         i = self.get_simple_index("notanindex")
73         self.assertEquals(0, len(i))
74         self.assertFalse(os.path.exists(i._filename))
75
76
77 class SimpleIndexWriterTestCase(IndexTestCase):
78
79     def setUp(self):
80         IndexTestCase.setUp(self)
81         self.tempdir = tempfile.mkdtemp()
82
83     def tearDown(self):
84         IndexTestCase.tearDown(self)
85         shutil.rmtree(self.tempdir)
86
87     def test_simple_write(self):
88         entries = [('barbla', (1230680220, 0), (1230680220, 0), 2050, 3761020,
89                     33188, 1000, 1000, 0,
90                     'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391', 0)]
91         filename = os.path.join(self.tempdir, 'test-simple-write-index')
92         x = open(filename, 'w+')
93         try:
94             write_index(x, entries)
95         finally:
96             x.close()
97         x = open(filename, 'r')
98         try:
99             self.assertEquals(entries, list(read_index(x)))
100         finally:
101             x.close()
102
103
104 class CommitTreeTests(TestCase):
105
106     def setUp(self):
107         super(CommitTreeTests, self).setUp()
108         self.store = MemoryObjectStore()
109
110     def test_single_blob(self):
111         blob = Blob()
112         blob.data = "foo"
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()))
119
120     def test_nested(self):
121         blob = Blob()
122         blob.data = "foo"
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()))
133
134
135 class CleanupModeTests(TestCase):
136
137     def test_file(self):
138         self.assertEquals(0100644, cleanup_mode(0100000))
139
140     def test_executable(self):
141         self.assertEquals(0100755, cleanup_mode(0100711))
142
143     def test_symlink(self):
144         self.assertEquals(0120000, cleanup_mode(0120711))
145
146     def test_dir(self):
147         self.assertEquals(0040000, cleanup_mode(040531))
148
149     def test_submodule(self):
150         self.assertEquals(0160000, cleanup_mode(0160744))
151
152
153 class WriteCacheTimeTests(TestCase):
154
155     def test_write_string(self):
156         f = StringIO()
157         self.assertRaises(TypeError, write_cache_time, f, "foo")
158
159     def test_write_int(self):
160         f = StringIO()
161         write_cache_time(f, 434343)
162         self.assertEquals(struct.pack(">LL", 434343, 0), f.getvalue())
163
164     def test_write_tuple(self):
165         f = StringIO()
166         write_cache_time(f, (434343, 21))
167         self.assertEquals(struct.pack(">LL", 434343, 21), f.getvalue())
168
169     def test_write_float(self):
170         f = StringIO()
171         write_cache_time(f, 434343.000000021)
172         self.assertEquals(struct.pack(">LL", 434343, 21), f.getvalue())