~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: INADA Naoki
  • Date: 2011-05-18 06:01:08 UTC
  • mto: This revision was merged to the branch mainline in revision 5894.
  • Revision ID: songofacandy@gmail.com-20110518060108-86t2kffcrzu0nf6i
Update Japanese docs.

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-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
19
19
 
20
20
import os
21
21
 
22
 
from bzrlib import (branch, bzrdir, errors, repository)
 
22
from bzrlib import (
 
23
    branch,
 
24
    bzrdir,
 
25
    errors,
 
26
    revision as _mod_revision,
 
27
    )
23
28
from bzrlib.repofmt.knitrepo import RepositoryFormatKnit1
24
 
from bzrlib.tests.blackbox import ExternalBase
 
29
from bzrlib.tests import TestCaseWithTransport
25
30
from bzrlib.tests import (
26
 
    KnownFailure,
 
31
    fixtures,
27
32
    HardlinkFeature,
 
33
    script,
 
34
    test_server,
28
35
    )
 
36
from bzrlib.tests.blackbox import test_switch
29
37
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
 
38
from bzrlib.tests.script import run_script
30
39
from bzrlib.urlutils import local_path_to_url, strip_trailing_slash
31
40
from bzrlib.workingtree import WorkingTree
32
41
 
33
42
 
34
 
class TestBranch(ExternalBase):
 
43
class TestBranch(TestCaseWithTransport):
35
44
 
36
45
    def example_branch(self, path='.'):
37
46
        tree = self.make_branch_and_tree(path)
52
61
        self.assertFalse(b._transport.has('branch-name'))
53
62
        b.bzrdir.open_workingtree().commit(message='foo', allow_pointless=True)
54
63
 
 
64
    def test_branch_switch_no_branch(self):
 
65
        # No branch in the current directory:
 
66
        #  => new branch will be created, but switch fails
 
67
        self.example_branch('a')
 
68
        self.make_repository('current')
 
69
        self.run_bzr_error(['No WorkingTree exists for'],
 
70
            'branch --switch ../a ../b', working_dir='current')
 
71
        a = branch.Branch.open('a')
 
72
        b = branch.Branch.open('b')
 
73
        self.assertEqual(a.last_revision(), b.last_revision())
 
74
 
 
75
    def test_branch_switch_no_wt(self):
 
76
        # No working tree in the current directory:
 
77
        #  => new branch will be created, but switch fails and the current
 
78
        #     branch is unmodified
 
79
        self.example_branch('a')
 
80
        self.make_branch('current')
 
81
        self.run_bzr_error(['No WorkingTree exists for'],
 
82
            'branch --switch ../a ../b', working_dir='current')
 
83
        a = branch.Branch.open('a')
 
84
        b = branch.Branch.open('b')
 
85
        self.assertEqual(a.last_revision(), b.last_revision())
 
86
        work = branch.Branch.open('current')
 
87
        self.assertEqual(work.last_revision(), _mod_revision.NULL_REVISION)
 
88
 
 
89
    def test_branch_switch_no_checkout(self):
 
90
        # Standalone branch in the current directory:
 
91
        #  => new branch will be created, but switch fails and the current
 
92
        #     branch is unmodified
 
93
        self.example_branch('a')
 
94
        self.make_branch_and_tree('current')
 
95
        self.run_bzr_error(['Cannot switch a branch, only a checkout'],
 
96
            'branch --switch ../a ../b', working_dir='current')
 
97
        a = branch.Branch.open('a')
 
98
        b = branch.Branch.open('b')
 
99
        self.assertEqual(a.last_revision(), b.last_revision())
 
100
        work = branch.Branch.open('current')
 
101
        self.assertEqual(work.last_revision(), _mod_revision.NULL_REVISION)
 
102
 
 
103
    def test_branch_switch_checkout(self):
 
104
        # Checkout in the current directory:
 
105
        #  => new branch will be created and checkout bound to the new branch
 
106
        self.example_branch('a')
 
107
        self.run_bzr('checkout a current')
 
108
        out, err = self.run_bzr('branch --switch ../a ../b', working_dir='current')
 
109
        a = branch.Branch.open('a')
 
110
        b = branch.Branch.open('b')
 
111
        self.assertEqual(a.last_revision(), b.last_revision())
 
112
        work = WorkingTree.open('current')
 
113
        self.assertEndsWith(work.branch.get_bound_location(), '/b/')
 
114
        self.assertContainsRe(err, "Switched to branch: .*/b/")
 
115
 
 
116
    def test_branch_switch_lightweight_checkout(self):
 
117
        # Lightweight checkout in the current directory:
 
118
        #  => new branch will be created and lightweight checkout pointed to
 
119
        #     the new branch
 
120
        self.example_branch('a')
 
