~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

Merge bzr.dev, update to use new hooks.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 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
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
 
39
from bzrlib.tests.matchers import ContainsNoVfsCalls
36
40
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
 
41
from bzrlib.tests.script import run_script
37
42
from bzrlib.urlutils import local_path_to_url, strip_trailing_slash
38
43
from bzrlib.workingtree import WorkingTree
39
44
 
40
45
 
41
46
class TestBranch(TestCaseWithTransport):
42
47
 
43
 
    def example_branch(self, path='.'):
44
 
        tree = self.make_branch_and_tree(path)
 
48
    def example_branch(self, path='.', format=None):
 
49
        tree = self.make_branch_and_tree(path, format=format)
45
50
        self.build_tree_contents([(path + '/hello', 'foo')])
46
51
        tree.add('hello')
47
52
        tree.commit(message='setup')
48
53
        self.build_tree_contents([(path + '/goodbye', 'baz')])
49
54
        tree.add('goodbye')
50
55
        tree.commit(message='setup')
 
56
        return tree
51
57
 
52
58
    def test_branch(self):
53
59
        """Branch from one branch to another."""
59
65
        self.assertFalse(b._transport.has('branch-name'))
60
66
        b.bzrdir.open_workingtree().commit(message='foo', allow_pointless=True)
61
67
 
 
68
    def test_into_colocated(self):
 
69
        """Branch from a branch into a colocated branch."""
 
70
        self.example_branch('a')
 
71
        out, err = self.run_bzr(
 
72
            'init --format=development-colo file:b,branch=orig')
 
73
        self.assertEqual(
 
74
            """Created a lightweight checkout (format: development-colo)\n""",
 
75
            out)
 
76
        self.assertEqual('', err)
 
77
        out, err = self.run_bzr(
 
78
            'branch a file:b,branch=thiswasa')
 
79
        self.assertEqual('', out)
 
80
        self.assertEqual('Branched 2 revisions.\n', err)
 
81
        out, err = self.run_bzr('branches b')
 
82
        self.assertEqual(" thiswasa\n orig\n", out)
 
83
        self.assertEqual('', err)
 
84
        out,err = self.run_bzr('branch a file:b,branch=orig', retcode=3)
 
85
        self.assertEqual('', out)
 
86
        self.assertEqual('bzr: ERROR: Already a branch: "file:b,branch=orig".\n', err)
 
87
 
 
88
    def test_from_colocated(self):
 
89
        """Branch from a colocated branch into a regular branch."""
 
90
        tree = self.example_branch('a', format='development-colo')
 
91
        tree.bzrdir.create_branch(name='somecolo')
 
92
        out, err = self.run_bzr('branch %s,branch=somecolo' %
 
93
            local_path_to_url('a'))
 
94
        self.assertEqual('', out)
 
95
        self.assertEqual('Branched 0 revisions.\n', err)
 
96
        self.assertPathExists("somecolo")
 
97
 
 
98
    def test_branch_broken_pack(self):
 
99
        """branching with a corrupted pack file."""
 
100
        self.example_branch('a')
 
101
        # add some corruption
 
102
        packs_dir = 'a/.bzr/repository/packs/'
 
103
        fname = packs_dir + os.listdir(packs_dir)[0]
 
104
        with open(fname, 'rb+') as f:
 
105
            # Start from the end of the file to avoid choosing a place bigger
 
106
            # than the file itself.
 
107
            f.seek(-5, os.SEEK_END)
 
108
            c = f.read(1)
 
109
            f.seek(-5, os.SEEK_END)
 
110
            # Make sure we inject a value different than the one we just read
 
111
            if c == '\xFF':
 
112
                corrupt = '\x00'
 
113
            else:
 
114
                corrupt = '\xFF'
 
115
            f.write(corrupt) # make sure we corrupt something
 
116
        self.run_bzr_error(['Corruption while decompressing repository file'], 
 
117
                            'branch a b', retcode=3)
 
118
 
62
119
    def test_branch_switch_no_branch(self):
63
120
        # No branch in the current directory:
64
121
        #  => new branch will be created, but switch fails
134
191
 
135
192
        def make_shared_tree(path):
136
193
            shared_repo.bzrdir.root_transport.mkdir(path)
137
 
            shared_repo.bzrdir.create_branch_convenience('repo/' + path)
 
194
            controldir.ControlDir.create_branch_convenience('repo/' + path)
138
195
            return WorkingTree.open('repo/' + path)
139
196
        tree_a = make_shared_tree('a')
