/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to tests/test_remote.py

  • Committer: Jelmer Vernooij
  • Date: 2018-04-02 14:59:43 UTC
  • mto: (0.200.1913 work)
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jelmer@jelmer.uk-20180402145943-s5jmpbvvf1x42pao
Just don't touch the URL if it's already a valid URL.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2010-2018 Jelmer Vernooij <jelmer@jelmer.uk>
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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
 
16
 
 
17
"""Test the smart client."""
 
18
 
 
19
from __future__ import absolute_import
 
20
 
 
21
from StringIO import StringIO
 
22
 
 
23
import os
 
24
import time
 
25
 
 
26
from ....controldir import ControlDir
 
27
from ....errors import (
 
28
    BzrError,
 
29
    DivergedBranches,
 
30
    NotBranchError,
 
31
    NoSuchTag,
 
32
    )
 
33
 
 
34
from ....tests import (
 
35
    TestCase,
 
36
    TestCaseWithTransport,
 
37
    )
 
38
from ....tests.features import ExecutableFeature
 
39
 
 
40
from ..mapping import default_mapping
 
41
from ..remote import (
 
42
    split_git_url,
 
43
    parse_git_error,
 
44
    RemoteGitBranchFormat,
 
45
    )
 
46
 
 
47
from dulwich import porcelain
 
48
from dulwich.repo import Repo as GitRepo
 
49
 
 
50
 
 
51
class SplitUrlTests(TestCase):
 
52
 
 
53
    def test_simple(self):
 
54
        self.assertEquals(("foo", None, None, "/bar"),
 
55
            split_git_url("git://foo/bar"))
 
56
 
 
57
    def test_port(self):
 
58
        self.assertEquals(("foo", 343, None, "/bar"),
 
59
            split_git_url("git://foo:343/bar"))
 
60
 
 
61
    def test_username(self):
 
62
        self.assertEquals(("foo", None, "la", "/bar"),
 
63
            split_git_url("git://la@foo/bar"))
 
64
 
 
65
    def test_nopath(self):
 
66
        self.assertEquals(("foo", None, None, "/"),
 
67
            split_git_url("git://foo/"))
 
68
 
 
69
    def test_slashpath(self):
 
70
        self.assertEquals(("foo", None, None, "//bar"),
 
71
            split_git_url("git://foo//bar"))
 
72
 
 
73
    def test_homedir(self):
 
74
        self.assertEquals(("foo", None, None, "~bar"),
 
75
            split_git_url("git://foo/~bar"))
 
76
 
 
77
 
 
78
class ParseGitErrorTests(TestCase):
 
79
 
 
80
    def test_unknown(self):
 
81
        e = parse_git_error("url", "foo")
 
82
        self.assertIsInstance(e, BzrError)
 
83
 
 
84
    def test_notbrancherror(self):
 
85
        e = parse_git_error("url", "\n Could not find Repository foo/bar")
 
86
        self.assertIsInstance(e, NotBranchError)
 
87
 
 
88
 
 
89
class TestRemoteGitBranchFormat(TestCase):
 
90
 
 
91
    def setUp(self):
 
92
        super(TestRemoteGitBranchFormat, self).setUp()
 
93
        self.format = RemoteGitBranchFormat()
 
94
 
 
95
    def test_get_format_description(self):
 
96
        self.assertEquals("Remote Git Branch", self.format.get_format_description())
 
97
 
 
98
    def test_get_network_name(self):
 
99
        self.assertEquals("git", self.format.network_name())
 
100
 
 
101
    def test_supports_tags(self):
 
102
        self.assertTrue(self.format.supports_tags())
 
103
 
 
104
 
 
105
class FetchFromRemoteTestBase(object):
 
106
 
 
107
    _test_needs_features = [ExecutableFeature('git')]
 
108
 
 
109
    _to_format = None
 
110
 
 
111
    def setUp(self):
 
112
        TestCaseWithTransport.setUp(self)
 
113
        self.remote_real = GitRepo.init('remote', mkdir=True)
 
114
        self.remote_url = 'git://%s/' % os.path.abspath(self.remote_real.path)
 
115
        self.permit_url(self.remote_url)
 
116
 
 
117
    def test_sprout_simple(self):
 
118
        self.remote_real.do_commit(
 
119
                message='message',
 
120
                committer='committer <committer@example.com>',
 
121
                author='author <author@example.com>')
 
122
 
 
123
        remote = ControlDir.open(self.remote_url)
 
124
        self.make_controldir('local', format=self._to_format)
 
125
        local = remote.sprout('local')
 
126
        self.assertEqual(
 
127
                default_mapping.revision_id_foreign_to_bzr(self.remote_real.head()),
 
128
                local.open_branch().last_revision())
 