121
        self.run_bzr('checkout --lightweight a current')
 
122
        out, err = self.run_bzr('branch --switch ../a ../b', working_dir='current')
 
123
        a = branch.Branch.open('a')
 
124
        b = branch.Branch.open('b')
 
125
        self.assertEqual(a.last_revision(), b.last_revision())
 
126
        work = WorkingTree.open('current')
 
127
        self.assertEndsWith(work.branch.base, '/b/')
 
128
        self.assertContainsRe(err, "Switched to branch: .*/b/")
 
129
 
55
130
    def test_branch_only_copies_history(self):
56
131
        # Knit branches should only push the history for the current revision.
57
132
        format = bzrdir.BzrDirMetaFormat1()
99
174
        out, err = self.run_bzr(['branch', 'source', 'target', '--hardlink'])
100
175
        source_stat = os.stat('source/file1')
101
176
        target_stat = os.stat('target/file1')
102
 
        same_file = (source_stat == target_stat)
103
 
        if same_file:
104
 
            pass
105
 
        else:
106
 
            # https://bugs.edge.launchpad.net/bzr/+bug/408193
107
 
            self.assertContainsRe(err, "hardlinking working copy files is "
108
 
                "not currently supported")
109
 
            raise KnownFailure("--hardlink doesn't work in formats "
110
 
                "that support content filtering (#408193)")
 
177
        self.assertEqual(source_stat, target_stat)
 
178
 
 
179
    def test_branch_files_from(self):
 
180
        source = self.make_branch_and_tree('source')
 
181
        self.build_tree(['source/file1'])
 
182
        source.add('file1')
 
183
        source.commit('added file')
 
184
        out, err = self.run_bzr('branch source target --files-from source')
 
185
        self.assertPathExists('target/file1')
 
186
 
 
187
    def test_branch_files_from_hardlink(self):
 
188
        self.requireFeature(HardlinkFeature)
 
189
        source = self.make_branch_and_tree('source')
 
190
        self.build_tree(['source/file1'])
 
191
        source.add('file1')
 
192
        source.commit('added file')
 
193
        source.bzrdir.sprout('second')
 
194
        out, err = self.run_bzr('branch source target --files-from second'
 
195
                                ' --hardlink')
 
196
        source_stat = os.stat('source/file1')
 
197
        second_stat = os.stat('second/file1')
 
198
        target_stat = os.stat('target/file1')
 
199
        self.assertNotEqual(source_stat, target_stat)
 
200
        self.assertEqual(second_stat, target_stat)
111
201
 
112
202
    def test_branch_standalone(self):
113
203
        shared_repo = self.make_repository('repo', shared=True)
121
211
    def test_branch_no_tree(self):
122
212
        self.example_branch('source')
123
213
        self.run_bzr('branch --no-tree source target')
124
 
        self.failIfExists('target/hello')
125
 
        self.failIfExists('target/goodbye')
 
214
        self.assertPathDoesNotExist('target/hello')
 
215
        self.assertPathDoesNotExist('target/goodbye')
126
216
 
127
217
    def test_branch_into_existing_dir(self):
128
218
        self.example_branch('a')
138
228
        # force operation
139
229
        self.run_bzr('branch a b --use-existing-dir')
140
230
        # check conflicts
141
 
        self.failUnlessExists('b/hello.moved')
142
 
        self.failIfExists('b/godbye.moved')
 
231
        self.assertPathExists('b/hello.moved')
 
232
        self.assertPathDoesNotExist('b/godbye.moved')
143
233
        # we can't branch into branch
144
234
        out,err = self.run_bzr('branch a b --use-existing-dir', retcode=3)
145
235
        self.assertEqual('', out)
146
236
        self.assertEqual('bzr: ERROR: Already a branch: "b".\n', err)
147
237
 
148
 
 
149
 
class TestBranchStacked(ExternalBase):
 
238
    def test_branch_bind(self):
 
239
        self.example_branch('a')
 
240
        out, err = self.run_bzr('branch a b --bind')
 
241
        self.assertEndsWith(err, "New branch bound to a\n")
 
242
        b = branch.Branch.open('b')
 
243
        self.assertEndsWith(b.get_bound_location(), '/a/')
 
244
 
 
245
    def test_branch_with_post_branch_init_hook(self):
 
246
        calls = []
 
247
        branch.Branch.hooks.install_named_hook('post_branch_init',
 
248
            calls.append, None)
 
249
        self.assertLength(0, calls)
 
250
        self.example_branch('a')
 
251
        self.assertLength(1, calls)
 
252
        self.run_bzr('branch a b')
 
253
        self.assertLength(2, calls)
 
254
 
 
255
    def test_checkout_with_post_branch_init_hook(self):
 
