1
# Copyright (C) 2006-2010 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
CaseInsensitiveFilesystemFeature,
30
TestCaseWithTransport,
35
class TestMove(TestCaseWithTransport):
37
def assertMoved(self,from_path,to_path):
38
"""Assert that to_path is existing and versioned but from_path not. """
39
self.assertPathDoesNotExist(from_path)
40
self.assertNotInWorkingTree(from_path)
42
self.assertPathExists(to_path)
43
self.assertInWorkingTree(to_path)
45
def test_mv_modes(self):
46
"""Test two modes of operation for mv"""
47
tree = self.make_branch_and_tree('.')
48
files = self.build_tree(['a', 'c', 'subdir/'])
49
tree.add(['a', 'c', 'subdir'])
51
self.run_bzr('mv a b')
52
self.assertMoved('a','b')
54
self.run_bzr('mv b subdir')
55
self.assertMoved('b','subdir/b')
57
self.run_bzr('mv subdir/b a')
58
self.assertMoved('subdir/b','a')
60
self.run_bzr('mv a c subdir')
61
self.assertMoved('a','subdir/a')
62
self.assertMoved('c','subdir/c')
64
self.run_bzr('mv subdir/a subdir/newa')
65
self.assertMoved('subdir/a','subdir/newa')
67
def test_mv_unversioned(self):
68
self.build_tree(['unversioned.txt'])
70
["^bzr: ERROR: Could not rename unversioned.txt => elsewhere."
71
" .*unversioned.txt is not versioned\.$"],
72
'mv unversioned.txt elsewhere')
74
def test_mv_nonexisting(self):
76
["^bzr: ERROR: Could not rename doesnotexist => somewhereelse."
77
" .*doesnotexist is not versioned\.$"],
78
'mv doesnotexist somewhereelse')
80
def test_mv_unqualified(self):
81
self.run_bzr_error(['^bzr: ERROR: missing file argument$'], 'mv')
83
def test_mv_invalid(self):
84
tree = self.make_branch_and_tree('.')
85
self.build_tree(['test.txt', 'sub1/'])
86
tree.add(['test.txt'])
89
["^bzr: ERROR: Could not move to sub1: sub1 is not versioned\.$"],
93
["^bzr: ERROR: Could not move test.txt => .*hello.txt: "
94
"sub1 is not versioned\.$"],
95
'mv test.txt sub1/hello.txt')
97
def test_mv_dirs(self):
98
tree = self.make_branch_and_tree('.')
99
self.build_tree(['hello.txt', 'sub1/'])
100
tree.add(['hello.txt', 'sub1'])
102
self.run_bzr('mv sub1 sub2')
103
self.assertMoved('sub1','sub2')
105
self.run_bzr('mv hello.txt sub2')
106
self.assertMoved('hello.txt','sub2/hello.txt')
108
self.build_tree(['sub1/'])
110
self.run_bzr('mv sub2/hello.txt sub1')
111
self.assertMoved('sub2/hello.txt','sub1/hello.txt')
113
self.run_bzr('mv sub2 sub1')
114
self.assertMoved('sub2','sub1/sub2')
116
def test_mv_relative(self):
117
self.build_tree(['sub1/', 'sub1/sub2/', 'sub1/hello.txt'])
118
tree = self.make_branch_and_tree('.')
119
tree.add(['sub1', 'sub1/sub2', 'sub1/hello.txt'])
121
os.chdir('sub1/sub2')
122
self.run_bzr('mv ../hello.txt .')
123
self.assertPathExists('./hello.txt')
126
self.run_bzr('mv sub2/hello.txt .')
128
self.assertMoved('sub1/sub2/hello.txt','sub1/hello.txt')
130
def test_mv_change_case_file(self):
131
# test for bug #77740 (mv unable change filename case on Windows)
132
tree = self.make_branch_and_tree('.')
133
self.build_tree(['test.txt'])
134
tree.add(['test.txt'])
135
self.run_bzr('mv test.txt Test.txt')
136
# we can't use failUnlessExists on case-insensitive filesystem
137
# so try to check shape of the tree
138
shape = sorted(os.listdir(u'.'))
139
self.assertEqual(['.bzr', 'Test.txt'], shape)
140
self.assertInWorkingTree('Test.txt')
141
self.assertNotInWorkingTree('test.txt')
143
def test_mv_change_case_dir(self):
144
tree = self.make_branch_and_tree('.')
145
self.build_tree(['foo/'])
147
self.run_bzr('mv foo Foo')
148
# we can't use failUnlessExists on case-insensitive filesystem
149
# so try to check shape of the tree
150
shape = sorted(os.listdir(u'.'))
151
self.assertEqual(['.bzr', 'Foo'], shape)
152
self.assertInWorkingTree('Foo')
153
self.assertNotInWorkingTree('foo')
155
def test_mv_change_case_dir_w_files(self):
156
tree = self.make_branch_and_tree('.')
157
self.build_tree(['foo/', 'foo/bar'])
159
self.run_bzr('mv foo Foo')
160
# we can't use failUnlessExists on case-insensitive filesystem
161
# so try to check shape of the tree
162
shape = sorted(os.listdir(u'.'))
163
self.assertEqual(['.bzr', 'Foo'], shape)
164
self.assertInWorkingTree('Foo')
165
self.assertNotInWorkingTree('foo')
167
def test_mv_file_to_wrong_case_dir(self):
168
self.requireFeature(CaseInsensitiveFilesystemFeature)
169
tree = self.make_branch_and_tree('.')
170
self.build_tree(['foo/', 'bar'])
171
tree.add(['foo', 'bar'])
172
out, err = self.run_bzr('mv bar Foo', retcode=3)
173
self.assertEquals('', out)
175
'bzr: ERROR: Could not move to Foo: Foo is not versioned.\n',
178
def test_mv_smoke_aliases(self):
179
# just test that aliases for mv exist, if their behaviour is changed in
180
# the future, then extend the tests.
181
self.build_tree(['a'])
182
tree = self.make_branch_and_tree('.')
185
self.run_bzr('move a b')
186
self.run_bzr('rename b a')
188
def test_mv_through_symlinks(self):
189
self.requireFeature(SymlinkFeature)
190
tree = self.make_branch_and_tree('.')
191
self.build_tree(['a/', 'a/b'])
194
tree.add(['a', 'a/b', 'c'], ['a-id', 'b-id', 'c-id'])
195
self.run_bzr('mv c/b b')
196
tree = workingtree.WorkingTree.open('.')
197
self.assertEqual('b-id', tree.path2id('b'))
199
def test_mv_already_moved_file(self):
200
"""Test bzr mv original_file to moved_file.
202
Tests if a file which has allready been moved by an external tool,
203
is handled correctly by bzr mv.
204
Setup: a is in the working tree, b does not exist.
205
User does: mv a b; bzr mv a b
207
self.build_tree(['a'])
208
tree = self.make_branch_and_tree('.')
211
osutils.rename('a', 'b')
212
self.run_bzr('mv a b')
213
self.assertMoved('a','b')
215
def test_mv_already_moved_file_to_versioned_target(self):
216
"""Test bzr mv existing_file to versioned_file.
218
Tests if an attempt to move an existing versioned file
219
to another versiond file will fail.
220
Setup: a and b are in the working tree.
221
User does: rm b; mv a b; bzr mv a b
223
self.build_tree(['a', 'b'])
224
tree = self.make_branch_and_tree('.')
228
osutils.rename('a', 'b')
230
["^bzr: ERROR: Could not move a => b. b is already versioned\.$"],
232
#check that nothing changed
233
self.assertPathDoesNotExist('a')
234
self.assertPathExists('b')
236
def test_mv_already_moved_file_into_subdir(self):
237
"""Test bzr mv original_file to versioned_directory/file.
239
Tests if a file which has already been moved into a versioned
240
directory by an external tool, is handled correctly by bzr mv.
241
Setup: a and sub/ are in the working tree.
242
User does: mv a sub/a; bzr mv a sub/a
244
self.build_tree(['a', 'sub/'])
245
tree = self.make_branch_and_tree('.')
246
tree.add(['a', 'sub'])
248
osutils.rename('a', 'sub/a')
249
self.run_bzr('mv a sub/a')
250
self.assertMoved('a','sub/a')
252
def test_mv_already_moved_file_into_unversioned_subdir(self):
253
"""Test bzr mv original_file to unversioned_directory/file.
255
Tests if an attempt to move an existing versioned file
256
into an unversioned directory will fail.
257
Setup: a is in the working tree, sub/ is not.
258
User does: mv a sub/a; bzr mv a sub/a
260
self.build_tree(['a', 'sub/'])
261
tree = self.make_branch_and_tree('.')
264
osutils.rename('a', 'sub/a')
266
["^bzr: ERROR: Could not move a => a: sub is not versioned\.$"],
268
self.assertPathDoesNotExist('a')
269
self.assertPathExists('sub/a')
271
def test_mv_already_moved_files_into_subdir(self):
272
"""Test bzr mv original_files to versioned_directory.
274
Tests if files which has already been moved into a versioned
275
directory by an external tool, is handled correctly by bzr mv.
276
Setup: a1, a2, sub are in the working tree.
277
User does: mv a1 sub/.; bzr mv a1 a2 sub
279
self.build_tree(['a1', 'a2', 'sub/'])
280
tree = self.make_branch_and_tree('.')
281
tree.add(['a1', 'a2', 'sub'])
283
osutils.rename('a1', 'sub/a1')
284
self.run_bzr('mv a1 a2 sub')
285
self.assertMoved('a1','sub/a1')
286
self.assertMoved('a2','sub/a2')
288
def test_mv_already_moved_files_into_unversioned_subdir(self):
289
"""Test bzr mv original_file to unversioned_directory.
291
Tests if an attempt to move existing versioned file
292
into an unversioned directory will fail.
293
Setup: a1, a2 are in the working tree, sub is not.
294
User does: mv a1 sub/.; bzr mv a1 a2 sub
296
self.build_tree(['a1', 'a2', 'sub/'])
297
tree = self.make_branch_and_tree('.')
298
tree.add(['a1', 'a2'])
300
osutils.rename('a1', 'sub/a1')
302
["^bzr: ERROR: Could not move to sub. sub is not versioned\.$"],
304
self.assertPathDoesNotExist('a1')
305
self.assertPathExists('sub/a1')
306
self.assertPathExists('a2')
307
self.assertPathDoesNotExist('sub/a2')
309
def test_mv_already_moved_file_forcing_after(self):
310
"""Test bzr mv versioned_file to unversioned_file.
312
Tests if an attempt to move an existing versioned file to an existing
313
unversioned file will fail, informing the user to use the --after
314
option to force this.
315
Setup: a is in the working tree, b not versioned.
316
User does: mv a b; touch a; bzr mv a b
318
self.build_tree(['a', 'b'])
319
tree = self.make_branch_and_tree('.')
322
osutils.rename('a', 'b')
323
self.build_tree(['a']) #touch a
325
["^bzr: ERROR: Could not rename a => b because both files exist."
326
" \(Use --after to tell bzr about a rename that has already"
329
self.assertPathExists('a')
330
self.assertPathExists('b')
332
def test_mv_already_moved_file_using_after(self):
333
"""Test bzr mv --after versioned_file to unversioned_file.
335
Tests if an existing versioned file can be forced to move to an
336
existing unversioned file using the --after option. With the result
337
that bazaar considers the unversioned_file to be moved from
338
versioned_file and versioned_file will become unversioned.
339
Setup: a is in the working tree and b exists.
340
User does: mv a b; touch a; bzr mv a b --after
341
Resulting in a => b and a is unknown.
343
self.build_tree(['a', 'b'])
344
tree = self.make_branch_and_tree('.')
346
osutils.rename('a', 'b')
347
self.build_tree(['a']) #touch a
349
self.run_bzr('mv a b --after')
350
self.assertPathExists('a')
351
self.assertNotInWorkingTree('a')#a should be unknown now.
352
self.assertPathExists('b')
353
self.assertInWorkingTree('b')
355
def test_mv_already_moved_files_forcing_after(self):
356
"""Test bzr mv versioned_files to directory/unversioned_file.
358
Tests if an attempt to move an existing versioned file to an existing
359
unversioned file in some other directory will fail, informing the user
360
to use the --after option to force this.
362
Setup: a1, a2, sub are versioned and in the working tree,
363
sub/a1, sub/a2 are in working tree.
364
User does: mv a* sub; touch a1; touch a2; bzr mv a1 a2 sub
366
self.build_tree(['a1', 'a2', 'sub/', 'sub/a1', 'sub/a2'])
367
tree = self.make_branch_and_tree('.')
368
tree.add(['a1', 'a2', 'sub'])
369
osutils.rename('a1', 'sub/a1')
370
osutils.rename('a2', 'sub/a2')
371
self.build_tree(['a1']) #touch a1
372
self.build_tree(['a2']) #touch a2
375
["^bzr: ERROR: Could not rename a1 => sub/a1 because both files"
376
" exist. \(Use --after to tell bzr about a rename that has already"
379
self.assertPathExists('a1')
380
self.assertPathExists('a2')
381
self.assertPathExists('sub/a1')
382
self.assertPathExists('sub/a2')
384
def test_mv_already_moved_files_using_after(self):
385
"""Test bzr mv --after versioned_file to directory/unversioned_file.
387
Tests if an existing versioned file can be forced to move to an
388
existing unversioned file in some other directory using the --after
389
option. With the result that bazaar considers
390
directory/unversioned_file to be moved from versioned_file and
391
versioned_file will become unversioned.
393
Setup: a1, a2, sub are versioned and in the working tree,
394
sub/a1, sub/a2 are in working tree.
395
User does: mv a* sub; touch a1; touch a2; bzr mv a1 a2 sub --after
397
self.build_tree(['a1', 'a2', 'sub/', 'sub/a1', 'sub/a2'])
398
tree = self.make_branch_and_tree('.')
399
tree.add(['a1', 'a2', 'sub'])
400
osutils.rename('a1', 'sub/a1')
401
osutils.rename('a2', 'sub/a2')
402
self.build_tree(['a1']) #touch a1
403
self.build_tree(['a2']) #touch a2
405
self.run_bzr('mv a1 a2 sub --after')
406
self.assertPathExists('a1')
407
self.assertPathExists('a2')
408
self.assertPathExists('sub/a1')
409
self.assertPathExists('sub/a2')
410
self.assertInWorkingTree('sub/a1')
411
self.assertInWorkingTree('sub/a2')
413
def test_mv_already_moved_directory(self):
414
"""Use `bzr mv a b` to mark a directory as renamed.
416
https://bugs.launchpad.net/bzr/+bug/107967/
418
self.build_tree(['a/', 'c/'])
419
tree = self.make_branch_and_tree('.')
421
osutils.rename('a', 'b')
422
osutils.rename('c', 'd')
423
# mv a b should work just like it does for already renamed files
424
self.run_bzr('mv a b')
425
self.assertPathDoesNotExist('a')
426
self.assertNotInWorkingTree('a')
427
self.assertPathExists('b')
428
self.assertInWorkingTree('b')
429
# and --after should work, too (technically it's ignored)
430
self.run_bzr('mv --after c d')
431
self.assertPathDoesNotExist('c')
432
self.assertNotInWorkingTree('c')
433
self.assertPathExists('d')
434
self.assertInWorkingTree('d')
436
def make_abcd_tree(self):
437
tree = self.make_branch_and_tree('tree')
438
self.build_tree(['tree/a', 'tree/c'])
440
tree.commit('record old names')
441
osutils.rename('tree/a', 'tree/b')
442
osutils.rename('tree/c', 'tree/d')
445
def test_mv_auto(self):
446
self.make_abcd_tree()
447
out, err = self.run_bzr('mv --auto', working_dir='tree')
448
self.assertEqual(out, '')
449
self.assertEqual(err, 'a => b\nc => d\n')
450
tree = workingtree.WorkingTree.open('tree')
451
self.assertIsNot(None, tree.path2id('b'))
452
self.assertIsNot(None, tree.path2id('d'))
454
def test_mv_auto_one_path(self):
455
self.make_abcd_tree()
456
out, err = self.run_bzr('mv --auto tree')
457
self.assertEqual(out, '')
458
self.assertEqual(err, 'a => b\nc => d\n')
459
tree = workingtree.WorkingTree.open('tree')
460
self.assertIsNot(None, tree.path2id('b'))
461
self.assertIsNot(None, tree.path2id('d'))
463
def test_mv_auto_two_paths(self):
464
self.make_abcd_tree()
465
out, err = self.run_bzr('mv --auto tree tree2', retcode=3)
466
self.assertEqual('bzr: ERROR: Only one path may be specified to'
469
def test_mv_auto_dry_run(self):
470
self.make_abcd_tree()
471
out, err = self.run_bzr('mv --auto --dry-run', working_dir='tree')
472
self.assertEqual(out, '')
473
self.assertEqual(err, 'a => b\nc => d\n')
474
tree = workingtree.WorkingTree.open('tree')
475
self.assertIsNot(None, tree.path2id('a'))
476
self.assertIsNot(None, tree.path2id('c'))
478
def test_mv_no_auto_dry_run(self):
479
self.make_abcd_tree()
480
out, err = self.run_bzr('mv c d --dry-run',
481
working_dir='tree', retcode=3)
482
self.assertEqual('bzr: ERROR: --dry-run requires --auto.\n', err)
484
def test_mv_auto_after(self):
485
self.make_abcd_tree()
486
out, err = self.run_bzr('mv --auto --after', working_dir='tree',
488
self.assertEqual('bzr: ERROR: --after cannot be specified with'
491
def test_mv_quiet(self):
492
tree = self.make_branch_and_tree('.')
493
self.build_tree(['aaa'])
495
out, err = self.run_bzr('mv --quiet aaa bbb')
496
self.assertEqual(out, '')
497
self.assertEqual(err, '')
499
def test_mv_readonly_lightweight_checkout(self):
500
branch = self.make_branch('foo')
501
branch = bzrlib.branch.Branch.open(self.get_readonly_url('foo'))
502
tree = branch.create_checkout('tree', lightweight=True)
503
self.build_tree(['tree/path'])
505
# If this fails, the tree is trying to acquire a branch lock, which it
507
self.run_bzr(['mv', 'tree/path', 'tree/path2'])
509
def test_mv_unversioned_non_ascii(self):
510
"""Clear error on mv of an unversioned non-ascii file, see lp:707954"""
511
self.requireFeature(UnicodeFilename)
512
tree = self.make_branch_and_tree(".")
513
self.build_tree([u"\xA7"])
514
out, err = self.run_bzr_error(["Could not rename", "not versioned"],
515
["mv", u"\xA7", "b"])