/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

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

  • Committer: Aaron Bentley
  • Date: 2006-04-07 22:46:52 UTC
  • mfrom: (1645 +trunk)
  • mto: This revision was merged to the branch mainline in revision 1727.
  • Revision ID: aaron.bentley@utoronto.ca-20060407224652-4925bc3735b926f8
Merged latest bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005 by Canonical Ltd
 
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
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
 
 
18
"""Tests of bound branches (binding, unbinding, commit, etc) command.
 
19
"""
 
20
 
 
21
import os
 
22
from cStringIO import StringIO
 
23
 
 
24
from bzrlib.tests import TestCaseWithTransport
 
25
from bzrlib.branch import Branch
 
26
from bzrlib.bzrdir import (BzrDir,
 
27
                           BzrDirFormat,
 
28
                           BzrDirFormat6,
 
29
                           BzrDirMetaFormat1,
 
30
                           )
 
31
from bzrlib.osutils import getcwd
 
32
from bzrlib.workingtree import WorkingTree
 
33
 
 
34
 
 
35
class TestLegacyFormats(TestCaseWithTransport):
 
36
    
 
37
    def setUp(self):
 
38
        super(TestLegacyFormats, self).setUp()
 
39
        self.build_tree(['master/', 'child/'])
 
40
        self.run_bzr('init', 'master')
 
41
        old_format = BzrDirFormat.get_default_format()
 
42
        BzrDirFormat.set_default_format(BzrDirFormat6())
 
43
        try:
 
44
            self.run_bzr('init', 'child')
 
45
        finally:
 
46
            BzrDirFormat.set_default_format(old_format)
 
47
        os.chdir('child')
 
48
    
 
49
    def test_bind_format_6_bzrdir(self):
 
50
        # bind on a format 6 bzrdir should error
 
51
        out,err = self.run_bzr('bind', '../master', retcode=3)
 
52
        self.assertEqual('', out)
 
53
        self.assertEqual('bzr: ERROR: To use this feature you must '
 
54
                         'upgrade your branch at %s/.\n' % getcwd(), err)
 
55
    
 
56
    def test_unbind_format_6_bzrdir(self):
 
57
        # bind on a format 6 bzrdir should error
 
58
        out,err = self.run_bzr('unbind', retcode=3)
 
59
        self.assertEqual('', out)
 
60
        self.assertEqual('bzr: ERROR: To use this feature you must '
 
61
                         'upgrade your branch at %s/.\n' % getcwd(), err)
 
62
 
 
63
 
 
64
class TestBoundBranches(TestCaseWithTransport):
 
65
 
 
66
    def create_branches(self):
 
67
        bzr = self.run_bzr
 
68
        self.build_tree(['base/', 'base/a', 'base/b'])
 
69
 
 
70
        self.init_meta_branch('base')
 
71
        os.chdir('base')
 
72
        bzr('add')
 
73
        bzr('commit', '-m', 'init')
 
74
 
 
75
        os.chdir('..')
 
76
 
 
77
        bzr('checkout', 'base', 'child')
 
78
 
 
79
        self.failUnlessExists('child')
 
80
 
 
81
        self.check_revno(1, 'child')
 
82
        d = BzrDir.open('child')
 
83
        self.assertNotEqual(None, d.open_branch().get_master_branch())
 
84
 
 
85
    def check_revno(self, val, loc=None):
 
86
        if loc is not None:
 
87
            cwd = os.getcwd()
 
88
            os.chdir(loc)
 
89
        self.assertEquals(str(val), self.run_bzr('revno')[0].strip())
 
90
        if loc is not None:
 
91
            os.chdir(cwd)
 
92
 
 
93
    def test_simple_binding(self):
 
94
        self.build_tree(['base/', 'base/a', 'base/b'])
 
95
 
 
96
        self.init_meta_branch('base')
 
97
        self.run_bzr('add', 'base')
 
98
        self.run_bzr('commit', '-m', 'init', 'base')
 
