~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/blackbox/test_branch.py

  • Committer: Martin Packman
  • Date: 2011-11-17 13:45:49 UTC
  • mto: This revision was merged to the branch mainline in revision 6271.
  • Revision ID: martin.packman@canonical.com-20111117134549-080e1fhtrzoicexg
Only assert FileExists path in test_transform directory clash tests to avoid stringification fallout

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006-2011 Canonical Ltd
 
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
 
 
18
"""Black-box tests for bzr branch."""
 
19
 
 
20
import os
 
21
 
 
22
from bzrlib import (
 
23
    branch,
 
24
    bzrdir,
 
25
    controldir,
 
26
    errors,
 
27
    revision as _mod_revision,
 
28
    )
 
29
from bzrlib.repofmt.knitrepo import RepositoryFormatKnit1
 
30
from bzrlib.tests import TestCaseWithTransport
 
31
from bzrlib.tests import (
 
32
    fixtures,
 
33
    test_server,
 
34
    )
 
35
from bzrlib.tests.features import (
 
36
    HardlinkFeature,
 
37
    )
 
38
from bzrlib.tests.blackbox import test_switch
 
39
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
 
40
from bzrlib.tests.script import run_script
 
41
from bzrlib.urlutils import local_path_to_url, strip_trailing_slash
 
42
from bzrlib.workingtree import WorkingTree
 
43
 
 
44
 
 
45
class TestBranch(TestCaseWithTransport):
 
46
 
 
47
    def example_branch(self, path='.'):
 
48
        tree = self.make_branch_and_tree(path)
 
49
        self.build_tree_contents([(path + '/hello', 'foo')])
 
50
        tree.add('hello')
 
51
        tree.commit(message='setup')
 
52
        self.build_tree_contents([(path + '/goodbye', 'baz')])
 
53
        tree.add('goodbye')
 
54
        tree.commit(message='setup')
 
55
 
 
56
    def test_branch(self):
 
57
        """Branch from one branch to another."""
 
58
        self.example_branch('a')
 
59
        self.run_bzr('branch a b')
 
60
        b = branch.Branch.open('b')
 
61
        self.run_bzr('branch a c -r 1')
 
62
        # previously was erroneously created by branching
 
63
        self.assertFalse(b._transport.has('branch-name'))
 
64
        b.bzrdir.open_workingtree().commit(message='foo', allow_pointless=True)
 
65
 
 
66
    def test_into_colocated(self):
 
67
        """Branch from a branch into a colocated branch."""
 
68
        self.example_branch('a')
 
69
        out, err = self.run_bzr(
 
70
            'init --format=development-colo file:b,branch=orig')
 
71
        self.assertEqual(
 
72
            """Created a standalone tree (format: development-colo)\n""",
 
73
            out)
 
74
        self.assertEqual('', err)
 
75
        out, err = self.run_bzr(
 
76
            'branch a file:b,branch=thiswasa')
 
77
        self.assertEqual('', out)
 
78
        self.assertEqual('Branched 2 revisions.\n', err)
 
79
        out, err = self.run_bzr('branches b')
 
80
        self.assertEqual(" orig\n thiswasa\n", out)
 
81
        self.assertEqual('', err)
 
82
        out,err = self.run_bzr('branch a file:b,branch=orig', retcode=3)
 
83
        self.assertEqual('', out)
 
84
        self.assertEqual('bzr: ERROR: Already a branch: "file:b,branch=orig".\n', err)
 
85
 
 
86
    def test_branch_broken_pack(self):
 
87
        """branching with a corrupted pack file."""
 
88
        self.example_branch('a')
 
89
        # add some corruption
 
90
        packs_dir = 'a/.bzr/repository/packs/'
 
91
        fname = packs_dir + os.listdir(packs_dir)[0]
 
92
        with open(fname, 'rb+') as f:
 
93
            # Start from the end of the file to avoid choosing a place bigger
 
94
            # than the file itself.
 
95
            f.seek(-5, os.SEEK_END)
 
96
            c = f.read(1)
 
