~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Wouter van Heyst
  • Date: 2012-01-25 21:13:15 UTC
  • mto: (6437.3.27 2.5)
  • mto: This revision was merged to the branch mainline in revision 6451.
  • Revision ID: larstiq@larstiq.dyndns.org-20120125211315-ch74doua37bqet0i
mechanically replace file().write() pattern with a with-keyword version

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-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
    tests,
28
29
    )
29
30
from bzrlib.repofmt.knitrepo import RepositoryFormatKnit1
30
 
from bzrlib.tests.blackbox import ExternalBase
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(tests.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(
 
87
            'bzr: ERROR: Already a branch: "file:b,branch=orig".\n', err)
 
88
 
 
89
    def test_from_colocated(self):
 
90
        """Branch from a colocated branch into a regular branch."""
 
91
        tree = self.example_branch('a', format='development-colo')
 
92
        tree.bzrdir.create_branch(name='somecolo')
 
93
        out, err = self.run_bzr('branch %s,branch=somecolo' %
 
94
            local_path_to_url('a'))
 
95
        self.assertEqual('', out)
 
96
        self.assertEqual('Branched 0 revisions.\n', err)
 
97
        self.assertPathExists("somecolo")
 
98
 
 
99
    def test_branch_broken_pack(self):
 
100
        """branching with a corrupted pack file."""
 
101
        self.example_branch('a')
 
102
        # add some corruption
 
103
        packs_dir = 'a/.bzr/repository/packs/'
 
104
        fname = packs_dir + os.listdir(packs_dir)[0]
 
105
        with open(fname, 'rb+') as f:
 
106
            # Start from the end of the file to avoid choosing a place bigger
 
107
            # than the file itself.
 
108
            f.seek(-5, os.SEEK_END)
 
109
            c = f.read(1)
 
110
            f.seek(-5, os.SEEK_END)
 
111
            # Make sure we inject a value different than the one we just read
 
112
            if c == '\xFF':
 
113
                corrupt = '\x00'
 
114
            else:
 
115
                corrupt = '\xFF'
 
116
            f.write(corrupt) # make sure we corrupt something
 
117
        self.run_bzr_error(['Corruption while decompressing repository file'],
 
118
                            'branch a b', retcode=3)
 
119
 
62
120
    def test_branch_switch_no_branch(self):
63
121
        # No branch in the current directory:
64
122
        #  => new branch will be created, but switch fails
98
156
        work = branch.Branch.open('current')
99
157
        self.assertEqual(work.last_revision(), _mod_revision.NULL_REVISION)
100
158
 
 
159
    def test_branch_into_empty_dir(self):
 
160
        t = self.example_branch('source')
 
161
        self.make_bzrdir('target')
 
162
        self.run_bzr("branch source target")
 
163
        self.assertEquals(2, len(t.branch.repository.all_revision_ids()))
 
164
 
101
165
    def test_branch_switch_checkout(self):
102
166
        # Checkout in the current directory:
103
167
        #  => new branch will be created and checkout bound to the new branch
104
168
        self.example_branch('a')
105
169
        self.run_bzr('checkout a current')
106
 
        out, err = self.run_bzr('branch --switch ../a ../b', working_dir='current')
 
170
        out, err = self.run_bzr('branch --switch ../a ../b',
 
171
                                working_dir='current')
107
172
        a = branch.Branch.open('a')
108
173
        b = branch.Branch.open('b')
109
174
        self.assertEqual(a.last_revision(), b.last_revision())
117
182
        #     the new branch
118
183
        self.example_branch('a')
119
184
        self.run_bzr('checkout --lightweight a current')
120
 
        out, err = self.run_bzr('branch --switch ../a ../b', working_dir='current')
 
185
        out, err = self.run_bzr('branch --switch ../a ../b',
 
186
                                working_dir='current')
121
187
        a = branch.Branch.open('a')
122
188
        b = branch.Branch.open('b')
123
189
        self.assertEqual(a.last_revision(), b.last_revision())
134
200
 
135
201
        def make_shared_tree(path):
136
202
            shared_repo.bzrdir.root_transport.mkdir(path)
137
 
            shared_repo.bzrdir.create_branch_convenience('repo/' + path)
 
203
            controldir.ControlDir.create_branch_convenience('repo/' + path)
138
204
            return WorkingTree.open('repo/' + path)
139
205
        tree_a = make_shared_tree('a')
140
206
        self.build_tree(['repo/a/file'])
174
240
        target_stat = os.stat('target/file1')
175
241
        self.assertEqual(source_stat, target_stat)
176
242
 
 
243
    def test_branch_files_from(self):
 
244
        source = self.make_branch_and_tree('source')
 
245
        self.build_tree(['source/file1'])
 
246
        source.add('file1')
 
247
        source.commit('added file')
 
248
        out, err = self.run_bzr('branch source target --files-from source')
 
249
        self.assertPathExists('target/file1')
 
250
 
 
251
    def test_branch_files_from_hardlink(self):
 
252
        self.requireFeature(HardlinkFeature)
 
253
        source = self.make_branch_and_tree('source')
 
254
        self.build_tree(['source/file1'])
 
255
        source.add('file1')
 
256
        source.commit('added file')
 
257
        source.bzrdir.sprout('second')
 
258
        out, err = self.run_bzr('branch source target --files-from second'
 
259
                                ' --hardlink')
 
260
        source_stat = os.stat('source/file1')
 
261
        second_stat = os.stat('second/file1')
 
262
        target_stat = os.stat('target/file1')
 
263
        self.assertNotEqual(source_stat, target_stat)
 
264
        self.assertEqual(second_stat, target_stat)
 
265
 
177
266
    def test_branch_standalone(self):
178
267
        shared_repo = self.make_repository('repo', shared=True)
179
268
        self.example_branch('source')
186
275
    def test_branch_no_tree(self):
187
276
        self.example_branch('source')
188
277
        self.run_bzr('branch --no-tree source target')
189
 
        self.failIfExists('target/hello')
190
 
        self.failIfExists('target/goodbye')
 
278
        self.assertPathDoesNotExist('target/hello')
 
279
        self.assertPathDoesNotExist('target/goodbye')
191
280
 
192
281
    def test_branch_into_existing_dir(self):
193
282
        self.example_branch('a')
203
292
        # force operation
204
293
        self.run_bzr('branch a b --use-existing-dir')
205
294
        # check conflicts
206
 
        self.failUnlessExists('b/hello.moved')
207
 
        self.failIfExists('b/godbye.moved')
 
295
        self.assertPathExists('b/hello.moved')
 
296
        self.assertPathDoesNotExist('b/godbye.moved')
208
297
        # we can't branch into branch
209
298
        out,err = self.run_bzr('branch a b --use-existing-dir', retcode=3)
210
299
        self.assertEqual('', out)
217
306
        b = branch.Branch.open('b')
218
307
        self.assertEndsWith(b.get_bound_location(), '/a/')
219
308
 
220
 
 
221
 
class TestBranchStacked(ExternalBase):
 
309
    def test_branch_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('branch a b')
 
317
        self.assertLength(2, calls)
 
318
 
 
319
    def test_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 a b')
 
327
        self.assertLength(2, calls)
 
328
 
 
329
    def test_lightweight_checkout_with_post_branch_init_hook(self):
 
330
        calls = []
 
331
        branch.Branch.hooks.install_named_hook('post_branch_init',
 
332
            calls.append, None)
 
333
        self.assertLength(0, calls)
 
334
        self.example_branch('a')
 
335
        self.assertLength(1, calls)
 
336
        self.run_bzr('checkout --lightweight a b')
 
337
        self.assertLength(2, calls)
 
338
 
 
339
    def test_branch_fetches_all_tags(self):
 
340
        builder = self.make_branch_builder('source')
 
341
        source = fixtures.build_branch_with_non_ancestral_rev(builder)
 
342
        source.tags.set_tag('tag-a', 'rev-2')
 
343
        source.get_config_stack().set('branch.fetch_tags', True)
 
344
        # Now source has a tag not in its ancestry.  Make a branch from it.
 
345
        self.run_bzr('branch source new-branch')
 
346
        new_branch = branch.Branch.open('new-branch')
 
347
        # The tag is present, and so is its revision.
 
348
        self.assertEqual('rev-2', new_branch.tags.lookup_tag('tag-a'))
 
349
        new_branch.repository.get_revision('rev-2')
 
350
 
 
351
 
 
352
class TestBranchStacked(tests.TestCaseWithTransport):
222
353
    """Tests for branch --stacked"""
223
354
 
224
355
    def assertRevisionInRepository(self, repo_path, revid):
225
 
        """Check that a revision is in a repository, disregarding stacking."""
 
356
        """Check that a revision is in a repo, disregarding stacking."""
226
357
        repo = bzrdir.BzrDir.open(repo_path).open_repository()
227
358
        self.assertTrue(repo.has_revision(revid))
228
359
 
229
360
    def assertRevisionNotInRepository(self, repo_path, revid):
230
 
        """Check that a revision is not in a repository, disregarding stacking."""
 
361
        """Check that a revision is not in a repo, disregarding stacking."""
231
362
        repo = bzrdir.BzrDir.open(repo_path).open_repository()
232
363
        self.assertFalse(repo.has_revision(revid))
233
364
 
254
385
        # mainline.
255
386
        out, err = self.run_bzr(['branch', 'branch', 'newbranch'])
256
387
        self.assertEqual('', out)
257
 
        self.assertEqual('Branched 2 revision(s).\n',
 
388
        self.assertEqual('Branched 2 revisions.\n',
258
389
            err)
259
390
        # it should have preserved the branch format, and so it should be
260
391
        # capable of supporting stacking, but not actually have a stacked_on
305
436
            trunk_tree.branch.base, err)
306
437
        self.assertRevisionNotInRepository('newbranch', original_revid)
307
438
        new_branch = branch.Branch.open('newbranch')
308
 
        self.assertEqual(trunk_tree.branch.base, new_branch.get_stacked_on_url())
 
439
        self.assertEqual(trunk_tree.branch.base,
 
440
                         new_branch.get_stacked_on_url())
309
441
 
310
442
    def test_branch_stacked_from_smart_server(self):
311
443
        # We can branch stacking on a smart server
325
457
            '  Packs 5 (adds stacking support, requires bzr 1.6)\n'
326
458
            'Source branch format does not support stacking, using format:\n'
327
459
            '  Branch format 7\n'
 
460
            'Doing on-the-fly conversion from RepositoryFormatKnitPack1() to RepositoryFormatKnitPack5().\n'
 
461
            'This may take some time. Upgrade the repositories to the same format for better performance.\n'
328
462
            'Created new stacked branch referring to %s.\n' % (trunk.base,),
329
463
            err)
