/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

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

  • Committer: Martin von Gagern
  • Date: 2010-04-20 08:47:38 UTC
  • mfrom: (5167 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5195.
  • Revision ID: martin.vgagern@gmx.net-20100420084738-ygymnqmdllzrhpfn
merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007, 2008 Canonical Ltd
 
1
# Copyright (C) 2005-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
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
"""Tests for branch implementations - tests a branch format."""
18
18
 
19
 
import os
20
 
import sys
21
 
 
22
19
from bzrlib import (
23
 
    branch,
 
20
    branch as _mod_branch,
24
21
    bzrdir,
 
22
    delta as _mod_delta,
25
23
    errors,
26
24
    gpg,
 
25
    merge,
27
26
    urlutils,
28
27
    transactions,
 
28
    transport,
29
29
    remote,
30
30
    repository,
 
31
    revision,
31
32
    tests,
32
33
    )
33
 
from bzrlib.branch import Branch, needs_read_lock, needs_write_lock
34
 
from bzrlib.delta import TreeDelta
35
 
from bzrlib.errors import (FileExists,
36
 
                           NoSuchRevision,
37
 
                           NoSuchFile,
38
 
                           UninitializableFormat,
39
 
                           NotBranchError,
40
 
                           )
41
 
from bzrlib.osutils import getcwd
42
 
import bzrlib.revision
43
34
from bzrlib.symbol_versioning import deprecated_in
44
 
from bzrlib.tests import TestCase, TestCaseWithTransport, TestSkipped
45
 
from bzrlib.tests.branch_implementations import TestCaseWithBranch
 
35
from bzrlib.tests import (
 
36
    http_server,
 
37
    per_branch,
 
38
    )
46
39
from bzrlib.tests.http_server import HttpServer
47
 
from bzrlib.trace import mutter
48
 
from bzrlib.transport import get_transport
49
 
from bzrlib.transport.memory import MemoryServer
50
 
from bzrlib.upgrade import upgrade
51
 
from bzrlib.workingtree import WorkingTree
52
 
 
53
 
 
54
 
class TestBranch(TestCaseWithBranch):
 
40
from bzrlib.transport import memory
 
41
 
 
42
 
 
43
class TestTestCaseWithBranch(per_branch.TestCaseWithBranch):
 
44
 
 
45
    def test_branch_format_matches_bzrdir_branch_format(self):
 
46
        bzrdir_branch_format = self.bzrdir_format.get_branch_format()
 
47
        self.assertIs(self.branch_format.__class__,
 
48
                      bzrdir_branch_format.__class__)
 
49
 
 
50
    def test_make_branch_gets_expected_format(self):
 
51
        branch = self.make_branch('.')
 
52
        self.assertIs(self.branch_format.__class__,
 
53
            branch._format.__class__)
 
54
 
 
55
 
 
56
class TestBranch(per_branch.TestCaseWithBranch):
55
57
 
56
58
    def test_create_tree_with_merge(self):
57
59
        tree = self.create_tree_with_merge()
96
98
        wt.commit('lala!', rev_id='revision-1', allow_pointless=False)
97
99
 
98
100
        b2 = self.make_branch('b2')
99
 
        self.assertEqual((1, []), b2.fetch(b1))
 
101
        b2.fetch(b1)
100
102
 
101
103
        rev = b2.repository.get_revision('revision-1')
102
104
        tree = b2.repository.revision_tree('revision-1')
114
116
        tree_a.commit('rev2', rev_id='rev2')
115
117
 
116
118
        delta = tree_a.branch.get_revision_delta(1)
117
 
        self.assertIsInstance(delta, TreeDelta)
 
119
        self.assertIsInstance(delta, _mod_delta.TreeDelta)
118
120
        self.assertEqual([('foo', 'file1', 'file')], delta.added)
119
121
        delta = tree_a.branch.get_revision_delta(2)
120
 
        self.assertIsInstance(delta, TreeDelta)
 
122
        self.assertIsInstance(delta, _mod_delta.TreeDelta)
121
123
        self.assertEqual([('vla', 'file2', 'file')], delta.added)
122
124
 
123
125
    def get_unbalanced_tree_pair(self):
172
174
 
173
175
    def test_clone_branch_nickname(self):
174
176
        # test the nick name is preserved always
175
 
        raise TestSkipped('XXX branch cloning is not yet tested.')
 
177
        raise tests.TestSkipped('XXX branch cloning is not yet tested.')
176
178
 
177
179
    def test_clone_branch_parent(self):
178
180
        # test the parent is preserved always
199
201
        self.assertEqual(branch.get_submit_branch(), 'sftp://example.com')
200
202
        branch.set_submit_branch('sftp://example.net')
201
203
        self.assertEqual(branch.get_submit_branch(), 'sftp://example.net')
202
 
        
 
204
 
203
205
    def test_public_branch(self):
204
206
        """public location can be queried and set"""
205
207
        branch = self.make_branch('branch')
245
247
                          None)
