~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Vincent Ladeuil
  • Date: 2011-11-24 15:48:29 UTC
  • mfrom: (6289 +trunk)
  • mto: This revision was merged to the branch mainline in revision 6337.
  • Revision ID: v.ladeuil+lp@free.fr-20111124154829-avowjpsxdl8yp2vz
merge trunk resolving conflicts

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2012, 2016 Canonical Ltd
 
1
# Copyright (C) 2005-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
16
16
 
17
17
"""Tests for branch implementations - tests a branch format."""
18
18
 
19
 
import contextlib
20
 
 
21
19
from bzrlib import (
22
20
    branch as _mod_branch,
23
 
    controldir,
 
21
    bzrdir,
24
22
    config,
25
23
    delta as _mod_delta,
26
24
    errors,
 
25
    gpg,
27
26
    merge,
28
27
    osutils,
29
28
    urlutils,
30
 
    transform,
31
29
    transport,
32
30
    remote,
33
31
    repository,
34
32
    revision,
35
 
    shelf,
36
33
    symbol_versioning,
37
34
    tests,
38
35
    )
321
318
            repo = self.make_repository('.', shared=True)
322
319
        except errors.IncompatibleFormat:
323
320
            return
324
 
        if repo.bzrdir._format.colocated_branches:
325
 
            raise tests.TestNotApplicable(
326
 
                "control dir does not support colocated branches")
327
 
        self.assertEqual(0, len(repo.bzrdir.list_branches()))
328
 
        if not self.bzrdir_format.colocated_branches:
329
 
            raise tests.TestNotApplicable("control dir format does not support "
330
 
                "colocated branches")
 
321
        self.assertEquals(0, len(repo.bzrdir.list_branches()))
331
322
        try:
332
323
            child_branch1 = self.branch_format.initialize(repo.bzrdir, 
333
324
                name='branch1')
334
 
        except errors.UninitializableFormat:
 
325
        except (errors.UninitializableFormat, errors.NoColocatedBranchSupport):
335
326
            # branch references are not default init'able and
336
327
            # not all bzrdirs support colocated branches.
337
328
            return
338
 
        self.assertEqual(1, len(repo.bzrdir.list_branches()))
 
329
        self.assertEquals(1, len(repo.bzrdir.list_branches()))
339
330
        self.branch_format.initialize(repo.bzrdir, name='branch2')
340
 
        self.assertEqual(2, len(repo.bzrdir.list_branches()))
 
331
        self.assertEquals(2, len(repo.bzrdir.list_branches()))
341
332
 
342
333
    def test_create_append_revisions_only(self):
343
334
        try:
352
343
                # branch references are not default init'able and
353
344
                # not all branches support append_revisions_only
354
345
                return
355
 
            self.assertEqual(True, branch.get_append_revisions_only())
 
346
            self.assertEquals(True, branch.get_append_revisions_only())
356
347
            repo.bzrdir.destroy_branch()
357
348
 
358
349
    def test_get_set_append_revisions_only(self):
479
470
        checkout = source_branch.create_checkout('c')
480
471
        self.assertEqual(rev_id, checkout.last_revision())
481
472
 
 
473
    def test_set_revision_history(self):
 
474
        tree = self.make_branch_and_tree('a')
 
475
        tree.commit('a commit', rev_id='rev1')
 
476
        br = tree.branch
 
477
        self.applyDeprecated(symbol_versioning.deprecated_in((2, 4, 0)),
 
478
            br.set_revision_history, ["rev1"])
 
479
        self.assertEquals(br.last_revision(), "rev1")
 
480
        self.applyDeprecated(symbol_versioning.deprecated_in((2, 4, 0)),
 
481
            br.set_revision_history, [])
 
482
        self.assertEquals(br.last_revision(), 'null:')
 
483
 
482
484
    def test_heads_to_fetch(self):
483
485
        # heads_to_fetch is a method that returns a collection of revids that
484
486
        # need to be fetched to copy this branch into another repo.  At a
