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
CaseInsensitiveFilesystemFeature,
30
TestCaseWithTransport,
24
34
class TestMove(TestCaseWithTransport):
36
def assertMoved(self,from_path,to_path):
37
"""Assert that to_path is existing and versioned but from_path not. """
38
self.failIfExists(from_path)
39
self.assertNotInWorkingTree(from_path)
41
self.failUnlessExists(to_path)
42
self.assertInWorkingTree(to_path)
26
44
def test_mv_modes(self):
27
45
"""Test two modes of operation for mv"""
28
46
tree = self.make_branch_and_tree('.')
29
47
files = self.build_tree(['a', 'c', 'subdir/'])
30
48
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')
50
self.run_bzr('mv a b')
51
self.assertMoved('a','b')
53
self.run_bzr('mv b subdir')
54
self.assertMoved('b','subdir/b')
56
self.run_bzr('mv subdir/b a')
57
self.assertMoved('subdir/b','a')
59
self.run_bzr('mv a c subdir')
60
self.assertMoved('a','subdir/a')
61
self.assertMoved('c','subdir/c')
63
self.run_bzr('mv subdir/a subdir/newa')
64
self.assertMoved('subdir/a','subdir/newa')
54
66
def test_mv_unversioned(self):
55
67
self.build_tree(['unversioned.txt'])
56
68
self.run_bzr_error(
57
["^bzr: ERROR: can't rename: old name .* is not versioned$"],
58
'mv', 'unversioned.txt', 'elsewhere')
69
["^bzr: ERROR: Could not rename unversioned.txt => elsewhere."
70
" .*unversioned.txt is not versioned\.$"],
71
'mv unversioned.txt elsewhere')
60
73
def test_mv_nonexisting(self):
61
74
self.run_bzr_error(
62
["^bzr: ERROR: can't rename: old working file .* does not exist$"],
63
'mv', 'doesnotexist', 'somewhereelse')
75
["^bzr: ERROR: Could not rename doesnotexist => somewhereelse."
76
" .*doesnotexist is not versioned\.$"],
77
'mv doesnotexist somewhereelse')
65
79
def test_mv_unqualified(self):
66
80
self.run_bzr_error(['^bzr: ERROR: missing file argument$'], 'mv')
68
82
def test_mv_invalid(self):
69
83
tree = self.make_branch_and_tree('.')
70
84
self.build_tree(['test.txt', 'sub1/'])
71
85
tree.add(['test.txt'])
73
87
self.run_bzr_error(
74
["^bzr: ERROR: destination u'sub1' is not a versioned directory$"],
75
'mv', 'test.txt', 'sub1')
88
["^bzr: ERROR: Could not move to sub1: sub1 is not versioned\.$"],
77
91
self.run_bzr_error(
78
["^bzr: ERROR: can't determine destination directory id for u'sub1'$"],
79
'mv', 'test.txt', 'sub1/hello.txt')
92
["^bzr: ERROR: Could not move test.txt => .*hello.txt: "
93
"sub1 is not versioned\.$"],
94
'mv test.txt sub1/hello.txt')
81
96
def test_mv_dirs(self):
82
97
tree = self.make_branch_and_tree('.')
83
98
self.build_tree(['hello.txt', 'sub1/'])
84
99
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")
101
self.run_bzr('mv sub1 sub2')
102
self.assertMoved('sub1','sub2')
93
tree.read_working_inventory()
94
tree.commit('commit with some things moved to subdirs')
104
self.run_bzr('mv hello.txt sub2')
105
self.assertMoved('hello.txt','sub2/hello.txt')
96
107
self.build_tree(['sub1/'])
97
108
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')
109
self.run_bzr('mv sub2/hello.txt sub1')
110
self.assertMoved('sub2/hello.txt','sub1/hello.txt')
112
self.run_bzr('mv sub2 sub1')
113
self.assertMoved('sub2','sub1/sub2')
105
115
def test_mv_relative(self):
106
116
self.build_tree(['sub1/', 'sub1/sub2/', 'sub1/hello.txt'])
107
117
tree = self.make_branch_and_tree('.')
108
118
tree.add(['sub1', 'sub1/sub2', 'sub1/hello.txt'])
109
tree.commit('initial tree')
111
120
os.chdir('sub1/sub2')
112
self.run_bzr('mv', '../hello.txt', '.')
121
self.run_bzr('mv ../hello.txt .')
113
122
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')
125
self.run_bzr('mv sub2/hello.txt .')
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',
122
177
def test_mv_smoke_aliases(self):
123
178
# just test that aliases for mv exist, if their behaviour is changed in
126
181
tree = self.make_branch_and_tree('.')
129
self.run_bzr('move', 'a', 'b')
130
self.run_bzr('rename', 'b', 'a')
184
self.run_bzr('move a b')
185
self.run_bzr('rename b a')
187
def test_mv_through_symlinks(self):
188
self.requireFeature(SymlinkFeature)
189
tree = self.make_branch_and_tree('.')
190
self.build_tree(['a/', 'a/b'])
193
tree.add(['a', 'a/b', 'c'], ['a-id', 'b-id', 'c-id'])
194
self.run_bzr('mv c/b b')
195
tree = workingtree.WorkingTree.open('.')
196
self.assertEqual('b-id', tree.path2id('b'))
198
def test_mv_already_moved_file(self):
199
"""Test bzr mv original_file to moved_file.
201
Tests if a file which has allready been moved by an external tool,
202
is handled correctly by bzr mv.
203
Setup: a is in the working tree, b does not exist.
204
User does: mv a b; bzr mv a b
206
self.build_tree(['a'])
207
tree = self.make_branch_and_tree('.')
210
osutils.rename('a', 'b')
211
self.run_bzr('mv a b')
212
self.assertMoved('a','b')
214
def test_mv_already_moved_file_to_versioned_target(self):
215
"""Test bzr mv existing_file to versioned_file.
217
Tests if an attempt to move an existing versioned file
218
to another versiond file will fail.
219
Setup: a and b are in the working tree.
220
User does: rm b; mv a b; bzr mv a b
222
self.build_tree(['a', 'b'])
223
tree = self.make_branch_and_tree('.')
227
osutils.rename('a', 'b')
229
["^bzr: ERROR: Could not move a => b. b is already versioned\.$"],
231
#check that nothing changed
232
self.failIfExists('a')
233
self.failUnlessExists('b')
235
def test_mv_already_moved_file_into_subdir(self):
236
"""Test bzr mv original_file to versioned_directory/file.
238
Tests if a file which has already been moved into a versioned
239
directory by an external tool, is handled correctly by bzr mv.
240
Setup: a and sub/ are in the working tree.
241
User does: mv a sub/a; bzr mv a sub/a
243
self.build_tree(['a', 'sub/'])
244
tree = self.make_branch_and_tree('.')
245
tree.add(['a', 'sub'])
247
osutils.rename('a', 'sub/a')
248
self.run_bzr('mv a sub/a')
249
self.assertMoved('a','sub/a')
251
def test_mv_already_moved_file_into_unversioned_subdir(self):
252
"""Test bzr mv original_file to unversioned_directory/file.
254
Tests if an attempt to move an existing versioned file
255
into an unversioned directory will fail.
256
Setup: a is in the working tree, sub/ is not.
257
User does: mv a sub/a; bzr mv a sub/a
259
self.build_tree(['a', 'sub/'])
260
tree = self.make_branch_and_tree('.')
263
osutils.rename('a', 'sub/a')
265
["^bzr: ERROR: Could not move a => a: sub is not versioned\.$"],
267
self.failIfExists('a')
268
self.failUnlessExists('sub/a')
270
def test_mv_already_moved_files_into_subdir(self):
271
"""Test bzr mv original_files to versioned_directory.
273
Tests if files which has already been moved into a versioned
274
directory by an external tool, is handled correctly by bzr mv.
275
Setup: a1, a2, sub are in the working tree.
276
User does: mv a1 sub/.; bzr mv a1 a2 sub
278
self.build_tree(['a1', 'a2', 'sub/'])
279
tree = self.make_branch_and_tree('.')
280
tree.add(['a1', 'a2', 'sub'])
282
osutils.rename('a1', 'sub/a1')
283
self.run_bzr('mv a1 a2 sub')
284
self.assertMoved('a1','sub/a1')
285
self.assertMoved('a2','sub/a2')
287
def test_mv_already_moved_files_into_unversioned_subdir(self):
288
"""Test bzr mv original_file to unversioned_directory.
290
Tests if an attempt to move existing versioned file
291
into an unversioned directory will fail.
292
Setup: a1, a2 are in the working tree, sub is not.
293
User does: mv a1 sub/.; bzr mv a1 a2 sub
295
self.build_tree(['a1', 'a2', 'sub/'])
296
tree = self.make_branch_and_tree('.')
297
tree.add(['a1', 'a2'])
299
osutils.rename('a1', 'sub/a1')
301
["^bzr: ERROR: Could not move to sub. sub is not versioned\.$"],
303
self.failIfExists('a1')
304
self.failUnlessExists('sub/a1')
305
self.failUnlessExists('a2')
306
self.failIfExists('sub/a2')
308
def test_mv_already_moved_file_forcing_after(self):
309
"""Test bzr mv versioned_file to unversioned_file.
311
Tests if an attempt to move an existing versioned file to an existing
312
unversioned file will fail, informing the user to use the --after
313
option to force this.
314
Setup: a is in the working tree, b not versioned.
315
User does: mv a b; touch a; bzr mv a b
317
self.build_tree(['a', 'b'])
318
tree = self.make_branch_and_tree('.')
321
osutils.rename('a', 'b')
322
self.build_tree(['a']) #touch a
324
["^bzr: ERROR: Could not rename a => b because both files exist."
325
" \(Use --after to tell bzr about a rename that has already"
328
self.failUnlessExists('a')
329
self.failUnlessExists('b')
331
def test_mv_already_moved_file_using_after(self):
332
"""Test bzr mv --after versioned_file to unversioned_file.
334
Tests if an existing versioned file can be forced to move to an
335
existing unversioned file using the --after option. With the result
336
that bazaar considers the unversioned_file to be moved from
337
versioned_file and versioned_file will become unversioned.
338
Setup: a is in the working tree and b exists.
339
User does: mv a b; touch a; bzr mv a b --after
340
Resulting in a => b and a is unknown.
342
self.build_tree(['a', 'b'])
343
tree = self.make_branch_and_tree('.')
345
osutils.rename('a', 'b')
346
self.build_tree(['a']) #touch a
348
self.run_bzr('mv a b --after')
349
self.failUnlessExists('a')
350
self.assertNotInWorkingTree('a')#a should be unknown now.
351
self.failUnlessExists('b')
352
self.assertInWorkingTree('b')
354
def test_mv_already_moved_files_forcing_after(self):
355
"""Test bzr mv versioned_files to directory/unversioned_file.
357
Tests if an attempt to move an existing versioned file to an existing
358
unversioned file in some other directory will fail, informing the user
359
to use the --after option to force this.
361
Setup: a1, a2, sub are versioned and in the working tree,
362
sub/a1, sub/a2 are in working tree.
363
User does: mv a* sub; touch a1; touch a2; bzr mv a1 a2 sub
365
self.build_tree(['a1', 'a2', 'sub/', 'sub/a1', 'sub/a2'])
366
tree = self.make_branch_and_tree('.')
367
tree.add(['a1', 'a2', 'sub'])
368
osutils.rename('a1', 'sub/a1')
369
osutils.rename('a2', 'sub/a2')
370
self.build_tree(['a1']) #touch a1
371
self.build_tree(['a2']) #touch a2
374
["^bzr: ERROR: Could not rename a1 => sub/a1 because both files"
375
" exist. \(Use --after to tell bzr about a rename that has already"
378
self.failUnlessExists('a1')
379
self.failUnlessExists('a2')
380
self.failUnlessExists('sub/a1')
381
self.failUnlessExists('sub/a2')
383
def test_mv_already_moved_files_using_after(self):
384
"""Test bzr mv --after versioned_file to directory/unversioned_file.
386
Tests if an existing versioned file can be forced to move to an
387
existing unversioned file in some other directory using the --after
388
option. With the result that bazaar considers
389
directory/unversioned_file to be moved from versioned_file and
390
versioned_file will become unversioned.
392
Setup: a1, a2, sub are versioned and in the working tree,
393
sub/a1, sub/a2 are in working tree.
394
User does: mv a* sub; touch a1; touch a2; bzr mv a1 a2 sub --after
396
self.build_tree(['a1', 'a2', 'sub/', 'sub/a1', 'sub/a2'])
397
tree = self.make_branch_and_tree('.')
398
tree.add(['a1', 'a2', 'sub'])
399
osutils.rename('a1', 'sub/a1')
400
osutils.rename('a2', 'sub/a2')
401
self.build_tree(['a1']) #touch a1
402
self.build_tree(['a2']) #touch a2
404
self.run_bzr('mv a1 a2 sub --after')
405
self.failUnlessExists('a1')
406
self.failUnlessExists('a2')
407
self.failUnlessExists('sub/a1')
408
self.failUnlessExists('sub/a2')
409
self.assertInWorkingTree('sub/a1')
410
self.assertInWorkingTree('sub/a2')
412
def test_mv_already_moved_directory(self):
413
"""Use `bzr mv a b` to mark a directory as renamed.
415
https://bugs.launchpad.net/bzr/+bug/107967/
417
self.build_tree(['a/', 'c/'])
418
tree = self.make_branch_and_tree('.')
420
osutils.rename('a', 'b')
421
osutils.rename('c', 'd')
422
# mv a b should work just like it does for already renamed files
423
self.run_bzr('mv a b')
424
self.failIfExists('a')
425
self.assertNotInWorkingTree('a')
426
self.failUnlessExists('b')
427
self.assertInWorkingTree('b')
428
# and --after should work, too (technically it's ignored)
429
self.run_bzr('mv --after c d')
430
self.failIfExists('c')
431
self.assertNotInWorkingTree('c')
432
self.failUnlessExists('d')
433
self.assertInWorkingTree('d')
435
def make_abcd_tree(self):
436
tree = self.make_branch_and_tree('tree')
437
self.build_tree(['tree/a', 'tree/c'])
439
tree.commit('record old names')
440
osutils.rename('tree/a', 'tree/b')
441
osutils.rename('tree/c', 'tree/d')
444
def test_mv_auto(self):
445
self.make_abcd_tree()
446
out, err = self.run_bzr('mv --auto', working_dir='tree')
447
self.assertEqual(out, '')
448
self.assertEqual(err, 'a => b\nc => d\n')
449
tree = workingtree.WorkingTree.open('tree')
450
self.assertIsNot(None, tree.path2id('b'))
451
self.assertIsNot(None, tree.path2id('d'))
453
def test_mv_auto_one_path(self):
454
self.make_abcd_tree()
455
out, err = self.run_bzr('mv --auto 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_two_paths(self):
463
self.make_abcd_tree()
464
out, err = self.run_bzr('mv --auto tree tree2', retcode=3)
465
self.assertEqual('bzr: ERROR: Only one path may be specified to'
468
def test_mv_auto_dry_run(self):
469
self.make_abcd_tree()
470
out, err = self.run_bzr('mv --auto --dry-run', working_dir='tree')
471
self.assertEqual(out, '')
472
self.assertEqual(err, 'a => b\nc => d\n')
473
tree = workingtree.WorkingTree.open('tree')
474
self.assertIsNot(None, tree.path2id('a'))
475
self.assertIsNot(None, tree.path2id('c'))
477
def test_mv_no_auto_dry_run(self):
478
self.make_abcd_tree()
479
out, err = self.run_bzr('mv c d --dry-run',
480
working_dir='tree', retcode=3)
481
self.assertEqual('bzr: ERROR: --dry-run requires --auto.\n', err)
483
def test_mv_auto_after(self):
484
self.make_abcd_tree()
485
out, err = self.run_bzr('mv --auto --after', working_dir='tree',
487
self.assertEqual('bzr: ERROR: --after cannot be specified with'
490
def test_mv_quiet(self):
491
tree = self.make_branch_and_tree('.')
492
self.build_tree(['aaa'])
494
out, err = self.run_bzr('mv --quiet aaa bbb')
495
self.assertEqual(out, '')
496
self.assertEqual(err, '')
498
def test_mv_readonly_lightweight_checkout(self):
499
branch = self.make_branch('foo')
500
branch = bzrlib.branch.Branch.open(self.get_readonly_url('foo'))
501
tree = branch.create_checkout('tree', lightweight=True)
502
self.build_tree(['tree/path'])
504
# If this fails, the tree is trying to acquire a branch lock, which it
506
self.run_bzr(['mv', 'tree/path', 'tree/path2'])