use ~/foo instead of /foo for repo.
[jelmer/gitpython.git] / test / git / test_repo.py
1 import os
2 import time
3 from test.testlib import *
4 from git_python import *
5
6 class TestRepo(object):
7     def setup(self):
8         self.repo = Repo(GIT_REPO)
9     
10     @raises(InvalidGitRepositoryError)
11     def test_new_should_raise_on_invalid_repo_location(self):
12         Repo("/tmp")
13
14     @raises(NoSuchPathError)
15     def test_new_should_raise_on_non_existant_path(self):
16         Repo("~/foobar")
17
18     def test_description(self):
19         assert_equal("Unnamed repository; edit this file to name it for gitweb.", self.repo.description)
20
21     def test_heads_should_return_array_of_head_objects(self):
22         for head in self.repo.heads:
23             assert_equal(Head, head.__class__)
24
25     @patch(Git, 'method_missing')
26     def test_heads_should_populate_head_data(self, git):
27         git.return_value = fixture('for_each_ref')
28         
29         head = self.repo.heads[0]
30         assert_equal('master', head.name)
31         assert_equal('634396b2f541a9f2d58b00be1a07f0c358b999b3', head.commit.id)
32         
33         assert_true(git.called)
34         assert_equal(git.call_args, (('for_each_ref', 'refs/heads'), {'sort': 'committerdate', 'format': '%(refname)%00%(objectname)'}))
35
36     @patch(Git, 'method_missing')  
37     def test_commits(self, git):
38         git.return_value = fixture('rev_list')
39         
40         commits = self.repo.commits('master', 10)
41     
42         c = commits[0]
43         assert_equal('4c8124ffcf4039d292442eeccabdeca5af5c5017', c.id)
44         assert_equal(["634396b2f541a9f2d58b00be1a07f0c358b999b3"], [p.id for p in c.parents])
45         assert_equal("672eca9b7f9e09c22dcb128c283e8c3c8d7697a4", c.tree.id)
46         assert_equal("Tom Preston-Werner", c.author.name)
47         assert_equal("tom@mojombo.com", c.author.email)
48         assert_equal(time.gmtime(1191999972), c.authored_date)
49         assert_equal("Tom Preston-Werner", c.committer.name)
50         assert_equal("tom@mojombo.com", c.committer.email)
51         assert_equal(time.gmtime(1191999972), c.committed_date)
52         assert_equal("implement Grit#heads", c.message)
53         
54         c = commits[1]
55         assert_equal([], c.parents)
56         
57         c = commits[2]
58         assert_equal(["6e64c55896aabb9a7d8e9f8f296f426d21a78c2c", "7f874954efb9ba35210445be456c74e037ba6af2"], map(lambda p: p.id, c.parents))
59         assert_equal("Merge branch 'site'", c.message)
60
61         assert_true(git.called)
62         assert_equal(git.call_args, (('rev_list', 'master'), {'skip': 0, 'pretty': 'raw', 'max_count': 10}))
63
64     @patch(Git, 'method_missing')
65     def test_commit_count(self, git):
66         git.return_value = fixture('rev_list_count')
67         
68         assert_equal(655, self.repo.commit_count('master'))
69         
70         assert_true(git.called)
71         assert_equal(git.call_args, (('rev_list', 'master'), {}))
72   
73     @patch(Git, 'method_missing')  
74     def test_commit(self, git):
75         git.return_value = fixture('rev_list_single')
76         
77         commit = self.repo.commit('4c8124ffcf4039d292442eeccabdeca5af5c5017')
78         
79         assert_equal("4c8124ffcf4039d292442eeccabdeca5af5c5017", commit.id)
80         
81         assert_true(git.called)
82         assert_equal(git.call_args, (('rev_list', '4c8124ffcf4039d292442eeccabdeca5af5c5017'), {'pretty': 'raw', 'max_count': 1}))
83   
84     @patch(Git, 'method_missing')
85     def test_tree(self, git):
86         git.return_value = fixture('ls_tree_a')
87         
88         tree = self.repo.tree('master')
89         
90         assert_equal(4, len([c for c in tree.contents if isinstance(c, Blob)]))
91         assert_equal(3, len([c for c in tree.contents if isinstance(c, Tree)]))
92         
93         assert_true(git.called)
94         assert_equal(git.call_args, (('ls_tree', 'master'), {}))
95
96     @patch(Git, 'method_missing')
97     def test_blob(self, git):
98         git.return_value = fixture('cat_file_blob')
99         
100         blob = self.repo.blob("abc")
101         assert_equal("Hello world", blob.data)
102         
103         assert_true(git.called)
104         assert_equal(git.call_args, (('cat_file', 'abc'), {'p': True}))
105
106     @patch(Repo, '__init__')
107     @patch(Git, 'method_missing')
108     def test_init_bare(self, repo, git):
109         git.return_value = True
110         
111         Repo.init_bare("~/foo/bar.git")
112         
113         assert_true(git.called)
114         assert_equal(git.call_args, (('init',), {}))
115         assert_true(repo.called)
116         assert_equal(repo.call_args, (('~/foo/bar.git',), {}))
117
118     @patch(Repo, '__init__')
119     @patch(Git, 'method_missing')
120     def test_init_bare_with_options(self, repo, git):
121         git.return_value = True
122         
123         Repo.init_bare("~/foo/bar.git", **{'template': "/baz/sweet"})
124
125         assert_true(git.called)
126         assert_equal(git.call_args, (('init',), {'template': '/baz/sweet'}))
127         assert_true(repo.called)
128         assert_equal(repo.call_args, (('~/foo/bar.git',), {}))
129
130     @patch(Repo, '__init__')
131     @patch(Git, 'method_missing')
132     def test_fork_bare(self, repo, git):
133         git.return_value = None
134         
135         self.repo.fork_bare("~/foo/bar.git")
136         
137         assert_true(git.called)
138         assert_equal(git.call_args, (('clone', '%s/.git' % absolute_project_path(), '~/foo/bar.git'), {'bare': True}))
139         assert_true(repo.called)
140
141     @patch(Repo, '__init__')
142     @patch(Git, 'method_missing')
143     def test_fork_bare_with_options(self, repo, git):
144         git.return_value = None
145         
146         self.repo.fork_bare("~/foo/bar.git", **{'template': '/awesome'})
147         
148         assert_true(git.called)
149         assert_equal(git.call_args, (('clone', '%s/.git' % absolute_project_path(), '~/foo/bar.git'), 
150                                       {'bare': True, 'template': '/awesome'}))
151         assert_true(repo.called)
152
153     @patch(Git, 'method_missing')
154     def test_diff(self, git):
155         self.repo.diff('master^', 'master')
156         
157         assert_true(git.called)
158         assert_equal(git.call_args, (('diff', 'master^', 'master', '--'), {}))
159
160         self.repo.diff('master^', 'master', 'foo/bar')
161
162         assert_true(git.called)
163         assert_equal(git.call_args, (('diff', 'master^', 'master', '--', 'foo/bar'), {}))
164         
165         self.repo.diff('master^', 'master', 'foo/bar', 'foo/baz')
166         
167         assert_true(git.called)
168         assert_equal(git.call_args, (('diff', 'master^', 'master', '--', 'foo/bar', 'foo/baz'), {}))
169
170     @patch(Git, 'method_missing')
171     def test_diff(self, git):
172         git.return_value = fixture('diff_p')
173         
174         diffs = self.repo.commit_diff('master')
175         assert_equal(15, len(diffs))
176         assert_true(git.called)
177
178     def test_archive_tar(self):
179         self.repo.archive_tar
180   
181     def test_archive_tar_gz(self):
182         self.repo.archive_tar_gz
183
184     @patch('git_python.utils', 'touch')
185     def test_enable_daemon_serve(self, touch):
186         self.repo.enable_daemon_serve
187
188     def test_disable_daemon_serve(self):
189         self.repo.disable_daemon_serve  
190   
191     # @patch(os.path, 'exists')
192     # @patch('__builtin__', 'open')
193     # def test_alternates_with_two_alternates(self, exists, read):
194     #     # File.expects(:exist?).with("#{absolute_project_path}/.git/objects/info/alternates").returns(true)
195     #     # File.expects(:read).returns("/path/to/repo1/.git/objects\n/path/to/repo2.git/objects\n")        
196     #     exists.return_value = True
197     #     read.return_value = ("/path/to/repo1/.git/objects\n/path/to/repo2.git/objects\n")
198     #     
199     #     assert_equal(["/path/to/repo1/.git/objects", "/path/to/repo2.git/objects"], self.repo.alternates)
200     #     
201     #     assert_true(exists.called)
202     #     assert_true(read.called)
203     # 
204     @patch(os.path, 'exists')
205     def test_alternates_no_file(self, os):
206         os.return_value = False
207         assert_equal([], self.repo.alternates)
208         
209         assert_true(os.called)
210         
211     # @patch(os.path, 'exists')
212     # def test_alternates_setter_ok(self, os):
213     #     os.return_value = True
214     #     alts = ['/path/to/repo.git/objects', '/path/to/repo2.git/objects']
215     #     
216     #     # File.any_instance.expects(:write).with(alts.join("\n"))
217     #     
218     #     self.repo.alternates = alts
219     #     
220     #     assert_true(os.called)
221     #     # assert_equal(os.call_args, ((alts,), {}))
222     #     # for alt in alts:
223     #         
224     # @patch(os.path, 'exists')
225     # @raises(NoSuchPathError)
226     # def test_alternates_setter_bad(self, os):
227     #     os.return_value = False
228     #     
229     #     alts = ['/path/to/repo.git/objects']
230     #     # File.any_instance.expects(:write).never
231     #     self.repo.alternates = alts
232     #     
233     #     for alt in alts:
234     #         assert_true(os.called)
235     #         assert_equal(os.call_args, (alt, {}))
236
237     @patch(os, 'remove')
238     def test_alternates_setter_empty(self, os):
239         self.repo.alternates = []
240         assert_true(os.called)
241
242     def test_repr(self):
243         assert_equal('<GitPython.Repo "%s/.git">' % os.path.abspath(GIT_REPO), repr(self.repo))
244
245     @patch(Git, 'method_missing')
246     def test_log(self, git):
247         git.return_value = fixture('rev_list')
248         assert_equal('4c8124ffcf4039d292442eeccabdeca5af5c5017', self.repo.log()[0].id)
249         assert_equal('ab25fd8483882c3bda8a458ad2965d2248654335', self.repo.log()[-1].id)
250         assert_true(git.called)
251         assert_equal(git.call_count, 2)
252         assert_equal(git.call_args, (('log', 'master'), {'pretty': 'raw'}))
253
254     @patch(Git, 'method_missing')
255     def test_log_with_path_and_options(self, git):
256         git.return_value = fixture('rev_list')
257         self.repo.log('master', 'file.rb', **{'max_count': 1})
258         assert_true(git.called)
259         assert_equal(git.call_args, (('log', 'master', '--', 'file.rb'), {'pretty': 'raw', 'max_count': 1}))
260
261     # @patch(Git, 'method_missing')
262     # @patch(Git, 'method_missing')
263     # def test_commit_deltas_from_nothing_new(self, gitb, gita):
264     #     gitb.return_value = fixture("rev_list_delta_b")
265     #     gita.return_value = fixture("rev_list_delta_a")
266     #     other_repo = Repo(GIT_REPO)
267     #     # self.repo.git.expects(:rev_list).with({}, "master").returns(fixture("rev_list_delta_b"))
268     #     # other_repo.git.expects(:rev_list).with({}, "master").returns(fixture("rev_list_delta_a"))
269     #     
270     #     delta_commits = self.repo.commit_deltas_from(other_repo)
271     #     assert_equal(0, len(delta_commits))
272     #     assert_true(gitb.called)
273     #     assert_equal(gitb.call_args, (('rev_list', 'master'), {}))
274     #     assert_true(gita.called)
275     #     assert_equal(gita.call_args, (('rev_list', 'master'), {}))
276     #   
277     # def test_commit_deltas_from_when_other_has_new(self):
278     #     other_repo = Repo(GIT_REPO)
279     #     # self.repo.git.expects(:rev_list).with({}, "master").returns(fixture("rev_list_delta_a"))
280     #     # other_repo.git.expects(:rev_list).with({}, "master").returns(fixture("rev_list_delta_b"))
281     #     # for ref in ['4c8124ffcf4039d292442eeccabdeca5af5c5017',
282     #     #             '634396b2f541a9f2d58b00be1a07f0c358b999b3',
283     #     #             'ab25fd8483882c3bda8a458ad2965d2248654335']:
284     #     #     Commit.expects(:find_all).with(other_repo, ref, :max_count => 1).returns([stub()])
285     #     delta_commits = self.repo.commit_deltas_from(other_repo)
286     #     assert_equal(3, len(delta_commits))