~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: John Arbash Meinel
  • Date: 2013-05-19 14:29:37 UTC
  • mfrom: (6437.63.9 2.5)
  • mto: (6437.63.10 2.5)
  • mto: This revision was merged to the branch mainline in revision 6575.
  • Revision ID: john@arbash-meinel.com-20130519142937-21ykz2n2y2f22za9
Merge in the actual 2.5 branch. It seems I failed before

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 Canonical Ltd
 
1
# Copyright (C) 2006-2010 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
 
18
18
import os
 
19
import sys
19
20
 
20
 
from bzrlib.tests.blackbox import ExternalBase
 
21
from bzrlib.tests import (
 
22
    script,
 
23
    features,
 
24
    TestCaseWithTransport,
 
25
    TestSkipped,
 
26
    )
21
27
from bzrlib.workingtree import WorkingTree
22
 
 
23
 
 
24
 
class TestRemove(ExternalBase):
25
 
 
26
 
    def test_remove_deleted(self):
27
 
        self.runbzr("init")
28
 
        self.build_tree(['a'])
29
 
        self.runbzr(['add', 'a'])
30
 
        self.runbzr(['commit', '-m', 'added a'])
31
 
        os.unlink('a')
32
 
        self.runbzr(['remove', 'a'])
33
 
 
34
 
    def test_remove_new(self):
35
 
        self.build_tree(['filefile',
36
 
                         'dir/',
37
 
                         'dir/filefilefile'])
38
 
        wt = self.make_branch_and_tree('.')
39
 
        wt.add(['filefile', 'dir', 'dir/filefilefile'], 
40
 
               ['filefile-id', 'dir-id', 'filefilefile-id'])
41
 
        self.assertEqual(wt.path2id('filefile'), 'filefile-id')
42
 
        self.assertEqual(wt.path2id('dir/filefilefile'), 'filefilefile-id')
43
 
        self.assertEqual(wt.path2id('dir'), 'dir-id')
44
 
        self.runbzr('remove --new')
45
 
        wt = WorkingTree.open('.')
46
 
        self.assertIs(wt.path2id('filefile'), None)
47
 
        self.assertIs(wt.path2id('dir/filefilefile'), None)
48
 
        self.assertIs(wt.path2id('dir'), None)
49
 
        wt.add(['filefile', 'dir', 'dir/filefilefile'], 
50
 
               ['filefile-id', 'dir-id', 'filefilefile-id'])
51
 
        self.assertEqual(wt.path2id('filefile'), 'filefile-id')
52
 
        self.assertEqual(wt.path2id('dir/filefilefile'), 'filefilefile-id')
53
 
        self.assertEqual(wt.path2id('dir'), 'dir-id')
54
 
        self.runbzr('remove --new dir')
55
 
        wt = WorkingTree.open('.')
56
 
        self.assertEqual(wt.path2id('filefile'), 'filefile-id')
57
 
        self.assertIs(wt.path2id('dir/filefilefile'), None)
58
 
        self.assertIs(wt.path2id('dir'), None)
59
 
        self.runbzr('remove --new .')
60
 
        wt = WorkingTree.open('.')
61
 
        self.assertIs(wt.path2id('filefile'), None)
62
 
        self.runbzr('remove --new .', retcode=3)
 
28
from bzrlib import osutils
 
29
 
 
30
_id='-id'
 
31
a='a'
 
32
b='b/'
 
33
c='b/c'
 
34
d='d/'
 
35
files=(a, b, c, d)
 
36
 
 
37
 
 
38
class TestRemove(TestCaseWithTransport):
 
39
 
 
40
    def _make_tree_and_add(self, paths):
 
41
        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()
 
50
        return tree
 
51
 
 
52
    def assertFilesDeleted(self, files):
 
53
        for f in files:
 
54
            id=f+_id
 
55
            self.assertNotInWorkingTree(f)
 
56
            self.assertPathDoesNotExist(f)
 
57
 
 
58
    def assertFilesUnversioned(self, files):
 
59
        for f in files:
 
60
            self.assertNotInWorkingTree(f)
 
61
            self.assertPathExists(f)
 
62
 
 
63
    def changeFile(self, file_name):
 
64
        f = file(file_name, 'ab')
 
65
        f.write("\nsome other new content!")
 
66
        f.close()
 
67
 
 
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('.')
 
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
 
 
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)
 
141
 
 
142
    def test_rm_one_file(self):
 
143
        tree = self._make_tree_and_add([a])
 
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):
 
149
        tree = self._make_tree_and_add([a])
 
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):
 
155
        tree = self._make_tree_and_add([a])
 
