~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-08-17 08:40:16 UTC
  • mfrom: (5642.4.6 712474-module-available)
  • Revision ID: pqm@pqm.ubuntu.com-20110817084016-600z65qzqmmt44w7
(vila) ModuleAvailableFeature don't try to imported already imported
 modules. (Vincent Ladeuil)

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
23
23
    branch,
24
24
    bzrdir,
25
25
    errors,
26
 
    repository,
27
26
    revision as _mod_revision,
28
27
    )
29
28
from bzrlib.repofmt.knitrepo import RepositoryFormatKnit1
30
 
from bzrlib.tests.blackbox import ExternalBase
 
29
from bzrlib.tests import TestCaseWithTransport
31
30
from bzrlib.tests import (
32
 
    KnownFailure,
 
31
    fixtures,
 
32
    script,
 
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
36
39
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
 
40
from bzrlib.tests.script import run_script
37
41
from bzrlib.urlutils import local_path_to_url, strip_trailing_slash
38
42
from bzrlib.workingtree import WorkingTree
39
43
 
40
44
 
41
 
class TestBranch(ExternalBase):
 
45
class TestBranch(TestCaseWithTransport):
42
46
 
43
47
    def example_branch(self, path='.'):
44
48
        tree = self.make_branch_and_tree(path)
59
63
        self.assertFalse(b._transport.has('branch-name'))
60
64
        b.bzrdir.open_workingtree().commit(message='foo', allow_pointless=True)
61
65
 
 
66
    def test_branch_broken_pack(self):
 
67
        """branching with a corrupted pack file."""
 
68
        self.example_branch('a')
 
69
        #now add some random corruption
 
70
        fname = 'a/.bzr/repository/packs/' + os.listdir('a/.bzr/repository/packs')[0]
 
71
        with open(fname, 'rb+') as f:
 
72
            f.seek(750)
 
73
            f.write("\xff")
 
74
        self.run_bzr_error(['Corruption while decompressing repository file'], 
 
75
                            'branch a b', retcode=3)
 
76
 
62
77
    def test_branch_switch_no_branch(self):
63
78
        # No branch in the current directory:
64
79
        #  => new branch will be created, but switch fails
174
189
        target_stat = os.stat('target/file1')
175
190
        self.assertEqual(source_stat, target_stat)
176
191
 
 
192
    def test_branch_files_from(self):
 
193
        source = self.make_branch_and_tree('source')
 
194
        self.build_tree(['source/file1'])
 
195
        source.add('file1')
 
196
        source.commit('added file')
 
197
        out, err = self.run_bzr('branch source target --files-from source')
 
198
        self.assertPathExists('target/file1')
 
199
 
 
200
    def test_branch_files_from_hardlink(self):
 
201
        self.requireFeature(HardlinkFeature)
 
202
        source = self.make_branch_and_tree('source')
 
203
        self.build_tree(['source/file1'])
 
204
        source.add('file1')
 
205
        source.commit('added file')
 
206
        source.bzrdir.sprout('second')
 
207
        out, err = self.run_bzr('branch source target --files-from second'
 
208
                                ' --hardlink')
 
209
        source_stat = os.stat('source/file1')
 
210
        second_stat = os.stat('second/file1')
 
211
        target_stat = os.stat('target/file1')
 
212
        self.assertNotEqual(source_stat, target_stat)
 
213
        self.assertEqual(second_stat, target_stat)
 
214
 
177
215
    def test_branch_standalone(self):
178
216
        shared_repo = self.make_repository('repo', shared=True)
179
217
        self.example_branch('source')
186
224
    def test_branch_no_tree(self):
187
225
        self.example_branch('source')
188
226
        self.run_bzr('branch --no-tree source target')
189
 
        self.failIfExists('target/hello')
190
 
        self.failIfExists('target/goodbye')
 
227
        self.assertPathDoesNotExist('target/hello')
 
228
        self.assertPathDoesNotExist('target/goodbye')
191
229
 
192
230
    def test_branch_into_existing_dir(self):
193
231
        self.example_branch('a')
203
241
        # force operation
204
242
        self.run_bzr('branch a b --use-existing-dir')
205
243
        # check conflicts
206
 
        self.failUnlessExists('b/hello.moved')
207
 
        self.failIfExists('b/godbye.moved')
 
244
        self.assertPathExists('b/hello.moved')
 
245
        self.assertPathDoesNotExist('b/godbye.moved')
208
246
        # we can't branch into branch
209
247
        out,err = self.run_bzr('branch a b --use-existing-dir', retcode=3)
210
248
        self.assertEqual('', out)
217
255
        b = branch.Branch.open('b')
218
256
        self.assertEndsWith(b.get_bound_location(), '/a/')
219
257
 
220
 
 
221
 
class TestBranchStacked(ExternalBase):
 
258
    def test_branch_with_post_branch_init_hook(self):
 
259
        calls = []
 
260
        branch.Branch.hooks.install_named_hook('post_branch_init',
 
261
            calls.append, None)
 
262
        self.assertLength(0, calls)
 
263
        self.example_branch('a')
 
264
        self.assertLength(1, calls)
 
265
        self.run_bzr('branch a b')
 
266
        self.assertLength(2, calls)
 
267
 
 
268
    def test_checkout_with_post_branch_init_hook(self):
 
269
        calls = []
 
270
        branch.Branch.hooks.install_named_hook('post_branch_init',
 
271
            calls.append, None)
 
272
        self.assertLength(0, calls)
 
273
        self.example_branch('a')
 
274
        self.assertLength(1, calls)
 
275
        self.run_bzr('checkout a b')
 
276
        self.assertLength(2, calls)
 
277
 
 
278
    def test_lightweight_checkout_with_post_branch_init_hook(self):
 
279
        calls = []
 
280
        branch.Branch.hooks.install_named_hook('post_branch_init',
 
281
            calls.append, None)
 
282
        self.assertLength(0, calls)
 
283
        self.example_branch('a')
 
284
        self.assertLength(1, calls)
 
285
        self.run_bzr('checkout --lightweight a b')
 
286
        self.assertLength(2, calls)
 
287
 
 
288
    def test_branch_fetches_all_tags(self):
 
289
        builder = self.make_branch_builder('source')
 
290
        source = fixtures.build_branch_with_non_ancestral_rev(builder)
 
291
        source.tags.set_tag('tag-a', 'rev-2')
 
292
        source.get_config().set_user_option('branch.fetch_tags', 'True')
 
293
        # Now source has a tag not in its ancestry.  Make a branch from it.
 
294
        self.run_bzr('branch source new-branch')
 
295
        new_branch = branch.Branch.open('new-branch')
 
296
        # The tag is present, and so is its revision.
 
297
        self.assertEqual('rev-2', new_branch.tags.lookup_tag('tag-a'))
 
298
        new_branch.repository.get_revision('rev-2')
 
299
 
 
300
 
 
301
class TestBranchStacked(TestCaseWithTransport):
222
302
    """Tests for branch --stacked"""
223
303
 
224
304
    def assertRevisionInRepository(self, repo_path, revid):
325
405
            '  Packs 5 (adds stacking support, requires bzr 1.6)\n'
326
406
            'Source branch format does not support stacking, using format:\n'
327
407
            '  Branch format 7\n'
 
408
            'Doing on-the-fly conversion from RepositoryFormatKnitPack1() to RepositoryFormatKnitPack5().\n'
 
409
            'This may take some time. Upgrade the repositories to the same format for better performance.\n'
328
410
            'Created new stacked branch referring to %s.\n' % (trunk.base,),
329
411
            err)
