~bzr-pqm/bzr/bzr.dev

6423.1.1 by Vincent Ladeuil
Cleanup old blackbox tests and then some. Remove os.chdir() calls, caught a few bugs, make sure we don't leave file handles opened.
1
# Copyright (C) 2006-2012 Canonical Ltd
2127.2.1 by Daniel Silverstone
Add remove-tree and its blackbox tests
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2127.2.1 by Daniel Silverstone
Add remove-tree and its blackbox tests
16
17
2127.2.2 by Daniel Silverstone
Refactor the remove-tree stuff after review from J-A-M
18
"""Black-box tests for bzr remove-tree."""
2127.2.1 by Daniel Silverstone
Add remove-tree and its blackbox tests
19
20
import os
21
5268.3.1 by Matt Giuca
remove-tree now refuses to run without --force if there are shelved changes.
22
from bzrlib import shelf
5283.4.5 by Martin Pool
Update remaining subclasses of ExternalBase
23
from bzrlib.tests import TestCaseWithTransport
24
25
26
class TestRemoveTree(TestCaseWithTransport):
2127.2.1 by Daniel Silverstone
Add remove-tree and its blackbox tests
27
2127.2.2 by Daniel Silverstone
Refactor the remove-tree stuff after review from J-A-M
28
    def setUp(self):
29
        super(TestRemoveTree, self).setUp()
30
        self.tree = self.make_branch_and_tree('branch1')
31
        self.build_tree(['branch1/foo'])
32
        self.tree.add('foo')
33
        self.tree.commit('1')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
34
        self.assertPathExists('branch1/foo')
2127.2.2 by Daniel Silverstone
Refactor the remove-tree stuff after review from J-A-M
35
36
    # Success modes
37
38
    def test_remove_tree_original_branch(self):
6423.1.1 by Vincent Ladeuil
Cleanup old blackbox tests and then some. Remove os.chdir() calls, caught a few bugs, make sure we don't leave file handles opened.
39
        self.run_bzr('remove-tree', working_dir='branch1')
40
        self.assertPathDoesNotExist('branch1/foo')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
41
2127.2.2 by Daniel Silverstone
Refactor the remove-tree stuff after review from J-A-M
42
    def test_remove_tree_original_branch_explicit(self):
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
43
        self.run_bzr('remove-tree branch1')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
44
        self.assertPathDoesNotExist('branch1/foo')
2127.2.2 by Daniel Silverstone
Refactor the remove-tree stuff after review from J-A-M
45
4748.1.9 by Andrew Bennetts
Fix bug introduced by the change to take multiple locations, and add a test for multiple locations.
46
    def test_remove_tree_multiple_branch_explicit(self):
47
        self.tree.bzrdir.sprout('branch2')
48
        self.run_bzr('remove-tree branch1 branch2')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
49
        self.assertPathDoesNotExist('branch1/foo')
50
        self.assertPathDoesNotExist('branch2/foo')
4748.1.9 by Andrew Bennetts
Fix bug introduced by the change to take multiple locations, and add a test for multiple locations.
51
2127.2.2 by Daniel Silverstone
Refactor the remove-tree stuff after review from J-A-M
52
    def test_remove_tree_sprouted_branch(self):
53
        self.tree.bzrdir.sprout('branch2')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
54
        self.assertPathExists('branch2/foo')
6423.1.1 by Vincent Ladeuil
Cleanup old blackbox tests and then some. Remove os.chdir() calls, caught a few bugs, make sure we don't leave file handles opened.
55
        self.run_bzr('remove-tree', working_dir='branch2')
56
        self.assertPathDoesNotExist('branch2/foo')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
57
2127.2.2 by Daniel Silverstone
Refactor the remove-tree stuff after review from J-A-M
58
    def test_remove_tree_sprouted_branch_explicit(self):
59
        self.tree.bzrdir.sprout('branch2')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
60
        self.assertPathExists('branch2/foo')
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
61
        self.run_bzr('remove-tree branch2')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
62
        self.assertPathDoesNotExist('branch2/foo')
