~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Martin
  • Date: 2010-05-03 20:57:39 UTC
  • mto: This revision was merged to the branch mainline in revision 5204.
  • Revision ID: gzlist@googlemail.com-20100503205739-n326zdvevv0rmruh
Retain original stack and error message when translating to ValueError in bencode

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2012 Canonical Ltd
 
1
# Copyright (C) 2006-2010 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,
26
25
    errors,
 
26
    repository,
27
27
    revision as _mod_revision,
28
 
    tests,
29
28
    )
30
29
from bzrlib.repofmt.knitrepo import RepositoryFormatKnit1
 
30
from bzrlib.tests.blackbox import ExternalBase
31
31
from bzrlib.tests import (
32
 
    fixtures,
 
32
    KnownFailure,
 
33
    HardlinkFeature,
33
34
    test_server,
34
35
    )
35
 
from bzrlib.tests.features import (
36
 
    HardlinkFeature,
37
 
    )
38
 
from bzrlib.tests.blackbox import test_switch
39
 
from bzrlib.tests.matchers import ContainsNoVfsCalls
40
36
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
41
 
from bzrlib.tests.script import run_script
42
37
from bzrlib.urlutils import local_path_to_url, strip_trailing_slash
43
38
from bzrlib.workingtree import WorkingTree
44
39
 
45
40
 
46
 
class TestBranch(tests.TestCaseWithTransport):
 
41
class TestBranch(ExternalBase):
47
42
 
48
 
    def example_branch(self, path='.', format=None):
49
 
        tree = self.make_branch_and_tree(path, format=format)
 
43
    def example_branch(self, path='.'):
 
44
        tree = self.make_branch_and_tree(path)
50
45
        self.build_tree_contents([(path + '/hello', 'foo')])
51
46
        tree.add('hello')
52
47
        tree.commit(message='setup')
53
48
        self.build_tree_contents([(path + '/goodbye', 'baz')])
54
49
        tree.add('goodbye')
55
50
        tree.commit(message='setup')
56
 
        return tree
57
51
 
58
52
    def test_branch(self):
59
53
        """Branch from one branch to another."""
65
59
        self.assertFalse(b._transport.has('branch-name'))
66
60
        b.bzrdir.open_workingtree().commit(message='foo', allow_pointless=True)
67
61
 
68
 
    def test_branch_no_to_location(self):
69
 
        """The to_location is derived from the source branch name."""
70
 
        os.mkdir("something")
71
 
        a = self.example_branch('something/a').branch
72
 
        self.run_bzr('branch something/a')
73
 
        b = branch.Branch.open('a')
74
 
        self.assertEquals(b.last_revision_info(), a.last_revision_info())
75
 
 
76
 
    def test_into_colocated(self):
77
 
        """Branch from a branch into a colocated branch."""
78
 
        self.example_branch('a')
79
 
        out, err = self.run_bzr(
80
 
            'init --format=development-colo file:b,branch=orig')
81
 
        self.assertEqual(
82
 
            """Created a lightweight checkout (format: development-colo)\n""",
83
 
            out)
84
 
        self.assertEqual('', err)
85
 
        out, err = self.run_bzr(
86
 
            'branch a file:b,branch=thiswasa')
87
 
        self.assertEqual('', out)
88
 
        self.assertEqual('Branched 2 revisions.\n', err)
89
 
        out, err = self.run_bzr('branches b')
90
 
        self.assertEqual("  orig\n  thiswasa\n", out)
91
 
        self.assertEqual('', err)
92
 
        out,err = self.run_bzr('branch a file:b,branch=orig', retcode=3)
93
 
        self.assertEqual('', out)
94
 
        self.assertEqual(
95
 
            'bzr: ERROR: Already a branch: "file:b,branch=orig".\n', err)
96
 
 
97
 
    def test_from_colocated(self):
98
 
        """Branch from a colocated branch into a regular branch."""
99
 
        tree = self.example_branch('a', format='development-colo')
100
 
        tree.bzrdir.create_branch(name='somecolo')
101
 
        out, err = self.run_bzr('branch %s,branch=somecolo' %
102
 
            local_path_to_url('a'))
103
 
        self.assertEqual('', out)
