~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: 2007-06-20 22:06:22 UTC
  • mto: (2520.5.2 bzr.mpbundle)
  • mto: This revision was merged to the branch mainline in revision 2631.
  • Revision ID: abentley@panoramicfeedback.com-20070620220622-9lasxr96rr0e0xvn
Use a fresh versionedfile each time

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, re, shlex
 
19
 
 
20
from bzrlib.tests.blackbox import ExternalBase
 
21
from bzrlib.workingtree import WorkingTree
 
22
from bzrlib import osutils
 
23
 
 
24
_id='-id'
 
25
a='a'
 
26
b='b/'
 
27
c='b/c'
 
28
d='d/'
 
29
files=(a, b, c, d)
 
30
 
 
31
 
 
32
class TestRemove(ExternalBase):
 
33
 
 
34
    def run_bzr_captured(self, argv, retcode=0, encoding=None, stdin=None,
 
35
                         working_dir=None):
 
36
 
 
37
        # magically convert commands like 'remove abc' to ['remove', 'abc']
 
38
        if (isinstance(argv, tuple) and len(argv) == 1 and
 
39
            isinstance(argv[0], basestring)):
 
40
            argv = shlex.split(argv[0])
 
41
        return ExternalBase.run_bzr_captured(self, argv, retcode, encoding,
 
42
            stdin, working_dir)
 
43
 
 
44
    def _make_add_and_assert_tree(self, files):
 
45
        tree = self.make_branch_and_tree('.')
 
46
        self.build_tree(files)
 
47
        for f in files:
 
48
            id=str(f).replace('/', '_') + _id
 
49
            tree.add(f, id)
 
50
            self.assertEqual(tree.path2id(f), id)
 
51
            self.failUnlessExists(f)
 
52
            self.assertInWorkingTree(f)
 
53
        return tree
 
54
 
 
55
    def assertFilesDeleted(self, files):
 
56
        for f in files:
 
57
            id=f+_id
 
58
            self.assertNotInWorkingTree(f)
 
59
            self.failIfExists(f)
 
60
 
 
61
    def assertFilesUnversioned(self, files):
 
62
        for f in files:
 
63
            self.assertNotInWorkingTree(f)
 
64
            self.failUnlessExists(f)
 
65
 
 
66
    def changeFile(self, file_name):
 
67
        f = file(file_name, 'ab')
 
68
        f.write("\nsome other new content!")
 
69
        f.close()
 
70
 
 
71
    def run_bzr_remove_changed_files(self, error_regexes, files_to_remove):
 
72
        error_regexes.extend(["Can't remove changed or unknown files:",
 
73
            'Use --keep to not delete them,'
 
74
            ' or --force to delete them regardless.'
 
75
            ])
 
76
        self.run_bzr_error(error_regexes,
 
77
            'remove ' + ' '.join(files_to_remove))
 
78
        #see if we can force it now
 
79
        self.run_bzr('remove --force ' + ' '.join(files_to_remove))
 
80
 
 
81
    def test_remove_no_files_specified(self):
 
82
        tree = self._make_add_and_assert_tree([])
 
83
        self.run_bzr_error(["bzr: ERROR: Specify one or more files to remove, "
 
84
            "or use --new."], 'remove')
 
85
 
 
86
        self.run_bzr_error(["bzr: ERROR: No matching files."], 'remove --new')
 
87
 
 
88
        self.run_bzr_error(["bzr: ERROR: No matching files."],
 
89
            'remove --new .')
 
90
 
 
91
    def test_rm_one_file(self):
 
92
        tree = self._make_add_and_assert_tree([a])
 
93
        self.run_bzr("commit -m 'added a'")
 
94
        self.run_bzr('rm a', error_regexes=["deleted a"])
 
95
        self.assertFilesDeleted([a])
 
96
 
 
97
    def test_remove_one_file(self):
 
98
        tree = self._make_add_and_assert_tree([a])
 
99
        self.run_bzr("commit -m 'added a'")
 
100
        self.run_bzr('remove a', error_regexes=["deleted a"])
 
101
        self.assertFilesDeleted([a])
 
102
 
 
103
    def test_remove_keep_one_file(self):
 
104
        tree = self._make_add_and_assert_tree([a])
 
105
        self.run_bzr('remove --keep a', error_regexes=["removed a"])
 
106
        self.assertFilesUnversioned([a])
 
107
 
 
108
    def test_remove_one_deleted_file(self):
 
109
        tree = self._make_add_and_assert_tree([a])
 
110
        self.run_bzr("commit -m 'added a'")
 
111
        os.unlink(a)
 
112
        self.assertInWorkingTree(a)
 
113
        self.run_bzr('remove a')
 
114
        self.assertNotInWorkingTree(a)
 
115
 
 
116
    def test_remove_invalid_files(self):
 
117
        self.build_tree(files)
 
118
        tree = self.make_branch_and_tree('.')
 
119
        self.run_bzr_remove_changed_files(['unknown:[.\s]*xyz[.\s]*abc/def'],
 
120
            ['.', 'xyz', 'abc/def'])
 
