~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Jelmer Vernooij
  • Date: 2011-12-05 14:12:23 UTC
  • mto: This revision was merged to the branch mainline in revision 6348.
  • Revision ID: jelmer@samba.org-20111205141223-8qxae4h37satlzgq
Move more functionality to vf_search.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2008, 2009 Canonical Ltd
 
1
# Copyright (C) 2006-2011 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
22
22
from bzrlib import (
23
23
    branch,
24
24
    bzrdir,
 
25
    controldir,
25
26
    errors,
26
 
    repository,
27
27
    revision as _mod_revision,
28
28
    )
29
29
from bzrlib.repofmt.knitrepo import RepositoryFormatKnit1
30
 
from bzrlib.tests.blackbox import ExternalBase
 
30
from bzrlib.tests import TestCaseWithTransport
31
31
from bzrlib.tests import (
32
 
    KnownFailure,
 
32
    fixtures,
 
33
    test_server,
 
34
    )
 
35
from bzrlib.tests.features import (
33
36
    HardlinkFeature,
34
 
    test_server,
35
37
    )
 
38
from bzrlib.tests.blackbox import test_switch
36
39
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
 
40
from bzrlib.tests.script import run_script
37
41
from bzrlib.urlutils import local_path_to_url, strip_trailing_slash
38
42
from bzrlib.workingtree import WorkingTree
39
43
 
40
44
 
41
 
class TestBranch(ExternalBase):
 
45
class TestBranch(TestCaseWithTransport):
42
46
 
43
 
    def example_branch(self, path='.'):
44
 
        tree = self.make_branch_and_tree(path)
 
47
    def example_branch(self, path='.', format=None):
 
48
        tree = self.make_branch_and_tree(path, format=format)
45
49
        self.build_tree_contents([(path + '/hello', 'foo')])
46
50
        tree.add('hello')
47
51
        tree.commit(message='setup')
48
52
        self.build_tree_contents([(path + '/goodbye', 'baz')])
49
53
        tree.add('goodbye')
50
54
        tree.commit(message='setup')
 
55
        return tree
51
56
 
52
57
    def test_branch(self):
53
58
        """Branch from one branch to another."""
59
64
        self.assertFalse(b._transport.has('branch-name'))
60
65
        b.bzrdir.open_workingtree().commit(message='foo', allow_pointless=True)
61
66
 
 
67
    def test_into_colocated(self):
 
68
        """Branch from a branch into a colocated branch."""
 
69
        self.example_branch('a')
 
70
        out, err = self.run_bzr(
 
71
            'init --format=development-colo file:b,branch=orig')
 
72
        self.assertEqual(
 
73
            """Created a lightweight checkout (format: development-colo)\n""",
 
74
            out)
 
75
        self.assertEqual('', err)
 
76
        out, err = self.run_bzr(
 
77
            'branch a file:b,branch=thiswasa')
 
78
        self.assertEqual('', out)
 
79
        self.assertEqual('Branched 2 revisions.\n', err)
 
80
        out, err = self.run_bzr('branches b')
 
81
        self.assertEqual(" orig\n thiswasa\n", out)
 
82
        self.assertEqual('', err)
 
83
        out,err = self.run_bzr('branch a file:b,branch=orig', retcode=3)
 
84
        self.assertEqual('', out)
 
85
        self.assertEqual('bzr: ERROR: Already a branch: "file:b,branch=orig".\n', err)
 
86
 
 
87
    def test_from_colocated(self):
 
88
        """Branch from a colocated branch into a regular branch."""
 
89
        tree = self.example_branch('a', format='development-colo')
 
90
        tree.bzrdir.create_branch(name='somecolo')
 
91
        out, err = self.run_bzr('branch %s,branch=somecolo' %
 
92
            local_path_to_url('a'))
 
93
        self.assertEqual('', out)
 
94
        self.assertEqual('Branched 0 revisions.\n', err)
 
95
        self.assertPathExists("somecolo")
 
96
 
 
97
    def test_branch_broken_pack(self):
 
98
        """branching with a corrupted pack file."""
 
99
        self.example_branch('a')
 
100
        # add some corruption
 
101
        packs_dir = 'a/.bzr/repository/packs/'
 
102
        fname = packs_dir + os.listdir(packs_dir)[0]
 
103
        with open(fname, 'rb+') as f:
 
104
            # Start from the end of the file to avoid choosing a place bigger
 
