~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Aaron Bentley
  • Date: 2005-07-29 17:19:16 UTC
  • mto: (1092.1.41) (1185.3.4) (974.1.47)
  • mto: This revision was merged to the branch mainline in revision 1020.
  • Revision ID: abentley@panoramicfeedback.com-20050729171916-322fd81b451d2e3e
Added merge-type parameter to merge.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
 
 
17
 
 
18
 
import os
19
 
import sys
20
 
 
21
 
from bzrlib.tests import SymlinkFeature, TestSkipped
22
 
from bzrlib.tests.blackbox import ExternalBase
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(ExternalBase):
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, error_regexes, files_to_remove):
65
 
        error_regexes.extend(["Can't safely remove modified or unknown files:",
66
 
            'Use --keep to not delete them,'
67
 
            ' or --force to delete them regardless.'
68
 
            ])
69
 
        self.run_bzr_error(error_regexes,
70
 
            ['remove'] + list(files_to_remove))
71
 
        #see if we can force it now
72
 
        self.run_bzr(['remove', '--force'] + list(files_to_remove))
73
 
 
74
 
    def test_remove_new_no_files_specified(self):
75
 
        tree = self.make_branch_and_tree('.')
76
 
        self.run_bzr_error(["bzr: ERROR: No matching files."], 'remove --new')
77
 
        self.run_bzr_error(["bzr: ERROR: No matching files."], 'remove --new .')
78
 
 
79
 
    def test_remove_no_files_specified(self):
80
 
        tree = self._make_tree_and_add(['foo'])
81
 
        out, err = self.run_bzr(['rm'])
82
 
        self.assertEqual('', err)
83
 
        self.assertEqual('', out)
84
 
        self.assertInWorkingTree('foo', tree=tree)
85
 
        self.failUnlessExists('foo')
86
 
 
87
 
    def test_remove_no_files_specified_missing_dir_and_contents(self):
88
 
        tree = self._make_tree_and_add(
89
 
            ['foo', 'dir/', 'dir/missing/', 'dir/missing/child'])
90
 
        self.get_transport('.').delete_tree('dir/missing')
91
 
        out, err = self.run_bzr(['rm'])
92
 
        self.assertEqual('', out)
93
 
        self.assertEqual(
94
 
            'removed dir/missing/child\n'
95
 
            'removed dir/missing\n',
96
 
            err)
97
 
        # non-missing paths not touched:
98
 
        self.assertInWorkingTree('foo', tree=tree)
99
 
        self.failUnlessExists('foo')
100
 
        self.assertInWorkingTree('dir', tree=tree)
101
 
        self.failUnlessExists('dir')
102
 
        # missing files unversioned
103
 
        self.assertNotInWorkingTree('dir/missing', tree=tree)
104
 
        self.assertNotInWorkingTree('dir/missing/child', tree=tree)
105
 
 
106
 
    def test_remove_no_files_specified_already_deleted(self):
107
 
        tree = self._make_tree_and_add(['foo', 'bar'])
108
 
        tree.commit('save foo and bar')
109
 
        os.unlink('bar')
110
 
        self.run_bzr(['rm'])
111
 
        self.assertEqual(None, tree.path2id('bar'))
112
 
        # Running rm with a deleted file does not error.
113
 
        out, err = self.run_bzr(['rm'])
114
 
        self.assertEqual('', out)
115
 
        self.assertEqual('', err)
116
 
 
117
 
    def test_remove_no_files_specified_missing_file(self):
118
 
        tree = self._make_tree_and_add(['foo', 'bar'])
119
 
        os.unlink('bar')
120
 
        out, err = self.run_bzr(['rm'])
121
 
        self.assertEqual('', out)
122
 
        self.assertEqual('removed bar\n', err)
123
 
        # non-missing files not touched:
124
 
        self.assertInWorkingTree('foo', tree=tree)
125
 
        self.failUnlessExists('foo')
