~bzr-pqm/bzr/bzr.dev

2052.3.2 by John Arbash Meinel
Change Copyright .. by Canonical to Copyright ... Canonical
1
# Copyright (C) 2005, 2006 Canonical Ltd
1551.6.25 by Aaron Bentley
split out blackbox test for bzr remove
2
#
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.
7
#
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.
12
#
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1551.6.25 by Aaron Bentley
split out blackbox test for bzr remove
16
17
2655.2.5 by Marius Kruger
* Improve BzrRemoveChangedFilesError message.
18
import os
2804.4.1 by Alexander Belchenko
some win32-specific fixes for selftest
19
import sys
1551.6.25 by Aaron Bentley
split out blackbox test for bzr remove
20
3619.5.1 by Robert Collins
* ``bzr rm`` will now scan for files that are missing and remove just
21
from bzrlib.tests import SymlinkFeature, TestSkipped
1551.6.25 by Aaron Bentley
split out blackbox test for bzr remove
22
from bzrlib.tests.blackbox import ExternalBase
1551.6.26 by Aaron Bentley
Add support for remove --new
23
from bzrlib.workingtree import WorkingTree
2292.1.1 by Marius Kruger
"bzr remove" and "bzr rm" will now remove the working file.
24
from bzrlib import osutils
25
26
_id='-id'
27
a='a'
28
b='b/'
29
c='b/c'
2292.1.30 by Marius Kruger
* Minor text fixes.
30
d='d/'
31
files=(a, b, c, d)
2292.1.1 by Marius Kruger
"bzr remove" and "bzr rm" will now remove the working file.
32
33
2292.1.13 by Marius Kruger
* merge the unversion command back into the remove command,
34
class TestRemove(ExternalBase):
2292.1.1 by Marius Kruger
"bzr remove" and "bzr rm" will now remove the working file.
35
3619.5.3 by Robert Collins
Review feedback.
36
    def _make_tree_and_add(self, paths):
2292.1.1 by Marius Kruger
"bzr remove" and "bzr rm" will now remove the working file.
37
        tree = self.make_branch_and_tree('.')
3619.5.1 by Robert Collins
* ``bzr rm`` will now scan for files that are missing and remove just
38
        tree.lock_write()
39
        try:
40
            self.build_tree(paths)
41
            for path in paths:
42
                file_id=str(path).replace('/', '_') + _id
43
                tree.add(path, file_id)
44
        finally:
45
            tree.unlock()
2292.1.1 by Marius Kruger
"bzr remove" and "bzr rm" will now remove the working file.
46
        return tree
47
2292.1.32 by Marius Kruger
* tests/__init__.run_bzr
48
    def assertFilesDeleted(self, files):
2292.1.1 by Marius Kruger
"bzr remove" and "bzr rm" will now remove the working file.
49
        for f in files:
50
            id=f+_id
51
            self.assertNotInWorkingTree(f)
2292.1.14 by Marius Kruger
* blackbox/test_remove
52
            self.failIfExists(f)
2292.1.1 by Marius Kruger
"bzr remove" and "bzr rm" will now remove the working file.
53
2292.1.32 by Marius Kruger
* tests/__init__.run_bzr
54
    def assertFilesUnversioned(self, files):
2292.1.13 by Marius Kruger
* merge the unversion command back into the remove command,
55
        for f in files:
56
            self.assertNotInWorkingTree(f)
2292.1.14 by Marius Kruger
* blackbox/test_remove
57
            self.failUnlessExists(f)
2292.1.13 by Marius Kruger
* merge the unversion command back into the remove command,
58
2292.1.32 by Marius Kruger
* tests/__init__.run_bzr
59
    def changeFile(self, file_name):
60
        f = file(file_name, 'ab')
61
        f.write("\nsome other new content!")
62
        f.close()
63
4676.2.3 by Vincent Ladeuil
FreeBSD is more strict than other OSes about deleting the working dir.
64
    def run_bzr_remove_changed_files(self, error_regexes, files_to_remove,
65
                                     working_dir=None):
2655.2.5 by Marius Kruger
* Improve BzrRemoveChangedFilesError message.
66
        error_regexes.extend(["Can't safely remove modified or unknown files:",
2292.1.26 by Marius Kruger
* tests/__init__
67
            'Use --keep to not delete them,'
68
            ' or --force to delete them regardless.'
69
            ])