121
 
 
122
    def test_remove_unversioned_files(self):
 
123
        self.build_tree(files)
 
124
        tree = self.make_branch_and_tree('.')
 
125
        self.run_bzr_remove_changed_files(
 
126
            ['unknown:[.\s]*d/[.\s]*b/c[.\s]*b/[.\s]*a'], files)
 
127
 
 
128
    def test_remove_changed_files(self):
 
129
        tree = self._make_add_and_assert_tree(files)
 
130
        self.run_bzr("commit -m 'added files'")
 
131
        self.changeFile(a)
 
132
        self.changeFile(c)
 
133
        self.run_bzr_remove_changed_files(['modified:[.\s]*a[.\s]*b/c'], files)
 
134
 
 
135
    def test_remove_changed_files_from_child_dir(self):
 
136
        tree = self._make_add_and_assert_tree(files)
 
137
        self.run_bzr("commit -m 'added files'")
 
138
        self.changeFile(a)
 
139
        self.changeFile(c)
 
140
        os.chdir('b')
 
141
        self.run_bzr_remove_changed_files(['modified:[.\s]*a[.\s]*b/c'],
 
142
            ['../a', 'c', '.', '../d'])
 
143
        os.chdir('..')
 
144
        self.assertNotInWorkingTree(files)
 
145
        self.failIfExists(files)
 
146
 
 
147
    def test_remove_keep_unversioned_files(self):
 
148
        self.build_tree(files)
 
149
        tree = self.make_branch_and_tree('.')
 
150
        self.run_bzr('remove --keep a', error_regexes=["a is not versioned."])
 
151
        self.assertFilesUnversioned(files)
 
152
 
 
153
    def test_remove_force_unversioned_files(self):
 
154
        self.build_tree(files)
 
155
        tree = self.make_branch_and_tree('.')
 
156
        self.run_bzr('remove --force ' + ' '.join(files),
 
157
                     error_regexes=["deleted a", "deleted b",
 
158
                                    "deleted b/c", "deleted d"])
 
159
        self.assertFilesDeleted(files)
 
160
 
 
161
    def test_remove_deleted_files(self):
 
162
        tree = self._make_add_and_assert_tree(files)
 
163
        self.run_bzr("commit -m 'added files'")
 
164
        my_files=[f for f in files]
 
165
        my_files.sort(reverse=True)
 
166
        for f in my_files:
 
167
            osutils.delete_any(f)
 
168
        self.assertInWorkingTree(files)
 
169
        self.failIfExists(files)
 
170
        self.run_bzr('remove ' + ' '.join(files))
 
171
        self.assertNotInWorkingTree(a)
 
172
        self.failIfExists(files)
 
173
 
 
174
    def test_remove_non_existing_files(self):
 
175
        tree = self._make_add_and_assert_tree([])
 
176
        self.run_bzr_remove_changed_files(['unknown:[.\s]*b'], ['b'])
 
177
 
 
178
    def test_remove_keep_non_existing_files(self):
 
179
        tree = self._make_add_and_assert_tree([])
 
180
        self.run_bzr('remove --keep b', error_regexes=["b is not versioned."])
 
181
 
 
182
    def test_remove_files(self):
 
183
        tree = self._make_add_and_assert_tree(files)
 
184
        self.run_bzr("commit -m 'added files'")
 
185
        self.run_bzr('remove a b b/c d',
 
186
                     error_regexes=["deleted a", "deleted b", "deleted b/c",
 
187
                     "deleted d"])
 
188
        self.assertFilesDeleted(files)
 
189
 
 
190
    def test_remove_keep_files(self):
 
191
        tree = self._make_add_and_assert_tree(files)
 
192
        self.run_bzr("commit -m 'added files'")
 
193
        self.run_bzr('remove --keep a b b/c d',
 
194
                     error_regexes=["removed a", "removed b", "removed b/c",
 
195
                     "removed d"])
 
196
        self.assertFilesUnversioned(files)
 
197
 
 
198
    def test_remove_with_new(self):
 
199
        tree = self._make_add_and_assert_tree(files)
 
200
        self.run_bzr('remove --new --keep',
 
201
                     error_regexes=["removed a", "removed b", "removed b/c"])
 
202
        self.assertFilesUnversioned(files)
 
203
 
 
204
    def test_remove_with_new_in_dir1(self):
 
205
        tree = self._make_add_and_assert_tree(files)
 
206
        self.run_bzr('remove --new --keep b b/c',
 
207
                     error_regexes=["removed b", "removed b/c"])
 
208
        tree = WorkingTree.open('.')
 
209
        self.assertInWorkingTree(a)
 
210
        self.assertEqual(tree.path2id(a), a + _id)
 
211
        self.assertFilesUnversioned([b,c])
 
212
 
 
213
    def test_remove_with_new_in_dir2(self):
 
214
        tree = self._make_add_and_assert_tree(files)
 
215
        self.run_bzr('remove --new --keep .',
 
216
                     error_regexes=["removed a", "removed b", "removed b/c"])
 
217
        tree = WorkingTree.open('.')
 
218
        self.assertFilesUnversioned(files)