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
21
from bzrlib.tests import TestSkipped
22
from bzrlib.tests.blackbox import ExternalBase
23
from bzrlib.workingtree import WorkingTree
24
from bzrlib import osutils
34
class TestRemove(ExternalBase):
36
def _make_add_and_assert_tree(self, files):
37
tree = self.make_branch_and_tree('.')
38
self.build_tree(files)
40
id=str(f).replace('/', '_') + _id
42
self.assertEqual(tree.path2id(f), id)
43
self.failUnlessExists(f)
44
self.assertInWorkingTree(f)
47
def assertFilesDeleted(self, files):
50
self.assertNotInWorkingTree(f)
53
def assertFilesUnversioned(self, files):
55
self.assertNotInWorkingTree(f)
56
self.failUnlessExists(f)
58
def changeFile(self, file_name):
59
f = file(file_name, 'ab')
60
f.write("\nsome other new content!")
63
def run_bzr_remove_changed_files(self, error_regexes, files_to_remove):
64
error_regexes.extend(["Can't safely remove modified or unknown files:",
65
'Use --keep to not delete them,'
66
' or --force to delete them regardless.'
68
self.run_bzr_error(error_regexes,
69
['remove'] + list(files_to_remove))
70
#see if we can force it now
71
self.run_bzr(['remove', '--force'] + list(files_to_remove))
73
def test_remove_no_files_specified(self):
74
tree = self._make_add_and_assert_tree([])
75
self.run_bzr_error(["bzr: ERROR: Specify one or more files to remove, "
76
"or use --new."], 'remove')
78
self.run_bzr_error(["bzr: ERROR: No matching files."], 'remove --new')
80
self.run_bzr_error(["bzr: ERROR: No matching files."],
83
def test_rm_one_file(self):
84
tree = self._make_add_and_assert_tree([a])
85
self.run_bzr("commit -m 'added a'")
86
self.run_bzr('rm a', error_regexes=["deleted a"])
87
self.assertFilesDeleted([a])
89
def test_remove_one_file(self):
90
tree = self._make_add_and_assert_tree([a])
91
self.run_bzr("commit -m 'added a'")
92
self.run_bzr('remove a', error_regexes=["deleted a"])
93
self.assertFilesDeleted([a])
95
def test_remove_keep_one_file(self):
96
tree = self._make_add_and_assert_tree([a])
97
self.run_bzr('remove --keep a', error_regexes=["removed a"])
98
self.assertFilesUnversioned([a])
100
def test_remove_one_deleted_file(self):
101
tree = self._make_add_and_assert_tree([a])
102
self.run_bzr("commit -m 'added a'")
104
self.assertInWorkingTree(a)
105
self.run_bzr('remove a')
106
self.assertNotInWorkingTree(a)
108
def test_remove_invalid_files(self):
109
self.build_tree(files)
110
tree = self.make_branch_and_tree('.')
111
self.run_bzr(['remove', '.', 'xyz', 'abc/def'])
113
def test_remove_unversioned_files(self):
114
self.build_tree(files)
115
tree = self.make_branch_and_tree('.')
116
self.run_bzr_remove_changed_files(
117
['unknown:[.\s]*d/[.\s]*b/c[.\s]*b/[.\s]*a'], files)
119
def test_remove_changed_files(self):
120
tree = self._make_add_and_assert_tree(files)
121
self.run_bzr("commit -m 'added files'")
124
self.run_bzr_remove_changed_files(['modified:[.\s]*a[.\s]*b/c'], files)
126
def test_remove_changed_ignored_files(self):
127
tree = self._make_add_and_assert_tree(['a'])
128
self.run_bzr(['ignore', 'a'])
129
self.run_bzr_remove_changed_files(['added:[.\s]*a'], ['a'])
131
def test_remove_changed_files_from_child_dir(self):
132
if sys.platform == 'win32':
133
raise TestSkipped("Windows unable to remove '.' directory")
134
tree = self._make_add_and_assert_tree(files)
135
self.run_bzr("commit -m 'added files'")
139
self.run_bzr_remove_changed_files(['modified:[.\s]*a[.\s]*b/c'],
140
['../a', 'c', '.', '../d'])
142
self.assertNotInWorkingTree(files)
143
self.failIfExists(files)
145
def test_remove_keep_unversioned_files(self):
146
self.build_tree(files)
147
tree = self.make_branch_and_tree('.')
148
self.run_bzr('remove --keep a', error_regexes=["a is not versioned."])
149
self.assertFilesUnversioned(files)
151
def test_remove_force_unversioned_files(self):
152
self.build_tree(files)
153
tree = self.make_branch_and_tree('.')
154
self.run_bzr(['remove', '--force'] + list(files),
155
error_regexes=["deleted a", "deleted b",
156
"deleted b/c", "deleted d"])
157
self.assertFilesDeleted(files)
159
def test_remove_deleted_files(self):
160
tree = self._make_add_and_assert_tree(files)
161
self.run_bzr("commit -m 'added files'")
162
my_files=[f for f in files]
163
my_files.sort(reverse=True)
165
osutils.delete_any(f)
166
self.assertInWorkingTree(files)
167
self.failIfExists(files)
168
self.run_bzr('remove ' + ' '.join(files))
169
self.assertNotInWorkingTree(a)
170
self.failIfExists(files)
172
def test_remove_non_existing_files(self):
173
tree = self._make_add_and_assert_tree([])
174
self.run_bzr(['remove', 'b'])
176
def test_remove_keep_non_existing_files(self):
177
tree = self._make_add_and_assert_tree([])
178
self.run_bzr('remove --keep b', error_regexes=["b is not versioned."])
180
def test_remove_files(self):
181
tree = self._make_add_and_assert_tree(files)
182
self.run_bzr("commit -m 'added files'")
183
self.run_bzr('remove a b b/c d',
184
error_regexes=["deleted a", "deleted b", "deleted b/c",
186
self.assertFilesDeleted(files)
188
def test_remove_keep_files(self):
189
tree = self._make_add_and_assert_tree(files)
190
self.run_bzr("commit -m 'added files'")
191
self.run_bzr('remove --keep a b b/c d',
192
error_regexes=["removed a", "removed b", "removed b/c",
194
self.assertFilesUnversioned(files)
196
def test_remove_with_new(self):
197
tree = self._make_add_and_assert_tree(files)
198
self.run_bzr('remove --new --keep',
199
error_regexes=["removed a", "removed b", "removed b/c"])
200
self.assertFilesUnversioned(files)
202
def test_remove_with_new_in_dir1(self):
203
tree = self._make_add_and_assert_tree(files)
204
self.run_bzr('remove --new --keep b b/c',
205
error_regexes=["removed b", "removed b/c"])
206
tree = WorkingTree.open('.')
207
self.assertInWorkingTree(a)
208
self.assertEqual(tree.path2id(a), a + _id)
209
self.assertFilesUnversioned([b,c])
211
def test_remove_with_new_in_dir2(self):
212
tree = self._make_add_and_assert_tree(files)
213
self.run_bzr('remove --new --keep .',
214
error_regexes=["removed a", "removed b", "removed b/c"])
215
tree = WorkingTree.open('.')
216
self.assertFilesUnversioned(files)