~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Alexander Belchenko
  • Date: 2008-02-07 07:53:19 UTC
  • mto: This revision was merged to the branch mainline in revision 3231.
  • Revision ID: bialix@ukr.net-20080207075319-d9jj7as8i5ztiff7
some cleanup in package_mf.py; mention about load_from_zip() deprecation in NEWS.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2005, 2006 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
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
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
 
18
18
import os
19
19
import sys
20
20
 
21
 
from bzrlib.tests import (
22
 
    script,
23
 
    features,
24
 
    TestCaseWithTransport,
25
 
    TestSkipped,
26
 
    )
 
21
from bzrlib.tests import TestSkipped
 
22
from bzrlib.tests.blackbox import ExternalBase
27
23
from bzrlib.workingtree import WorkingTree
28
24
from bzrlib import osutils
29
25
 
35
31
files=(a, b, c, d)
36
32
 
37
33
 
38
 
class TestRemove(TestCaseWithTransport):
 
34
class TestRemove(ExternalBase):
39
35
 
40
 
    def _make_tree_and_add(self, paths):
 
36
    def _make_add_and_assert_tree(self, files):
41
37
        tree = self.make_branch_and_tree('.')
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()
 
38
        self.build_tree(files)
 
39
        for f in files:
 
40
            id=str(f).replace('/', '_') + _id
 
41
            tree.add(f, id)
 
42
            self.assertEqual(tree.path2id(f), id)
 
43
            self.failUnlessExists(f)
 
44
            self.assertInWorkingTree(f)
50
45
        return tree
51
46
 
52
47
    def assertFilesDeleted(self, files):
53
48
        for f in files:
54
49
            id=f+_id
55
50
            self.assertNotInWorkingTree(f)
56
 
            self.assertPathDoesNotExist(f)
 
51
            self.failIfExists(f)
57
52
 
58
53
    def assertFilesUnversioned(self, files):
59
54
        for f in files:
60
55
            self.assertNotInWorkingTree(f)
61
 
            self.assertPathExists(f)
 
56
            self.failUnlessExists(f)
62
57
 
63
58
    def changeFile(self, file_name):
64
59
        f = file(file_name, 'ab')
65
60
        f.write("\nsome other new content!")
66
61
        f.close()
67
62
 
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)
71
 
 
72
 
    def test_remove_new_no_files_specified(self):
73
 
        tree = self.make_branch_and_tree('.')
 
63
    def run_bzr_remove_changed_files(self, error_regexes, files_to_remove):
 
64
        error_regexes.extend(["Can't safely remove modified or unknown files:",
 
65
            'Use --keep to not delete them,'
 
66
            ' or --force to delete them regardless.'
 
67
            ])
 
68
        self.run_bzr_error(error_regexes,
 
69
            ['remove'] + list(files_to_remove))
 
70
        #see if we can force it now
 
71
        self.run_bzr(['remove', '--force'] + list(files_to_remove))
 
72
 
 
73
    def test_remove_no_files_specified(self):
 
74
        tree = self._make_add_and_assert_tree([])
 
75
        self.run_bzr_error(["bzr: ERROR: Specify one or more files to remove, "
 
76
            "or use --new."], 'remove')
 
77
 
74
78
        self.run_bzr_error(["bzr: ERROR: No matching files."], 'remove --new')
75
 
        self.run_bzr_error(["bzr: ERROR: No matching files."], 'remove --new .')
76
 
 
77
 
    def test_remove_no_files_specified(self):
78
 
        tree = self._make_tree_and_add(['foo'])
79
 
        out, err = self.run_bzr(['rm'])
80
 
        self.assertEqual('', err)
81
 
        self.assertEqual('', out)
82
 
        self.assertInWorkingTree('foo', tree=tree)
83
 
        self.assertPathExists('foo')
84
 
 
85
 
    def test_remove_no_files_specified_missing_dir_and_contents(self):
86
 
        tree = self._make_tree_and_add(
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)
97
 
        self.assertPathExists('foo')
98
 
        self.assertInWorkingTree('dir', tree=tree)
99
 
        self.assertPathExists('dir')
100
 
        # missing files unversioned
101
 
        self.assertNotInWorkingTree('dir/missing', tree=tree)
102
 
        self.assertNotInWorkingTree('dir/missing/child', tree=tree)