104
 
        self.assertEqual('Branched 0 revisions.\n', err)
105
 
        self.assertPathExists("somecolo")
106
 
 
107
 
    def test_branch_broken_pack(self):
108
 
        """branching with a corrupted pack file."""
109
 
        self.example_branch('a')
110
 
        # add some corruption
111
 
        packs_dir = 'a/.bzr/repository/packs/'
112
 
        fname = packs_dir + os.listdir(packs_dir)[0]
113
 
        with open(fname, 'rb+') as f:
114
 
            # Start from the end of the file to avoid choosing a place bigger
115
 
            # than the file itself.
116
 
            f.seek(-5, os.SEEK_END)
117
 
            c = f.read(1)
118
 
            f.seek(-5, os.SEEK_END)
119
 
            # Make sure we inject a value different than the one we just read
120
 
            if c == '\xFF':
121
 
                corrupt = '\x00'
122
 
            else:
123
 
                corrupt = '\xFF'
124
 
            f.write(corrupt) # make sure we corrupt something
125
 
        self.run_bzr_error(['Corruption while decompressing repository file'],
126
 
                            'branch a b', retcode=3)
127
 
 
128
62
    def test_branch_switch_no_branch(self):
129
63
        # No branch in the current directory:
130
64
        #  => new branch will be created, but switch fails
155
89
        #  => new branch will be created, but switch fails and the current
156
90
        #     branch is unmodified
157
91
        self.example_branch('a')
158
 
        tree = self.make_branch_and_tree('current')
159
 
        c1 = tree.commit('some diverged change')
 
92
        self.make_branch_and_tree('current')
160
93
        self.run_bzr_error(['Cannot switch a branch, only a checkout'],
161
94
            'branch --switch ../a ../b', working_dir='current')
162
95
        a = branch.Branch.open('a')
163
96
        b = branch.Branch.open('b')
164
97
        self.assertEqual(a.last_revision(), b.last_revision())
165
98
        work = branch.Branch.open('current')
166
 
        self.assertEqual(work.last_revision(), c1)
167
 
 
168
 
    def test_branch_into_empty_dir(self):
169
 
        t = self.example_branch('source')
170
 
        self.make_bzrdir('target')
171
 
        self.run_bzr("branch source target")
172
 
        self.assertEquals(2, len(t.branch.repository.all_revision_ids()))
 
99
        self.assertEqual(work.last_revision(), _mod_revision.NULL_REVISION)
173
100
 
174
101
    def test_branch_switch_checkout(self):
175
102
        # Checkout in the current directory:
176
103
        #  => new branch will be created and checkout bound to the new branch
177
104
        self.example_branch('a')
178
105
        self.run_bzr('checkout a current')
179
 
        out, err = self.run_bzr('branch --switch ../a ../b',
180
 
                                working_dir='current')
 
106
        out, err = self.run_bzr('branch --switch ../a ../b', working_dir='current')
181
107
        a = branch.Branch.open('a')
182
108
        b = branch.Branch.open('b')
183
109
        self.assertEqual(a.last_revision(), b.last_revision())
191
117
        #     the new branch
192
118
        self.example_branch('a')
193
119
        self.run_bzr('checkout --lightweight a current')
194
 
        out, err = self.run_bzr('branch --switch ../a ../b',
195
 
                                working_dir='current')
 
120
        out, err = self.run_bzr('branch --switch ../a ../b', working_dir='current')
196
121
        a = branch.Branch.open('a')
197
122
        b = branch.Branch.open('b')
198
123
        self.assertEqual(a.last_revision(), b.last_revision())
209
134
 
210
135
        def make_shared_tree(path):
211
136
            shared_repo.bzrdir.root_transport.mkdir(path)
212
 
            controldir.ControlDir.create_branch_convenience('repo/' + path)
 
137
            shared_repo.bzrdir.create_branch_convenience('repo/' + path)
213
138
            return WorkingTree.open('repo/' + path)
214
139
        tree_a = make_shared_tree('a')
215
140
        self.build_tree(['repo/a/file'])
249
174
        target_stat = os.stat('target/file1')
250
175
        self.assertEqual(source_stat, target_stat)
251
176
 
