~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/blackbox/test_bound_branches.py

  • Committer: Tarmac
  • Author(s): Vincent Ladeuil
  • Date: 2017-01-30 14:42:05 UTC
  • mfrom: (6620.1.1 trunk)
  • Revision ID: tarmac-20170130144205-r8fh2xpmiuxyozpv
Merge  2.7 into trunk including fix for bug #1657238 [r=vila]

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005-2012, 2016 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
17
17
 
18
18
"""Tests of bound branches (binding, unbinding, commit, etc) command."""
19
19
 
20
 
import os
21
 
 
22
20
from bzrlib import (
 
21
    branch,
 
22
    controldir,
23
23
    errors,
24
24
    tests,
25
25
    )
26
 
from bzrlib.branch import Branch
27
 
from bzrlib.bzrdir import BzrDir
28
26
from bzrlib.tests import script
29
27
 
30
28
 
37
35
        base_tree.add(['a', 'b'])
38
36
        base_tree.commit('init')
39
37
        base_tree.unlock()
40
 
        branch = base_tree.branch
41
38
 
42
 
        child_tree = branch.create_checkout('child')
 
39
        child_tree = base_tree.branch.create_checkout('child')
43
40
 
44
41
        self.check_revno(1, 'child')
45
 
        d = BzrDir.open('child')
 
42
        d = controldir.ControlDir.open('child')
46
43
        self.assertNotEqual(None, d.open_branch().get_master_branch())
47
44
 
48
45
        return base_tree, child_tree
49
46
 
50
47
    def check_revno(self, val, loc='.'):
51
48
        self.assertEqual(
52
 
            val, len(BzrDir.open(loc).open_branch().revision_history()))
 
49
            val, controldir.ControlDir.open(loc).open_branch().last_revision_info()[0])
53
50
 
54
51
    def test_simple_binding(self):
55
52
        tree = self.make_branch_and_tree('base')
56
53
        self.build_tree(['base/a', 'base/b'])
57
54
        tree.add('a', 'b')
58
55
        tree.commit(message='init')
59
 
        branch = tree.branch
60
56
 
61
57
        tree.bzrdir.sprout('child')
62
58
 
63
 
        os.chdir('child')
64
 
        self.run_bzr('bind ../base')
 
59
        self.run_bzr('bind ../base', working_dir='child')
65
60
 
66
 
        d = BzrDir.open('')
 
61
        d = controldir.ControlDir.open('child')
67
62
        self.assertNotEqual(None, d.open_branch().get_master_branch())
68
63
 
69
 
        self.run_bzr('unbind')
 
64
        self.run_bzr('unbind', working_dir='child')
70
65
        self.assertEqual(None, d.open_branch().get_master_branch())
71
66
 
72
 
        self.run_bzr('unbind', retcode=3)
 
67
        self.run_bzr('unbind', retcode=3, working_dir='child')
73
68
 
74
69
    def test_bind_branch6(self):
75
70
        branch1 = self.make_branch('branch1', format='dirstate-tags')
76
 
        os.chdir('branch1')
77
 
        error = self.run_bzr('bind', retcode=3)[1]
78
 
        self.assertContainsRe(error, 'no previous location known')
 
71
        error = self.run_bzr('bind', retcode=3, working_dir='branch1')[1]
 
72
        self.assertEndsWith(
 
73
            error, 'No location supplied and no previous location known\n')
79
74
 
80
75
    def setup_rebind(self, format):
81
76
        branch1 = self.make_branch('branch1')
85
80
 
86
81
    def test_rebind_branch6(self):
87
82
        self.setup_rebind('dirstate-tags')
88
 
        os.chdir('branch2')
89
 
        self.run_bzr('bind')
90
 
        b = Branch.open('.')
91
 
        self.assertContainsRe(b.get_bound_location(), '\/branch1\/$')
 
83
        self.run_bzr('bind', working_dir='branch2')
 
84
        b = branch.Branch.open('branch2')
 
85
        self.assertEndsWith(b.get_bound_location(), '/branch1/')
92
86
 
93
87
    def test_rebind_branch5(self):
94
88
        self.setup_rebind('knit')
95
 
        os.chdir('branch2')
96
 
        error = self.run_bzr('bind', retcode=3)[1]
97
 
        self.assertContainsRe(error, 'old locations')
 
89
        error = self.run_bzr('bind', retcode=3, working_dir='branch2')[1]
 
90
        self.assertEndsWith(
 
91
            error, 'No location supplied.  This format does not remember'
 
92
            ' old locations.\n')
