25
25
class TestSwitch(tests.TestCaseWithTransport):
28
super(TestSwitch, self).setUp()
29
self.lightweight = True
27
31
def _setup_tree(self):
28
32
tree = self.make_branch_and_tree('branch-1')
29
33
self.build_tree(['branch-1/file-1'])
40
44
tree.remove('file-1')
41
45
tree.commit('rev2')
42
checkout = tree.branch.create_checkout('checkout', lightweight=True)
46
checkout = tree.branch.create_checkout('checkout',
47
lightweight=self.lightweight)
43
48
self.build_tree(['checkout/file-3'])
44
49
checkout.add('file-3')
45
50
self.failIfExists('checkout/file-1')
52
57
def test_switch_after_branch_moved(self):
53
58
"""Test switch after the branch is moved."""
54
59
tree = self._setup_tree()
55
checkout = tree.branch.create_checkout('checkout', lightweight=True)
60
checkout = tree.branch.create_checkout('checkout',
61
lightweight=self.lightweight)
56
62
self.build_tree(['branch-1/file-2'])
58
64
tree.remove('file-1')
63
69
# rename the branch on disk, the checkout object is now invalid.
64
70
os.rename('branch-1', 'branch-2')
65
71
to_branch = branch.Branch.open('branch-2')
66
switch.switch(checkout.bzrdir, to_branch)
72
# Check fails without --force
73
err = self.assertRaises((errors.NotBranchError,
74
errors.BoundBranchConnectionFailure),
75
switch.switch, checkout.bzrdir, to_branch)
76
switch.switch(checkout.bzrdir, to_branch, force=True)
67
77
self.failIfExists('checkout/file-1')
68
78
self.failUnlessExists('checkout/file-2')
69
79
self.failUnlessExists('checkout/file-3')
71
def test_switch_on_heavy_checkout(self):
72
"""Test graceful failure on heavyweight checkouts."""
73
tree = self._setup_tree()
74
checkout = tree.branch.create_checkout('checkout-1', lightweight=False)
75
branch2 = self.make_branch('branch-2')
76
err = self.assertRaises(errors.BzrCommandError,
77
switch.switch, checkout.bzrdir, branch2)
78
self.assertContainsRe(str(err),
79
"The switch command can only be used on a lightweight checkout")
81
81
def test_switch_when_pending_merges(self):
82
82
"""Test graceful failure if pending merges are outstanding."""
83
83
# Create 2 branches and a checkout
84
84
tree = self._setup_tree()
85
85
tree2 = tree.bzrdir.sprout('branch-2').open_workingtree()
86
checkout = tree.branch.create_checkout('checkout', lightweight=True)
86
checkout = tree.branch.create_checkout('checkout',
87
lightweight=self.lightweight)
87
88
# Change tree2 and merge it into the checkout without committing
88
89
self.build_tree(['branch-2/file-2'])
89
90
tree2.add('file-2')
94
95
switch.switch, checkout.bzrdir, tree2.branch)
95
96
self.assertContainsRe(str(err),
96
97
"Pending merges must be committed or reverted before using switch")
100
class TestSwitchHeavyweight(TestSwitch):
103
super(TestSwitchHeavyweight, self).setUp()
104
self.lightweight = False
106
def test_switch_with_local_commits(self):
107
"""Test switch complains about local commits unless --force given."""
108
tree = self._setup_tree()
109
to_branch = tree.bzrdir.sprout('branch-2').open_branch()
110
self.build_tree(['branch-1/file-2'])
112
tree.remove('file-1')
114
checkout = tree.branch.create_checkout('checkout')
115
self.build_tree(['checkout/file-3'])
116
checkout.add('file-3')
117
checkout.commit(message='local only commit', local=True)
118
self.build_tree(['checkout/file-4'])
119
# Check the error reporting is as expected
120
err = self.assertRaises(errors.BzrCommandError,
121
switch.switch, checkout.bzrdir, to_branch)
122
self.assertContainsRe(str(err),
123
'Cannot switch as local commits found in the checkout.')
124
# Check all is ok when force is given
125
self.failIfExists('checkout/file-1')
126
self.failUnlessExists('checkout/file-2')
127
switch.switch(checkout.bzrdir, to_branch, force=True)
128
self.failUnlessExists('checkout/file-1')
129
self.failIfExists('checkout/file-2')
130
self.failIfExists('checkout/file-3')
131
self.failUnlessExists('checkout/file-4')
132
# Check that the checkout is a true mirror of the bound branch
133
missing_in_checkout = checkout.branch.missing_revisions(to_branch)
134
self.assertEqual([], missing_in_checkout)
135
missing_in_remote = to_branch.missing_revisions(checkout.branch)
136
self.assertEqual([], missing_in_remote)