~bzr-pqm/bzr/bzr.dev

6404.6.1 by Vincent Ladeuil
Tests passing for a first rough version of a cached branch config store. The changes here are too invasive and several parallel proposals have been made.
1
# Copyright (C) 2009-2012 Canonical Ltd
4044.1.1 by Robert Collins
Create Branch.create_clone_on_transport helper method to combine bzr and branch creation for push.
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
4044.1.1 by Robert Collins
Create Branch.create_clone_on_transport helper method to combine bzr and branch creation for push.
16
17
"""Tests for branch.create_clone behaviour."""
18
5010.2.14 by Vincent Ladeuil
Fix imports in per_branch/test_create_clone.py.
19
from bzrlib import (
20
    branch,
21
    errors,
22
    remote,
23
    tests,
24
    )
25
from bzrlib.tests import per_branch
26
27
28
class TestCreateClone(per_branch.TestCaseWithBranch):
4044.1.1 by Robert Collins
Create Branch.create_clone_on_transport helper method to combine bzr and branch creation for push.
29
4294.2.1 by Robert Collins
Move directory checking for bzr push options into Branch.create_clone_on_transport.
30
    def test_create_clone_on_transport_missing_parent_dir(self):
31
        tree = self.make_branch_and_tree('source')
32
        tree.commit('a commit')
33
        source = tree.branch
34
        target_transport = self.get_transport('subdir').clone('target')
4294.2.9 by Robert Collins
Fixup tests broken by cleaning up the layering.
35
        self.assertRaises(errors.NoSuchFile,
4294.2.1 by Robert Collins
Move directory checking for bzr push options into Branch.create_clone_on_transport.
36
            tree.branch.create_clone_on_transport, target_transport)
37
        self.assertFalse(self.get_transport('.').has('subdir'))
38
39
    def test_create_clone_on_transport_missing_parent_dir_create(self):
40
        tree = self.make_branch_and_tree('source')
41
        tree.commit('a commit')
42
        source = tree.branch
43
        target_transport = self.get_transport('subdir').clone('target')
44
        result = tree.branch.create_clone_on_transport(target_transport,
45
            create_prefix=True)
46
        self.assertEqual(source.last_revision(), result.last_revision())
47
        self.assertEqual(target_transport.base,
48
            result.bzrdir.root_transport.base)
49
50
    def test_create_clone_on_transport_use_existing_dir_false(self):
51
        tree = self.make_branch_and_tree('source')
52
        tree.commit('a commit')
53
        source = tree.branch
54
        target_transport = self.get_transport('target')
55
        target_transport.create_prefix()
4294.2.9 by Robert Collins
Fixup tests broken by cleaning up the layering.
56
        self.assertRaises(errors.FileExists,
4294.2.1 by Robert Collins
Move directory checking for bzr push options into Branch.create_clone_on_transport.
57
            tree.branch.create_clone_on_transport, target_transport)
58
        self.assertFalse(target_transport.has(".bzr"))
59
60
    def test_create_clone_on_transport_use_existing_dir_true(self):
61
        tree = self.make_branch_and_tree('source')
62
        tree.commit('a commit')
63
        source = tree.branch
64
        target_transport = self.get_transport('target')
65
        target_transport.create_prefix()
66
        result = tree.branch.create_clone_on_transport(target_transport,
67
            use_existing_dir=True)
68
        self.assertEqual(source.last_revision(), result.last_revision())
69
4044.1.1 by Robert Collins
Create Branch.create_clone_on_transport helper method to combine bzr and branch creation for push.
70
    def test_create_clone_on_transport_no_revision_id(self):
71
        tree = self.make_branch_and_tree('source')
72
        tree.commit('a commit')
73
        source = tree.branch
74
        target_transport = self.get_transport('target')
75
        result = tree.branch.create_clone_on_transport(target_transport)
76
        self.assertEqual(source.last_revision(), result.last_revision())
77
78
    def test_create_clone_on_transport_revision_id(self):
79
        tree = self.make_branch_and_tree('source')
80
        old_revid = tree.commit('a commit')
81
        source_tip = tree.commit('a second commit')
82
        source = tree.branch
83
        target_transport = self.get_transport('target')
84
        result = tree.branch.create_clone_on_transport(target_transport,
85
            revision_id=old_revid)
86
        self.assertEqual(old_revid, result.last_revision())
87
        result.lock_read()