98
93
 
99
94
    def test_bound_commit(self):
100
95
        child_tree = self.create_branches()[1]
131
126
 
132
127
    def test_double_binding(self):
133
128
        child_tree = self.create_branches()[1]
134
 
 
135
 
        child2_tree = child_tree.bzrdir.sprout('child2').open_workingtree()
136
 
 
137
 
        os.chdir('child2')
 
129
        child_tree.bzrdir.sprout('child2')
 
130
 
138
131
        # Double binding succeeds, but committing to child2 should fail
139
 
        self.run_bzr('bind ../child')
 
132
        self.run_bzr('bind ../child', working_dir='child2')
140
133
 
 
134
        # Refresh the child tree object as 'unbind' modified it
 
135
        child2_tree = controldir.ControlDir.open('child2').open_workingtree()
141
136
        self.assertRaises(errors.CommitToDoubleBoundBranch,
142
137
                child2_tree.commit, message='child2', allow_pointless=True)
143
138
 
152
147
        self.check_revno(2, 'base')
153
148
 
154
149
        self.check_revno(1, 'child')
155
 
        os.chdir('child')
156
 
        self.run_bzr("commit -m child", retcode=3)
157
 
        self.check_revno(1)
158
 
        self.run_bzr('unbind')
 
150
        self.run_bzr("commit -m child", retcode=3, working_dir='child')
 
151
        self.check_revno(1, 'child')
 
152
        self.run_bzr('unbind', working_dir='child')
 
153
        # Refresh the child tree/branch objects as 'unbind' modified them
 
154
        child_tree = child_tree.bzrdir.open_workingtree()
159
155
        child_tree.commit(message='child')
160
 
        self.check_revno(2)
 
156
        self.check_revno(2, 'child')
161
157
 
162
158
    def test_commit_remote_bound(self):
163
159
        # It is not possible to commit to a branch
165
161
        base_tree, child_tree = self.create_branches()
166
162
        base_tree.bzrdir.sprout('newbase')
167
163
 
168
 
        os.chdir('base')
169
164
        # There is no way to know that B has already
170
165
        # been bound by someone else, otherwise it
171
166
        # might be nice if this would fail
172
 
        self.run_bzr('bind ../newbase')
 
167
        self.run_bzr('bind ../newbase', working_dir='base')
173
168
 
174
 
        os.chdir('../child')
175
 
        self.run_bzr('commit -m failure --unchanged', retcode=3)
 
169
        self.run_bzr('commit -m failure --unchanged', retcode=3,
 
170
                     working_dir='child')
176
171
 
177
172
    def test_pull_updates_both(self):
178
173
        base_tree = self.create_branches()[0]
181
176
        newchild_tree.commit(message='newchild')
182
177
        self.check_revno(2, 'newchild')
183
178
 
184
 
        os.chdir('child')
185
179
        # The pull should succeed, and update
186
180
        # the bound parent branch
187
 
        self.run_bzr('pull ../newchild')
188
 
        self.check_revno(2)
189
 
 
190
 
        self.check_revno(2, '../base')
 
181
        self.run_bzr('pull ../newchild', working_dir='child')
 
182
        self.check_revno(2, 'child')
 
183
        self.check_revno(2, 'base')
191
184
 
192
185
    def test_pull_local_updates_local(self):
193
186
        base_tree = self.create_branches()[0]
196
189
        newchild_tree.commit(message='newchild')
197
190
        self.check_revno(2, 'newchild')
198
191
 
199
 
        os.chdir('child')
200
192
        # The pull should succeed, and update
201
193
        # the bound parent branch
202
 
        self.run_bzr('pull ../newchild --local')
203
 
        self.check_revno(2)
204
 
 
205
 
        self.check_revno(1, '../base')
 
194
        self.run_bzr('pull ../newchild --local', working_dir='child')
 
195
        self.check_revno(2, 'child')
 
196
        self.check_revno(1, 'base')
206
197
 
207
198
    def test_bind_diverged(self):
208
199
        base_tree, child_tree = self.create_branches()
209
200
        base_branch = base_tree.branch
210
201
        child_branch = child_tree.branch
211
202
 
212
 
        os.chdir('child')
213
 
        self.run_bzr('unbind')
 
203
        self.run_bzr('unbind', working_dir='child')
214
204
 
 
205
        # Refresh the child tree/branch objects as 'unbind' modified them
 
206
        child_tree = child_tree.bzrdir.open_workingtree()