252
 
    def test_branch_files_from(self):
253
 
        source = self.make_branch_and_tree('source')
254
 
        self.build_tree(['source/file1'])
255
 
        source.add('file1')
256
 
        source.commit('added file')
257
 
        out, err = self.run_bzr('branch source target --files-from source')
258
 
        self.assertPathExists('target/file1')
259
 
 
260
 
    def test_branch_files_from_hardlink(self):
261
 
        self.requireFeature(HardlinkFeature)
262
 
        source = self.make_branch_and_tree('source')
263
 
        self.build_tree(['source/file1'])
264
 
        source.add('file1')
265
 
        source.commit('added file')
266
 
        source.bzrdir.sprout('second')
267
 
        out, err = self.run_bzr('branch source target --files-from second'
268
 
                                ' --hardlink')
269
 
        source_stat = os.stat('source/file1')
270
 
        second_stat = os.stat('second/file1')
271
 
        target_stat = os.stat('target/file1')
272
 
        self.assertNotEqual(source_stat, target_stat)
273
 
        self.assertEqual(second_stat, target_stat)
274
 
 
275
177
    def test_branch_standalone(self):
276
178
        shared_repo = self.make_repository('repo', shared=True)
277
179
        self.example_branch('source')
284
186
    def test_branch_no_tree(self):
285
187
        self.example_branch('source')
286
188
        self.run_bzr('branch --no-tree source target')
287
 
        self.assertPathDoesNotExist('target/hello')
288
 
        self.assertPathDoesNotExist('target/goodbye')
 
189
        self.failIfExists('target/hello')
 
190
        self.failIfExists('target/goodbye')
289
191
 
290
192
    def test_branch_into_existing_dir(self):
291
193
        self.example_branch('a')
301
203
        # force operation
302
204
        self.run_bzr('branch a b --use-existing-dir')
303
205
        # check conflicts
304
 
        self.assertPathExists('b/hello.moved')
305
 
        self.assertPathDoesNotExist('b/godbye.moved')
 
206
        self.failUnlessExists('b/hello.moved')
 
207
        self.failIfExists('b/godbye.moved')
306
208
        # we can't branch into branch
307
209
        out,err = self.run_bzr('branch a b --use-existing-dir', retcode=3)
308
210
        self.assertEqual('', out)
345
247
        self.run_bzr('checkout --lightweight a b')
346
248
        self.assertLength(2, calls)
347
249
 
348
 
    def test_branch_fetches_all_tags(self):
349
 
        builder = self.make_branch_builder('source')
350
 
        source = fixtures.build_branch_with_non_ancestral_rev(builder)
351
 
        source.tags.set_tag('tag-a', 'rev-2')
352
 
        source.get_config_stack().set('branch.fetch_tags', True)
353
 
        # Now source has a tag not in its ancestry.  Make a branch from it.
354
 
        self.run_bzr('branch source new-branch')
355
 
        new_branch = branch.Branch.open('new-branch')
356
 
        # The tag is present, and so is its revision.
357
 
        self.assertEqual('rev-2', new_branch.tags.lookup_tag('tag-a'))
358
 
        new_branch.repository.get_revision('rev-2')
359
 
 
360
 
 
361
 
class TestBranchStacked(tests.TestCaseWithTransport):
 
250
 
 
251
class TestBranchStacked(ExternalBase):
362
252
    """Tests for branch --stacked"""
363
253
 
364
254
    def assertRevisionInRepository(self, repo_path, revid):
365
 
        """Check that a revision is in a repo, disregarding stacking."""
366
 
        repo = controldir.ControlDir.open(repo_path).open_repository()
 
255
        """Check that a revision is in a repository, disregarding stacking."""
 
256
        repo = bzrdir.BzrDir.open(repo_path).open_repository()
367
257
        self.assertTrue(repo.has_revision(revid))
368
258
 
369
259
    def assertRevisionNotInRepository(self, repo_path, revid):
370
 
        """Check that a revision is not in a repo, disregarding stacking."""
371
 
        repo = controldir.ControlDir.open(repo_path).open_repository()
 
260
        """Check that a revision is not in a repository, disregarding stacking."""
 
