1 # test_objects.py -- tests for objects.py
2 # Copyright (C) 2007 James Westby <jw+debian@jameswestby.net>
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
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.
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,
22 from dulwich.objects import (Blob,
27 a_sha = '6f670c0fb53f9463760b7295fbb814e965fb20c8'
28 b_sha = '2969be3e8ee1c0222396a5611407e4769f14e54b'
29 c_sha = '954a536f7819d40e6f637f849ee187dd10066349'
30 tree_sha = '70c190eb48fa8bbb50ddc692a17b44cb781af7f6'
32 class BlobReadTests(unittest.TestCase):
33 """Test decompression of blobs"""
35 def get_sha_file(self, obj, base, sha):
36 return obj.from_file(os.path.join(os.path.dirname(__file__),
39 def get_blob(self, sha):
40 """Return the blob named sha from the test data dir"""
41 return self.get_sha_file(Blob, 'blobs', sha)
43 def get_tree(self, sha):
44 return self.get_sha_file(Tree, 'trees', sha)
46 def get_commit(self, sha):
47 return self.get_sha_file(Commit, 'commits', sha)
49 def test_decompress_simple_blob(self):
50 b = self.get_blob(a_sha)
51 self.assertEqual(b.text(), 'test 1\n')
52 self.assertEqual(b.sha().hexdigest(), a_sha)
54 def test_parse_empty_blob_object(self):
55 sha = 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391'
56 b = self.get_blob(sha)
57 self.assertEqual(b.text(), '')
58 self.assertEqual(b.sha().hexdigest(), sha)
60 def test_create_blob_from_string(self):
62 b = Blob.from_string(string)
63 self.assertEqual(b.text(), string)
64 self.assertEqual(b.sha().hexdigest(), b_sha)
66 def test_parse_legacy_blob(self):
68 b = self.get_blob(c_sha)
69 self.assertEqual(b.text(), string)
70 self.assertEqual(b.sha().hexdigest(), c_sha)
73 blob1 = self.get_blob(a_sha)
74 blob2 = self.get_blob(a_sha)
75 self.assertEqual(blob1, blob2)
77 def test_read_tree_from_file(self):
78 t = self.get_tree(tree_sha)
79 self.assertEqual(t.entries()[0], (33188, 'a', a_sha))
80 self.assertEqual(t.entries()[1], (33188, 'b', b_sha))
82 def test_read_commit_from_file(self):
83 sha = '60dacdc733de308bb77bb76ce0fb0f9b44c9769e'
84 c = self.get_commit(sha)
85 self.assertEqual(c.tree(), tree_sha)
86 self.assertEqual(c.parents(), ['0d89f20333fbb1d2f3a94da77f4981373d8f4310'])
87 self.assertEqual(c.author(),
88 'James Westby <jw+debian@jameswestby.net>')
89 self.assertEqual(c.committer(),
90 'James Westby <jw+debian@jameswestby.net>')
91 self.assertEqual(c.commit_time(), 1174759230)
92 self.assertEqual(c.message(), 'Test commit\n')
94 def test_read_commit_no_parents(self):
95 sha = '0d89f20333fbb1d2f3a94da77f4981373d8f4310'
96 c = self.get_commit(sha)
97 self.assertEqual(c.tree(), '90182552c4a85a45ec2a835cadc3451bebdfe870')
98 self.assertEqual(c.parents(), [])
99 self.assertEqual(c.author(),
100 'James Westby <jw+debian@jameswestby.net>')
101 self.assertEqual(c.committer(),
102 'James Westby <jw+debian@jameswestby.net>')
103 self.assertEqual(c.commit_time(), 1174758034)
104 self.assertEqual(c.message(), 'Test commit\n')
106 def test_read_commit_two_parents(self):
107 sha = '5dac377bdded4c9aeb8dff595f0faeebcc8498cc'
108 c = self.get_commit(sha)
109 self.assertEqual(c.tree(), 'd80c186a03f423a81b39df39dc87fd269736ca86')
110 self.assertEqual(c.parents(), ['ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd',
111 '4cffe90e0a41ad3f5190079d7c8f036bde29cbe6'])
112 self.assertEqual(c.author(),
113 'James Westby <jw+debian@jameswestby.net>')
114 self.assertEqual(c.committer(),
115 'James Westby <jw+debian@jameswestby.net>')
116 self.assertEqual(c.commit_time(), 1174773719)
117 self.assertEqual(c.message(), 'Merge ../b\n')