~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2011-06-30 18:28:17 UTC
  • mfrom: (5967.10.2 test-cat)
  • Revision ID: pqm@pqm.ubuntu.com-20110630182817-83a5q9r9rxfkdn8r
(mbp) don't use subprocesses for testing cat (Martin Pool)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2012 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,
26
25
    errors,
27
26
    revision as _mod_revision,
28
 
    tests,
29
27
    )
30
28
from bzrlib.repofmt.knitrepo import RepositoryFormatKnit1
 
29
from bzrlib.tests import TestCaseWithTransport
31
30
from bzrlib.tests import (
32
31
    fixtures,
 
32
    HardlinkFeature,
 
33
    script,
33
34
    test_server,
34
35
    )
35
 
from bzrlib.tests.features import (
36
 
    HardlinkFeature,
37
 
    )
38
36
from bzrlib.tests.blackbox import test_switch
39
 
from bzrlib.tests.matchers import ContainsNoVfsCalls
40
37
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
41
38
from bzrlib.tests.script import run_script
42
39
from bzrlib.urlutils import local_path_to_url, strip_trailing_slash
43
40
from bzrlib.workingtree import WorkingTree
44
41
 
45
42
 
46
 
class TestBranch(tests.TestCaseWithTransport):
 
43
class TestBranch(TestCaseWithTransport):
47
44
 
48
 
    def example_branch(self, path='.', format=None):
49
 
        tree = self.make_branch_and_tree(path, format=format)
 
45
    def example_branch(self, path='.'):
 
46
        tree = self.make_branch_and_tree(path)
50
47
        self.build_tree_contents([(path + '/hello', 'foo')])
51
48
        tree.add('hello')
52
49
        tree.commit(message='setup')
53
50
        self.build_tree_contents([(path + '/goodbye', 'baz')])
54
51
        tree.add('goodbye')
55
52
        tree.commit(message='setup')
56
 
        return tree
57
53
 
58
54
    def test_branch(self):
59
55
        """Branch from one branch to another."""
65
61
        self.assertFalse(b._transport.has('branch-name'))
66
62
        b.bzrdir.open_workingtree().commit(message='foo', allow_pointless=True)
67
63
 
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
64
    def test_branch_broken_pack(self):
108
65
        """branching with a corrupted pack file."""
109
66
        self.example_branch('a')
110
 
        # add some corruption
111
 
        packs_dir = 'a/.bzr/repository/packs/'
112
 
        fname = packs_dir + os.listdir(packs_dir)[0]
 
67
        #now add some random corruption
 
68
        fname = 'a/.bzr/repository/packs/' + os.listdir('a/.bzr/repository/packs')[0]
113
69
        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'],
 
70
            f.seek(750)
 
71
            f.write("\xff")
 
72
        self.run_bzr_error(['Corruption while decompressing repository file'], 
126
73
                            'branch a b', retcode=3)
127
74
 
128
75
    def test_branch_switch_no_branch(self):
155
102
        #  => new branch will be created, but switch fails and the current
156
103
        #     branch is unmodified
157
104
        self.example_branch('a')
158
 
        tree = self.make_branch_and_tree('current')
159
 
        c1 = tree.commit('some diverged change')
 
105
        self.make_branch_and_tree('current')
160
106
        self.run_bzr_error(['Cannot switch a branch, only a checkout'],
161
107
            'branch --switch ../a ../b', working_dir='current')
162
108
        a = branch.Branch.open('a')
163
109
        b = branch.Branch.open('b')
164
110
        self.assertEqual(a.last_revision(), b.last_revision())
165
111
        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()))
 
112
        self.assertEqual(work.last_revision(), _mod_revision.NULL_REVISION)
173
113
 
174
114
    def test_branch_switch_checkout(self):
175
115
        # Checkout in the current directory:
176
116
        #  => new branch will be created and checkout bound to the new branch
177
117
        self.example_branch('a')
178
118
        self.run_bzr('checkout a current')
179
 
        out, err = self.run_bzr('branch --switch ../a ../b',
180
 
                                working_dir='current')
 
