Change parse_tree to return a list rather than a dict.
[jelmer/dulwich-libgit2.git] / dulwich / tests / test_objects.py
1 # test_objects.py -- tests for objects.py
2 # Copyright (C) 2007 James Westby <jw+debian@jameswestby.net>
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 # of the License or (at your option) any later version of 
8 # the License.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 # MA  02110-1301, USA.
19
20
21 """Tests for git base objects."""
22
23 # TODO: Round-trip parse-serialize-parse and serialize-parse-serialize tests.
24
25
26 import os
27 import stat
28 import unittest
29
30 import nose
31
32 from dulwich.objects import (
33     Blob,
34     Tree,
35     Commit,
36     Tag,
37     format_timezone,
38     hex_to_sha,
39     parse_timezone,
40     parse_tree,
41     _parse_tree_py,
42     )
43
44 a_sha = '6f670c0fb53f9463760b7295fbb814e965fb20c8'
45 b_sha = '2969be3e8ee1c0222396a5611407e4769f14e54b'
46 c_sha = '954a536f7819d40e6f637f849ee187dd10066349'
47 tree_sha = '70c190eb48fa8bbb50ddc692a17b44cb781af7f6'
48 tag_sha = '71033db03a03c6a36721efcf1968dd8f8e0cf023'
49
50 class BlobReadTests(unittest.TestCase):
51     """Test decompression of blobs"""
52   
53     def get_sha_file(self, obj, base, sha):
54         return obj.from_file(os.path.join(os.path.dirname(__file__),
55                                           'data', base, sha))
56   
57     def get_blob(self, sha):
58         """Return the blob named sha from the test data dir"""
59         return self.get_sha_file(Blob, 'blobs', sha)
60   
61     def get_tree(self, sha):
62         return self.get_sha_file(Tree, 'trees', sha)
63   
64     def get_tag(self, sha):
65         return self.get_sha_file(Tag, 'tags', sha)
66   
67     def commit(self, sha):
68         return self.get_sha_file(Commit, 'commits', sha)
69   
70     def test_decompress_simple_blob(self):
71         b = self.get_blob(a_sha)
72         self.assertEqual(b.data, 'test 1\n')
73         self.assertEqual(b.sha().hexdigest(), a_sha)
74   
75     def test_hash(self):
76         b = self.get_blob(a_sha)
77         self.assertEqual(hash(b.id), hash(b))
78
79     def test_parse_empty_blob_object(self):
80         sha = 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391'
81         b = self.get_blob(sha)
82         self.assertEqual(b.data, '')
83         self.assertEqual(b.id, sha)
84         self.assertEqual(b.sha().hexdigest(), sha)
85   
86     def test_create_blob_from_string(self):
87         string = 'test 2\n'
88         b = Blob.from_string(string)
89         self.assertEqual(b.data, string)
90         self.assertEqual(b.sha().hexdigest(), b_sha)
91
92     def test_chunks(self):
93         string = 'test 5\n'
94         b = Blob.from_string(string)
95         self.assertEqual([string], b.chunked)
96
97     def test_set_chunks(self):
98         b = Blob()
99         b.chunked = ['te', 'st', ' 5\n']
100         self.assertEqual('test 5\n', b.data)
101         b.chunked = ['te', 'st', ' 6\n']
102         self.assertEqual('test 6\n', b.as_raw_string())
103   
104     def test_parse_legacy_blob(self):
105         string = 'test 3\n'
106         b = self.get_blob(c_sha)
107         self.assertEqual(b.data, string)
108         self.assertEqual(b.sha().hexdigest(), c_sha)
109   
110     def test_eq(self):
111         blob1 = self.get_blob(a_sha)
112         blob2 = self.get_blob(a_sha)
113         self.assertEqual(blob1, blob2)
114   
115     def test_read_tree_from_file(self):
116         t = self.get_tree(tree_sha)
117         self.assertEqual(t.entries()[0], (33188, 'a', a_sha))
118         self.assertEqual(t.entries()[1], (33188, 'b', b_sha))
119   
120     def test_read_tag_from_file(self):
121         t = self.get_tag(tag_sha)
122         self.assertEqual(t.object, (Commit, '51b668fd5bf7061b7d6fa525f88803e6cfadaa51'))
123         self.assertEqual(t.name,'signed')
124         self.assertEqual(t.tagger,'Ali Sabil <ali.sabil@gmail.com>')
125         self.assertEqual(t.tag_time, 1231203091)
126         self.assertEqual(t.message, 'This is a signed tag\n-----BEGIN PGP SIGNATURE-----\nVersion: GnuPG v1.4.9 (GNU/Linux)\n\niEYEABECAAYFAkliqx8ACgkQqSMmLy9u/kcx5ACfakZ9NnPl02tOyYP6pkBoEkU1\n5EcAn0UFgokaSvS371Ym/4W9iJj6vh3h\n=ql7y\n-----END PGP SIGNATURE-----\n')
127   
128   
129     def test_read_commit_from_file(self):
130         sha = '60dacdc733de308bb77bb76ce0fb0f9b44c9769e'
131         c = self.commit(sha)
132         self.assertEqual(c.tree, tree_sha)
133         self.assertEqual(c.parents, ['0d89f20333fbb1d2f3a94da77f4981373d8f4310'])
134         self.assertEqual(c.author,
135             'James Westby <jw+debian@jameswestby.net>')
136         self.assertEqual(c.committer,
137             'James Westby <jw+debian@jameswestby.net>')
138         self.assertEqual(c.commit_time, 1174759230)
139         self.assertEqual(c.commit_timezone, 0)
140         self.assertEqual(c.author_timezone, 0)
141         self.assertEqual(c.message, 'Test commit\n')
142   
143     def test_read_commit_no_parents(self):
144         sha = '0d89f20333fbb1d2f3a94da77f4981373d8f4310'
145         c = self.commit(sha)
146         self.assertEqual(c.tree, '90182552c4a85a45ec2a835cadc3451bebdfe870')
147         self.assertEqual(c.parents, [])
148         self.assertEqual(c.author,
149             'James Westby <jw+debian@jameswestby.net>')
150         self.assertEqual(c.committer,
151             'James Westby <jw+debian@jameswestby.net>')
152         self.assertEqual(c.commit_time, 1174758034)
153         self.assertEqual(c.commit_timezone, 0)
154         self.assertEqual(c.author_timezone, 0)
155         self.assertEqual(c.message, 'Test commit\n')
156   
157     def test_read_commit_two_parents(self):
158         sha = '5dac377bdded4c9aeb8dff595f0faeebcc8498cc'
159         c = self.commit(sha)
160         self.assertEqual(c.tree, 'd80c186a03f423a81b39df39dc87fd269736ca86')
161         self.assertEqual(c.parents, ['ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd',
162                                        '4cffe90e0a41ad3f5190079d7c8f036bde29cbe6'])
163         self.assertEqual(c.author,
164             'James Westby <jw+debian@jameswestby.net>')
165         self.assertEqual(c.committer,
166             'James Westby <jw+debian@jameswestby.net>')
167         self.assertEqual(c.commit_time, 1174773719)
168         self.assertEqual(c.commit_timezone, 0)
169         self.assertEqual(c.author_timezone, 0)
170         self.assertEqual(c.message, 'Merge ../b\n')
171   
172
173
174 class CommitSerializationTests(unittest.TestCase):
175
176     def make_base(self):
177         c = Commit()
178         c.tree = 'd80c186a03f423a81b39df39dc87fd269736ca86'
179         c.parents = ['ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd', '4cffe90e0a41ad3f5190079d7c8f036bde29cbe6']
180         c.author = 'James Westby <jw+debian@jameswestby.net>'
181         c.committer = 'James Westby <jw+debian@jameswestby.net>'
182         c.commit_time = 1174773719
183         c.author_time = 1174773719
184         c.commit_timezone = 0
185         c.author_timezone = 0
186         c.message =  'Merge ../b\n'
187         return c
188
189     def test_encoding(self):
190         c = self.make_base()
191         c.encoding = "iso8859-1"
192         self.assertTrue("encoding iso8859-1\n" in c.as_raw_string())        
193
194     def test_short_timestamp(self):
195         c = self.make_base()
196         c.commit_time = 30
197         c1 = Commit()
198         c1.set_raw_string(c.as_raw_string())
199         self.assertEquals(30, c1.commit_time)
200
201     def test_raw_length(self):
202         c = self.make_base()
203         self.assertEquals(len(c.as_raw_string()), c.raw_length())
204
205     def test_simple(self):
206         c = self.make_base()
207         self.assertEquals(c.id, '5dac377bdded4c9aeb8dff595f0faeebcc8498cc')
208         self.assertEquals(
209                 'tree d80c186a03f423a81b39df39dc87fd269736ca86\n'
210                 'parent ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd\n'
211                 'parent 4cffe90e0a41ad3f5190079d7c8f036bde29cbe6\n'
212                 'author James Westby <jw+debian@jameswestby.net> 1174773719 +0000\n'
213                 'committer James Westby <jw+debian@jameswestby.net> 1174773719 +0000\n'
214                 '\n'
215                 'Merge ../b\n', c.as_raw_string())
216
217     def test_timezone(self):
218         c = self.make_base()
219         c.commit_timezone = 5 * 60
220         self.assertTrue(" +0005\n" in c.as_raw_string())
221
222     def test_neg_timezone(self):
223         c = self.make_base()
224         c.commit_timezone = -1 * 3600
225         self.assertTrue(" -0100\n" in c.as_raw_string())
226
227
228 class CommitDeserializationTests(unittest.TestCase):
229
230     def test_simple(self):
231         c = Commit.from_string(
232                 'tree d80c186a03f423a81b39df39dc87fd269736ca86\n'
233                 'parent ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd\n'
234                 'parent 4cffe90e0a41ad3f5190079d7c8f036bde29cbe6\n'
235                 'author James Westby <jw+debian@jameswestby.net> 1174773719 +0000\n'
236                 'committer James Westby <jw+debian@jameswestby.net> 1174773719 +0000\n'
237                 '\n'
238                 'Merge ../b\n')
239         self.assertEquals('Merge ../b\n', c.message)
240         self.assertEquals('James Westby <jw+debian@jameswestby.net>',
241             c.author)
242         self.assertEquals('James Westby <jw+debian@jameswestby.net>',
243             c.committer)
244         self.assertEquals('d80c186a03f423a81b39df39dc87fd269736ca86',
245             c.tree)
246         self.assertEquals(['ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd',
247                           '4cffe90e0a41ad3f5190079d7c8f036bde29cbe6'],
248             c.parents)
249
250     def test_custom(self):
251         c = Commit.from_string(
252                 'tree d80c186a03f423a81b39df39dc87fd269736ca86\n'
253                 'parent ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd\n'
254                 'parent 4cffe90e0a41ad3f5190079d7c8f036bde29cbe6\n'
255                 'author James Westby <jw+debian@jameswestby.net> 1174773719 +0000\n'
256                 'committer James Westby <jw+debian@jameswestby.net> 1174773719 +0000\n'
257                 'extra-field data\n'
258                 '\n'
259                 'Merge ../b\n')
260         self.assertEquals([('extra-field', 'data')], c.extra)
261
262
263 class TreeTests(unittest.TestCase):
264
265     def test_simple(self):
266         myhexsha = "d80c186a03f423a81b39df39dc87fd269736ca86"
267         x = Tree()
268         x["myname"] = (0100755, myhexsha)
269         self.assertEquals('100755 myname\0' + hex_to_sha(myhexsha),
270                 x.as_raw_string())
271
272     def test_tree_dir_sort(self):
273         x = Tree()
274         x["a.c"] = (0100755, "d80c186a03f423a81b39df39dc87fd269736ca86")
275         x["a"] = (stat.S_IFDIR, "d80c186a03f423a81b39df39dc87fd269736ca86")
276         x["a/c"] = (stat.S_IFDIR, "d80c186a03f423a81b39df39dc87fd269736ca86")
277         self.assertEquals(["a.c", "a", "a/c"], [p[0] for p in x.iteritems()])
278
279     def _do_test_parse_tree(self, parse_tree):
280         o = Tree.from_file(os.path.join(os.path.dirname(__file__), 'data',
281                                         'trees', tree_sha))
282         self.assertEquals([('a', 0100644, a_sha), ('b', 0100644, b_sha)],
283                           list(parse_tree(o.as_raw_string())))
284
285     def test_parse_tree(self):
286         self._do_test_parse_tree(_parse_tree_py)
287
288     def test_parse_tree_extension(self):
289         if parse_tree is _parse_tree_py:
290             raise nose.SkipTest('parse_tree extension not found')
291         self._do_test_parse_tree(parse_tree)
292
293
294 class TagSerializeTests(unittest.TestCase):
295
296     def test_serialize_simple(self):
297         x = Tag()
298         x.tagger = "Jelmer Vernooij <jelmer@samba.org>"
299         x.name = "0.1"
300         x.message = "Tag 0.1"
301         x.object = (Blob, "d80c186a03f423a81b39df39dc87fd269736ca86")
302         x.tag_time = 423423423
303         x.tag_timezone = 0
304         self.assertEquals("""object d80c186a03f423a81b39df39dc87fd269736ca86
305 type blob
306 tag 0.1
307 tagger Jelmer Vernooij <jelmer@samba.org> 423423423 +0000
308
309 Tag 0.1""", x.as_raw_string())
310
311
312 class TagParseTests(unittest.TestCase):
313
314     def test_parse_ctime(self):
315         x = Tag()
316         x.set_raw_string("""object a38d6181ff27824c79fc7df825164a212eff6a3f
317 type commit
318 tag v2.6.22-rc7
319 tagger Linus Torvalds <torvalds@woody.linux-foundation.org> Sun Jul 1 12:54:34 2007 -0700
320
321 Linux 2.6.22-rc7
322 -----BEGIN PGP SIGNATURE-----
323 Version: GnuPG v1.4.7 (GNU/Linux)
324
325 iD8DBQBGiAaAF3YsRnbiHLsRAitMAKCiLboJkQECM/jpYsY3WPfvUgLXkACgg3ql
326 OK2XeQOiEeXtT76rV4t2WR4=
327 =ivrA
328 -----END PGP SIGNATURE-----
329 """)
330         self.assertEquals("Linus Torvalds <torvalds@woody.linux-foundation.org>", x.tagger)
331         self.assertEquals("v2.6.22-rc7", x.name)
332
333     def test_parse_no_tagger(self):
334         x = Tag()
335         x.set_raw_string("""object a38d6181ff27824c79fc7df825164a212eff6a3f
336 type commit
337 tag v2.6.22-rc7
338
339 Linux 2.6.22-rc7
340 -----BEGIN PGP SIGNATURE-----
341 Version: GnuPG v1.4.7 (GNU/Linux)
342
343 iD8DBQBGiAaAF3YsRnbiHLsRAitMAKCiLboJkQECM/jpYsY3WPfvUgLXkACgg3ql
344 OK2XeQOiEeXtT76rV4t2WR4=
345 =ivrA
346 -----END PGP SIGNATURE-----
347 """)
348         self.assertEquals(None, x.tagger)
349         self.assertEquals("v2.6.22-rc7", x.name)
350
351
352 class TimezoneTests(unittest.TestCase):
353
354     def test_parse_timezone_utc(self):
355         self.assertEquals(0, parse_timezone("+0000"))
356
357     def test_generate_timezone_utc(self):
358         self.assertEquals("+0000", format_timezone(0))
359
360     def test_parse_timezone_cet(self):
361         self.assertEquals(60 * 60, parse_timezone("+0100"))
362
363     def test_format_timezone_cet(self):
364         self.assertEquals("+0100", format_timezone(60 * 60))
365
366     def test_format_timezone_pdt(self):
367         self.assertEquals("-0400", format_timezone(-4 * 60 * 60))
368
369     def test_parse_timezone_pdt(self):
370         self.assertEquals(-4 * 60 * 60, parse_timezone("-0400"))
371
372     def test_format_timezone_pdt_half(self):
373         self.assertEquals("-0440", format_timezone(int(((-4 * 60) - 40) * 60)))
374
375     def test_parse_timezone_pdt_half(self):
376         self.assertEquals(((-4 * 60) - 40) * 60, parse_timezone("-0440"))