~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-06-21 23:43:17 UTC
  • mto: (2520.5.2 bzr.mpbundle)
  • mto: This revision was merged to the branch mainline in revision 2631.
  • Revision ID: abentley@panoramicfeedback.com-20070621234317-5w3h8h36oe90sups
Implement new merge directive format

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
 
 
17
 
 
18
 
import os
19
 
import sys
20
 
 
21
 
from bzrlib.tests import SymlinkFeature, TestSkipped
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
 
 
18
import os, re, shlex
 
19
 
22
20
from bzrlib.tests.blackbox import ExternalBase
23
21
from bzrlib.workingtree import WorkingTree
24
22
from bzrlib import osutils
33
31
 
34
32
class TestRemove(ExternalBase):
35
33
 
36
 
    def _make_tree_and_add(self, paths):
 
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):
37
45
        tree = self.make_branch_and_tree('.')
38
 
        tree.lock_write()
39
 
        try:
40
 
            self.build_tree(paths)
41
 
            for path in paths:
42
 
                file_id=str(path).replace('/', '_') + _id
43
 
                tree.add(path, file_id)
44
 
        finally:
45
 
            tree.unlock()
 
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)
46
53
        return tree
47
54
 
48
55
    def assertFilesDeleted(self, files):
61
68
        f.write("\nsome other new content!")
62
69
        f.close()
63
70
 
64
 
    def run_bzr_remove_changed_files(self, error_regexes, files_to_remove,
65
 
                                     working_dir=None):
