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 (
28
TestCaseWithTransport,
32
class TestMove(TestCaseWithTransport):
34
def assertMoved(self,from_path,to_path):
35
"""Assert that to_path is existing and versioned but from_path not. """
36
self.failIfExists(from_path)
37
self.assertNotInWorkingTree(from_path)
39
self.failUnlessExists(to_path)
40
self.assertInWorkingTree(to_path)
42
def test_mv_modes(self):
43
"""Test two modes of operation for mv"""
44
tree = self.make_branch_and_tree('.')
45
files = self.build_tree(['a', 'c', 'subdir/'])
46
tree.add(['a', 'c', 'subdir'])
48
self.run_bzr('mv a b')
49
self.assertMoved('a','b')
51
self.run_bzr('mv b subdir')
52
self.assertMoved('b','subdir/b')
54
self.run_bzr('mv subdir/b a')
55
self.assertMoved('subdir/b','a')
57
self.run_bzr('mv a c subdir')
58
self.assertMoved('a','subdir/a')
59
self.assertMoved('c','subdir/c')
61
self.run_bzr('mv subdir/a subdir/newa')
62
self.assertMoved('subdir/a','subdir/newa')
64
def test_mv_unversioned(self):
65
self.build_tree(['unversioned.txt'])
67
["^bzr: ERROR: Could not rename unversioned.txt => elsewhere."
68
" .*unversioned.txt is not versioned\.$"],
69
'mv unversioned.txt elsewhere')
71
def test_mv_nonexisting(self):
73
["^bzr: ERROR: Could not rename doesnotexist => somewhereelse."
74
" .*doesnotexist is not versioned\.$"],
75
'mv doesnotexist somewhereelse')
77
def test_mv_unqualified(self):
78
self.run_bzr_error(['^bzr: ERROR: missing file argument$'], 'mv')
80
def test_mv_invalid(self):
81
tree = self.make_branch_and_tree('.')
82
self.build_tree(['test.txt', 'sub1/'])
83
tree.add(['test.txt'])
86
["^bzr: ERROR: Could not move to sub1: sub1 is not versioned\.$"],
90
["^bzr: ERROR: Could not move test.txt => .*hello.txt: "
91
"sub1 is not versioned\.$"],
92
'mv test.txt sub1/hello.txt')
94
def test_mv_dirs(self):
95
tree = self.make_branch_and_tree('.')
96
self.build_tree(['hello.txt', 'sub1/'])
97
tree.add(['hello.txt', 'sub1'])
99
self.run_bzr('mv sub1 sub2')
100
self.assertMoved('sub1','sub2')
102
self.run_bzr('mv hello.txt sub2')
103
self.assertMoved('hello.txt','sub2/hello.txt')
105
self.build_tree(['sub1/'])
107
self.run_bzr('mv sub2/hello.txt sub1')
108
self.assertMoved('sub2/hello.txt','sub1/hello.txt')
110
self.run_bzr('mv sub2 sub1')
111
self.assertMoved('sub2','sub1/sub2')
113
def test_mv_relative(self):
114
self.build_tree(['sub1/', 'sub1/sub2/', 'sub1/hello.txt'])
115
tree = self.make_branch_and_tree('.')
116
tree.add(['sub1', 'sub1/sub2', 'sub1/hello.txt'])
118
os.chdir('sub1/sub2')
119
self.run_bzr('mv ../hello.txt .')
120
self.failUnlessExists('./hello.txt')
123
self.run_bzr('mv sub2/hello.txt .')
125
self.assertMoved('sub1/sub2/hello.txt','sub1/hello.txt')
127
def test_mv_change_case(self):
128
# test for bug #77740 (mv unable change filename case on Windows)
129
tree = self.make_branch_and_tree('.')
130
self.build_tree(['test.txt'])
131
tree.add(['test.txt'])
132
self.run_bzr('mv test.txt Test.txt')
133
# we can't use failUnlessExists on case-insensitive filesystem
134
# so try to check shape of the tree
135
shape = sorted(os.listdir(u'.'))
136
self.assertEqual(['.bzr', 'Test.txt'], shape)
137
self.assertInWorkingTree('Test.txt')
138
self.assertNotInWorkingTree('test.txt')
140
def test_mv_smoke_aliases(self):
141
# just test that aliases for mv exist, if their behaviour is changed in
142
# the future, then extend the tests.
143
self.build_tree(['a'])
144
tree = self.make_branch_and_tree('.')
147
self.run_bzr('move a b')
148
self.run_bzr('rename b a')
150
def test_mv_through_symlinks(self):
151
self.requireFeature(SymlinkFeature)
152
tree = self.make_branch_and_tree('.')
153
self.build_tree(['a/', 'a/b'])
156
tree.add(['a', 'a/b', 'c'], ['a-id', 'b-id', 'c-id'])
157
self.run_bzr('mv c/b b')
158
tree = workingtree.WorkingTree.open('.')
159
self.assertEqual('b-id', tree.path2id('b'))
161
def test_mv_already_moved_file(self):
162
"""Test bzr mv original_file to moved_file.
164
Tests if a file which has allready been moved by an external tool,
165
is handled correctly by bzr mv.
166
Setup: a is in the working tree, b does not exist.
167
User does: mv a b; bzr mv a b
169
self.build_tree(['a'])
170
tree = self.make_branch_and_tree('.')
173
osutils.rename('a', 'b')
174
self.run_bzr('mv a b')
175
self.assertMoved('a','b')
177
def test_mv_already_moved_file_to_versioned_target(self):
178
"""Test bzr mv existing_file to versioned_file.
180
Tests if an attempt to move an existing versioned file
181
to another versiond file will fail.
182
Setup: a and b are in the working tree.
183
User does: rm b; mv a b; bzr mv a b
185
self.build_tree(['a', 'b'])
186
tree = self.make_branch_and_tree('.')
190
osutils.rename('a', 'b')
192
["^bzr: ERROR: Could not move a => b. b is already versioned\.$"],
194
#check that nothing changed
195
self.failIfExists('a')
196
self.failUnlessExists('b')
198
def test_mv_already_moved_file_into_subdir(self):
199
"""Test bzr mv original_file to versioned_directory/file.
201
Tests if a file which has already been moved into a versioned
202
directory by an external tool, is handled correctly by bzr mv.
203
Setup: a and sub/ are in the working tree.
204
User does: mv a sub/a; bzr mv a sub/a
206
self.build_tree(['a', 'sub/'])
207
tree = self.make_branch_and_tree('.')
208
tree.add(['a', 'sub'])
210
osutils.rename('a', 'sub/a')
211
self.run_bzr('mv a sub/a')
212
self.assertMoved('a','sub/a')
214
def test_mv_already_moved_file_into_unversioned_subdir(self):
215
"""Test bzr mv original_file to unversioned_directory/file.
217
Tests if an attempt to move an existing versioned file
218
into an unversioned directory will fail.
219
Setup: a is in the working tree, sub/ is not.
220
User does: mv a sub/a; bzr mv a sub/a
222
self.build_tree(['a', 'sub/'])
223
tree = self.make_branch_and_tree('.')
226
osutils.rename('a', 'sub/a')
228
["^bzr: ERROR: Could not move a => a: sub is not versioned\.$"],
230
self.failIfExists('a')
231
self.failUnlessExists('sub/a')
233
def test_mv_already_moved_files_into_subdir(self):
234
"""Test bzr mv original_files to versioned_directory.
236
Tests if files which has already been moved into a versioned
237
directory by an external tool, is handled correctly by bzr mv.
238
Setup: a1, a2, sub are in the working tree.
239
User does: mv a1 sub/.; bzr mv a1 a2 sub
241
self.build_tree(['a1', 'a2', 'sub/'])
242
tree = self.make_branch_and_tree('.')
243
tree.add(['a1', 'a2', 'sub'])
245
osutils.rename('a1', 'sub/a1')
246
self.run_bzr('mv a1 a2 sub')
247
self.assertMoved('a1','sub/a1')
248
self.assertMoved('a2','sub/a2')
250
def test_mv_already_moved_files_into_unversioned_subdir(self):
251
"""Test bzr mv original_file to unversioned_directory.
253
Tests if an attempt to move existing versioned file
254
into an unversioned directory will fail.
255
Setup: a1, a2 are in the working tree, sub is not.
256
User does: mv a1 sub/.; bzr mv a1 a2 sub
258
self.build_tree(['a1', 'a2', 'sub/'])
259
tree = self.make_branch_and_tree('.')
260
tree.add(['a1', 'a2'])
262
osutils.rename('a1', 'sub/a1')
264
["^bzr: ERROR: Could not move to sub. sub is not versioned\.$"],
266
self.failIfExists('a1')
267
self.failUnlessExists('sub/a1')
268
self.failUnlessExists('a2')
269
self.failIfExists('sub/a2')
271
def test_mv_already_moved_file_forcing_after(self):
272
"""Test bzr mv versioned_file to unversioned_file.
274
Tests if an attempt to move an existing versioned file to an existing
275
unversioned file will fail, informing the user to use the --after
276
option to force this.
277
Setup: a is in the working tree, b not versioned.
278
User does: mv a b; touch a; bzr mv a b
280
self.build_tree(['a', 'b'])
281
tree = self.make_branch_and_tree('.')
284
osutils.rename('a', 'b')
285
self.build_tree(['a']) #touch a
287
["^bzr: ERROR: Could not rename a => b because both files exist."
288
" \(Use --after to tell bzr about a rename that has already"
291
self.failUnlessExists('a')
292
self.failUnlessExists('b')
294
def test_mv_already_moved_file_using_after(self):
295
"""Test bzr mv --after versioned_file to unversioned_file.
297
Tests if an existing versioned file can be forced to move to an
298
existing unversioned file using the --after option. With the result
299
that bazaar considers the unversioned_file to be moved from
300
versioned_file and versioned_file will become unversioned.
301
Setup: a is in the working tree and b exists.
302
User does: mv a b; touch a; bzr mv a b --after
303
Resulting in a => b and a is unknown.
305
self.build_tree(['a', 'b'])
306
tree = self.make_branch_and_tree('.')
308
osutils.rename('a', 'b')
309
self.build_tree(['a']) #touch a
311
self.run_bzr('mv a b --after')
312
self.failUnlessExists('a')
313
self.assertNotInWorkingTree('a')#a should be unknown now.
314
self.failUnlessExists('b')
315
self.assertInWorkingTree('b')
317
def test_mv_already_moved_files_forcing_after(self):
318
"""Test bzr mv versioned_files to directory/unversioned_file.
320
Tests if an attempt to move an existing versioned file to an existing
321
unversioned file in some other directory will fail, informing the user
322
to use the --after option to force this.
324
Setup: a1, a2, sub are versioned and in the working tree,
325
sub/a1, sub/a2 are in working tree.
326
User does: mv a* sub; touch a1; touch a2; bzr mv a1 a2 sub
328
self.build_tree(['a1', 'a2', 'sub/', 'sub/a1', 'sub/a2'])
329
tree = self.make_branch_and_tree('.')
330
tree.add(['a1', 'a2', 'sub'])
331
osutils.rename('a1', 'sub/a1')
332
osutils.rename('a2', 'sub/a2')
333
self.build_tree(['a1']) #touch a1
334
self.build_tree(['a2']) #touch a2
337
["^bzr: ERROR: Could not rename a1 => sub/a1 because both files"
338
" exist. \(Use --after to tell bzr about a rename that has already"
341
self.failUnlessExists('a1')
342
self.failUnlessExists('a2')
343
self.failUnlessExists('sub/a1')
344
self.failUnlessExists('sub/a2')
346
def test_mv_already_moved_files_using_after(self):
347
"""Test bzr mv --after versioned_file to directory/unversioned_file.
349
Tests if an existing versioned file can be forced to move to an
350
existing unversioned file in some other directory using the --after
351
option. With the result that bazaar considers
352
directory/unversioned_file to be moved from versioned_file and
353
versioned_file will become unversioned.
355
Setup: a1, a2, sub are versioned and in the working tree,
356
sub/a1, sub/a2 are in working tree.
357
User does: mv a* sub; touch a1; touch a2; bzr mv a1 a2 sub --after
359
self.build_tree(['a1', 'a2', 'sub/', 'sub/a1', 'sub/a2'])
360
tree = self.make_branch_and_tree('.')
361
tree.add(['a1', 'a2', 'sub'])
362
osutils.rename('a1', 'sub/a1')
363
osutils.rename('a2', 'sub/a2')
364
self.build_tree(['a1']) #touch a1
365
self.build_tree(['a2']) #touch a2
367
self.run_bzr('mv a1 a2 sub --after')
368
self.failUnlessExists('a1')
369
self.failUnlessExists('a2')
370
self.failUnlessExists('sub/a1')
371
self.failUnlessExists('sub/a2')
372
self.assertInWorkingTree('sub/a1')
373
self.assertInWorkingTree('sub/a2')