summaryrefslogtreecommitdiff
path: root/bzrlib/tests/per_branch/test_create_clone.py
blob: a30965dc02fcb34b8792e5195f2a9e0c7a548394 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# Copyright (C) 2009-2012 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

"""Tests for branch.create_clone behaviour."""

from bzrlib import (
    branch,
    errors,
    remote,
    tests,
    )
from bzrlib.tests import per_branch


class TestCreateClone(per_branch.TestCaseWithBranch):

    def test_create_clone_on_transport_missing_parent_dir(self):
        tree = self.make_branch_and_tree('source')
        tree.commit('a commit')
        source = tree.branch
        target_transport = self.get_transport('subdir').clone('target')
        self.assertRaises(errors.NoSuchFile,
            tree.branch.create_clone_on_transport, target_transport)
        self.assertFalse(self.get_transport('.').has('subdir'))

    def test_create_clone_on_transport_missing_parent_dir_create(self):
        tree = self.make_branch_and_tree('source')
        tree.commit('a commit')
        source = tree.branch
        target_transport = self.get_transport('subdir').clone('target')
        result = tree.branch.create_clone_on_transport(target_transport,
            create_prefix=True)
        self.assertEqual(source.last_revision(), result.last_revision())
        self.assertEqual(target_transport.base,
            result.bzrdir.root_transport.base)

    def test_create_clone_on_transport_use_existing_dir_false(self):
        tree = self.make_branch_and_tree('source')
        tree.commit('a commit')
        source = tree.branch
        target_transport = self.get_transport('target')
        target_transport.create_prefix()
        self.assertRaises(errors.FileExists,
            tree.branch.create_clone_on_transport, target_transport)
        self.assertFalse(target_transport.has(".bzr"))

    def test_create_clone_on_transport_use_existing_dir_true(self):
        tree = self.make_branch_and_tree('source')
        tree.commit('a commit')
        source = tree.branch
        target_transport = self.get_transport('target')
        target_transport.create_prefix()
        result = tree.branch.create_clone_on_transport(target_transport,
            use_existing_dir=True)
        self.assertEqual(source.last_revision(), result.last_revision())

    def test_create_clone_on_transport_no_revision_id(self):
        tree = self.make_branch_and_tree('source')
        tree.commit('a commit')
        source = tree.branch
        target_transport = self.get_transport('target')
        result = tree.branch.create_clone_on_transport(target_transport)
        self.assertEqual(source.last_revision(), result.last_revision())

    def test_create_clone_on_transport_revision_id(self):
        tree = self.make_branch_and_tree('source')
        old_revid = tree.commit('a commit')
        source_tip = tree.commit('a second commit')
        source = tree.branch
        target_transport = self.get_transport('target')
        result = tree.branch.create_clone_on_transport(target_transport,
            revision_id=old_revid)
        self.assertEqual(old_revid, result.last_revision())
        result.lock_read()
        self.addCleanup(result.unlock)
        self.assertFalse(result.repository.has_revision(source_tip))

    def test_create_clone_on_transport_stacked(self):
        tree = self.make_branch_and_tree('source')
        tree.commit('a commit')
        trunk = tree.branch.create_clone_on_transport(
            self.get_transport('trunk'))
        revid = tree.commit('a second commit')
        source = tree.branch
        target_transport = self.get_transport('target')
        try:
            result = tree.branch.create_clone_on_transport(target_transport,
                stacked_on=trunk.base)
        except errors.UnstackableBranchFormat:
            if not trunk.repository._format.supports_full_versioned_files:
                raise tests.TestNotApplicable("can not stack on format")
            raise
        self.assertEqual(revid, result.last_revision())
        self.assertEqual(trunk.base, result.get_stacked_on_url())

    def test_create_clone_of_multiple_roots(self):
        try:
            builder = self.make_branch_builder('local')
        except (errors.TransportNotPossible, errors.UninitializableFormat):
            raise tests.TestNotApplicable('format not directly constructable')
        builder.start_series()
        builder.build_snapshot('rev1', None, [
            ('add', ('', 'root-id', 'directory', ''))])
        builder.build_snapshot('rev2', ['rev1'], [])
        builder.build_snapshot('other', None, [
            ('add', ('', 'root-id', 'directory', ''))])
        builder.build_snapshot('rev3', ['rev2', 'other'], [])
        builder.finish_series()
        local = builder.get_branch()
        local.bzrdir.clone(self.get_url('remote'), revision_id='rev3')

    def assertBranchHookBranchIsStacked(self, pre_change_params):
        # Just calling will either succeed or fail.
        pre_change_params.branch.get_stacked_on_url()
        self.hook_calls.append(pre_change_params)

    def test_create_clone_on_transport_stacked_hooks_get_stacked_branch(self):
        tree = self.make_branch_and_tree('source')
        tree.commit('a commit')
        trunk = tree.branch.create_clone_on_transport(
            self.get_transport('trunk'))
        revid = tree.commit('a second commit')
        target_transport = self.get_transport('target')
        self.hook_calls = []
        branch.Branch.hooks.install_named_hook(
            'pre_change_branch_tip', self.assertBranchHookBranchIsStacked, None)
        try:
            result = tree.branch.create_clone_on_transport(target_transport,
                stacked_on=trunk.base)
        except errors.UnstackableBranchFormat:
            if not trunk.repository._format.supports_full_versioned_files:
                raise tests.TestNotApplicable("can not stack on format")
            raise
        self.assertEqual(revid, result.last_revision())
        self.assertEqual(trunk.base, result.get_stacked_on_url())
        # Smart servers invoke hooks on both sides
        if isinstance(result, remote.RemoteBranch):
            expected_calls = 2
        else:
            expected_calls = 1
        self.assertEqual(expected_calls, len(self.hook_calls))