330
464
 
338
472
            '  Packs 5 rich-root (adds stacking support, requires bzr 1.6.1)\n'
339
473
            'Source branch format does not support stacking, using format:\n'
340
474
            '  Branch format 7\n'
 
475
            'Doing on-the-fly conversion from RepositoryFormatKnitPack4() to RepositoryFormatKnitPack5RichRoot().\n'
 
476
            'This may take some time. Upgrade the repositories to the same format for better performance.\n'
341
477
            'Created new stacked branch referring to %s.\n' % (trunk.base,),
342
478
            err)
343
479
 
344
480
 
345
 
class TestSmartServerBranching(ExternalBase):
 
481
class TestSmartServerBranching(tests.TestCaseWithTransport):
346
482
 
347
483
    def test_branch_from_trivial_branch_to_same_server_branch_acceptance(self):
348
484
        self.setup_smart_server_with_call_log()
357
493
        # being too low. If rpc_count increases, more network roundtrips have
358
494
        # become necessary for this use case. Please do not adjust this number
359
495
        # upwards without agreement from bzr's network support maintainers.
360
 
        self.assertLength(38, self.hpss_calls)
 
496
        self.assertLength(2, self.hpss_connections)
 
497
        self.assertLength(33, self.hpss_calls)
 
498
        self.expectFailure("branching to the same branch requires VFS access",
 
499
            self.assertThat, self.hpss_calls, ContainsNoVfsCalls)