256
        calls = []
 
257
        branch.Branch.hooks.install_named_hook('post_branch_init',
 
258
            calls.append, None)
 
259
        self.assertLength(0, calls)
 
260
        self.example_branch('a')
 
261
        self.assertLength(1, calls)
 
262
        self.run_bzr('checkout a b')
 
263
        self.assertLength(2, calls)
 
264
 
 
265
    def test_lightweight_checkout_with_post_branch_init_hook(self):
 
266
        calls = []
 
267
        branch.Branch.hooks.install_named_hook('post_branch_init',
 
268
            calls.append, None)
 
269
        self.assertLength(0, calls)
 
270
        self.example_branch('a')
 
271
        self.assertLength(1, calls)
 
272
        self.run_bzr('checkout --lightweight a b')
 
273
        self.assertLength(2, calls)
 
274
 
 
275
    def test_branch_fetches_all_tags(self):
 
276
        builder = self.make_branch_builder('source')
 
277
        source = fixtures.build_branch_with_non_ancestral_rev(builder)
 
278
        source.tags.set_tag('tag-a', 'rev-2')
 
279
        # Now source has a tag not in its ancestry.  Make a branch from it.
 
280
        self.run_bzr('branch source new-branch')
 
281
        new_branch = branch.Branch.open('new-branch')
 
282
        # The tag is present, and so is its revision.
 
283
        self.assertEqual('rev-2', new_branch.tags.lookup_tag('tag-a'))
 
284
        new_branch.repository.get_revision('rev-2')
 
285
 
 
286
 
 
287
class TestBranchStacked(TestCaseWithTransport):
150
288
    """Tests for branch --stacked"""
151
289
 
152
290
    def assertRevisionInRepository(self, repo_path, revid):
237
375
 
238
376
    def test_branch_stacked_from_smart_server(self):
239
377
        # We can branch stacking on a smart server
240
 
        from bzrlib.smart.server import SmartTCPServer_for_testing
241
 
        self.transport_server = SmartTCPServer_for_testing
 
378
        self.transport_server = test_server.SmartTCPServer_for_testing
242
379
        trunk = self.make_branch('mainline', format='1.9')
243
380
        out, err = self.run_bzr(
244
381
            ['branch', '--stacked', self.get_url('mainline'), 'shallow'])
254
391
            '  Packs 5 (adds stacking support, requires bzr 1.6)\n'
255
392
            'Source branch format does not support stacking, using format:\n'
256
393
            '  Branch format 7\n'
 
394
            'Doing on-the-fly conversion from RepositoryFormatKnitPack1() to RepositoryFormatKnitPack5().\n'
 
395
            'This may take some time. Upgrade the repositories to the same format for better performance.\n'
257
396
            'Created new stacked branch referring to %s.\n' % (trunk.base,),
258
397
            err)
259
398
 
267
406
            '  Packs 5 rich-root (adds stacking support, requires bzr 1.6.1)\n'
268
407
            'Source branch format does not support stacking, using format:\n'
269
408
            '  Branch format 7\n'
 
409
            'Doing on-the-fly conversion from RepositoryFormatKnitPack4() to RepositoryFormatKnitPack5RichRoot().\n'
 
410
            'This may take some time. Upgrade the repositories to the same format for better performance.\n'
270
411
            'Created new stacked branch referring to %s.\n' % (trunk.base,),
271
412
            err)
272
413
 
273
414
 
274
 
class TestSmartServerBranching(ExternalBase):
 
415
class TestSmartServerBranching(TestCaseWithTransport):
275
416
 
276
417
    def test_branch_from_trivial_branch_to_same_server_branch_acceptance(self):
277
418
        self.setup_smart_server_with_call_log()
286
427
        # being too low. If rpc_count increases, more network roundtrips have
287
428
        # become necessary for this use case. Please do not adjust this number
288
429
        # upwards without agreement from bzr's network support maintainers.
289
 
        self.assertLength(38, self.hpss_calls)
 
430
        self.assertLength(36, self.hpss_calls)
290
431
 
291
432
    def test_branch_from_trivial_branch_streaming_acceptance(self):
292
433
        self.setup_smart_server_with_call_log()
301
442
        # being too low. If rpc_count increases, more network roundtrips have
302
443
        # become necessary for this use case. Please do not adjust this number
303
444
        # upwards without agreement from bzr's network support maintainers.
304
 
        self.assertLength(10, self.hpss_calls)
 
445
        self.assertLength(9, self.hpss_calls)
305
446
 
306
447
    def test_branch_from_trivial_stacked_branch_streaming_acceptance(self):
307
448
        self.setup_smart_server_with_call_log()
321
462
        # being too low. If rpc_count increases, more network roundtrips have