330
412
 
338
420
            '  Packs 5 rich-root (adds stacking support, requires bzr 1.6.1)\n'
339
421
            'Source branch format does not support stacking, using format:\n'
340
422
            '  Branch format 7\n'
 
423
            'Doing on-the-fly conversion from RepositoryFormatKnitPack4() to RepositoryFormatKnitPack5RichRoot().\n'
 
424
            'This may take some time. Upgrade the repositories to the same format for better performance.\n'
341
425
            'Created new stacked branch referring to %s.\n' % (trunk.base,),
342
426
            err)
343
427
 
344
428
 
345
 
class TestSmartServerBranching(ExternalBase):
 
429
class TestSmartServerBranching(TestCaseWithTransport):
346
430
 
347
431
    def test_branch_from_trivial_branch_to_same_server_branch_acceptance(self):
348
432
        self.setup_smart_server_with_call_log()
357
441
        # being too low. If rpc_count increases, more network roundtrips have
358
442
        # become necessary for this use case. Please do not adjust this number
359
443
        # upwards without agreement from bzr's network support maintainers.
360
 
        self.assertLength(38, self.hpss_calls)
 
444
        self.assertLength(37, self.hpss_calls)
361
445
 
362
446
    def test_branch_from_trivial_branch_streaming_acceptance(self):
363
447
        self.setup_smart_server_with_call_log()
394
478
        # upwards without agreement from bzr's network support maintainers.
395
479
        self.assertLength(15, self.hpss_calls)
