/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
5273.1.5 by Vincent Ladeuil
Merge bzr.dev into cleanup
1
# Copyright (C) 2009, 2010 Canonical Ltd
4000.5.11 by Jelmer Vernooij
Improve tests for InterBranch.pull.
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
4000.5.16 by Jelmer Vernooij
Fix FSF address.
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
4000.5.11 by Jelmer Vernooij
Improve tests for InterBranch.pull.
16
17
"""Tests for InterBranch.pull behaviour."""
18
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
19
from breezy.branch import Branch
20
from breezy.controldir import ControlDir
21
from breezy import errors
22
from breezy.memorytree import MemoryTree
23
from breezy.revision import NULL_REVISION
6874.1.2 by Jelmer Vernooij
Consistently use Branch.user_url.
24
from breezy.tests import TestNotApplicable
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
25
from breezy.tests.per_interbranch import TestCaseWithInterBranch
4000.5.11 by Jelmer Vernooij
Improve tests for InterBranch.pull.
26
27
7143.15.2 by Jelmer Vernooij
Run autopep8.
28
# The tests here are based on the tests in
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
29
# breezy.tests.per_branch.test_pull
4000.5.23 by Jelmer Vernooij
Review feedback from Ian; add some comments about origin of tests, comment on further work in pull.
30
31
4000.5.11 by Jelmer Vernooij
Improve tests for InterBranch.pull.
32
class TestPull(TestCaseWithInterBranch):
33
34
    def test_pull_convergence_simple(self):
35
        # when revisions are pulled, the left-most accessible parents must
36
        # become the revision-history.
4000.5.20 by Jelmer Vernooij
Fix InterBranch.pull tests.
37
        parent = self.make_from_branch_and_tree('parent')
6747.3.1 by Jelmer Vernooij
Avoid more uses of revision_id.
38
        parent.commit('1st post', allow_pointless=True)
6883.21.1 by Jelmer Vernooij
Allow InterBranch formats to raise NoRoundtrippingSupport.
39
        try:
40
            mine = self.sprout_to(parent.controldir, 'mine').open_workingtree()
41
        except errors.NoRoundtrippingSupport:
42
            raise TestNotApplicable(
43
                'lossless push between %r and %r not supported' %
44
                (self.branch_format_from, self.branch_format_to))
6747.3.1 by Jelmer Vernooij
Avoid more uses of revision_id.
45
        mine.commit('my change', allow_pointless=True)
6883.21.2 by Jelmer Vernooij
One more.
46
        try:
47
            parent.merge_from_branch(mine.branch)
48
        except errors.NoRoundtrippingSupport:
49
            raise TestNotApplicable(
50
                'lossless push between %r and %r not supported' %
51
                (self.branch_format_from, self.branch_format_to))
6747.3.1 by Jelmer Vernooij
Avoid more uses of revision_id.
52
        p2 = parent.commit('merge my change')
4000.5.11 by Jelmer Vernooij
Improve tests for InterBranch.pull.
53
        mine.pull(parent.branch)
6747.3.1 by Jelmer Vernooij
Avoid more uses of revision_id.
54
        self.assertEqual(p2, mine.branch.last_revision())
4000.5.11 by Jelmer Vernooij
Improve tests for InterBranch.pull.
55
56
    def test_pull_merged_indirect(self):
57
        # it should be possible to do a pull from one branch into another
58
        # when the tip of the target was merged into the source branch
59
        # via a third branch - so its buried in the ancestry and is not
60
        # directly accessible.
4000.5.20 by Jelmer Vernooij
Fix InterBranch.pull tests.
61
        parent = self.make_from_branch_and_tree('parent')
6747.3.1 by Jelmer Vernooij
Avoid more uses of revision_id.
62
        parent.commit('1st post', allow_pointless=True)
6883.21.1 by Jelmer Vernooij
Allow InterBranch formats to raise NoRoundtrippingSupport.
63
        try:
