~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/blackbox/test_remove.py

  • Committer: Martin Pool
  • Date: 2005-07-28 11:56:24 UTC
  • Revision ID: mbp@sourcefrog.net-20050728115624-93c11c2b1e399023
- note changes to command line parsing

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
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
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
 
 
17
 
 
18
 
import os
19
 
import sys
20
 
 
21
 
from bzrlib.tests import SymlinkFeature, TestSkipped
22
 
from bzrlib.tests import TestCaseWithTransport
23
 
from bzrlib.workingtree import WorkingTree
24
 
from bzrlib import osutils
25
 
 
26
 
_id='-id'
27
 
a='a'
28
 
b='b/'
29
 
c='b/c'
30
 
d='d/'
31
 
files=(a, b, c, d)
32
 
 
33
 
 
34
 
class TestRemove(TestCaseWithTransport):
35
 
 
36
 
    def _make_tree_and_add(self, paths):
37
 
        tree = self.make_branch_and_tree('.')
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()
46
 
        return tree
47
 
 
48
 
    def assertFilesDeleted(self, files):
49
 
        for f in files:
50
 
            id=f+_id
51
 
            self.assertNotInWorkingTree(f)
52
 
            self.failIfExists(f)
53
 
 
54
 
    def assertFilesUnversioned(self, files):
55
 
        for f in files:
56
 
            self.assertNotInWorkingTree(f)
57
 
            self.failUnlessExists(f)
58
 
 
59
 
    def changeFile(self, file_name):
60
 
        f = file(file_name, 'ab')
61
 
        f.write("\nsome other new content!")
62
 
        f.close()
63
 
 
64
 
    def run_bzr_remove_changed_files(self, files_to_remove, working_dir=None):
65
 
        self.run_bzr(['remove'] + list(files_to_remove),
66
 
           working_dir=working_dir)
67
 
 
68
 
    def test_remove_new_no_files_specified(self):
69
 
        tree = self.make_branch_and_tree('.')
70
 
        self.run_bzr_error(["bzr: ERROR: No matching files."], 'remove --new')
71
 
        self.run_bzr_error(["bzr: ERROR: No matching files."], 'remove --new .')
72
 
 
73
 
    def test_remove_no_files_specified(self):
74
 
        tree = self._make_tree_and_add(['foo'])
75
 
        out, err = self.run_bzr(['rm'])
76
 
        self.assertEqual('', err)
77
 
        self.assertEqual('', out)
78
 
        self.assertInWorkingTree('foo', tree=tree)
79
 
        self.failUnlessExists('foo')
80
 
 
81
 
    def test_remove_no_files_specified_missing_dir_and_contents(self):
82
 
        tree = self._make_tree_and_add(
83
 
            ['foo', 'dir/', 'dir/missing/', 'dir/missing/child'])
84
 
        self.get_transport('.').delete_tree('dir/missing')
85
 
        out, err = self.run_bzr(['rm'])
86
 
        self.assertEqual('', out)
87
 
        self.assertEqual(
88
 
            'removed dir/missing/child\n'
89
 
            'removed dir/missing\n',
90
 
            err)
91
 
        # non-missing paths not touched:
92
 
        self.assertInWorkingTree('foo', tree=tree)
93
 
        self.failUnlessExists('foo')
94
 
        self.assertInWorkingTree('dir', tree=tree)
95
 
        self.failUnlessExists('dir')
96
 
        # missing files unversioned
97
 
        self.assertNotInWorkingTree('dir/missing', tree=tree)
98
 
        self.assertNotInWorkingTree('dir/missing/child', tree=tree)
99
 
 
100
 
    def test_remove_no_files_specified_already_deleted(self):
101
 
        tree = self._make_tree_and_add(['foo', 'bar'])
102
 
        tree.commit('save foo and bar')
103
 
        os.unlink('bar')
104
 
        self.run_bzr(['rm'])
105
 
        self.assertEqual(None, tree.path2id('bar'))
106
 
        # Running rm with a deleted file does not error.
107
 
        out, err = self.run_bzr(['rm'])