97
            f.seek(-5, os.SEEK_END)
 
98
            # Make sure we inject a value different than the one we just read
 
99
            if c == '\xFF':
 
100
                corrupt = '\x00'
 
101
            else:
 
102
                corrupt = '\xFF'
 
103
            f.write(corrupt) # make sure we corrupt something
 
104
        self.run_bzr_error(['Corruption while decompressing repository file'], 
 
105
                            'branch a b', retcode=3)
 
106
 
 
107
    def test_branch_switch_no_branch(self):
 
108
        # No branch in the current directory:
 
109
        #  => new branch will be created, but switch fails
 
110
        self.example_branch('a')
 
111
        self.make_repository('current')
 
112
        self.run_bzr_error(['No WorkingTree exists for'],
 
113
            'branch --switch ../a ../b', working_dir='current')
 
114
        a = branch.Branch.open('a')
 
115
        b = branch.Branch.open('b')
 
116
        self.assertEqual(a.last_revision(), b.last_revision())
 
117
 
 
118
    def test_branch_switch_no_wt(self):
 
119
        # No working tree in the current directory:
 
120
        #  => new branch will be created, but switch fails and the current
 
121
        #     branch is unmodified
 
122
        self.example_branch('a')
 
123
        self.make_branch('current')
 
124
        self.run_bzr_error(['No WorkingTree exists for'],
 
125
            'branch --switch ../a ../b', working_dir='current')
 
126
        a = branch.Branch.open('a')
 
127
        b = branch.Branch.open('b')
 
128
        self.assertEqual(a.last_revision(), b.last_revision())
 
129
        work = branch.Branch.open('current')
 
130
        self.assertEqual(work.last_revision(), _mod_revision.NULL_REVISION)
 
131
 
 
132
    def test_branch_switch_no_checkout(self):
 
133
        # Standalone branch in the current directory:
 
134
        #  => new branch will be created, but switch fails and the current
 
135
        #     branch is unmodified
 
136
        self.example_branch('a')
 
137
        self.make_branch_and_tree('current')
 
138
        self.run_bzr_error(['Cannot switch a branch, only a checkout'],
 
139
            'branch --switch ../a ../b', working_dir='current')
 
140
        a = branch.Branch.open('a')
 
141
        b = branch.Branch.open('b')
 
142
        self.assertEqual(a.last_revision(), b.last_revision())
 
143
        work = branch.Branch.open('current')
 
144
        self.assertEqual(work.last_revision(), _mod_revision.NULL_REVISION)
 
145
 
 
146
    def test_branch_switch_checkout(self):
 
147
        # Checkout in the current directory:
 
148
        #  => new branch will be created and checkout bound to the new branch
 
149
        self.example_branch('a')
 
150
        self.run_bzr('checkout a current')
 
151
        out, err = self.run_bzr('branch --switch ../a ../b', working_dir='current')
 
152
        a = branch.Branch.open('a')
 
153
        b = branch.Branch.open('b')
 
154
        self.assertEqual(a.last_revision(), b.last_revision())
 
155
        work = WorkingTree.open('current')
 
156
        self.assertEndsWith(work.branch.get_bound_location(), '/b/')
 
157
        self.assertContainsRe(err, "Switched to branch: .*/b/")
 
158
 
 
159
    def test_branch_switch_lightweight_checkout(self):
 
160
        # Lightweight checkout in the current directory:
 
161
        #  => new branch will be created and lightweight checkout pointed to
 
162
        #     the new branch
 
163
        self.example_branch('a')
 
164
        self.run_bzr('checkout --lightweight a current')
 
165
        out, err = self.run_bzr('branch --switch ../a ../b', working_dir='current')
 
166
        a = branch.Branch.open('a')
 
167
        b = branch.Branch.open('b')
 
168
        self.assertEqual(a.last_revision(), b.last_revision())
 
169
        work = WorkingTree.open('current')
 
170
        self.assertEndsWith(work.branch.base, '/b/')
 
171
        self.assertContainsRe(err, "Switched to branch: .*/b/")
 
