~bzr-pqm/bzr/bzr.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# Copyright (C) 2005, 2006 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


import os, re, shlex

from bzrlib.tests.blackbox import ExternalBase
from bzrlib.workingtree import WorkingTree
from bzrlib import osutils

_id='-id'
a='a'
b='b/'
c='b/c'
d='d/'
files=(a, b, c, d)


class TestRemove(ExternalBase):

    def run_bzr_captured(self, argv, retcode=0, encoding=None, stdin=None,
                         working_dir=None):

        # magically convert commands like 'remove abc' to ['remove', 'abc']
        if (isinstance(argv, tuple) and len(argv) == 1 and
            isinstance(argv[0], basestring)):
            argv = shlex.split(argv[0])
        return ExternalBase.run_bzr_captured(self, argv, retcode, encoding,
            stdin, working_dir)

    def _make_add_and_assert_tree(self, files):
        tree = self.make_branch_and_tree('.')
        self.build_tree(files)
        for f in files:
            id=str(f).replace('/', '_') + _id
            tree.add(f, id)
            self.assertEqual(tree.path2id(f), id)
            self.failUnlessExists(f)
            self.assertInWorkingTree(f)
        return tree

    def assertFilesDeleted(self, files):
        for f in files:
            id=f+_id
            self.assertNotInWorkingTree(f)
            self.failIfExists(f)

    def assertFilesUnversioned(self, files):
        for f in files:
            self.assertNotInWorkingTree(f)
            self.failUnlessExists(f)

    def changeFile(self, file_name):
        f = file(file_name, 'ab')
        f.write("\nsome other new content!")
        f.close()

    def run_bzr_remove_changed_files(self, error_regexes, files_to_remove):
        error_regexes.extend(["Can't remove changed or unknown files:",
            'Use --keep to not delete them,'
            ' or --force to delete them regardless.'
            ])
        self.run_bzr_error(error_regexes,
            'remove ' + ' '.join(files_to_remove))
        #see if we can force it now
        self.run_bzr('remove --force ' + ' '.join(files_to_remove))

    def test_remove_no_files_specified(self):
        tree = self._make_add_and_assert_tree([])
        self.run_bzr_error(["bzr: ERROR: Specify one or more files to remove, "
            "or use --new."], 'remove')

        self.run_bzr_error(["bzr: ERROR: No matching files."], 'remove --new')

        self.run_bzr_error(["bzr: ERROR: No matching files."],
            'remove --new .')

    def test_rm_one_file(self):
        tree = self._make_add_and_assert_tree([a])
        self.run_bzr("commit -m 'added a'")
        self.run_bzr('rm a', error_regexes=["deleted a"])
        self.assertFilesDeleted([a])

    def test_remove_one_file(self):
        tree = self._make_add_and_assert_tree([a])
        self.run_bzr("commit -m 'added a'")
        self.run_bzr('remove a', error_regexes=["deleted a"])
        self.assertFilesDeleted([a])

    def test_remove_keep_one_file(self):
        tree = self._make_add_and_assert_tree([a])
        self.run_bzr('remove --keep a', error_regexes=["removed a"])
        self.assertFilesUnversioned([a])

    def test_remove_one_deleted_file(self):
        tree = self._make_add_and_assert_tree([a])
        self.run_bzr("commit -m 'added a'")
        os.unlink(a)
        self.assertInWorkingTree(a)
        self.run_bzr('remove a')
        self.assertNotInWorkingTree(a)

    def test_remove_invalid_files(self):
        self.build_tree(files)
        tree = self.make_branch_and_tree('.')
        self.run_bzr_remove_changed_files(['unknown:[.\s]*xyz[.\s]*abc/def'],
            ['.', 'xyz', 'abc/def'])

    def test_remove_unversioned_files(self):
        self.build_tree(files)
        tree = self.make_branch_and_tree('.')
        self.run_bzr_remove_changed_files(
            ['unknown:[.\s]*d/[.\s]*b/c[.\s]*b/[.\s]*a'], files)

    def test_remove_changed_files(self):
        tree = self._make_add_and_assert_tree(files)
        self.run_bzr("commit -m 'added files'")
        self.changeFile(a)
        self.changeFile(c)
        self.run_bzr_remove_changed_files(['modified:[.\s]*a[.\s]*b/c'], files)

    def test_remove_changed_files_from_child_dir(self):
        tree = self._make_add_and_assert_tree(files)
        self.run_bzr("commit -m 'added files'")
        self.changeFile(a)
        self.changeFile(c)
        os.chdir('b')
        self.run_bzr_remove_changed_files(['modified:[.\s]*a[.\s]*b/c'],
            ['../a', 'c', '.', '../d'])
        os.chdir('..')
        self.assertNotInWorkingTree(files)
        self.failIfExists(files)

    def test_remove_keep_unversioned_files(self):
        self.build_tree(files)
        tree = self.make_branch_and_tree('.')
        self.run_bzr('remove --keep a', error_regexes=["a is not versioned."])
        self.assertFilesUnversioned(files)

    def test_remove_force_unversioned_files(self):
        self.build_tree(files)
        tree = self.make_branch_and_tree('.')
        self.run_bzr('remove --force ' + ' '.join(files),
                     error_regexes=["deleted a", "deleted b",
                                    "deleted b/c", "deleted d"])
        self.assertFilesDeleted(files)

    def test_remove_deleted_files(self):
        tree = self._make_add_and_assert_tree(files)
        self.run_bzr("commit -m 'added files'")
        my_files=[f for f in files]
        my_files.sort(reverse=True)
        for f in my_files:
            osutils.delete_any(f)
        self.assertInWorkingTree(files)
        self.failIfExists(files)
        self.run_bzr('remove ' + ' '.join(files))
        self.assertNotInWorkingTree(a)
        self.failIfExists(files)

    def test_remove_non_existing_files(self):
        tree = self._make_add_and_assert_tree([])
        self.run_bzr_remove_changed_files(['unknown:[.\s]*b'], ['b'])

    def test_remove_keep_non_existing_files(self):
        tree = self._make_add_and_assert_tree([])
        self.run_bzr('remove --keep b', error_regexes=["b is not versioned."])

    def test_remove_files(self):
        tree = self._make_add_and_assert_tree(files)
        self.run_bzr("commit -m 'added files'")
        self.run_bzr('remove a b b/c d',
                     error_regexes=["deleted a", "deleted b", "deleted b/c",
                     "deleted d"])
        self.assertFilesDeleted(files)

    def test_remove_keep_files(self):
        tree = self._make_add_and_assert_tree(files)
        self.run_bzr("commit -m 'added files'")
        self.run_bzr('remove --keep a b b/c d',
                     error_regexes=["removed a", "removed b", "removed b/c",
                     "removed d"])
        self.assertFilesUnversioned(files)

    def test_remove_with_new(self):
        tree = self._make_add_and_assert_tree(files)
        self.run_bzr('remove --new --keep',
                     error_regexes=["removed a", "removed b", "removed b/c"])
        self.assertFilesUnversioned(files)

    def test_remove_with_new_in_dir1(self):
        tree = self._make_add_and_assert_tree(files)
        self.run_bzr('remove --new --keep b b/c',
                     error_regexes=["removed b", "removed b/c"])
        tree = WorkingTree.open('.')
        self.assertInWorkingTree(a)
        self.assertEqual(tree.path2id(a), a + _id)
        self.assertFilesUnversioned([b,c])

    def test_remove_with_new_in_dir2(self):
        tree = self._make_add_and_assert_tree(files)
        self.run_bzr('remove --new --keep .',
                     error_regexes=["removed a", "removed b", "removed b/c"])
        tree = WorkingTree.open('.')
        self.assertFilesUnversioned(files)