129
 
 
130
    def test_sprout_with_tags(self):
 
131
        c1 = self.remote_real.do_commit(
 
132
                message='message',
 
133
                committer='committer <committer@example.com>',
 
134
                author='author <author@example.com>')
 
135
        c2 = self.remote_real.do_commit(
 
136
                message='another commit',
 
137
                committer='committer <committer@example.com>',
 
138
                author='author <author@example.com>',
 
139
                ref='refs/tags/another')
 
140
        self.remote_real.refs['refs/tags/blah'] = self.remote_real.head()
 
141
 
 
142
        remote = ControlDir.open(self.remote_url)
 
143
        self.make_controldir('local', format=self._to_format)
 
144
        local = remote.sprout('local')
 
145
        local_branch = local.open_branch()
 
146
        self.assertEqual(
 
147
                default_mapping.revision_id_foreign_to_bzr(c1),
 
148
                local_branch.last_revision())
 
149
        self.assertEqual(
 
150
                {'blah': local_branch.last_revision(),
 
151
                 'another': default_mapping.revision_id_foreign_to_bzr(c2)},
 
152
                local_branch.tags.get_tag_dict())
 
153
 
 
154
    def test_sprout_with_annotated_tag(self):
 
155
        c1 = self.remote_real.do_commit(
 
156
                message='message',
 
157
                committer='committer <committer@example.com>',
 
158
                author='author <author@example.com>')
 
159
        c2 = self.remote_real.do_commit(
 
160
                message='another commit',
 
161
                committer='committer <committer@example.com>',
 
162
                author='author <author@example.com>',
 
163
                ref='refs/heads/another')
 
164
        porcelain.tag_create(
 
165
                self.remote_real,
 
166
                tag="blah",
 
167
                author='author <author@example.com>',
 
168
                objectish=c2,
 
169
                tag_time=int(time.time()),
 
170
                tag_timezone=0,
 
171
                annotated=True,
 
172
                message="Annotated tag")
 
173
 
 
174
        remote = ControlDir.open(self.remote_url)
 
175
        self.make_controldir('local', format=self._to_format)
 
176
        local = remote.sprout('local', revision_id=default_mapping.revision_id_foreign_to_bzr(c1))
 
177
        local_branch = local.open_branch()
 
178
        self.assertEqual(
 
179
                default_mapping.revision_id_foreign_to_bzr(c1),
 
180
                local_branch.last_revision())
 
181
        self.assertEqual(
 
182
                {'blah': default_mapping.revision_id_foreign_to_bzr(c2)},
 
183
                local_branch.tags.get_tag_dict())
 
184
 
 
185
 
 
186
class FetchFromRemoteToBzrTests(FetchFromRemoteTestBase,TestCaseWithTransport):
 
187
 
 
188
    _to_format = '2a'
 
189
 
 
190
 
 
191
class FetchFromRemoteToGitTests(FetchFromRemoteTestBase,TestCaseWithTransport):
 
192
 
 
193
    _to_format = 'git'
 
194
 
 
195
 
 
196
class PushToRemoteBase(object):
 
197
 
 
198
    _test_needs_features = [ExecutableFeature('git')]
 
199
 
 
200
    _from_format = None
 
201
 
 
202
    def setUp(self):
 
203
        TestCaseWithTransport.setUp(self)
 
204
        self.remote_real = GitRepo.init('remote', mkdir=True)
 
205
        self.remote_url = 'git://%s/' % os.path.abspath(self.remote_real.path)
 
206
        self.permit_url(self.remote_url)
 
207
 
 
208
    def test_push_branch_new(self):
 
209
        remote = ControlDir.open(self.remote_url)
 
210
        wt = self.make_branch_and_tree('local', format=self._from_format)
 
211
        self.build_tree(['local/blah'])
 
212
        wt.add(['blah'])
 
213
        revid = wt.commit('blah')
 
214
 
 
215
        if self._from_format == 'git':
 
216
            result = remote.push_branch(wt.branch, name='newbranch')
 
217
        else:
 
218
            result = remote.push_branch(wt.branch, lossy=True, name='newbranch')
 
219
 
 
220
        self.assertEqual(0, result.old_revno)
 
221
        if self._from_format == 'git':
 
222
            self.assertEqual(1, result.new_revno)
 
223
        else:
 
224
            self.assertIs(None, result.new_revno)
 
225
 
 
226
        result.report(StringIO())
 
227
 
 
228
        self.assertEqual(
 
229
                {'refs/heads/newbranch': self.remote_real.refs['refs/heads/newbranch'],
 
230
                },
 
231
                self.remote_real.get_refs())
 
232
 
 
233
    def test_push(self):
 
234
        c1 = self.remote_real.do_commit(
 
235
                message='message',
 
236
                committer='committer <committer@example.com>',
 
237
                author='author <author@example.com>')
 
