~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
219
# Copyright (C) 2006, 2007 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

"""Tests for interface conformance of 'WorkingTree.remove'"""

import re
from bzrlib.tests.workingtree_implementations import TestCaseWithWorkingTree
from bzrlib import errors, osutils

class TestRemove(TestCaseWithWorkingTree):
    """Tests WorkingTree.remove"""

    files=['a', 'b/', 'b/c', 'd/']
    a = ['a']
    b = ['b']
    b_c = ['b', 'b/c']

    def getTree(self):
        tree = self.make_branch_and_tree('.')
        self.build_tree(TestRemove.files)
        return tree

    def test_remove_keep(self):
        """Check that files are unversioned but not deleted."""
        tree = self.getTree()
        tree.add(TestRemove.files)
        self.assertInWorkingTree(TestRemove.files)

        tree.remove(TestRemove.files)
        self.assertNotInWorkingTree(TestRemove.files)
        self.failUnlessExists(TestRemove.files)

    def test_remove_unchanged_files(self):
        """Check that unchanged files are removed and deleted."""
        tree = self.getTree()
        tree.add(TestRemove.files)
        tree.commit("files must not have changes")
        self.assertInWorkingTree(TestRemove.files)

        tree.remove(TestRemove.files, keep_files=False)

        self.assertNotInWorkingTree(TestRemove.files)
        self.failIfExists(TestRemove.files)

    def test_remove_added_files(self):
        """Removal of newly added files must fail."""
        tree = self.getTree()
        tree.add(TestRemove.files)
        self.assertInWorkingTree(TestRemove.files)
        try:
            tree.remove(TestRemove.files, keep_files=False)
            self.fail('Should throw BzrRemoveChangedFilesError')
        except errors.BzrRemoveChangedFilesError, e:
            self.assertTrue(re.match('Can\'t remove changed or unknown files:'
                '.*added:.*a.*b.*b/c.*d.*',
                str(e), re.DOTALL))
        self.assertInWorkingTree(TestRemove.files)
        self.failUnlessExists(TestRemove.files)

    def test_remove_changed_file(self):
        """Removal of a changed files must fail."""
        tree = self.getTree()
        tree.add(TestRemove.a)
        tree.commit("make sure a is versioned")
        f = file('a', 'wb')
        f.write("some other new content!")
        f.close()
        self.assertInWorkingTree(TestRemove.a)
        try:
            tree.remove(TestRemove.a, keep_files=False)
            self.fail('Should throw BzrRemoveChangedFilesError')
        except errors.BzrRemoveChangedFilesError, e:
            self.assertTrue(re.match('Can\'t remove changed or unknown files:'
                '.*modified:.*a.*',
                str(e), re.DOTALL))
        self.assertInWorkingTree(TestRemove.a)
        self.failUnlessExists(TestRemove.a)

    def test_remove_deleted_files(self):
        """Check that files are removed if they don't exist any more."""
        tree = self.getTree()
        tree.add(TestRemove.files)
        tree.commit("make sure files are versioned")
        for f in ['b/c', 'b', 'a', 'd']:
            osutils.delete_any(f)
        self.assertInWorkingTree(TestRemove.files)
        self.failIfExists(TestRemove.files)

        tree.remove(TestRemove.files, keep_files=False)

        self.assertNotInWorkingTree(TestRemove.files)
        self.failIfExists(TestRemove.files)

    def test_force_remove_changed_files(self):
        """Check that changed files are removed and deleted when forced."""
        tree = self.getTree()
        tree.add(TestRemove.files)
        self.assertInWorkingTree(TestRemove.files)

        tree.remove(TestRemove.files, keep_files=False, force=True)

        self.assertNotInWorkingTree(TestRemove.files)
        self.failIfExists(TestRemove.files)

    def test_remove_unknown_files(self):
        """Try to delete unknown files."""
        tree = self.getTree()
        try:
            tree.remove(TestRemove.files, keep_files=False)
            self.fail('Should throw BzrRemoveChangedFilesError')
        except errors.BzrRemoveChangedFilesError, e:
            self.assertTrue(re.match('Can\'t remove changed or unknown files:'
                '.*unknown:.*b/c.*b.*a.*d.*',
                str(e), re.DOTALL))

    def test_remove_nonexisting_files(self):
        """Try to delete non-existing files."""
        tree = self.getTree()
        tree.remove([''], keep_files=False)
        try:
            tree.remove(['xyz', 'abc/def'], keep_files=False)
            self.fail('Should throw BzrRemoveChangedFilesError')
        except errors.BzrRemoveChangedFilesError, e:
            self.assertTrue(re.match('Can\'t remove changed or unknown files:'
                '.*unknown:.*xyz.*abc/def.*',
                str(e), re.DOTALL))

    def test_remove_nonempty_directory(self):
        """Unchanged non-empty directories should be deleted."""
        tree = self.getTree()
        tree.add(TestRemove.files)
        tree.commit("make sure b is versioned")
        self.assertInWorkingTree(TestRemove.files)
        self.failUnlessExists(TestRemove.files)
        tree.remove(TestRemove.b, keep_files=False)
        self.assertNotInWorkingTree(TestRemove.b)
        self.failIfExists(TestRemove.b)

    def test_remove_nonempty_directory_with_unknowns(self):
        """Unchanged non-empty directories should be deleted."""
        tree = self.getTree()
        tree.add(TestRemove.files)
        tree.commit("make sure b is versioned")
        self.assertInWorkingTree(TestRemove.files)
        self.failUnlessExists(TestRemove.files)
        f = file('b/my_unknown_file', 'wb')
        f.write("some content!")
        f.close()
        try:
            tree.remove(TestRemove.b, keep_files=False)
            self.fail('Should throw BzrRemoveChangedFilesError')
        except errors.BzrRemoveChangedFilesError, e:
            self.assertTrue(re.match('Can\'t remove changed or unknown files:'
                '.*unknown:.*b/my_unknown_file.*',
                str(e), re.DOTALL))
        self.assertInWorkingTree(TestRemove.b)
        self.failUnlessExists(TestRemove.b)

    def test_force_remove_nonempty_directory(self):
        tree = self.getTree()
        tree.add(TestRemove.files)
        tree.commit("make sure b is versioned")
        self.assertInWorkingTree(TestRemove.files)
        self.failUnlessExists(TestRemove.files)
        tree.remove(TestRemove.b, keep_files=False, force=True)
        self.assertNotInWorkingTree(TestRemove.b_c)
        self.failIfExists(TestRemove.b_c)

    def test_remove_directory_with_changed_file(self):
        """Refuse to delete directories with changed files."""
        tree = self.getTree()
        tree.add(TestRemove.b_c)
        tree.commit("make sure b and c are versioned")
        f = file('b/c', 'wb')
        f.write("some other new content!")
        f.close()
        self.assertInWorkingTree(TestRemove.b_c)
        try:
            tree.remove(TestRemove.b, keep_files=False)
            self.fail('Should throw BzrRemoveChangedFilesError')
        except errors.BzrRemoveChangedFilesError, e:
            self.assertTrue(re.match('Can\'t remove changed or unknown files:'
                '.*modified:.*b/c.*',
                str(e), re.DOTALL))
        self.assertInWorkingTree(TestRemove.b_c)
        self.failUnlessExists(TestRemove.b_c)

        #see if we can force it now..
        tree.remove(TestRemove.b, keep_files=False, force=True)
        self.assertNotInWorkingTree(TestRemove.b_c)
        self.failIfExists(TestRemove.b_c)

    def test_remove_subtree(self):
        tree = self.make_branch_and_tree('.')
        subtree = self.make_branch_and_tree('subtree')
        tree.add('subtree', 'subtree-id')
        tree.remove('subtree')
        self.assertIs(None, tree.path2id('subtree'))

    def test_non_cwd(self):
        tree = self.make_branch_and_tree('tree')
        self.build_tree(['tree/dir/', 'tree/dir/file'])
        tree.add(['dir', 'dir/file'])
        tree.commit('add file')
        tree.remove('dir/', keep_files=False)
        self.failIfExists('tree/dir/file')