105
            # than the file itself.
 
106
            f.seek(-5, os.SEEK_END)
 
107
            c = f.read(1)
 
108
            f.seek(-5, os.SEEK_END)
 
109
            # Make sure we inject a value different than the one we just read
 
110
            if c == '\xFF':
 
111
                corrupt = '\x00'
 
112
            else:
 
113
                corrupt = '\xFF'
 
114
            f.write(corrupt) # make sure we corrupt something
 
115
        self.run_bzr_error(['Corruption while decompressing repository file'], 
 
116
                            'branch a b', retcode=3)
 
117
 
62
118
    def test_branch_switch_no_branch(self):
63
119
        # No branch in the current directory:
64
120
        #  => new branch will be created, but switch fails
134
190
 
135
191
        def make_shared_tree(path):
136
192
            shared_repo.bzrdir.root_transport.mkdir(path)
137
 
            shared_repo.bzrdir.create_branch_convenience('repo/' + path)
 
193
            controldir.ControlDir.create_branch_convenience('repo/' + path)
138
194
            return WorkingTree.open('repo/' + path)
139
195
        tree_a = make_shared_tree('a')
140
196
        self.build_tree(['repo/a/file'])
174
230
        target_stat = os.stat('target/file1')
175
231
        self.assertEqual(source_stat, target_stat)
176
232
 
 
233
    def test_branch_files_from(self):
 
234
        source = self.make_branch_and_tree('source')
 
235
        self.build_tree(['source/file1'])
 
236
        source.add('file1')
 
237
        source.commit('added file')
 
238
        out, err = self.run_bzr('branch source target --files-from source')
 
239
        self.assertPathExists('target/file1')
 
240
 
 
241
    def test_branch_files_from_hardlink(self):
 
242
        self.requireFeature(HardlinkFeature)
 
243
        source = self.make_branch_and_tree('source')
 
244
        self.build_tree(['source/file1'])
 
245
        source.add('file1')
 
246
        source.commit('added file')
 
247
        source.bzrdir.sprout('second')
 
248
        out, err = self.run_bzr('branch source target --files-from second'
 
249
                                ' --hardlink')
 
250
        source_stat = os.stat('source/file1')
 
251
        second_stat = os.stat('second/file1')
 
252
        target_stat = os.stat('target/file1')
 
253
        self.assertNotEqual(source_stat, target_stat)
 
254
        self.assertEqual(second_stat, target_stat)
 
255
 
177
256
    def test_branch_standalone(self):
178
257
        shared_repo = self.make_repository('repo', shared=True)
179
258
        self.example_branch('source')
186
265
    def test_branch_no_tree(self):
187
266
        self.example_branch('source')
188
267
        self.run_bzr('branch --no-tree source target')
189
 
        self.failIfExists('target/hello')
190
 
        self.failIfExists('target/goodbye')
 
268
        self.assertPathDoesNotExist('target/hello')
 
269
        self.assertPathDoesNotExist('target/goodbye')
191
270
 
192
271
    def test_branch_into_existing_dir(self):
193
272
        self.example_branch('a')
203
282
        # force operation
204
283
        self.run_bzr('branch a b --use-existing-dir')
205
284
        # check conflicts
206
 
        self.failUnlessExists('b/hello.moved')
207
 
        self.failIfExists('b/godbye.moved')
 
285
        self.assertPathExists('b/hello.moved')
 
286
        self.assertPathDoesNotExist('b/godbye.moved')
208
287
        # we can't branch into branch
209
288
        out,err = self.run_bzr('branch a b --use-existing-dir', retcode=3)
210
289
        self.assertEqual('', out)
217
296
        b = branch.Branch.open('b')
218
297
        self.assertEndsWith(b.get_bound_location(), '/a/')
219
298
 
220
 
 
221
 
class TestBranchStacked(ExternalBase):
 
299
    def test_branch_with_post_branch_init_hook(self):
 
300
        calls = []
 
301
        branch.Branch.hooks.install_named_hook('post_branch_init',
 
302
            calls.append, None)
 
303
        self.assertLength(0, calls)
 
304
        self.example_branch('a')
 
305
        self.assertLength(1, calls)
 
306
        self.run_bzr('branch a b')
 
307
        self.assertLength(2, calls)
 
308
 
 
309
    def test_checkout_with_post_branch_init_hook(self):
 
310
        calls = []
 