521
523
            looked_up_format = registry.get(network_name)
522
524
            self.assertEqual(format.__class__, looked_up_format.__class__)
523
525
 
524
 
    def test_get_config_calls(self):
 
526
    def get_get_config_calls(self):
525
527
        # Smoke test that all branch succeed getting a config
526
528
        br = self.make_branch('.')
527
529
        br.get_config()
649
651
 
650
652
    def test_get_child_submit_format(self):
651
653
        branch = self.get_branch()
652
 
        branch.get_config_stack().set('child_submit_format', '10')
 
654
        branch.get_config().set_user_option('child_submit_format', '10')
653
655
        branch = self.get_branch()
654
656
        self.assertEqual('10', branch.get_child_submit_format())
655
657
 
701
703
        self.assertIsInstance(made_branch, _mod_branch.Branch)
702
704
 
703
705
        # find it via bzrdir opening:
704
 
        opened_control = controldir.ControlDir.open(readonly_t.base)
 
706
        opened_control = bzrdir.BzrDir.open(readonly_t.base)
705
707
        direct_opened_branch = opened_control.open_branch()
706
708
        self.assertEqual(direct_opened_branch.__class__, made_branch.__class__)
707
709
        self.assertEqual(opened_control, direct_opened_branch.bzrdir)
772
774
        branch.unbind()
773
775
        self.assertEqual(None, branch.get_master_branch())
774
776
 
 
777
    def test_unlocked_does_not_cache_master_branch(self):
 
778
        """Unlocked branches do not cache the result of get_master_branch."""
 
779
        master = self.make_branch('master')
 
780
        branch1 = self.make_branch('branch')
 
781
        try:
 
782
            branch1.bind(master)
 
783
        except errors.UpgradeRequired:
 
784
            raise tests.TestNotApplicable('Format does not support binding')
 
785
        # Open branch1 again
 
786
        branch2 = branch1.bzrdir.open_branch()
 
787
        self.assertNotEqual(None, branch1.get_master_branch())
 
788
        # Unbind the branch via branch2.  branch1 isn't locked so will
 
789
        # immediately return the new value for get_master_branch.
 
790
        branch2.unbind()
 
791
        self.assertEqual(None, branch1.get_master_branch())
 
792
 
775
793
    def test_bind_clears_cached_master_branch(self):
776
794
        """b.bind clears any cached value of b.get_master_branch."""
777
795
        master1 = self.make_branch('master1')
842
860
    def test_fallbacks_not_opened(self):
843
861
        stacked = self.make_branch_with_fallback()
844
862
        self.get_transport('').rename('fallback', 'moved')
845
 
        reopened_dir = controldir.ControlDir.open(stacked.base)
 
863
        reopened_dir = bzrdir.BzrDir.open(stacked.base)
846
864
        reopened = reopened_dir.open_branch(ignore_fallbacks=True)
847
865
        self.assertEqual([], reopened.repository._fallback_repositories)
848
866
 
849
867
    def test_fallbacks_are_opened(self):
850
868
        stacked = self.make_branch_with_fallback()
851
 
        reopened_dir = controldir.ControlDir.open(stacked.base)
 
869
        reopened_dir = bzrdir.BzrDir.open(stacked.base)
852
870
        reopened = reopened_dir.open_branch(ignore_fallbacks=False)
853
871
        self.assertLength(1, reopened.repository._fallback_repositories)
854
872
 
1061
1079
        # above the control dir but we might need to relax that?
1062
1080
        self.assertEqual(br.control_url.find(br.user_url), 0)
1063
1081
        self.assertEqual(br.control_url, br.control_transport.base)
1064
 
 
1065
 
 
1066
 
class FakeShelfCreator(object):
1067
 
 
1068
 
    def __init__(self, branch):
1069
 
        self.branch = branch
1070
 
 
1071
 
    def write_shelf(self, shelf_file, message=None):
1072
 
        tree = self.branch.repository.revision_tree(revision.NULL_REVISION)