64
            mine = self.sprout_to(parent.controldir, 'mine').open_workingtree()
65
        except errors.NoRoundtrippingSupport:
66
            raise TestNotApplicable(
67
                'lossless push between %r and %r not supported' %
68
                (self.branch_format_from, self.branch_format_to))
6747.3.1 by Jelmer Vernooij
Avoid more uses of revision_id.
69
        mine.commit('my change', allow_pointless=True)
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
70
        other = self.sprout_to(parent.controldir, 'other').open_workingtree()
4000.5.11 by Jelmer Vernooij
Improve tests for InterBranch.pull.
71
        other.merge_from_branch(mine.branch)
6747.3.1 by Jelmer Vernooij
Avoid more uses of revision_id.
72
        other.commit('merge my change')
6883.21.2 by Jelmer Vernooij
One more.
73
        try:
74
            parent.merge_from_branch(other.branch)
75
        except errors.NoRoundtrippingSupport:
76
            raise TestNotApplicable(
77
                'lossless push between %r and %r not supported' %
78
                (self.branch_format_from, self.branch_format_to))
6747.3.1 by Jelmer Vernooij
Avoid more uses of revision_id.
79
        p2 = parent.commit('merge other')
4000.5.11 by Jelmer Vernooij
Improve tests for InterBranch.pull.
80
        mine.pull(parent.branch)
6747.3.1 by Jelmer Vernooij
Avoid more uses of revision_id.
81
        self.assertEqual(p2, mine.branch.last_revision())
4000.5.11 by Jelmer Vernooij
Improve tests for InterBranch.pull.
82
83
    def test_pull_updates_checkout_and_master(self):
84
        """Pulling into a checkout updates the checkout and the master branch"""
4000.5.20 by Jelmer Vernooij
Fix InterBranch.pull tests.
85
        master_tree = self.make_from_branch_and_tree('master')
6747.3.1 by Jelmer Vernooij
Avoid more uses of revision_id.
86
        master_tree.commit('master')
4000.5.11 by Jelmer Vernooij
Improve tests for InterBranch.pull.
87
        checkout = master_tree.branch.create_checkout('checkout')
6883.21.2 by Jelmer Vernooij
One more.
88
        try:
7143.15.2 by Jelmer Vernooij
Run autopep8.
89
            other = self.sprout_to(
90
                master_tree.branch.controldir, 'other').open_workingtree()
6883.21.2 by Jelmer Vernooij
One more.
91
        except errors.NoRoundtrippingSupport:
92
            raise TestNotApplicable(
93
                'lossless push between %r and %r not supported' %
94
                (self.branch_format_from, self.branch_format_to))
4000.5.11 by Jelmer Vernooij
Improve tests for InterBranch.pull.
95
        rev2 = other.commit('other commit')
96
        # now pull, which should update both checkout and master.
6883.21.3 by Jelmer Vernooij
Some more.
97
        try:
98
            checkout.branch.pull(other.branch)
99
        except errors.NoRoundtrippingSupport:
100
            raise TestNotApplicable(
101
                'lossless push between %r and %r not supported' %
102
                (self.branch_format_from, self.branch_format_to))
6165.4.4 by Jelmer Vernooij
Avoid .revision_history().
103
        self.assertEqual(rev2, checkout.branch.last_revision())
104
        self.assertEqual(rev2, master_tree.branch.last_revision())
4000.5.11 by Jelmer Vernooij
Improve tests for InterBranch.pull.
105
106
    def test_pull_raises_specific_error_on_master_connection_error(self):
4000.5.20 by Jelmer Vernooij
Fix InterBranch.pull tests.
107
        master_tree = self.make_from_branch_and_tree('master')
4000.5.11 by Jelmer Vernooij
Improve tests for InterBranch.pull.
108
        checkout = master_tree.branch.create_checkout('checkout')