66
 
        error_regexes.extend(["Can't safely remove modified or unknown files:",
 
71
    def run_bzr_remove_changed_files(self, error_regexes, files_to_remove):
 
72
        error_regexes.extend(["Can't remove changed or unknown files:",
67
73
            'Use --keep to not delete them,'
68
74
            ' or --force to delete them regardless.'
69
75
            ])
70
76
        self.run_bzr_error(error_regexes,
71
 
                           ['remove'] + list(files_to_remove),
72
 
                           working_dir=working_dir)
 
77
            'remove ' + ' '.join(files_to_remove))
73
78
        #see if we can force it now
74
 
        self.run_bzr(['remove', '--force'] + list(files_to_remove),
75
 
                     working_dir=working_dir)
76
 
 
77
 
    def test_remove_new_no_files_specified(self):
78
 
        tree = self.make_branch_and_tree('.')
 
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
 
79
86
        self.run_bzr_error(["bzr: ERROR: No matching files."], 'remove --new')
80
 
        self.run_bzr_error(["bzr: ERROR: No matching files."], 'remove --new .')
81
 
 
82
 
    def test_remove_no_files_specified(self):
83
 
        tree = self._make_tree_and_add(['foo'])
84
 
        out, err = self.run_bzr(['rm'])
85
 
        self.assertEqual('', err)
86
 
        self.assertEqual('', out)
87
 
        self.assertInWorkingTree('foo', tree=tree)
88
 
        self.failUnlessExists('foo')
89
 
 
90
 
    def test_remove_no_files_specified_missing_dir_and_contents(self):
91
 
        tree = self._make_tree_and_add(
92
 
            ['foo', 'dir/', 'dir/missing/', 'dir/missing/child'])
93
 
        self.get_transport('.').delete_tree('dir/missing')
94
 
        out, err = self.run_bzr(['rm'])
95
 
        self.assertEqual('', out)
96
 
        self.assertEqual(
97
 
            'removed dir/missing/child\n'
98
 
            'removed dir/missing\n',
99
 
            err)
100
 
        # non-missing paths not touched:
101
 
        self.assertInWorkingTree('foo', tree=tree)
102
 
        self.failUnlessExists('foo')
103
 
        self.assertInWorkingTree('dir', tree=tree)
104
 
        self.failUnlessExists('dir')
105
 
        # missing files unversioned
106
 
        self.assertNotInWorkingTree('dir/missing', tree=tree)
107
 
        self.assertNotInWorkingTree('dir/missing/child', tree=tree)
108
 
 
109
 
    def test_remove_no_files_specified_already_deleted(self):
110
 
        tree = self._make_tree_and_add(['foo', 'bar'])
111
 
        tree.commit('save foo and bar')
112
 
        os.unlink('bar')
113
 
        self.run_bzr(['rm'])
114
 
        self.assertEqual(None, tree.path2id('bar'))
115
 
        # Running rm with a deleted file does not error.
116
 
        out, err = self.run_bzr(['rm'])
117
 
        self.assertEqual('', out)
118
 
        self.assertEqual('', err)
119
 
 
120
 
    def test_remove_no_files_specified_missing_file(self):
121
 
        tree = self._make_tree_and_add(['foo', 'bar'])
122
 
        os.unlink('bar')
123
 
        out, err = self.run_bzr(['rm'])
124
 
        self.assertEqual('', out)
125
 
        self.assertEqual('removed bar\n', err)
126
 
        # non-missing files not touched:
127
 
        self.assertInWorkingTree('foo', tree=tree)
128
 
        self.failUnlessExists('foo')
129
 
        # missing files unversioned
130
 
        self.assertNotInWorkingTree('bar', tree=tree)
131
 
 
132
 
    def test_remove_no_files_specified_missing_link(self):
133
 
        self.requireFeature(SymlinkFeature)
134
 
        tree = self._make_tree_and_add(['foo'])
135
 
        os.symlink('foo', 'linkname')
136
 
        tree.add(['linkname'])
137
 
        os.unlink('linkname')
138
 
        out, err = self.run_bzr(['rm'])
139
 
        self.assertEqual('', out)
140
 
        self.assertEqual('removed linkname\n', err)
141
 
        # non-missing files not touched:
142
 
        self.assertInWorkingTree('foo', tree=tree)
143
 
        self.failUnlessExists('foo')
144
 
        # missing files unversioned
145
 
        self.assertNotInWorkingTree('linkname', tree=tree)
 
87
 
 
88
        self.run_bzr_error(["bzr: ERROR: No matching files."],
 
89
            'remove --new .')
146
90
 
147
91
    def test_rm_one_file(self):
148
 
        tree = self._make_tree_and_add([a])
 
92
        tree = self._make_add_and_assert_tree([a])
149
93
        self.run_bzr("commit -m 'added a'")
150
94
        self.run_bzr('rm a', error_regexes=["deleted a"])
151
95
        self.assertFilesDeleted([a])
152
96
 
153
97
    def test_remove_one_file(self):
154
 
        tree = self._make_tree_and_add([a])
 
98
        tree = self._make_add_and_assert_tree([a])
155
99
        self.run_bzr("commit -m 'added a'")
156
100
        self.run_bzr('remove a', error_regexes=["deleted a"])
157
101
        self.assertFilesDeleted([a])
158
102
 
159
103
    def test_remove_keep_one_file(self):
160
 
        tree = self._make_tree_and_add([a])
 
104
        tree = self._make_add_and_assert_tree([a])
161
105
        self.run_bzr('remove --keep a', error_regexes=["removed a"])
162
106
        self.assertFilesUnversioned([a])
163
107
 
164
108
    def test_remove_one_deleted_file(self):
165
 
        tree = self._make_tree_and_add([a])
 
109
        tree = self._make_add_and_assert_tree([a])
166
110
        self.run_bzr("commit -m 'added a'")
167
111
        os.unlink(a)
168
112
        self.assertInWorkingTree(a)
172
116
    def test_remove_invalid_files(self):
173
117
        self.build_tree(files)
174
118
        tree = self.make_branch_and_tree('.')
175
 
        self.run_bzr(['remove', '.', 'xyz', 'abc/def'])
 
119
        self.run_bzr_remove_changed_files(['unknown:[.\s]*xyz[.\s]*abc/def'],
 
120
            ['.', 'xyz', 'abc/def'])
176
121
 
177
122
    def test_remove_unversioned_files(self):
178
123
        self.build_tree(files)
181
126
            ['unknown:[.\s]*d/[.\s]*b/c[.\s]*b/[.\s]*a'], files)
182
127
 
183
128
    def test_remove_changed_files(self):
184
 
        tree = self._make_tree_and_add(files)
 
129
        tree = self._make_add_and_assert_tree(files)
185
130
        self.run_bzr("commit -m 'added files'")
186
131
        self.changeFile(a)
187
132
        self.changeFile(c)
188
133
        self.run_bzr_remove_changed_files(['modified:[.\s]*a[.\s]*b/c'], files)
189
134
 
190
 
    def test_remove_changed_ignored_files(self):
191
 
        tree = self._make_tree_and_add(['a'])
192
 
        self.run_bzr(['ignore', 'a'])
193
 
        self.run_bzr_remove_changed_files(['added:[.\s]*a'], ['a'])
194
 
 
195
135
    def test_remove_changed_files_from_child_dir(self):
