/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 bzrlib/tests/blackbox/test_branch.py

  • Committer: Vincent Ladeuil
  • Date: 2012-01-05 14:26:58 UTC
  • mto: This revision was merged to the branch mainline in revision 6468.
  • Revision ID: v.ladeuil+lp@free.fr-20120105142658-vek3v6pzlxb751s2
Tests passing for a first rough version of a cached branch config store. The changes here are too invasive and several parallel proposals have been made. 

@only_raises is evil and gave a hard time since any exception during
save_changes() was swallowed.

Possible improvements: 

- add some needs_write_lock decorators to crucial
  methods (_set_config_location ?) but keep locking the branch at higher levels

- decorate branch.unlock to call stack.save if last_lock() it True
  outside of @only_raises scope (evil decorator)

- add @needs_write_lock to stack.set and stack.remove (will probably get
  rid of most testing issues) we probably need a specialized decorator
  that can relay to the store and from there to the branch or whatever is
  needed. This will also helps bzr config to get it right. The
  get_mutable_section trick should not be needed anymore either.

- decorate branch.unlock to call stack.save if last_lock() it True outside
  of @only_raises scope (evil decorator)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2006-2012 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
 
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
 
class TestBranch(ExternalBase):
 
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("  orig\n  thiswasa\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
103
160
        #  => new branch will be created and checkout bound to the new branch
104
161
        self.example_branch('a')
105
162
        self.run_bzr('checkout a current')
106
 
        out, err = self.run_bzr('branch --switch ../a ../b', working_dir='current')
 
163
        out, err = self.run_bzr('branch --switch ../a ../b',
 
164
                                working_dir='current')
107
165
        a = branch.Branch.open('a')
108
166
        b = branch.Branch.open('b')
109
167
        self.assertEqual(a.last_revision(), b.last_revision())
134
192
 
135
193
        def make_shared_tree(path):
136
194
            shared_repo.bzrdir.root_transport.mkdir(path)
137
 
            shared_repo.bzrdir.create_branch_convenience('repo/' + path)
 
195
            controldir.ControlDir.create_branch_convenience('repo/' + path)
138
196
            return WorkingTree.open('repo/' + path)
139
197
        tree_a = make_shared_tree('a')
140
198
        self.build_tree(['repo/a/file'])
174
232
        target_stat = os.stat('target/file1')
175
233
        self.assertEqual(source_stat, target_stat)
176
234
 
 
235
    def test_branch_files_from(self):
 
236
        source = self.make_branch_and_tree('source')
 
237
        self.build_tree(['source/file1'])
 
238
        source.add('file1')
 
239
        source.commit('added file')
 
240
        out, err = self.run_bzr('branch source target --files-from source')
 
241
        self.assertPathExists('target/file1')
 
242
 
 
243
    def test_branch_files_from_hardlink(self):
 
244
        self.requireFeature(HardlinkFeature)
 
245
        source = self.make_branch_and_tree('source')
 
246
        self.build_tree(['source/file1'])
 
247
        source.add('file1')
 
248
        source.commit('added file')
 
249
        source.bzrdir.sprout('second')
 
250
        out, err = self.run_bzr('branch source target --files-from second'
 
251
                                ' --hardlink')
 
252
        source_stat = os.stat('source/file1')
 
253
        second_stat = os.stat('second/file1')
 
254
        target_stat = os.stat('target/file1')
 
255
        self.assertNotEqual(source_stat, target_stat)
 
256
        self.assertEqual(second_stat, target_stat)
 
257
 
177
258
    def test_branch_standalone(self):
178
259
        shared_repo = self.make_repository('repo', shared=True)
179
260
        self.example_branch('source')
186
267
    def test_branch_no_tree(self):
187
268
        self.example_branch('source')
188
269
        self.run_bzr('branch --no-tree source target')
189
 
        self.failIfExists('target/hello')
190
 
        self.failIfExists('target/goodbye')
 
270
        self.assertPathDoesNotExist('target/hello')
 
271
        self.assertPathDoesNotExist('target/goodbye')
191
272
 
192
273
    def test_branch_into_existing_dir(self):
193
274
        self.example_branch('a')
203
284
        # force operation
204
285
        self.run_bzr('branch a b --use-existing-dir')
205
286
        # check conflicts
206
 
        self.failUnlessExists('b/hello.moved')
207
 
        self.failIfExists('b/godbye.moved')
 
287
        self.assertPathExists('b/hello.moved')
 
288
        self.assertPathDoesNotExist('b/godbye.moved')
208
289
        # we can't branch into branch
209
290
        out,err = self.run_bzr('branch a b --use-existing-dir', retcode=3)
210
291
        self.assertEqual('', out)
247
328
        self.run_bzr('checkout --lightweight a b')
248
329
        self.assertLength(2, calls)
249
330
 
250
 
 
251
 
class TestBranchStacked(ExternalBase):
 
331
    def test_branch_fetches_all_tags(self):
 
332
        builder = self.make_branch_builder('source')
 
333
        source = fixtures.build_branch_with_non_ancestral_rev(builder)
 
334
        source.tags.set_tag('tag-a', 'rev-2')
 
335
        source.get_config().set_user_option('branch.fetch_tags', 'True')
 
336
        # Now source has a tag not in its ancestry.  Make a branch from it.
 
337
        self.run_bzr('branch source new-branch')
 
338
        new_branch = branch.Branch.open('new-branch')
 
339
        # The tag is present, and so is its revision.
 
340
        self.assertEqual('rev-2', new_branch.tags.lookup_tag('tag-a'))
 
341
        new_branch.repository.get_revision('rev-2')
 
342
 
 
343
 
 
344
class TestBranchStacked(TestCaseWithTransport):
252
345
    """Tests for branch --stacked"""
253
346
 
254
347
    def assertRevisionInRepository(self, repo_path, revid):
284
377
        # mainline.
285
378
        out, err = self.run_bzr(['branch', 'branch', 'newbranch'])
286
379
        self.assertEqual('', out)
287
 
        self.assertEqual('Branched 2 revision(s).\n',
 
380
        self.assertEqual('Branched 2 revisions.\n',
288
381
            err)
289
382
        # it should have preserved the branch format, and so it should be
290
383
        # capable of supporting stacking, but not actually have a stacked_on
376
469
            err)
377
470
 
378
471
 
379
 
class TestSmartServerBranching(ExternalBase):
 
472
class TestSmartServerBranching(TestCaseWithTransport):
380
473
 
381
474
    def test_branch_from_trivial_branch_to_same_server_branch_acceptance(self):
382
475
        self.setup_smart_server_with_call_log()
391
484
        # being too low. If rpc_count increases, more network roundtrips have
392
485
        # become necessary for this use case. Please do not adjust this number
393
486
        # upwards without agreement from bzr's network support maintainers.
394
 
        self.assertLength(38, self.hpss_calls)
 
487
        self.assertLength(33, self.hpss_calls)
 
488
        self.expectFailure("branching to the same branch requires VFS access",
 
489
            self.assertThat, self.hpss_calls, ContainsNoVfsCalls)
395
490
 
396
491
    def test_branch_from_trivial_branch_streaming_acceptance(self):
397
492
        self.setup_smart_server_with_call_log()
406
501
        # being too low. If rpc_count increases, more network roundtrips have
407
502
        # become necessary for this use case. Please do not adjust this number
408
503
        # upwards without agreement from bzr's network support maintainers.
 
504
        self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
409
505
        self.assertLength(10, self.hpss_calls)
410
506
 
411
507
    def test_branch_from_trivial_stacked_branch_streaming_acceptance(self):
427
523
        # become necessary for this use case. Please do not adjust this number
428
524
        # upwards without agreement from bzr's network support maintainers.
429
525
        self.assertLength(15, self.hpss_calls)
 
526
        self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
 
527
 
 
528
    def test_branch_from_branch_with_tags(self):
 
529
        self.setup_smart_server_with_call_log()
 
530
        builder = self.make_branch_builder('source')
 
531
        source = fixtures.build_branch_with_non_ancestral_rev(builder)
 
532
        source.get_config().set_user_option('branch.fetch_tags', 'True')
 
533
        source.tags.set_tag('tag-a', 'rev-2')
 
534
        source.tags.set_tag('tag-missing', 'missing-rev')
 
535
        # Now source has a tag not in its ancestry.  Make a branch from it.
 
536
        self.reset_smart_call_log()
 
537
        out, err = self.run_bzr(['branch', self.get_url('source'), 'target'])
 
538
        # This figure represent the amount of work to perform this use case. It
 
539
        # is entirely ok to reduce this number if a test fails due to rpc_count
 
540
        # being too low. If rpc_count increases, more network roundtrips have
 
541
        # become necessary for this use case. Please do not adjust this number
 
542
        # upwards without agreement from bzr's network support maintainers.
 
543
        self.assertLength(10, self.hpss_calls)
 
544
        self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
 
545
 
 
546
    def test_branch_to_stacked_from_trivial_branch_streaming_acceptance(self):
 
547
        self.setup_smart_server_with_call_log()
 
548
        t = self.make_branch_and_tree('from')
 
549
        for count in range(9):
 
550
            t.commit(message='commit %d' % count)
 
551
        self.reset_smart_call_log()
 
552
        out, err = self.run_bzr(['branch', '--stacked', self.get_url('from'),
 
553
            'local-target'])
 
554
        # XXX: the number of hpss calls for this case isn't deterministic yet,
 
555
        # so we can't easily assert about the number of calls.
 
556
        #self.assertLength(XXX, self.hpss_calls)
 
557
        # We can assert that none of the calls were readv requests for rix
 
558
        # files, though (demonstrating that at least get_parent_map calls are
 
559
        # not using VFS RPCs).
 
560
        readvs_of_rix_files = [
 
561
            c for c in self.hpss_calls
 
562
            if c.call.method == 'readv' and c.call.args[-1].endswith('.rix')]
 
563
        self.assertLength(0, readvs_of_rix_files)
 
564
        self.expectFailure("branching to stacked requires VFS access",
 
565
            self.assertThat, self.hpss_calls, ContainsNoVfsCalls)
430
566
 
431
567
 
432
568
class TestRemoteBranch(TestCaseWithSFTPServer):
452
588
        # Ensure that no working tree what created remotely
453
589
        self.assertFalse(t.has('remote/file'))
454
590
 
 
591
 
 
592
class TestDeprecatedAliases(TestCaseWithTransport):
 
593
 
 
594
    def test_deprecated_aliases(self):
 
595
        """bzr branch can be called clone or get, but those names are deprecated.
 
596
 
 
597
        See bug 506265.
 
598
        """
 
599
        for command in ['clone', 'get']:
 
600
            run_script(self, """
 
601
            $ bzr %(command)s A B
 
602
            2>The command 'bzr %(command)s' has been deprecated in bzr 2.4. Please use 'bzr branch' instead.
 
603
            2>bzr: ERROR: Not a branch...
 
604
            """ % locals())
 
605
 
 
606
 
 
607
class TestBranchParentLocation(test_switch.TestSwitchParentLocationBase):
 
608
 
 
609
    def _checkout_and_branch(self, option=''):
 
610
        self.script_runner.run_script(self, '''
 
611
                $ bzr checkout %(option)s repo/trunk checkout
 
612
                $ cd checkout
 
613
                $ bzr branch --switch ../repo/trunk ../repo/branched
 
614
                2>Branched 0 revisions.
 
615
                2>Tree is up to date at revision 0.
 
616
                2>Switched to branch:...branched...
 
617
                $ cd ..
 
618
                ''' % locals())
 
619
        bound_branch = branch.Branch.open_containing('checkout')[0]
 
620
        master_branch = branch.Branch.open_containing('repo/branched')[0]
 
621
        return (bound_branch, master_branch)
 
622
 
 
623
    def test_branch_switch_parent_lightweight(self):
 
624
        """Lightweight checkout using bzr branch --switch."""
 
625
        bb, mb = self._checkout_and_branch(option='--lightweight')
 
626
        self.assertParent('repo/trunk', bb)
 
627
        self.assertParent('repo/trunk', mb)
 
628
 
 
629
    def test_branch_switch_parent_heavyweight(self):
 
630
        """Heavyweight checkout using bzr branch --switch."""
 
631
        bb, mb = self._checkout_and_branch()
 
632
        self.assertParent('repo/trunk', bb)
 
633
        self.assertParent('repo/trunk', mb)