~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

bzr rm removes the working file

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006, 2007 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
"""Tests for interface conformance of 'WorkingTree.remove'"""
 
18
 
 
19
import re
 
20
from bzrlib.tests.workingtree_implementations import TestCaseWithWorkingTree
 
21
from bzrlib import errors, osutils
 
22
 
 
23
class TestRemove(TestCaseWithWorkingTree):
 
24
    """Tests WorkingTree.remove"""
 
25
 
 
26
    files=['a', 'b/', 'b/c', 'd/']
 
27
    a = ['a']
 
28
    b = ['b']
 
29
    b_c = ['b', 'b/c']
 
30
 
 
31
    def getTree(self):
 
32
        self.makeAndChdirToTestDir()
 
33
        tree = self.make_branch_and_tree('.')
 
34
        self.build_tree(TestRemove.files)
 
35
        return tree
 
36
 
 
37
    def test_remove_keep(self):
 
38
        """Check that files are unversioned but not deleted."""
 
39
        tree = self.getTree()
 
40
        tree.add(TestRemove.files)
 
41
        self.assertInWorkingTree(TestRemove.files)
 
42
 
 
43
        tree.remove(TestRemove.files)
 
44
        self.assertNotInWorkingTree(TestRemove.files)
 
45
        self.failUnlessExists(TestRemove.files)
 
46
 
 
47
    def test_remove_unchanged_files(self):
 
48
        """Check that unchanged files are removed and deleted."""
 
49
        tree = self.getTree()
 
50
        tree.add(TestRemove.files)
 
51
        tree.commit("files must not have changes")
 
52
        self.assertInWorkingTree(TestRemove.files)
 
53
 
 
54
        tree.remove(TestRemove.files, keep_files=False)
 
55
 
 
56
        self.assertNotInWorkingTree(TestRemove.files)
 
57
        self.failIfExists(TestRemove.files)
 
58
 
 
59
    def test_remove_added_files(self):
 
60
        """Removal of newly added files must fail."""
 
61
        tree = self.getTree()
 
62
        tree.add(TestRemove.files)
 
63
        self.assertInWorkingTree(TestRemove.files)
 
64
        try:
 
65
            tree.remove(TestRemove.files, keep_files=False)
 
66
            self.fail('Should throw BzrRemoveChangedFilesError')
 
67
        except errors.BzrRemoveChangedFilesError, e:
 
68
            self.assertTrue(re.match('Can\'t remove changed or unknown files:'
 
69
                '.*added:.*a.*b.*b/c.*d.*',
 
70
                str(e), re.DOTALL))
 
71
        self.assertInWorkingTree(TestRemove.files)
 
72
        self.failUnlessExists(TestRemove.files)
 
73
 
 
74
    def test_remove_changed_file(self):
 
75
        """Removal of a changed files must fail."""
 
76
        tree = self.getTree()
 
77
        tree.add(TestRemove.a)
 
78
        tree.commit("make sure a is versioned")
 
79
        f = file('a', 'wb')
 
80
        f.write("some other new content!")
 
81
        f.close()
 
82
        self.assertInWorkingTree(TestRemove.a)
 
83
        try:
 
84
            tree.remove(TestRemove.a, keep_files=False)
 
85
            self.fail('Should throw BzrRemoveChangedFilesError')
 
86
        except errors.BzrRemoveChangedFilesError, e:
 
87
            self.assertTrue(re.match('Can\'t remove changed or unknown files:'
 
88
                '.*modified:.*a.*',
 
89
                str(e), re.DOTALL))
 
90
        self.assertInWorkingTree(TestRemove.a)
 
91
        self.failUnlessExists(TestRemove.a)
 
92
 
 
93
    def test_remove_deleted_files(self):
 
94
        """Check that files are removed if they don't exist any more."""
 
95
        tree = self.getTree()
 
96
        tree.add(TestRemove.files)
 
97
        tree.commit("make sure files are versioned")
 
98
        for f in ['b/c', 'b', 'a', 'd']:
 
99
            osutils.delete_any(f)
 
100
        self.assertInWorkingTree(TestRemove.files)
 
101
        self.failIfExists(TestRemove.files)
 
102
 
 
103
        tree.remove(TestRemove.files, keep_files=False)
 
104
 
 
105
        self.assertNotInWorkingTree(TestRemove.files)
 
106
        self.failIfExists(TestRemove.files)
 
107
 
 
108
    def test_force_remove_changed_files(self):
 
109
        """Check that changed files are removed and deleted when forced."""
 
110
        tree = self.getTree()
 
111
        tree.add(TestRemove.files)
 
112
        self.assertInWorkingTree(TestRemove.files)
 
113
 
 
114
        tree.remove(TestRemove.files, keep_files=False, force=True)
 
115
 
 
116
        self.assertNotInWorkingTree(TestRemove.files)
 
