/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1505.1.1 by John Arbash Meinel
Adding test for bound behavior
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
1587.1.6 by Robert Collins
Update bound branch implementation to 0.8.
24
from bzrlib.tests import TestCaseWithTransport
1505.1.1 by John Arbash Meinel
Adding test for bound behavior
25
from bzrlib.branch import Branch
1694.2.6 by Martin Pool
[merge] bzr.dev
26
from bzrlib.bzrdir import (BzrDir, BzrDirFormat, BzrDirMetaFormat1)
1551.2.43 by abentley
Fix bound branch tests
27
from bzrlib.osutils import getcwd
1587.1.6 by Robert Collins
Update bound branch implementation to 0.8.
28
from bzrlib.workingtree import WorkingTree
29
30
31
class TestLegacyFormats(TestCaseWithTransport):
32
    
33
    def setUp(self):
34
        super(TestLegacyFormats, self).setUp()
35
        self.build_tree(['master/', 'child/'])
36
        self.run_bzr('init', 'master')
1694.2.6 by Martin Pool
[merge] bzr.dev
37
        self.run_bzr('init', '--format=weave', 'child')
1587.1.6 by Robert Collins
Update bound branch implementation to 0.8.
38
        os.chdir('child')
39
    
40
    def test_bind_format_6_bzrdir(self):
41
        # bind on a format 6 bzrdir should error
42
        out,err = self.run_bzr('bind', '../master', retcode=3)
43
        self.assertEqual('', out)
44
        self.assertEqual('bzr: ERROR: To use this feature you must '
1551.2.43 by abentley
Fix bound branch tests
45
                         'upgrade your branch at %s/.\n' % getcwd(), err)
1587.1.6 by Robert Collins
Update bound branch implementation to 0.8.
46
    
47
    def test_unbind_format_6_bzrdir(self):
48
        # bind on a format 6 bzrdir should error
49
        out,err = self.run_bzr('unbind', retcode=3)
50
        self.assertEqual('', out)
51
        self.assertEqual('bzr: ERROR: To use this feature you must '
1551.2.43 by abentley
Fix bound branch tests
52
                         'upgrade your branch at %s/.\n' % getcwd(), err)
1587.1.6 by Robert Collins
Update bound branch implementation to 0.8.
53
54
55
class TestBoundBranches(TestCaseWithTransport):
56
1505.1.1 by John Arbash Meinel
Adding test for bound behavior
57
    def create_branches(self):
1505.1.6 by John Arbash Meinel
Cleaned up tests and code, all bound branch tests succeed.
58
        bzr = self.run_bzr
1505.1.1 by John Arbash Meinel
Adding test for bound behavior
59
        self.build_tree(['base/', 'base/a', 'base/b'])
60
1607.1.14 by Robert Collins
Reduce lock thrashing somewhat - drops bound branch tests lock count from 6554 to 4456 locks.
61
        branch = self.init_meta_branch('base')
62
        tree = branch.bzrdir.open_workingtree()
63
        tree.lock_write()
64
        tree.add(['a', 'b'])
65
        tree.commit('init')
66
        tree.unlock()
67
68
        self.run_bzr('checkout', 'base', 'child')
1505.1.1 by John Arbash Meinel
Adding test for bound behavior
69
1505.1.24 by John Arbash Meinel
Updated commit to handle bound branches. Included test to handle commit after merge
70
        self.check_revno(1, 'child')
1587.1.6 by Robert Collins
Update bound branch implementation to 0.8.
71
        d = BzrDir.open('child')
72
        self.assertNotEqual(None, d.open_branch().get_master_branch())
1505.1.1 by John Arbash Meinel
Adding test for bound behavior
73
1607.1.14 by Robert Collins
Reduce lock thrashing somewhat - drops bound branch tests lock count from 6554 to 4456 locks.
74
    def check_revno(self, val, loc='.'):
75
        self.assertEqual(
76
            val, len(BzrDir.open(loc).open_branch().revision_history()))
1505.1.1 by John Arbash Meinel
Adding test for bound behavior
77
1505.1.4 by John Arbash Meinel
Wrote a simple test which actually makes a branch become bound, and made it work
78
    def test_simple_binding(self):
79
        self.build_tree(['base/', 'base/a', 'base/b'])
80
1587.1.6 by Robert Collins
Update bound branch implementation to 0.8.
81
        self.init_meta_branch('base')