246
248
 
247
249
# TODO 20051003 RBC:
248
 
# compare the gpg-to-sign info for a commit with a ghost and 
 
250
# compare the gpg-to-sign info for a commit with a ghost and
249
251
#     an identical tree without a ghost
250
252
# fetch missing should rewrite the TOC of weaves to list newly available parents.
251
 
        
 
253
 
252
254
    def test_sign_existing_revision(self):
253
255
        wt = self.make_branch_and_tree('.')
254
256
        branch = wt.branch
339
341
 
340
342
    def test_nicks(self):
341
343
        """Test explicit and implicit branch nicknames.
342
 
        
 
344
 
343
345
        Nicknames are implicitly the name of the branch's directory, unless an
344
346
        explicit nickname is set.  That is, an explicit nickname always
345
347
        overrides the implicit one.
346
348
        """
347
 
        t = get_transport(self.get_url())
 
349
        t = transport.get_transport(self.get_url())
348
350
        branch = self.make_branch('bzr.dev')
349
351
        # The nick will be 'bzr.dev', because there is no explicit nick set.
350
352
        self.assertEqual(branch.nick, 'bzr.dev')
351
353
        # Move the branch to a different directory, 'bzr.ab'.  Now that branch
352
354
        # will report its nick as 'bzr.ab'.
353
355
        t.move('bzr.dev', 'bzr.ab')
354
 
        branch = Branch.open(self.get_url('bzr.ab'))
 
356
        branch = _mod_branch.Branch.open(self.get_url('bzr.ab'))
355
357
        self.assertEqual(branch.nick, 'bzr.ab')
356
358
        # Set the branch nick explicitly.  This will ensure there's a branch
357
359
        # config file in the branch.
362
364
        # "Aaron's branch", regardless of directory name.
363
365
        self.assertEqual(branch.nick, "Aaron's branch")
364
366
        t.move('bzr.ab', 'integration')
365
 
        branch = Branch.open(self.get_url('integration'))
 
367
        branch = _mod_branch.Branch.open(self.get_url('integration'))
366
368
        self.assertEqual(branch.nick, "Aaron's branch")
367
369
        branch.nick = u"\u1234"
368
370
        self.assertEqual(branch.nick, u"\u1234")
377
379
        self.assertEqual(committed.properties["branch-nick"],
378
380
                         "My happy branch")
379
381
 
 
382
    def test_create_colocated(self):
 
383
        try:
 
384
            repo = self.make_repository('.', shared=True)
 
385
        except errors.IncompatibleFormat:
 
386
            return
 
387
        self.assertEquals(0, len(repo.bzrdir.list_branches()))
 
388
        try:
 
389
            child_branch1 = self.branch_format.initialize(repo.bzrdir, 
 
390
                name='branch1')
 
391
        except (errors.UninitializableFormat, errors.NoColocatedBranchSupport):
 
392
            # branch references are not default init'able and
 
393
            # not all bzrdirs support colocated branches.
 
394
            return
 
395
        self.assertEquals(1, len(repo.bzrdir.list_branches()))
 
396
        self.branch_format.initialize(repo.bzrdir, name='branch2')
 
397
        self.assertEquals(2, len(repo.bzrdir.list_branches()))
 
398
 
380
399
    def test_create_open_branch_uses_repository(self):
381
400
        try:
382
401
            repo = self.make_repository('.', shared=True)
392
411
            return
393
412
        self.assertEqual(repo.bzrdir.root_transport.base,
394
413
                         child_branch.repository.bzrdir.root_transport.base)