172
 
 
173
    def test_branch_only_copies_history(self):
 
174
        # Knit branches should only push the history for the current revision.
 
175
        format = bzrdir.BzrDirMetaFormat1()
 
176
        format.repository_format = RepositoryFormatKnit1()
 
177
        shared_repo = self.make_repository('repo', format=format, shared=True)
 
178
        shared_repo.set_make_working_trees(True)
 
179
 
 
180
        def make_shared_tree(path):
 
181
            shared_repo.bzrdir.root_transport.mkdir(path)
 
182
            controldir.ControlDir.create_branch_convenience('repo/' + path)
 
183
            return WorkingTree.open('repo/' + path)
 
184
        tree_a = make_shared_tree('a')
 
185
        self.build_tree(['repo/a/file'])
 
186
        tree_a.add('file')
 
187
        tree_a.commit('commit a-1', rev_id='a-1')
 
188
        f = open('repo/a/file', 'ab')
 
189
        f.write('more stuff\n')
 
190
        f.close()
 
191
        tree_a.commit('commit a-2', rev_id='a-2')
 
192
 
 
193
        tree_b = make_shared_tree('b')
 
194
        self.build_tree(['repo/b/file'])
 
195
        tree_b.add('file')
 
196
        tree_b.commit('commit b-1', rev_id='b-1')
 
197
 
 
198
        self.assertTrue(shared_repo.has_revision('a-1'))
 
199
        self.assertTrue(shared_repo.has_revision('a-2'))
 
200
        self.assertTrue(shared_repo.has_revision('b-1'))
 
201
 
 
202
        # Now that we have a repository with shared files, make sure
 
203
        # that things aren't copied out by a 'branch'
 
204
        self.run_bzr('branch repo/b branch-b')
 
205
        pushed_tree = WorkingTree.open('branch-b')
 
206
        pushed_repo = pushed_tree.branch.repository
 
207
        self.assertFalse(pushed_repo.has_revision('a-1'))
 
208
        self.assertFalse(pushed_repo.has_revision('a-2'))
 
209
        self.assertTrue(pushed_repo.has_revision('b-1'))
 
210
 
 
211
    def test_branch_hardlink(self):
 
212
        self.requireFeature(HardlinkFeature)
 
213
        source = self.make_branch_and_tree('source')
 
214
        self.build_tree(['source/file1'])
 
215
        source.add('file1')
 
216
        source.commit('added file')
 
217
        out, err = self.run_bzr(['branch', 'source', 'target', '--hardlink'])
 
218
        source_stat = os.stat('source/file1')
 
219
        target_stat = os.stat('target/file1')
 
220
        self.assertEqual(source_stat, target_stat)
 
221
 
 
222
    def test_branch_files_from(self):
 
223
        source = self.make_branch_and_tree('source')
 
224
        self.build_tree(['source/file1'])
 
225
        source.add('file1')
 
226
        source.commit('added file')
 
227
        out, err = self.run_bzr('branch source target --files-from source')
 
228
        self.assertPathExists('target/file1')
 
229
 
 
230
    def test_branch_files_from_hardlink(self):
 
231
        self.requireFeature(HardlinkFeature)
 
232
        source = self.make_branch_and_tree('source')
 
233
        self.build_tree(['source/file1'])
 
234
        source.add('file1')
 
235
        source.commit('added file')
 
236
        source.bzrdir.sprout('second')
 
237
        out, err = self.run_bzr('branch source target --files-from second'
 
238
                                ' --hardlink')
 
239
        source_stat = os.stat('source/file1')
 
240
        second_stat = os.stat('second/file1')
 
241
        target_stat = os.stat('target/file1')
 
242
        self.assertNotEqual(source_stat, target_stat)
 
243
        self.assertEqual(second_stat, target_stat)
 
244
 
 
245
    def test_branch_standalone(self):
 
246
        shared_repo = self.make_repository('repo', shared=True)
 
247
        self.example_branch('source')
 
248
        self.run_bzr('branch --standalone source repo/target')
 
249
        b = branch.Branch.open('repo/target')
 