7143.15.2 by Jelmer Vernooij
Run autopep8.
109
        other = self.sprout_to(
110
            master_tree.branch.controldir, 'other').open_branch()
4000.5.11 by Jelmer Vernooij
Improve tests for InterBranch.pull.
111
        # move the branch out of the way on disk to cause a connection
112
        # error.
6874.1.2 by Jelmer Vernooij
Consistently use Branch.user_url.
113
        try:
114
            master_tree.branch.controldir.destroy_branch()
115
        except errors.UnsupportedOperation:
116
            raise TestNotApplicable(
117
                'control format does not support destroying default branch')
4000.5.11 by Jelmer Vernooij
Improve tests for InterBranch.pull.
118
        # try to pull, which should raise a BoundBranchConnectionFailure.
119
        self.assertRaises(errors.BoundBranchConnectionFailure,
6747.3.1 by Jelmer Vernooij
Avoid more uses of revision_id.
120
                          checkout.branch.pull, other)
4000.5.11 by Jelmer Vernooij
Improve tests for InterBranch.pull.
121
122
    def test_pull_returns_result(self):
4000.5.20 by Jelmer Vernooij
Fix InterBranch.pull tests.
123
        parent = self.make_from_branch_and_tree('parent')
6747.3.1 by Jelmer Vernooij
Avoid more uses of revision_id.
124
        p1 = parent.commit('1st post')
6883.21.1 by Jelmer Vernooij
Allow InterBranch formats to raise NoRoundtrippingSupport.
125
        try:
126
            mine = self.sprout_to(parent.controldir, 'mine').open_workingtree()
127
        except errors.NoRoundtrippingSupport:
128
            raise TestNotApplicable(
129
                'lossless push between %r and %r not supported' %
130
                (self.branch_format_from, self.branch_format_to))
6747.3.1 by Jelmer Vernooij
Avoid more uses of revision_id.
131
        m1 = mine.commit('my change')
6883.21.2 by Jelmer Vernooij
One more.
132
        try:
133
            result = parent.branch.pull(mine.branch)
134
        except errors.NoRoundtrippingSupport:
135
            raise TestNotApplicable(
136
                'lossless push between %r and %r not supported' %
137
                (self.branch_format_from, self.branch_format_to))
4000.5.11 by Jelmer Vernooij
Improve tests for InterBranch.pull.
138
        self.assertIsNot(None, result)
139
        self.assertIs(mine.branch, result.source_branch)
140
        self.assertIs(parent.branch, result.target_branch)
141
        self.assertIs(parent.branch, result.master_branch)
142
        self.assertIs(None, result.local_branch)
143
        self.assertEqual(1, result.old_revno)
6747.3.1 by Jelmer Vernooij
Avoid more uses of revision_id.
144
        self.assertEqual(p1, result.old_revid)
4000.5.11 by Jelmer Vernooij
Improve tests for InterBranch.pull.
145
        self.assertEqual(2, result.new_revno)
6747.3.1 by Jelmer Vernooij
Avoid more uses of revision_id.
146
        self.assertEqual(m1, result.new_revid)
6112.4.7 by Jelmer Vernooij
Fix tests.
147
        self.assertEqual([], result.tag_conflicts)
4000.5.11 by Jelmer Vernooij
Improve tests for InterBranch.pull.
148
149
    def test_pull_overwrite(self):
4000.5.20 by Jelmer Vernooij
Fix InterBranch.pull tests.
150
        tree_a = self.make_from_branch_and_tree('tree_a')
4000.5.11 by Jelmer Vernooij
Improve tests for InterBranch.pull.
151
        tree_a.commit('message 1')
6883.21.1 by Jelmer Vernooij
Allow InterBranch formats to raise NoRoundtrippingSupport.
152
        try:
7143.15.2 by Jelmer Vernooij
Run autopep8.
153
            tree_b = self.sprout_to(
154
                tree_a.controldir, 'tree_b').open_workingtree()
