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