2127.2.2 by Daniel Silverstone
Refactor the remove-tree stuff after review from J-A-M
63
64
    def test_remove_tree_checkout(self):
65
        self.tree.branch.create_checkout('branch2', lightweight=False)
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
66
        self.assertPathExists('branch2/foo')
6423.1.1 by Vincent Ladeuil
Cleanup old blackbox tests and then some. Remove os.chdir() calls, caught a few bugs, make sure we don't leave file handles opened.
67
        self.run_bzr('remove-tree', working_dir='branch2')
68
        self.assertPathDoesNotExist('branch2/foo')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
69
        self.assertPathExists('branch1/foo')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
70
2127.2.2 by Daniel Silverstone
Refactor the remove-tree stuff after review from J-A-M
71
    def test_remove_tree_checkout_explicit(self):
72
        self.tree.branch.create_checkout('branch2', lightweight=False)
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
73
        self.assertPathExists('branch2/foo')
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
74
        self.run_bzr('remove-tree branch2')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
75
        self.assertPathDoesNotExist('branch2/foo')
76
        self.assertPathExists('branch1/foo')
2127.2.2 by Daniel Silverstone
Refactor the remove-tree stuff after review from J-A-M
77
78
    # Failure modes
79
80
    def test_remove_tree_lightweight_checkout(self):
81
        self.tree.branch.create_checkout('branch2', lightweight=True)
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
82
        self.assertPathExists('branch2/foo')
2127.2.2 by Daniel Silverstone
Refactor the remove-tree stuff after review from J-A-M
83
        output = self.run_bzr_error(
2127.2.3 by Daniel Silverstone
Oops, fix the message up
84
            ["You cannot remove the working tree from a lightweight checkout"],
6423.1.1 by Vincent Ladeuil
Cleanup old blackbox tests and then some. Remove os.chdir() calls, caught a few bugs, make sure we don't leave file handles opened.
85
            'remove-tree', retcode=3, working_dir='branch2')
86
        self.assertPathExists('branch2/foo')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
87
        self.assertPathExists('branch1/foo')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
88
2127.2.2 by Daniel Silverstone
Refactor the remove-tree stuff after review from J-A-M
89
    def test_remove_tree_lightweight_checkout_explicit(self):
90
        self.tree.branch.create_checkout('branch2', lightweight=True)
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
91
        self.assertPathExists('branch2/foo')
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
92
        output = self.run_bzr_error(
93
            ["You cannot remove the working tree from a lightweight checkout"],
94
            'remove-tree branch2', retcode=3)
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
95
        self.assertPathExists('branch2/foo')
96
        self.assertPathExists('branch1/foo')
2127.2.2 by Daniel Silverstone
Refactor the remove-tree stuff after review from J-A-M
97
98
    def test_remove_tree_empty_dir(self):
99
        os.mkdir('branch2')
6423.1.1 by Vincent Ladeuil
Cleanup old blackbox tests and then some. Remove os.chdir() calls, caught a few bugs, make sure we don't leave file handles opened.
100
        output = self.run_bzr_error(
101
            ["Not a branch"], 'remove-tree', retcode=3, working_dir='branch2')
2127.2.2 by Daniel Silverstone
Refactor the remove-tree stuff after review from J-A-M
102
103
    def test_remove_tree_repeatedly(self):
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
104
        self.run_bzr('remove-tree branch1')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
105
        self.assertPathDoesNotExist('branch1/foo')
2127.2.2 by Daniel Silverstone
Refactor the remove-tree stuff after review from J-A-M
106
        output = self.run_bzr_error(["No working tree to remove"],
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
107
                                    'remove-tree branch1', retcode=3)
2127.2.2 by Daniel Silverstone
Refactor the remove-tree stuff after review from J-A-M
108
109
    def test_remove_tree_remote_path(self):
110
        # TODO: I can't think of a way to implement this...
111
        pass
3667.2.1 by Lukáš Lalinský
Make `bzr remove-tree` not remove trees with uncommitted changes by default
112
113
    def test_remove_tree_uncommitted_changes(self):