82
        self.run_bzr('add', 'base')
83
        self.run_bzr('commit', '-m', 'init', 'base')
1505.1.4 by John Arbash Meinel
Wrote a simple test which actually makes a branch become bound, and made it work
84
1587.1.6 by Robert Collins
Update bound branch implementation to 0.8.
85
        self.run_bzr('branch', 'base', 'child')
1505.1.4 by John Arbash Meinel
Wrote a simple test which actually makes a branch become bound, and made it work
86
87
        os.chdir('child')
1587.1.6 by Robert Collins
Update bound branch implementation to 0.8.
88
        self.run_bzr('bind', '../base')
89
90
        d = BzrDir.open('')
91
        self.assertNotEqual(None, d.open_branch().get_master_branch())
92
93
        self.run_bzr('unbind')
94
        self.assertEqual(None, d.open_branch().get_master_branch())
95
96
        self.run_bzr('unbind', retcode=3)
97
98
    def init_meta_branch(self, path):
99
        old_format = BzrDirFormat.get_default_format()
100
        BzrDirFormat.set_default_format(BzrDirMetaFormat1())
101
        try:
1607.1.14 by Robert Collins
Reduce lock thrashing somewhat - drops bound branch tests lock count from 6554 to 4456 locks.
102
            return BzrDir.create_branch_convenience(
103
                path, BzrDirMetaFormat1())
1587.1.6 by Robert Collins
Update bound branch implementation to 0.8.
104
        finally:
105
            BzrDirFormat.set_default_format(old_format)
1505.1.5 by John Arbash Meinel
Added a test for the unbind command.
106
1505.1.1 by John Arbash Meinel
Adding test for bound behavior
107
    def test_bound_commit(self):
108
        bzr = self.run_bzr
109
        self.create_branches()
110
111
        os.chdir('child')
112
        open('a', 'wb').write('new contents\n')
113
        bzr('commit', '-m', 'child')
114
1505.1.3 by John Arbash Meinel
(broken) Adding more tests, and some functionality
115
        self.check_revno(2)
1505.1.1 by John Arbash Meinel
Adding test for bound behavior
116
117
        # Make sure it committed on the parent
1505.1.24 by John Arbash Meinel
Updated commit to handle bound branches. Included test to handle commit after merge
118
        self.check_revno(2, '../base')
1505.1.1 by John Arbash Meinel
Adding test for bound behavior
119
1505.1.4 by John Arbash Meinel
Wrote a simple test which actually makes a branch become bound, and made it work
120
    def test_bound_fail(self):
1505.1.26 by John Arbash Meinel
Created a set of tests which bind to an sftp branch. Found some bugs, need to fix commit.
121
        # Make sure commit fails if out of date.
1505.1.1 by John Arbash Meinel
Adding test for bound behavior
122
        bzr = self.run_bzr
123
        self.create_branches()
124
125
        os.chdir('base')
126
        open('a', 'wb').write('new base contents\n')
127
        bzr('commit', '-m', 'base')
1587.1.11 by Robert Collins
Local commits appear to be working properly.
128
        self.check_revno(2)
1505.1.1 by John Arbash Meinel
Adding test for bound behavior
129
130
        os.chdir('../child')
1587.1.11 by Robert Collins
Local commits appear to be working properly.
131
        self.check_revno(1)
1505.1.1 by John Arbash Meinel
Adding test for bound behavior
132
        open('b', 'wb').write('new b child contents\n')
1505.1.16 by John Arbash Meinel
[merge] robertc's integration, updated tests to check for retcode=3
133
        bzr('commit', '-m', 'child', retcode=3)
1587.1.11 by Robert Collins
Local commits appear to be working properly.
134
        self.check_revno(1)
1505.1.1 by John Arbash Meinel
Adding test for bound behavior
135
1505.1.13 by John Arbash Meinel
Adding the bzr update command, to update checkouts and bound branches.
136
        bzr('update')
1505.1.3 by John Arbash Meinel
(broken) Adding more tests, and some functionality
137
        self.check_revno(2)
1505.1.1 by John Arbash Meinel
Adding test for bound behavior
138
139
        bzr('commit', '-m', 'child')
1505.1.3 by John Arbash Meinel
(broken) Adding more tests, and some functionality
140
        self.check_revno(3)
