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 SymlinkFeature, 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_tree_and_add(self, paths):
37
tree = self.make_branch_and_tree('.')
40
self.build_tree(paths)
42
file_id=str(path).replace('/', '_') + _id
43
tree.add(path, file_id)
48
def assertFilesDeleted(self, files):
51
self.assertNotInWorkingTree(f)
54
def assertFilesUnversioned(self, files):
56
self.assertNotInWorkingTree(f)
57
self.failUnlessExists(f)
59
def changeFile(self, file_name):
60
f = file(file_name, 'ab')
61
f.write("\nsome other new content!")
64
def run_bzr_remove_changed_files(self, error_regexes, files_to_remove):
65
error_regexes.extend(["Can't safely remove modified or unknown files:",
66
'Use --keep to not delete them,'
67
' or --force to delete them regardless.'
69
self.run_bzr_error(error_regexes,
70
['remove'] + list(files_to_remove))
71
#see if we can force it now
72
self.run_bzr(['remove', '--force'] + list(files_to_remove))
74
def test_remove_new_no_files_specified(self):
75
tree = self.make_branch_and_tree('.')
76
self.run_bzr_error(["bzr: ERROR: No matching files."], 'remove --new')
77
self.run_bzr_error(["bzr: ERROR: No matching files."], 'remove --new .')
79
def test_remove_no_files_specified(self):
80
tree = self._make_tree_and_add(['foo'])
81
out, err = self.run_bzr(['rm'])
82
self.assertEqual('', err)
83
self.assertEqual('', out)
84
self.assertInWorkingTree('foo', tree=tree)
85
self.failUnlessExists('foo')
87
def test_remove_no_files_specified_missing_dir_and_contents(self):
88
tree = self._make_tree_and_add(
89
['foo', 'dir/', 'dir/missing/', 'dir/missing/child'])
90
self.get_transport('.').delete_tree('dir/missing')
91
out, err = self.run_bzr(['rm'])
92
self.assertEqual('', out)
94
'removed dir/missing/child\n'
95
'removed dir/missing\n',
97
# non-missing paths not touched:
98
self.assertInWorkingTree('foo', tree=tree)
99
self.failUnlessExists('foo')
100
self.assertInWorkingTree('dir', tree=tree)
101
self.failUnlessExists('dir')
102
# missing files unversioned
103
self.assertNotInWorkingTree('dir/missing', tree=tree)
104
self.assertNotInWorkingTree('dir/missing/child', tree=tree)
106
def test_remove_no_files_specified_already_deleted(self):
107
tree = self._make_tree_and_add(['foo', 'bar'])
108
tree.commit('save foo and bar')
111
self.assertEqual(None, tree.path2id('bar'))
112
# Running rm with a deleted file does not error.
113
out, err = self.run_bzr(['rm'])
114
self.assertEqual('', out)
115
self.assertEqual('', err)
117
def test_remove_no_files_specified_missing_file(self):
118
tree = self._make_tree_and_add(['foo', 'bar'])
120
out, err = self.run_bzr(['rm'])
121
self.assertEqual('', out)
122
self.assertEqual('removed bar\n', err)
123
# non-missing files not touched:
124
self.assertInWorkingTree('foo', tree=tree)
125
self.failUnlessExists('foo')
126
# missing files unversioned
127
self.assertNotInWorkingTree('bar', tree=tree)
129
def test_remove_no_files_specified_missing_link(self):
130
self.requireFeature(SymlinkFeature)
131
tree = self._make_tree_and_add(['foo'])
132
os.symlink('foo', 'linkname')
133
tree.add(['linkname'])
134
os.unlink('linkname')
135
out, err = self.run_bzr(['rm'])
136
self.assertEqual('', out)
137
self.assertEqual('removed linkname\n', err)
138
# non-missing files not touched:
139
self.assertInWorkingTree('foo', tree=tree)
140
self.failUnlessExists('foo')
141
# missing files unversioned
142
self.assertNotInWorkingTree('linkname', tree=tree)
144
def test_rm_one_file(self):
145
tree = self._make_tree_and_add([a])
146
self.run_bzr("commit -m 'added a'")
147
self.run_bzr('rm a', error_regexes=["deleted a"])
148
self.assertFilesDeleted([a])
150
def test_remove_one_file(self):
151
tree = self._make_tree_and_add([a])
152
self.run_bzr("commit -m 'added a'")
153
self.run_bzr('remove a', error_regexes=["deleted a"])
154
self.assertFilesDeleted([a])
156
def test_remove_keep_one_file(self):
157
tree = self._make_tree_and_add([a])
158
self.run_bzr('remove --keep a', error_regexes=["removed a"])
159
self.assertFilesUnversioned([a])
161
def test_remove_one_deleted_file(self):
162
tree = self._make_tree_and_add([a])
163
self.run_bzr("commit -m 'added a'")
165
self.assertInWorkingTree(a)
166
self.run_bzr('remove a')
167
self.assertNotInWorkingTree(a)
169
def test_remove_invalid_files(self):
170
self.build_tree(files)
171
tree = self.make_branch_and_tree('.')
172
self.run_bzr(['remove', '.', 'xyz', 'abc/def'])
174
def test_remove_unversioned_files(self):
175
self.build_tree(files)
176
tree = self.make_branch_and_tree('.')
177
self.run_bzr_remove_changed_files(
178
['unknown:[.\s]*d/[.\s]*b/c[.\s]*b/[.\s]*a'], files)
180
def test_remove_changed_files(self):
181
tree = self._make_tree_and_add(files)
182
self.run_bzr("commit -m 'added files'")
185
self.run_bzr_remove_changed_files(['modified:[.\s]*a[.\s]*b/c'], files)
187
def test_remove_changed_ignored_files(self):
188
tree = self._make_tree_and_add(['a'])
189
self.run_bzr(['ignore', 'a'])
190
self.run_bzr_remove_changed_files(['added:[.\s]*a'], ['a'])
192
def test_remove_changed_files_from_child_dir(self):
193
if sys.platform == 'win32':
194
raise TestSkipped("Windows unable to remove '.' directory")
195
tree = self._make_tree_and_add(files)
196
self.run_bzr("commit -m 'added files'")
200
self.run_bzr_remove_changed_files(['modified:[.\s]*a[.\s]*b/c'],
201
['../a', 'c', '.', '../d'])
203
self.assertNotInWorkingTree(files)
204
self.failIfExists(files)
206
def test_remove_keep_unversioned_files(self):
207
self.build_tree(files)
208
tree = self.make_branch_and_tree('.')
209
self.run_bzr('remove --keep a', error_regexes=["a is not versioned."])
210
self.assertFilesUnversioned(files)
212
def test_remove_force_unversioned_files(self):
213
self.build_tree(files)
214
tree = self.make_branch_and_tree('.')
215
self.run_bzr(['remove', '--force'] + list(files),
216
error_regexes=["deleted a", "deleted b",
217
"deleted b/c", "deleted d"])
218
self.assertFilesDeleted(files)
220
def test_remove_deleted_files(self):
221
tree = self._make_tree_and_add(files)
222
self.run_bzr("commit -m 'added files'")
223
my_files=[f for f in files]
224
my_files.sort(reverse=True)
226
osutils.delete_any(f)
227
self.assertInWorkingTree(files)
228
self.failIfExists(files)
229
self.run_bzr('remove ' + ' '.join(files))
230
self.assertNotInWorkingTree(a)
231
self.failIfExists(files)
233
def test_remove_non_existing_files(self):
234
tree = self._make_tree_and_add([])
235
self.run_bzr(['remove', 'b'])
237
def test_remove_keep_non_existing_files(self):
238
tree = self._make_tree_and_add([])
239
self.run_bzr('remove --keep b', error_regexes=["b is not versioned."])
241
def test_remove_files(self):
242
tree = self._make_tree_and_add(files)
243
self.run_bzr("commit -m 'added files'")
244
self.run_bzr('remove a b b/c d',
245
error_regexes=["deleted a", "deleted b", "deleted b/c",
247
self.assertFilesDeleted(files)
249
def test_remove_keep_files(self):
250
tree = self._make_tree_and_add(files)
251
self.run_bzr("commit -m 'added files'")
252
self.run_bzr('remove --keep a b b/c d',
253
error_regexes=["removed a", "removed b", "removed b/c",
255
self.assertFilesUnversioned(files)
257
def test_remove_with_new(self):
258
tree = self._make_tree_and_add(files)
259
self.run_bzr('remove --new --keep',
260
error_regexes=["removed a", "removed b", "removed b/c"])
261
self.assertFilesUnversioned(files)
263
def test_remove_with_new_in_dir1(self):
264
tree = self._make_tree_and_add(files)
265
self.run_bzr('remove --new --keep b b/c',
266
error_regexes=["removed b", "removed b/c"])
267
tree = WorkingTree.open('.')
268
self.assertInWorkingTree(a)
269
self.assertEqual(tree.path2id(a), a + _id)
270
self.assertFilesUnversioned([b,c])
272
def test_remove_with_new_in_dir2(self):
273
tree = self._make_tree_and_add(files)
274
self.run_bzr('remove --new --keep .',
275
error_regexes=["removed a", "removed b", "removed b/c"])
276
tree = WorkingTree.open('.')
277
self.assertFilesUnversioned(files)