140
197
        self.build_tree(['repo/a/file'])
180
237
        source.add('file1')
181
238
        source.commit('added file')
182
239
        out, err = self.run_bzr('branch source target --files-from source')
183
 
        self.failUnlessExists('target/file1')
 
240
        self.assertPathExists('target/file1')
184
241
 
185
242
    def test_branch_files_from_hardlink(self):
186
243
        self.requireFeature(HardlinkFeature)
209
266
    def test_branch_no_tree(self):
210
267
        self.example_branch('source')
211
268
        self.run_bzr('branch --no-tree source target')
212
 
        self.failIfExists('target/hello')
213
 
        self.failIfExists('target/goodbye')
 
269
        self.assertPathDoesNotExist('target/hello')
 
270
        self.assertPathDoesNotExist('target/goodbye')
214
271
 
215
272
    def test_branch_into_existing_dir(self):
216
273
        self.example_branch('a')
226
283
        # force operation
227
284
        self.run_bzr('branch a b --use-existing-dir')
228
285
        # check conflicts
229
 
        self.failUnlessExists('b/hello.moved')
230
 
        self.failIfExists('b/godbye.moved')
 
286
        self.assertPathExists('b/hello.moved')
 
287
        self.assertPathDoesNotExist('b/godbye.moved')
231
288
        # we can't branch into branch
232
289
        out,err = self.run_bzr('branch a b --use-existing-dir', retcode=3)
233
290
        self.assertEqual('', out)
270
327
        self.run_bzr('checkout --lightweight a b')
271
328
        self.assertLength(2, calls)
272
329
 
 
330
    def test_branch_fetches_all_tags(self):
 
331
        builder = self.make_branch_builder('source')
 
332
        source = fixtures.build_branch_with_non_ancestral_rev(builder)
 
333
        source.tags.set_tag('tag-a', 'rev-2')
 
334
        source.get_config().set_user_option('branch.fetch_tags', 'True')
 
335
        # Now source has a tag not in its ancestry.  Make a branch from it.
 
336
        self.run_bzr('branch source new-branch')
 
337
        new_branch = branch.Branch.open('new-branch')
 
338
        # The tag is present, and so is its revision.
 
339
        self.assertEqual('rev-2', new_branch.tags.lookup_tag('tag-a'))
 
340
        new_branch.repository.get_revision('rev-2')
 
341
 
273
342
 
274
343
class TestBranchStacked(TestCaseWithTransport):
275
344
    """Tests for branch --stacked"""
307
376
        # mainline.
308
377
        out, err = self.run_bzr(['branch', 'branch', 'newbranch'])
309
378
        self.assertEqual('', out)