156
        self.run_bzr('remove --keep a', error_regexes=["removed a"])
 
157
        self.assertFilesUnversioned([a])
 
158
 
 
159
    def test_remove_one_deleted_file(self):
 
160
        tree = self._make_tree_and_add([a])
 
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
 
 
167
    def test_remove_invalid_files(self):
 
168
        self.build_tree(files)
 
169
        tree = self.make_branch_and_tree('.')
 
170
        self.run_bzr(['remove', '.', 'xyz', 'abc/def'])
 
171
 
 
172
    def test_remove_unversioned_files(self):
 
173
        self.build_tree(files)
 
174
        tree = self.make_branch_and_tree('.')
 
175
        self.run_bzr_remove_changed_files(files)
 
176
 
 
177
    def test_remove_changed_files(self):
 
178
        tree = self._make_tree_and_add(files)
 
179
        self.run_bzr("commit -m 'added files'")
 
180
        self.changeFile(a)
 
181
        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'])
 
188
 
 
189
    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)
 
193
        self.run_bzr("commit -m 'added files'")
 
194
        self.changeFile(a)
 
195
        self.changeFile(c)
 
196
        self.run_bzr_remove_changed_files(
 
197
            ['../a', 'c', '.', '../d'], working_dir='b')
 
198
        self.assertNotInWorkingTree(files)
 
199
        self.assertPathDoesNotExist(files)
 
200
 
 
201
    def test_remove_keep_unversioned_files(self):
 
202
        self.build_tree(files)
 
203
        tree = self.make_branch_and_tree('.')
 
204
        self.run_bzr('remove --keep a', error_regexes=["a is not versioned."])
 
205
        self.assertFilesUnversioned(files)
 
206
 
 
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
    def test_remove_force_unversioned_files(self):
 
220
        self.build_tree(files)
 
221
        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
        ''')
 
230
        self.assertFilesDeleted(files)
 
231
 
 
232
    def test_remove_deleted_files(self):
 
233
        tree = self._make_tree_and_add(files)
 
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)
 
240
        self.assertPathDoesNotExist(files)
 
241
        self.run_bzr('remove ' + ' '.join(files))
 
242
        self.assertNotInWorkingTree(a)
 
243
        self.assertPathDoesNotExist(files)
 
244
 
 
245
    def test_remove_non_existing_files(self):
 
246
        tree = self._make_tree_and_add([])
 
247
        self.run_bzr(['remove', 'b'])
 
248
 
 
249
    def test_remove_keep_non_existing_files(self):
 
250
        tree = self._make_tree_and_add([])
 
251
        self.run_bzr('remove --keep b', error_regexes=["b is not versioned."])
 
252
 
 
253
    def test_remove_files(self):
 
254
        tree = self._make_tree_and_add(files)
 
255
        self.run_bzr("commit -m 'added files'")
 
256
        self.run_bzr('remove a b b/c d',
 
257
                     error_regexes=["deleted a", "deleted b", "deleted b/c",
 
258
                     "deleted d"])
 
259
        self.assertFilesDeleted(files)
 
260
 
 
261
    def test_remove_keep_files(self):
 
262
        tree = self._make_tree_and_add(files)
 
263
        self.run_bzr("commit -m 'added files'")
 
264
        self.run_bzr('remove --keep a b b/c d',
 
265
                     error_regexes=["removed a", "removed b", "removed b/c",
 
266
                     "removed d"])
 
267
        self.assertFilesUnversioned(files)
 
268
 
 
269
    def test_remove_with_new(self):
 
270
        tree = self._make_tree_and_add(files)
 
271
        self.run_bzr('remove --new --keep',
 
272
                     error_regexes=["removed a", "removed b", "removed b/c"])
 
273
        self.assertFilesUnversioned(files)
 
274
 
 
275
    def test_remove_with_new_in_dir1(self):
 
276
        tree = self._make_tree_and_add(files)
 
277
        self.run_bzr('remove --new --keep b b/c',
 
278
                     error_regexes=["removed b", "removed b/c"])
 
279
        tree = WorkingTree.open('.')
 
280
        self.assertInWorkingTree(a)
 
281
        self.assertEqual(tree.path2id(a), a + _id)
 
282
        self.assertFilesUnversioned([b,c])
 
283
 
 
284
    def test_remove_with_new_in_dir2(self):
 
285
        tree = self._make_tree_and_add(files)
 
286
        self.run_bzr('remove --new --keep .',
 
287
                     error_regexes=["removed a", "removed b", "removed b/c"])
 
288
        tree = WorkingTree.open('.')
 
289
        self.assertFilesUnversioned(files)