361
500
 
362
501
    def test_branch_from_trivial_branch_streaming_acceptance(self):
363
502
        self.setup_smart_server_with_call_log()
372
511
        # being too low. If rpc_count increases, more network roundtrips have
373
512
        # become necessary for this use case. Please do not adjust this number
374
513
        # upwards without agreement from bzr's network support maintainers.
 
514
        self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
375
515
        self.assertLength(10, self.hpss_calls)
 
516
        self.assertLength(1, self.hpss_connections)
376
517
 
377
518
    def test_branch_from_trivial_stacked_branch_streaming_acceptance(self):
378
519
        self.setup_smart_server_with_call_log()
393
534
        # become necessary for this use case. Please do not adjust this number
394
535
        # upwards without agreement from bzr's network support maintainers.
395
536
        self.assertLength(15, self.hpss_calls)
 
537
        self.assertLength(1, self.hpss_connections)
 
538
        self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
 
539
 
 
540
    def test_branch_from_branch_with_tags(self):
 
541
        self.setup_smart_server_with_call_log()
 
542
        builder = self.make_branch_builder('source')
 
543
        source = fixtures.build_branch_with_non_ancestral_rev(builder)
 
544
        source.get_config_stack().set('branch.fetch_tags', True)
 
545
        source.tags.set_tag('tag-a', 'rev-2')
 
