1
# Copyright (C) 2005, 2006 Canonical Ltd
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.
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.
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
20
from bzrlib.tests.blackbox import ExternalBase
21
from bzrlib.workingtree import WorkingTree
22
from bzrlib import osutils
32
class TestRemove(ExternalBase):
34
def run_bzr_captured(self, argv, retcode=0, encoding=None, stdin=None,
37
# magically convert commands like 'remove abc' to ['remove', 'abc']
38
if (isinstance(argv, tuple) and len(argv) == 1 and
39
isinstance(argv[0], basestring)):
40
argv = shlex.split(argv[0])
41
return ExternalBase.run_bzr_captured(self, argv, retcode, encoding,
44
def _make_add_and_assert_tree(self, files):
45
tree = self.make_branch_and_tree('.')
46
self.build_tree(files)
48
id=str(f).replace('/', '_') + _id
50
self.assertEqual(tree.path2id(f), id)
51
self.failUnlessExists(f)
52
self.assertInWorkingTree(f)
55
def assertFilesDeleted(self, files):
58
self.assertNotInWorkingTree(f)
61
def assertFilesUnversioned(self, files):
63
self.assertNotInWorkingTree(f)
64
self.failUnlessExists(f)
66
def changeFile(self, file_name):
67
f = file(file_name, 'ab')
68
f.write("\nsome other new content!")
71
def run_bzr_remove_changed_files(self, error_regexes, files_to_remove):
72
error_regexes.extend(["Can't remove changed or unknown files:",
73
'Use --keep to not delete them,'
74
' or --force to delete them regardless.'
76
self.run_bzr_error(error_regexes,
77
'remove ' + ' '.join(files_to_remove))
78
#see if we can force it now
79
self.run_bzr('remove --force ' + ' '.join(files_to_remove))
81
def test_remove_no_files_specified(self):
82
tree = self._make_add_and_assert_tree([])
83
self.run_bzr_error(["bzr: ERROR: Specify one or more files to remove, "
84
"or use --new."], 'remove')
86
self.run_bzr_error(["bzr: ERROR: No matching files."], 'remove --new')
88
self.run_bzr_error(["bzr: ERROR: No matching files."],
91
def test_rm_one_file(self):
92
tree = self._make_add_and_assert_tree([a])
93
self.run_bzr("commit -m 'added a'")
94
self.run_bzr('rm a', error_regexes=["deleted a"])
95
self.assertFilesDeleted([a])
97
def test_remove_one_file(self):
98
tree = self._make_add_and_assert_tree([a])
99
self.run_bzr("commit -m 'added a'")
100
self.run_bzr('remove a', error_regexes=["deleted a"])
101
self.assertFilesDeleted([a])
103
def test_remove_keep_one_file(self):
104
tree = self._make_add_and_assert_tree([a])
105
self.run_bzr('remove --keep a', error_regexes=["removed a"])
106
self.assertFilesUnversioned([a])
108
def test_remove_one_deleted_file(self):
109
tree = self._make_add_and_assert_tree([a])
110
self.run_bzr("commit -m 'added a'")
112
self.assertInWorkingTree(a)
113
self.run_bzr('remove a')
114
self.assertNotInWorkingTree(a)
116
def test_remove_invalid_files(self):
117
self.build_tree(files)
118
tree = self.make_branch_and_tree('.')
119
self.run_bzr_remove_changed_files(['unknown:[.\s]*xyz[.\s]*abc/def'],
120
['.', 'xyz', 'abc/def'])
122
def test_remove_unversioned_files(self):
123
self.build_tree(files)
124
tree = self.make_branch_and_tree('.')
125
self.run_bzr_remove_changed_files(
126
['unknown:[.\s]*d/[.\s]*b/c[.\s]*b/[.\s]*a'], files)
128
def test_remove_changed_files(self):
129
tree = self._make_add_and_assert_tree(files)
130
self.run_bzr("commit -m 'added files'")
133
self.run_bzr_remove_changed_files(['modified:[.\s]*a[.\s]*b/c'], files)
135
def test_remove_changed_files_from_child_dir(self):
136
tree = self._make_add_and_assert_tree(files)
137
self.run_bzr("commit -m 'added files'")
141
self.run_bzr_remove_changed_files(['modified:[.\s]*a[.\s]*b/c'],
142
['../a', 'c', '.', '../d'])
144
self.assertNotInWorkingTree(files)
145
self.failIfExists(files)
147
def test_remove_keep_unversioned_files(self):
148
self.build_tree(files)
149
tree = self.make_branch_and_tree('.')
150
self.run_bzr('remove --keep a', error_regexes=["a is not versioned."])
151
self.assertFilesUnversioned(files)
153
def test_remove_force_unversioned_files(self):
154
self.build_tree(files)
155
tree = self.make_branch_and_tree('.')
156
self.run_bzr('remove --force ' + ' '.join(files),
157
error_regexes=["deleted a", "deleted b",
158
"deleted b/c", "deleted d"])
159
self.assertFilesDeleted(files)
161
def test_remove_deleted_files(self):
162
tree = self._make_add_and_assert_tree(files)
163
self.run_bzr("commit -m 'added files'")
164
my_files=[f for f in files]
165
my_files.sort(reverse=True)
167
osutils.delete_any(f)
168
self.assertInWorkingTree(files)
169
self.failIfExists(files)
170
self.run_bzr('remove ' + ' '.join(files))
171
self.assertNotInWorkingTree(a)
172
self.failIfExists(files)
174
def test_remove_non_existing_files(self):
175
tree = self._make_add_and_assert_tree([])
176
self.run_bzr_remove_changed_files(['unknown:[.\s]*b'], ['b'])
178
def test_remove_keep_non_existing_files(self):
179
tree = self._make_add_and_assert_tree([])
180
self.run_bzr('remove --keep b', error_regexes=["b is not versioned."])
182
def test_remove_files(self):
183
tree = self._make_add_and_assert_tree(files)
184
self.run_bzr("commit -m 'added files'")
185
self.run_bzr('remove a b b/c d',
186
error_regexes=["deleted a", "deleted b", "deleted b/c",
188
self.assertFilesDeleted(files)
190
def test_remove_keep_files(self):
191
tree = self._make_add_and_assert_tree(files)
192
self.run_bzr("commit -m 'added files'")
193
self.run_bzr('remove --keep a b b/c d',
194
error_regexes=["removed a", "removed b", "removed b/c",
196
self.assertFilesUnversioned(files)
198
def test_remove_with_new(self):
199
tree = self._make_add_and_assert_tree(files)
200
self.run_bzr('remove --new --keep',
201
error_regexes=["removed a", "removed b", "removed b/c"])
202
self.assertFilesUnversioned(files)
204
def test_remove_with_new_in_dir1(self):
205
tree = self._make_add_and_assert_tree(files)
206
self.run_bzr('remove --new --keep b b/c',
207
error_regexes=["removed b", "removed b/c"])
208
tree = WorkingTree.open('.')
209
self.assertInWorkingTree(a)
210
self.assertEqual(tree.path2id(a), a + _id)
211
self.assertFilesUnversioned([b,c])
213
def test_remove_with_new_in_dir2(self):
214
tree = self._make_add_and_assert_tree(files)
215
self.run_bzr('remove --new --keep .',
216
error_regexes=["removed a", "removed b", "removed b/c"])
217
tree = WorkingTree.open('.')
218
self.assertFilesUnversioned(files)