250
        expected_repo_path = os.path.abspath('repo/target/.bzr/repository')
 
251
        self.assertEqual(strip_trailing_slash(b.repository.base),
 
252
            strip_trailing_slash(local_path_to_url(expected_repo_path)))
 
253
 
 
254
    def test_branch_no_tree(self):
 
255
        self.example_branch('source')
 
256
        self.run_bzr('branch --no-tree source target')
 
257
        self.assertPathDoesNotExist('target/hello')
 
258
        self.assertPathDoesNotExist('target/goodbye')
 
259
 
 
260
    def test_branch_into_existing_dir(self):
 
261
        self.example_branch('a')
 
262
        # existing dir with similar files but no .bzr dir
 
263
        self.build_tree_contents([('b/',)])
 
264
        self.build_tree_contents([('b/hello', 'bar')])  # different content
 
265
        self.build_tree_contents([('b/goodbye', 'baz')])# same content
 
266
        # fails without --use-existing-dir
 
267
        out,err = self.run_bzr('branch a b', retcode=3)
 
268
        self.assertEqual('', out)
 
269
        self.assertEqual('bzr: ERROR: Target directory "b" already exists.\n',
 
270
            err)
 
271
        # force operation
 
272
        self.run_bzr('branch a b --use-existing-dir')
 
273
        # check conflicts
 
274
        self.assertPathExists('b/hello.moved')
 
275
        self.assertPathDoesNotExist('b/godbye.moved')
 
276
        # we can't branch into branch
 
277
        out,err = self.run_bzr('branch a b --use-existing-dir', retcode=3)
 
278
        self.assertEqual('', out)
 
279
        self.assertEqual('bzr: ERROR: Already a branch: "b".\n', err)
 
280
 
 
281
    def test_branch_bind(self):
 
282
        self.example_branch('a')
 
283
        out, err = self.run_bzr('branch a b --bind')
 
284
        self.assertEndsWith(err, "New branch bound to a\n")
 
285
        b = branch.Branch.open('b')
 
286
        self.assertEndsWith(b.get_bound_location(), '/a/')
 
287
 
 
288
    def test_branch_with_post_branch_init_hook(self):
 
289
        calls = []
 
290
        branch.Branch.hooks.install_named_hook('post_branch_init',
 
291
            calls.append, None)
 
292
        self.assertLength(0, calls)
 
293
        self.example_branch('a')
 
294
        self.assertLength(1, calls)
 
295
        self.run_bzr('branch a b')
 
296
        self.assertLength(2, calls)
 
297
 
 
298
    def test_checkout_with_post_branch_init_hook(self):
 
299
        calls = []
 
300
        branch.Branch.hooks.install_named_hook('post_branch_init',
 
301
            calls.append, None)
 
302
        self.assertLength(0, calls)
 
303
        self.example_branch('a')
 
304
        self.assertLength(1, calls)
 
305
        self.run_bzr('checkout a b')
 
306
        self.assertLength(2, calls)
 
307
 
 
308
    def test_lightweight_checkout_with_post_branch_init_hook(self):
 
309
        calls = []
 
310
        branch.Branch.hooks.install_named_hook('post_branch_init',
 
311
            calls.append, None)
 
312
        self.assertLength(0, calls)
 
313
        self.example_branch('a')
 
314
        self.assertLength(1, calls)
 
315
        self.run_bzr('checkout --lightweight a b')
 
316
        self.assertLength(2, calls)
 
317
 
 
318
    def test_branch_fetches_all_tags(self):
 
319
        builder = self.make_branch_builder('source')
 
320
        source = fixtures.build_branch_with_non_ancestral_rev(builder)
 
321
        source.tags.set_tag('tag-a', 'rev-2')
 
322
        source.get_config().set_user_option('branch.fetch_tags', 'True')
 
323
        # Now source has a tag not in its ancestry.  Make a branch from it.
 
324
        self.run_bzr('branch source new-branch')
 
325
        new_branch = branch.Branch.open('new-branch')
 
326
        # The tag is present, and so is its revision.
 