2292.1.32 by Marius Kruger
* tests/__init__.run_bzr
70
        self.run_bzr_error(error_regexes,
4676.2.3 by Vincent Ladeuil
FreeBSD is more strict than other OSes about deleting the working dir.
71
                           ['remove'] + list(files_to_remove),
72
                           working_dir=working_dir)
2292.1.32 by Marius Kruger
* tests/__init__.run_bzr
73
        #see if we can force it now
4676.2.3 by Vincent Ladeuil
FreeBSD is more strict than other OSes about deleting the working dir.
74
        self.run_bzr(['remove', '--force'] + list(files_to_remove),
75
                     working_dir=working_dir)
2292.1.26 by Marius Kruger
* tests/__init__
76
3619.5.3 by Robert Collins
Review feedback.
77
    def test_remove_new_no_files_specified(self):
78
        tree = self.make_branch_and_tree('.')
79
        self.run_bzr_error(["bzr: ERROR: No matching files."], 'remove --new')
80
        self.run_bzr_error(["bzr: ERROR: No matching files."], 'remove --new .')
81
2292.1.13 by Marius Kruger
* merge the unversion command back into the remove command,
82
    def test_remove_no_files_specified(self):
3619.5.3 by Robert Collins
Review feedback.
83
        tree = self._make_tree_and_add(['foo'])
3619.5.1 by Robert Collins
* ``bzr rm`` will now scan for files that are missing and remove just
84
        out, err = self.run_bzr(['rm'])
85
        self.assertEqual('', err)
86
        self.assertEqual('', out)
87
        self.assertInWorkingTree('foo', tree=tree)
88
        self.failUnlessExists('foo')
89
90
    def test_remove_no_files_specified_missing_dir_and_contents(self):
3619.5.3 by Robert Collins
Review feedback.
91
        tree = self._make_tree_and_add(
3619.5.1 by Robert Collins
* ``bzr rm`` will now scan for files that are missing and remove just
92
            ['foo', 'dir/', 'dir/missing/', 'dir/missing/child'])
93
        self.get_transport('.').delete_tree('dir/missing')
94
        out, err = self.run_bzr(['rm'])
95
        self.assertEqual('', out)
96
        self.assertEqual(
97
            'removed dir/missing/child\n'
98
            'removed dir/missing\n',
99
            err)
100
        # non-missing paths not touched:
101
        self.assertInWorkingTree('foo', tree=tree)
102
        self.failUnlessExists('foo')
103
        self.assertInWorkingTree('dir', tree=tree)
104
        self.failUnlessExists('dir')
105
        # missing files unversioned
106
        self.assertNotInWorkingTree('dir/missing', tree=tree)
107
        self.assertNotInWorkingTree('dir/missing/child', tree=tree)
108
3619.5.3 by Robert Collins
Review feedback.
109
    def test_remove_no_files_specified_already_deleted(self):
110
        tree = self._make_tree_and_add(['foo', 'bar'])
111
        tree.commit('save foo and bar')
112
        os.unlink('bar')
113
        self.run_bzr(['rm'])
114
        self.assertEqual(None, tree.path2id('bar'))
115
        # Running rm with a deleted file does not error.
116
        out, err = self.run_bzr(['rm'])
117
        self.assertEqual('', out)
118
        self.assertEqual('', err)
119
3619.5.1 by Robert Collins
* ``bzr rm`` will now scan for files that are missing and remove just
120
    def test_remove_no_files_specified_missing_file(self):
3619.5.3 by Robert Collins
Review feedback.
121
        tree = self._make_tree_and_add(['foo', 'bar'])
3619.5.1 by Robert Collins
* ``bzr rm`` will now scan for files that are missing and remove just
122
        os.unlink('bar')
123
        out, err = self.run_bzr(['rm'])
124
        self.assertEqual('', out)
125
        self.assertEqual('removed bar\n', err)
126
        # non-missing files not touched:
127
        self.assertInWorkingTree('foo', tree=tree)
128
        self.failUnlessExists('foo')
129
        # missing files unversioned
130
        self.assertNotInWorkingTree('bar', tree=tree)
131
132
    def test_remove_no_files_specified_missing_link(self):