396
480
 
 
481
    def test_branch_from_branch_with_tags(self):
 
482
        self.setup_smart_server_with_call_log()
 
483
        builder = self.make_branch_builder('source')
 
484
        source = fixtures.build_branch_with_non_ancestral_rev(builder)
 
485
        source.get_config().set_user_option('branch.fetch_tags', 'True')
 
486
        source.tags.set_tag('tag-a', 'rev-2')
 
487
        source.tags.set_tag('tag-missing', 'missing-rev')
 
488
        # Now source has a tag not in its ancestry.  Make a branch from it.
 
489
        self.reset_smart_call_log()
 
490
        out, err = self.run_bzr(['branch', self.get_url('source'), 'target'])
 
491
        # This figure represent the amount of work to perform this use case. It
 
492
        # is entirely ok to reduce this number if a test fails due to rpc_count
 
493
        # being too low. If rpc_count increases, more network roundtrips have
 
494
        # become necessary for this use case. Please do not adjust this number
 
495
        # upwards without agreement from bzr's network support maintainers.
 
496
        self.assertLength(10, self.hpss_calls)
 
497
 
 
498
    def test_branch_to_stacked_from_trivial_branch_streaming_acceptance(self):
 
499
        self.setup_smart_server_with_call_log()
 
500
        t = self.make_branch_and_tree('from')
 
501
        for count in range(9):
 
502
            t.commit(message='commit %d' % count)
 
503
        self.reset_smart_call_log()
 
504
        out, err = self.run_bzr(['branch', '--stacked', self.get_url('from'),
 
505
            'local-target'])
 
506
        # XXX: the number of hpss calls for this case isn't deterministic yet,
 
507
        # so we can't easily assert about the number of calls.
 
508
        #self.assertLength(XXX, self.hpss_calls)
 
509
        # We can assert that none of the calls were readv requests for rix
 
510
        # files, though (demonstrating that at least get_parent_map calls are
 
511
        # not using VFS RPCs).
 
512
        readvs_of_rix_files = [
 
513
            c for c in self.hpss_calls
 
514
            if c.call.method == 'readv' and c.call.args[-1].endswith('.rix')]
 
515
        self.assertLength(0, readvs_of_rix_files)
 
516
 
397
517
 
398
518
class TestRemoteBranch(TestCaseWithSFTPServer):
399
519
 
418
538
        # Ensure that no working tree what created remotely
419
539
        self.assertFalse(t.has('remote/file'))
420
540
 
 
541
 
 
542
class TestDeprecatedAliases(TestCaseWithTransport):
 
543
 
 
544
    def test_deprecated_aliases(self):
 
545
        """bzr branch can be called clone or get, but those names are deprecated.
 
546
 
 
547
        See bug 506265.
 
548
        """
 
549
        for command in ['clone', 'get']:
 
550
            run_script(self, """
 
551
            $ bzr %(command)s A B
 
552
            2>The command 'bzr %(command)s' has been deprecated in bzr 2.4. Please use 'bzr branch' instead.
 
553
            2>bzr: ERROR: Not a branch...
 
554
            """ % locals())
 
555
 
 
556
 
 
557
class TestBranchParentLocation(test_switch.TestSwitchParentLocationBase):
 
558
 
 
559
    def _checkout_and_branch(self, option=''):
 
560
        self.script_runner.run_script(self, '''
 
561
                $ bzr checkout %(option)s repo/trunk checkout
 
562
                $ cd checkout
 
563
                $ bzr branch --switch ../repo/trunk ../repo/branched
 
564
                2>Branched 0 revision(s).
 
565
                2>Tree is up to date at revision 0.
 
566
                2>Switched to branch:...branched...
 
567
                $ cd ..
 
568
                ''' % locals())
 
569
        bound_branch = branch.Branch.open_containing('checkout')[0]
 
570
        master_branch = branch.Branch.open_containing('repo/branched')[0]
 
571
        return (bound_branch, master_branch)
 
572
 
 
573
    def test_branch_switch_parent_lightweight(self):
 
574
        """Lightweight checkout using bzr branch --switch."""
 
575
        bb, mb = self._checkout_and_branch(option='--lightweight')
 
576
        self.assertParent('repo/trunk', bb)
 
577
        self.assertParent('repo/trunk', mb)
 
578
 
 
579
    def test_branch_switch_parent_heavyweight(self):
 
580
        """Heavyweight checkout using bzr branch --switch."""
 
581
        bb, mb = self._checkout_and_branch()
 
582
        self.assertParent('repo/trunk', bb)
 
583
        self.assertParent('repo/trunk', mb)