311
        branch.Branch.hooks.install_named_hook('post_branch_init',
 
312
            calls.append, None)
 
313
        self.assertLength(0, calls)
 
314
        self.example_branch('a')
 
315
        self.assertLength(1, calls)
 
316
        self.run_bzr('checkout a b')
 
317
        self.assertLength(2, calls)
 
318
 
 
319
    def test_lightweight_checkout_with_post_branch_init_hook(self):
 
320
        calls = []
 
321
        branch.Branch.hooks.install_named_hook('post_branch_init',
 
322
            calls.append, None)
 
323
        self.assertLength(0, calls)
 
324
        self.example_branch('a')
 
325
        self.assertLength(1, calls)
 
326
        self.run_bzr('checkout --lightweight a b')
 
327
        self.assertLength(2, calls)
 
328
 
 
329
    def test_branch_fetches_all_tags(self):
 
330
        builder = self.make_branch_builder('source')
 
331
        source = fixtures.build_branch_with_non_ancestral_rev(builder)
 
332
        source.tags.set_tag('tag-a', 'rev-2')
 
333
        source.get_config().set_user_option('branch.fetch_tags', 'True')
 
334
        # Now source has a tag not in its ancestry.  Make a branch from it.
 
335
        self.run_bzr('branch source new-branch')
 
336
        new_branch = branch.Branch.open('new-branch')
 
337
        # The tag is present, and so is its revision.
 
338
        self.assertEqual('rev-2', new_branch.tags.lookup_tag('tag-a'))
 
339
        new_branch.repository.get_revision('rev-2')
 
340
 
 
341
 
 
342
class TestBranchStacked(TestCaseWithTransport):
222
343
    """Tests for branch --stacked"""
223
344
 
224
345
    def assertRevisionInRepository(self, repo_path, revid):
254
375
        # mainline.
255
376
        out, err = self.run_bzr(['branch', 'branch', 'newbranch'])
256
377
        self.assertEqual('', out)
257
 
        self.assertEqual('Branched 2 revision(s).\n',
 
378
        self.assertEqual('Branched 2 revisions.\n',
258
379
            err)
259
380
        # it should have preserved the branch format, and so it should be
260
381
        # capable of supporting stacking, but not actually have a stacked_on
325
446
            '  Packs 5 (adds stacking support, requires bzr 1.6)\n'
326
447
            'Source branch format does not support stacking, using format:\n'
327
448
            '  Branch format 7\n'
 
449
            'Doing on-the-fly conversion from RepositoryFormatKnitPack1() to RepositoryFormatKnitPack5().\n'
 
450
            'This may take some time. Upgrade the repositories to the same format for better performance.\n'
328
451
            'Created new stacked branch referring to %s.\n' % (trunk.base,),
329
452
            err)
330
453
 
338
461
            '  Packs 5 rich-root (adds stacking support, requires bzr 1.6.1)\n'
339
462
            'Source branch format does not support stacking, using format:\n'
340
463
            '  Branch format 7\n'
 
464
            'Doing on-the-fly conversion from RepositoryFormatKnitPack4() to RepositoryFormatKnitPack5RichRoot().\n'
 
465
            'This may take some time. Upgrade the repositories to the same format for better performance.\n'
341
466
            'Created new stacked branch referring to %s.\n' % (trunk.base,),
342
467
            err)
343
468
 
344
469
 
345
 
class TestSmartServerBranching(ExternalBase):
 
470
class TestSmartServerBranching(TestCaseWithTransport):
346
471
 
347
472
    def test_branch_from_trivial_branch_to_same_server_branch_acceptance(self):
348
473
        self.setup_smart_server_with_call_log()
357
482
        # being too low. If rpc_count increases, more network roundtrips have
358
483
        # become necessary for this use case. Please do not adjust this number
359
484
        # upwards without agreement from bzr's network support maintainers.
360
 
        self.assertLength(38, self.hpss_calls)
 
485
        self.assertLength(40, self.hpss_calls)
361
486
 
362
487
    def test_branch_from_trivial_branch_streaming_acceptance(self):
363
488
        self.setup_smart_server_with_call_log()
394
519
        # upwards without agreement from bzr's network support maintainers.
395
520
        self.assertLength(15, self.hpss_calls)
396
521
 
 
522
    def test_branch_from_branch_with_tags(self):
 
523
        self.setup_smart_server_with_call_log()
 
524
        builder = self.make_branch_builder('source')
 