215
207
        child_tree.commit(message='child', allow_pointless=True)
216
 
        self.check_revno(2)
 
208
        self.check_revno(2, 'child')
217
209
 
218
 
        os.chdir('..')
219
210
        self.check_revno(1, 'base')
220
211
        base_tree.commit(message='base', allow_pointless=True)
221
212
        self.check_revno(2, 'base')
222
213
 
223
 
        os.chdir('child')
224
214
        # These branches have diverged, but bind should succeed anyway
225
 
        self.run_bzr('bind ../base')
 
215
        self.run_bzr('bind ../base', working_dir='child')
226
216
 
 
217
        # Refresh the child tree/branch objects as 'bind' modified them
 
218
        child_tree = child_tree.bzrdir.open_workingtree()
227
219
        # This should turn the local commit into a merge
228
220
        child_tree.update()
229
221
        child_tree.commit(message='merged')
230
 
        self.check_revno(3)
231
 
 
232
 
        # After binding, the revision history should be unaltered
233
 
        # take a copy before
234
 
        base_history = base_branch.revision_history()
235
 
        child_history = child_branch.revision_history()
 
222
        self.check_revno(3, 'child')
 
223
        self.assertEqual(child_tree.branch.last_revision(),
 
224
                          base_tree.branch.last_revision())
236
225
 
237
226
    def test_bind_parent_ahead(self):
238
227
        base_tree = self.create_branches()[0]
239
228
 
240
 
        os.chdir('child')
241
 
        self.run_bzr('unbind')
 
229
        self.run_bzr('unbind', working_dir='child')
242
230
 
243
231
        base_tree.commit(message='base', allow_pointless=True)
244
232
 
245
 
        self.check_revno(1)
246
 
        self.run_bzr('bind ../base')
 
233
        self.check_revno(1, 'child')
 
234
        self.run_bzr('bind ../base', working_dir='child')
247
235
 
248
236
        # binding does not pull data:
249
 
        self.check_revno(1)
250
 
        self.run_bzr('unbind')
 
237
        self.check_revno(1, 'child')
 
238
        self.run_bzr('unbind', working_dir='child')
251
239
 
252
240
        # Check and make sure it also works if parent is ahead multiple
253
241
        base_tree.commit(message='base 3', allow_pointless=True)
254
242
        base_tree.commit(message='base 4', allow_pointless=True)
255
243
        base_tree.commit(message='base 5', allow_pointless=True)
256
 
        self.check_revno(5, '../base')
 
244
        self.check_revno(5, 'base')
257
245
 
258
 
        self.check_revno(1)
259
 
        self.run_bzr('bind ../base')
260
 
        self.check_revno(1)
 
246
        self.check_revno(1, 'child')
 
247
        self.run_bzr('bind ../base', working_dir='child')
 
248
        self.check_revno(1, 'child')
261
249
 
262
250
    def test_bind_child_ahead(self):
263
251
        # test binding when the master branches history is a prefix of the
265
253
        # be altered
266
254
        child_tree = self.create_branches()[1]
267
255
 
268
 
        os.chdir('child')
269
 
        self.run_bzr('unbind')
 
256
        self.run_bzr('unbind', working_dir='child')
 
257
        # Refresh the child tree/branch objects as 'bind' modified them
 
258
        child_tree = child_tree.bzrdir.open_workingtree()
270
259
        child_tree.commit(message='child', allow_pointless=True)
271
 
        self.check_revno(2)
272
 
        self.check_revno(1, '../base')
 
260
        self.check_revno(2, 'child')
 
261
        self.check_revno(1, 'base')
273
262
 
274
 
        self.run_bzr('bind ../base')
275
 
        self.check_revno(1, '../base')
 
263
        self.run_bzr('bind ../base', working_dir='child')
 
264
        self.check_revno(1, 'base')
276
265
 
277
266
        # Check and make sure it also works if child is ahead multiple
278
 
        self.run_bzr('unbind')
 
267
        self.run_bzr('unbind', working_dir='child')
279
268
        child_tree.commit(message='child 3', allow_pointless=True)
280
269
        child_tree.commit(message='child 4', allow_pointless=True)
281
270
        child_tree.commit(message='child 5', allow_pointless=True)
282
 
        self.check_revno(5)
 
271
        self.check_revno(5, 'child')
283
272
 
284
 
        self.check_revno(1, '../base')
285
 
        self.run_bzr('bind ../base')
286
 
        self.check_revno(1, '../base')
 
273
        self.check_revno(1, 'base')
 
