1
# Copyright (C) 2006 Canonical Ltd
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.
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.
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
17
"""Test for 'bzr mv'"""
26
from bzrlib.tests import (
27
TestCaseWithTransport,
32
class TestMove(TestCaseWithTransport):
34
def assertInWorkingTree(self,path):
35
tree = workingtree.WorkingTree.open('.')
36
self.assertIsNot(tree.path2id(path), None,
37
path+' not in working tree.')
39
def assertNotInWorkingTree(self,path):
40
tree = workingtree.WorkingTree.open('.')
41
self.assertIs(tree.path2id(path), None, path+' in working tree.')
43
def assertMoved(self,from_path,to_path):
44
"""Assert that to_path is existing and versioned but from_path not. """
45
self.failIfExists(from_path)
46
self.assertNotInWorkingTree(from_path)
48
self.failUnlessExists(to_path)
49
self.assertInWorkingTree(to_path)
51
def test_mv_modes(self):
52
"""Test two modes of operation for mv"""
53
tree = self.make_branch_and_tree('.')
54
files = self.build_tree(['a', 'c', 'subdir/'])
55
tree.add(['a', 'c', 'subdir'])
57
self.run_bzr('mv', 'a', 'b')
58
self.assertMoved('a','b')
60
self.run_bzr('mv', 'b', 'subdir')
61
self.assertMoved('b','subdir/b')
63
self.run_bzr('mv', 'subdir/b', 'a')
64
self.assertMoved('subdir/b','a')
66
self.run_bzr('mv', 'a', 'c', 'subdir')
67
self.assertMoved('a','subdir/a')
68
self.assertMoved('c','subdir/c')
70
self.run_bzr('mv', 'subdir/a', 'subdir/newa')
71
self.assertMoved('subdir/a','subdir/newa')
73
def test_mv_unversioned(self):
74
self.build_tree(['unversioned.txt'])
76
["^bzr: ERROR: Could not rename unversioned.txt => elsewhere."
77
" .*unversioned.txt is not versioned$"],
78
'mv', 'unversioned.txt', 'elsewhere')
80
def test_mv_nonexisting(self):
82
["^bzr: ERROR: Could not rename doesnotexist => somewhereelse."
83
" .*doesnotexist is not versioned$"],
84
'mv', 'doesnotexist', 'somewhereelse')
86
def test_mv_unqualified(self):
87
self.run_bzr_error(['^bzr: ERROR: missing file argument$'], 'mv')
89
def test_mv_invalid(self):
90
tree = self.make_branch_and_tree('.')
91
self.build_tree(['test.txt', 'sub1/'])
92
tree.add(['test.txt'])
95
["^bzr: ERROR: Could not move to sub1: sub1 is not versioned$"],
96
'mv', 'test.txt', 'sub1')
99
["^bzr: ERROR: Could not move test.txt => .*hello.txt: "
100
"sub1 is not versioned$"],
101
'mv', 'test.txt', 'sub1/hello.txt')
103
def test_mv_dirs(self):
104
tree = self.make_branch_and_tree('.')
105
self.build_tree(['hello.txt', 'sub1/'])
106
tree.add(['hello.txt', 'sub1'])
108
self.run_bzr('mv', 'sub1', 'sub2')
109
self.assertMoved('sub1','sub2')
111
self.run_bzr('mv', 'hello.txt', 'sub2')
112
self.assertMoved('hello.txt','sub2/hello.txt')
114
self.build_tree(['sub1/'])
116
self.run_bzr('mv', 'sub2/hello.txt', 'sub1')
117
self.assertMoved('sub2/hello.txt','sub1/hello.txt')
119
self.run_bzr('mv', 'sub2', 'sub1')
120
self.assertMoved('sub2','sub1/sub2')
122
def test_mv_relative(self):
123
self.build_tree(['sub1/', 'sub1/sub2/', 'sub1/hello.txt'])
124
tree = self.make_branch_and_tree('.')
125
tree.add(['sub1', 'sub1/sub2', 'sub1/hello.txt'])
127
os.chdir('sub1/sub2')
128
self.run_bzr('mv', '../hello.txt', '.')
129
self.failUnlessExists('./hello.txt')
132
self.run_bzr('mv', 'sub2/hello.txt', '.')
134
self.assertMoved('sub1/sub2/hello.txt','sub1/hello.txt')
136
def test_mv_smoke_aliases(self):
137
# just test that aliases for mv exist, if their behaviour is changed in
138
# the future, then extend the tests.
139
self.build_tree(['a'])
140
tree = self.make_branch_and_tree('.')
143
self.run_bzr('move', 'a', 'b')
144
self.run_bzr('rename', 'b', 'a')
146
def test_mv_through_symlinks(self):
147
if not osutils.has_symlinks():
148
raise TestSkipped('Symlinks are not supported on this platform')
149
tree = self.make_branch_and_tree('.')
150
self.build_tree(['a/', 'a/b'])
153
tree.add(['a', 'a/b', 'c'], ['a-id', 'b-id', 'c-id'])
154
self.run_bzr('mv', 'c/b', 'b')
155
tree = workingtree.WorkingTree.open('.')
156
self.assertEqual('b-id', tree.path2id('b'))
158
def test_mv_already_moved_file(self):
159
"""Test bzr mv original_file to moved_file.
161
Tests if a file which has allready been moved by an external tool,
162
is handled correctly by bzr mv.
163
Setup: a is in the working tree, b does not exist.
164
User does: mv a b; bzr mv a b
166
self.build_tree(['a'])
167
tree = self.make_branch_and_tree('.')
170
osutils.rename('a', 'b')
171
self.run_bzr('mv', 'a', 'b')
172
self.assertMoved('a','b')
174
def test_mv_already_moved_file_to_versioned_target(self):
175
"""Test bzr mv existing_file to versioned_file.
177
Tests if an attempt to move an existing versioned file
178
to another versiond file will fail.
179
Setup: a and b are in the working tree.
180
User does: rm b; mv a b; bzr mv a b
182
self.build_tree(['a', 'b'])
183
tree = self.make_branch_and_tree('.')
187
osutils.rename('a', 'b')
189
["^bzr: ERROR: Could not move a => b. b is already versioned$"],
191
#check that nothing changed
192
self.failIfExists('a')
193
self.failUnlessExists('b')
195
def test_mv_already_moved_file_into_subdir(self):
196
"""Test bzr mv original_file to versioned_directory/file.
198
Tests if a file which has already been moved into a versioned
199
directory by an external tool, is handled correctly by bzr mv.
200
Setup: a and sub/ are in the working tree.
201
User does: mv a sub/a; bzr mv a sub/a
203
self.build_tree(['a', 'sub/'])
204
tree = self.make_branch_and_tree('.')
205
tree.add(['a', 'sub'])
207
osutils.rename('a', 'sub/a')
208
self.run_bzr('mv', 'a', 'sub/a')
209
self.assertMoved('a','sub/a')
211
def test_mv_already_moved_file_into_unversioned_subdir(self):
212
"""Test bzr mv original_file to unversioned_directory/file.
214
Tests if an attempt to move an existing versioned file
215
into an unversioned directory will fail.
216
Setup: a is in the working tree, sub/ is not.
217
User does: mv a sub/a; bzr mv a sub/a
219
self.build_tree(['a', 'sub/'])
220
tree = self.make_branch_and_tree('.')
223
osutils.rename('a', 'sub/a')
225
["^bzr: ERROR: Could not move a => a: sub is not versioned$"],
227
self.failIfExists('a')
228
self.failUnlessExists('sub/a')
230
def test_mv_already_moved_files_into_subdir(self):
231
"""Test bzr mv original_files to versioned_directory.
233
Tests if files which has already been moved into a versioned
234
directory by an external tool, is handled correctly by bzr mv.
235
Setup: a1, a2, sub are in the working tree.
236
User does: mv a1 sub/.; bzr mv a1 a2 sub
238
self.build_tree(['a1', 'a2', 'sub/'])
239
tree = self.make_branch_and_tree('.')
240
tree.add(['a1', 'a2', 'sub'])
242
osutils.rename('a1', 'sub/a1')
243
self.run_bzr('mv', 'a1', 'a2', 'sub')
244
self.assertMoved('a1','sub/a1')
245
self.assertMoved('a2','sub/a2')
247
def test_mv_already_moved_files_into_unversioned_subdir(self):
248
"""Test bzr mv original_file to unversioned_directory.
250
Tests if an attempt to move existing versioned file
251
into an unversioned directory will fail.
252
Setup: a1, a2 are in the working tree, sub is not.
253
User does: mv a1 sub/.; bzr mv a1 a2 sub
255
self.build_tree(['a1', 'a2', 'sub/'])
256
tree = self.make_branch_and_tree('.')
257
tree.add(['a1', 'a2'])
259
osutils.rename('a1', 'sub/a1')
261
["^bzr: ERROR: Could not move to sub. sub is not versioned$"],
262
'mv', 'a1', 'a2', 'sub')
263
self.failIfExists('a1')
264
self.failUnlessExists('sub/a1')
265
self.failUnlessExists('a2')
266
self.failIfExists('sub/a2')
268
def test_mv_already_moved_file_forcing_after(self):
269
"""Test bzr mv versioned_file to unversioned_file.
271
Tests if an attempt to move an existing versioned file to an existing
272
unversioned file will fail, informing the user to use the --after
273
option to force this.
274
Setup: a is in the working tree, b not versioned.
275
User does: mv a b; touch a; bzr mv a b
277
self.build_tree(['a', 'b'])
278
tree = self.make_branch_and_tree('.')
281
osutils.rename('a', 'b')
282
self.build_tree(['a']) #touch a
284
["^bzr: ERROR: Could not rename a => b because both files exist."
285
" \(Use --after to update the Bazaar id\)$"],
287
self.failUnlessExists('a')
288
self.failUnlessExists('b')
290
def test_mv_already_moved_file_using_after(self):
291
"""Test bzr mv --after versioned_file to unversioned_file.
293
Tests if an existing versioned file can be forced to move to an
294
existing unversioned file using the --after option. With the result
295
that bazaar considers the unversioned_file to be moved from
296
versioned_file and versioned_file will become unversioned.
297
Setup: a is in the working tree and b exists.
298
User does: mv a b; touch a; bzr mv a b --after
299
Resulting in a => b and a is unknown.
301
self.build_tree(['a', 'b'])
302
tree = self.make_branch_and_tree('.')
304
osutils.rename('a', 'b')
305
self.build_tree(['a']) #touch a
307
self.run_bzr('mv', 'a', 'b', '--after')
308
self.failUnlessExists('a')
309
self.assertNotInWorkingTree('a')#a should be unknown now.
310
self.failUnlessExists('b')
311
self.assertInWorkingTree('b')
313
def test_mv_already_moved_files_forcing_after(self):
314
"""Test bzr mv versioned_files to directory/unversioned_file.
316
Tests if an attempt to move an existing versioned file to an existing
317
unversioned file in some other directory will fail, informing the user
318
to use the --after option to force this.
320
Setup: a1, a2, sub are versioned and in the working tree,
321
sub/a1, sub/a2 are in working tree.
322
User does: mv a* sub; touch a1; touch a2; bzr mv a1 a2 sub
324
self.build_tree(['a1', 'a2', 'sub/', 'sub/a1', 'sub/a2'])
325
tree = self.make_branch_and_tree('.')
326
tree.add(['a1', 'a2', 'sub'])
327
osutils.rename('a1', 'sub/a1')
328
osutils.rename('a2', 'sub/a2')
329
self.build_tree(['a1']) #touch a1
330
self.build_tree(['a2']) #touch a2
333
["^bzr: ERROR: Could not rename a1 => sub/a1 because both files exist."
334
" \(Use --after to update the Bazaar id\)$"],
335
'mv', 'a1', 'a2', 'sub')
336
self.failUnlessExists('a1')
337
self.failUnlessExists('a2')
338
self.failUnlessExists('sub/a1')
339
self.failUnlessExists('sub/a2')
341
def test_mv_already_moved_files_using_after(self):
342
"""Test bzr mv --after versioned_file to directory/unversioned_file.
344
Tests if an existing versioned file can be forced to move to an
345
existing unversioned file in some other directory using the --after
346
option. With the result that bazaar considers
347
directory/unversioned_file to be moved from versioned_file and
348
versioned_file will become unversioned.
350
Setup: a1, a2, sub are versioned and in the working tree,
351
sub/a1, sub/a2 are in working tree.
352
User does: mv a* sub; touch a1; touch a2; bzr mv a1 a2 sub --after
354
self.build_tree(['a1', 'a2', 'sub/', 'sub/a1', 'sub/a2'])
355
tree = self.make_branch_and_tree('.')
356
tree.add(['a1', 'a2', 'sub'])
357
osutils.rename('a1', 'sub/a1')
358
osutils.rename('a2', 'sub/a2')
359
self.build_tree(['a1']) #touch a1
360
self.build_tree(['a2']) #touch a2
362
self.run_bzr('mv', 'a1', 'a2', 'sub', '--after')
363
self.failUnlessExists('a1')
364
self.failUnlessExists('a2')
365
self.failUnlessExists('sub/a1')
366
self.failUnlessExists('sub/a2')
367
self.assertInWorkingTree('sub/a1')
368
self.assertInWorkingTree('sub/a2')