196
 
        if sys.platform == 'win32':
197
 
            raise TestSkipped("Windows unable to remove '.' directory")
198
 
        tree = self._make_tree_and_add(files)
 
136
        tree = self._make_add_and_assert_tree(files)
199
137
        self.run_bzr("commit -m 'added files'")
200
138
        self.changeFile(a)
201
139
        self.changeFile(c)
202
 
        self.run_bzr_remove_changed_files(
203
 
            ['modified:[.\s]*a[.\s]*b/c'],
204
 
            ['../a', 'c', '.', '../d'], working_dir='b')
 
140
        os.chdir('b')
 
141
        self.run_bzr_remove_changed_files(['modified:[.\s]*a[.\s]*b/c'],
 
142
            ['../a', 'c', '.', '../d'])
 
143
        os.chdir('..')
205
144
        self.assertNotInWorkingTree(files)
206
145
        self.failIfExists(files)
207
146
 
214
153
    def test_remove_force_unversioned_files(self):
215
154
        self.build_tree(files)
216
155
        tree = self.make_branch_and_tree('.')
217
 
        self.run_bzr(['remove', '--force'] + list(files),
 
156
        self.run_bzr('remove --force ' + ' '.join(files),
218
157
                     error_regexes=["deleted a", "deleted b",
219
158
                                    "deleted b/c", "deleted d"])
220
159
        self.assertFilesDeleted(files)
221
160
 
222
161
    def test_remove_deleted_files(self):
223
 
        tree = self._make_tree_and_add(files)
 
162
        tree = self._make_add_and_assert_tree(files)
224
163
        self.run_bzr("commit -m 'added files'")
225
164
        my_files=[f for f in files]
226
165
        my_files.sort(reverse=True)
233
172
        self.failIfExists(files)
234
173
 
235
174
    def test_remove_non_existing_files(self):
236
 
        tree = self._make_tree_and_add([])
237
 
        self.run_bzr(['remove', 'b'])
 
175
        tree = self._make_add_and_assert_tree([])
 
176
        self.run_bzr_remove_changed_files(['unknown:[.\s]*b'], ['b'])
238
177
 
239
178
    def test_remove_keep_non_existing_files(self):
240
 
        tree = self._make_tree_and_add([])
 
179
        tree = self._make_add_and_assert_tree([])
241
180
        self.run_bzr('remove --keep b', error_regexes=["b is not versioned."])
242
181
 
243
182
    def test_remove_files(self):
244
 
        tree = self._make_tree_and_add(files)
 
183
        tree = self._make_add_and_assert_tree(files)
245
184
        self.run_bzr("commit -m 'added files'")
246
185
        self.run_bzr('remove a b b/c d',
247
186
                     error_regexes=["deleted a", "deleted b", "deleted b/c",
249
188
        self.assertFilesDeleted(files)
250
189
 
251
190
    def test_remove_keep_files(self):
252
 
        tree = self._make_tree_and_add(files)
 
191
        tree = self._make_add_and_assert_tree(files)
253
192
        self.run_bzr("commit -m 'added files'")
254
193
        self.run_bzr('remove --keep a b b/c d',
255
194
                     error_regexes=["removed a", "removed b", "removed b/c",
257
196
        self.assertFilesUnversioned(files)
258
197
 
259
198
    def test_remove_with_new(self):
260
 
        tree = self._make_tree_and_add(files)
 
199
        tree = self._make_add_and_assert_tree(files)
261
200
        self.run_bzr('remove --new --keep',
262
201
                     error_regexes=["removed a", "removed b", "removed b/c"])
263
202
        self.assertFilesUnversioned(files)
264
203
 
265
204
    def test_remove_with_new_in_dir1(self):
266
 
        tree = self._make_tree_and_add(files)
 
205
        tree = self._make_add_and_assert_tree(files)
267
206
        self.run_bzr('remove --new --keep b b/c',
268
207
                     error_regexes=["removed b", "removed b/c"])
269
208
        tree = WorkingTree.open('.')
272
211
        self.assertFilesUnversioned([b,c])
273
212
 
274
213
    def test_remove_with_new_in_dir2(self):
275
 
        tree = self._make_tree_and_add(files)
 
214
        tree = self._make_add_and_assert_tree(files)
276
215
        self.run_bzr('remove --new --keep .',
277
216
                     error_regexes=["removed a", "removed b", "removed b/c"])
278
217
        tree = WorkingTree.open('.')