99
 
 
100
        self.run_bzr('branch', 'base', 'child')
 
101
 
 
102
        os.chdir('child')
 
103
        self.run_bzr('bind', '../base')
 
104
 
 
105
        d = BzrDir.open('')
 
106
        self.assertNotEqual(None, d.open_branch().get_master_branch())
 
107
 
 
108
        self.run_bzr('unbind')
 
109
        self.assertEqual(None, d.open_branch().get_master_branch())
 
110
 
 
111
        self.run_bzr('unbind', retcode=3)
 
112
 
 
113
    def init_meta_branch(self, path):
 
114
        old_format = BzrDirFormat.get_default_format()
 
115
        BzrDirFormat.set_default_format(BzrDirMetaFormat1())
 
116
        try:
 
117
            self.run_bzr('init', path)
 
118
        finally:
 
119
            BzrDirFormat.set_default_format(old_format)
 
120
 
 
121
    def test_bound_commit(self):
 
122
        bzr = self.run_bzr
 
123
        self.create_branches()
 
124
 
 
125
        os.chdir('child')
 
126
        open('a', 'wb').write('new contents\n')
 
127
        bzr('commit', '-m', 'child')
 
128
 
 
129
        self.check_revno(2)
 
130
 
 
131
        # Make sure it committed on the parent
 
132
        self.check_revno(2, '../base')
 
133
 
 
134
    def test_bound_fail(self):
 
135
        # Make sure commit fails if out of date.
 
136
        bzr = self.run_bzr
 
137
        self.create_branches()
 
138
 
 
139
        os.chdir('base')
 
140
        open('a', 'wb').write('new base contents\n')
 
141
        bzr('commit', '-m', 'base')
 
142
        self.check_revno(2)
 
143
 
 
144
        os.chdir('../child')
 
145
        self.check_revno(1)
 
146
        open('b', 'wb').write('new b child contents\n')
 
147
        bzr('commit', '-m', 'child', retcode=3)
 
148
        self.check_revno(1)
 
149
 
 
150
        bzr('update')
 
151
        self.check_revno(2)
 
152
 
 
153
        bzr('commit', '-m', 'child')
 
154
        self.check_revno(3)
 
155
        self.check_revno(3, '../base')
 
156
 
 
157
    def test_double_binding(self):
 
158
        bzr = self.run_bzr
 
159
        self.create_branches()
 
160
 
 
161
        bzr('branch', 'child', 'child2')
 
162
        os.chdir('child2')
 
163
 
 
164
        # Double binding succeeds, but committing to child2 should fail
 
165
        bzr('bind', '../child')
 
166
 
 
167
        bzr('commit', '-m', 'child2', '--unchanged', retcode=3)
 
168
 
 
169
    def test_unbinding(self):
 
170
        bzr = self.run_bzr
 
171
        self.create_branches()
 
172
 
 
173
        os.chdir('base')
 
174
        open('a', 'wb').write('new base contents\n')
 
175
        bzr('commit', '-m', 'base')
 
176
        self.check_revno(2)
 
177
 
 
178
        os.chdir('../child')
 
179
        open('b', 'wb').write('new b child contents\n')
 
180
        self.check_revno(1)
 
181
        bzr('commit', '-m', 'child', retcode=3)
 
182
        self.check_revno(1)
 
183
        bzr('unbind')
 
184
        bzr('commit', '-m', 'child')
 
185
        self.check_revno(2)
 
186
 
 
187
        bzr('bind', retcode=3)
 
188
 
 
189
    def test_commit_remote_bound(self):
 
190
        # It is not possible to commit to a branch
 
191
        # which is bound to a branch which is bound
 
192
        bzr = self.run_bzr
 
193
        self.create_branches()
 
194
        bzr('branch', 'base', 'newbase')
 
195
        os.chdir('base')
 
196
        
 
197
        # There is no way to know that B has already
 