133
        self.requireFeature(SymlinkFeature)
3619.5.3 by Robert Collins
Review feedback.
134
        tree = self._make_tree_and_add(['foo'])
3619.5.1 by Robert Collins
* ``bzr rm`` will now scan for files that are missing and remove just
135
        os.symlink('foo', 'linkname')
136
        tree.add(['linkname'])
137
        os.unlink('linkname')
138
        out, err = self.run_bzr(['rm'])
139
        self.assertEqual('', out)
140
        self.assertEqual('removed linkname\n', err)
141
        # non-missing files not touched:
142
        self.assertInWorkingTree('foo', tree=tree)
143
        self.failUnlessExists('foo')
144
        # missing files unversioned
145
        self.assertNotInWorkingTree('linkname', tree=tree)
2292.1.1 by Marius Kruger
"bzr remove" and "bzr rm" will now remove the working file.
146
2292.1.32 by Marius Kruger
* tests/__init__.run_bzr
147
    def test_rm_one_file(self):
3619.5.3 by Robert Collins
Review feedback.
148
        tree = self._make_tree_and_add([a])
2292.1.32 by Marius Kruger
* tests/__init__.run_bzr
149
        self.run_bzr("commit -m 'added a'")
150
        self.run_bzr('rm a', error_regexes=["deleted a"])
151
        self.assertFilesDeleted([a])
152
153
    def test_remove_one_file(self):
3619.5.3 by Robert Collins
Review feedback.
154
        tree = self._make_tree_and_add([a])
2292.1.32 by Marius Kruger
* tests/__init__.run_bzr
155
        self.run_bzr("commit -m 'added a'")
156
        self.run_bzr('remove a', error_regexes=["deleted a"])
157
        self.assertFilesDeleted([a])
158
159
    def test_remove_keep_one_file(self):
3619.5.3 by Robert Collins
Review feedback.
160
        tree = self._make_tree_and_add([a])
2292.1.32 by Marius Kruger
* tests/__init__.run_bzr
161
        self.run_bzr('remove --keep a', error_regexes=["removed a"])
162
        self.assertFilesUnversioned([a])
163
164
    def test_remove_one_deleted_file(self):
3619.5.3 by Robert Collins
Review feedback.
165
        tree = self._make_tree_and_add([a])
2292.1.32 by Marius Kruger
* tests/__init__.run_bzr
166
        self.run_bzr("commit -m 'added a'")
167
        os.unlink(a)
168
        self.assertInWorkingTree(a)
169
        self.run_bzr('remove a')
170
        self.assertNotInWorkingTree(a)
171
2292.1.13 by Marius Kruger
* merge the unversion command back into the remove command,
172
    def test_remove_invalid_files(self):
2292.1.32 by Marius Kruger
* tests/__init__.run_bzr
173
        self.build_tree(files)
2292.1.1 by Marius Kruger
"bzr remove" and "bzr rm" will now remove the working file.
174
        tree = self.make_branch_and_tree('.')
2605.1.1 by Martin Pool
Merge fix for rm renamed files
175
        self.run_bzr(['remove', '.', 'xyz', 'abc/def'])
2292.1.1 by Marius Kruger
"bzr remove" and "bzr rm" will now remove the working file.
176
2292.1.13 by Marius Kruger
* merge the unversion command back into the remove command,
177
    def test_remove_unversioned_files(self):
2292.1.32 by Marius Kruger
* tests/__init__.run_bzr
178
        self.build_tree(files)
2292.1.13 by Marius Kruger
* merge the unversion command back into the remove command,
179
        tree = self.make_branch_and_tree('.')
2292.1.32 by Marius Kruger
* tests/__init__.run_bzr
180
        self.run_bzr_remove_changed_files(
181
            ['unknown:[.\s]*d/[.\s]*b/c[.\s]*b/[.\s]*a'], files)
182
183
    def test_remove_changed_files(self):
3619.5.3 by Robert Collins
Review feedback.
184
        tree = self._make_tree_and_add(files)
2292.1.32 by Marius Kruger
* tests/__init__.run_bzr
185
        self.run_bzr("commit -m 'added files'")
186
        self.changeFile(a)
187
        self.changeFile(c)
188
        self.run_bzr_remove_changed_files(['modified:[.\s]*a[.\s]*b/c'], files)