395
 
        child_branch = branch.Branch.open(self.get_url('child'))
 
414
        child_branch = _mod_branch.Branch.open(self.get_url('child'))
396
415
        self.assertEqual(repo.bzrdir.root_transport.base,
397
416
                         child_branch.repository.bzrdir.root_transport.base)
398
417
 
421
440
    def test_generate_revision_history_NULL_REVISION(self):
422
441
        tree = self.make_branch_and_tree('.')
423
442
        rev1 = tree.commit('foo')
424
 
        tree.branch.generate_revision_history(bzrlib.revision.NULL_REVISION)
 
443
        tree.branch.generate_revision_history(revision.NULL_REVISION)
425
444
        self.assertEqual([], tree.branch.revision_history())
426
445
 
427
446
    def test_create_checkout(self):
439
458
        self.assertEqual('rev2', branch_a.last_revision())
440
459
        self.assertEqual(checkout_c.branch.base, branch_a.base)
441
460
 
442
 
        os.mkdir('d')
443
461
        checkout_d = branch_a.create_checkout('d', lightweight=True)
444
462
        self.assertEqual('rev2', checkout_d.last_revision())
445
 
        os.mkdir('e')
446
463
        checkout_e = branch_a.create_checkout('e')
447
464
        self.assertEqual('rev2', checkout_e.last_revision())
448
465
 
451
468
        tree_a = self.make_branch_and_tree('a')
452
469
        rev_id = tree_a.commit('put some content in the branch')
453
470
        # open the branch via a readonly transport
454
 
        source_branch = bzrlib.branch.Branch.open(self.get_readonly_url('a'))
 
471
        source_branch = _mod_branch.Branch.open(self.get_readonly_url('a'))
455
472
        # sanity check that the test will be valid
456
473
        self.assertRaises((errors.LockError, errors.TransportNotPossible),
457
474
            source_branch.lock_write)
463
480
        tree_a = self.make_branch_and_tree('a')
464
481
        rev_id = tree_a.commit('put some content in the branch')
465
482
        # open the branch via a readonly transport
466
 
        source_branch = bzrlib.branch.Branch.open(self.get_readonly_url('a'))
 
483
        source_branch = _mod_branch.Branch.open(self.get_readonly_url('a'))
467
484
        # sanity check that the test will be valid
468
485
        self.assertRaises((errors.LockError, errors.TransportNotPossible),
469
486
            source_branch.lock_write)
480
497
        self.assertEquals(br.revision_history(), [])
481
498
 
482
499
 
483
 
class ChrootedTests(TestCaseWithBranch):
 
500
class TestBranchFormat(per_branch.TestCaseWithBranch):
 
501
 
 
502
    def test_branch_format_network_name(self):
 
503
        br = self.make_branch('.')
 
504
        format = br._format
 
505
        network_name = format.network_name()
 
506
        self.assertIsInstance(network_name, str)
 
507
        # We want to test that the network_name matches the actual format on
 
508
        # disk. For local branches that means that using network_name as a key
 
509
        # in the registry gives back the same format. For remote branches we
 
510
        # check that the network_name of the RemoteBranchFormat we have locally
 
511
        # matches the actual format present on disk.
 
512
        if isinstance(format, remote.RemoteBranchFormat):
 
513
            br._ensure_real()
 
514
            real_branch = br._real_branch
 
515
            self.assertEqual(real_branch._format.network_name(), network_name)
 
516
        else:
 
517
            registry = _mod_branch.network_format_registry
 
518
            looked_up_format = registry.get(network_name)
 
519
            self.assertEqual(format.__class__, looked_up_format.__class__)
 