310
 
        self.assertEqual('Branched 2 revision(s).\n',
 
379
        self.assertEqual('Branched 2 revisions.\n',
311
380
            err)
312
381
        # it should have preserved the branch format, and so it should be
313
382
        # capable of supporting stacking, but not actually have a stacked_on
414
483
        # being too low. If rpc_count increases, more network roundtrips have
415
484
        # become necessary for this use case. Please do not adjust this number
416
485
        # upwards without agreement from bzr's network support maintainers.
417
 
        self.assertLength(38, self.hpss_calls)
 
486
        self.assertLength(40, self.hpss_calls)
 
487
        self.expectFailure("branching to the same branch requires VFS access",
 
488
            self.assertThat, self.hpss_calls, ContainsNoVfsCalls)
418
489
 
419
490
    def test_branch_from_trivial_branch_streaming_acceptance(self):
420
491
        self.setup_smart_server_with_call_log()
429
500
        # being too low. If rpc_count increases, more network roundtrips have
430
501
        # become necessary for this use case. Please do not adjust this number
431
502
        # upwards without agreement from bzr's network support maintainers.
 
503
        self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
432
504
        self.assertLength(10, self.hpss_calls)
433
505
 
434
506
    def test_branch_from_trivial_stacked_branch_streaming_acceptance(self):
449
521
        # being too low. If rpc_count increases, more network roundtrips have
450
522
        # become necessary for this use case. Please do not adjust this number
451
523
        # upwards without agreement from bzr's network support maintainers.
 
524
        self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
452
525
        self.assertLength(15, self.hpss_calls)
453
526
 
 
527
    def test_branch_from_branch_with_tags(self):
 
528
        self.setup_smart_server_with_call_log()
 
529
        builder = self.make_branch_builder('source')
 
530
        source = fixtures.build_branch_with_non_ancestral_rev(builder)
 
531
        source.get_config().set_user_option('branch.fetch_tags', 'True')
 
532
        source.tags.set_tag('tag-a', 'rev-2')
 
533
        source.tags.set_tag('tag-missing', 'missing-rev')
 
534
        # Now source has a tag not in its ancestry.  Make a branch from it.
 
535
        self.reset_smart_call_log()
 
536
        out, err = self.run_bzr(['branch', self.get_url('source'), 'target'])
 
537
        # This figure represent the amount of work to perform this use case. It
 
538
        # is entirely ok to reduce this number if a test fails due to rpc_count
 
539
        # being too low. If rpc_count increases, more network roundtrips have
 
540
        # become necessary for this use case. Please do not adjust this number
 
541
        # upwards without agreement from bzr's network support maintainers.
 
542
        self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
 
543
        self.assertLength(10, self.hpss_calls)
 
544
 
 
545
    def test_branch_to_stacked_from_trivial_branch_streaming_acceptance(self):
 
546
        self.setup_smart_server_with_call_log()
 
547
        t = self.make_branch_and_tree('from')
 
548
        for count in range(9):
 
549
            t.commit(message='commit %d' % count)
 
550
        self.reset_smart_call_log()
 
551
        out, err = self.run_bzr(['branch', '--stacked', self.get_url('from'),
 
552
            'local-target'])
 
553
        # XXX: the number of hpss calls for this case isn't deterministic yet,
 
554
        # so we can't easily assert about the number of calls.
 
555
        #self.assertLength(XXX, self.hpss_calls)
 
556
        # We can assert that none of the calls were readv requests for rix
 
557
        # files, though (demonstrating that at least get_parent_map calls are
 
558
        # not using VFS RPCs).
 
559
        readvs_of_rix_files = [
 
560
            c for c in self.hpss_calls
 
561
            if c.call.method == 'readv' and c.call.args[-1].endswith('.rix')]
 
562
        self.assertLength(0, readvs_of_rix_files)
 
563
        self.expectFailure("branching to stacked requires VFS access",
 
564
            self.assertThat, self.hpss_calls, ContainsNoVfsCalls)
 
565
 
454
566
 
455
567
class TestRemoteBranch(TestCaseWithSFTPServer):
456
568
 
475
587
        # Ensure that no working tree what created remotely
476
588
        self.assertFalse(t.has('remote/file'))
477
589
 
 
590
 
 
591
class TestDeprecatedAliases(TestCaseWithTransport):
 
592
 
 
593
    def test_deprecated_aliases(self):
 
594
        """bzr branch can be called clone or get, but those names are deprecated.
 
595
 
 
596
        See bug 506265.
 
597
        """
 
598
        for command in ['clone', 'get']:
 
599
            run_script(self, """
 
600
            $ bzr %(command)s A B
 
601
            2>The command 'bzr %(command)s' has been deprecated in bzr 2.4. Please use 'bzr branch' instead.
 
602
            2>bzr: ERROR: Not a branch...
 
603
            """ % locals())
 
604
 
 
605
 
 
606
class TestBranchParentLocation(test_switch.TestSwitchParentLocationBase):
 
607
 
 
608
    def _checkout_and_branch(self, option=''):
 
609
        self.script_runner.run_script(self, '''
 
610
                $ bzr checkout %(option)s repo/trunk checkout
 
611
                $ cd checkout
 
612
                $ bzr branch --switch ../repo/trunk ../repo/branched
 
613
                2>Branched 0 revisions.
 
614
                2>Tree is up to date at revision 0.
 
615
                2>Switched to branch:...branched...
 
616
                $ cd ..
 
617
                ''' % locals())
 
618
        bound_branch = branch.Branch.open_containing('checkout')[0]
 
619
        master_branch = branch.Branch.open_containing('repo/branched')[0]
 
620
        return (bound_branch, master_branch)
 
621
 
 
622
    def test_branch_switch_parent_lightweight(self):
 
623
        """Lightweight checkout using bzr branch --switch."""
 
624
        bb, mb = self._checkout_and_branch(option='--lightweight')
 
625
        self.assertParent('repo/trunk', bb)
 
626
        self.assertParent('repo/trunk', mb)
 
627
 
 
628
    def test_branch_switch_parent_heavyweight(self):
 
629
        """Heavyweight checkout using bzr branch --switch."""
 
630
        bb, mb = self._checkout_and_branch()
 
631
        self.assertParent('repo/trunk', bb)
 
632
        self.assertParent('repo/trunk', mb)