~bzr-pqm/bzr/bzr.dev

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