1505.1.24 by John Arbash Meinel
Updated commit to handle bound branches. Included test to handle commit after merge
141
        self.check_revno(3, '../base')
1505.1.1 by John Arbash Meinel
Adding test for bound behavior
142
1505.1.2 by John Arbash Meinel
(broken) working on implementing bound branches.
143
    def test_double_binding(self):
144
        bzr = self.run_bzr
145
        self.create_branches()
146
147
        bzr('branch', 'child', 'child2')
148
        os.chdir('child2')
149
1505.1.6 by John Arbash Meinel
Cleaned up tests and code, all bound branch tests succeed.
150
        # Double binding succeeds, but committing to child2 should fail
151
        bzr('bind', '../child')
1505.1.2 by John Arbash Meinel
(broken) working on implementing bound branches.
152
1505.1.16 by John Arbash Meinel
[merge] robertc's integration, updated tests to check for retcode=3
153
        bzr('commit', '-m', 'child2', '--unchanged', retcode=3)
1505.1.2 by John Arbash Meinel
(broken) working on implementing bound branches.
154
1505.1.1 by John Arbash Meinel
Adding test for bound behavior
155
    def test_unbinding(self):
156
        bzr = self.run_bzr
157
        self.create_branches()
158
159
        os.chdir('base')
160
        open('a', 'wb').write('new base contents\n')
161
        bzr('commit', '-m', 'base')
1505.1.3 by John Arbash Meinel
(broken) Adding more tests, and some functionality
162
        self.check_revno(2)
1505.1.1 by John Arbash Meinel
Adding test for bound behavior
163
164
        os.chdir('../child')
165
        open('b', 'wb').write('new b child contents\n')
1505.1.3 by John Arbash Meinel
(broken) Adding more tests, and some functionality
166
        self.check_revno(1)
1505.1.16 by John Arbash Meinel
[merge] robertc's integration, updated tests to check for retcode=3
167
        bzr('commit', '-m', 'child', retcode=3)
1505.1.3 by John Arbash Meinel
(broken) Adding more tests, and some functionality
168
        self.check_revno(1)
1505.1.1 by John Arbash Meinel
Adding test for bound behavior
169
        bzr('unbind')
170
        bzr('commit', '-m', 'child')
1505.1.3 by John Arbash Meinel
(broken) Adding more tests, and some functionality
171
        self.check_revno(2)
1505.1.1 by John Arbash Meinel
Adding test for bound behavior
172
1505.1.16 by John Arbash Meinel
[merge] robertc's integration, updated tests to check for retcode=3
173
        bzr('bind', retcode=3)
1505.1.3 by John Arbash Meinel
(broken) Adding more tests, and some functionality
174
175
    def test_commit_remote_bound(self):
176
        # It is not possible to commit to a branch
177
        # which is bound to a branch which is bound
178
        bzr = self.run_bzr
1505.1.6 by John Arbash Meinel
Cleaned up tests and code, all bound branch tests succeed.
179
        self.create_branches()
1505.1.3 by John Arbash Meinel
(broken) Adding more tests, and some functionality
180
        bzr('branch', 'base', 'newbase')
181
        os.chdir('base')
182
        
183
        # There is no way to know that B has already
184
        # been bound by someone else, otherwise it
185
        # might be nice if this would fail
186
        bzr('bind', '../newbase')
187
188
        os.chdir('../child')
1505.1.16 by John Arbash Meinel
[merge] robertc's integration, updated tests to check for retcode=3
189
        bzr('commit', '-m', 'failure', '--unchanged', retcode=3)
1505.1.3 by John Arbash Meinel
(broken) Adding more tests, and some functionality
190
191
    def test_pull_updates_both(self):
192
        bzr = self.run_bzr
193
        self.create_branches()
194
        bzr('branch', 'base', 'newchild')
195
        os.chdir('newchild')
196
        open('b', 'wb').write('newchild b contents\n')
197
        bzr('commit', '-m', 'newchild')
198
        self.check_revno(2)
199
200
        os.chdir('../child')
201
        # The pull should succeed, and update
202
        # the bound parent branch
203
        bzr('pull', '../newchild')
204
        self.check_revno(2)
205
1505.1.24 by John Arbash Meinel
Updated commit to handle bound branches. Included test to handle commit after merge
206
        self.check_revno(2, '../base')