126
 
        # missing files unversioned
127
 
        self.assertNotInWorkingTree('bar', tree=tree)
128
 
 
129
 
    def test_remove_no_files_specified_missing_link(self):
130
 
        self.requireFeature(SymlinkFeature)
131
 
        tree = self._make_tree_and_add(['foo'])
132
 
        os.symlink('foo', 'linkname')
133
 
        tree.add(['linkname'])
134
 
        os.unlink('linkname')
135
 
        out, err = self.run_bzr(['rm'])
136
 
        self.assertEqual('', out)
137
 
        self.assertEqual('removed linkname\n', err)
138
 
        # non-missing files not touched:
139
 
        self.assertInWorkingTree('foo', tree=tree)
140
 
        self.failUnlessExists('foo')
141
 
        # missing files unversioned
142
 
        self.assertNotInWorkingTree('linkname', tree=tree)
143
 
 
144
 
    def test_rm_one_file(self):
145
 
        tree = self._make_tree_and_add([a])
146
 
        self.run_bzr("commit -m 'added a'")
147
 
        self.run_bzr('rm a', error_regexes=["deleted a"])
148
 
        self.assertFilesDeleted([a])
149
 
 
150
 
    def test_remove_one_file(self):
151
 
        tree = self._make_tree_and_add([a])
152
 
        self.run_bzr("commit -m 'added a'")
153
 
        self.run_bzr('remove a', error_regexes=["deleted a"])
154
 
        self.assertFilesDeleted([a])
155
 
 
156
 
    def test_remove_keep_one_file(self):
157
 
        tree = self._make_tree_and_add([a])
158
 
        self.run_bzr('remove --keep a', error_regexes=["removed a"])
159
 
        self.assertFilesUnversioned([a])
160
 
 
161
 
    def test_remove_one_deleted_file(self):
162
 
        tree = self._make_tree_and_add([a])
163
 
        self.run_bzr("commit -m 'added a'")
164
 
        os.unlink(a)
165
 
        self.assertInWorkingTree(a)
166
 
        self.run_bzr('remove a')
167
 
        self.assertNotInWorkingTree(a)
168
 
 
169
 
    def test_remove_invalid_files(self):
170
 
        self.build_tree(files)
171
 
        tree = self.make_branch_and_tree('.')
172
 
        self.run_bzr(['remove', '.', 'xyz', 'abc/def'])
173
 
 
174
 
    def test_remove_unversioned_files(self):
175
 
        self.build_tree(files)
176
 
        tree = self.make_branch_and_tree('.')
177
 
        self.run_bzr_remove_changed_files(
178
 
            ['unknown:[.\s]*d/[.\s]*b/c[.\s]*b/[.\s]*a'], files)
179
 
 
180
 
    def test_remove_changed_files(self):
181
 
        tree = self._make_tree_and_add(files)
182
 
        self.run_bzr("commit -m 'added files'")
183
 
        self.changeFile(a)
184
 
        self.changeFile(c)
185
 
        self.run_bzr_remove_changed_files(['modified:[.\s]*a[.\s]*b/c'], files)
186
 
 
187
 
    def test_remove_changed_ignored_files(self):
188
 
        tree = self._make_tree_and_add(['a'])
189
 
        self.run_bzr(['ignore', 'a'])
190
 
        self.run_bzr_remove_changed_files(['added:[.\s]*a'], ['a'])
191
 
 
192
 
    def test_remove_changed_files_from_child_dir(self):
193
 
        if sys.platform == 'win32':
194
 
            raise TestSkipped("Windows unable to remove '.' directory")
195
 
        tree = self._make_tree_and_add(files)
196
 
        self.run_bzr("commit -m 'added files'")
197
 
        self.changeFile(a)
198
 
        self.changeFile(c)
199
 
        os.chdir('b')
200
 
        self.run_bzr_remove_changed_files(['modified:[.\s]*a[.\s]*b/c'],
201
 
            ['../a', 'c', '.', '../d'])