108
 
        self.assertEqual('', out)
109
 
        self.assertEqual('', err)
110
 
 
111
 
    def test_remove_no_files_specified_missing_file(self):
112
 
        tree = self._make_tree_and_add(['foo', 'bar'])
113
 
        os.unlink('bar')
114
 
        out, err = self.run_bzr(['rm'])
115
 
        self.assertEqual('', out)
116
 
        self.assertEqual('removed bar\n', err)
117
 
        # non-missing files not touched:
118
 
        self.assertInWorkingTree('foo', tree=tree)
119
 
        self.failUnlessExists('foo')
120
 
        # missing files unversioned
121
 
        self.assertNotInWorkingTree('bar', tree=tree)
122
 
 
123
 
    def test_remove_no_files_specified_missing_link(self):
124
 
        self.requireFeature(SymlinkFeature)
125
 
        tree = self._make_tree_and_add(['foo'])
126
 
        os.symlink('foo', 'linkname')
127
 
        tree.add(['linkname'])
128
 
        os.unlink('linkname')
129
 
        out, err = self.run_bzr(['rm'])
130
 
        self.assertEqual('', out)
131
 
        self.assertEqual('removed linkname\n', err)
132
 
        # non-missing files not touched:
133
 
        self.assertInWorkingTree('foo', tree=tree)
134
 
        self.failUnlessExists('foo')
135
 
        # missing files unversioned
136
 
        self.assertNotInWorkingTree('linkname', tree=tree)
137
 
 
138
 
    def test_rm_one_file(self):
139
 
        tree = self._make_tree_and_add([a])
140
 
        self.run_bzr("commit -m 'added a'")
141
 
        self.run_bzr('rm a', error_regexes=["deleted a"])
142
 
        self.assertFilesDeleted([a])
143
 
 
144
 
    def test_remove_one_file(self):
145
 
        tree = self._make_tree_and_add([a])
146
 
        self.run_bzr("commit -m 'added a'")
147
 
        self.run_bzr('remove a', error_regexes=["deleted a"])
148
 
        self.assertFilesDeleted([a])
149
 
 
150
 
    def test_remove_keep_one_file(self):
151
 
        tree = self._make_tree_and_add([a])
152
 
        self.run_bzr('remove --keep a', error_regexes=["removed a"])
153
 
        self.assertFilesUnversioned([a])
154
 
 
155
 
    def test_remove_one_deleted_file(self):
156
 
        tree = self._make_tree_and_add([a])
157
 
        self.run_bzr("commit -m 'added a'")
158
 
        os.unlink(a)
159
 
        self.assertInWorkingTree(a)
160
 
        self.run_bzr('remove a')
161
 
        self.assertNotInWorkingTree(a)
162
 
 
163
 
    def test_remove_invalid_files(self):
164
 
        self.build_tree(files)
165
 
        tree = self.make_branch_and_tree('.')
166
 
        self.run_bzr(['remove', '.', 'xyz', 'abc/def'])
167
 
 
168
 
    def test_remove_unversioned_files(self):
169
 
        self.build_tree(files)
170
 
        tree = self.make_branch_and_tree('.')
171
 
        self.run_bzr_remove_changed_files(files)
172
 
 
173
 
    def test_remove_changed_files(self):
174
 
        tree = self._make_tree_and_add(files)
175
 
        self.run_bzr("commit -m 'added files'")
176
 
        self.changeFile(a)
177
 
        self.changeFile(c)
178
 
        self.run_bzr_remove_changed_files(files)
179
 
 
180
 
    def test_remove_changed_ignored_files(self):
181
 
        tree = self._make_tree_and_add(['a'])
182
 
        self.run_bzr(['ignore', 'a'])
183
 
        self.run_bzr_remove_changed_files(['a'])
184
 
 
185
 
    def test_remove_changed_files_from_child_dir(self):
186
 
        if sys.platform == 'win32':
187
 
            raise TestSkipped("Windows unable to remove '.' directory")
188
 
        tree = self._make_tree_and_add(files)