261
        repo = bzrdir.BzrDir.open(repo_path).open_repository()
372
262
        self.assertFalse(repo.has_revision(revid))
373
263
 
374
264
    def assertRevisionsInBranchRepository(self, revid_list, branch_path):
394
284
        # mainline.
395
285
        out, err = self.run_bzr(['branch', 'branch', 'newbranch'])
396
286
        self.assertEqual('', out)
397
 
        self.assertEqual('Branched 2 revisions.\n',
 
287
        self.assertEqual('Branched 2 revision(s).\n',
398
288
            err)
399
289
        # it should have preserved the branch format, and so it should be
400
290
        # capable of supporting stacking, but not actually have a stacked_on
401
291
        # branch configured
402
292
        self.assertRaises(errors.NotStacked,
403
 
            controldir.ControlDir.open('newbranch').open_branch().get_stacked_on_url)
 
293
            bzrdir.BzrDir.open('newbranch').open_branch().get_stacked_on_url)
404
294
 
405
295
    def test_branch_stacked_branch_stacked(self):
406
296
        """Asking to stack on a stacked branch does work"""
445
335
            trunk_tree.branch.base, err)
446
336
        self.assertRevisionNotInRepository('newbranch', original_revid)
447
337
        new_branch = branch.Branch.open('newbranch')
448
 
        self.assertEqual(trunk_tree.branch.base,
449
 
                         new_branch.get_stacked_on_url())
 
338
        self.assertEqual(trunk_tree.branch.base, new_branch.get_stacked_on_url())
450
339
 
451
340
    def test_branch_stacked_from_smart_server(self):
452
341
        # We can branch stacking on a smart server
487
376
            err)
488
377
 
489
378
 
490
 
class TestSmartServerBranching(tests.TestCaseWithTransport):
 
379
class TestSmartServerBranching(ExternalBase):
491
380
 
492
381
    def test_branch_from_trivial_branch_to_same_server_branch_acceptance(self):
493
382
        self.setup_smart_server_with_call_log()
502
391
        # being too low. If rpc_count increases, more network roundtrips have
503
392
        # become necessary for this use case. Please do not adjust this number
504
393
        # upwards without agreement from bzr's network support maintainers.
505
 
        self.assertLength(2, self.hpss_connections)
506
 
        self.assertLength(33, self.hpss_calls)
507
 
        self.expectFailure("branching to the same branch requires VFS access",
508
 
            self.assertThat, self.hpss_calls, ContainsNoVfsCalls)
 
394
        self.assertLength(38, self.hpss_calls)
509
395
 
510
396
    def test_branch_from_trivial_branch_streaming_acceptance(self):
511
397
        self.setup_smart_server_with_call_log()
520
406
        # being too low. If rpc_count increases, more network roundtrips have
521
407
        # become necessary for this use case. Please do not adjust this number
522
408
        # upwards without agreement from bzr's network support maintainers.
523
 
        self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
524
409
        self.assertLength(10, self.hpss_calls)
525
 
        self.assertLength(1, self.hpss_connections)
526
410
 
527
411
    def test_branch_from_trivial_stacked_branch_streaming_acceptance(self):
528
412
        self.setup_smart_server_with_call_log()
543
427
        # become necessary for this use case. Please do not adjust this number
544
428
        # upwards without agreement from bzr's network support maintainers.
545
429
        self.assertLength(15, self.hpss_calls)
546
 
        self.assertLength(1, self.hpss_connections)
547
 
        self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
548
 
 
549
 
    def test_branch_from_branch_with_tags(self):
550
 
        self.setup_smart_server_with_call_log()
551
 
        builder = self.make_branch_builder('source')
552
 
        source = fixtures.build_branch_with_non_ancestral_rev(builder)
553
 
        source.get_config_stack().set('branch.fetch_tags', True)
554
 
        source.tags.set_tag('tag-a', 'rev-2')
555
 
        source.tags.set_tag('tag-missing', 'missing-rev')
556
 
        # Now source has a tag not in its ancestry.  Make a branch from it.
557
 
        self.reset_smart_call_log()
558
 
        out, err = self.run_bzr(['branch', self.get_url('source'), 'target'])