119
        out, err = self.run_bzr('branch --switch ../a ../b', working_dir='current')
181
120
        a = branch.Branch.open('a')
182
121
        b = branch.Branch.open('b')
183
122
        self.assertEqual(a.last_revision(), b.last_revision())
191
130
        #     the new branch
192
131
        self.example_branch('a')
193
132
        self.run_bzr('checkout --lightweight a current')
194
 
        out, err = self.run_bzr('branch --switch ../a ../b',
195
 
                                working_dir='current')
 
133
        out, err = self.run_bzr('branch --switch ../a ../b', working_dir='current')
196
134
        a = branch.Branch.open('a')
197
135
        b = branch.Branch.open('b')
198
136
        self.assertEqual(a.last_revision(), b.last_revision())
209
147
 
210
148
        def make_shared_tree(path):
211
149
            shared_repo.bzrdir.root_transport.mkdir(path)
212
 
            controldir.ControlDir.create_branch_convenience('repo/' + path)
 
150
            shared_repo.bzrdir.create_branch_convenience('repo/' + path)
213
151
            return WorkingTree.open('repo/' + path)
214
152
        tree_a = make_shared_tree('a')
215
153
        self.build_tree(['repo/a/file'])
349
287
        builder = self.make_branch_builder('source')
350
288
        source = fixtures.build_branch_with_non_ancestral_rev(builder)
351
289
        source.tags.set_tag('tag-a', 'rev-2')
352
 
        source.get_config_stack().set('branch.fetch_tags', True)
353
290
        # Now source has a tag not in its ancestry.  Make a branch from it.
354
291
        self.run_bzr('branch source new-branch')
355
292
        new_branch = branch.Branch.open('new-branch')
358
295
        new_branch.repository.get_revision('rev-2')
359
296
 
360
297
 
361
 
class TestBranchStacked(tests.TestCaseWithTransport):
 
298
class TestBranchStacked(TestCaseWithTransport):
362
299
    """Tests for branch --stacked"""
363
300
 
364
301
    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()
 
302
        """Check that a revision is in a repository, disregarding stacking."""
 
303
        repo = bzrdir.BzrDir.open(repo_path).open_repository()
367
304
        self.assertTrue(repo.has_revision(revid))
368
305
 
369
306
    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()
 
307
        """Check that a revision is not in a repository, disregarding stacking."""
 
308
        repo = bzrdir.BzrDir.open(repo_path).open_repository()
372
309
        self.assertFalse(repo.has_revision(revid))
373
310
 
374
311
    def assertRevisionsInBranchRepository(self, revid_list, branch_path):
394
331
        # mainline.
395
332
        out, err = self.run_bzr(['branch', 'branch', 'newbranch'])
396
333
        self.assertEqual('', out)
397
 
        self.assertEqual('Branched 2 revisions.\n',
 
334
        self.assertEqual('Branched 2 revision(s).\n',
398
335
            err)
399
336
        # it should have preserved the branch format, and so it should be
400
337
        # capable of supporting stacking, but not actually have a stacked_on
401
338
        # branch configured
402
339
        self.assertRaises(errors.NotStacked,
403
 
            controldir.ControlDir.open('newbranch').open_branch().get_stacked_on_url)
 
340
            bzrdir.BzrDir.open('newbranch').open_branch().get_stacked_on_url)
404
341
 
405
342
    def test_branch_stacked_branch_stacked(self):
406
343
        """Asking to stack on a stacked branch does work"""
445
382
            trunk_tree.branch.base, err)
446
383
        self.assertRevisionNotInRepository('newbranch', original_revid)
447
384
        new_branch = branch.Branch.open('newbranch')
448
 
        self.assertEqual(trunk_tree.branch.base,
449
 
                         new_branch.get_stacked_on_url())
 
385
        self.assertEqual(trunk_tree.branch.base, new_branch.get_stacked_on_url())
450
386
 
451
387
    def test_branch_stacked_from_smart_server(self):
452
388
        # We can branch stacking on a smart server
487
423
            err)
488
424
 
489
425
 
490
 
