/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 breezy/git/tests/test_remote.py

  • Committer: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2018-09-13 12:50:28 UTC
  • mfrom: (7096.2.2 empty-port)
  • Revision ID: breezy.the.bot@gmail.com-20180913125028-mja5gz8xsams9iey
Allow port to be empty when parsing URLs.

Merged from https://code.launchpad.net/~jelmer/brz/empty-port/+merge/354640

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