Merge Dave's fixes for the compatibility tests and web.
[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 stat
28 import struct
29 from unittest import TestCase
30
31 from dulwich.index import (
32     Index,
33     cleanup_mode,
34     commit_tree,
35     read_index,
36     write_cache_time,
37     write_index,
38     )
39 from dulwich.object_store import (
40     MemoryObjectStore,
41     )
42 from dulwich.objects import (
43     Blob,
44     )
45
46 class IndexTestCase(TestCase):
47
48     datadir = os.path.join(os.path.dirname(__file__), 'data/indexes')
49
50     def get_simple_index(self, name):
51         return Index(os.path.join(self.datadir, name))
52
53
54 class SimpleIndexTestcase(IndexTestCase):
55
56     def test_len(self):
57         self.assertEquals(1, len(self.get_simple_index("index")))
58
59     def test_iter(self):
60         self.assertEquals(['bla'], list(self.get_simple_index("index")))
61
62     def test_getitem(self):
63         self.assertEquals( ((1230680220, 0), (1230680220, 0), 2050, 3761020, 33188, 1000, 1000, 0, 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391', 0)
64             , 
65                 self.get_simple_index("index")["bla"])
66
67
68 class SimpleIndexWriterTestCase(IndexTestCase):
69
70     def test_simple_write(self):
71         entries = [('barbla', (1230680220, 0), (1230680220, 0), 2050, 3761020, 33188, 1000, 1000, 0, 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391', 0)]
72         x = open('test-simple-write-index', 'w+')
73         try:
74             write_index(x, entries)
75         finally:
76             x.close()
77         x = open('test-simple-write-index', 'r')
78         try:
79             self.assertEquals(entries, list(read_index(x)))
80         finally:
81             x.close()
82
83
84 class CommitTreeTests(TestCase):
85
86     def setUp(self):
87         super(CommitTreeTests, self).setUp()
88         self.store = MemoryObjectStore()
89
90     def test_single_blob(self):
91         blob = Blob()
92         blob.data = "foo"
93         self.store.add_object(blob)
94         blobs = [("bla", blob.id, stat.S_IFREG)]
95         rootid = commit_tree(self.store, blobs)
96         self.assertEquals(rootid, "1a1e80437220f9312e855c37ac4398b68e5c1d50")
97         self.assertEquals((stat.S_IFREG, blob.id), self.store[rootid]["bla"])
98         self.assertEquals(set([rootid, blob.id]), set(self.store._data.keys()))
99
100     def test_nested(self):
101         blob = Blob()
102         blob.data = "foo"
103         self.store.add_object(blob)
104         blobs = [("bla/bar", blob.id, stat.S_IFREG)]
105         rootid = commit_tree(self.store, blobs)
106         self.assertEquals(rootid, "d92b959b216ad0d044671981196781b3258fa537")
107         dirid = self.store[rootid]["bla"][1]
108         self.assertEquals(dirid, "c1a1deb9788150829579a8b4efa6311e7b638650")
109         self.assertEquals((stat.S_IFDIR, dirid), self.store[rootid]["bla"])
110         self.assertEquals((stat.S_IFREG, blob.id), self.store[dirid]["bar"])
111         self.assertEquals(set([rootid, dirid, blob.id]), 
112                           set(self.store._data.keys()))
113
114
115 class CleanupModeTests(TestCase):
116
117     def test_file(self):
118         self.assertEquals(0100644, cleanup_mode(0100000))
119
120     def test_executable(self):
121         self.assertEquals(0100755, cleanup_mode(0100711))
122
123     def test_symlink(self):
124         self.assertEquals(0120000, cleanup_mode(0120711))
125
126     def test_dir(self):
127         self.assertEquals(0040000, cleanup_mode(040531))
128
129     def test_submodule(self):
130         self.assertEquals(0160000, cleanup_mode(0160744))
131
132
133 class WriteCacheTimeTests(TestCase):
134
135     def test_write_string(self):
136         f = StringIO()
137         self.assertRaises(TypeError, write_cache_time, f, "foo")
138
139     def test_write_int(self):
140         f = StringIO()
141         write_cache_time(f, 434343)
142         self.assertEquals(struct.pack(">LL", 434343, 0), f.getvalue())
143
144     def test_write_tuple(self):
145         f = StringIO()
146         write_cache_time(f, (434343, 21))
147         self.assertEquals(struct.pack(">LL", 434343, 21), f.getvalue())
148
149     def test_write_float(self):
150         f = StringIO()
151         write_cache_time(f, 434343.000000021)
152         self.assertEquals(struct.pack(">LL", 434343, 21), f.getvalue())