327
        self.assertEqual('rev-2', new_branch.tags.lookup_tag('tag-a'))
 
328
        new_branch.repository.get_revision('rev-2')
 
329
 
 
330
 
 
331
class TestBranchStacked(TestCaseWithTransport):
 
332
    """Tests for branch --stacked"""
 
333
 
 
334
    def assertRevisionInRepository(self, repo_path, revid):
 
335
        """Check that a revision is in a repository, disregarding stacking."""
 
336
        repo = bzrdir.BzrDir.open(repo_path).open_repository()
 
337
        self.assertTrue(repo.has_revision(revid))
 
338
 
 
339
    def assertRevisionNotInRepository(self, repo_path, revid):
 
340
        """Check that a revision is not in a repository, disregarding stacking."""
 
341
        repo = bzrdir.BzrDir.open(repo_path).open_repository()
 
342
        self.assertFalse(repo.has_revision(revid))
 
343
 
 
344
    def assertRevisionsInBranchRepository(self, revid_list, branch_path):
 
345
        repo = branch.Branch.open(branch_path).repository
 
346
        self.assertEqual(set(revid_list),
 
347
            repo.has_revisions(revid_list))
 
348
 
 
349
    def test_branch_stacked_branch_not_stacked(self):
 
350
        """Branching a stacked branch is not stacked by default"""
 
351
        # We have a mainline
 
352
        trunk_tree = self.make_branch_and_tree('target',
 
353
            format='1.9')
 
354
        trunk_tree.commit('mainline')
 
355
        # and a branch from it which is stacked
 
356
        branch_tree = self.make_branch_and_tree('branch',
 
357
            format='1.9')
 
358
        branch_tree.branch.set_stacked_on_url(trunk_tree.branch.base)
 
359
        # with some work on it
 
360
        work_tree = trunk_tree.branch.bzrdir.sprout('local').open_workingtree()
 
361
        work_tree.commit('moar work plz')
 
362
        work_tree.branch.push(branch_tree.branch)
 
363
        # branching our local branch gives us a new stacked branch pointing at
 
364
        # mainline.
 
365
        out, err = self.run_bzr(['branch', 'branch', 'newbranch'])
 
366
        self.assertEqual('', out)
 
367
        self.assertEqual('Branched 2 revisions.\n',
 
368
            err)
 
369
        # it should have preserved the branch format, and so it should be
 
370
        # capable of supporting stacking, but not actually have a stacked_on
 
371
        # branch configured
 
372
        self.assertRaises(errors.NotStacked,
 
373
            bzrdir.BzrDir.open('newbranch').open_branch().get_stacked_on_url)
 
374
 
 
375
    def test_branch_stacked_branch_stacked(self):
 
376
        """Asking to stack on a stacked branch does work"""
 
377
        # We have a mainline
 
378
        trunk_tree = self.make_branch_and_tree('target',
 
379
            format='1.9')
 
380
        trunk_revid = trunk_tree.commit('mainline')
 
381
        # and a branch from it which is stacked
 
382
        branch_tree = self.make_branch_and_tree('branch',
 
383
            format='1.9')
 
384
        branch_tree.branch.set_stacked_on_url(trunk_tree.branch.base)
 
385
        # with some work on it
 
386
        work_tree = trunk_tree.branch.bzrdir.sprout('local').open_workingtree()
 
387
        branch_revid = work_tree.commit('moar work plz')
 
388
        work_tree.branch.push(branch_tree.branch)
 
389
        # you can chain branches on from there
 
390
        out, err = self.run_bzr(['branch', 'branch', '--stacked', 'branch2'])
 
391
        self.assertEqual('', out)
 
392
        self.assertEqual('Created new stacked branch referring to %s.\n' %
 
393
            branch_tree.branch.base, err)
 
394
        self.assertEqual(branch_tree.branch.base,
 
395
            branch.Branch.open('branch2').get_stacked_on_url())
 
396
        branch2_tree = WorkingTree.open('branch2')
 
397
        branch2_revid = work_tree.commit('work on second stacked branch')
 