103
 
 
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
 
 
115
 
    def test_remove_no_files_specified_missing_file(self):
116
 
        tree = self._make_tree_and_add(['foo', 'bar'])
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)
123
 
        self.assertPathExists('foo')
124
 
        # missing files unversioned
125
 
        self.assertNotInWorkingTree('bar', tree=tree)
126
 
 
127
 
    def test_remove_no_files_specified_missing_link(self):
128
 
        self.requireFeature(features.SymlinkFeature)
129
 
        tree = self._make_tree_and_add(['foo'])
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)
138
 
        self.assertPathExists('foo')
139
 
        # missing files unversioned
140
 
        self.assertNotInWorkingTree('linkname', tree=tree)
 
79
 
 
80
        self.run_bzr_error(["bzr: ERROR: No matching files."],
 
81
            'remove --new .')
141
82
 
142
83
    def test_rm_one_file(self):
143
 
        tree = self._make_tree_and_add([a])
 
84
        tree = self._make_add_and_assert_tree([a])
144
85
        self.run_bzr("commit -m 'added a'")
145
86
        self.run_bzr('rm a', error_regexes=["deleted a"])
146
87
        self.assertFilesDeleted([a])
147
88
 
148
89
    def test_remove_one_file(self):
149
 
        tree = self._make_tree_and_add([a])
 
90
        tree = self._make_add_and_assert_tree([a])
150
91
        self.run_bzr("commit -m 'added a'")
151
92
        self.run_bzr('remove a', error_regexes=["deleted a"])
152
93
        self.assertFilesDeleted([a])
153
94
 
154
95
    def test_remove_keep_one_file(self):
155
 
        tree = self._make_tree_and_add([a])
 
96
        tree = self._make_add_and_assert_tree([a])
156
97
        self.run_bzr('remove --keep a', error_regexes=["removed a"])
157
98
        self.assertFilesUnversioned([a])
158
99
 
159
100
    def test_remove_one_deleted_file(self):
160
 
        tree = self._make_tree_and_add([a])
 
101
        tree = self._make_add_and_assert_tree([a])
161
102
        self.run_bzr("commit -m 'added a'")
162
103
        os.unlink(a)
163
104
        self.assertInWorkingTree(a)
172
113
    def test_remove_unversioned_files(self):
173
114
        self.build_tree(files)
174
115
        tree = self.make_branch_and_tree('.')
175
 
        self.run_bzr_remove_changed_files(files)
 
116
        self.run_bzr_remove_changed_files(
 
117
            ['unknown:[.\s]*d/[.\s]*b/c[.\s]*b/[.\s]*a'], files)
176
118
 
177
119
    def test_remove_changed_files(self):
178
 
        tree = self._make_tree_and_add(files)
 
120
        tree = self._make_add_and_assert_tree(files)
179
121
        self.run_bzr("commit -m 'added files'")
180
122
        self.changeFile(a)
181
123
        self.changeFile(c)
182
 
        self.run_bzr_remove_changed_files(files)
 
124
        self.run_bzr_remove_changed_files(['modified:[.\s]*a[.\s]*b/c'], files)
183
125
 
184
126
    def test_remove_changed_ignored_files(self):
185
 
        tree = self._make_tree_and_add(['a'])
 
127
        tree = self._make_add_and_assert_tree(['a'])
186
128
        self.run_bzr(['ignore', 'a'])
187
 
        self.run_bzr_remove_changed_files(['a'])
 
129
        self.run_bzr_remove_changed_files(['added:[.\s]*a'], ['a'])
188
130
 
189
131
    def test_remove_changed_files_from_child_dir(self):
190
132
        if sys.platform == 'win32':
191
133
            raise TestSkipped("Windows unable to remove '.' directory")
192
 
        tree = self._make_tree_and_add(files)
 
134
        tree = self._make_add_and_assert_tree(files)
193
135
        self.run_bzr("commit -m 'added files'")
194
136
        self.changeFile(a)
195
137
        self.changeFile(c)
196
 
        self.run_bzr_remove_changed_files(
197
 
            ['../a', 'c', '.', '../d'], working_dir='b')
 
138
        os.chdir('b')
 
139
        self.run_bzr_remove_changed_files(['modified:[.\s]*a[.\s]*b/c'],
 
140
            ['../a', 'c', '.', '../d'])
 
