18
18
"""Tests of bound branches (binding, unbinding, commit, etc) command.
22
23
from cStringIO import StringIO
25
from bzrlib.selftest import TestCaseInTempDir
24
26
from bzrlib.branch import Branch
25
from bzrlib.bzrdir import (BzrDir, BzrDirFormat, BzrDirMetaFormat1)
26
from bzrlib.osutils import getcwd
27
from bzrlib.tests import TestCaseWithTransport
28
import bzrlib.urlutils as urlutils
29
from bzrlib.workingtree import WorkingTree
32
class TestLegacyFormats(TestCaseWithTransport):
35
super(TestLegacyFormats, self).setUp()
36
self.build_tree(['master/', 'child/'])
37
self.run_bzr('init', 'master')
38
self.run_bzr('init', '--format=weave', 'child')
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)
45
# TODO: jam 20060427 Probably something like this really should
46
# print out the actual path, rather than the URL
47
cwd = urlutils.local_path_to_url(getcwd())
48
self.assertEqual('bzr: ERROR: To use this feature you must '
49
'upgrade your branch at %s/.\n' % cwd, err)
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)
55
cwd = urlutils.local_path_to_url(getcwd())
56
self.assertEqual('bzr: ERROR: To use this feature you must '
57
'upgrade your branch at %s/.\n' % cwd, err)
60
class TestBoundBranches(TestCaseWithTransport):
28
class TestBoundBranches(TestCaseInTempDir):
62
30
def create_branches(self):
64
32
self.build_tree(['base/', 'base/a', 'base/b'])
66
branch = self.init_meta_branch('base')
67
tree = branch.bzrdir.open_workingtree()
73
self.run_bzr('checkout', 'base', 'child')
75
self.check_revno(1, 'child')
76
d = BzrDir.open('child')
77
self.assertNotEqual(None, d.open_branch().get_master_branch())
79
def check_revno(self, val, loc='.'):
81
val, len(BzrDir.open(loc).open_branch().revision_history()))
37
bzr('commit', '-m', 'init')
41
bzr('get', '--bound', 'base', 'child')
43
self.failUnlessExists('child')
47
self.failUnlessExists('.bzr/bound')
50
def check_revno(self, val, loc=None):
54
self.assertEquals(self.capture('revno').strip(), str(val))
83
58
def test_simple_binding(self):
84
60
self.build_tree(['base/', 'base/a', 'base/b'])
86
self.init_meta_branch('base')
87
self.run_bzr('add', 'base')
88
self.run_bzr('commit', '-m', 'init', 'base')
65
bzr('commit', '-m', 'init')
90
self.run_bzr('branch', 'base', 'child')
68
bzr('branch', 'base', 'child')
93
self.run_bzr('bind', '../base')
96
self.assertNotEqual(None, d.open_branch().get_master_branch())
98
self.run_bzr('unbind')
99
self.assertEqual(None, d.open_branch().get_master_branch())
101
self.run_bzr('unbind', retcode=3)
103
def init_meta_branch(self, path):
104
old_format = BzrDirFormat.get_default_format()
105
BzrDirFormat.set_default_format(BzrDirMetaFormat1())
107
return BzrDir.create_branch_convenience(
108
path, BzrDirMetaFormat1())
110
BzrDirFormat.set_default_format(old_format)
71
bzr('bind', '../base')
73
self.failUnlessExists('.bzr/bound')
76
self.failIf(os.path.lexists('.bzr/bound'))
78
# TODO: Should unbinding a non-bound branch fail?
112
81
def test_bound_commit(self):
113
82
bzr = self.run_bzr
120
89
self.check_revno(2)
122
91
# Make sure it committed on the parent
123
self.check_revno(2, '../base')
125
95
def test_bound_fail(self):
126
# Make sure commit fails if out of date.
96
"""Make sure commit fails if out of date."""
127
97
bzr = self.run_bzr
128
98
self.create_branches()
131
101
open('a', 'wb').write('new base contents\n')
132
102
bzr('commit', '-m', 'base')
135
104
os.chdir('../child')
137
105
open('b', 'wb').write('new b child contents\n')
138
bzr('commit', '-m', 'child', retcode=3)
106
bzr('commit', '-m', 'child', retcode=1)
142
109
self.check_revno(2)
144
111
bzr('commit', '-m', 'child')
145
112
self.check_revno(3)
146
self.check_revno(3, '../base')
148
116
def test_double_binding(self):
149
117
bzr = self.run_bzr
235
205
bzr('commit', '-m', 'merged')
236
206
self.check_revno(3)
238
# After a merge, trying to bind again should succeed
239
# by pushing the new change to base
208
# This should also be possible by doing a 'bzr push' to the
209
# base, rather than doing 'bzr pull' from it
211
bzr('pull', '../child')
240
215
bzr('bind', '../base')
242
self.check_revno(3, '../base')
244
217
# After binding, the revision history should be identical
245
child_rh = bzr('revision-history')[0]
218
child_rh = self.capture('revision-history')
246
219
os.chdir('../base')
247
base_rh = bzr('revision-history')[0]
220
base_rh = self.capture('revision-history')
248
221
self.assertEquals(child_rh, base_rh)
250
223
def test_bind_parent_ahead(self):
300
270
self.check_revno(5)
302
272
self.check_revno(2, '../base')
303
bzr('bind', '../base')
304
274
self.check_revno(5, '../base')
306
def test_commit_after_merge(self):
308
self.create_branches()
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
315
bzr('branch', 'child', 'other')
318
open('c', 'wb').write('file c\n')
320
bzr('commit', '-m', 'adding c')
321
new_rev_id = bzr('revision-history')[0].strip().split('\n')[-1]
324
bzr('merge', '../other')
326
self.failUnlessExists('c')
327
tree = WorkingTree.open('.')
328
self.assertEqual([new_rev_id], tree.pending_merges())
330
# Make sure the local branch has the installed revision
331
bzr('cat-revision', new_rev_id)
333
# And make sure that the base tree does not
335
bzr('cat-revision', new_rev_id, retcode=3)
337
# Commit should succeed, and cause merged revisions to
338
# be pulled into base
340
bzr('commit', '-m', 'merge other')
347
bzr('cat-revision', new_rev_id)
349
def test_pull_overwrite_fails(self):
351
self.create_branches()
353
bzr('branch', 'child', 'other')
356
open('a', 'wb').write('new contents\n')
357
bzr('commit', '-m', 'changed a')
359
open('a', 'ab').write('and then some\n')
360
bzr('commit', '-m', 'another a')
362
open('a', 'ab').write('and some more\n')
363
bzr('commit', '-m', 'yet another a')
367
open('a', 'wb').write('also changed a\n')
368
bzr('commit', '-m', 'child modified a')
371
self.check_revno(2, '../base')
373
# It might be possible that we want pull --overwrite to
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)
379
# It should fail without changing the local revision
381
self.check_revno(2, '../base')
383
# TODO: jam 20051230 Test that commit & pull fail when the branch we
384
# are bound to is not available