/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-03-31 19:47:40 UTC
  • mto: (0.200.1910 work)
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jelmer@jelmer.uk-20180331194740-q1lhux2ao2etsblk
Use same logic for interpreting progress reports everywhere.

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