~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_switch.py

  • Committer: Martin Pool
  • Date: 2009-03-03 01:45:32 UTC
  • mto: (4070.4.5 gnu-changelog)
  • mto: This revision was merged to the branch mainline in revision 4081.
  • Revision ID: mbp@sourcefrog.net-20090303014532-d223fxy97cb1og8f
Recommend setting timestamp in BranchBuilder

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007 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
"""Tests for bzrlib.switch."""
 
18
 
 
19
 
 
20
import os
 
21
 
 
22
from bzrlib import branch, errors, switch, tests
 
23
 
 
24
 
 
25
class TestSwitch(tests.TestCaseWithTransport):
 
26
 
 
27
    def setUp(self):
 
28
        super(TestSwitch, self).setUp()
 
29
        self.lightweight = True
 
30
 
 
31
    def _setup_tree(self):
 
32
        tree = self.make_branch_and_tree('branch-1')
 
33
        self.build_tree(['branch-1/file-1'])
 
34
        tree.add('file-1')
 
35
        tree.commit('rev1')
 
36
        return tree
 
37
 
 
38
    def test_switch_updates(self):
 
39
        """Test switch updates tree and keeps uncommitted changes."""
 
40
        tree = self._setup_tree()
 
41
        to_branch = tree.bzrdir.sprout('branch-2').open_branch()
 
42
        self.build_tree(['branch-1/file-2'])
 
43
        tree.add('file-2')
 
44
        tree.remove('file-1')
 
45
        tree.commit('rev2')
 
46
        checkout = tree.branch.create_checkout('checkout',
 
47
            lightweight=self.lightweight)
 
48
        self.build_tree(['checkout/file-3'])
 
49
        checkout.add('file-3')
 
50
        self.failIfExists('checkout/file-1')
 
51
        self.failUnlessExists('checkout/file-2')
 
52
        switch.switch(checkout.bzrdir, to_branch)
 
53
        self.failUnlessExists('checkout/file-1')
 
54
        self.failIfExists('checkout/file-2')
 
55
        self.failUnlessExists('checkout/file-3')
 
56
 
 
57
    def test_switch_after_branch_moved(self):
 
58
        """Test switch after the branch is moved."""
 
59
        tree = self._setup_tree()
 
60
        checkout = tree.branch.create_checkout('checkout',
 
61
            lightweight=self.lightweight)
 
62
        self.build_tree(['branch-1/file-2'])
 
63
        tree.add('file-2')
 
64
        tree.remove('file-1')
 
65
        tree.commit('rev2')
 
66
        self.build_tree(['checkout/file-3'])
 
67
        checkout.add('file-3')
 
68
        checkout_dir = checkout.bzrdir
 
69
        # rename the branch on disk, the checkout object is now invalid.
 
70
        os.rename('branch-1', 'branch-2')
 
71
        to_branch = branch.Branch.open('branch-2')
 
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)
 
77
        self.failIfExists('checkout/file-1')
 
78
        self.failUnlessExists('checkout/file-2')
 
79
        self.failUnlessExists('checkout/file-3')
 
80
 
 
81
    def test_switch_when_pending_merges(self):
 
82
        """Test graceful failure if pending merges are outstanding."""
 
83
        # Create 2 branches and a checkout
 
84
        tree = self._setup_tree()
 
85
        tree2 = tree.bzrdir.sprout('branch-2').open_workingtree()
 
86
        checkout = tree.branch.create_checkout('checkout',
 
87
            lightweight=self.lightweight)
 
88
        # Change tree2 and merge it into the checkout without committing
 
89
        self.build_tree(['branch-2/file-2'])
 
90
        tree2.add('file-2')
 
91
        tree2.commit('rev2')
 
92
        checkout.merge_from_branch(tree2.branch)
 
93
        # Check the error reporting is as expected
 
94
        err = self.assertRaises(errors.BzrCommandError,
 
95
            switch.switch, checkout.bzrdir, tree2.branch)
 
96
        self.assertContainsRe(str(err),
 
97
            "Pending merges must be committed or reverted before using switch")
 
98
 
 
99
 
 
100
class TestSwitchHeavyweight(TestSwitch):
 
101
 
 
102
    def setUp(self):
 
103
        super(TestSwitchHeavyweight, self).setUp()
 
104
        self.lightweight = False
 
105
 
 
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'])
 
111
        tree.add('file-2')
 
112
        tree.remove('file-1')
 
113
        tree.commit('rev2')
 
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
        self.assertEqual(to_branch.last_revision_info(),
 
134
                         checkout.branch.last_revision_info())