189
3042.2.1 by Lukáš Lalinský
Fix ``bzr rm`` to not delete modified and ignored files.
190
    def test_remove_changed_ignored_files(self):
3619.5.3 by Robert Collins
Review feedback.
191
        tree = self._make_tree_and_add(['a'])
3042.2.1 by Lukáš Lalinský
Fix ``bzr rm`` to not delete modified and ignored files.
192
        self.run_bzr(['ignore', 'a'])
193
        self.run_bzr_remove_changed_files(['added:[.\s]*a'], ['a'])
194
2292.1.32 by Marius Kruger
* tests/__init__.run_bzr
195
    def test_remove_changed_files_from_child_dir(self):
2804.4.1 by Alexander Belchenko
some win32-specific fixes for selftest
196
        if sys.platform == 'win32':
197
            raise TestSkipped("Windows unable to remove '.' directory")
3619.5.3 by Robert Collins
Review feedback.
198
        tree = self._make_tree_and_add(files)
2292.1.32 by Marius Kruger
* tests/__init__.run_bzr
199
        self.run_bzr("commit -m 'added files'")
200
        self.changeFile(a)
201
        self.changeFile(c)
4676.2.3 by Vincent Ladeuil
FreeBSD is more strict than other OSes about deleting the working dir.
202
        self.run_bzr_remove_changed_files(
203
            ['modified:[.\s]*a[.\s]*b/c'],
204
            ['../a', 'c', '.', '../d'], working_dir='b')
2292.1.32 by Marius Kruger
* tests/__init__.run_bzr
205
        self.assertNotInWorkingTree(files)
206
        self.failIfExists(files)
2292.1.13 by Marius Kruger
* merge the unversion command back into the remove command,
207
208
    def test_remove_keep_unversioned_files(self):
2292.1.32 by Marius Kruger
* tests/__init__.run_bzr
209
        self.build_tree(files)
2292.1.13 by Marius Kruger
* merge the unversion command back into the remove command,
210
        tree = self.make_branch_and_tree('.')
2292.1.27 by Marius Kruger
* tests/__init__.TestCase.run_bzr_captured
211
        self.run_bzr('remove --keep a', error_regexes=["a is not versioned."])
2292.1.32 by Marius Kruger
* tests/__init__.run_bzr
212
        self.assertFilesUnversioned(files)
2292.1.13 by Marius Kruger
* merge the unversion command back into the remove command,
213
214
    def test_remove_force_unversioned_files(self):
2292.1.32 by Marius Kruger
* tests/__init__.run_bzr
215
        self.build_tree(files)
2292.1.13 by Marius Kruger
* merge the unversion command back into the remove command,
216
        tree = self.make_branch_and_tree('.')
2605.1.2 by Martin Pool
Fix up run_bzr calls
217
        self.run_bzr(['remove', '--force'] + list(files),
2292.1.32 by Marius Kruger
* tests/__init__.run_bzr
218
                     error_regexes=["deleted a", "deleted b",
219
                                    "deleted b/c", "deleted d"])
220
        self.assertFilesDeleted(files)
221
222
    def test_remove_deleted_files(self):
3619.5.3 by Robert Collins
Review feedback.
223
        tree = self._make_tree_and_add(files)
2292.1.32 by Marius Kruger
* tests/__init__.run_bzr
224
        self.run_bzr("commit -m 'added files'")
225
        my_files=[f for f in files]
226
        my_files.sort(reverse=True)
227
        for f in my_files:
228
            osutils.delete_any(f)
229
        self.assertInWorkingTree(files)
230
        self.failIfExists(files)
231
        self.run_bzr('remove ' + ' '.join(files))
232
        self.assertNotInWorkingTree(a)
233
        self.failIfExists(files)
2292.1.13 by Marius Kruger
* merge the unversion command back into the remove command,
234
235
    def test_remove_non_existing_files(self):
3619.5.3 by Robert Collins
Review feedback.
236
        tree = self._make_tree_and_add([])
2605.1.1 by Martin Pool
Merge fix for rm renamed files
237
        self.run_bzr(['remove', 'b'])
2292.1.13 by Marius Kruger
* merge the unversion command back into the remove command,
238
239
    def test_remove_keep_non_existing_files(self):