202
 
        os.chdir('..')
203
 
        self.assertNotInWorkingTree(files)
204
 
        self.failIfExists(files)
205
 
 
206
 
    def test_remove_keep_unversioned_files(self):
207
 
        self.build_tree(files)
208
 
        tree = self.make_branch_and_tree('.')
209
 
        self.run_bzr('remove --keep a', error_regexes=["a is not versioned."])
210
 
        self.assertFilesUnversioned(files)
211
 
 
212
 
    def test_remove_force_unversioned_files(self):
213
 
        self.build_tree(files)
214
 
        tree = self.make_branch_and_tree('.')
215
 
        self.run_bzr(['remove', '--force'] + list(files),
216
 
                     error_regexes=["deleted a", "deleted b",
217
 
                                    "deleted b/c", "deleted d"])
218
 
        self.assertFilesDeleted(files)
219
 
 
220
 
    def test_remove_deleted_files(self):
221
 
        tree = self._make_tree_and_add(files)
222
 
        self.run_bzr("commit -m 'added files'")
223
 
        my_files=[f for f in files]
224
 
        my_files.sort(reverse=True)
225
 
        for f in my_files:
226
 
            osutils.delete_any(f)
227
 
        self.assertInWorkingTree(files)
228
 
        self.failIfExists(files)
229
 
        self.run_bzr('remove ' + ' '.join(files))
230
 
        self.assertNotInWorkingTree(a)
231
 
        self.failIfExists(files)
232
 
 
233
 
    def test_remove_non_existing_files(self):
234
 
        tree = self._make_tree_and_add([])
235
 
        self.run_bzr(['remove', 'b'])
236
 
 
237
 
    def test_remove_keep_non_existing_files(self):
238
 
        tree = self._make_tree_and_add([])
239
 
        self.run_bzr('remove --keep b', error_regexes=["b is not versioned."])
240
 
 
241
 
    def test_remove_files(self):
242
 
        tree = self._make_tree_and_add(files)
243
 
        self.run_bzr("commit -m 'added files'")
244
 
        self.run_bzr('remove a b b/c d',
245
 
                     error_regexes=["deleted a", "deleted b", "deleted b/c",
246
 
                     "deleted d"])
247
 
        self.assertFilesDeleted(files)
248
 
 
249
 
    def test_remove_keep_files(self):
250
 
        tree = self._make_tree_and_add(files)
251
 
        self.run_bzr("commit -m 'added files'")
252
 
        self.run_bzr('remove --keep a b b/c d',
253
 
                     error_regexes=["removed a", "removed b", "removed b/c",
254
 
                     "removed d"])
255
 
        self.assertFilesUnversioned(files)
256
 
 
257
 
    def test_remove_with_new(self):
258
 
        tree = self._make_tree_and_add(files)
259
 
        self.run_bzr('remove --new --keep',
260
 
                     error_regexes=["removed a", "removed b", "removed b/c"])
261
 
        self.assertFilesUnversioned(files)
262
 
 
263
 
    def test_remove_with_new_in_dir1(self):
264
 
        tree = self._make_tree_and_add(files)
265
 
        self.run_bzr('remove --new --keep b b/c',
266
 
                     error_regexes=["removed b", "removed b/c"])
267
 
        tree = WorkingTree.open('.')
268
 
        self.assertInWorkingTree(a)
269
 
        self.assertEqual(tree.path2id(a), a + _id)
270
 
        self.assertFilesUnversioned([b,c])
271
 
 
272
 
    def test_remove_with_new_in_dir2(self):
273
 
        tree = self._make_tree_and_add(files)
274
 
        self.run_bzr('remove --new --keep .',
275
 
                     error_regexes=["removed a", "removed b", "removed b/c"])
276
 
        tree = WorkingTree.open('.')
277
 
        self.assertFilesUnversioned(files)