1505.1.3 by John Arbash Meinel
(broken) Adding more tests, and some functionality
207
208
    def test_bind_diverged(self):
209
        bzr = self.run_bzr
210
        self.create_branches()
211
212
        os.chdir('child')
213
        bzr('unbind')
214
215
        bzr('commit', '-m', 'child', '--unchanged')
216
        self.check_revno(2)
217
218
        os.chdir('../base')
219
        self.check_revno(1)
220
        bzr('commit', '-m', 'base', '--unchanged')
221
        self.check_revno(2)
222
223
        os.chdir('../child')
224
        # These branches have diverged
1505.1.16 by John Arbash Meinel
[merge] robertc's integration, updated tests to check for retcode=3
225
        bzr('bind', '../base', retcode=3)
1505.1.3 by John Arbash Meinel
(broken) Adding more tests, and some functionality
226
227
        # TODO: In the future, this might require actual changes
228
        # to have occurred, rather than just a new revision entry
229
        bzr('merge', '../base')
230
        bzr('commit', '-m', 'merged')
231
        self.check_revno(3)
232
1505.1.13 by John Arbash Meinel
Adding the bzr update command, to update checkouts and bound branches.
233
        # After a merge, trying to bind again should succeed
234
        # by pushing the new change to base
1505.1.3 by John Arbash Meinel
(broken) Adding more tests, and some functionality
235
        bzr('bind', '../base')
1505.1.13 by John Arbash Meinel
Adding the bzr update command, to update checkouts and bound branches.
236
        self.check_revno(3)
237
        self.check_revno(3, '../base')
1505.1.3 by John Arbash Meinel
(broken) Adding more tests, and some functionality
238
239
        # After binding, the revision history should be identical
1505.1.24 by John Arbash Meinel
Updated commit to handle bound branches. Included test to handle commit after merge
240
        child_rh = bzr('revision-history')[0]
1505.1.3 by John Arbash Meinel
(broken) Adding more tests, and some functionality
241
        os.chdir('../base')
1505.1.24 by John Arbash Meinel
Updated commit to handle bound branches. Included test to handle commit after merge
242
        base_rh = bzr('revision-history')[0]
1505.1.3 by John Arbash Meinel
(broken) Adding more tests, and some functionality
243
        self.assertEquals(child_rh, base_rh)
244
245
    def test_bind_parent_ahead(self):
246
        bzr = self.run_bzr
247
        self.create_branches()
248
249
        os.chdir('child')
250
        bzr('unbind')
251
252
        os.chdir('../base')
253
        bzr('commit', '-m', 'base', '--unchanged')
254
255
        os.chdir('../child')
256
        self.check_revno(1)
257
        bzr('bind', '../base')
258
259
        self.check_revno(2)
1505.1.11 by John Arbash Meinel
Adding a little bit more to the test suite.
260
        bzr('unbind')
261
262
        # Check and make sure it also works if parent is ahead multiple
263
        os.chdir('../base')
264
        bzr('commit', '-m', 'base 3', '--unchanged')
265
        bzr('commit', '-m', 'base 4', '--unchanged')
266
        bzr('commit', '-m', 'base 5', '--unchanged')
267
        self.check_revno(5)
268
269
        os.chdir('../child')
270
        self.check_revno(2)
1587.1.14 by Robert Collins
Make bound branch creation happen via 'checkout'
271
        bzr('bind', '../base')
1505.1.11 by John Arbash Meinel
Adding a little bit more to the test suite.
272
        self.check_revno(5)
1505.1.3 by John Arbash Meinel
(broken) Adding more tests, and some functionality
273
274
    def test_bind_child_ahead(self):
1607.1.14 by Robert Collins
Reduce lock thrashing somewhat - drops bound branch tests lock count from 6554 to 4456 locks.
275
        # test binding when the master branches history is a prefix of the 
276
        # childs
1505.1.3 by John Arbash Meinel
(broken) Adding more tests, and some functionality
277
        bzr = self.run_bzr
278
        self.create_branches()
1607.1.14 by Robert Collins
Reduce lock thrashing somewhat - drops bound branch tests lock count from 6554 to 4456 locks.
279
        return
1505.1.3 by John Arbash Meinel
(broken) Adding more tests, and some functionality
280
281
        os.chdir('child')
282
        bzr('unbind')
283
        bzr('commit', '-m', 'child', '--unchanged')
