~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-12-12 15:17:13 UTC
  • mto: This revision was merged to the branch mainline in revision 3113.
  • Revision ID: abentley@panoramicfeedback.com-20071212151713-ox5n8rlx8m3nsspy
Add support for reconfiguring repositories into branches or trees

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_files_from_child_dir(self):
 
127
        if sys.platform == 'win32':
 
128
            raise TestSkipped("Windows unable to remove '.' directory")
 
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
        os.chdir('b')
 
134
        self.run_bzr_remove_changed_files(['modified:[.\s]*a[.\s]*b/c'],
 
135
            ['../a', 'c', '.', '../d'])
 
136
        os.chdir('..')
 
137
        self.assertNotInWorkingTree(files)
 
138
        self.failIfExists(files)
 
139
 
 
140
    def test_remove_keep_unversioned_files(self):
 
141
        self.build_tree(files)
 
142
        tree = self.make_branch_and_tree('.')
 
143
        self.run_bzr('remove --keep a', error_regexes=["a is not versioned."])
 
144
        self.assertFilesUnversioned(files)
 
145
 
 
146
    def test_remove_force_unversioned_files(self):
 
147
        self.build_tree(files)
 
148
        tree = self.make_branch_and_tree('.')
 
149
        self.run_bzr(['remove', '--force'] + list(files),
 
150
                     error_regexes=["deleted a", "deleted b",
 
151
                                    "deleted b/c", "deleted d"])
 
152
        self.assertFilesDeleted(files)
 
153
 
 
154
    def test_remove_deleted_files(self):
 
155
        tree = self._make_add_and_assert_tree(files)
 
156
        self.run_bzr("commit -m 'added files'")
 
157
        my_files=[f for f in files]
 
158
        my_files.sort(reverse=True)
 
159
        for f in my_files:
 
160
            osutils.delete_any(f)
 
161
        self.assertInWorkingTree(files)
 
162
        self.failIfExists(files)
 
163
        self.run_bzr('remove ' + ' '.join(files))
 
164
        self.assertNotInWorkingTree(a)
 
165
        self.failIfExists(files)
 
166
 
 
167
    def test_remove_non_existing_files(self):
 
168
        tree = self._make_add_and_assert_tree([])
 
169
        self.run_bzr(['remove', 'b'])
 
170
 
 
171
    def test_remove_keep_non_existing_files(self):
 
172
        tree = self._make_add_and_assert_tree([])
 
173
        self.run_bzr('remove --keep b', error_regexes=["b is not versioned."])
 
174
 
 
175
    def test_remove_files(self):
 
176
        tree = self._make_add_and_assert_tree(files)
 
177
        self.run_bzr("commit -m 'added files'")
 
178
        self.run_bzr('remove a b b/c d',
 
179
                     error_regexes=["deleted a", "deleted b", "deleted b/c",
 
180
                     "deleted d"])
 
181
        self.assertFilesDeleted(files)
 
182
 
 
183
    def test_remove_keep_files(self):
 
184
        tree = self._make_add_and_assert_tree(files)
 
185
        self.run_bzr("commit -m 'added files'")
 
186
        self.run_bzr('remove --keep a b b/c d',
 
187
                     error_regexes=["removed a", "removed b", "removed b/c",
 
188
                     "removed d"])
 
189
        self.assertFilesUnversioned(files)
 
190
 
 
191
    def test_remove_with_new(self):
 
192
        tree = self._make_add_and_assert_tree(files)
 
193
        self.run_bzr('remove --new --keep',
 
194
                     error_regexes=["removed a", "removed b", "removed b/c"])
 
195
        self.assertFilesUnversioned(files)
 
196
 
 
197
    def test_remove_with_new_in_dir1(self):
 
198
        tree = self._make_add_and_assert_tree(files)
 
199
        self.run_bzr('remove --new --keep b b/c',
 
200
                     error_regexes=["removed b", "removed b/c"])
 
201
        tree = WorkingTree.open('.')
 
202
        self.assertInWorkingTree(a)
 
203
        self.assertEqual(tree.path2id(a), a + _id)
 
204
        self.assertFilesUnversioned([b,c])
 
205
 
 
206
    def test_remove_with_new_in_dir2(self):
 
207
        tree = self._make_add_and_assert_tree(files)
 
208
        self.run_bzr('remove --new --keep .',
 
209
                     error_regexes=["removed a", "removed b", "removed b/c"])
 
210
        tree = WorkingTree.open('.')
 
211
        self.assertFilesUnversioned(files)