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