284
        self.check_revno(2)
285
        self.check_revno(1, '../base')
286
287
        bzr('bind', '../base')
288
        self.check_revno(2, '../base')
1505.1.1 by John Arbash Meinel
Adding test for bound behavior
289
1505.1.11 by John Arbash Meinel
Adding a little bit more to the test suite.
290
        # Check and make sure it also works if child is ahead multiple
291
        bzr('unbind')
292
        bzr('commit', '-m', 'child 3', '--unchanged')
293
        bzr('commit', '-m', 'child 4', '--unchanged')
294
        bzr('commit', '-m', 'child 5', '--unchanged')
295
        self.check_revno(5)
296
297
        self.check_revno(2, '../base')
1587.1.14 by Robert Collins
Make bound branch creation happen via 'checkout'
298
        bzr('bind', '../base')
1505.1.11 by John Arbash Meinel
Adding a little bit more to the test suite.
299
        self.check_revno(5, '../base')
300
1505.1.24 by John Arbash Meinel
Updated commit to handle bound branches. Included test to handle commit after merge
301
    def test_commit_after_merge(self):
302
        bzr = self.run_bzr
303
        self.create_branches()
304
305
        # We want merge to be able to be a local only
306
        # operation, because it can be without violating
307
        # the binding invariants.
308
        # But we can't fail afterwards
309
310
        bzr('branch', 'child', 'other')
311
312
        os.chdir('other')
313
        open('c', 'wb').write('file c\n')
314
        bzr('add', 'c')
315
        bzr('commit', '-m', 'adding c')
316
        new_rev_id = bzr('revision-history')[0].strip().split('\n')[-1]
317
318
        os.chdir('../child')
319
        bzr('merge', '../other')
320
321
        self.failUnlessExists('c')
1587.1.6 by Robert Collins
Update bound branch implementation to 0.8.
322
        tree = WorkingTree.open('.')
323
        self.assertEqual([new_rev_id], tree.pending_merges())
1505.1.24 by John Arbash Meinel
Updated commit to handle bound branches. Included test to handle commit after merge
324
325
        # Make sure the local branch has the installed revision
326
        bzr('cat-revision', new_rev_id)
327
        
328
        # And make sure that the base tree does not
329
        os.chdir('../base')
330
        bzr('cat-revision', new_rev_id, retcode=3)
331
332
        # Commit should succeed, and cause merged revisions to
333
        # be pulled into base
334
        os.chdir('../child')
335
        bzr('commit', '-m', 'merge other')
336
337
        self.check_revno(2)
338
339
        os.chdir('../base')
340
        self.check_revno(2)
341
342
        bzr('cat-revision', new_rev_id)
343
1505.1.25 by John Arbash Meinel
Updated pull. Now all paths which call set_revision_history maintain the branch invariant. All tests pass.
344
    def test_pull_overwrite_fails(self):
345
        bzr = self.run_bzr
346
        self.create_branches()
347
348
        bzr('branch', 'child', 'other')
349
        
350
        os.chdir('other')
351
        open('a', 'wb').write('new contents\n')
352
        bzr('commit', '-m', 'changed a')
353
        self.check_revno(2)
354
        open('a', 'ab').write('and then some\n')
355
        bzr('commit', '-m', 'another a')
356
        self.check_revno(3)
357
        open('a', 'ab').write('and some more\n')
358
        bzr('commit', '-m', 'yet another a')
359
        self.check_revno(4)
360
361
        os.chdir('../child')
362
        open('a', 'wb').write('also changed a\n')
363
        bzr('commit', '-m', 'child modified a')
364
365
        self.check_revno(2)
366
        self.check_revno(2, '../base')
367
368
        # It might be possible that we want pull --overwrite to
369
        # actually succeed.
370
        # If we want it, just change this test to make sure that 
371
        # both base and child are updated properly
372
        bzr('pull', '--overwrite', '../other', retcode=3)
373
374
        # It should fail without changing the local revision
375
        self.check_revno(2)
376
        self.check_revno(2, '../base')
1505.1.24 by John Arbash Meinel
Updated commit to handle bound branches. Included test to handle commit after merge
377
1505.1.28 by John Arbash Meinel
todo
378
    # TODO: jam 20051230 Test that commit & pull fail when the branch we 
379
    #       are bound to is not available
380
381