1
# Copyright (C) 2010-2018 Jelmer Vernooij <jelmer@jelmer.uk>
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Test the smart client."""
19
from __future__ import absolute_import
24
from ....controldir import ControlDir
25
from ....errors import (
31
from ....tests import (
33
TestCaseWithTransport,
35
from ....tests.features import ExecutableFeature
37
from ..mapping import default_mapping
38
from ..remote import (
41
RemoteGitBranchFormat,
44
from dulwich import porcelain
45
from dulwich.repo import Repo as GitRepo
48
class SplitUrlTests(TestCase):
50
def test_simple(self):
51
self.assertEquals(("foo", None, None, "/bar"),
52
split_git_url("git://foo/bar"))
55
self.assertEquals(("foo", 343, None, "/bar"),
56
split_git_url("git://foo:343/bar"))
58
def test_username(self):
59
self.assertEquals(("foo", None, "la", "/bar"),
60
split_git_url("git://la@foo/bar"))
62
def test_nopath(self):
63
self.assertEquals(("foo", None, None, "/"),
64
split_git_url("git://foo/"))
66
def test_slashpath(self):
67
self.assertEquals(("foo", None, None, "//bar"),
68
split_git_url("git://foo//bar"))
70
def test_homedir(self):
71
self.assertEquals(("foo", None, None, "~bar"),
72
split_git_url("git://foo/~bar"))
75
class ParseGitErrorTests(TestCase):
77
def test_unknown(self):
78
e = parse_git_error("url", "foo")
79
self.assertIsInstance(e, BzrError)
81
def test_notbrancherror(self):
82
e = parse_git_error("url", "\n Could not find Repository foo/bar")
83
self.assertIsInstance(e, NotBranchError)
86
class TestRemoteGitBranchFormat(TestCase):
89
super(TestRemoteGitBranchFormat, self).setUp()
90
self.format = RemoteGitBranchFormat()
92
def test_get_format_description(self):
93
self.assertEquals("Remote Git Branch", self.format.get_format_description())
95
def test_get_network_name(self):
96
self.assertEquals("git", self.format.network_name())
98
def test_supports_tags(self):
99
self.assertTrue(self.format.supports_tags())
102
class FetchFromRemoteTestBase(object):
104
_test_needs_features = [ExecutableFeature('git')]
109
TestCaseWithTransport.setUp(self)
110
self.remote_real = GitRepo.init('remote', mkdir=True)
111
self.remote_url = 'git://%s/' % os.path.abspath(self.remote_real.path)
112
self.permit_url(self.remote_url)
114
def test_sprout_simple(self):
115
self.remote_real.do_commit(
117
committer='committer <committer@example.com>',
118
author='author <author@example.com>')
120
remote = ControlDir.open(self.remote_url)
121
self.make_controldir('local', format=self._to_format)
122
local = remote.sprout('local')
124
default_mapping.revision_id_foreign_to_bzr(self.remote_real.head()),
125
local.open_branch().last_revision())
127
def test_sprout_with_tags(self):
128
c1 = self.remote_real.do_commit(
130
committer='committer <committer@example.com>',
131
author='author <author@example.com>')
132
c2 = self.remote_real.do_commit(
133
message='another commit',
134
committer='committer <committer@example.com>',
135
author='author <author@example.com>',
136
ref='refs/tags/another')
137
self.remote_real.refs['refs/tags/blah'] = self.remote_real.head()
139
remote = ControlDir.open(self.remote_url)
140
self.make_controldir('local', format=self._to_format)
141
local = remote.sprout('local')
142
local_branch = local.open_branch()
144
default_mapping.revision_id_foreign_to_bzr(c1),
145
local_branch.last_revision())
147
{'blah': local_branch.last_revision(),
148
'another': default_mapping.revision_id_foreign_to_bzr(c2)},
149
local_branch.tags.get_tag_dict())
151
def test_sprout_with_annotated_tag(self):
152
c1 = self.remote_real.do_commit(
154
committer='committer <committer@example.com>',
155
author='author <author@example.com>')
156
c2 = self.remote_real.do_commit(
157
message='another commit',
158
committer='committer <committer@example.com>',
159
author='author <author@example.com>',
160
ref='refs/heads/another')
161
porcelain.tag_create(
164
author='author <author@example.com>',
166
tag_time=int(time.time()),
169
message="Annotated tag")
171
remote = ControlDir.open(self.remote_url)
172
self.make_controldir('local', format=self._to_format)
173
local = remote.sprout('local', revision_id=default_mapping.revision_id_foreign_to_bzr(c1))
174
local_branch = local.open_branch()
176
default_mapping.revision_id_foreign_to_bzr(c1),
177
local_branch.last_revision())
179
{'blah': default_mapping.revision_id_foreign_to_bzr(c2)},
180
local_branch.tags.get_tag_dict())
183
class FetchFromRemoteToBzrTests(FetchFromRemoteTestBase,TestCaseWithTransport):
188
class FetchFromRemoteToGitTests(FetchFromRemoteTestBase,TestCaseWithTransport):
193
class PushToRemoteBase(object):
195
_test_needs_features = [ExecutableFeature('git')]
200
TestCaseWithTransport.setUp(self)
201
self.remote_real = GitRepo.init('remote', mkdir=True)
202
self.remote_url = 'git://%s/' % os.path.abspath(self.remote_real.path)
203
self.permit_url(self.remote_url)
206
c1 = self.remote_real.do_commit(
208
committer='committer <committer@example.com>',
209
author='author <author@example.com>')
211
remote = ControlDir.open(self.remote_url)
212
self.make_controldir('local', format=self._from_format)
213
local = remote.sprout('local')
214
self.build_tree(['local/blah'])
215
wt = local.open_workingtree()
217
revid = wt.commit('blah')
218
wt.branch.tags.set_tag('sometag', revid)
219
wt.branch.get_config_stack().set('branch.fetch_tags', True)
221
if self._from_format == 'git':
222
wt.branch.push(remote.create_branch('newbranch'))
224
wt.branch.push(remote.create_branch('newbranch'), lossy=True)
227
{'refs/heads/master': self.remote_real.head(),
228
'HEAD': self.remote_real.head(),
229
'refs/heads/newbranch': self.remote_real.refs['refs/heads/newbranch'],
230
'refs/tags/sometag': self.remote_real.refs['refs/heads/newbranch'],
232
self.remote_real.get_refs())
235
class PushToRemoteFromBzrTests(PushToRemoteBase,TestCaseWithTransport):
240
class PushToRemoteFromGitTests(PushToRemoteBase,TestCaseWithTransport):
245
class RemoteControlDirTests(TestCaseWithTransport):
247
_test_needs_features = [ExecutableFeature('git')]
250
TestCaseWithTransport.setUp(self)
251
self.remote_real = GitRepo.init('remote', mkdir=True)
252
self.remote_url = 'git://%s/' % os.path.abspath(self.remote_real.path)
253
self.permit_url(self.remote_url)
255
def test_remove_branch(self):
256
c1 = self.remote_real.do_commit(
258
committer='committer <committer@example.com>',
259
author='author <author@example.com>')
260
c2 = self.remote_real.do_commit(
261
message='another commit',
262
committer='committer <committer@example.com>',
263
author='author <author@example.com>',
264
ref='refs/heads/blah')
266
remote = ControlDir.open(self.remote_url)
267
remote.destroy_branch(name='blah')
269
self.remote_real.get_refs(),
270
{'refs/heads/master': self.remote_real.head(),
271
'HEAD': self.remote_real.head(),
274
def test_list_branches(self):
275
c1 = self.remote_real.do_commit(
277
committer='committer <committer@example.com>',
278
author='author <author@example.com>')
279
c2 = self.remote_real.do_commit(
280
message='another commit',
281
committer='committer <committer@example.com>',
282
author='author <author@example.com>',
283
ref='refs/heads/blah')
285
remote = ControlDir.open(self.remote_url)
287
['master', 'blah', 'master'],
288
[b.name for b in remote.list_branches()])
290
def test_get_branches(self):
291
c1 = self.remote_real.do_commit(
293
committer='committer <committer@example.com>',
294
author='author <author@example.com>')
295
c2 = self.remote_real.do_commit(
296
message='another commit',
297
committer='committer <committer@example.com>',
298
author='author <author@example.com>',
299
ref='refs/heads/blah')
301
remote = ControlDir.open(self.remote_url)
303
{'': 'master', 'blah': 'blah', 'master': 'master'},
304
{n: b.name for (n, b) in remote.get_branches().items()})
306
def test_remove_tag(self):
307
c1 = self.remote_real.do_commit(
309
committer='committer <committer@example.com>',
310
author='author <author@example.com>')
311
c2 = self.remote_real.do_commit(
312
message='another commit',
313
committer='committer <committer@example.com>',
314
author='author <author@example.com>',
315
ref='refs/tags/blah')
317
remote = ControlDir.open(self.remote_url)
318
remote_branch = remote.open_branch()
319
remote_branch.tags.delete_tag('blah')
320
self.assertRaises(NoSuchTag, remote_branch.tags.delete_tag, 'blah')
322
self.remote_real.get_refs(),
323
{'refs/heads/master': self.remote_real.head(),
324
'HEAD': self.remote_real.head(),
327
def test_set_tag(self):
328
c1 = self.remote_real.do_commit(
330
committer='committer <committer@example.com>',
331
author='author <author@example.com>')
332
c2 = self.remote_real.do_commit(
333
message='another commit',
334
committer='committer <committer@example.com>',
335
author='author <author@example.com>')
337
remote = ControlDir.open(self.remote_url)
338
remote.open_branch().tags.set_tag(
339
'blah', default_mapping.revision_id_foreign_to_bzr(c1))
341
self.remote_real.get_refs(),
342
{'refs/heads/master': self.remote_real.head(),
343
'refs/tags/blah': c1,
344
'HEAD': self.remote_real.head(),
347
def test_annotated_tag(self):
348
c1 = self.remote_real.do_commit(
350
committer='committer <committer@example.com>',
351
author='author <author@example.com>')
352
c2 = self.remote_real.do_commit(
353
message='another commit',
354
committer='committer <committer@example.com>',
355
author='author <author@example.com>')
357
porcelain.tag_create(
360
author='author <author@example.com>',
362
tag_time=int(time.time()),
365
message="Annotated tag")
367
remote = ControlDir.open(self.remote_url)
368
remote_branch = remote.open_branch()
370
'blah': default_mapping.revision_id_foreign_to_bzr(c2)},
371
remote_branch.tags.get_tag_dict())
373
def tetst_get_branch_reference(self):
374
c1 = self.remote_real.do_commit(
376
committer='committer <committer@example.com>',
377
author='author <author@example.com>')
378
c2 = self.remote_real.do_commit(
379
message='another commit',
380
committer='committer <committer@example.com>',
381
author='author <author@example.com>')
383
remote = ControlDir.open(self.remote_url)
384
self.assertEqual('refs/heads/master', remote.get_branch_reference(''))
385
self.assertEqual(None, remote.get_branch_reference('master'))