~bzr-pqm/bzr/bzr.dev

2999.1.1 by Ian Clatworthy
migrate switch command into the core - was in BzrTools
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
2999.1.3 by Ian Clatworthy
fix pending merge detection and test
27
    def _setup_tree(self):
2999.1.1 by Ian Clatworthy
migrate switch command into the core - was in BzrTools
28
        tree = self.make_branch_and_tree('branch-1')
29
        self.build_tree(['branch-1/file-1'])
30
        tree.add('file-1')
31
        tree.commit('rev1')
2999.1.3 by Ian Clatworthy
fix pending merge detection and test
32
        return tree
2999.1.1 by Ian Clatworthy
migrate switch command into the core - was in BzrTools
33
2999.1.3 by Ian Clatworthy
fix pending merge detection and test
34
    def test_switch_updates(self):
35
        """Test switch updates tree and keeps uncommitted changes."""
36
        tree = self._setup_tree()
2999.1.1 by Ian Clatworthy
migrate switch command into the core - was in BzrTools
37
        to_branch = tree.bzrdir.sprout('branch-2').open_branch()
38
        self.build_tree(['branch-1/file-2'])
39
        tree.add('file-2')
40
        tree.remove('file-1')
41
        tree.commit('rev2')
42
        checkout = tree.branch.create_checkout('checkout', lightweight=True)
43
        self.build_tree(['checkout/file-3'])
44
        checkout.add('file-3')
45
        self.failIfExists('checkout/file-1')
46
        self.failUnlessExists('checkout/file-2')
47
        switch.switch(checkout.bzrdir, to_branch)
48
        self.failUnlessExists('checkout/file-1')
49
        self.failIfExists('checkout/file-2')
50
        self.failUnlessExists('checkout/file-3')
51
2999.1.3 by Ian Clatworthy
fix pending merge detection and test
52
    def test_switch_after_branch_moved(self):
53
        """Test switch after the branch is moved."""
54
        tree = self._setup_tree()
55
        checkout = tree.branch.create_checkout('checkout', lightweight=True)
56
        self.build_tree(['branch-1/file-2'])
57
        tree.add('file-2')
58
        tree.remove('file-1')
59
        tree.commit('rev2')
3044.1.4 by Martin Pool
Set default format to pack-0.92
60
        self.build_tree(['checkout/file-3'])
61
        checkout.add('file-3')
62
        checkout_dir = checkout.bzrdir
63
        # rename the branch on disk, the checkout object is now invalid.
2999.1.3 by Ian Clatworthy
fix pending merge detection and test
64
        os.rename('branch-1', 'branch-2')
65
        to_branch = branch.Branch.open('branch-2')
66
        switch.switch(checkout.bzrdir, to_branch)
67
        self.failIfExists('checkout/file-1')
68
        self.failUnlessExists('checkout/file-2')
69
        self.failUnlessExists('checkout/file-3')
70
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)
2999.1.1 by Ian Clatworthy
migrate switch command into the core - was in BzrTools
75
        branch2 = self.make_branch('branch-2')
76
        err = self.assertRaises(errors.BzrCommandError,
2999.1.3 by Ian Clatworthy
fix pending merge detection and test
77
            switch.switch, checkout.bzrdir, branch2)
2999.1.1 by Ian Clatworthy
migrate switch command into the core - was in BzrTools
78
        self.assertContainsRe(str(err),
79
            "The switch command can only be used on a lightweight checkout")
2999.1.3 by Ian Clatworthy
fix pending merge detection and test
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', lightweight=True)
87
        # Change tree2 and merge it into the checkout without committing
88
        self.build_tree(['branch-2/file-2'])
89
        tree2.add('file-2')
90
        tree2.commit('rev2')
91
        checkout.merge_from_branch(tree2.branch)
92
        # Check the error reporting is as expected
93
        err = self.assertRaises(errors.BzrCommandError,
94
            switch.switch, checkout.bzrdir, tree2.branch)
95
        self.assertContainsRe(str(err),
96
            "Pending merges must be committed or reverted before using switch")