198
        # been bound by someone else, otherwise it
 
199
        # might be nice if this would fail
 
200
        bzr('bind', '../newbase')
 
201
 
 
202
        os.chdir('../child')
 
203
        bzr('commit', '-m', 'failure', '--unchanged', retcode=3)
 
204
 
 
205
    def test_pull_updates_both(self):
 
206
        bzr = self.run_bzr
 
207
        self.create_branches()
 
208
        bzr('branch', 'base', 'newchild')
 
209
        os.chdir('newchild')
 
210
        open('b', 'wb').write('newchild b contents\n')
 
211
        bzr('commit', '-m', 'newchild')
 
212
        self.check_revno(2)
 
213
 
 
214
        os.chdir('../child')
 
215
        # The pull should succeed, and update
 
216
        # the bound parent branch
 
217
        bzr('pull', '../newchild')
 
218
        self.check_revno(2)
 
219
 
 
220
        self.check_revno(2, '../base')
 
221
 
 
222
    def test_bind_diverged(self):
 
223
        bzr = self.run_bzr
 
224
        self.create_branches()
 
225
 
 
226
        os.chdir('child')
 
227
        bzr('unbind')
 
228
 
 
229
        bzr('commit', '-m', 'child', '--unchanged')
 
230
        self.check_revno(2)
 
231
 
 
232
        os.chdir('../base')
 
233
        self.check_revno(1)
 
234
        bzr('commit', '-m', 'base', '--unchanged')
 
235
        self.check_revno(2)
 
236
 
 
237
        os.chdir('../child')
 
238
        # These branches have diverged
 
239
        bzr('bind', '../base', retcode=3)
 
240
 
 
241
        # TODO: In the future, this might require actual changes
 
242
        # to have occurred, rather than just a new revision entry
 
243
        bzr('merge', '../base')
 
244
        bzr('commit', '-m', 'merged')
 
245
        self.check_revno(3)
 
246
 
 
247
        # After a merge, trying to bind again should succeed
 
248
        # by pushing the new change to base
 
249
        bzr('bind', '../base')
 
250
        self.check_revno(3)
 
251
        self.check_revno(3, '../base')
 
252
 
 
253
        # After binding, the revision history should be identical
 
254
        child_rh = bzr('revision-history')[0]
 
255
        os.chdir('../base')
 
256
        base_rh = bzr('revision-history')[0]
 
257
        self.assertEquals(child_rh, base_rh)
 
258
 
 
259
    def test_bind_parent_ahead(self):
 
260
        bzr = self.run_bzr
 
261
        self.create_branches()
 
262
 
 
263
        os.chdir('child')
 
264
        bzr('unbind')
 
265
 
 
266
        os.chdir('../base')
 
267
        bzr('commit', '-m', 'base', '--unchanged')
 
268
 
 
269
        os.chdir('../child')
 
270
        self.check_revno(1)
 
271
        bzr('bind', '../base')
 
272
 
 
273
        self.check_revno(2)
 
274
        bzr('unbind')
 
275
 
 
276
        # Check and make sure it also works if parent is ahead multiple
 
277
        os.chdir('../base')
 
278
        bzr('commit', '-m', 'base 3', '--unchanged')
 
279
        bzr('commit', '-m', 'base 4', '--unchanged')
 
280
        bzr('commit', '-m', 'base 5', '--unchanged')
 
281
        self.check_revno(5)
 
282
 
 
283
        os.chdir('../child')
 
284
        self.check_revno(2)
 
285
        bzr('bind', '../base')
 
286
        self.check_revno(5)
 
287
 
 
288
    def test_bind_child_ahead(self):
 
289
        bzr = self.run_bzr
 
290
        self.create_branches()
 
291
 
 
292
        os.chdir('child')
 
293
        bzr('unbind')
 
294
        bzr('commit', '-m', 'child', '--unchanged')
 
295
        self.check_revno(2)
 
296
        self.check_revno(1, '../base')
 