520
 
 
521
 
 
522
class ChrootedTests(per_branch.TestCaseWithBranch):
484
523
    """A support class that provides readonly urls outside the local namespace.
485
524
 
486
525
    This is done by checking if self.transport_server is a MemoryServer. if it
490
529
 
491
530
    def setUp(self):
492
531
        super(ChrootedTests, self).setUp()
493
 
        if not self.vfs_transport_factory == MemoryServer:
 
532
        if not self.vfs_transport_factory == memory.MemoryServer:
494
533
            self.transport_readonly_server = HttpServer
495
534
 
496
535
    def test_open_containing(self):
497
 
        self.assertRaises(NotBranchError, Branch.open_containing,
 
536
        self.assertRaises(errors.NotBranchError,
 
537
                          _mod_branch.Branch.open_containing,
498
538
                          self.get_readonly_url(''))
499
 
        self.assertRaises(NotBranchError, Branch.open_containing,
 
539
        self.assertRaises(errors.NotBranchError,
 
540
                          _mod_branch.Branch.open_containing,
500
541
                          self.get_readonly_url('g/p/q'))
501
542
        branch = self.make_branch('.')
502
 
        branch, relpath = Branch.open_containing(self.get_readonly_url(''))
 
543
        branch, relpath = _mod_branch.Branch.open_containing(
 
544
            self.get_readonly_url(''))
503
545
        self.assertEqual('', relpath)
504
 
        branch, relpath = Branch.open_containing(self.get_readonly_url('g/p/q'))
 
546
        branch, relpath = _mod_branch.Branch.open_containing(
 
547
            self.get_readonly_url('g/p/q'))
505
548
        self.assertEqual('g/p/q', relpath)
506
 
        
 
549
 
507
550
 
508
551
class InstrumentedTransaction(object):
509
552
 
528
571
    def unlock(self):
529
572
        self._calls.append('ul')
530
573
 
531
 
    @needs_read_lock
 
574
    @_mod_branch.needs_read_lock
532
575
    def do_with_read(self):
533
576
        return 1
534
577
 
535
 
    @needs_read_lock
 
578
    @_mod_branch.needs_read_lock
536
579
    def except_with_read(self):
537
580
        raise RuntimeError
538
581
 
539
 
    @needs_write_lock
 
582
    @_mod_branch.needs_write_lock
540
583
    def do_with_write(self):
541
584
        return 2
542
585
 
543
 
    @needs_write_lock
 
586
    @_mod_branch.needs_write_lock
544
587
    def except_with_write(self):
545
588
        raise RuntimeError
546
589
 
547
590
 
548
 
class TestDecorators(TestCase):
 
591
class TestDecorators(tests.TestCase):
549
592
 
550
593
    def test_needs_read_lock(self):
551
594
        branch = TestDecorator()
568
611
        self.assertEqual(['lw', 'ul'], branch._calls)
569
612
 
570
613
 
571
 
class TestBranchPushLocations(TestCaseWithBranch):
 
614
class TestBranchPushLocations(per_branch.TestCaseWithBranch):
572
615
 
573
616
    def test_get_push_location_unset(self):
574
617
        self.assertEqual(None, self.get_branch().get_push_location())
589
632
        self.assertEqual('foo', branch.get_push_location())
590
633
 
591
634
 
592
 
class TestFormat(TestCaseWithBranch):
 
635
class TestChildSubmitFormats(per_branch.TestCaseWithBranch):
 
636
 
 
637
    def test_get_child_submit_format_default(self):
 
638
        self.assertEqual(None, self.get_branch().get_child_submit_format())
 
639
 
 
640
    def test_get_child_submit_format(self):
 
641
        branch = self.get_branch()
 
642
        branch.get_config().set_user_option('child_submit_format', '10')
 
643
        branch = self.get_branch()
 
644
        self.assertEqual('10', branch.get_child_submit_format())
 
645
 
 
646
 
 
647
class TestFormat(per_branch.TestCaseWithBranch):
593
648
    """Tests for the format itself."""
594
649
 
595
650
    def test_get_reference(self):
629
684
            # they may not be initializable.
630
685
            return
631
686
        # supported formats must be able to init and open
632
 
        t = get_transport(self.get_url())
633
 
        readonly_t = get_transport(self.get_readonly_url())
 
687
        t = transport.get_transport(self.get_url())
 
688
        readonly_t = transport.get_transport(self.get_readonly_url())
634
689
        made_branch = self.make_branch('.')
635
 
        self.failUnless(isinstance(made_branch, branch.Branch))
 
690
        self.failUnless(isinstance(made_branch, _mod_branch.Branch))
636
691
 
637
692
        # find it via bzrdir opening:
638
693
        opened_control = bzrdir.BzrDir.open(readonly_t.base)
643
698
                        self.branch_format.__class__))
644
699
 
645
700
        # find it via Branch.open
646
 
        opened_branch = branch.Branch.open(readonly_t.base)
 
701
        opened_branch = _mod_branch.Branch.open(readonly_t.base)
647
702
        self.failUnless(isinstance(opened_branch, made_branch.__class__))
648
703
        self.assertEqual(made_branch._format.__class__,
649
704
                         opened_branch._format.__class__)
656
711
                         opened_control.find_branch_format())
657
712
 
658
713
 
659
 
class TestBound(TestCaseWithBranch):
 
714
class TestBound(per_branch.TestCaseWithBranch):
660
715
 
661
716
    def test_bind_unbind(self):
662
717
        branch = self.make_branch('1')
694
749
            raise tests.TestNotApplicable('Format does not support binding')
695
750
 
696
751
 
697
 
class TestStrict(TestCaseWithBranch):
 
752
class TestStrict(per_branch.TestCaseWithBranch):
698
753
 
699
754
    def test_strict_history(self):
700
755
        tree1 = self.make_branch_and_tree('tree1')
701
756
        try:
702
757
            tree1.branch.set_append_revisions_only(True)
703
758
        except errors.UpgradeRequired:
704
 
            raise TestSkipped('Format does not support strict history')
 
759
            raise tests.TestSkipped('Format does not support strict history')
705
760
        tree1.commit('empty commit')
706
761
        tree2 = tree1.bzrdir.sprout('tree2').open_workingtree()
707
762
        tree2.commit('empty commit 2')
717
772
        tree3.merge_from_branch(tree2.branch)
718
773
        tree3.commit('empty commit 6')
719
774
        tree2.pull(tree3.branch)
 
775
 
 
776
 
 
777
class TestIgnoreFallbacksParameter(per_branch.TestCaseWithBranch):
 
778
 
 
779
    def make_branch_with_fallback(self):
 
780
        fallback = self.make_branch('fallback')
 
781
        if not fallback._format.supports_stacking():
 
782
            raise tests.TestNotApplicable("format does not support stacking")
 
783
        stacked = self.make_branch('stacked')
 
784
        stacked.set_stacked_on_url(fallback.base)
 
785
        return stacked
 
786
 
 
787
    def test_fallbacks_not_opened(self):
 
788
        stacked = self.make_branch_with_fallback()
 
789
        self.get_transport('').rename('fallback', 'moved')
 
790
        reopened = stacked.bzrdir.open_branch(ignore_fallbacks=True)
 
791
        self.assertEqual([], reopened.repository._fallback_repositories)
 
792
 
 
793
    def test_fallbacks_are_opened(self):
 
794
        stacked = self.make_branch_with_fallback()
 
795
        reopened = stacked.bzrdir.open_branch(ignore_fallbacks=False)
 
796
        self.assertLength(1, reopened.repository._fallback_repositories)
 
797
 
 
798
 
 
799
class TestReferenceLocation(per_branch.TestCaseWithBranch):
 
800
 
 
801
    def test_reference_parent(self):
 
802
        tree = self.make_branch_and_tree('tree')
 
803
        subtree = self.make_branch_and_tree('tree/subtree')
 
804
        subtree.set_root_id('subtree-id')
 
805
        try:
 
806
            tree.add_reference(subtree)
 
807
        except errors.UnsupportedOperation:
 
808
            raise tests.TestNotApplicable('Tree cannot hold references.')
 
809
        reference_parent = tree.branch.reference_parent('subtree-id',
 
810
                                                        'subtree')
 
811
        self.assertEqual(subtree.branch.base, reference_parent.base)
 
812
 
 
813
    def test_reference_parent_accepts_possible_transports(self):
 
814
        tree = self.make_branch_and_tree('tree')
 
815
        subtree = self.make_branch_and_tree('tree/subtree')
 
816
        subtree.set_root_id('subtree-id')
 
817
        try:
 
818
            tree.add_reference(subtree)
 
819
        except errors.UnsupportedOperation:
 
820
            raise tests.TestNotApplicable('Tree cannot hold references.')
 
821
        reference_parent = tree.branch.reference_parent('subtree-id',
 
822
            'subtree', possible_transports=[subtree.bzrdir.root_transport])
 
823
 
 
824
    def test_get_reference_info(self):
 
825
        branch = self.make_branch('branch')
 
826
        try:
 
827
            path, loc = branch.get_reference_info('file-id')
 
828
        except errors.UnsupportedOperation:
 
829
            raise tests.TestNotApplicable('Branch cannot hold references.')
 
830
        self.assertIs(None, path)
 
831
        self.assertIs(None, loc)
 
832
 
 
833
    def test_set_reference_info(self):
 
834
        branch = self.make_branch('branch')
 
835
        try:
 
836
            branch.set_reference_info('file-id', 'path/to/location',
 
837
                                      'path/to/file')
 
838
        except errors.UnsupportedOperation:
 
839
            raise tests.TestNotApplicable('Branch cannot hold references.')
 
840
 
 
841
    def test_set_get_reference_info(self):
 
842
        branch = self.make_branch('branch')
 
843
        try:
 
844
            branch.set_reference_info('file-id', 'path/to/file',
 
845
                                      'path/to/location')
 
846
        except errors.UnsupportedOperation:
 
847
            raise tests.TestNotApplicable('Branch cannot hold references.')
 
848
        # Create a new instance to ensure storage is permanent
 
849
        branch = _mod_branch.Branch.open('branch')
 
850
        tree_path, branch_location = branch.get_reference_info('file-id')
 
851
        self.assertEqual('path/to/location', branch_location)
 
852
 
 
853
    def test_set_null_reference_info(self):
 
854
        branch = self.make_branch('branch')
 
855
        try:
 
856
            branch.set_reference_info('file-id', 'path/to/file',
 
857
                                      'path/to/location')
 
858
        except errors.UnsupportedOperation:
 
859
            raise tests.TestNotApplicable('Branch cannot hold references.')
 
860
        branch.set_reference_info('file-id', None, None)
 
861
        tree_path, branch_location = branch.get_reference_info('file-id')
 
862
        self.assertIs(None, tree_path)
 
863
        self.assertIs(None, branch_location)
 
864
 
 
865
    def test_set_null_reference_info_when_null(self):
 
866
        branch = self.make_branch('branch')
 
867
        try:
 
868
            tree_path, branch_location = branch.get_reference_info('file-id')
 
869
        except errors.UnsupportedOperation:
 
870
            raise tests.TestNotApplicable('Branch cannot hold references.')
 
871
        self.assertIs(None, tree_path)
 
872
        self.assertIs(None, branch_location)
 
873
        branch.set_reference_info('file-id', None, None)
 
874
 
 
875
    def test_set_null_requires_two_nones(self):
 
876
        branch = self.make_branch('branch')
 
877
        try:
 
878
            e = self.assertRaises(ValueError, branch.set_reference_info,
 
879
                                  'file-id', 'path', None)
 
880
        except errors.UnsupportedOperation:
 
881
            raise tests.TestNotApplicable('Branch cannot hold references.')
 
882
        self.assertEqual('tree_path must be None when branch_location is'
 
883
                         ' None.', str(e))
 
884
        e = self.assertRaises(ValueError, branch.set_reference_info,
 
885
                              'file-id', None, 'location')
 
886
        self.assertEqual('branch_location must be None when tree_path is'
 
887
                         ' None.', str(e))
 
888
 
 
889
    def make_branch_with_reference(self, location, reference_location,
 
890
                                   file_id='file-id'):
 
891
        branch = self.make_branch(location)
 
892
        try:
 
893
            branch.set_reference_info(file_id, 'path/to/file',
 
894
                                      reference_location)
 
895
        except errors.UnsupportedOperation:
 
896
            raise tests.TestNotApplicable('Branch cannot hold references.')
 
897
        return branch
 
898
 
 
899
    def test_reference_parent_from_reference_info_(self):
 
900
        referenced_branch = self.make_branch('reference_branch')
 
901
        branch = self.make_branch_with_reference('branch',
 
902
                                                 referenced_branch.base)
 
903
        parent = branch.reference_parent('file-id', 'path/to/file')
 
904
        self.assertEqual(parent.base, referenced_branch.base)
 
905
 
 
906
    def test_branch_relative_reference_location(self):
 
907
        branch = self.make_branch('branch')
 
908
        try:
 
909
            branch.set_reference_info('file-id', 'path/to/file',
 
910
            '../reference_branch')
 
911
        except errors.UnsupportedOperation:
 
912
            raise tests.TestNotApplicable('Branch cannot hold references.')
 
913
        referenced_branch = self.make_branch('reference_branch')
 
914
        parent = branch.reference_parent('file-id', 'path/to/file')
 
915
        self.assertEqual(parent.base, referenced_branch.base)
 
916
 
 
917
    def test_sprout_copies_reference_location(self):
 
918
        branch = self.make_branch_with_reference('branch', '../reference')
 
919
        new_branch = branch.bzrdir.sprout('new-branch').open_branch()
 
920
        self.assertEqual('../reference',
 
921
                         new_branch.get_reference_info('file-id')[1])
 
922
 
 
923
    def test_clone_copies_reference_location(self):
 
924
        branch = self.make_branch_with_reference('branch', '../reference')
 
925
        new_branch = branch.bzrdir.clone('new-branch').open_branch()
 
926
        self.assertEqual('../reference',
 
927
                         new_branch.get_reference_info('file-id')[1])
 
928
 
 
929
    def test_copied_locations_are_rebased(self):
 
930
        branch = self.make_branch_with_reference('branch', 'reference')
 
931
        new_branch = branch.bzrdir.sprout('branch/new-branch').open_branch()
 
932
        self.assertEqual('../reference',
 
933
                         new_branch.get_reference_info('file-id')[1])
 
934
 
 
935
    def test_update_references_retains_old_references(self):
 
936
        branch = self.make_branch_with_reference('branch', 'reference')
 
937
        new_branch = self.make_branch_with_reference(
 
938
            'new_branch', 'reference', 'file-id2')
 
939
        new_branch.update_references(branch)
 
940
        self.assertEqual('reference',
 
941
                         branch.get_reference_info('file-id')[1])
 
942
 
 
943
    def test_update_references_retains_known_references(self):
 
944
        branch = self.make_branch_with_reference('branch', 'reference')
 
945
        new_branch = self.make_branch_with_reference(
 
946
            'new_branch', 'reference2')
 
947
        new_branch.update_references(branch)
 
948
        self.assertEqual('reference',
 
949
                         branch.get_reference_info('file-id')[1])
 
950
 
 
951
    def test_update_references_skips_known_references(self):
 
952
        branch = self.make_branch_with_reference('branch', 'reference')
 
953
        new_branch = branch.bzrdir.sprout('branch/new-branch').open_branch()
 
954
        new_branch.set_reference_info('file-id', '../foo', '../foo')
 
955
        new_branch.update_references(branch)
 
956
        self.assertEqual('reference',
 
957
                         branch.get_reference_info('file-id')[1])
 
958
 
 
959
    def test_pull_updates_references(self):
 
960
        branch = self.make_branch_with_reference('branch', 'reference')
 
961
        new_branch = branch.bzrdir.sprout('branch/new-branch').open_branch()
 
962
        new_branch.set_reference_info('file-id2', '../foo', '../foo')
 
963
        branch.pull(new_branch)
 
964
        self.assertEqual('foo',
 
965
                         branch.get_reference_info('file-id2')[1])
 
966
 
 
967
    def test_push_updates_references(self):
 
968
        branch = self.make_branch_with_reference('branch', 'reference')
 
969
        new_branch = branch.bzrdir.sprout('branch/new-branch').open_branch()
 
970
        new_branch.set_reference_info('file-id2', '../foo', '../foo')
 
971
        new_branch.push(branch)
 
972
        self.assertEqual('foo',
 
973
                         branch.get_reference_info('file-id2')[1])
 
974
 
 
975
    def test_merge_updates_references(self):
 
976
        branch = self.make_branch_with_reference('branch', 'reference')
 
977
        tree = self.make_branch_and_tree('tree')
 
978
        tree.commit('foo')
 
979
        branch.pull(tree.branch)
 
980
        checkout = branch.create_checkout('checkout', lightweight=True)
 
981
        checkout.commit('bar')
 
982
        tree.lock_write()
 
983
        self.addCleanup(tree.unlock)
 
984
        merger = merge.Merger.from_revision_ids(None, tree,
 
985
                                                branch.last_revision(),
 
986
                                                other_branch=branch)
 
987
        merger.merge_type = merge.Merge3Merger
 
988
        merger.do_merge()
 
989
        self.assertEqual('../branch/reference',
 
990
                         tree.branch.get_reference_info('file-id')[1])