class TestSmartServerBranching(tests.TestCaseWithTransport):
 
426
class TestSmartServerBranching(TestCaseWithTransport):
491
427
 
492
428
    def test_branch_from_trivial_branch_to_same_server_branch_acceptance(self):
493
429
        self.setup_smart_server_with_call_log()
502
438
        # being too low. If rpc_count increases, more network roundtrips have
503
439
        # become necessary for this use case. Please do not adjust this number
504
440
        # 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)
 
441
        self.assertLength(36, self.hpss_calls)
509
442
 
510
443
    def test_branch_from_trivial_branch_streaming_acceptance(self):
511
444
        self.setup_smart_server_with_call_log()
520
453
        # being too low. If rpc_count increases, more network roundtrips have
521
454
        # become necessary for this use case. Please do not adjust this number
522
455
        # upwards without agreement from bzr's network support maintainers.
523
 
        self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
524
 
        self.assertLength(10, self.hpss_calls)
525
 
        self.assertLength(1, self.hpss_connections)
 
456
        self.assertLength(9, self.hpss_calls)
526
457
 
527
458
    def test_branch_from_trivial_stacked_branch_streaming_acceptance(self):
528
459
        self.setup_smart_server_with_call_log()
542
473
        # being too low. If rpc_count increases, more network roundtrips have
543
474
        # become necessary for this use case. Please do not adjust this number
544
475
        # upwards without agreement from bzr's network support maintainers.
545
 
        self.assertLength(15, self.hpss_calls)
546
 
        self.assertLength(1, self.hpss_connections)
547
 
        self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
 
476
        self.assertLength(14, self.hpss_calls)
548
477
 
549
478
    def test_branch_from_branch_with_tags(self):
550
479
        self.setup_smart_server_with_call_log()
551
480
        builder = self.make_branch_builder('source')
552
481
        source = fixtures.build_branch_with_non_ancestral_rev(builder)
553
 
        source.get_config_stack().set('branch.fetch_tags', True)
554
482
        source.tags.set_tag('tag-a', 'rev-2')
555
483
        source.tags.set_tag('tag-missing', 'missing-rev')
556
484
        # Now source has a tag not in its ancestry.  Make a branch from it.
561
489
        # being too low. If rpc_count increases, more network roundtrips have
562
490
        # become necessary for this use case. Please do not adjust this number
563
491
        # 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)
 
492
        self.assertLength(9, self.hpss_calls)
567
493
 
568
494
    def test_branch_to_stacked_from_trivial_branch_streaming_acceptance(self):
569
495
        self.setup_smart_server_with_call_log()
582
508
        readvs_of_rix_files = [
583
509
            c for c in self.hpss_calls
584
510
            if c.call.method == 'readv' and c.call.args[-1].endswith('.rix')]
585
 
        self.assertLength(1, self.hpss_connections)
586
511
        self.assertLength(0, readvs_of_rix_files)
587
 
        self.expectFailure("branching to stacked requires VFS access",
588
 
            self.assertThat, self.hpss_calls, ContainsNoVfsCalls)
589
512
 
590
513
 
591
514
class TestRemoteBranch(TestCaseWithSFTPServer):
612
535
        self.assertFalse(t.has('remote/file'))
613
536
 
614
537
 
615
 
class TestDeprecatedAliases(tests.TestCaseWithTransport):
 
538
class TestDeprecatedAliases(TestCaseWithTransport):
616
539
 
617
540
    def test_deprecated_aliases(self):
618
 
        """bzr branch can be called clone or get, but those names are
619
 
        deprecated.
 
541
        """bzr branch can be called clone or get, but those names are deprecated.
620
542
 
621
543
        See bug 506265.
622
544
        """
635
557
                $ bzr checkout %(option)s repo/trunk checkout
636
558
                $ cd checkout
637
559
                $ bzr branch --switch ../repo/trunk ../repo/branched
638
 
                2>Branched 0 revisions.
 
560
                2>Branched 0 revision(s).
639
561
                2>Tree is up to date at revision 0.
640
562
                2>Switched to branch:...branched...
641
563
                $ cd ..