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