6883.21.1 by Jelmer Vernooij
Allow InterBranch formats to raise NoRoundtrippingSupport.
155
        except errors.NoRoundtrippingSupport:
156
            raise TestNotApplicable(
157
                'lossless push between %r and %r not supported' %
158
                (self.branch_format_from, self.branch_format_to))
159
6862.1.1 by Jelmer Vernooij
Add some uniqueness to commit messages.
160
        rev2a = tree_a.commit('message 2a')
161
        rev2b = tree_b.commit('message 2b')
6883.21.3 by Jelmer Vernooij
Some more.
162
        try:
7143.15.2 by Jelmer Vernooij
Run autopep8.
163
            self.assertRaises(errors.DivergedBranches,
164
                              tree_a.pull, tree_b.branch)
6883.21.3 by Jelmer Vernooij
Some more.
165
        except errors.NoRoundtrippingSupport:
166
            raise TestNotApplicable(
167
                'lossless push between %r and %r not supported' %
168
                (self.branch_format_from, self.branch_format_to))
4000.5.11 by Jelmer Vernooij
Improve tests for InterBranch.pull.
169
        self.assertRaises(errors.DivergedBranches,
170
                          tree_a.branch.pull, tree_b.branch,
6747.3.1 by Jelmer Vernooij
Avoid more uses of revision_id.
171
                          overwrite=False, stop_revision=rev2b)
4000.5.11 by Jelmer Vernooij
Improve tests for InterBranch.pull.
172
        # It should not have updated the branch tip, but it should have fetched
6217.4.1 by Jelmer Vernooij
Add RepositoryFormat.supports_invisible_revisions.
173
        # the revision if the repository supports "invisible" revisions.
6747.3.1 by Jelmer Vernooij
Avoid more uses of revision_id.
174
        self.assertEqual(rev2a, tree_a.branch.last_revision())
6217.4.2 by Jelmer Vernooij
s/invisible/unreferenced.
175
        if tree_a.branch.repository._format.supports_unreferenced_revisions:
6747.3.1 by Jelmer Vernooij
Avoid more uses of revision_id.
176
            self.assertTrue(tree_a.branch.repository.has_revision(rev2b))
4000.5.11 by Jelmer Vernooij
Improve tests for InterBranch.pull.
177
        tree_a.branch.pull(tree_b.branch, overwrite=True,
6747.3.1 by Jelmer Vernooij
Avoid more uses of revision_id.
178
                           stop_revision=rev2b)
179
        self.assertEqual(rev2b, tree_a.branch.last_revision())
6165.4.4 by Jelmer Vernooij
Avoid .revision_history().
180
        self.assertEqual(tree_b.branch.last_revision(),
181
                         tree_a.branch.last_revision())
4000.5.11 by Jelmer Vernooij
Improve tests for InterBranch.pull.
182
183
184
class TestPullHook(TestCaseWithInterBranch):
185
186
    def setUp(self):
187
        self.hook_calls = []
6552.1.4 by Vincent Ladeuil
Remaining tests matching setup(self) that can be rewritten with super().
188
        super(TestPullHook, self).setUp()
4000.5.11 by Jelmer Vernooij
Improve tests for InterBranch.pull.
189
190
    def capture_post_pull_hook(self, result):
191
        """Capture post pull hook calls to self.hook_calls.
192
193
        The call is logged, as is some state of the two branches.
194
        """
195
        if result.local_branch:
196
            local_locked = result.local_branch.is_locked()
197
            local_base = result.local_branch.base
198
        else:
199
            local_locked = None
200
            local_base = None
201
        self.hook_calls.append(
202
            ('post_pull', result.source_branch, local_base,
203
             result.master_branch.base, result.old_revno,
204
             result.old_revid,
205
             result.new_revno, result.new_revid,
206
             result.source_branch.is_locked(), local_locked,
207
             result.master_branch.is_locked()))
208
209
    def test_post_pull_empty_history(self):
