1
# Copyright (C) 2006-2010 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
from bzrlib.tests import SymlinkFeature, TestSkipped
22
from bzrlib.tests import TestCaseWithTransport
23
from bzrlib.workingtree import WorkingTree
24
from bzrlib import osutils
34
class TestRemove(TestCaseWithTransport):
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, files_to_remove, working_dir=None):
65
self.run_bzr(['remove'] + list(files_to_remove),
66
working_dir=working_dir)
68
def test_remove_new_no_files_specified(self):
69
tree = self.make_branch_and_tree('.')
70
self.run_bzr_error(["bzr: ERROR: No matching files."], 'remove --new')
71
self.run_bzr_error(["bzr: ERROR: No matching files."], 'remove --new .')
73
def test_remove_no_files_specified(self):
74
tree = self._make_tree_and_add(['foo'])
75
out, err = self.run_bzr(['rm'])
76
self.assertEqual('', err)
77
self.assertEqual('', out)
78
self.assertInWorkingTree('foo', tree=tree)
79
self.failUnlessExists('foo')
81
def test_remove_no_files_specified_missing_dir_and_contents(self):
82
tree = self._make_tree_and_add(
83
['foo', 'dir/', 'dir/missing/', 'dir/missing/child'])
84
self.get_transport('.').delete_tree('dir/missing')
85
out, err = self.run_bzr(['rm'])
86
self.assertEqual('', out)
88
'removed dir/missing/child\n'
89
'removed dir/missing\n',
91
# non-missing paths not touched:
92
self.assertInWorkingTree('foo', tree=tree)
93
self.failUnlessExists('foo')
94
self.assertInWorkingTree('dir', tree=tree)
95
self.failUnlessExists('dir')
96
# missing files unversioned
97
self.assertNotInWorkingTree('dir/missing', tree=tree)
98
self.assertNotInWorkingTree('dir/missing/child', tree=tree)
100
def test_remove_no_files_specified_already_deleted(self):
101
tree = self._make_tree_and_add(['foo', 'bar'])
102
tree.commit('save foo and bar')
105
self.assertEqual(None, tree.path2id('bar'))
106
# Running rm with a deleted file does not error.
107
out, err = self.run_bzr(['rm'])
108
self.assertEqual('', out)
109
self.assertEqual('', err)
111
def test_remove_no_files_specified_missing_file(self):
112
tree = self._make_tree_and_add(['foo', 'bar'])
114
out, err = self.run_bzr(['rm'])
115
self.assertEqual('', out)
116
self.assertEqual('removed bar\n', err)
117
# non-missing files not touched:
118
self.assertInWorkingTree('foo', tree=tree)
119
self.failUnlessExists('foo')
120
# missing files unversioned
121
self.assertNotInWorkingTree('bar', tree=tree)
123
def test_remove_no_files_specified_missing_link(self):
124
self.requireFeature(SymlinkFeature)
125
tree = self._make_tree_and_add(['foo'])
126
os.symlink('foo', 'linkname')
127
tree.add(['linkname'])
128
os.unlink('linkname')
129
out, err = self.run_bzr(['rm'])
130
self.assertEqual('', out)
131
self.assertEqual('removed linkname\n', err)
132
# non-missing files not touched:
133
self.assertInWorkingTree('foo', tree=tree)
134
self.failUnlessExists('foo')
135
# missing files unversioned
136
self.assertNotInWorkingTree('linkname', tree=tree)
138
def test_rm_one_file(self):
139
tree = self._make_tree_and_add([a])
140
self.run_bzr("commit -m 'added a'")
141
self.run_bzr('rm a', error_regexes=["deleted a"])
142
self.assertFilesDeleted([a])
144
def test_remove_one_file(self):
145
tree = self._make_tree_and_add([a])
146
self.run_bzr("commit -m 'added a'")
147
self.run_bzr('remove a', error_regexes=["deleted a"])
148
self.assertFilesDeleted([a])
150
def test_remove_keep_one_file(self):
151
tree = self._make_tree_and_add([a])
152
self.run_bzr('remove --keep a', error_regexes=["removed a"])
153
self.assertFilesUnversioned([a])
155
def test_remove_one_deleted_file(self):
156
tree = self._make_tree_and_add([a])
157
self.run_bzr("commit -m 'added a'")
159
self.assertInWorkingTree(a)
160
self.run_bzr('remove a')
161
self.assertNotInWorkingTree(a)
163
def test_remove_invalid_files(self):
164
self.build_tree(files)
165
tree = self.make_branch_and_tree('.')
166
self.run_bzr(['remove', '.', 'xyz', 'abc/def'])
168
def test_remove_unversioned_files(self):
169
self.build_tree(files)
170
tree = self.make_branch_and_tree('.')
171
self.run_bzr_remove_changed_files(files)
173
def test_remove_changed_files(self):
174
tree = self._make_tree_and_add(files)
175
self.run_bzr("commit -m 'added files'")
178
self.run_bzr_remove_changed_files(files)
180
def test_remove_changed_ignored_files(self):
181
tree = self._make_tree_and_add(['a'])
182
self.run_bzr(['ignore', 'a'])
183
self.run_bzr_remove_changed_files(['a'])
185
def test_remove_changed_files_from_child_dir(self):
186
if sys.platform == 'win32':
187
raise TestSkipped("Windows unable to remove '.' directory")
188
tree = self._make_tree_and_add(files)
189
self.run_bzr("commit -m 'added files'")
192
self.run_bzr_remove_changed_files(
193
['../a', 'c', '.', '../d'], working_dir='b')
194
self.assertNotInWorkingTree(files)
195
self.failIfExists(files)
197
def test_remove_keep_unversioned_files(self):
198
self.build_tree(files)
199
tree = self.make_branch_and_tree('.')
200
self.run_bzr('remove --keep a', error_regexes=["a is not versioned."])
201
self.assertFilesUnversioned(files)
203
def test_remove_force_unversioned_files(self):
204
self.build_tree(files)
205
tree = self.make_branch_and_tree('.')
206
self.run_bzr(['remove', '--force'] + list(files),
207
error_regexes=["deleted a", "deleted b",
208
"removed b/c", "deleted d"])
209
self.assertFilesDeleted(files)
211
def test_remove_deleted_files(self):
212
tree = self._make_tree_and_add(files)
213
self.run_bzr("commit -m 'added files'")
214
my_files=[f for f in files]
215
my_files.sort(reverse=True)
217
osutils.delete_any(f)
218
self.assertInWorkingTree(files)
219
self.failIfExists(files)
220
self.run_bzr('remove ' + ' '.join(files))
221
self.assertNotInWorkingTree(a)
222
self.failIfExists(files)
224
def test_remove_non_existing_files(self):
225
tree = self._make_tree_and_add([])
226
self.run_bzr(['remove', 'b'])
228
def test_remove_keep_non_existing_files(self):
229
tree = self._make_tree_and_add([])
230
self.run_bzr('remove --keep b', error_regexes=["b is not versioned."])
232
def test_remove_files(self):
233
tree = self._make_tree_and_add(files)
234
self.run_bzr("commit -m 'added files'")
235
self.run_bzr('remove a b b/c d',
236
error_regexes=["deleted a", "deleted b", "deleted b/c",
238
self.assertFilesDeleted(files)
240
def test_remove_keep_files(self):
241
tree = self._make_tree_and_add(files)
242
self.run_bzr("commit -m 'added files'")
243
self.run_bzr('remove --keep a b b/c d',
244
error_regexes=["removed a", "removed b", "removed b/c",
246
self.assertFilesUnversioned(files)
248
def test_remove_with_new(self):
249
tree = self._make_tree_and_add(files)
250
self.run_bzr('remove --new --keep',
251
error_regexes=["removed a", "removed b", "removed b/c"])
252
self.assertFilesUnversioned(files)
254
def test_remove_with_new_in_dir1(self):
255
tree = self._make_tree_and_add(files)
256
self.run_bzr('remove --new --keep b b/c',
257
error_regexes=["removed b", "removed b/c"])
258
tree = WorkingTree.open('.')
259
self.assertInWorkingTree(a)
260
self.assertEqual(tree.path2id(a), a + _id)
261
self.assertFilesUnversioned([b,c])
263
def test_remove_with_new_in_dir2(self):
264
tree = self._make_tree_and_add(files)
265
self.run_bzr('remove --new --keep .',
266
error_regexes=["removed a", "removed b", "removed b/c"])
267
tree = WorkingTree.open('.')
268
self.assertFilesUnversioned(files)