238
 
 
239
        remote = ControlDir.open(self.remote_url)
 
240
        self.make_controldir('local', format=self._from_format)
 
241
        local = remote.sprout('local')
 
242
        self.build_tree(['local/blah'])
 
243
        wt = local.open_workingtree()
 
244
        wt.add(['blah'])
 
245
        revid = wt.commit('blah')
 
246
        wt.branch.tags.set_tag('sometag', revid)
 
247
        wt.branch.get_config_stack().set('branch.fetch_tags', True)
 
248
 
 
249
        if self._from_format == 'git':
 
250
            result = wt.branch.push(remote.create_branch('newbranch'))
 
251
        else:
 
252
            result = wt.branch.push(remote.create_branch('newbranch'), lossy=True)
 
253
 
 
254
        self.assertEqual(0, result.old_revno)
 
255
        self.assertEqual(2, result.new_revno)
 
256
 
 
257
        result.report(StringIO())
 
258
 
 
259
        self.assertEqual(
 
260
                {'refs/heads/master': self.remote_real.head(),
 
261
                 'HEAD': self.remote_real.head(),
 
262
                 'refs/heads/newbranch': self.remote_real.refs['refs/heads/newbranch'],
 
263
                 'refs/tags/sometag': self.remote_real.refs['refs/heads/newbranch'],
 
264
                },
 
265
                self.remote_real.get_refs())
 
266
 
 
267
    def test_push_diverged(self):
 
268
        c1 = self.remote_real.do_commit(
 
269
                message='message',
 
270
                committer='committer <committer@example.com>',
 
271
                author='author <author@example.com>',
 
272
                ref='refs/heads/newbranch')
 
273
 
 
274
        remote = ControlDir.open(self.remote_url)
 
275
        wt = self.make_branch_and_tree('local', format=self._from_format)
 
276
        self.build_tree(['local/blah'])
 
277
        wt.add(['blah'])
 
278
        revid = wt.commit('blah')
 
279
 
 
280
        newbranch = remote.open_branch('newbranch')
 
281
        if self._from_format == 'git':
 
282
            self.assertRaises(DivergedBranches, wt.branch.push, newbranch)
 
283
        else:
 
284
            self.assertRaises(DivergedBranches, wt.branch.push, newbranch, lossy=True)
 
285
 
 
286
        self.assertEqual(
 
287
                {'refs/heads/newbranch': c1 },
 
288
                self.remote_real.get_refs())
 
289
 
 
290
        if self._from_format == 'git':
 
291
            wt.branch.push(newbranch, overwrite=True)
 
292
        else:
 
293
            wt.branch.push(newbranch, lossy=True, overwrite=True)
 
294
 
 
295
        self.assertNotEqual(c1, self.remote_real.refs['refs/heads/newbranch'])
 
296
 
 
297
 
 
298
class PushToRemoteFromBzrTests(PushToRemoteBase,TestCaseWithTransport):
 
299
 
 
300
    _from_format = '2a'
 
301
 
 
302
 
 
303
class PushToRemoteFromGitTests(PushToRemoteBase,TestCaseWithTransport):
 
304
 
 
305
    _from_format = 'git'
 
306
 
 
307
 
 
308
class RemoteControlDirTests(TestCaseWithTransport):
 
309
 
 
310
    _test_needs_features = [ExecutableFeature('git')]
 
311
 
 
312
    def setUp(self):
 
313
        TestCaseWithTransport.setUp(self)
 
314
        self.remote_real = GitRepo.init('remote', mkdir=True)
 
315
        self.remote_url = 'git://%s/' % os.path.abspath(self.remote_real.path)
 
316
        self.permit_url(self.remote_url)
 
317
 
 
318
    def test_remove_branch(self):
 
319
        c1 = self.remote_real.do_commit(
 
320
                message='message',
 
321
                committer='committer <committer@example.com>',
 
322
                author='author <author@example.com>')
 
323
        c2 = self.remote_real.do_commit(
 
324
                message='another commit',
 
325
                committer='committer <committer@example.com>',
 
326
                author='author <author@example.com>',
 
327
                ref='refs/heads/blah')
 
328
 
 
329
        remote = ControlDir.open(self.remote_url)
 
330
        remote.destroy_branch(name='blah')
 
331
        self.assertEqual(
 
332
                self.remote_real.get_refs(),
 
333
                {'refs/heads/master': self.remote_real.head(),
 
334
                 'HEAD': self.remote_real.head(),
 
335
                })
 
336
 
 
337
    def test_list_branches(self):
 
338
        c1 = self.remote_real.do_commit(
 
339
                message='message',
 
340
                committer='committer <committer@example.com>',
 
341
                author='author <author@example.com>')
 
