21
from bzrlib.tests import SymlinkFeature, TestSkipped
20
22
from bzrlib.tests.blackbox import ExternalBase
21
23
from bzrlib.workingtree import WorkingTree
24
from bzrlib import osutils
24
34
class TestRemove(ExternalBase):
26
def test_remove_deleted(self):
28
self.build_tree(['a'])
29
self.runbzr(['add', 'a'])
30
self.runbzr(['commit', '-m', 'added a'])
32
self.runbzr(['remove', 'a'])
34
def test_remove_new(self):
35
self.build_tree(['filefile',
38
wt = self.make_branch_and_tree('.')
39
wt.add(['filefile', 'dir', 'dir/filefilefile'],
40
['filefile-id', 'dir-id', 'filefilefile-id'])
41
self.assertEqual(wt.path2id('filefile'), 'filefile-id')
42
self.assertEqual(wt.path2id('dir/filefilefile'), 'filefilefile-id')
43
self.assertEqual(wt.path2id('dir'), 'dir-id')
44
self.runbzr('remove --new')
45
wt = WorkingTree.open('.')
46
self.assertIs(wt.path2id('filefile'), None)
47
self.assertIs(wt.path2id('dir/filefilefile'), None)
48
self.assertIs(wt.path2id('dir'), None)
49
wt.add(['filefile', 'dir', 'dir/filefilefile'],
50
['filefile-id', 'dir-id', 'filefilefile-id'])
51
self.assertEqual(wt.path2id('filefile'), 'filefile-id')
52
self.assertEqual(wt.path2id('dir/filefilefile'), 'filefilefile-id')
53
self.assertEqual(wt.path2id('dir'), 'dir-id')
54
self.runbzr('remove --new dir')
55
wt = WorkingTree.open('.')
56
self.assertEqual(wt.path2id('filefile'), 'filefile-id')
57
self.assertIs(wt.path2id('dir/filefilefile'), None)
58
self.assertIs(wt.path2id('dir'), None)
59
self.runbzr('remove --new .')
60
wt = WorkingTree.open('.')
61
self.assertIs(wt.path2id('filefile'), None)
62
self.runbzr('remove --new .', retcode=3)
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)