297
 
 
298
        bzr('bind', '../base')
 
299
        self.check_revno(2, '../base')
 
300
 
 
301
        # Check and make sure it also works if child is ahead multiple
 
302
        bzr('unbind')
 
303
        bzr('commit', '-m', 'child 3', '--unchanged')
 
304
        bzr('commit', '-m', 'child 4', '--unchanged')
 
305
        bzr('commit', '-m', 'child 5', '--unchanged')
 
306
        self.check_revno(5)
 
307
 
 
308
        self.check_revno(2, '../base')
 
309
        bzr('bind', '../base')
 
310
        self.check_revno(5, '../base')
 
311
 
 
312
    def test_commit_after_merge(self):
 
313
        bzr = self.run_bzr
 
314
        self.create_branches()
 
315
 
 
316
        # We want merge to be able to be a local only
 
317
        # operation, because it can be without violating
 
318
        # the binding invariants.
 
319
        # But we can't fail afterwards
 
320
 
 
321
        bzr('branch', 'child', 'other')
 
322
 
 
323
        os.chdir('other')
 
324
        open('c', 'wb').write('file c\n')
 
325
        bzr('add', 'c')
 
326
        bzr('commit', '-m', 'adding c')
 
327
        new_rev_id = bzr('revision-history')[0].strip().split('\n')[-1]
 
328
 
 
329
        os.chdir('../child')
 
330
        bzr('merge', '../other')
 
331
 
 
332
        self.failUnlessExists('c')
 
333
        tree = WorkingTree.open('.')
 
334
        self.assertEqual([new_rev_id], tree.pending_merges())
 
335
 
 
336
        # Make sure the local branch has the installed revision
 
337
        bzr('cat-revision', new_rev_id)
 
338
        
 
339
        # And make sure that the base tree does not
 
340
        os.chdir('../base')
 
341
        bzr('cat-revision', new_rev_id, retcode=3)
 
342
 
 
343
        # Commit should succeed, and cause merged revisions to
 
344
        # be pulled into base
 
345
        os.chdir('../child')
 
346
        bzr('commit', '-m', 'merge other')
 
347
 
 
348
        self.check_revno(2)
 
349
 
 
350
        os.chdir('../base')
 
351
        self.check_revno(2)
 
352
 
 
353
        bzr('cat-revision', new_rev_id)
 
354
 
 
355
    def test_pull_overwrite_fails(self):
 
356
        bzr = self.run_bzr
 
357
        self.create_branches()
 
358
 
 
359
        bzr('branch', 'child', 'other')
 
360
        
 
361
        os.chdir('other')
 
362
        open('a', 'wb').write('new contents\n')
 
363
        bzr('commit', '-m', 'changed a')
 
364
        self.check_revno(2)
 
365
        open('a', 'ab').write('and then some\n')
 
366
        bzr('commit', '-m', 'another a')
 
367
        self.check_revno(3)
 
368
        open('a', 'ab').write('and some more\n')
 
369
        bzr('commit', '-m', 'yet another a')
 
370
        self.check_revno(4)
 
371
 
 
372
        os.chdir('../child')
 
373
        open('a', 'wb').write('also changed a\n')
 
374
        bzr('commit', '-m', 'child modified a')
 
375
 
 
376
        self.check_revno(2)
 
377
        self.check_revno(2, '../base')
 
378
 
 
379
        # It might be possible that we want pull --overwrite to
 
380
        # actually succeed.
 
381
        # If we want it, just change this test to make sure that 
 
382
        # both base and child are updated properly
 
383
        bzr('pull', '--overwrite', '../other', retcode=3)
 
384
 
 
385
        # It should fail without changing the local revision
 
386
        self.check_revno(2)
 
387
        self.check_revno(2, '../base')
 
388
 
 
389
    # TODO: jam 20051230 Test that commit & pull fail when the branch we 
 
390
    #       are bound to is not available
 
391
 
 
392