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 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_add_and_assert_tree(self, files):
37
tree = self.make_branch_and_tree('.')
38
self.build_tree(files)
40
id=str(f).replace('/', '_') + _id
42
self.assertEqual(tree.path2id(f), id)
43
self.failUnlessExists(f)
44
self.assertInWorkingTree(f)
47
def assertFilesDeleted(self, files):
50
self.assertNotInWorkingTree(f)
53
def assertFilesUnversioned(self, files):
55
self.assertNotInWorkingTree(f)
56
self.failUnlessExists(f)
58
def changeFile(self, file_name):
59
f = file(file_name, 'ab')
60
f.write("\nsome other new content!")
63
def run_bzr_remove_changed_files(self, error_regexes, files_to_remove):
64
error_regexes.extend(["Can't safely remove modified or unknown files:",
65
'Use --keep to not delete them,'
66
' or --force to delete them regardless.'
68
self.run_bzr_error(error_regexes,
69
['remove'] + list(files_to_remove))
70
#see if we can force it now
71
self.run_bzr(['remove', '--force'] + list(files_to_remove))
73
def test_remove_no_files_specified(self):
74
tree = self._make_add_and_assert_tree([])
75
self.run_bzr_error(["bzr: ERROR: Specify one or more files to remove, "
76
"or use --new."], 'remove')
78
self.run_bzr_error(["bzr: ERROR: No matching files."], 'remove --new')
80
self.run_bzr_error(["bzr: ERROR: No matching files."],
83
def test_rm_one_file(self):
84
tree = self._make_add_and_assert_tree([a])
85
self.run_bzr("commit -m 'added a'")
86
self.run_bzr('rm a', error_regexes=["deleted a"])
87
self.assertFilesDeleted([a])
89
def test_remove_one_file(self):
90
tree = self._make_add_and_assert_tree([a])
91
self.run_bzr("commit -m 'added a'")
92
self.run_bzr('remove a', error_regexes=["deleted a"])
93
self.assertFilesDeleted([a])
95
def test_remove_keep_one_file(self):
96
tree = self._make_add_and_assert_tree([a])
97
self.run_bzr('remove --keep a', error_regexes=["removed a"])
98
self.assertFilesUnversioned([a])
100
def test_remove_one_deleted_file(self):
101
tree = self._make_add_and_assert_tree([a])
102
self.run_bzr("commit -m 'added a'")
104
self.assertInWorkingTree(a)
105
self.run_bzr('remove a')
106
self.assertNotInWorkingTree(a)
108
def test_remove_invalid_files(self):
109
self.build_tree(files)
110
tree = self.make_branch_and_tree('.')
111
self.run_bzr(['remove', '.', 'xyz', 'abc/def'])
113
def test_remove_unversioned_files(self):
114
self.build_tree(files)
115
tree = self.make_branch_and_tree('.')
116
self.run_bzr_remove_changed_files(
117
['unknown:[.\s]*d/[.\s]*b/c[.\s]*b/[.\s]*a'], files)
119
def test_remove_changed_files(self):
120
tree = self._make_add_and_assert_tree(files)
121
self.run_bzr("commit -m 'added files'")
124
self.run_bzr_remove_changed_files(['modified:[.\s]*a[.\s]*b/c'], files)
126
def test_remove_changed_files_from_child_dir(self):
127
if sys.platform == 'win32':
128
raise TestSkipped("Windows unable to remove '.' directory")
129
tree = self._make_add_and_assert_tree(files)
130
self.run_bzr("commit -m 'added files'")
134
self.run_bzr_remove_changed_files(['modified:[.\s]*a[.\s]*b/c'],
135
['../a', 'c', '.', '../d'])
137
self.assertNotInWorkingTree(files)
138
self.failIfExists(files)
140
def test_remove_keep_unversioned_files(self):
141
self.build_tree(files)
142
tree = self.make_branch_and_tree('.')
143
self.run_bzr('remove --keep a', error_regexes=["a is not versioned."])
144
self.assertFilesUnversioned(files)
146
def test_remove_force_unversioned_files(self):
147
self.build_tree(files)
148
tree = self.make_branch_and_tree('.')
149
self.run_bzr(['remove', '--force'] + list(files),
150
error_regexes=["deleted a", "deleted b",
151
"deleted b/c", "deleted d"])
152
self.assertFilesDeleted(files)
154
def test_remove_deleted_files(self):
155
tree = self._make_add_and_assert_tree(files)
156
self.run_bzr("commit -m 'added files'")
157
my_files=[f for f in files]
158
my_files.sort(reverse=True)
160
osutils.delete_any(f)
161
self.assertInWorkingTree(files)
162
self.failIfExists(files)
163
self.run_bzr('remove ' + ' '.join(files))
164
self.assertNotInWorkingTree(a)
165
self.failIfExists(files)
167
def test_remove_non_existing_files(self):
168
tree = self._make_add_and_assert_tree([])
169
self.run_bzr(['remove', 'b'])
171
def test_remove_keep_non_existing_files(self):
172
tree = self._make_add_and_assert_tree([])
173
self.run_bzr('remove --keep b', error_regexes=["b is not versioned."])
175
def test_remove_files(self):
176
tree = self._make_add_and_assert_tree(files)
177
self.run_bzr("commit -m 'added files'")
178
self.run_bzr('remove a b b/c d',
179
error_regexes=["deleted a", "deleted b", "deleted b/c",
181
self.assertFilesDeleted(files)
183
def test_remove_keep_files(self):
184
tree = self._make_add_and_assert_tree(files)
185
self.run_bzr("commit -m 'added files'")
186
self.run_bzr('remove --keep a b b/c d',
187
error_regexes=["removed a", "removed b", "removed b/c",
189
self.assertFilesUnversioned(files)
191
def test_remove_with_new(self):
192
tree = self._make_add_and_assert_tree(files)
193
self.run_bzr('remove --new --keep',
194
error_regexes=["removed a", "removed b", "removed b/c"])
195
self.assertFilesUnversioned(files)
197
def test_remove_with_new_in_dir1(self):
198
tree = self._make_add_and_assert_tree(files)
199
self.run_bzr('remove --new --keep b b/c',
200
error_regexes=["removed b", "removed b/c"])
201
tree = WorkingTree.open('.')
202
self.assertInWorkingTree(a)
203
self.assertEqual(tree.path2id(a), a + _id)
204
self.assertFilesUnversioned([b,c])
206
def test_remove_with_new_in_dir2(self):
207
tree = self._make_add_and_assert_tree(files)
208
self.run_bzr('remove --new --keep .',
209
error_regexes=["removed a", "removed b", "removed b/c"])
210
tree = WorkingTree.open('.')
211
self.assertFilesUnversioned(files)