210
        target = self.make_to_branch('target')
4000.5.20 by Jelmer Vernooij
Fix InterBranch.pull tests.
211
        source = self.make_from_branch('source')
4000.5.11 by Jelmer Vernooij
Improve tests for InterBranch.pull.
212
        Branch.hooks.install_named_hook('post_pull',
7143.15.2 by Jelmer Vernooij
Run autopep8.
213
                                        self.capture_post_pull_hook, None)
4000.5.11 by Jelmer Vernooij
Improve tests for InterBranch.pull.
214
        target.pull(source)
215
        # with nothing there we should still get a notification, and
216
        # have both branches locked at the notification time.
217
        self.assertEqual([
218
            ('post_pull', source, None, target.base, 0, NULL_REVISION,
219
             0, NULL_REVISION, True, None, True)
220
            ],
221
            self.hook_calls)
222
223
    def test_post_pull_bound_branch(self):
224
        # pulling to a bound branch should pass in the master branch to the
225
        # hook, allowing the correct number of emails to be sent, while still
226
        # allowing hooks that want to modify the target to do so to both
227
        # instances.
228
        target = self.make_to_branch('target')
4000.5.20 by Jelmer Vernooij
Fix InterBranch.pull tests.
229
        local = self.make_from_branch('local')
4000.5.11 by Jelmer Vernooij
Improve tests for InterBranch.pull.
230
        try:
231
            local.bind(target)
232
        except errors.UpgradeRequired:
233
            # We can't bind this format to itself- typically it is the local
234
            # branch that doesn't support binding.  As of May 2007
235
            # remotebranches can't be bound.  Let's instead make a new local
236
            # branch of the default type, which does allow binding.
237
            # See https://bugs.launchpad.net/bzr/+bug/112020
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
238
            local = ControlDir.create_branch_convenience('local2')
4000.5.11 by Jelmer Vernooij
Improve tests for InterBranch.pull.
239
            local.bind(target)
4000.5.20 by Jelmer Vernooij
Fix InterBranch.pull tests.
240
        source = self.make_from_branch('source')
4000.5.11 by Jelmer Vernooij
Improve tests for InterBranch.pull.
241
        Branch.hooks.install_named_hook('post_pull',
7143.15.2 by Jelmer Vernooij
Run autopep8.
242
                                        self.capture_post_pull_hook, None)
4000.5.11 by Jelmer Vernooij
Improve tests for InterBranch.pull.
243
        local.pull(source)
244
        # with nothing there we should still get a notification, and
245
        # have both branches locked at the notification time.
246
        self.assertEqual([
247
            ('post_pull', source, local.base, target.base, 0, NULL_REVISION,
248
             0, NULL_REVISION, True, True, True)
249
            ],
250
            self.hook_calls)
251
252
    def test_post_pull_nonempty_history(self):
253
        target = self.make_to_branch_and_memory_tree('target')
254
        target.lock_write()
255
        target.add('')
256
        rev1 = target.commit('rev 1')
257
        target.unlock()
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
258
        sourcedir = target.controldir.clone(self.get_url('source'))
4000.5.11 by Jelmer Vernooij
Improve tests for InterBranch.pull.
259
        source = MemoryTree.create_on_branch(sourcedir.open_branch())
260
        rev2 = source.commit('rev 2')
261
        Branch.hooks.install_named_hook('post_pull',
7143.15.2 by Jelmer Vernooij
Run autopep8.
262
                                        self.capture_post_pull_hook, None)
4000.5.11 by Jelmer Vernooij
Improve tests for InterBranch.pull.
263
        target.branch.pull(source.branch)
264
        # with nothing there we should still get a notification, and
265
        # have both branches locked at the notification time.
266
        self.assertEqual([
267
            ('post_pull', source.branch, None, target.branch.base, 1, rev1,
268
             2, rev2, True, None, True)
269
            ],
270
            self.hook_calls)