525
        source = fixtures.build_branch_with_non_ancestral_rev(builder)
 
526
        source.get_config().set_user_option('branch.fetch_tags', 'True')
 
527
        source.tags.set_tag('tag-a', 'rev-2')
 
528
        source.tags.set_tag('tag-missing', 'missing-rev')
 
529
        # Now source has a tag not in its ancestry.  Make a branch from it.
 
530
        self.reset_smart_call_log()
 
531
        out, err = self.run_bzr(['branch', self.get_url('source'), 'target'])
 
532
        # This figure represent the amount of work to perform this use case. It
 
533
        # is entirely ok to reduce this number if a test fails due to rpc_count
 
534
        # being too low. If rpc_count increases, more network roundtrips have
 
535
        # become necessary for this use case. Please do not adjust this number
 
536
        # upwards without agreement from bzr's network support maintainers.
 
537
        self.assertLength(10, self.hpss_calls)
 
538
 
 
539
    def test_branch_to_stacked_from_trivial_branch_streaming_acceptance(self):
 
540
        self.setup_smart_server_with_call_log()
 
541
        t = self.make_branch_and_tree('from')
 
542
        for count in range(9):
 
543
            t.commit(message='commit %d' % count)
 
544
        self.reset_smart_call_log()
 
545
        out, err = self.run_bzr(['branch', '--stacked', self.get_url('from'),
 
546
            'local-target'])
 
547
        # XXX: the number of hpss calls for this case isn't deterministic yet,
 
548
        # so we can't easily assert about the number of calls.
 
549
        #self.assertLength(XXX, self.hpss_calls)
 
550
        # We can assert that none of the calls were readv requests for rix
 
551
        # files, though (demonstrating that at least get_parent_map calls are
 
552
        # not using VFS RPCs).
 
553
        readvs_of_rix_files = [
 
554
            c for c in self.hpss_calls
 
555
            if c.call.method == 'readv' and c.call.args[-1].endswith('.rix')]
 
556
        self.assertLength(0, readvs_of_rix_files)
 
557
 
397
558
 
398
559
class TestRemoteBranch(TestCaseWithSFTPServer):
399
560
 
418
579
        # Ensure that no working tree what created remotely
419
580
        self.assertFalse(t.has('remote/file'))
420
581
 
 
582
 
 
583
class TestDeprecatedAliases(TestCaseWithTransport):
 
584
 
 
585
    def test_deprecated_aliases(self):
 
586
        """bzr branch can be called clone or get, but those names are deprecated.
 
587
 
 
588
        See bug 506265.
 
589
        """
 
590
        for command in ['clone', 'get']:
 
591
            run_script(self, """
 
592
            $ bzr %(command)s A B
 
593
            2>The command 'bzr %(command)s' has been deprecated in bzr 2.4. Please use 'bzr branch' instead.
 
594
            2>bzr: ERROR: Not a branch...
 
595
            """ % locals())
 
596
 
 
597
 
 
598
class TestBranchParentLocation(test_switch.TestSwitchParentLocationBase):
 
599
 
 
600
    def _checkout_and_branch(self, option=''):
 
601
        self.script_runner.run_script(self, '''
 
602
                $ bzr checkout %(option)s repo/trunk checkout
 
603
                $ cd checkout
 
604
                $ bzr branch --switch ../repo/trunk ../repo/branched
 
605
                2>Branched 0 revisions.
 
606
                2>Tree is up to date at revision 0.
 
607
                2>Switched to branch:...branched...
 
608
                $ cd ..
 
609
                ''' % locals())
 
610
        bound_branch = branch.Branch.open_containing('checkout')[0]
 
611
        master_branch = branch.Branch.open_containing('repo/branched')[0]
 
612
        return (bound_branch, master_branch)
 
613
 
 
614
    def test_branch_switch_parent_lightweight(self):
 
615
        """Lightweight checkout using bzr branch --switch."""
 
616
        bb, mb = self._checkout_and_branch(option='--lightweight')
 
617
        self.assertParent('repo/trunk', bb)
 
618
        self.assertParent('repo/trunk', mb)
 
619
 
 
620
    def test_branch_switch_parent_heavyweight(self):
 
621
        """Heavyweight checkout using bzr branch --switch."""
 
622
        bb, mb = self._checkout_and_branch()
 
623
        self.assertParent('repo/trunk', bb)
 
624
        self.assertParent('repo/trunk', mb)