114
        self.build_tree(['branch1/bar'])
115
        self.tree.add('bar')
116
        output = self.run_bzr_error(["Working tree .* has uncommitted changes"],
117
                                    'remove-tree branch1', retcode=3)
118
119
    def test_remove_tree_uncommitted_changes_force(self):
120
        self.build_tree(['branch1/bar'])
121
        self.tree.add('bar')
122
        self.run_bzr('remove-tree branch1 --force')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
123
        self.assertPathDoesNotExist('branch1/foo')
124
        self.assertPathExists('branch1/bar')
4672.3.3 by Vincent Ladeuil
Don't allow remove-tree to succeed with pending merges unless
125
126
    def test_remove_tree_pending_merges(self):
127
        self.run_bzr(['branch', 'branch1', 'branch2'])
128
        self.build_tree(['branch1/bar'])
129
        self.tree.add('bar')
130
        self.tree.commit('2')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
131
        self.assertPathExists('branch1/bar')
4672.3.3 by Vincent Ladeuil
Don't allow remove-tree to succeed with pending merges unless
132
        self.run_bzr(['merge', '../branch1'], working_dir='branch2')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
133
        self.assertPathExists('branch2/bar')
4672.3.3 by Vincent Ladeuil
Don't allow remove-tree to succeed with pending merges unless
134
        self.run_bzr(['revert', '.'], working_dir='branch2')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
135
        self.assertPathDoesNotExist('branch2/bar')
4672.3.3 by Vincent Ladeuil
Don't allow remove-tree to succeed with pending merges unless
136
        output = self.run_bzr_error(["Working tree .* has uncommitted changes"],
137
                                    'remove-tree branch2', retcode=3)
138
139
    def test_remove_tree_pending_merges_force(self):
140
        self.run_bzr(['branch', 'branch1', 'branch2'])
141
        self.build_tree(['branch1/bar'])
142
        self.tree.add('bar')
143
        self.tree.commit('2')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
144
        self.assertPathExists('branch1/bar')
4672.3.3 by Vincent Ladeuil
Don't allow remove-tree to succeed with pending merges unless
145
        self.run_bzr(['merge', '../branch1'], working_dir='branch2')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
146
        self.assertPathExists('branch2/bar')
4672.3.3 by Vincent Ladeuil
Don't allow remove-tree to succeed with pending merges unless
147
        self.run_bzr(['revert', '.'], working_dir='branch2')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
148
        self.assertPathDoesNotExist('branch2/bar')
4672.3.3 by Vincent Ladeuil
Don't allow remove-tree to succeed with pending merges unless
149
        self.run_bzr('remove-tree branch2 --force')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
150
        self.assertPathDoesNotExist('branch2/foo')
151
        self.assertPathDoesNotExist('branch2/bar')
5268.3.1 by Matt Giuca
remove-tree now refuses to run without --force if there are shelved changes.
152
153
    def test_remove_tree_shelved_changes(self):
154
        # https://bugs.launchpad.net/bzr/+bug/586639
155
        tree = self.make_branch_and_tree('.')
156
        creator = shelf.ShelfCreator(tree, tree.basis_tree(), [])
157
        self.addCleanup(creator.finalize)
158
        shelf_id = tree.get_shelf_manager().shelve_changes(creator, 'Foo')
159
        output = self.run_bzr_error(["Working tree .* has shelved changes"],
160
                                    'remove-tree', retcode=3)
161
162
    def test_remove_tree_shelved_changes_force(self):
163
        tree = self.make_branch_and_tree('.')
164
        creator = shelf.ShelfCreator(tree, tree.basis_tree(), [])
165
        self.addCleanup(creator.finalize)
166
        shelf_id = tree.get_shelf_manager().shelve_changes(creator, 'Foo')
167
        self.run_bzr('remove-tree --force')
168
        self.run_bzr('checkout')
169
        # Ensure shelf is empty
170
        self.assertIs(None, tree.get_shelf_manager().last_shelf())