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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
17
"""Test for 'bzr mv'"""
21
from bzrlib.tests import TestCaseWithTransport
27
from bzrlib.tests import (
28
TestCaseWithTransport,
30
from bzrlib.tests.features import (
31
CaseInsensitiveFilesystemFeature,
33
UnicodeFilenameFeature,
24
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)
26
47
def test_mv_modes(self):
27
48
"""Test two modes of operation for mv"""
28
49
tree = self.make_branch_and_tree('.')
29
50
files = self.build_tree(['a', 'c', 'subdir/'])
30
51
tree.add(['a', 'c', 'subdir'])
32
self.run_bzr('mv', 'a', 'b')
33
self.failUnlessExists('b')
34
self.failIfExists('a')
36
self.run_bzr('mv', 'b', 'subdir')
37
self.failUnlessExists('subdir/b')
38
self.failIfExists('b')
40
self.run_bzr('mv', 'subdir/b', 'a')
41
self.failUnlessExists('a')
42
self.failIfExists('subdir/b')
44
self.run_bzr('mv', 'a', 'c', 'subdir')
45
self.failUnlessExists('subdir/a')
46
self.failUnlessExists('subdir/c')
47
self.failIfExists('a')
48
self.failIfExists('c')
50
self.run_bzr('mv', 'subdir/a', 'subdir/newa')
51
self.failUnlessExists('subdir/newa')
52
self.failIfExists('subdir/a')
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')
54
69
def test_mv_unversioned(self):
55
70
self.build_tree(['unversioned.txt'])
56
71
self.run_bzr_error(
57
["^bzr: ERROR: can't rename: old name .* is not versioned$"],
58
'mv', 'unversioned.txt', 'elsewhere')
72
["^bzr: ERROR: Could not rename unversioned.txt => elsewhere."
73
" .*unversioned.txt is not versioned\.$"],
74
'mv unversioned.txt elsewhere')
60
76
def test_mv_nonexisting(self):
61
77
self.run_bzr_error(
62
["^bzr: ERROR: can't rename: old working file .* does not exist$"],
63
'mv', 'doesnotexist', 'somewhereelse')
78
["^bzr: ERROR: Could not rename doesnotexist => somewhereelse."
79
" .*doesnotexist is not versioned\.$"],
80
'mv doesnotexist somewhereelse')
65
82
def test_mv_unqualified(self):
66
83
self.run_bzr_error(['^bzr: ERROR: missing file argument$'], 'mv')
68
85
def test_mv_invalid(self):
69
86
tree = self.make_branch_and_tree('.')
70
87
self.build_tree(['test.txt', 'sub1/'])
71
88
tree.add(['test.txt'])
73
90
self.run_bzr_error(
74
["^bzr: ERROR: destination u'sub1' is not a versioned directory$"],
75
'mv', 'test.txt', 'sub1')
91
["^bzr: ERROR: Could not move to sub1: sub1 is not versioned\.$"],
77
94
self.run_bzr_error(
78
["^bzr: ERROR: can't determine destination directory id for u'sub1'$"],
79
'mv', 'test.txt', 'sub1/hello.txt')
95
["^bzr: ERROR: Could not move test.txt => .*hello.txt: "
96
"sub1 is not versioned\.$"],
97
'mv test.txt sub1/hello.txt')
81
99
def test_mv_dirs(self):
82
100
tree = self.make_branch_and_tree('.')
83
101
self.build_tree(['hello.txt', 'sub1/'])
84
102
tree.add(['hello.txt', 'sub1'])
86
self.run_bzr('mv', 'sub1', 'sub2')
87
self.failUnlessExists('sub2')
88
self.failIfExists('sub1')
89
self.run_bzr('mv', 'hello.txt', 'sub2')
90
self.failUnlessExists("sub2/hello.txt")
91
self.failIfExists("hello.txt")
104
self.run_bzr('mv sub1 sub2')
105
self.assertMoved('sub1','sub2')
93
tree.read_working_inventory()
94
tree.commit('commit with some things moved to subdirs')
107
self.run_bzr('mv hello.txt sub2')
108
self.assertMoved('hello.txt','sub2/hello.txt')
96
110
self.build_tree(['sub1/'])
97
111
tree.add(['sub1'])
98
self.run_bzr('mv', 'sub2/hello.txt', 'sub1')
99
self.failIfExists('sub2/hello.txt')
100
self.failUnlessExists('sub1/hello.txt')
101
self.run_bzr('mv', 'sub2', 'sub1')
102
self.failIfExists('sub2')
103
self.failUnlessExists('sub1/sub2')
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')
105
118
def test_mv_relative(self):
106
119
self.build_tree(['sub1/', 'sub1/sub2/', 'sub1/hello.txt'])
107
120
tree = self.make_branch_and_tree('.')
108
121
tree.add(['sub1', 'sub1/sub2', 'sub1/hello.txt'])
109
tree.commit('initial tree')
111
123
os.chdir('sub1/sub2')
112
self.run_bzr('mv', '../hello.txt', '.')
113
self.failUnlessExists('./hello.txt')
114
tree.read_working_inventory()
115
tree.commit('move to parent directory')
119
self.run_bzr('mv', 'sub2/hello.txt', '.')
120
self.failUnlessExists('hello.txt')
124
self.run_bzr('mv ../hello.txt .')
125
self.assertPathExists('./hello.txt')
128
self.run_bzr('mv sub2/hello.txt .')
130
self.assertMoved('sub1/sub2/hello.txt','sub1/hello.txt')
132
def test_mv_change_case_file(self):
133
# test for bug #77740 (mv unable change filename case on Windows)
134
tree = self.make_branch_and_tree('.')
135
self.build_tree(['test.txt'])
136
tree.add(['test.txt'])
137
self.run_bzr('mv test.txt Test.txt')
138
# we can't use failUnlessExists on case-insensitive filesystem
139
# so try to check shape of the tree
140
shape = sorted(os.listdir(u'.'))
141
self.assertEqual(['.bzr', 'Test.txt'], shape)
142
self.assertInWorkingTree('Test.txt')
143
self.assertNotInWorkingTree('test.txt')
145
def test_mv_change_case_dir(self):
146
tree = self.make_branch_and_tree('.')
147
self.build_tree(['foo/'])
149
self.run_bzr('mv foo Foo')
150
# we can't use failUnlessExists on case-insensitive filesystem
151
# so try to check shape of the tree
152
shape = sorted(os.listdir(u'.'))
153
self.assertEqual(['.bzr', 'Foo'], shape)
154
self.assertInWorkingTree('Foo')
155
self.assertNotInWorkingTree('foo')
157
def test_mv_change_case_dir_w_files(self):
158
tree = self.make_branch_and_tree('.')
159
self.build_tree(['foo/', 'foo/bar'])
161
self.run_bzr('mv foo Foo')
162
# we can't use failUnlessExists on case-insensitive filesystem
163
# so try to check shape of the tree
164
shape = sorted(os.listdir(u'.'))
165
self.assertEqual(['.bzr', 'Foo'], shape)
166
self.assertInWorkingTree('Foo')
167
self.assertNotInWorkingTree('foo')
169
def test_mv_file_to_wrong_case_dir(self):
170
self.requireFeature(CaseInsensitiveFilesystemFeature)
171
tree = self.make_branch_and_tree('.')
172
self.build_tree(['foo/', 'bar'])
173
tree.add(['foo', 'bar'])
174
out, err = self.run_bzr('mv bar Foo', retcode=3)
175
self.assertEquals('', out)
177
'bzr: ERROR: Could not move to Foo: Foo is not versioned.\n',
122
180
def test_mv_smoke_aliases(self):
123
181
# just test that aliases for mv exist, if their behaviour is changed in
126
184
tree = self.make_branch_and_tree('.')
129
self.run_bzr('move', 'a', 'b')
130
self.run_bzr('rename', 'b', 'a')
187
self.run_bzr('move a b')
188
self.run_bzr('rename b a')
190
def test_mv_no_root(self):
191
tree = self.make_branch_and_tree('.')
193
["bzr: ERROR: can not move root of branch"],
196
def test_mv_through_symlinks(self):
197
self.requireFeature(SymlinkFeature)
198
tree = self.make_branch_and_tree('.')
199
self.build_tree(['a/', 'a/b'])
202
tree.add(['a', 'a/b', 'c'], ['a-id', 'b-id', 'c-id'])
203
self.run_bzr('mv c/b b')
204
tree = workingtree.WorkingTree.open('.')
205
self.assertEqual('b-id', tree.path2id('b'))
207
def test_mv_already_moved_file(self):
208
"""Test bzr mv original_file to moved_file.
210
Tests if a file which has allready been moved by an external tool,
211
is handled correctly by bzr mv.
212
Setup: a is in the working tree, b does not exist.
213
User does: mv a b; bzr mv a b
215
self.build_tree(['a'])
216
tree = self.make_branch_and_tree('.')
219
osutils.rename('a', 'b')
220
self.run_bzr('mv a b')
221
self.assertMoved('a','b')
223
def test_mv_already_moved_file_to_versioned_target(self):
224
"""Test bzr mv existing_file to versioned_file.
226
Tests if an attempt to move an existing versioned file
227
to another versiond file will fail.
228
Setup: a and b are in the working tree.
229
User does: rm b; mv a b; bzr mv a b
231
self.build_tree(['a', 'b'])
232
tree = self.make_branch_and_tree('.')
236
osutils.rename('a', 'b')
238
["^bzr: ERROR: Could not move a => b. b is already versioned\.$"],
240
#check that nothing changed
241
self.assertPathDoesNotExist('a')
242
self.assertPathExists('b')
244
def test_mv_already_moved_file_into_subdir(self):
245
"""Test bzr mv original_file to versioned_directory/file.
247
Tests if a file which has already been moved into a versioned
248
directory by an external tool, is handled correctly by bzr mv.
249
Setup: a and sub/ are in the working tree.
250
User does: mv a sub/a; bzr mv a sub/a
252
self.build_tree(['a', 'sub/'])
253
tree = self.make_branch_and_tree('.')
254
tree.add(['a', 'sub'])
256
osutils.rename('a', 'sub/a')
257
self.run_bzr('mv a sub/a')
258
self.assertMoved('a','sub/a')
260
def test_mv_already_moved_file_into_unversioned_subdir(self):
261
"""Test bzr mv original_file to unversioned_directory/file.
263
Tests if an attempt to move an existing versioned file
264
into an unversioned directory will fail.
265
Setup: a is in the working tree, sub/ is not.
266
User does: mv a sub/a; bzr mv a sub/a
268
self.build_tree(['a', 'sub/'])
269
tree = self.make_branch_and_tree('.')
272
osutils.rename('a', 'sub/a')
274
["^bzr: ERROR: Could not move a => a: sub is not versioned\.$"],
276
self.assertPathDoesNotExist('a')
277
self.assertPathExists('sub/a')
279
def test_mv_already_moved_files_into_subdir(self):
280
"""Test bzr mv original_files to versioned_directory.
282
Tests if files which has already been moved into a versioned
283
directory by an external tool, is handled correctly by bzr mv.
284
Setup: a1, a2, sub are in the working tree.
285
User does: mv a1 sub/.; bzr mv a1 a2 sub
287
self.build_tree(['a1', 'a2', 'sub/'])
288
tree = self.make_branch_and_tree('.')
289
tree.add(['a1', 'a2', 'sub'])
291
osutils.rename('a1', 'sub/a1')
292
self.run_bzr('mv a1 a2 sub')
293
self.assertMoved('a1','sub/a1')
294
self.assertMoved('a2','sub/a2')
296
def test_mv_already_moved_files_into_unversioned_subdir(self):
297
"""Test bzr mv original_file to unversioned_directory.
299
Tests if an attempt to move existing versioned file
300
into an unversioned directory will fail.
301
Setup: a1, a2 are in the working tree, sub is not.
302
User does: mv a1 sub/.; bzr mv a1 a2 sub
304
self.build_tree(['a1', 'a2', 'sub/'])
305
tree = self.make_branch_and_tree('.')
306
tree.add(['a1', 'a2'])
308
osutils.rename('a1', 'sub/a1')
310
["^bzr: ERROR: Could not move to sub. sub is not versioned\.$"],
312
self.assertPathDoesNotExist('a1')
313
self.assertPathExists('sub/a1')
314
self.assertPathExists('a2')
315
self.assertPathDoesNotExist('sub/a2')
317
def test_mv_already_moved_file_forcing_after(self):
318
"""Test bzr mv versioned_file to unversioned_file.
320
Tests if an attempt to move an existing versioned file to an existing
321
unversioned file will fail, informing the user to use the --after
322
option to force this.
323
Setup: a is in the working tree, b not versioned.
324
User does: mv a b; touch a; bzr mv a b
326
self.build_tree(['a', 'b'])
327
tree = self.make_branch_and_tree('.')
330
osutils.rename('a', 'b')
331
self.build_tree(['a']) #touch a
333
["^bzr: ERROR: Could not rename a => b because both files exist."
334
" \(Use --after to tell bzr about a rename that has already"
337
self.assertPathExists('a')
338
self.assertPathExists('b')
340
def test_mv_already_moved_file_using_after(self):
341
"""Test bzr mv --after versioned_file to unversioned_file.
343
Tests if an existing versioned file can be forced to move to an
344
existing unversioned file using the --after option. With the result
345
that bazaar considers the unversioned_file to be moved from
346
versioned_file and versioned_file will become unversioned.
347
Setup: a is in the working tree and b exists.
348
User does: mv a b; touch a; bzr mv a b --after
349
Resulting in a => b and a is unknown.
351
self.build_tree(['a', 'b'])
352
tree = self.make_branch_and_tree('.')
354
osutils.rename('a', 'b')
355
self.build_tree(['a']) #touch a
357
self.run_bzr('mv a b --after')
358
self.assertPathExists('a')
359
self.assertNotInWorkingTree('a')#a should be unknown now.
360
self.assertPathExists('b')
361
self.assertInWorkingTree('b')
363
def test_mv_already_moved_files_forcing_after(self):
364
"""Test bzr mv versioned_files to directory/unversioned_file.
366
Tests if an attempt to move an existing versioned file to an existing
367
unversioned file in some other directory will fail, informing the user
368
to use the --after option to force this.
370
Setup: a1, a2, sub are versioned and in the working tree,
371
sub/a1, sub/a2 are in working tree.
372
User does: mv a* sub; touch a1; touch a2; bzr mv a1 a2 sub
374
self.build_tree(['a1', 'a2', 'sub/', 'sub/a1', 'sub/a2'])
375
tree = self.make_branch_and_tree('.')
376
tree.add(['a1', 'a2', 'sub'])
377
osutils.rename('a1', 'sub/a1')
378
osutils.rename('a2', 'sub/a2')
379
self.build_tree(['a1']) #touch a1
380
self.build_tree(['a2']) #touch a2
383
["^bzr: ERROR: Could not rename a1 => sub/a1 because both files"
384
" exist. \(Use --after to tell bzr about a rename that has already"
387
self.assertPathExists('a1')
388
self.assertPathExists('a2')
389
self.assertPathExists('sub/a1')
390
self.assertPathExists('sub/a2')
392
def test_mv_already_moved_files_using_after(self):
393
"""Test bzr mv --after versioned_file to directory/unversioned_file.
395
Tests if an existing versioned file can be forced to move to an
396
existing unversioned file in some other directory using the --after
397
option. With the result that bazaar considers
398
directory/unversioned_file to be moved from versioned_file and
399
versioned_file will become unversioned.
401
Setup: a1, a2, sub are versioned and in the working tree,
402
sub/a1, sub/a2 are in working tree.
403
User does: mv a* sub; touch a1; touch a2; bzr mv a1 a2 sub --after
405
self.build_tree(['a1', 'a2', 'sub/', 'sub/a1', 'sub/a2'])
406
tree = self.make_branch_and_tree('.')
407
tree.add(['a1', 'a2', 'sub'])
408
osutils.rename('a1', 'sub/a1')
409
osutils.rename('a2', 'sub/a2')
410
self.build_tree(['a1']) #touch a1
411
self.build_tree(['a2']) #touch a2
413
self.run_bzr('mv a1 a2 sub --after')
414
self.assertPathExists('a1')
415
self.assertPathExists('a2')
416
self.assertPathExists('sub/a1')
417
self.assertPathExists('sub/a2')
418
self.assertInWorkingTree('sub/a1')
419
self.assertInWorkingTree('sub/a2')
421
def test_mv_already_moved_directory(self):
422
"""Use `bzr mv a b` to mark a directory as renamed.
424
https://bugs.launchpad.net/bzr/+bug/107967/
426
self.build_tree(['a/', 'c/'])
427
tree = self.make_branch_and_tree('.')
429
osutils.rename('a', 'b')
430
osutils.rename('c', 'd')
431
# mv a b should work just like it does for already renamed files
432
self.run_bzr('mv a b')
433
self.assertPathDoesNotExist('a')
434
self.assertNotInWorkingTree('a')
435
self.assertPathExists('b')
436
self.assertInWorkingTree('b')
437
# and --after should work, too (technically it's ignored)
438
self.run_bzr('mv --after c d')
439
self.assertPathDoesNotExist('c')
440
self.assertNotInWorkingTree('c')
441
self.assertPathExists('d')
442
self.assertInWorkingTree('d')
444
def make_abcd_tree(self):
445
tree = self.make_branch_and_tree('tree')
446
self.build_tree(['tree/a', 'tree/c'])
448
tree.commit('record old names')
449
osutils.rename('tree/a', 'tree/b')
450
osutils.rename('tree/c', 'tree/d')
453
def test_mv_auto(self):
454
self.make_abcd_tree()
455
out, err = self.run_bzr('mv --auto', working_dir='tree')
456
self.assertEqual(out, '')
457
self.assertEqual(err, 'a => b\nc => d\n')
458
tree = workingtree.WorkingTree.open('tree')
459
self.assertIsNot(None, tree.path2id('b'))
460
self.assertIsNot(None, tree.path2id('d'))
462
def test_mv_auto_one_path(self):
463
self.make_abcd_tree()
464
out, err = self.run_bzr('mv --auto tree')
465
self.assertEqual(out, '')
466
self.assertEqual(err, 'a => b\nc => d\n')
467
tree = workingtree.WorkingTree.open('tree')
468
self.assertIsNot(None, tree.path2id('b'))
469
self.assertIsNot(None, tree.path2id('d'))
471
def test_mv_auto_two_paths(self):
472
self.make_abcd_tree()
473
out, err = self.run_bzr('mv --auto tree tree2', retcode=3)
474
self.assertEqual('bzr: ERROR: Only one path may be specified to'
477
def test_mv_auto_dry_run(self):
478
self.make_abcd_tree()
479
out, err = self.run_bzr('mv --auto --dry-run', working_dir='tree')
480
self.assertEqual(out, '')
481
self.assertEqual(err, 'a => b\nc => d\n')
482
tree = workingtree.WorkingTree.open('tree')
483
self.assertIsNot(None, tree.path2id('a'))
484
self.assertIsNot(None, tree.path2id('c'))
486
def test_mv_no_auto_dry_run(self):
487
self.make_abcd_tree()
488
out, err = self.run_bzr('mv c d --dry-run',
489
working_dir='tree', retcode=3)
490
self.assertEqual('bzr: ERROR: --dry-run requires --auto.\n', err)
492
def test_mv_auto_after(self):
493
self.make_abcd_tree()
494
out, err = self.run_bzr('mv --auto --after', working_dir='tree',
496
self.assertEqual('bzr: ERROR: --after cannot be specified with'
499
def test_mv_quiet(self):
500
tree = self.make_branch_and_tree('.')
501
self.build_tree(['aaa'])
503
out, err = self.run_bzr('mv --quiet aaa bbb')
504
self.assertEqual(out, '')
505
self.assertEqual(err, '')
507
def test_mv_readonly_lightweight_checkout(self):
508
branch = self.make_branch('foo')
509
branch = bzrlib.branch.Branch.open(self.get_readonly_url('foo'))
510
tree = branch.create_checkout('tree', lightweight=True)
511
self.build_tree(['tree/path'])
513
# If this fails, the tree is trying to acquire a branch lock, which it
515
self.run_bzr(['mv', 'tree/path', 'tree/path2'])
517
def test_mv_unversioned_non_ascii(self):
518
"""Clear error on mv of an unversioned non-ascii file, see lp:707954"""
519
self.requireFeature(UnicodeFilenameFeature)
520
tree = self.make_branch_and_tree(".")
521
self.build_tree([u"\xA7"])
522
out, err = self.run_bzr_error(["Could not rename", "not versioned"],
523
["mv", u"\xA7", "b"])