559
 
        # This figure represent the amount of work to perform this use case. It
560
 
        # is entirely ok to reduce this number if a test fails due to rpc_count
561
 
        # being too low. If rpc_count increases, more network roundtrips have
562
 
        # become necessary for this use case. Please do not adjust this number
563
 
        # upwards without agreement from bzr's network support maintainers.
564
 
        self.assertLength(10, self.hpss_calls)
565
 
        self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
566
 
        self.assertLength(1, self.hpss_connections)
567
 
 
568
 
    def test_branch_to_stacked_from_trivial_branch_streaming_acceptance(self):
569
 
        self.setup_smart_server_with_call_log()
570
 
        t = self.make_branch_and_tree('from')
571
 
        for count in range(9):
572
 
            t.commit(message='commit %d' % count)
573
 
        self.reset_smart_call_log()
574
 
        out, err = self.run_bzr(['branch', '--stacked', self.get_url('from'),
575
 
            'local-target'])
576
 
        # XXX: the number of hpss calls for this case isn't deterministic yet,
577
 
        # so we can't easily assert about the number of calls.
578
 
        #self.assertLength(XXX, self.hpss_calls)
579
 
        # We can assert that none of the calls were readv requests for rix
580
 
        # files, though (demonstrating that at least get_parent_map calls are
581
 
        # not using VFS RPCs).
582
 
        readvs_of_rix_files = [
583
 
            c for c in self.hpss_calls
584
 
            if c.call.method == 'readv' and c.call.args[-1].endswith('.rix')]
585
 
        self.assertLength(1, self.hpss_connections)
586
 
        self.assertLength(0, readvs_of_rix_files)
587
 
        self.expectFailure("branching to stacked requires VFS access",
588
 
            self.assertThat, self.hpss_calls, ContainsNoVfsCalls)
589
430
 
590
431
 
591
432
class TestRemoteBranch(TestCaseWithSFTPServer):
611
452
        # Ensure that no working tree what created remotely
612
453
        self.assertFalse(t.has('remote/file'))
613
454
 
614
 
 
615
 
class TestDeprecatedAliases(tests.TestCaseWithTransport):
616
 
 
617
 
    def test_deprecated_aliases(self):
618
 
        """bzr branch can be called clone or get, but those names are
619
 
        deprecated.
620
 
 
621
 
        See bug 506265.
622
 
        """
623
 
        for command in ['clone', 'get']:
624
 
            run_script(self, """
625
 
            $ bzr %(command)s A B
626
 
            2>The command 'bzr %(command)s' has been deprecated in bzr 2.4. Please use 'bzr branch' instead.
627
 
            2>bzr: ERROR: Not a branch...
628
 
            """ % locals())
629
 
 
630
 
 
631
 
class TestBranchParentLocation(test_switch.TestSwitchParentLocationBase):
632
 
 
633
 
    def _checkout_and_branch(self, option=''):
634
 
        self.script_runner.run_script(self, '''
635
 
                $ bzr checkout %(option)s repo/trunk checkout
636
 
                $ cd checkout
637
 
                $ bzr branch --switch ../repo/trunk ../repo/branched
638
 
                2>Branched 0 revisions.
639
 
                2>Tree is up to date at revision 0.
640
 
                2>Switched to branch:...branched...
641
 
                $ cd ..
642
 
                ''' % locals())
643
 
        bound_branch = branch.Branch.open_containing('checkout')[0]
644
 
        master_branch = branch.Branch.open_containing('repo/branched')[0]
645
 
        return (bound_branch, master_branch)
646
 
 
647
 
    def test_branch_switch_parent_lightweight(self):
648
 
        """Lightweight checkout using bzr branch --switch."""
649
 
        bb, mb = self._checkout_and_branch(option='--lightweight')
650
 
        self.assertParent('repo/trunk', bb)
651
 
        self.assertParent('repo/trunk', mb)
652
 
 
653
 
    def test_branch_switch_parent_heavyweight(self):
654
 
        """Heavyweight checkout using bzr branch --switch."""
655
 
        bb, mb = self._checkout_and_branch()
656
 
        self.assertParent('repo/trunk', bb)
657
 
        self.assertParent('repo/trunk', mb)