1
# Copyright (C) 2006, 2007 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
17
"""Tests for interface conformance of 'WorkingTree.remove'"""
20
from bzrlib.tests.workingtree_implementations import TestCaseWithWorkingTree
21
from bzrlib import errors, osutils
23
class TestRemove(TestCaseWithWorkingTree):
24
"""Tests WorkingTree.remove"""
26
files=['a', 'b/', 'b/c', 'd/']
32
self.makeAndChdirToTestDir()
33
tree = self.make_branch_and_tree('.')
34
self.build_tree(TestRemove.files)
37
def test_remove_keep(self):
38
"""Check that files are unversioned but not deleted."""
40
tree.add(TestRemove.files)
41
self.assertInWorkingTree(TestRemove.files)
43
tree.remove(TestRemove.files)
44
self.assertNotInWorkingTree(TestRemove.files)
45
self.failUnlessExists(TestRemove.files)
47
def test_remove_unchanged_files(self):
48
"""Check that unchanged files are removed and deleted."""
50
tree.add(TestRemove.files)
51
tree.commit("files must not have changes")
52
self.assertInWorkingTree(TestRemove.files)
54
tree.remove(TestRemove.files, keep_files=False)
56
self.assertNotInWorkingTree(TestRemove.files)
57
self.failIfExists(TestRemove.files)
59
def test_remove_added_files(self):
60
"""Removal of newly added files must fail."""
62
tree.add(TestRemove.files)
63
self.assertInWorkingTree(TestRemove.files)
65
tree.remove(TestRemove.files, keep_files=False)
66
self.fail('Should throw BzrRemoveChangedFilesError')
67
except errors.BzrRemoveChangedFilesError, e:
68
self.assertTrue(re.match('Can\'t remove changed or unknown files:'
69
'.*added:.*a.*b.*b/c.*d.*',
71
self.assertInWorkingTree(TestRemove.files)
72
self.failUnlessExists(TestRemove.files)
74
def test_remove_changed_file(self):
75
"""Removal of a changed files must fail."""
77
tree.add(TestRemove.a)
78
tree.commit("make sure a is versioned")
80
f.write("some other new content!")
82
self.assertInWorkingTree(TestRemove.a)
84
tree.remove(TestRemove.a, keep_files=False)
85
self.fail('Should throw BzrRemoveChangedFilesError')
86
except errors.BzrRemoveChangedFilesError, e:
87
self.assertTrue(re.match('Can\'t remove changed or unknown files:'
90
self.assertInWorkingTree(TestRemove.a)
91
self.failUnlessExists(TestRemove.a)
93
def test_remove_deleted_files(self):
94
"""Check that files are removed if they don't exist any more."""
96
tree.add(TestRemove.files)
97
tree.commit("make sure files are versioned")
98
for f in ['b/c', 'b', 'a', 'd']:
100
self.assertInWorkingTree(TestRemove.files)
101
self.failIfExists(TestRemove.files)
103
tree.remove(TestRemove.files, keep_files=False)
105
self.assertNotInWorkingTree(TestRemove.files)
106
self.failIfExists(TestRemove.files)
108
def test_force_remove_changed_files(self):
109
"""Check that changed files are removed and deleted when forced."""
110
tree = self.getTree()
111
tree.add(TestRemove.files)
112
self.assertInWorkingTree(TestRemove.files)
114
tree.remove(TestRemove.files, keep_files=False, force=True)
116
self.assertNotInWorkingTree(TestRemove.files)
117
self.failIfExists(TestRemove.files)
119
def test_remove_unknown_files(self):
120
"""Try to delete unknown files."""
121
tree = self.getTree()
123
tree.remove(TestRemove.files, keep_files=False)
124
self.fail('Should throw BzrRemoveChangedFilesError')
125
except errors.BzrRemoveChangedFilesError, e:
126
self.assertTrue(re.match('Can\'t remove changed or unknown files:'
127
'.*unknown:.*b/c.*b.*a.*d.*',
130
def test_remove_nonexisting_files(self):
131
"""Try to delete non-existing files."""
132
tree = self.getTree()
133
tree.remove([''], keep_files=False)
135
tree.remove(['xyz', 'abc/def'], keep_files=False)
136
self.fail('Should throw BzrRemoveChangedFilesError')
137
except errors.BzrRemoveChangedFilesError, e:
138
self.assertTrue(re.match('Can\'t remove changed or unknown files:'
139
'.*unknown:.*xyz.*abc/def.*',
142
def test_remove_nonempty_directory(self):
143
"""Unchanged non-empty directories should be deleted."""
144
tree = self.getTree()
145
tree.add(TestRemove.files)
146
tree.commit("make sure b is versioned")
147
self.assertInWorkingTree(TestRemove.files)
148
self.failUnlessExists(TestRemove.files)
149
tree.remove(TestRemove.b, keep_files=False)
150
self.assertNotInWorkingTree(TestRemove.b)
151
self.failIfExists(TestRemove.b)
153
def test_remove_nonempty_directory_with_unknowns(self):
154
"""Unchanged non-empty directories should be deleted."""
155
tree = self.getTree()
156
tree.add(TestRemove.files)
157
tree.commit("make sure b is versioned")
158
self.assertInWorkingTree(TestRemove.files)
159
self.failUnlessExists(TestRemove.files)
160
f = file('b/my_unknown_file', 'wb')
161
f.write("some content!")
164
tree.remove(TestRemove.b, keep_files=False)
165
self.fail('Should throw BzrRemoveChangedFilesError')
166
except errors.BzrRemoveChangedFilesError, e:
167
self.assertTrue(re.match('Can\'t remove changed or unknown files:'
168
'.*unknown:.*b/my_unknown_file.*',
170
self.assertInWorkingTree(TestRemove.b)
171
self.failUnlessExists(TestRemove.b)
173
def test_force_remove_nonempty_directory(self):
174
tree = self.getTree()
175
tree.add(TestRemove.files)
176
tree.commit("make sure b is versioned")
177
self.assertInWorkingTree(TestRemove.files)
178
self.failUnlessExists(TestRemove.files)
179
tree.remove(TestRemove.b, keep_files=False, force=True)
180
self.assertNotInWorkingTree(TestRemove.b_c)
181
self.failIfExists(TestRemove.b_c)
183
def test_remove_directory_with_changed_file(self):
184
"""Refuse to delete directories with changed files."""
185
tree = self.getTree()
186
tree.add(TestRemove.b_c)
187
tree.commit("make sure b and c are versioned")
188
f = file('b/c', 'wb')
189
f.write("some other new content!")
191
self.assertInWorkingTree(TestRemove.b_c)
193
tree.remove(TestRemove.b, keep_files=False)
194
self.fail('Should throw BzrRemoveChangedFilesError')
195
except errors.BzrRemoveChangedFilesError, e:
196
self.assertTrue(re.match('Can\'t remove changed or unknown files:'
197
'.*modified:.*b/c.*',
199
self.assertInWorkingTree(TestRemove.b_c)
200
self.failUnlessExists(TestRemove.b_c)
202
#see if we can force it now..
203
tree.remove(TestRemove.b, keep_files=False, force=True)
204
self.assertNotInWorkingTree(TestRemove.b_c)
205
self.failIfExists(TestRemove.b_c)