398
        work_tree.branch.push(branch2_tree.branch)
 
399
        self.assertRevisionInRepository('branch2', branch2_revid)
 
400
        self.assertRevisionsInBranchRepository(
 
401
            [trunk_revid, branch_revid, branch2_revid],
 
402
            'branch2')
 
403
 
 
404
    def test_branch_stacked(self):
 
405
        # We have a mainline
 
406
        trunk_tree = self.make_branch_and_tree('mainline',
 
407
            format='1.9')
 
408
        original_revid = trunk_tree.commit('mainline')
 
409
        self.assertRevisionInRepository('mainline', original_revid)
 
410
        # and a branch from it which is stacked
 
411
        out, err = self.run_bzr(['branch', '--stacked', 'mainline',
 
412
            'newbranch'])
 
413
        self.assertEqual('', out)
 
414
        self.assertEqual('Created new stacked branch referring to %s.\n' %
 
415
            trunk_tree.branch.base, err)
 
416
        self.assertRevisionNotInRepository('newbranch', original_revid)
 
417
        new_branch = branch.Branch.open('newbranch')
 
418
        self.assertEqual(trunk_tree.branch.base, new_branch.get_stacked_on_url())
 
419
 
 
420
    def test_branch_stacked_from_smart_server(self):
 
421
        # We can branch stacking on a smart server
 
422
        self.transport_server = test_server.SmartTCPServer_for_testing
 
423
        trunk = self.make_branch('mainline', format='1.9')
 
424
        out, err = self.run_bzr(
 
425
            ['branch', '--stacked', self.get_url('mainline'), 'shallow'])
 
426
 
 
427
    def test_branch_stacked_from_non_stacked_format(self):
 
428
        """The origin format doesn't support stacking"""
 
429
        trunk = self.make_branch('trunk', format='pack-0.92')
 
430
        out, err = self.run_bzr(
 
431
            ['branch', '--stacked', 'trunk', 'shallow'])
 
432
        # We should notify the user that we upgraded their format
 
433
        self.assertEqualDiff(
 
434
            'Source repository format does not support stacking, using format:\n'
 
435
            '  Packs 5 (adds stacking support, requires bzr 1.6)\n'
 
436
            'Source branch format does not support stacking, using format:\n'
 
437
            '  Branch format 7\n'
 
438
            'Doing on-the-fly conversion from RepositoryFormatKnitPack1() to RepositoryFormatKnitPack5().\n'
 
439
            'This may take some time. Upgrade the repositories to the same format for better performance.\n'
 
440
            'Created new stacked branch referring to %s.\n' % (trunk.base,),
 
441
            err)
 
442
 
 
443
    def test_branch_stacked_from_rich_root_non_stackable(self):
 
444
        trunk = self.make_branch('trunk', format='rich-root-pack')
 
445
        out, err = self.run_bzr(
 
446
            ['branch', '--stacked', 'trunk', 'shallow'])
 
447
        # We should notify the user that we upgraded their format
 
448
        self.assertEqualDiff(
 
449
            'Source repository format does not support stacking, using format:\n'
 
450
            '  Packs 5 rich-root (adds stacking support, requires bzr 1.6.1)\n'
 
451
            'Source branch format does not support stacking, using format:\n'
 
452
            '  Branch format 7\n'
 
453
            'Doing on-the-fly conversion from RepositoryFormatKnitPack4() to RepositoryFormatKnitPack5RichRoot().\n'
 
454
            'This may take some time. Upgrade the repositories to the same format for better performance.\n'
 
455
            'Created new stacked branch referring to %s.\n' % (trunk.base,),
 
456
            err)
 
457
 
 
458
 
 
459
class TestSmartServerBranching(TestCaseWithTransport):
 
460
 
 
461
    def test_branch_from_trivial_branch_to_same_server_branch_acceptance(self):
 
462
        self.setup_smart_server_with_call_log()
 
463
        t = self.make_branch_and_tree('from')
 
464
        for count in range(9):
 
465
            t.commit(message='commit %d' % count)
 