322
463
        # become necessary for this use case. Please do not adjust this number
323
464
        # upwards without agreement from bzr's network support maintainers.
324
 
        self.assertLength(15, self.hpss_calls)
 
465
        self.assertLength(14, self.hpss_calls)
 
466
 
 
467
    def test_branch_from_branch_with_tags(self):
 
468
        self.setup_smart_server_with_call_log()
 
469
        builder = self.make_branch_builder('source')
 
470
        source = fixtures.build_branch_with_non_ancestral_rev(builder)
 
471
        source.tags.set_tag('tag-a', 'rev-2')
 
472
        source.tags.set_tag('tag-missing', 'missing-rev')
 
473
        # Now source has a tag not in its ancestry.  Make a branch from it.
 
474
        self.reset_smart_call_log()
 
475
        out, err = self.run_bzr(['branch', self.get_url('source'), 'target'])
 
476
        # This figure represent the amount of work to perform this use case. It
 
477
        # is entirely ok to reduce this number if a test fails due to rpc_count
 
478
        # being too low. If rpc_count increases, more network roundtrips have
 
479
        # become necessary for this use case. Please do not adjust this number
 
480
        # upwards without agreement from bzr's network support maintainers.
 
481
        self.assertLength(9, self.hpss_calls)
 
482
 
 
483
    def test_branch_to_stacked_from_trivial_branch_streaming_acceptance(self):
 
484
        self.setup_smart_server_with_call_log()
 
485
        t = self.make_branch_and_tree('from')
 
486
        for count in range(9):
 
487
            t.commit(message='commit %d' % count)
 
488
        self.reset_smart_call_log()
 
489
        out, err = self.run_bzr(['branch', '--stacked', self.get_url('from'),
 
490
            'local-target'])
 
491
        # XXX: the number of hpss calls for this case isn't deterministic yet,
 
492
        # so we can't easily assert about the number of calls.
 
493
        #self.assertLength(XXX, self.hpss_calls)
 
494
        # We can assert that none of the calls were readv requests for rix
 
495
        # files, though (demonstrating that at least get_parent_map calls are
 
496
        # not using VFS RPCs).
 
497
        readvs_of_rix_files = [
 
498
            c for c in self.hpss_calls
 
499
            if c.call.method == 'readv' and c.call.args[-1].endswith('.rix')]
 
500
        self.assertLength(0, readvs_of_rix_files)
325
501
 
326
502
 
327
503
class TestRemoteBranch(TestCaseWithSFTPServer):
347
523
        # Ensure that no working tree what created remotely
348
524
        self.assertFalse(t.has('remote/file'))
349
525
 
 
526
 
 
527
class TestDeprecatedAliases(TestCaseWithTransport):
 
528
 
 
529
    def test_deprecated_aliases(self):
 
530
        """bzr branch can be called clone or get, but those names are deprecated.
 
531
 
 
532
        See bug 506265.
 
533
        """
 
534
        for command in ['clone', 'get']:
 
535
            run_script(self, """
 
536
            $ bzr %(command)s A B
 
537
            2>The command 'bzr %(command)s' has been deprecated in bzr 2.4. Please use 'bzr branch' instead.
 
538
            2>bzr: ERROR: Not a branch...
 
539
            """ % locals())
 
540
 
 
541
 
 
542
class TestBranchParentLocation(test_switch.TestSwitchParentLocationBase):
 
543
 
 
544
    def _checkout_and_branch(self, option=''):
 
545
        self.script_runner.run_script(self, '''
 
546
                $ bzr checkout %(option)s repo/trunk checkout
 
547
                $ cd checkout
 
548
                $ bzr branch --switch ../repo/trunk ../repo/branched
 
549
                2>Branched 0 revision(s).
 
550
                2>Tree is up to date at revision 0.
 
551
                2>Switched to branch:...branched...
 
552
                $ cd ..
 
553
                ''' % locals())
 
554
        bound_branch = branch.Branch.open_containing('checkout')[0]
 
555
        master_branch = branch.Branch.open_containing('repo/branched')[0]
 
556
        return (bound_branch, master_branch)
 
557
 
 
558
    def test_branch_switch_parent_lightweight(self):
 
559
        """Lightweight checkout using bzr branch --switch."""
 
560
        bb, mb = self._checkout_and_branch(option='--lightweight')
 
561
        self.assertParent('repo/trunk', bb)
 
562
        self.assertParent('repo/trunk', mb)
 
563
 
 
564
    def test_branch_switch_parent_heavyweight(self):
 
565
        """Heavyweight checkout using bzr branch --switch."""
 
566
        bb, mb = self._checkout_and_branch()
 
567
        self.assertParent('repo/trunk', bb)
 
568
        self.assertParent('repo/trunk', mb)