~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Ian Clatworthy
  • Date: 2007-08-13 14:33:10 UTC
  • mto: (2733.1.1 ianc-integration)
  • mto: This revision was merged to the branch mainline in revision 2734.
  • Revision ID: ian.clatworthy@internode.on.net-20070813143310-twhj4la0qnupvze8
Added Quick Start Summary

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