466
        self.reset_smart_call_log()
 
467
        out, err = self.run_bzr(['branch', self.get_url('from'),
 
468
            self.get_url('target')])
 
469
        # This figure represent the amount of work to perform this use case. It
 
470
        # is entirely ok to reduce this number if a test fails due to rpc_count
 
471
        # being too low. If rpc_count increases, more network roundtrips have
 
472
        # become necessary for this use case. Please do not adjust this number
 
473
        # upwards without agreement from bzr's network support maintainers.
 
474
        self.assertLength(39, self.hpss_calls)
 
475
 
 
476
    def test_branch_from_trivial_branch_streaming_acceptance(self):
 
477
        self.setup_smart_server_with_call_log()
 
478
        t = self.make_branch_and_tree('from')
 
479
        for count in range(9):
 
480
            t.commit(message='commit %d' % count)
 
481
        self.reset_smart_call_log()
 
482
        out, err = self.run_bzr(['branch', self.get_url('from'),
 
483
            'local-target'])
 
484
        # This figure represent the amount of work to perform this use case. It
 
485
        # is entirely ok to reduce this number if a test fails due to rpc_count
 
486
        # being too low. If rpc_count increases, more network roundtrips have
 
487
        # become necessary for this use case. Please do not adjust this number
 
488
        # upwards without agreement from bzr's network support maintainers.
 
489
        self.assertLength(10, self.hpss_calls)
 
490
 
 
491
    def test_branch_from_trivial_stacked_branch_streaming_acceptance(self):
 
492
        self.setup_smart_server_with_call_log()
 
493
        t = self.make_branch_and_tree('trunk')
 
494
        for count in range(8):
 
495
            t.commit(message='commit %d' % count)
 
496
        tree2 = t.branch.bzrdir.sprout('feature', stacked=True
 
497
            ).open_workingtree()
 
498
        local_tree = t.branch.bzrdir.sprout('local-working').open_workingtree()
 
499
        local_tree.commit('feature change')
 
500
        local_tree.branch.push(tree2.branch)
 
501
        self.reset_smart_call_log()
 
502
        out, err = self.run_bzr(['branch', self.get_url('feature'),
 
503
            'local-target'])
 
504
        # This figure represent the amount of work to perform this use case. It
 
505
        # is entirely ok to reduce this number if a test fails due to rpc_count
 
506
        # being too low. If rpc_count increases, more network roundtrips have
 
507
        # become necessary for this use case. Please do not adjust this number
 
508
        # upwards without agreement from bzr's network support maintainers.
 
509
        self.assertLength(15, self.hpss_calls)
 
510
 
 
511
    def test_branch_from_branch_with_tags(self):
 
512
        self.setup_smart_server_with_call_log()
 
513
        builder = self.make_branch_builder('source')
 
514
        source = fixtures.build_branch_with_non_ancestral_rev(builder)
 
515
        source.get_config().set_user_option('branch.fetch_tags', 'True')
 
516
        source.tags.set_tag('tag-a', 'rev-2')
 
517
        source.tags.set_tag('tag-missing', 'missing-rev')
 
518
        # Now source has a tag not in its ancestry.  Make a branch from it.
 
519
        self.reset_smart_call_log()
 
520
        out, err = self.run_bzr(['branch', self.get_url('source'), 'target'])
 
521
        # This figure represent the amount of work to perform this use case. It
 
522
        # is entirely ok to reduce this number if a test fails due to rpc_count
 
523
        # being too low. If rpc_count increases, more network roundtrips have
 
524
        # become necessary for this use case. Please do not adjust this number
 
525
        # upwards without agreement from bzr's network support maintainers.
 
526
        self.assertLength(10, self.hpss_calls)
 
527
 
 
528
    def test_branch_to_stacked_from_trivial_branch_streaming_acceptance(self):
 
529
        self.setup_smart_server_with_call_log()
 
530
        t = self.make_branch_and_tree('from')
 
531
        for count in range(9):
 
532
            t.commit(message='commit %d' % count)
 