274
        self.run_bzr('bind ../base', working_dir='child')
 
275
        self.check_revno(1, 'base')
287
276
 
288
277
    def test_bind_fail_if_missing(self):
289
278
        """We should not be able to bind to a missing branch."""
290
279
        tree = self.make_branch_and_tree('tree_1')
291
280
        tree.commit('dummy commit')
292
 
        self.run_bzr_error(['Not a branch.*no-such-branch/'], ['bind', '../no-such-branch'],
293
 
                            working_dir='tree_1')
 
281
        self.run_bzr_error(['Not a branch.*no-such-branch/'],
 
282
                           ['bind', '../no-such-branch'],
 
283
                           working_dir='tree_1')
294
284
        self.assertIs(None, tree.branch.get_bound_location())
295
285
 
296
 
    def test_bind_nick(self):
297
 
        """Bind should not update implicit nick."""
298
 
        base = self.make_branch_and_tree('base')
299
 
        child = self.make_branch_and_tree('child')
300
 
        os.chdir('child')
301
 
        self.assertEqual(child.branch.nick, 'child')
302
 
        self.assertEqual(child.branch.get_config().has_explicit_nickname(),
303
 
            False)
304
 
        self.run_bzr('bind ../base')
305
 
        self.assertEqual(child.branch.nick, base.branch.nick)
306
 
        self.assertEqual(child.branch.get_config().has_explicit_nickname(),
307
 
            False)
308
 
 
309
 
    def test_bind_explicit_nick(self):
310
 
        """Bind should update explicit nick."""
311
 
        base = self.make_branch_and_tree('base')
312
 
        child = self.make_branch_and_tree('child')
313
 
        os.chdir('child')
314
 
        child.branch.nick = "explicit_nick"
315
 
        self.assertEqual(child.branch.nick, "explicit_nick")
316
 
        self.assertEqual(child.branch.get_config()._get_explicit_nickname(),
317
 
            "explicit_nick")
318
 
        self.run_bzr('bind ../base')
319
 
        self.assertEqual(child.branch.nick, base.branch.nick)
320
 
        self.assertEqual(child.branch.get_config()._get_explicit_nickname(),
321
 
            base.branch.nick)
322
 
 
323
286
    def test_commit_after_merge(self):
324
287
        base_tree, child_tree = self.create_branches()
325
288
 
333
296
        self.build_tree_contents([('other/c', 'file c\n')])
334
297
        other_tree.add('c')
335
298
        other_tree.commit(message='adding c')
336
 
        new_rev_id = other_branch.revision_history()[-1]
 
299
        new_rev_id = other_branch.last_revision()
337
300
 
338
301
        child_tree.merge_from_branch(other_branch)
339
302
 
348
311
 
349
312
        # Commit should succeed, and cause merged revisions to
350
313
        # be pulled into base
351
 
        os.chdir('child')
352
 
        self.run_bzr(['commit', '-m', 'merge other'])
353
 
 
354
 
        self.check_revno(2)
355
 
 
356
 
        self.check_revno(2, '../base')
357
 
 
 
314
        self.run_bzr(['commit', '-m', 'merge other'], working_dir='child')
 
315
        self.check_revno(2, 'child')
 
316
        self.check_revno(2, 'base')
358
317
        self.assertTrue(base_tree.branch.repository.has_revision(new_rev_id))
359
318
 
360
319
    def test_pull_overwrite(self):
381
340
        self.check_revno(2, 'child')
382
341
        self.check_revno(2, 'base')
383
342
 
384
 
        os.chdir('child')
385
 
        self.run_bzr('pull --overwrite ../other')
 
343
        self.run_bzr('pull --overwrite ../other', working_dir='child')
386
344
 
387
345
        # both the local and master should have been updated.
388
 
        self.check_revno(4)
389
 
        self.check_revno(4, '../base')
 
346
        self.check_revno(4, 'child')
 
347
        self.check_revno(4, 'base')
390
348
 
391
349
    def test_bind_directory(self):
392
350
        """Test --directory option"""
397
355
        branch = tree.branch
398
356
        tree.bzrdir.sprout('child')
399
357
        self.run_bzr('bind --directory=child base')
400
 
        d = BzrDir.open('child')
 
358
        d = controldir.ControlDir.open('child')
401
359
        self.assertNotEqual(None, d.open_branch().get_master_branch())
402
360
        self.run_bzr('unbind -d child')
403
361
        self.assertEqual(None, d.open_branch().get_master_branch())