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'"""
25
from bzrlib.tests import (
26
TestCaseWithTransport,
29
from bzrlib.osutils import (
33
class TestMove(TestCaseWithTransport):
35
def assertInWorkingTree(self,path):
36
tree = workingtree.WorkingTree.open('.')
37
self.assertIsNot(tree.path2id(path), None,
38
path+' not in working tree.')
40
def assertNotInWorkingTree(self,path):
41
tree = workingtree.WorkingTree.open('.')
42
self.assertIs(tree.path2id(path), None, path+' in working tree.')
44
def assertMoved(self,from_path,to_path):
45
"""Assert that to_path is existing and versioned but from_path not. """
46
self.failIfExists(from_path)
47
self.assertNotInWorkingTree(from_path)
49
self.failUnlessExists(to_path)
50
self.assertInWorkingTree(to_path)
52
def test_mv_modes(self):
53
"""Test two modes of operation for mv"""
54
tree = self.make_branch_and_tree('.')
55
files = self.build_tree(['a', 'c', 'subdir/'])
56
tree.add(['a', 'c', 'subdir'])
58
self.run_bzr('mv', 'a', 'b')
59
self.assertMoved('a','b')
61
self.run_bzr('mv', 'b', 'subdir')
62
self.assertMoved('b','subdir/b')
64
self.run_bzr('mv', 'subdir/b', 'a')
65
self.assertMoved('subdir/b','a')
67
self.run_bzr('mv', 'a', 'c', 'subdir')
68
self.assertMoved('a','subdir/a')
69
self.assertMoved('c','subdir/c')
71
self.run_bzr('mv', 'subdir/a', 'subdir/newa')
72
self.assertMoved('subdir/a','subdir/newa')
74
def test_mv_unversioned(self):
75
self.build_tree(['unversioned.txt'])
77
["^bzr: ERROR: Could not rename unversioned.txt => elsewhere."
78
" .*unversioned.txt is not versioned$"],
79
'mv', 'unversioned.txt', 'elsewhere')
81
def test_mv_nonexisting(self):
83
["^bzr: ERROR: Could not rename doesnotexist => somewhereelse."
84
" .*doesnotexist is not versioned$"],
85
'mv', 'doesnotexist', 'somewhereelse')
87
def test_mv_unqualified(self):
88
self.run_bzr_error(['^bzr: ERROR: missing file argument$'], 'mv')
90
def test_mv_invalid(self):
91
tree = self.make_branch_and_tree('.')
92
self.build_tree(['test.txt', 'sub1/'])
93
tree.add(['test.txt'])
96
["^bzr: ERROR: Could not move to sub1: sub1 is not versioned$"],
97
'mv', 'test.txt', 'sub1')
100
["^bzr: ERROR: Could not move test.txt => .*hello.txt: "
101
"sub1 is not versioned$"],
102
'mv', 'test.txt', 'sub1/hello.txt')
104
def test_mv_dirs(self):
105
tree = self.make_branch_and_tree('.')
106
self.build_tree(['hello.txt', 'sub1/'])
107
tree.add(['hello.txt', 'sub1'])
109
self.run_bzr('mv', 'sub1', 'sub2')
110
self.assertMoved('sub1','sub2')
112
self.run_bzr('mv', 'hello.txt', 'sub2')
113
self.assertMoved('hello.txt','sub2/hello.txt')
115
tree.read_working_inventory()
117
self.build_tree(['sub1/'])
119
self.run_bzr('mv', 'sub2/hello.txt', 'sub1')
120
self.assertMoved('sub2/hello.txt','sub1/hello.txt')
122
self.run_bzr('mv', 'sub2', 'sub1')
123
self.assertMoved('sub2','sub1/sub2')
125
def test_mv_relative(self):
126
self.build_tree(['sub1/', 'sub1/sub2/', 'sub1/hello.txt'])
127
tree = self.make_branch_and_tree('.')
128
tree.add(['sub1', 'sub1/sub2', 'sub1/hello.txt'])
130
os.chdir('sub1/sub2')
131
self.run_bzr('mv', '../hello.txt', '.')
132
self.failUnlessExists('./hello.txt')
133
tree.read_working_inventory()
136
self.run_bzr('mv', 'sub2/hello.txt', '.')
138
self.assertMoved('sub1/sub2/hello.txt','sub1/hello.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
if not osutils.has_symlinks():
152
raise TestSkipped('Symlinks are not supported on this platform')
153
tree = self.make_branch_and_tree('.')
154
self.build_tree(['a/', 'a/b'])
157
tree.add(['a', 'a/b', 'c'], ['a-id', 'b-id', 'c-id'])
158
self.run_bzr('mv', 'c/b', 'b')
159
tree = workingtree.WorkingTree.open('.')
160
self.assertEqual('b-id', tree.path2id('b'))
162
def test_mv_already_moved_file(self):
163
"""Test bzr mv original_file to moved_file.
165
Tests if a file which has allready been moved by an external tool,
166
is handled correctly by bzr mv.
167
Setup: a is in the working tree, b does not exist.
168
User does: mv a b; bzr mv a b
170
self.build_tree(['a'])
171
tree = self.make_branch_and_tree('.')
175
self.run_bzr('mv', 'a', 'b')
176
self.assertMoved('a','b')
178
def test_mv_already_moved_file_to_versioned_target(self):
179
"""Test bzr mv existing_file to versioned_file.
181
Tests if an attempt to move an existing versioned file
182
to another versiond file will fail.
183
Setup: a and b are in the working tree.
184
User does: rm b; mv a b; bzr mv a b
186
self.build_tree(['a', 'b'])
187
tree = self.make_branch_and_tree('.')
193
["^bzr: ERROR: Could not move a => b. b is already versioned$"],
195
#check that nothing changed
196
self.failIfExists('a')
197
self.failUnlessExists('b')
199
def test_mv_already_moved_file_into_subdir(self):
200
"""Test bzr mv original_file to versioned_directory/file.
202
Tests if a file which has already been moved into a versioned
203
directory by an external tool, is handled correctly by bzr mv.
204
Setup: a and sub/ are in the working tree.
205
User does: mv a sub/a; bzr mv a sub/a
207
self.build_tree(['a', 'sub/'])
208
tree = self.make_branch_and_tree('.')
209
tree.add(['a', 'sub'])
211
os.rename('a', 'sub/a')
212
self.run_bzr('mv', 'a', 'sub/a')
213
self.assertMoved('a','sub/a')
215
def test_mv_already_moved_file_into_unversioned_subdir(self):
216
"""Test bzr mv original_file to unversioned_directory/file.
218
Tests if an attempt to move an existing versioned file
219
into an unversioned directory will fail.
220
Setup: a is in the working tree, sub/ is not.
221
User does: mv a sub/a; bzr mv a sub/a
223
self.build_tree(['a', 'sub/'])
224
tree = self.make_branch_and_tree('.')
227
os.rename('a', 'sub/a')
229
["^bzr: ERROR: Could not move a => a: sub is not versioned$"],
231
self.failIfExists('a')
232
self.failUnlessExists('sub/a')
234
def test_mv_already_moved_files_into_subdir(self):
235
"""Test bzr mv original_files to versioned_directory.
237
Tests if files which has already been moved into a versioned
238
directory by an external tool, is handled correctly by bzr mv.
239
Setup: a1, a2, sub are in the working tree.
240
User does: mv a1 sub/.; bzr mv a1 a2 sub
242
self.build_tree(['a1', 'a2', 'sub/'])
243
tree = self.make_branch_and_tree('.')
244
tree.add(['a1', 'a2', 'sub'])
246
os.rename('a1', 'sub/a1')
247
self.run_bzr('mv', 'a1', 'a2', 'sub')
248
self.assertMoved('a1','sub/a1')
249
self.assertMoved('a2','sub/a2')
251
def test_mv_already_moved_files_into_unversioned_subdir(self):
252
"""Test bzr mv original_file to unversioned_directory.
254
Tests if an attempt to move existing versioned file
255
into an unversioned directory will fail.
256
Setup: a1, a2 are in the working tree, sub is not.
257
User does: mv a1 sub/.; bzr mv a1 a2 sub
259
self.build_tree(['a1', 'a2', 'sub/'])
260
tree = self.make_branch_and_tree('.')
261
tree.add(['a1', 'a2'])
263
os.rename('a1', 'sub/a1')
265
["^bzr: ERROR: Could not move to sub. sub is not versioned$"],
266
'mv', 'a1', 'a2', 'sub')
267
self.failIfExists('a1')
268
self.failUnlessExists('sub/a1')
269
self.failUnlessExists('a2')
270
self.failIfExists('sub/a2')
272
def test_mv_already_moved_file_forcing_after(self):
273
"""Test bzr mv versioned_file to unversioned_file.
275
Tests if an attempt to move an existing versioned file to an existing
276
unversioned file will fail, informing the user to use the --after
277
option to force this.
278
Setup: a is in the working tree, b not versioned.
279
User does: mv a b; touch a; bzr mv a b
281
self.build_tree(['a', 'b'])
282
tree = self.make_branch_and_tree('.')
286
self.build_tree(['a']) #touch a
288
["^bzr: ERROR: Could not rename a => b because both files exist."
289
" \(Use --after to update the Bazaar id\)$"],
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('.')
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
os.rename('a1', 'sub/a1')
332
os.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 exist."
338
" \(Use --after to update the Bazaar id\)$"],
339
'mv', 'a1', 'a2', 'sub')
340
self.failUnlessExists('a1')
341
self.failUnlessExists('a2')
342
self.failUnlessExists('sub/a1')
343
self.failUnlessExists('sub/a2')
345
def test_mv_already_moved_files_using_after(self):
346
"""Test bzr mv --after versioned_file to directory/unversioned_file.
348
Tests if an existing versioned file can be forced to move to an
349
existing unversioned file in some other directory using the --after
350
option. With the result that bazaar considers
351
directory/unversioned_file to be moved from versioned_file and
352
versioned_file will become unversioned.
354
Setup: a1, a2, sub are versioned and in the working tree,
355
sub/a1, sub/a2 are in working tree.
356
User does: mv a* sub; touch a1; touch a2; bzr mv a1 a2 sub --after
358
self.build_tree(['a1', 'a2', 'sub/', 'sub/a1', 'sub/a2'])
359
tree = self.make_branch_and_tree('.')
360
tree.add(['a1', 'a2', 'sub'])
361
os.rename('a1', 'sub/a1')
362
os.rename('a2', 'sub/a2')
363
self.build_tree(['a1']) #touch a1
364
self.build_tree(['a2']) #touch a2
366
self.run_bzr('mv', 'a1', 'a2', 'sub', '--after')
367
self.failUnlessExists('a1')
368
self.failUnlessExists('a2')
369
self.failUnlessExists('sub/a1')
370
self.failUnlessExists('sub/a2')
371
self.assertInWorkingTree('sub/a1')
372
self.assertInWorkingTree('sub/a2')
b'\\ No newline at end of file'