1
# Copyright (C) 2006-2012 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Test for 'bzr mv'"""
27
from bzrlib.tests import (
28
TestCaseWithTransport,
30
from bzrlib.tests.features import (
31
CaseInsensitiveFilesystemFeature,
33
UnicodeFilenameFeature,
37
class TestMove(TestCaseWithTransport):
39
def assertMoved(self,from_path,to_path):
40
"""Assert that to_path is existing and versioned but from_path not. """
41
self.assertPathDoesNotExist(from_path)
42
self.assertNotInWorkingTree(from_path)
44
self.assertPathExists(to_path)
45
self.assertInWorkingTree(to_path)
47
def test_mv_modes(self):
48
"""Test two modes of operation for mv"""
49
tree = self.make_branch_and_tree('.')
50
files = self.build_tree(['a', 'c', 'subdir/'])
51
tree.add(['a', 'c', 'subdir'])
53
self.run_bzr('mv a b')
54
self.assertMoved('a','b')
56
self.run_bzr('mv b subdir')
57
self.assertMoved('b','subdir/b')
59
self.run_bzr('mv subdir/b a')
60
self.assertMoved('subdir/b','a')
62
self.run_bzr('mv a c subdir')
63
self.assertMoved('a','subdir/a')
64
self.assertMoved('c','subdir/c')
66
self.run_bzr('mv subdir/a subdir/newa')
67
self.assertMoved('subdir/a','subdir/newa')
69
def test_mv_unversioned(self):
70
self.build_tree(['unversioned.txt'])
72
["^bzr: ERROR: Could not rename unversioned.txt => elsewhere."
73
" .*unversioned.txt is not versioned\.$"],
74
'mv unversioned.txt elsewhere')
76
def test_mv_nonexisting(self):
78
["^bzr: ERROR: Could not rename doesnotexist => somewhereelse."
79
" .*doesnotexist is not versioned\.$"],
80
'mv doesnotexist somewhereelse')
82
def test_mv_unqualified(self):
83
self.run_bzr_error(['^bzr: ERROR: missing file argument$'], 'mv')
85
def test_mv_invalid(self):
86
tree = self.make_branch_and_tree('.')
87
self.build_tree(['test.txt', 'sub1/'])
88
tree.add(['test.txt'])
91
["^bzr: ERROR: Could not move to sub1: sub1 is not versioned\.$"],
95
["^bzr: ERROR: Could not move test.txt => .*hello.txt: "
96
"sub1 is not versioned\.$"],
97
'mv test.txt sub1/hello.txt')
99
def test_mv_dirs(self):
100
tree = self.make_branch_and_tree('.')
101
self.build_tree(['hello.txt', 'sub1/'])
102
tree.add(['hello.txt', 'sub1'])
104
self.run_bzr('mv sub1 sub2')
105
self.assertMoved('sub1','sub2')
107
self.run_bzr('mv hello.txt sub2')
108
self.assertMoved('hello.txt','sub2/hello.txt')
110
self.build_tree(['sub1/'])
112
self.run_bzr('mv sub2/hello.txt sub1')
113
self.assertMoved('sub2/hello.txt','sub1/hello.txt')
115
self.run_bzr('mv sub2 sub1')
116
self.assertMoved('sub2','sub1/sub2')
118
def test_mv_relative(self):
119
self.build_tree(['sub1/', 'sub1/sub2/', 'sub1/hello.txt'])
120
tree = self.make_branch_and_tree('.')
121
tree.add(['sub1', 'sub1/sub2', 'sub1/hello.txt'])
123
self.run_bzr('mv ../hello.txt .', working_dir='sub1/sub2')
124
self.assertPathExists('sub1/sub2/hello.txt')
126
self.run_bzr('mv sub2/hello.txt .', working_dir='sub1')
127
self.assertMoved('sub1/sub2/hello.txt','sub1/hello.txt')
129
def test_mv_change_case_file(self):
130
# test for bug #77740 (mv unable change filename case on Windows)
131
tree = self.make_branch_and_tree('.')
132
self.build_tree(['test.txt'])
133
tree.add(['test.txt'])
134
self.run_bzr('mv test.txt Test.txt')
135
# we can't use failUnlessExists on case-insensitive filesystem
136
# so try to check shape of the tree
137
shape = sorted(os.listdir(u'.'))
138
self.assertEqual(['.bzr', 'Test.txt'], shape)
139
self.assertInWorkingTree('Test.txt')
140
self.assertNotInWorkingTree('test.txt')
142
def test_mv_change_case_dir(self):
143
tree = self.make_branch_and_tree('.')
144
self.build_tree(['foo/'])
146
self.run_bzr('mv foo Foo')
147
# we can't use failUnlessExists on case-insensitive filesystem
148
# so try to check shape of the tree
149
shape = sorted(os.listdir(u'.'))
150
self.assertEqual(['.bzr', 'Foo'], shape)
151
self.assertInWorkingTree('Foo')
152
self.assertNotInWorkingTree('foo')
154
def test_mv_change_case_dir_w_files(self):
155
tree = self.make_branch_and_tree('.')
156
self.build_tree(['foo/', 'foo/bar'])
158
self.run_bzr('mv foo Foo')
159
# we can't use failUnlessExists on case-insensitive filesystem
160
# so try to check shape of the tree
161
shape = sorted(os.listdir(u'.'))
162
self.assertEqual(['.bzr', 'Foo'], shape)
163
self.assertInWorkingTree('Foo')
164
self.assertNotInWorkingTree('foo')
166
def test_mv_file_to_wrong_case_dir(self):
167
self.requireFeature(CaseInsensitiveFilesystemFeature)
168
tree = self.make_branch_and_tree('.')
169
self.build_tree(['foo/', 'bar'])
170
tree.add(['foo', 'bar'])
171
out, err = self.run_bzr('mv bar Foo', retcode=3)
172
self.assertEquals('', out)
174
'bzr: ERROR: Could not move to Foo: Foo is not versioned.\n',
177
def test_mv_smoke_aliases(self):
178
# just test that aliases for mv exist, if their behaviour is changed in
179
# the future, then extend the tests.
180
self.build_tree(['a'])
181
tree = self.make_branch_and_tree('.')
184
self.run_bzr('move a b')
185
self.run_bzr('rename b a')
187
def test_mv_no_root(self):
188
tree = self.make_branch_and_tree('.')
190
["bzr: ERROR: can not move root of branch"],
193
def test_mv_through_symlinks(self):
194
self.requireFeature(SymlinkFeature)
195
tree = self.make_branch_and_tree('.')
196
self.build_tree(['a/', 'a/b'])
199
tree.add(['a', 'a/b', 'c'], ['a-id', 'b-id', 'c-id'])
200
self.run_bzr('mv c/b b')
201
tree = workingtree.WorkingTree.open('.')
202
self.assertEqual('b-id', tree.path2id('b'))
204
def test_mv_already_moved_file(self):
205
"""Test bzr mv original_file to moved_file.
207
Tests if a file which has allready been moved by an external tool,
208
is handled correctly by bzr mv.
209
Setup: a is in the working tree, b does not exist.
210
User does: mv a b; bzr mv a b
212
self.build_tree(['a'])
213
tree = self.make_branch_and_tree('.')
216
osutils.rename('a', 'b')
217
self.run_bzr('mv a b')
218
self.assertMoved('a','b')
220
def test_mv_already_moved_file_to_versioned_target(self):
221
"""Test bzr mv existing_file to versioned_file.
223
Tests if an attempt to move an existing versioned file
224
to another versiond file will fail.
225
Setup: a and b are in the working tree.
226
User does: rm b; mv a b; bzr mv a b
228
self.build_tree(['a', 'b'])
229
tree = self.make_branch_and_tree('.')
233
osutils.rename('a', 'b')
235
["^bzr: ERROR: Could not move a => b. b is already versioned\.$"],
237
#check that nothing changed
238
self.assertPathDoesNotExist('a')
239
self.assertPathExists('b')
241
def test_mv_already_moved_file_into_subdir(self):
242
"""Test bzr mv original_file to versioned_directory/file.
244
Tests if a file which has already been moved into a versioned
245
directory by an external tool, is handled correctly by bzr mv.
246
Setup: a and sub/ are in the working tree.
247
User does: mv a sub/a; bzr mv a sub/a
249
self.build_tree(['a', 'sub/'])
250
tree = self.make_branch_and_tree('.')
251
tree.add(['a', 'sub'])
253
osutils.rename('a', 'sub/a')
254
self.run_bzr('mv a sub/a')
255
self.assertMoved('a','sub/a')
257
def test_mv_already_moved_file_into_unversioned_subdir(self):
258
"""Test bzr mv original_file to unversioned_directory/file.
260
Tests if an attempt to move an existing versioned file
261
into an unversioned directory will fail.
262
Setup: a is in the working tree, sub/ is not.
263
User does: mv a sub/a; bzr mv a sub/a
265
self.build_tree(['a', 'sub/'])
266
tree = self.make_branch_and_tree('.')
269
osutils.rename('a', 'sub/a')
271
["^bzr: ERROR: Could not move a => a: sub is not versioned\.$"],
273
self.assertPathDoesNotExist('a')
274
self.assertPathExists('sub/a')
276
def test_mv_already_moved_files_into_subdir(self):
277
"""Test bzr mv original_files to versioned_directory.
279
Tests if files which has already been moved into a versioned
280
directory by an external tool, is handled correctly by bzr mv.
281
Setup: a1, a2, sub are in the working tree.
282
User does: mv a1 sub/.; bzr mv a1 a2 sub
284
self.build_tree(['a1', 'a2', 'sub/'])
285
tree = self.make_branch_and_tree('.')
286
tree.add(['a1', 'a2', 'sub'])
288
osutils.rename('a1', 'sub/a1')
289
self.run_bzr('mv a1 a2 sub')
290
self.assertMoved('a1','sub/a1')
291
self.assertMoved('a2','sub/a2')
293
def test_mv_already_moved_files_into_unversioned_subdir(self):
294
"""Test bzr mv original_file to unversioned_directory.
296
Tests if an attempt to move existing versioned file
297
into an unversioned directory will fail.
298
Setup: a1, a2 are in the working tree, sub is not.
299
User does: mv a1 sub/.; bzr mv a1 a2 sub
301
self.build_tree(['a1', 'a2', 'sub/'])
302
tree = self.make_branch_and_tree('.')
303
tree.add(['a1', 'a2'])
305
osutils.rename('a1', 'sub/a1')
307
["^bzr: ERROR: Could not move to sub. sub is not versioned\.$"],
309
self.assertPathDoesNotExist('a1')
310
self.assertPathExists('sub/a1')
311
self.assertPathExists('a2')
312
self.assertPathDoesNotExist('sub/a2')
314
def test_mv_already_moved_file_forcing_after(self):
315
"""Test bzr mv versioned_file to unversioned_file.
317
Tests if an attempt to move an existing versioned file to an existing
318
unversioned file will fail, informing the user to use the --after
319
option to force this.
320
Setup: a is in the working tree, b not versioned.
321
User does: mv a b; touch a; bzr mv a b
323
self.build_tree(['a', 'b'])
324
tree = self.make_branch_and_tree('.')
327
osutils.rename('a', 'b')
328
self.build_tree(['a']) #touch a
330
["^bzr: ERROR: Could not rename a => b because both files exist."
331
" \(Use --after to tell bzr about a rename that has already"
334
self.assertPathExists('a')
335
self.assertPathExists('b')
337
def test_mv_already_moved_file_using_after(self):
338
"""Test bzr mv --after versioned_file to unversioned_file.
340
Tests if an existing versioned file can be forced to move to an
341
existing unversioned file using the --after option. With the result
342
that bazaar considers the unversioned_file to be moved from
343
versioned_file and versioned_file will become unversioned.
344
Setup: a is in the working tree and b exists.
345
User does: mv a b; touch a; bzr mv a b --after
346
Resulting in a => b and a is unknown.
348
self.build_tree(['a', 'b'])
349
tree = self.make_branch_and_tree('.')
351
osutils.rename('a', 'b')
352
self.build_tree(['a']) #touch a
354
self.run_bzr('mv a b --after')
355
self.assertPathExists('a')
356
self.assertNotInWorkingTree('a')#a should be unknown now.
357
self.assertPathExists('b')
358
self.assertInWorkingTree('b')
360
def test_mv_already_moved_files_forcing_after(self):
361
"""Test bzr mv versioned_files to directory/unversioned_file.
363
Tests if an attempt to move an existing versioned file to an existing
364
unversioned file in some other directory will fail, informing the user
365
to use the --after option to force this.
367
Setup: a1, a2, sub are versioned and in the working tree,
368
sub/a1, sub/a2 are in working tree.
369
User does: mv a* sub; touch a1; touch a2; bzr mv a1 a2 sub
371
self.build_tree(['a1', 'a2', 'sub/', 'sub/a1', 'sub/a2'])
372
tree = self.make_branch_and_tree('.')
373
tree.add(['a1', 'a2', 'sub'])
374
osutils.rename('a1', 'sub/a1')
375
osutils.rename('a2', 'sub/a2')
376
self.build_tree(['a1']) #touch a1
377
self.build_tree(['a2']) #touch a2
380
["^bzr: ERROR: Could not rename a1 => sub/a1 because both files"
381
" exist. \(Use --after to tell bzr about a rename that has already"
384
self.assertPathExists('a1')
385
self.assertPathExists('a2')
386
self.assertPathExists('sub/a1')
387
self.assertPathExists('sub/a2')
389
def test_mv_already_moved_files_using_after(self):
390
"""Test bzr mv --after versioned_file to directory/unversioned_file.
392
Tests if an existing versioned file can be forced to move to an
393
existing unversioned file in some other directory using the --after
394
option. With the result that bazaar considers
395
directory/unversioned_file to be moved from versioned_file and
396
versioned_file will become unversioned.
398
Setup: a1, a2, sub are versioned and in the working tree,
399
sub/a1, sub/a2 are in working tree.
400
User does: mv a* sub; touch a1; touch a2; bzr mv a1 a2 sub --after
402
self.build_tree(['a1', 'a2', 'sub/', 'sub/a1', 'sub/a2'])
403
tree = self.make_branch_and_tree('.')
404
tree.add(['a1', 'a2', 'sub'])
405
osutils.rename('a1', 'sub/a1')
406
osutils.rename('a2', 'sub/a2')
407
self.build_tree(['a1']) #touch a1
408
self.build_tree(['a2']) #touch a2
410
self.run_bzr('mv a1 a2 sub --after')
411
self.assertPathExists('a1')
412
self.assertPathExists('a2')
413
self.assertPathExists('sub/a1')
414
self.assertPathExists('sub/a2')
415
self.assertInWorkingTree('sub/a1')
416
self.assertInWorkingTree('sub/a2')
418
def test_mv_already_moved_directory(self):
419
"""Use `bzr mv a b` to mark a directory as renamed.
421
https://bugs.launchpad.net/bzr/+bug/107967/
423
self.build_tree(['a/', 'c/'])
424
tree = self.make_branch_and_tree('.')
426
osutils.rename('a', 'b')
427
osutils.rename('c', 'd')
428
# mv a b should work just like it does for already renamed files
429
self.run_bzr('mv a b')
430
self.assertPathDoesNotExist('a')
431
self.assertNotInWorkingTree('a')
432
self.assertPathExists('b')
433
self.assertInWorkingTree('b')
434
# and --after should work, too (technically it's ignored)
435
self.run_bzr('mv --after c d')
436
self.assertPathDoesNotExist('c')
437
self.assertNotInWorkingTree('c')
438
self.assertPathExists('d')
439
self.assertInWorkingTree('d')
441
def make_abcd_tree(self):
442
tree = self.make_branch_and_tree('tree')
443
self.build_tree(['tree/a', 'tree/c'])
445
tree.commit('record old names')
446
osutils.rename('tree/a', 'tree/b')
447
osutils.rename('tree/c', 'tree/d')
450
def test_mv_auto(self):
451
self.make_abcd_tree()
452
out, err = self.run_bzr('mv --auto', working_dir='tree')
453
self.assertEqual(out, '')
454
self.assertEqual(err, 'a => b\nc => d\n')
455
tree = workingtree.WorkingTree.open('tree')
456
self.assertIsNot(None, tree.path2id('b'))
457
self.assertIsNot(None, tree.path2id('d'))
459
def test_mv_auto_one_path(self):
460
self.make_abcd_tree()
461
out, err = self.run_bzr('mv --auto tree')
462
self.assertEqual(out, '')
463
self.assertEqual(err, 'a => b\nc => d\n')
464
tree = workingtree.WorkingTree.open('tree')
465
self.assertIsNot(None, tree.path2id('b'))
466
self.assertIsNot(None, tree.path2id('d'))
468
def test_mv_auto_two_paths(self):
469
self.make_abcd_tree()
470
out, err = self.run_bzr('mv --auto tree tree2', retcode=3)
471
self.assertEqual('bzr: ERROR: Only one path may be specified to'
474
def test_mv_auto_dry_run(self):
475
self.make_abcd_tree()
476
out, err = self.run_bzr('mv --auto --dry-run', working_dir='tree')
477
self.assertEqual(out, '')
478
self.assertEqual(err, 'a => b\nc => d\n')
479
tree = workingtree.WorkingTree.open('tree')
480
self.assertIsNot(None, tree.path2id('a'))
481
self.assertIsNot(None, tree.path2id('c'))
483
def test_mv_no_auto_dry_run(self):
484
self.make_abcd_tree()
485
out, err = self.run_bzr('mv c d --dry-run',
486
working_dir='tree', retcode=3)
487
self.assertEqual('bzr: ERROR: --dry-run requires --auto.\n', err)
489
def test_mv_auto_after(self):
490
self.make_abcd_tree()
491
out, err = self.run_bzr('mv --auto --after', working_dir='tree',
493
self.assertEqual('bzr: ERROR: --after cannot be specified with'
496
def test_mv_quiet(self):
497
tree = self.make_branch_and_tree('.')
498
self.build_tree(['aaa'])
500
out, err = self.run_bzr('mv --quiet aaa bbb')
501
self.assertEqual(out, '')
502
self.assertEqual(err, '')
504
def test_mv_readonly_lightweight_checkout(self):
505
branch = self.make_branch('foo')
506
branch = bzrlib.branch.Branch.open(self.get_readonly_url('foo'))
507
tree = branch.create_checkout('tree', lightweight=True)
508
self.build_tree(['tree/path'])
510
# If this fails, the tree is trying to acquire a branch lock, which it
512
self.run_bzr(['mv', 'tree/path', 'tree/path2'])
514
def test_mv_unversioned_non_ascii(self):
515
"""Clear error on mv of an unversioned non-ascii file, see lp:707954"""
516
self.requireFeature(UnicodeFilenameFeature)
517
tree = self.make_branch_and_tree(".")
518
self.build_tree([u"\xA7"])
519
out, err = self.run_bzr_error(["Could not rename", "not versioned"],
520
["mv", u"\xA7", "b"])
522
def test_mv_removed_non_ascii(self):
523
"""Clear error on mv of a removed non-ascii file, see lp:898541"""
524
self.requireFeature(UnicodeFilenameFeature)
525
tree = self.make_branch_and_tree(".")
526
self.build_tree([u"\xA7"])
528
tree.commit(u"Adding \xA7")
530
out, err = self.run_bzr_error(["Could not rename", "not exist"],
531
["mv", u"\xA7", "b"])