546
        source.tags.set_tag('tag-missing', 'missing-rev')
 
547
        # Now source has a tag not in its ancestry.  Make a branch from it.
 
548
        self.reset_smart_call_log()
 
549
        out, err = self.run_bzr(['branch', self.get_url('source'), 'target'])
 
550
        # This figure represent the amount of work to perform this use case. It
 
551
        # is entirely ok to reduce this number if a test fails due to rpc_count
 
552
        # being too low. If rpc_count increases, more network roundtrips have
 
553
        # become necessary for this use case. Please do not adjust this number
 
554
        # upwards without agreement from bzr's network support maintainers.
 
555
        self.assertLength(10, self.hpss_calls)
 
556
        self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
 
557
        self.assertLength(1, self.hpss_connections)
 
558
 
 
559
    def test_branch_to_stacked_from_trivial_branch_streaming_acceptance(self):
 
560
        self.setup_smart_server_with_call_log()
 
561
        t = self.make_branch_and_tree('from')
 
562
        for count in range(9):
 
563
            t.commit(message='commit %d' % count)
 
564
        self.reset_smart_call_log()
 
565
        out, err = self.run_bzr(['branch', '--stacked', self.get_url('from'),
 
566
            'local-target'])
 
567
        # XXX: the number of hpss calls for this case isn't deterministic yet,
 
568
        # so we can't easily assert about the number of calls.
 
569
        #self.assertLength(XXX, self.hpss_calls)
 
570
        # We can assert that none of the calls were readv requests for rix
 
571
        # files, though (demonstrating that at least get_parent_map calls are
 
572
        # not using VFS RPCs).
 
573
        readvs_of_rix_files = [
 
574
            c for c in self.hpss_calls
 
575
            if c.call.method == 'readv' and c.call.args[-1].endswith('.rix')]
 
576
        self.assertLength(1, self.hpss_connections)
 
577
        self.assertLength(0, readvs_of_rix_files)
 
578
        self.expectFailure("branching to stacked requires VFS access",
 
579
            self.assertThat, self.hpss_calls, ContainsNoVfsCalls)
396
580
 
397
581
 
398
582
class TestRemoteBranch(TestCaseWithSFTPServer):
418
602
        # Ensure that no working tree what created remotely
419
603
        self.assertFalse(t.has('remote/file'))
420
604
 
 
605
 
 
606
class TestDeprecatedAliases(tests.TestCaseWithTransport):
 
607
 
 
608
    def test_deprecated_aliases(self):
 
609
        """bzr branch can be called clone or get, but those names are
 
610
        deprecated.
 
611
 
 
612
        See bug 506265.
 
613
        """
 
614
        for command in ['clone', 'get']:
 
615
            run_script(self, """
 
616
            $ bzr %(command)s A B
 
617
            2>The command 'bzr %(command)s' has been deprecated in bzr 2.4. Please use 'bzr branch' instead.
 
618
            2>bzr: ERROR: Not a branch...
 
619
            """ % locals())
 
620
 
 
621
 
 
622
class TestBranchParentLocation(test_switch.TestSwitchParentLocationBase):
 
623
 
 
624
    def _checkout_and_branch(self, option=''):
 
625
        self.script_runner.run_script(self, '''
 
626
                $ bzr checkout %(option)s repo/trunk checkout
 
627
                $ cd checkout
 
628
                $ bzr branch --switch ../repo/trunk ../repo/branched
 
629
                2>Branched 0 revisions.
 
630
                2>Tree is up to date at revision 0.
 
631
                2>Switched to branch:...branched...
 
632
                $ cd ..
 
633
                ''' % locals())
 
634
        bound_branch = branch.Branch.open_containing('checkout')[0]
 
635
        master_branch = branch.Branch.open_containing('repo/branched')[0]
 
636
        return (bound_branch, master_branch)
 
637
 
 
638
    def test_branch_switch_parent_lightweight(self):
 
639
        """Lightweight checkout using bzr branch --switch."""
 
640
        bb, mb = self._checkout_and_branch(option='--lightweight')
 
641
        self.assertParent('repo/trunk', bb)
 
642
        self.assertParent('repo/trunk', mb)
 
643
 
 
644
    def test_branch_switch_parent_heavyweight(self):
 
645
        """Heavyweight checkout using bzr branch --switch."""
 
646
        bb, mb = self._checkout_and_branch()
 
647
        self.assertParent('repo/trunk', bb)
 
648
        self.assertParent('repo/trunk', mb)