3619.5.3 by Robert Collins
Review feedback.
240
        tree = self._make_tree_and_add([])
2292.1.27 by Marius Kruger
* tests/__init__.TestCase.run_bzr_captured
241
        self.run_bzr('remove --keep b', error_regexes=["b is not versioned."])
2292.1.1 by Marius Kruger
"bzr remove" and "bzr rm" will now remove the working file.
242
2292.1.23 by Marius Kruger
Revert test_too_much.py and just do the minimum te get the tests to pass.
243
    def test_remove_files(self):
3619.5.3 by Robert Collins
Review feedback.
244
        tree = self._make_tree_and_add(files)
2292.1.27 by Marius Kruger
* tests/__init__.TestCase.run_bzr_captured
245
        self.run_bzr("commit -m 'added files'")
2292.1.30 by Marius Kruger
* Minor text fixes.
246
        self.run_bzr('remove a b b/c d',
247
                     error_regexes=["deleted a", "deleted b", "deleted b/c",
248
                     "deleted d"])
2292.1.23 by Marius Kruger
Revert test_too_much.py and just do the minimum te get the tests to pass.
249
        self.assertFilesDeleted(files)
250
251
    def test_remove_keep_files(self):
3619.5.3 by Robert Collins
Review feedback.
252
        tree = self._make_tree_and_add(files)
2292.1.27 by Marius Kruger
* tests/__init__.TestCase.run_bzr_captured
253
        self.run_bzr("commit -m 'added files'")
2292.1.30 by Marius Kruger
* Minor text fixes.
254
        self.run_bzr('remove --keep a b b/c d',
255
                     error_regexes=["removed a", "removed b", "removed b/c",
256
                     "removed d"])
2292.1.23 by Marius Kruger
Revert test_too_much.py and just do the minimum te get the tests to pass.
257
        self.assertFilesUnversioned(files)
258
2292.1.27 by Marius Kruger
* tests/__init__.TestCase.run_bzr_captured
259
    def test_remove_with_new(self):
3619.5.3 by Robert Collins
Review feedback.
260
        tree = self._make_tree_and_add(files)
2292.1.27 by Marius Kruger
* tests/__init__.TestCase.run_bzr_captured
261
        self.run_bzr('remove --new --keep',
262
                     error_regexes=["removed a", "removed b", "removed b/c"])
2292.1.14 by Marius Kruger
* blackbox/test_remove
263
        self.assertFilesUnversioned(files)
2292.1.1 by Marius Kruger
"bzr remove" and "bzr rm" will now remove the working file.
264
2292.1.27 by Marius Kruger
* tests/__init__.TestCase.run_bzr_captured
265
    def test_remove_with_new_in_dir1(self):
3619.5.3 by Robert Collins
Review feedback.
266
        tree = self._make_tree_and_add(files)
2292.1.27 by Marius Kruger
* tests/__init__.TestCase.run_bzr_captured
267
        self.run_bzr('remove --new --keep b b/c',
268
                     error_regexes=["removed b", "removed b/c"])
2292.1.1 by Marius Kruger
"bzr remove" and "bzr rm" will now remove the working file.
269
        tree = WorkingTree.open('.')
270
        self.assertInWorkingTree(a)
2292.1.32 by Marius Kruger
* tests/__init__.run_bzr
271
        self.assertEqual(tree.path2id(a), a + _id)
2292.1.14 by Marius Kruger
* blackbox/test_remove
272
        self.assertFilesUnversioned([b,c])
2292.1.1 by Marius Kruger
"bzr remove" and "bzr rm" will now remove the working file.
273
2292.1.27 by Marius Kruger
* tests/__init__.TestCase.run_bzr_captured
274
    def test_remove_with_new_in_dir2(self):
3619.5.3 by Robert Collins
Review feedback.
275
        tree = self._make_tree_and_add(files)
2292.1.27 by Marius Kruger
* tests/__init__.TestCase.run_bzr_captured
276
        self.run_bzr('remove --new --keep .',
277
                     error_regexes=["removed a", "removed b", "removed b/c"])
2292.1.1 by Marius Kruger
"bzr remove" and "bzr rm" will now remove the working file.
278
        tree = WorkingTree.open('.')
2292.1.32 by Marius Kruger
* tests/__init__.run_bzr
279
        self.assertFilesUnversioned(files)