/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: Jelmer Vernooij
  • Date: 2018-11-11 04:08:32 UTC
  • mto: (7143.16.20 even-more-cleanups)
  • mto: This revision was merged to the branch mainline in revision 7175.
  • Revision ID: jelmer@jelmer.uk-20181111040832-nsljjynzzwmznf3h
Run autopep8.

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