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