~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Alexander Belchenko
  • Date: 2008-03-11 08:49:42 UTC
  • mto: This revision was merged to the branch mainline in revision 3268.
  • Revision ID: bialix@ukr.net-20080311084942-w1w0w3v0m20p2pbc
use sys.version_info

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