17
17
"""Tests for interface conformance of 'WorkingTree.remove'"""
20
19
from bzrlib.tests.workingtree_implementations import TestCaseWithWorkingTree
21
20
from bzrlib import errors, osutils
23
22
class TestRemove(TestCaseWithWorkingTree):
24
23
"""Tests WorkingTree.remove"""
26
files=['a', 'b/', 'b/c', 'd/']
25
files = ['a', 'b/', 'b/c', 'd/']
26
rfiles = ['b/c', 'b', 'a', 'd']
60
60
tree = self.getTree()
61
61
tree.add(TestRemove.files)
62
62
self.assertInWorkingTree(TestRemove.files)
64
tree.remove(TestRemove.files, keep_files=False)
65
self.fail('Should throw BzrRemoveChangedFilesError')
66
except errors.BzrRemoveChangedFilesError, e:
67
self.assertTrue(re.match('Can\'t remove changed or unknown files:'
68
'.*added:.*a.*b.*b/c.*d.*',
63
err = self.assertRaises(errors.BzrRemoveChangedFilesError, tree.remove,
64
TestRemove.files, keep_files=False)
65
self.assertContainsRe(err.changes_as_text,
66
'(?s)added:.*a.*b/.*b/c.*d/')
70
67
self.assertInWorkingTree(TestRemove.files)
71
68
self.failUnlessExists(TestRemove.files)
75
72
tree = self.getTree()
76
73
tree.add(TestRemove.a)
77
74
tree.commit("make sure a is versioned")
79
f.write("some other new content!")
75
self.build_tree_contents([('a', "some other new content!")])
81
76
self.assertInWorkingTree(TestRemove.a)
83
tree.remove(TestRemove.a, keep_files=False)
84
self.fail('Should throw BzrRemoveChangedFilesError')
85
except errors.BzrRemoveChangedFilesError, e:
86
self.assertTrue(re.match('Can\'t remove changed or unknown files:'
77
err = self.assertRaises(errors.BzrRemoveChangedFilesError, tree.remove,
78
TestRemove.a, keep_files=False)
79
self.assertContainsRe(err.changes_as_text, '(?s)modified:.*a')
89
80
self.assertInWorkingTree(TestRemove.a)
90
81
self.failUnlessExists(TestRemove.a)
94
85
tree = self.getTree()
95
86
tree.add(TestRemove.files)
96
87
tree.commit("make sure files are versioned")
97
for f in ['b/c', 'b', 'a', 'd']:
88
for f in TestRemove.rfiles:
98
89
osutils.delete_any(f)
99
90
self.assertInWorkingTree(TestRemove.files)
100
91
self.failIfExists(TestRemove.files)
104
95
self.assertNotInWorkingTree(TestRemove.files)
105
96
self.failIfExists(TestRemove.files)
98
def test_remove_renamed_files(self):
99
"""Check that files are removed even if they are renamed."""
100
tree = self.getTree()
101
tree.add(TestRemove.files)
102
tree.commit("make sure files are versioned")
104
for f in TestRemove.rfiles:
105
tree.rename_one(f,f+'x')
106
rfilesx = ['bx/cx', 'bx', 'ax', 'dx']
107
self.assertInWorkingTree(rfilesx)
108
self.failUnlessExists(rfilesx)
110
tree.remove(rfilesx, keep_files=False)
112
self.assertNotInWorkingTree(rfilesx)
113
self.failIfExists(rfilesx)
115
def test_remove_renamed_changed_files(self):
116
"""Check that files are not removed if they are renamed and changed."""
117
tree = self.getTree()
118
tree.add(TestRemove.files)
119
tree.commit("make sure files are versioned")
121
for f in TestRemove.rfiles:
122
tree.rename_one(f,f+'x')
123
rfilesx = ['bx/cx', 'bx', 'ax', 'dx']
124
self.build_tree_contents([('ax','changed and renamed!'),
125
('bx/cx','changed and renamed!')])
126
self.assertInWorkingTree(rfilesx)
127
self.failUnlessExists(rfilesx)
129
err = self.assertRaises(errors.BzrRemoveChangedFilesError, tree.remove,
130
rfilesx, keep_files=False)
131
self.assertContainsRe(err.changes_as_text,
132
'(?s)modified:.*ax.*bx/cx')
133
self.assertInWorkingTree(rfilesx)
134
self.failUnlessExists(rfilesx)
107
136
def test_force_remove_changed_files(self):
108
137
"""Check that changed files are removed and deleted when forced."""
109
138
tree = self.getTree()
118
147
def test_remove_unknown_files(self):
119
148
"""Try to delete unknown files."""
120
149
tree = self.getTree()
122
tree.remove(TestRemove.files, keep_files=False)
123
self.fail('Should throw BzrRemoveChangedFilesError')
124
except errors.BzrRemoveChangedFilesError, e:
125
self.assertTrue(re.match('Can\'t remove changed or unknown files:'
126
'.*unknown:.*b/c.*b.*a.*d.*',
150
err = self.assertRaises(errors.BzrRemoveChangedFilesError, tree.remove,
151
TestRemove.files, keep_files=False)
152
self.assertContainsRe(err.changes_as_text,
153
'(?s)unknown:.*d/.*b/c.*b/.*a.*')
129
155
def test_remove_nonexisting_files(self):
130
156
"""Try to delete non-existing files."""
131
157
tree = self.getTree()
132
158
tree.remove([''], keep_files=False)
134
tree.remove(['xyz', 'abc/def'], keep_files=False)
135
self.fail('Should throw BzrRemoveChangedFilesError')
136
except errors.BzrRemoveChangedFilesError, e:
137
self.assertTrue(re.match('Can\'t remove changed or unknown files:'
138
'.*unknown:.*xyz.*abc/def.*',
159
tree.remove(['xyz', 'abc/def'], keep_files=False)
141
161
def test_remove_nonempty_directory(self):
142
162
"""Unchanged non-empty directories should be deleted."""
156
176
tree.commit("make sure b is versioned")
157
177
self.assertInWorkingTree(TestRemove.files)
158
178
self.failUnlessExists(TestRemove.files)
159
f = file('b/my_unknown_file', 'wb')
160
f.write("some content!")
163
tree.remove(TestRemove.b, keep_files=False)
164
self.fail('Should throw BzrRemoveChangedFilesError')
165
except errors.BzrRemoveChangedFilesError, e:
166
self.assertTrue(re.match('Can\'t remove changed or unknown files:'
167
'.*unknown:.*b/my_unknown_file.*',
179
self.build_tree(['b/my_unknown_file'])
180
err = self.assertRaises(errors.BzrRemoveChangedFilesError, tree.remove,
181
TestRemove.b, keep_files=False)
182
self.assertContainsRe(err.changes_as_text,
183
'(?s)unknown:.*b/my_unknown_file')
169
184
self.assertInWorkingTree(TestRemove.b)
170
185
self.failUnlessExists(TestRemove.b)
184
199
tree = self.getTree()
185
200
tree.add(TestRemove.b_c)
186
201
tree.commit("make sure b and c are versioned")
187
f = file('b/c', 'wb')
188
f.write("some other new content!")
202
self.build_tree_contents([('b/c', "some other new content!")])
190
203
self.assertInWorkingTree(TestRemove.b_c)
192
tree.remove(TestRemove.b, keep_files=False)
193
self.fail('Should throw BzrRemoveChangedFilesError')
194
except errors.BzrRemoveChangedFilesError, e:
195
self.assertTrue(re.match('Can\'t remove changed or unknown files:'
196
'.*modified:.*b/c.*',
204
err = self.assertRaises(errors.BzrRemoveChangedFilesError, tree.remove,
205
TestRemove.b, keep_files=False)
206
self.assertContainsRe(err.changes_as_text, '(?s)modified:.*b/c')
198
207
self.assertInWorkingTree(TestRemove.b_c)
199
208
self.failUnlessExists(TestRemove.b_c)