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 (
24
TestCaseWithTransport,
27
from bzrlib.workingtree import WorkingTree
28
from bzrlib import osutils
38
class TestRemove(TestCaseWithTransport):
40
def _make_tree_and_add(self, paths):
41
tree = self.make_branch_and_tree('.')
44
self.build_tree(paths)
46
file_id=str(path).replace('/', '_') + _id
47
tree.add(path, file_id)
52
def assertFilesDeleted(self, files):
55
self.assertNotInWorkingTree(f)
56
self.assertPathDoesNotExist(f)
58
def assertFilesUnversioned(self, files):
60
self.assertNotInWorkingTree(f)
61
self.assertPathExists(f)
63
def changeFile(self, file_name):
64
f = file(file_name, 'ab')
65
f.write("\nsome other new content!")
68
def run_bzr_remove_changed_files(self, files_to_remove, working_dir=None):
69
self.run_bzr(['remove'] + list(files_to_remove),
70
working_dir=working_dir)
72
def test_remove_new_no_files_specified(self):
73
tree = self.make_branch_and_tree('.')
74
self.run_bzr_error(["bzr: ERROR: No matching files."], 'remove --new')
75
self.run_bzr_error(["bzr: ERROR: No matching files."], 'remove --new .')
77
def test_remove_no_files_specified(self):
78
tree = self._make_tree_and_add(['foo'])
79
out, err = self.run_bzr(['rm'])
80
self.assertEqual('', err)
81
self.assertEqual('', out)
82
self.assertInWorkingTree('foo', tree=tree)
83
self.assertPathExists('foo')
85
def test_remove_no_files_specified_missing_dir_and_contents(self):
86
tree = self._make_tree_and_add(
87
['foo', 'dir/', 'dir/missing/', 'dir/missing/child'])
88
self.get_transport('.').delete_tree('dir/missing')
89
out, err = self.run_bzr(['rm'])
90
self.assertEqual('', out)
92
'removed dir/missing/child\n'
93
'removed dir/missing\n',
95
# non-missing paths not touched:
96
self.assertInWorkingTree('foo', tree=tree)
97
self.assertPathExists('foo')
98
self.assertInWorkingTree('dir', tree=tree)
99
self.assertPathExists('dir')
100
# missing files unversioned
101
self.assertNotInWorkingTree('dir/missing', tree=tree)
102
self.assertNotInWorkingTree('dir/missing/child', tree=tree)
104
def test_remove_no_files_specified_already_deleted(self):
105
tree = self._make_tree_and_add(['foo', 'bar'])
106
tree.commit('save foo and bar')
109
self.assertEqual(None, tree.path2id('bar'))
110
# Running rm with a deleted file does not error.
111
out, err = self.run_bzr(['rm'])
112
self.assertEqual('', out)
113
self.assertEqual('', err)
115
def test_remove_no_files_specified_missing_file(self):
116
tree = self._make_tree_and_add(['foo', 'bar'])
118
out, err = self.run_bzr(['rm'])
119
self.assertEqual('', out)
120
self.assertEqual('removed bar\n', err)
121
# non-missing files not touched:
122
self.assertInWorkingTree('foo', tree=tree)
123
self.assertPathExists('foo')
124
# missing files unversioned
125
self.assertNotInWorkingTree('bar', tree=tree)
127
def test_remove_no_files_specified_missing_link(self):
128
self.requireFeature(features.SymlinkFeature)
129
tree = self._make_tree_and_add(['foo'])
130
os.symlink('foo', 'linkname')
131
tree.add(['linkname'])
132
os.unlink('linkname')
133
out, err = self.run_bzr(['rm'])
134
self.assertEqual('', out)
135
self.assertEqual('removed linkname\n', err)
136
# non-missing files not touched:
137
self.assertInWorkingTree('foo', tree=tree)
138
self.assertPathExists('foo')
139
# missing files unversioned
140
self.assertNotInWorkingTree('linkname', tree=tree)
142
def test_rm_one_file(self):
143
tree = self._make_tree_and_add([a])
144
self.run_bzr("commit -m 'added a'")
145
self.run_bzr('rm a', error_regexes=["deleted a"])
146
self.assertFilesDeleted([a])
148
def test_remove_one_file(self):
149
tree = self._make_tree_and_add([a])
150
self.run_bzr("commit -m 'added a'")
151
self.run_bzr('remove a', error_regexes=["deleted a"])
152
self.assertFilesDeleted([a])
154
def test_remove_keep_one_file(self):
155
tree = self._make_tree_and_add([a])
156
self.run_bzr('remove --keep a', error_regexes=["removed a"])
157
self.assertFilesUnversioned([a])
159
def test_remove_one_deleted_file(self):
160
tree = self._make_tree_and_add([a])
161
self.run_bzr("commit -m 'added a'")
163
self.assertInWorkingTree(a)
164
self.run_bzr('remove a')
165
self.assertNotInWorkingTree(a)
167
def test_remove_invalid_files(self):
168
self.build_tree(files)
169
tree = self.make_branch_and_tree('.')
170
self.run_bzr(['remove', '.', 'xyz', 'abc/def'])
172
def test_remove_unversioned_files(self):
173
self.build_tree(files)
174
tree = self.make_branch_and_tree('.')
175
self.run_bzr_remove_changed_files(files)
177
def test_remove_changed_files(self):
178
tree = self._make_tree_and_add(files)
179
self.run_bzr("commit -m 'added files'")
182
self.run_bzr_remove_changed_files(files)
184
def test_remove_changed_ignored_files(self):
185
tree = self._make_tree_and_add(['a'])
186
self.run_bzr(['ignore', 'a'])
187
self.run_bzr_remove_changed_files(['a'])
189
def test_remove_changed_files_from_child_dir(self):
190
if sys.platform == 'win32':
191
raise TestSkipped("Windows unable to remove '.' directory")
192
tree = self._make_tree_and_add(files)
193
self.run_bzr("commit -m 'added files'")
196
self.run_bzr_remove_changed_files(
197
['../a', 'c', '.', '../d'], working_dir='b')
198
self.assertNotInWorkingTree(files)
199
self.assertPathDoesNotExist(files)
201
def test_remove_keep_unversioned_files(self):
202
self.build_tree(files)
203
tree = self.make_branch_and_tree('.')
204
self.run_bzr('remove --keep a', error_regexes=["a is not versioned."])
205
self.assertFilesUnversioned(files)
207
def test_remove_no_backup_unversioned_files(self):
208
self.build_tree(files)
209
tree = self.make_branch_and_tree('.')
210
script.ScriptRunner().run_script(self, '''
211
$ bzr remove --no-backup a b/ b/c d/
213
2>removed b/c (but kept a copy: b/c.~1~)
217
self.assertFilesDeleted(files)
219
def test_remove_deleted_files(self):
220
tree = self._make_tree_and_add(files)
221
self.run_bzr("commit -m 'added files'")
222
my_files=[f for f in files]
223
my_files.sort(reverse=True)
225
osutils.delete_any(f)
226
self.assertInWorkingTree(files)
227
self.assertPathDoesNotExist(files)
228
self.run_bzr('remove ' + ' '.join(files))
229
self.assertNotInWorkingTree(a)
230
self.assertPathDoesNotExist(files)
232
def test_remove_non_existing_files(self):
233
tree = self._make_tree_and_add([])
234
self.run_bzr(['remove', 'b'])
236
def test_remove_keep_non_existing_files(self):
237
tree = self._make_tree_and_add([])
238
self.run_bzr('remove --keep b', error_regexes=["b is not versioned."])
240
def test_remove_files(self):
241
tree = self._make_tree_and_add(files)
242
self.run_bzr("commit -m 'added files'")
243
self.run_bzr('remove a b b/c d',
244
error_regexes=["deleted a", "deleted b", "deleted b/c",
246
self.assertFilesDeleted(files)
248
def test_remove_keep_files(self):
249
tree = self._make_tree_and_add(files)
250
self.run_bzr("commit -m 'added files'")
251
self.run_bzr('remove --keep a b b/c d',
252
error_regexes=["removed a", "removed b", "removed b/c",
254
self.assertFilesUnversioned(files)
256
def test_remove_with_new(self):
257
tree = self._make_tree_and_add(files)
258
self.run_bzr('remove --new --keep',
259
error_regexes=["removed a", "removed b", "removed b/c"])
260
self.assertFilesUnversioned(files)
262
def test_remove_with_new_in_dir1(self):
263
tree = self._make_tree_and_add(files)
264
self.run_bzr('remove --new --keep b b/c',
265
error_regexes=["removed b", "removed b/c"])
266
tree = WorkingTree.open('.')
267
self.assertInWorkingTree(a)
268
self.assertEqual(tree.path2id(a), a + _id)
269
self.assertFilesUnversioned([b,c])
271
def test_remove_with_new_in_dir2(self):
272
tree = self._make_tree_and_add(files)
273
self.run_bzr('remove --new --keep .',
274
error_regexes=["removed a", "removed b", "removed b/c"])
275
tree = WorkingTree.open('.')
276
self.assertFilesUnversioned(files)