1073
 
        with transform.TransformPreview(tree) as tt:
1074
 
            shelf.ShelfCreator._write_shelf(
1075
 
                shelf_file, tt, revision.NULL_REVISION)
1076
 
 
1077
 
 
1078
 
@contextlib.contextmanager
1079
 
def skip_if_storing_uncommitted_unsupported():
1080
 
    try:
1081
 
        yield
1082
 
    except errors.StoringUncommittedNotSupported:
1083
 
        raise tests.TestNotApplicable('Cannot store uncommitted changes.')
1084
 
 
1085
 
 
1086
 
class TestUncommittedChanges(per_branch.TestCaseWithBranch):
1087
 
 
1088
 
    def bind(self, branch, master):
1089
 
        try:
1090
 
            branch.bind(master)
1091
 
        except errors.UpgradeRequired:
1092
 
            raise tests.TestNotApplicable('Branch cannot be bound.')
1093
 
 
1094
 
    def test_store_uncommitted(self):
1095
 
        tree = self.make_branch_and_tree('b')
1096
 
        branch = tree.branch
1097
 
        creator = FakeShelfCreator(branch)
1098
 
        with skip_if_storing_uncommitted_unsupported():
1099
 
            self.assertIs(None, branch.get_unshelver(tree))
1100
 
        branch.store_uncommitted(creator)
1101
 
        self.assertIsNot(None, branch.get_unshelver(tree))
1102
 
 
1103
 
    def test_store_uncommitted_bound(self):
1104
 
        tree = self.make_branch_and_tree('b')
1105
 
        branch = tree.branch
1106
 
        master = self.make_branch('master')
1107
 
        self.bind(branch, master)
1108
 
        creator = FakeShelfCreator(tree.branch)
1109
 
        self.assertIs(None, tree.branch.get_unshelver(tree))
1110
 
        self.assertIs(None, master.get_unshelver(tree))
1111
 
        tree.branch.store_uncommitted(creator)
1112
 
        self.assertIsNot(None, master.get_unshelver(tree))
1113
 
 
1114
 
    def test_store_uncommitted_already_stored(self):
1115
 
        branch = self.make_branch('b')
1116
 
        with skip_if_storing_uncommitted_unsupported():
1117
 
            branch.store_uncommitted(FakeShelfCreator(branch))
1118
 
        self.assertRaises(errors.ChangesAlreadyStored,
1119
 
                          branch.store_uncommitted, FakeShelfCreator(branch))
1120
 
 
1121
 
    def test_store_uncommitted_none(self):
1122
 
        branch = self.make_branch('b')
1123
 
        with skip_if_storing_uncommitted_unsupported():
1124
 
            branch.store_uncommitted(FakeShelfCreator(branch))
1125
 
        branch.store_uncommitted(None)
1126
 
        self.assertIs(None, branch.get_unshelver(None))
1127
 
 
1128
 
    def test_get_unshelver(self):
1129
 
        tree = self.make_branch_and_tree('tree')
1130
 
        tree.commit('')
1131
 
        self.build_tree_contents([('tree/file', 'contents1')])
1132
 
        tree.add('file')
1133
 
        with skip_if_storing_uncommitted_unsupported():
1134
 
            tree.store_uncommitted()
1135
 
        unshelver = tree.branch.get_unshelver(tree)
1136
 
        self.assertIsNot(None, unshelver)
1137
 
 
1138
 
    def test_get_unshelver_bound(self):
1139
 
        tree = self.make_branch_and_tree('tree')
1140
 
        tree.commit('')
1141
 
        self.build_tree_contents([('tree/file', 'contents1')])
1142
 
        tree.add('file')
1143
 
        with skip_if_storing_uncommitted_unsupported():
1144
 
            tree.store_uncommitted()
1145
 
        branch = self.make_branch('branch')
1146
 
        self.bind(branch, tree.branch)
1147
 
        unshelver = branch.get_unshelver(tree)
1148
 
        self.assertIsNot(None, unshelver)