117
        self.failIfExists(TestRemove.files)
 
118
 
 
119
    def test_remove_unknown_files(self):
 
120
        """Try to delete unknown files."""
 
121
        tree = self.getTree()
 
122
        try:
 
123
            tree.remove(TestRemove.files, keep_files=False)
 
124
            self.fail('Should throw BzrRemoveChangedFilesError')
 
125
        except errors.BzrRemoveChangedFilesError, e:
 
126
            self.assertTrue(re.match('Can\'t remove changed or unknown files:'
 
127
                '.*unknown:.*b/c.*b.*a.*d.*',
 
128
                str(e), re.DOTALL))
 
129
 
 
130
    def test_remove_nonexisting_files(self):
 
131
        """Try to delete non-existing files."""
 
132
        tree = self.getTree()
 
133
        tree.remove([''], keep_files=False)
 
134
        try:
 
135
            tree.remove(['xyz', 'abc/def'], keep_files=False)
 
136
            self.fail('Should throw BzrRemoveChangedFilesError')
 
137
        except errors.BzrRemoveChangedFilesError, e:
 
138
            self.assertTrue(re.match('Can\'t remove changed or unknown files:'
 
139
                '.*unknown:.*xyz.*abc/def.*',
 
140
                str(e), re.DOTALL))
 
141
 
 
142
    def test_remove_nonempty_directory(self):
 
143
        """Unchanged non-empty directories should be deleted."""
 
144
        tree = self.getTree()
 
145
        tree.add(TestRemove.files)
 
146
        tree.commit("make sure b is versioned")
 
147
        self.assertInWorkingTree(TestRemove.files)
 
148
        self.failUnlessExists(TestRemove.files)
 
149
        tree.remove(TestRemove.b, keep_files=False)
 
150
        self.assertNotInWorkingTree(TestRemove.b)
 
151
        self.failIfExists(TestRemove.b)
 
152
 
 
153
    def test_remove_nonempty_directory_with_unknowns(self):
 
154
        """Unchanged non-empty directories should be deleted."""
 
155
        tree = self.getTree()
 
156
        tree.add(TestRemove.files)
 
157
        tree.commit("make sure b is versioned")
 
158
        self.assertInWorkingTree(TestRemove.files)
 
159
        self.failUnlessExists(TestRemove.files)
 
160
        f = file('b/my_unknown_file', 'wb')
 
161
        f.write("some content!")
 
162
        f.close()
 
163
        try:
 
164
            tree.remove(TestRemove.b, keep_files=False)
 
165
            self.fail('Should throw BzrRemoveChangedFilesError')
 
166
        except errors.BzrRemoveChangedFilesError, e:
 
167
            self.assertTrue(re.match('Can\'t remove changed or unknown files:'
 
168
                '.*unknown:.*b/my_unknown_file.*',
 
169
                str(e), re.DOTALL))
 
170
        self.assertInWorkingTree(TestRemove.b)
 
171
        self.failUnlessExists(TestRemove.b)
 
172
 
 
173
    def test_force_remove_nonempty_directory(self):
 
174
        tree = self.getTree()
 
175
        tree.add(TestRemove.files)
 
176
        tree.commit("make sure b is versioned")
 
177
        self.assertInWorkingTree(TestRemove.files)
 
178
        self.failUnlessExists(TestRemove.files)
 
179
        tree.remove(TestRemove.b, keep_files=False, force=True)
 
180
        self.assertNotInWorkingTree(TestRemove.b_c)
 
181
        self.failIfExists(TestRemove.b_c)
 
182
 
 
183
    def test_remove_directory_with_changed_file(self):
 
184
        """Refuse to delete directories with changed files."""
 
185
        tree = self.getTree()
 
186
        tree.add(TestRemove.b_c)
 
187
        tree.commit("make sure b and c are versioned")
 
188
        f = file('b/c', 'wb')
 
189
        f.write("some other new content!")
 
190
        f.close()
 
191
        self.assertInWorkingTree(TestRemove.b_c)
 
192
        try:
 
193
            tree.remove(TestRemove.b, keep_files=False)
 
194
            self.fail('Should throw BzrRemoveChangedFilesError')
 
195
        except errors.BzrRemoveChangedFilesError, e:
 
196
            self.assertTrue(re.match('Can\'t remove changed or unknown files:'
 
197
                '.*modified:.*b/c.*',
 
198
                str(e), re.DOTALL))
 
199
        self.assertInWorkingTree(TestRemove.b_c)
 
200
        self.failUnlessExists(TestRemove.b_c)
 
201
 
 
202
        #see if we can force it now..
 
203
        tree.remove(TestRemove.b, keep_files=False, force=True)
 
204
        self.assertNotInWorkingTree(TestRemove.b_c)
 
205
        self.failIfExists(TestRemove.b_c)