533
        self.reset_smart_call_log()
 
534
        out, err = self.run_bzr(['branch', '--stacked', self.get_url('from'),
 
535
            'local-target'])
 
536
        # XXX: the number of hpss calls for this case isn't deterministic yet,
 
537
        # so we can't easily assert about the number of calls.
 
538
        #self.assertLength(XXX, self.hpss_calls)
 
539
        # We can assert that none of the calls were readv requests for rix
 
540
        # files, though (demonstrating that at least get_parent_map calls are
 
541
        # not using VFS RPCs).
 
542
        readvs_of_rix_files = [
 
543
            c for c in self.hpss_calls
 
544
            if c.call.method == 'readv' and c.call.args[-1].endswith('.rix')]
 
545
        self.assertLength(0, readvs_of_rix_files)
 
546
 
 
547
 
 
548
class TestRemoteBranch(TestCaseWithSFTPServer):
 
549
 
 
550
    def setUp(self):
 
551
        super(TestRemoteBranch, self).setUp()
 
552
        tree = self.make_branch_and_tree('branch')
 
553
        self.build_tree_contents([('branch/file', 'file content\n')])
 
554
        tree.add('file')
 
555
        tree.commit('file created')
 
556
 
 
557
    def test_branch_local_remote(self):
 
558
        self.run_bzr(['branch', 'branch', self.get_url('remote')])
 
559
        t = self.get_transport()
 
560
        # Ensure that no working tree what created remotely
 
561
        self.assertFalse(t.has('remote/file'))
 
562
 
 
563
    def test_branch_remote_remote(self):
 
564
        # Light cheat: we access the branch remotely
 
565
        self.run_bzr(['branch', self.get_url('branch'),
 
566
                      self.get_url('remote')])
 
567
        t = self.get_transport()
 
568
        # Ensure that no working tree what created remotely
 
569
        self.assertFalse(t.has('remote/file'))
 
570
 
 
571
 
 
572
class TestDeprecatedAliases(TestCaseWithTransport):
 
573
 
 
574
    def test_deprecated_aliases(self):
 
575
        """bzr branch can be called clone or get, but those names are deprecated.
 
576
 
 
577
        See bug 506265.
 
578
        """
 
579
        for command in ['clone', 'get']:
 
580
            run_script(self, """
 
581
            $ bzr %(command)s A B
 
582
            2>The command 'bzr %(command)s' has been deprecated in bzr 2.4. Please use 'bzr branch' instead.
 
583
            2>bzr: ERROR: Not a branch...
 
584
            """ % locals())
 
585
 
 
586
 
 
587
class TestBranchParentLocation(test_switch.TestSwitchParentLocationBase):
 
588
 
 
589
    def _checkout_and_branch(self, option=''):
 
590
        self.script_runner.run_script(self, '''
 
591
                $ bzr checkout %(option)s repo/trunk checkout
 
592
                $ cd checkout
 
593
                $ bzr branch --switch ../repo/trunk ../repo/branched
 
594
                2>Branched 0 revisions.
 
595
                2>Tree is up to date at revision 0.
 
596
                2>Switched to branch:...branched...
 
597
                $ cd ..
 
598
                ''' % locals())
 
599
        bound_branch = branch.Branch.open_containing('checkout')[0]
 
600
        master_branch = branch.Branch.open_containing('repo/branched')[0]
 
601
        return (bound_branch, master_branch)
 
602
 
 
603
    def test_branch_switch_parent_lightweight(self):
 
604
        """Lightweight checkout using bzr branch --switch."""
 
605
        bb, mb = self._checkout_and_branch(option='--lightweight')
 
606
        self.assertParent('repo/trunk', bb)
 
607
        self.assertParent('repo/trunk', mb)
 
608
 
 
609
    def test_branch_switch_parent_heavyweight(self):
 
610
        """Heavyweight checkout using bzr branch --switch."""
 
611
        bb, mb = self._checkout_and_branch()
 
612
        self.assertParent('repo/trunk', bb)
 
613
        self.assertParent('repo/trunk', mb)