88
        self.addCleanup(result.unlock)
89
        self.assertFalse(result.repository.has_revision(source_tip))
90
91
    def test_create_clone_on_transport_stacked(self):
92
        tree = self.make_branch_and_tree('source')
93
        tree.commit('a commit')
94
        trunk = tree.branch.create_clone_on_transport(
95
            self.get_transport('trunk'))
96
        revid = tree.commit('a second commit')
97
        source = tree.branch
98
        target_transport = self.get_transport('target')
6164.2.6 by Jelmer Vernooij
Non-stacking formats can also raise UnstackableRepositoryFormat.
99
        try:
100
            result = tree.branch.create_clone_on_transport(target_transport,
101
                stacked_on=trunk.base)
6164.2.7 by Jelmer Vernooij
Only accept UnstackableRepositoryFormat if the stacked_on does not support full versioned files
102
        except errors.UnstackableBranchFormat:
103
            if not trunk.repository._format.supports_full_versioned_files:
104
                raise tests.TestNotApplicable("can not stack on format")
105
            raise
4044.1.1 by Robert Collins
Create Branch.create_clone_on_transport helper method to combine bzr and branch creation for push.
106
        self.assertEqual(revid, result.last_revision())
107
        self.assertEqual(trunk.base, result.get_stacked_on_url())
4053.2.2 by Andrew Bennetts
Better fix, with test.
108
4053.2.3 by Andrew Bennetts
Fix some nits.
109
    def test_create_clone_of_multiple_roots(self):
4053.2.2 by Andrew Bennetts
Better fix, with test.
110
        try:
111
            builder = self.make_branch_builder('local')
112
        except (errors.TransportNotPossible, errors.UninitializableFormat):
113
            raise tests.TestNotApplicable('format not directly constructable')
114
        builder.start_series()
115
        builder.build_snapshot('rev1', None, [
116
            ('add', ('', 'root-id', 'directory', ''))])
117
        builder.build_snapshot('rev2', ['rev1'], [])
118
        builder.build_snapshot('other', None, [
119
            ('add', ('', 'root-id', 'directory', ''))])
120
        builder.build_snapshot('rev3', ['rev2', 'other'], [])
121
        builder.finish_series()
122
        local = builder.get_branch()
123
        local.bzrdir.clone(self.get_url('remote'), revision_id='rev3')
124
4050.1.1 by Robert Collins
Fix race condition with branch hooks during cloning when the new branch is stacked.
125
    def assertBranchHookBranchIsStacked(self, pre_change_params):
126
        # Just calling will either succeed or fail.
127
        pre_change_params.branch.get_stacked_on_url()
128
        self.hook_calls.append(pre_change_params)
129
130
    def test_create_clone_on_transport_stacked_hooks_get_stacked_branch(self):
131
        tree = self.make_branch_and_tree('source')
132
        tree.commit('a commit')
133
        trunk = tree.branch.create_clone_on_transport(
134
            self.get_transport('trunk'))
135
        revid = tree.commit('a second commit')
136
        target_transport = self.get_transport('target')
137
        self.hook_calls = []
5010.2.14 by Vincent Ladeuil
Fix imports in per_branch/test_create_clone.py.
138
        branch.Branch.hooks.install_named_hook(
139
            'pre_change_branch_tip', self.assertBranchHookBranchIsStacked, None)
6164.2.6 by Jelmer Vernooij
Non-stacking formats can also raise UnstackableRepositoryFormat.
140
        try:
141
            result = tree.branch.create_clone_on_transport(target_transport,
142
                stacked_on=trunk.base)
6164.2.7 by Jelmer Vernooij
Only accept UnstackableRepositoryFormat if the stacked_on does not support full versioned files
143
        except errors.UnstackableBranchFormat:
144
            if not trunk.repository._format.supports_full_versioned_files:
145
                raise tests.TestNotApplicable("can not stack on format")
146
            raise
4050.1.1 by Robert Collins
Fix race condition with branch hooks during cloning when the new branch is stacked.
147
        self.assertEqual(revid, result.last_revision())
148
        self.assertEqual(trunk.base, result.get_stacked_on_url())
149
        # Smart servers invoke hooks on both sides
150
        if isinstance(result, remote.RemoteBranch):
151
            expected_calls = 2
152
        else:
153
            expected_calls = 1
154
        self.assertEqual(expected_calls, len(self.hook_calls))