141
        os.chdir('..')
198
142
        self.assertNotInWorkingTree(files)
199
 
        self.assertPathDoesNotExist(files)
 
143
        self.failIfExists(files)
200
144
 
201
145
    def test_remove_keep_unversioned_files(self):
202
146
        self.build_tree(files)
204
148
        self.run_bzr('remove --keep a', error_regexes=["a is not versioned."])
205
149
        self.assertFilesUnversioned(files)
206
150
 
207
 
    def test_remove_no_backup_unversioned_files(self):
 
151
    def test_remove_force_unversioned_files(self):
208
152
        self.build_tree(files)
209
153
        tree = self.make_branch_and_tree('.')
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
 
        ''')
 
154
        self.run_bzr(['remove', '--force'] + list(files),
 
155
                     error_regexes=["deleted a", "deleted b",
 
156
                                    "deleted b/c", "deleted d"])
217
157
        self.assertFilesDeleted(files)
218
158
 
219
159
    def test_remove_deleted_files(self):
220
 
        tree = self._make_tree_and_add(files)
 
160
        tree = self._make_add_and_assert_tree(files)
221
161
        self.run_bzr("commit -m 'added files'")
222
162
        my_files=[f for f in files]
223
163
        my_files.sort(reverse=True)
224
164
        for f in my_files:
225
165
            osutils.delete_any(f)
226
166
        self.assertInWorkingTree(files)
227
 
        self.assertPathDoesNotExist(files)
 
167
        self.failIfExists(files)
228
168
        self.run_bzr('remove ' + ' '.join(files))
229
169
        self.assertNotInWorkingTree(a)
230
 
        self.assertPathDoesNotExist(files)
 
170
        self.failIfExists(files)
231
171
 
232
172
    def test_remove_non_existing_files(self):
233
 
        tree = self._make_tree_and_add([])
 
173
        tree = self._make_add_and_assert_tree([])
234
174
        self.run_bzr(['remove', 'b'])
235
175
 
236
176
    def test_remove_keep_non_existing_files(self):
237
 
        tree = self._make_tree_and_add([])
 
177
        tree = self._make_add_and_assert_tree([])
238
178
        self.run_bzr('remove --keep b', error_regexes=["b is not versioned."])
239
179
 
240
180
    def test_remove_files(self):
241
 
        tree = self._make_tree_and_add(files)
 
181
        tree = self._make_add_and_assert_tree(files)
242
182
        self.run_bzr("commit -m 'added files'")
243
183
        self.run_bzr('remove a b b/c d',
244
184
                     error_regexes=["deleted a", "deleted b", "deleted b/c",
246
186
        self.assertFilesDeleted(files)
247
187
 
248
188
    def test_remove_keep_files(self):
249
 
        tree = self._make_tree_and_add(files)
 
189
        tree = self._make_add_and_assert_tree(files)
250
190
        self.run_bzr("commit -m 'added files'")
251
191
        self.run_bzr('remove --keep a b b/c d',
252
192
                     error_regexes=["removed a", "removed b", "removed b/c",
254
194
        self.assertFilesUnversioned(files)
255
195
 
256
196
    def test_remove_with_new(self):
257
 
        tree = self._make_tree_and_add(files)
 
197
        tree = self._make_add_and_assert_tree(files)
258
198
        self.run_bzr('remove --new --keep',
259
199
                     error_regexes=["removed a", "removed b", "removed b/c"])
260
200
        self.assertFilesUnversioned(files)
261
201
 
262
202
    def test_remove_with_new_in_dir1(self):
263
 
        tree = self._make_tree_and_add(files)
 
203
        tree = self._make_add_and_assert_tree(files)
264
204
        self.run_bzr('remove --new --keep b b/c',
265
205
                     error_regexes=["removed b", "removed b/c"])
266
206
        tree = WorkingTree.open('.')
269
209
        self.assertFilesUnversioned([b,c])
270
210
 
271
211
    def test_remove_with_new_in_dir2(self):
272
 
        tree = self._make_tree_and_add(files)
 
212
        tree = self._make_add_and_assert_tree(files)
273
213
        self.run_bzr('remove --new --keep .',
274
214
                     error_regexes=["removed a", "removed b", "removed b/c"])
275
215
        tree = WorkingTree.open('.')