342
        c2 = self.remote_real.do_commit(
 
343
                message='another commit',
 
344
                committer='committer <committer@example.com>',
 
345
                author='author <author@example.com>',
 
346
                ref='refs/heads/blah')
 
347
 
 
348
        remote = ControlDir.open(self.remote_url)
 
349
        self.assertEqual(
 
350
                ['master', 'blah', 'master'],
 
351
                [b.name for b in remote.list_branches()])
 
352
 
 
353
    def test_get_branches(self):
 
354
        c1 = self.remote_real.do_commit(
 
355
                message='message',
 
356
                committer='committer <committer@example.com>',
 
357
                author='author <author@example.com>')
 
358
        c2 = self.remote_real.do_commit(
 
359
                message='another commit',
 
360
                committer='committer <committer@example.com>',
 
361
                author='author <author@example.com>',
 
362
                ref='refs/heads/blah')
 
363
 
 
364
        remote = ControlDir.open(self.remote_url)
 
365
        self.assertEqual(
 
366
                {'': 'master', 'blah': 'blah', 'master': 'master'},
 
367
                {n: b.name for (n, b) in remote.get_branches().items()})
 
368
 
 
369
    def test_remove_tag(self):
 
370
        c1 = self.remote_real.do_commit(
 
371
                message='message',
 
372
                committer='committer <committer@example.com>',
 
373
                author='author <author@example.com>')
 
374
        c2 = self.remote_real.do_commit(
 
375
                message='another commit',
 
376
                committer='committer <committer@example.com>',
 
377
                author='author <author@example.com>',
 
378
                ref='refs/tags/blah')
 
379
 
 
380
        remote = ControlDir.open(self.remote_url)
 
381
        remote_branch = remote.open_branch()
 
382
        remote_branch.tags.delete_tag('blah')
 
383
        self.assertRaises(NoSuchTag, remote_branch.tags.delete_tag, 'blah')
 
384
        self.assertEqual(
 
385
                self.remote_real.get_refs(),
 
386
                {'refs/heads/master': self.remote_real.head(),
 
387
                 'HEAD': self.remote_real.head(),
 
388
                })
 
389
 
 
390
    def test_set_tag(self):
 
391
        c1 = self.remote_real.do_commit(
 
392
                message='message',
 
393
                committer='committer <committer@example.com>',
 
394
                author='author <author@example.com>')
 
395
        c2 = self.remote_real.do_commit(
 
396
                message='another commit',
 
397
                committer='committer <committer@example.com>',
 
398
                author='author <author@example.com>')
 
399
 
 
400
        remote = ControlDir.open(self.remote_url)
 
401
        remote.open_branch().tags.set_tag(
 
402
            'blah', default_mapping.revision_id_foreign_to_bzr(c1))
 
403
        self.assertEqual(
 
404
                self.remote_real.get_refs(),
 
405
                {'refs/heads/master': self.remote_real.head(),
 
406
                 'refs/tags/blah': c1,
 
407
                 'HEAD': self.remote_real.head(),
 
408
                })
 
409
 
 
410
    def test_annotated_tag(self):
 
411
        c1 = self.remote_real.do_commit(
 
412
                message='message',
 
413
                committer='committer <committer@example.com>',
 
414
                author='author <author@example.com>')
 
415
        c2 = self.remote_real.do_commit(
 
416
                message='another commit',
 
417
                committer='committer <committer@example.com>',
 
418
                author='author <author@example.com>')
 
419
 
 
420
        porcelain.tag_create(
 
421
                self.remote_real,
 
422
                tag="blah",
 
423
                author='author <author@example.com>',
 
424
                objectish=c2,
 
425
                tag_time=int(time.time()),
 
426
                tag_timezone=0,
 
427
                annotated=True,
 
428
                message="Annotated tag")
 
429
 
 
430
        remote = ControlDir.open(self.remote_url)
 
431
        remote_branch = remote.open_branch()
 
432
        self.assertEqual({
 
433
            'blah': default_mapping.revision_id_foreign_to_bzr(c2)},
 
434
            remote_branch.tags.get_tag_dict())
 
435
 
 
436
    def tetst_get_branch_reference(self):
 
437
        c1 = self.remote_real.do_commit(
 
438
                message='message',
 
439
                committer='committer <committer@example.com>',
 
440
                author='author <author@example.com>')
 
441
        c2 = self.remote_real.do_commit(
 
442
                message='another commit',
 
443
                committer='committer <committer@example.com>',
 
444
                author='author <author@example.com>')
 
445
 
 
446
        remote = ControlDir.open(self.remote_url)
 
447
        self.assertEqual('refs/heads/master', remote.get_branch_reference(''))
 
448
        self.assertEqual(None, remote.get_branch_reference('master'))