189
 
        self.run_bzr("commit -m 'added files'")
190
 
        self.changeFile(a)
191
 
        self.changeFile(c)
192
 
        self.run_bzr_remove_changed_files(
193
 
            ['../a', 'c', '.', '../d'], working_dir='b')
194
 
        self.assertNotInWorkingTree(files)
195
 
        self.failIfExists(files)
196
 
 
197
 
    def test_remove_keep_unversioned_files(self):
198
 
        self.build_tree(files)
199
 
        tree = self.make_branch_and_tree('.')
200
 
        self.run_bzr('remove --keep a', error_regexes=["a is not versioned."])
201
 
        self.assertFilesUnversioned(files)
202
 
 
203
 
    def test_remove_force_unversioned_files(self):
204
 
        self.build_tree(files)
205
 
        tree = self.make_branch_and_tree('.')
206
 
        self.run_bzr(['remove', '--force'] + list(files),
207
 
                     error_regexes=["deleted a", "deleted b",
208
 
                                    "removed b/c", "deleted d"])
209
 
        self.assertFilesDeleted(files)
210
 
 
211
 
    def test_remove_deleted_files(self):
212
 
        tree = self._make_tree_and_add(files)
213
 
        self.run_bzr("commit -m 'added files'")
214
 
        my_files=[f for f in files]
215
 
        my_files.sort(reverse=True)
216
 
        for f in my_files:
217
 
            osutils.delete_any(f)
218
 
        self.assertInWorkingTree(files)
219
 
        self.failIfExists(files)
220
 
        self.run_bzr('remove ' + ' '.join(files))
221
 
        self.assertNotInWorkingTree(a)
222
 
        self.failIfExists(files)
223
 
 
224
 
    def test_remove_non_existing_files(self):
225
 
        tree = self._make_tree_and_add([])
226
 
        self.run_bzr(['remove', 'b'])
227
 
 
228
 
    def test_remove_keep_non_existing_files(self):
229
 
        tree = self._make_tree_and_add([])
230
 
        self.run_bzr('remove --keep b', error_regexes=["b is not versioned."])
231
 
 
232
 
    def test_remove_files(self):
233
 
        tree = self._make_tree_and_add(files)
234
 
        self.run_bzr("commit -m 'added files'")
235
 
        self.run_bzr('remove a b b/c d',
236
 
                     error_regexes=["deleted a", "deleted b", "deleted b/c",
237
 
                     "deleted d"])
238
 
        self.assertFilesDeleted(files)
239
 
 
240
 
    def test_remove_keep_files(self):
241
 
        tree = self._make_tree_and_add(files)
242
 
        self.run_bzr("commit -m 'added files'")
243
 
        self.run_bzr('remove --keep a b b/c d',
244
 
                     error_regexes=["removed a", "removed b", "removed b/c",
245
 
                     "removed d"])
246
 
        self.assertFilesUnversioned(files)
247
 
 
248
 
    def test_remove_with_new(self):
249
 
        tree = self._make_tree_and_add(files)
250
 
        self.run_bzr('remove --new --keep',
251
 
                     error_regexes=["removed a", "removed b", "removed b/c"])
252
 
        self.assertFilesUnversioned(files)
253
 
 
254
 
    def test_remove_with_new_in_dir1(self):
255
 
        tree = self._make_tree_and_add(files)
256
 
        self.run_bzr('remove --new --keep b b/c',
257
 
                     error_regexes=["removed b", "removed b/c"])
258
 
        tree = WorkingTree.open('.')
259
 
        self.assertInWorkingTree(a)
260
 
        self.assertEqual(tree.path2id(a), a + _id)
261
 
        self.assertFilesUnversioned([b,c])
262
 
 
263
 
    def test_remove_with_new_in_dir2(self):
264
 
        tree = self._make_tree_and_add(files)
265
 
        self.run_bzr('remove --new --keep .',
266
 
                     error_regexes=["removed a", "removed b", "removed b/c"])
267
 
        tree = WorkingTree.open('.')
268
 
        self.assertFilesUnversioned(files)