~bzr-pqm/bzr/bzr.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# Copyright (C) 2007-2012, 2016 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

"""Tests for interface conformance of 'WorkingTree.remove'"""

from bzrlib.tests.per_workingtree import TestCaseWithWorkingTree
from bzrlib import ignores, osutils

class TestRemove(TestCaseWithWorkingTree):
    """Tests WorkingTree.remove"""

    files = ['a', 'b/', 'b/c', 'd/']
    rfiles = ['b/c', 'b', 'a', 'd']
    backup_files = ['a.~1~', 'b.~1~/', 'b.~1~/c.~1~', 'd.~1~/']

    def get_tree(self, files):
        tree = self.make_branch_and_tree('.')
        self.build_tree(files)
        self.assertPathExists(files)
        return tree

    def get_committed_tree(self, files, message="Committing"):
        tree = self.get_tree(files)
        tree.add(files)
        tree.commit(message)
        self.assertInWorkingTree(files)
        return tree

    def assertRemovedAndDeleted(self, files):
        self.assertNotInWorkingTree(files)
        self.assertPathDoesNotExist(files)

    def assertRemovedAndNotDeleted(self, files):
        self.assertNotInWorkingTree(files)
        self.assertPathExists(files)

    def test_remove_keep(self):
        """Check that files and directories are unversioned but not deleted."""
        tree = self.get_tree(TestRemove.files)
        tree.add(TestRemove.files)
        self.assertInWorkingTree(TestRemove.files)

        tree.remove(TestRemove.files)
        self.assertRemovedAndNotDeleted(TestRemove.files)

    def test_remove_keep_subtree(self):
        """Check that a directory is unversioned but not deleted."""
        tree = self.make_branch_and_tree('.')
        subtree = self.make_branch_and_tree('subtree')
        tree.add('subtree', 'subtree-id')

        tree.remove('subtree')
        self.assertRemovedAndNotDeleted('subtree')

    def test_remove_unchanged_files(self):
        """Check that unchanged files are removed and deleted."""
        tree = self.get_committed_tree(TestRemove.files)
        tree.remove(TestRemove.files, keep_files=False)
        self.assertRemovedAndDeleted(TestRemove.files)
        tree._validate()

    def test_remove_added_files(self):
        """Removal of newly added files must back them up."""
        tree = self.get_tree(TestRemove.files)
        tree.add(TestRemove.files)
        self.assertInWorkingTree(TestRemove.files)
        tree.remove(TestRemove.files, keep_files=False)
        self.assertNotInWorkingTree(TestRemove.files)
        self.assertPathExists(TestRemove.backup_files)
        tree._validate()

    def test_remove_changed_file(self):
        """Removal of changed files must back it up."""
        tree = self.get_committed_tree(['a'])
        self.build_tree_contents([('a', "some other new content!")])
        self.assertInWorkingTree('a')
        tree.remove('a', keep_files=False)
        self.assertNotInWorkingTree(TestRemove.files)
        self.assertPathExists('a.~1~')
        tree._validate()

    def test_remove_deleted_files(self):
        """Check that files are removed if they don't exist any more."""
        tree = self.get_committed_tree(TestRemove.files)
        for f in TestRemove.rfiles:
            osutils.delete_any(f)
        self.assertInWorkingTree(TestRemove.files)
        self.assertPathDoesNotExist(TestRemove.files)
        tree.remove(TestRemove.files, keep_files=False)
        self.assertRemovedAndDeleted(TestRemove.files)
        tree._validate()

    def test_remove_renamed_files(self):
        """Check that files are removed even if they are renamed."""
        tree = self.get_committed_tree(TestRemove.files)

        for f in TestRemove.rfiles:
            tree.rename_one(f,f+'x')
        rfilesx = ['bx/cx', 'bx', 'ax', 'dx']
        self.assertInWorkingTree(rfilesx)
        self.assertPathExists(rfilesx)

        tree.remove(rfilesx, keep_files=False)
        self.assertRemovedAndDeleted(rfilesx)
        tree._validate()

    def test_remove_renamed_changed_files(self):
        """Check that files that are renamed and changed are backed up."""
        tree = self.get_committed_tree(TestRemove.files)

        for f in TestRemove.rfiles:
            tree.rename_one(f,f+'x')
        rfilesx = ['bx/cx', 'bx', 'ax', 'dx']
        self.build_tree_contents([('ax','changed and renamed!'),
                                  ('bx/cx','changed and renamed!')])
        self.assertInWorkingTree(rfilesx)
        self.assertPathExists(rfilesx)

        tree.remove(rfilesx, keep_files=False)
        self.assertNotInWorkingTree(rfilesx)
        self.assertPathExists(['bx.~1~/cx.~1~', 'bx.~1~', 'ax.~1~'])
        self.assertPathDoesNotExist('dx.~1~') # unchanged file
        tree._validate()

    def test_force_remove_changed_files(self):
        """Check that changed files are removed and deleted when forced."""
        tree = self.get_tree(TestRemove.files)
        tree.add(TestRemove.files)
        self.assertInWorkingTree(TestRemove.files)

        tree.remove(TestRemove.files, keep_files=False, force=True)
        self.assertRemovedAndDeleted(TestRemove.files)
        self.assertPathDoesNotExist(['a.~1~', 'b.~1~/', 'b.~1~/c', 'd.~1~/'])
        tree._validate()

    def test_remove_unknown_files(self):
        """Unknown files shuld be backed up"""
        tree = self.get_tree(TestRemove.files)
        tree.remove(TestRemove.files, keep_files=False)
        self.assertRemovedAndDeleted(TestRemove.files)
        self.assertPathExists(TestRemove.backup_files)
        tree._validate()

    def test_remove_nonexisting_files(self):
        """Try to delete non-existing files."""
        tree = self.get_tree(TestRemove.files)
        tree.remove([''], keep_files=False)
        tree.remove(['xyz', 'abc/def'], keep_files=False)
        tree._validate()

    def test_remove_unchanged_directory(self):
        """Unchanged directories should be deleted."""
        files = ['b/', 'b/c', 'b/sub_directory/', 'b/sub_directory/with_file']
        tree = self.get_committed_tree(files)
        tree.remove('b', keep_files=False)
        self.assertRemovedAndDeleted('b')
        tree._validate()

    def test_remove_absent_directory(self):
        """Removing a absent directory succeeds without corruption (#150438)."""
        paths = ['a/', 'a/b']
        tree = self.get_committed_tree(paths)
        tree.bzrdir.root_transport.delete_tree('a')
        tree.remove(['a'])
        self.assertRemovedAndDeleted('b')
        tree._validate()

    def test_remove_unknown_ignored_files(self):
        """Unknown ignored files should be deleted."""
        tree = self.get_committed_tree(['b/'])
        ignores.add_runtime_ignores(["*ignored*"])

        self.build_tree(['unknown_ignored_file'])
        self.assertNotEqual(None, tree.is_ignored('unknown_ignored_file'))
        tree.remove('unknown_ignored_file', keep_files=False)
        self.assertRemovedAndDeleted('unknown_ignored_file')

        self.build_tree(['b/unknown_ignored_file', 'b/unknown_ignored_dir/'])
        self.assertNotEqual(None, tree.is_ignored('b/unknown_ignored_file'))
        self.assertNotEqual(None, tree.is_ignored('b/unknown_ignored_dir'))
        tree.remove('b', keep_files=False)
        self.assertRemovedAndDeleted('b')
        tree._validate()

    def test_remove_changed_ignored_files(self):
        """Changed ignored files should be backed up."""
        files = ['an_ignored_file']
        tree = self.get_tree(files)
        tree.add(files)
        ignores.add_runtime_ignores(["*ignored*"])
        self.assertInWorkingTree(files)
        self.assertNotEqual(None, tree.is_ignored(files[0]))

        tree.remove(files, keep_files=False)
        self.assertNotInWorkingTree(files)
        self.assertPathExists('an_ignored_file.~1~')
        tree._validate()

    def test_dont_remove_directory_with_unknowns(self):
        """Directories with unknowns should be backed up."""
        directories = ['a/', 'b/', 'c/', 'c/c/']
        tree = self.get_committed_tree(directories)

        self.build_tree(['a/unknown_file'])
        tree.remove('a', keep_files=False)
        self.assertPathExists('a.~1~/unknown_file')

        self.build_tree(['b/unknown_directory'])
        tree.remove('b', keep_files=False)
        self.assertPathExists('b.~1~/unknown_directory')

        self.build_tree(['c/c/unknown_file'])
        tree.remove('c/c', keep_files=False)
        self.assertPathExists('c/c.~1~/unknown_file')

        tree.remove('c', keep_files=False)
        self.assertPathExists('c.~1~/')

        self.assertNotInWorkingTree(directories)
        tree._validate()

    def test_force_remove_directory_with_unknowns(self):
        """Unchanged non-empty directories should be deleted when forced."""
        files = ['b/', 'b/c']
        tree = self.get_committed_tree(files)

        other_files = ['b/unknown_file', 'b/sub_directory/',
            'b/sub_directory/with_file', 'b/sub_directory/sub_directory/']
        self.build_tree(other_files)

        self.assertInWorkingTree(files)
        self.assertPathExists(files)

        tree.remove('b', keep_files=False, force=True)

        self.assertRemovedAndDeleted(files)
        self.assertRemovedAndDeleted(other_files)
        tree._validate()

    def test_remove_directory_with_changed_file(self):
        """Backup directories with changed files."""
        files = ['b/', 'b/c']
        tree = self.get_committed_tree(files)
        self.build_tree_contents([('b/c', "some other new content!")])

        tree.remove('b', keep_files=False)
        self.assertPathExists('b.~1~/c.~1~')
        self.assertNotInWorkingTree(files)

    def test_remove_force_directory_with_changed_file(self):
        """Delete directories with changed files when forced."""
        files = ['b/', 'b/c']
        tree = self.get_committed_tree(files)
        self.build_tree_contents([('b/c', "some other new content!")])

        # see if we can force it now..
        tree.remove('b', keep_files=False, force=True)
        self.assertRemovedAndDeleted(files)
        tree._validate()

    def test_remove_directory_with_changed_emigrated_file(self):
        # As per bug #129880
        tree = self.make_branch_and_tree('.')
        self.build_tree_contents([('somedir/',), ('somedir/file', 'contents')])
        tree.add(['somedir', 'somedir/file'])
        tree.commit(message="first")
        self.build_tree_contents([('somedir/file', 'changed')])
        tree.rename_one('somedir/file', 'moved-file')
        tree.remove('somedir', keep_files=False)
        self.assertNotInWorkingTree('somedir')
        self.assertPathDoesNotExist('somedir')
        self.assertInWorkingTree('moved-file')
        self.assertPathExists('moved-file')

    def test_remove_directory_with_renames(self):
        """Delete directory with renames in or out."""

        files = ['a/', 'a/file', 'a/directory/', 'b/']
        files_to_move = ['a/file', 'a/directory/']

        tree = self.get_committed_tree(files)
        # move stuff from a=>b
        tree.move(['a/file', 'a/directory'], to_dir='b')

        moved_files = ['b/file', 'b/directory/']
        self.assertRemovedAndDeleted(files_to_move)
        self.assertInWorkingTree(moved_files)
        self.assertPathExists(moved_files)

        # check if it works with renames out
        tree.remove('a', keep_files=False)
        self.assertRemovedAndDeleted(['a/'])

        # check if it works with renames in
        tree.remove('b', keep_files=False)
        self.assertRemovedAndDeleted(['b/'])
        tree._validate()

    def test_non_cwd(self):
        tree = self.make_branch_and_tree('tree')
        self.build_tree(['tree/dir/', 'tree/dir/file'])
        tree.add(['dir', 'dir/file'])
        tree.commit('add file')
        tree.remove('dir/', keep_files=False)
        self.assertPathDoesNotExist('tree/dir/file')
        self.assertNotInWorkingTree('tree/dir/file', 'tree')
        tree._validate()

    def test_remove_uncommitted_removed_file(self):
        # As per bug #152811
        tree = self.get_committed_tree(['a'])
        tree.remove('a', keep_files=False)
        tree.remove('a', keep_files=False)
        self.assertPathDoesNotExist('a')
        tree._validate()

    def test_remove_file_and_containing_dir(self):
        tree = self.get_committed_tree(['config/', 'config/file'])
        tree.remove('config/file', keep_files=False)
        tree.remove('config', keep_files=False)
        self.assertPathDoesNotExist('config/file')
        self.assertPathDoesNotExist('config')
        tree._validate()

    def test_remove_dir_before_bzr(self):
        # As per bug #272648. Note that a file must be present in the directory
        # or the bug doesn't manifest itself.
        tree = self.get_committed_tree(['.aaa/', '.aaa/file'])
        tree.remove('.aaa/', keep_files=False)
        self.assertPathDoesNotExist('.aaa/file')
        self.assertPathDoesNotExist('.aaa')
        tree._validate()