1
# Copyright (C) 2005, 2006 Canonical Ltd
1
# Copyright (C) 2006-2010 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
13
13
# You should have received a copy of the GNU General Public License
14
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
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
from bzrlib.tests import SymlinkFeature, TestSkipped
22
from bzrlib.tests.blackbox import ExternalBase
21
from bzrlib.tests import (
24
TestCaseWithTransport,
23
27
from bzrlib.workingtree import WorkingTree
24
28
from bzrlib import osutils
34
class TestRemove(ExternalBase):
38
class TestRemove(TestCaseWithTransport):
36
40
def _make_tree_and_add(self, paths):
37
41
tree = self.make_branch_and_tree('.')
51
55
self.assertNotInWorkingTree(f)
56
self.assertPathDoesNotExist(f)
54
58
def assertFilesUnversioned(self, files):
56
60
self.assertNotInWorkingTree(f)
57
self.failUnlessExists(f)
61
self.assertPathExists(f)
59
63
def changeFile(self, file_name):
60
64
f = file(file_name, 'ab')
61
65
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))
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)
74
72
def test_remove_new_no_files_specified(self):
75
73
tree = self.make_branch_and_tree('.')
82
80
self.assertEqual('', err)
83
81
self.assertEqual('', out)
84
82
self.assertInWorkingTree('foo', tree=tree)
85
self.failUnlessExists('foo')
83
self.assertPathExists('foo')
87
85
def test_remove_no_files_specified_missing_dir_and_contents(self):
88
86
tree = self._make_tree_and_add(
97
95
# non-missing paths not touched:
98
96
self.assertInWorkingTree('foo', tree=tree)
99
self.failUnlessExists('foo')
97
self.assertPathExists('foo')
100
98
self.assertInWorkingTree('dir', tree=tree)
101
self.failUnlessExists('dir')
99
self.assertPathExists('dir')
102
100
# missing files unversioned
103
101
self.assertNotInWorkingTree('dir/missing', tree=tree)
104
102
self.assertNotInWorkingTree('dir/missing/child', tree=tree)
122
120
self.assertEqual('removed bar\n', err)
123
121
# non-missing files not touched:
124
122
self.assertInWorkingTree('foo', tree=tree)
125
self.failUnlessExists('foo')
123
self.assertPathExists('foo')
126
124
# missing files unversioned
127
125
self.assertNotInWorkingTree('bar', tree=tree)
129
127
def test_remove_no_files_specified_missing_link(self):
130
self.requireFeature(SymlinkFeature)
128
self.requireFeature(features.SymlinkFeature)
131
129
tree = self._make_tree_and_add(['foo'])
132
130
os.symlink('foo', 'linkname')
133
131
tree.add(['linkname'])
137
135
self.assertEqual('removed linkname\n', err)
138
136
# non-missing files not touched:
139
137
self.assertInWorkingTree('foo', tree=tree)
140
self.failUnlessExists('foo')
138
self.assertPathExists('foo')
141
139
# missing files unversioned
142
140
self.assertNotInWorkingTree('linkname', tree=tree)
174
172
def test_remove_unversioned_files(self):
175
173
self.build_tree(files)
176
174
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)
175
self.run_bzr_remove_changed_files(files)
180
177
def test_remove_changed_files(self):
181
178
tree = self._make_tree_and_add(files)
182
179
self.run_bzr("commit -m 'added files'")
183
180
self.changeFile(a)
184
181
self.changeFile(c)
185
self.run_bzr_remove_changed_files(['modified:[.\s]*a[.\s]*b/c'], files)
182
self.run_bzr_remove_changed_files(files)
187
184
def test_remove_changed_ignored_files(self):
188
185
tree = self._make_tree_and_add(['a'])
189
186
self.run_bzr(['ignore', 'a'])
190
self.run_bzr_remove_changed_files(['added:[.\s]*a'], ['a'])
187
self.run_bzr_remove_changed_files(['a'])
192
189
def test_remove_changed_files_from_child_dir(self):
193
190
if sys.platform == 'win32':
196
193
self.run_bzr("commit -m 'added files'")
197
194
self.changeFile(a)
198
195
self.changeFile(c)
200
self.run_bzr_remove_changed_files(['modified:[.\s]*a[.\s]*b/c'],
201
['../a', 'c', '.', '../d'])
196
self.run_bzr_remove_changed_files(
197
['../a', 'c', '.', '../d'], working_dir='b')
203
198
self.assertNotInWorkingTree(files)
204
self.failIfExists(files)
199
self.assertPathDoesNotExist(files)
206
201
def test_remove_keep_unversioned_files(self):
207
202
self.build_tree(files)
209
204
self.run_bzr('remove --keep a', error_regexes=["a is not versioned."])
210
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)
212
219
def test_remove_force_unversioned_files(self):
213
220
self.build_tree(files)
214
221
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"])
222
script.ScriptRunner().run_script(self, '''
223
$ bzr remove --force a b/ b/c d/
224
2>(The --force option is deprecated, rather use --no-backup in future.)
226
2>removed b/c (but kept a copy: b/c.~1~)
218
230
self.assertFilesDeleted(files)
220
232
def test_remove_deleted_files(self):
225
237
for f in my_files:
226
238
osutils.delete_any(f)
227
239
self.assertInWorkingTree(files)
228
self.failIfExists(files)
240
self.assertPathDoesNotExist(files)
229
241
self.run_bzr('remove ' + ' '.join(files))
230
242
self.assertNotInWorkingTree(a)
231
self.failIfExists(files)
243
self.assertPathDoesNotExist(files)
233
245
def test_remove_non_existing_files(self):
234
246
tree = self._make_tree_and_add([])