1 # test_repository.py -- tests for repository.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
7 # of the License or (at your option) any later version of
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.
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,
21 """Tests for the repository."""
27 from dulwich import errors
28 from dulwich.repo import Repo
30 missing_sha = 'b91fa4d900e17e99b433218e988c4eb4a3e9a097'
32 class RepositoryTests(unittest.TestCase):
34 def open_repo(self, name):
35 return Repo(os.path.join(os.path.dirname(__file__),
36 'data', 'repos', name))
38 def test_simple_props(self):
39 r = self.open_repo('a.git')
40 basedir = os.path.join(os.path.dirname(__file__),
41 os.path.join('data', 'repos', 'a.git'))
42 self.assertEqual(r.controldir(), basedir)
45 r = self.open_repo('a.git')
46 self.assertEqual(r.ref('refs/heads/master'),
47 'a90fa2d900a17e99b433217e988c4eb4a2e9a097')
49 def test_get_refs(self):
50 r = self.open_repo('a.git')
52 'HEAD': 'a90fa2d900a17e99b433217e988c4eb4a2e9a097',
53 'refs/heads/master': 'a90fa2d900a17e99b433217e988c4eb4a2e9a097'
57 r = self.open_repo('a.git')
58 self.assertEqual(r.head(), 'a90fa2d900a17e99b433217e988c4eb4a2e9a097')
60 def test_get_object(self):
61 r = self.open_repo('a.git')
62 obj = r.get_object(r.head())
63 self.assertEqual(obj._type, 'commit')
65 def test_get_object_non_existant(self):
66 r = self.open_repo('a.git')
67 self.assertRaises(KeyError, r.get_object, missing_sha)
69 def test_commit(self):
70 r = self.open_repo('a.git')
71 obj = r.commit(r.head())
72 self.assertEqual(obj._type, 'commit')
74 def test_commit_not_commit(self):
75 r = self.open_repo('a.git')
76 self.assertRaises(errors.NotCommitError,
77 r.commit, '4f2e6529203aa6d44b5af6e3292c837ceda003f9')
80 r = self.open_repo('a.git')
81 commit = r.commit(r.head())
82 tree = r.tree(commit.tree)
83 self.assertEqual(tree._type, 'tree')
84 self.assertEqual(tree.sha().hexdigest(), commit.tree)
86 def test_tree_not_tree(self):
87 r = self.open_repo('a.git')
88 self.assertRaises(errors.NotTreeError, r.tree, r.head())
90 def test_get_blob(self):
91 r = self.open_repo('a.git')
92 commit = r.commit(r.head())
93 tree = r.tree(commit.tree)
94 blob_sha = tree.entries()[0][2]
95 blob = r.get_blob(blob_sha)
96 self.assertEqual(blob._type, 'blob')
97 self.assertEqual(blob.sha().hexdigest(), blob_sha)
99 def test_get_blob_notblob(self):
100 r = self.open_repo('a.git')
101 self.assertRaises(errors.NotBlobError, r.get_blob, r.head())
103 def test_linear_history(self):
104 r = self.open_repo('a.git')
105 history = r.revision_history(r.head())
106 shas = [c.sha().hexdigest() for c in history]
107 self.assertEqual(shas, [r.head(),
108 '2a72d929692c41d8554c07f6301757ba18a65d91'])
110 def test_merge_history(self):
111 r = self.open_repo('simple_merge.git')
112 history = r.revision_history(r.head())
113 shas = [c.sha().hexdigest() for c in history]
114 self.assertEqual(shas, ['5dac377bdded4c9aeb8dff595f0faeebcc8498cc',
115 'ab64bbdcc51b170d21588e5c5d391ee5c0c96dfd',
116 '4cffe90e0a41ad3f5190079d7c8f036bde29cbe6',
117 '60dacdc733de308bb77bb76ce0fb0f9b44c9769e',
118 '0d89f20333fbb1d2f3a94da77f4981373d8f4310'])
120 def test_revision_history_missing_commit(self):
121 r = self.open_repo('simple_merge.git')
122 self.assertRaises(errors.MissingCommitError, r.revision_history,
125 def test_out_of_order_merge(self):
126 """Test that revision history is ordered by date, not parent order."""
127 r = self.open_repo('ooo_merge.git')
128 history = r.revision_history(r.head())
129 shas = [c.sha().hexdigest() for c in history]
130 self.assertEqual(shas, ['7601d7f6231db6a57f7bbb79ee52e4d462fd44d1',
131 'f507291b64138b875c28e03469025b1ea20bc614',
132 'fb5b0425c7ce46959bec94d54b9a157645e114f5',
133 'f9e39b120c68182a4ba35349f832d0e4e61f485c'])
135 def test_get_tags_empty(self):
136 r = self.open_repo('ooo_merge.git')
137 self.assertEquals({}, r.refs.as_dict('refs/tags'))
139 def test_get_config(self):
140 r = self.open_repo('ooo_merge.git')
141 self.assertEquals({}, r.get_config())