~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: John Arbash Meinel
  • Date: 2007-04-28 15:04:17 UTC
  • mfrom: (2466 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2566.
  • Revision ID: john@arbash-meinel.com-20070428150417-trp3pi0pzd411pu4
[merge] bzr.dev 2466

Show diffs side-by-side

added added

removed removed

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