Rev 2415: Add more tests for WorkingTree.move() and a similar suite in http://bazaar.launchpad.net/%7Ebzr/bzr/dirstate
John Arbash Meinel
john at arbash-meinel.com
Mon Feb 26 16:20:06 GMT 2007
At http://bazaar.launchpad.net/%7Ebzr/bzr/dirstate
------------------------------------------------------------
revno: 2415
revision-id: john at arbash-meinel.com-20070226161902-onqljoh62n5r80gh
parent: john at arbash-meinel.com-20070226152717-3so5kz4v7wz7dmpk
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: dirstate
timestamp: Mon 2007-02-26 10:19:02 -0600
message:
Add more tests for WorkingTree.move() and a similar suite
for WorkingTree.rename_one().
added:
bzrlib/tests/workingtree_implementations/test_rename_one.py test_rename_one.py-20070226161242-2d8ibdedl700jgio-1
modified:
bzrlib/tests/workingtree_implementations/__init__.py __init__.py-20060203003124-b2aa5aca21a8bfad
bzrlib/tests/workingtree_implementations/test_move.py test_move.py-20070225171927-mohn2vqj5fx7edc6-1
-------------- next part --------------
=== added file 'bzrlib/tests/workingtree_implementations/test_rename_one.py'
--- a/bzrlib/tests/workingtree_implementations/test_rename_one.py 1970-01-01 00:00:00 +0000
+++ b/bzrlib/tests/workingtree_implementations/test_rename_one.py 2007-02-26 16:19:02 +0000
@@ -0,0 +1,310 @@
+# Copyright (C) 2007 Canonical Ltd
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+"""Tests for interface conformance of 'WorkingTree.rename_one'"""
+
+import os
+
+from bzrlib import (
+ errors,
+ osutils,
+ )
+
+from bzrlib.workingtree_4 import WorkingTreeFormat4
+from bzrlib.tests.workingtree_implementations import TestCaseWithWorkingTree
+
+
+class TestRenameOne(TestCaseWithWorkingTree):
+
+ def get_tree_layout(self, tree):
+ """Get the (path, file_id) pairs for the current tree."""
+ tree.lock_read()
+ try:
+ return [(path, ie.file_id) for path, ie
+ in tree.iter_entries_by_dir()]
+ finally:
+ tree.unlock()
+
+ def assertTreeLayout(self, expected, tree):
+ """Check that the tree has the correct layout."""
+ actual = self.get_tree_layout(tree)
+ self.assertEqual(expected, actual)
+
+ def test_rename_one_target_not_dir(self):
+ tree = self.make_branch_and_tree('.')
+ self.build_tree(['a'])
+ tree.add(['a'])
+ tree.commit('initial', rev_id='rev-1')
+
+ self.assertRaises(errors.BzrMoveFailedError,
+ tree.rename_one, 'a', 'not-a-dir/b')
+
+ def test_rename_one_non_existent(self):
+ tree = self.make_branch_and_tree('.')
+ self.build_tree(['a/'])
+ tree.add(['a'])
+ tree.commit('initial', rev_id='rev-1')
+ self.assertRaises(errors.BzrMoveFailedError,
+ tree.rename_one, 'not-a-file', 'a/failure')
+ self.assertRaises(errors.BzrMoveFailedError,
+ tree.rename_one, 'not-a-file', 'also_not')
+
+ def test_rename_one_target_not_versioned(self):
+ tree = self.make_branch_and_tree('.')
+ self.build_tree(['a/', 'b'])
+ tree.add(['b'])
+ tree.commit('initial', rev_id='rev-1')
+ self.assertRaises(errors.BzrMoveFailedError,
+ tree.rename_one, 'b', 'a/b')
+
+ def test_rename_one_unversioned(self):
+ tree = self.make_branch_and_tree('.')
+ self.build_tree(['a/', 'b'])
+ tree.add(['a'])
+ tree.commit('initial', rev_id='rev-1')
+ self.assertRaises(errors.BzrMoveFailedError,
+ tree.rename_one, 'b', 'a/b')
+
+ def test_rename_one_samedir(self):
+ tree = self.make_branch_and_tree('.')
+ self.build_tree(['a', 'b/'])
+ tree.add(['a', 'b'], ['a-id', 'b-id'])
+ tree.commit('initial', rev_id='rev-1')
+ root_id = tree.get_root_id()
+
+ a_contents = tree.get_file_text('a-id')
+ tree.rename_one('a', 'foo')
+ self.assertTreeLayout([('', root_id), ('b', 'b-id'), ('foo', 'a-id')],
+ tree)
+ self.assertTreeLayout([('', root_id), ('a', 'a-id'), ('b', 'b-id')],
+ tree.basis_tree())
+ self.failIfExists('a')
+ self.assertFileEqual(a_contents, 'foo')
+
+ def test_rename_one_not_localdir(self):
+ tree = self.make_branch_and_tree('tree')
+ self.build_tree(['tree/a', 'tree/b/'])
+ tree.add(['a', 'b'], ['a-id', 'b-id'])
+ tree.commit('initial', rev_id='rev-1')
+ root_id = tree.get_root_id()
+
+ a_contents = tree.get_file_text('a-id')
+ tree.rename_one('a', 'b/foo')
+ self.assertTreeLayout([('', root_id), ('b', 'b-id'), ('b/foo', 'a-id')],
+ tree)
+ self.assertTreeLayout([('', root_id), ('a', 'a-id'), ('b', 'b-id')],
+ tree.basis_tree())
+ self.failIfExists('tree/a')
+ self.assertFileEqual(a_contents, 'tree/b/foo')
+
+ def test_rename_one_subdir(self):
+ tree = self.make_branch_and_tree('.')
+ self.build_tree(['a', 'b/', 'b/c'])
+ tree.add(['a', 'b', 'b/c'], ['a-id', 'b-id', 'c-id'])
+ tree.commit('initial', rev_id='rev-1')
+ root_id = tree.get_root_id()
+ self.assertTreeLayout([('', root_id), ('a', 'a-id'), ('b', 'b-id'),
+ ('b/c', 'c-id')], tree)
+ self.assertTreeLayout([('', root_id), ('a', 'a-id'), ('b', 'b-id'),
+ ('b/c', 'c-id')], tree.basis_tree())
+ a_contents = tree.get_file_text('a-id')
+ tree.rename_one('a', 'b/d')
+ self.assertTreeLayout([('', root_id), ('b', 'b-id'), ('b/c', 'c-id'),
+ ('b/d', 'a-id')], tree)
+ self.assertTreeLayout([('', root_id), ('a', 'a-id'), ('b', 'b-id'),
+ ('b/c', 'c-id')], tree.basis_tree())
+ self.failIfExists('a')
+ self.assertFileEqual(a_contents, 'b/d')
+
+ def test_rename_one_parent_dir(self):
+ tree = self.make_branch_and_tree('.')
+ self.build_tree(['a', 'b/', 'b/c'])
+ tree.add(['a', 'b', 'b/c'], ['a-id', 'b-id', 'c-id'])
+ tree.commit('initial', rev_id='rev-1')
+ root_id = tree.get_root_id()
+ c_contents = tree.get_file_text('c-id')
+ tree.rename_one('b/c', 'd')
+ self.assertTreeLayout([('', root_id), ('a', 'a-id'), ('b', 'b-id'),
+ ('d', 'c-id')], tree)
+ self.assertTreeLayout([('', root_id), ('a', 'a-id'), ('b', 'b-id'),
+ ('b/c', 'c-id')], tree.basis_tree())
+ self.failIfExists('b/c')
+ self.assertFileEqual(c_contents, 'd')
+
+ def test_rename_one_fail_consistent(self):
+ tree = self.make_branch_and_tree('.')
+ self.build_tree(['a', 'b/', 'b/a', 'c'])
+ tree.add(['a', 'b', 'c'], ['a-id', 'b-id', 'c-id'])
+ tree.commit('initial', rev_id='rev-1')
+ root_id = tree.get_root_id()
+ # Target already exists
+ self.assertRaises(errors.RenameFailedFilesExist,
+ tree.rename_one, 'a', 'b/a')
+ self.assertTreeLayout([('', root_id), ('a', 'a-id'), ('b', 'b-id'),
+ ('c', 'c-id')], tree)
+ self.assertTreeLayout([('', root_id), ('a', 'a-id'), ('b', 'b-id'),
+ ('c', 'c-id')], tree.basis_tree())
+
+ def test_rename_one_onto_existing(self):
+ tree = self.make_branch_and_tree('.')
+ self.build_tree(['a', 'b'])
+ tree.add(['a', 'b'], ['a-id', 'b-id'])
+ tree.commit('initial', rev_id='rev-1')
+
+ self.assertRaises(errors.BzrMoveFailedError,
+ tree.rename_one, 'a', 'b')
+
+ def test_rename_one_onto_self(self):
+ tree = self.make_branch_and_tree('.')
+ self.build_tree(['b/', 'b/a'])
+ tree.add(['b', 'b/a'], ['b-id', 'a-id'])
+ tree.commit('initial', rev_id='rev-1')
+
+ self.assertRaises(errors.BzrMoveFailedError,
+ tree.rename_one, 'b/a', 'b/a')
+
+ def test_rename_one_onto_self_root(self):
+ tree = self.make_branch_and_tree('.')
+ self.build_tree(['a'])
+ tree.add(['a'], ['a-id'])
+ tree.commit('initial', rev_id='rev-1')
+
+ self.assertRaises(errors.BzrMoveFailedError,
+ tree.rename_one, 'a', 'a')
+
+ def test_rename_one_after(self):
+ tree = self.make_branch_and_tree('.')
+ self.build_tree(['a', 'b/'])
+ tree.add(['a', 'b'], ['a-id', 'b-id'])
+ tree.commit('initial', rev_id='rev-1')
+ root_id = tree.get_root_id()
+ os.rename('a', 'b/foo')
+
+ self.assertTreeLayout([('', root_id), ('a', 'a-id'), ('b', 'b-id')],
+ tree)
+ # We don't need after=True as long as source is missing and target
+ # exists.
+ tree.rename_one('a', 'b/foo')
+ self.assertTreeLayout([('', root_id), ('b', 'b-id'),
+ ('b/foo', 'a-id')], tree)
+ self.assertTreeLayout([('', root_id), ('a', 'a-id'), ('b', 'b-id')],
+ tree.basis_tree())
+
+ def test_rename_one_after_with_after(self):
+ tree = self.make_branch_and_tree('.')
+ self.build_tree(['a', 'b/'])
+ tree.add(['a', 'b'], ['a-id', 'b-id'])
+ tree.commit('initial', rev_id='rev-1')
+ root_id = tree.get_root_id()
+ os.rename('a', 'b/foo')
+
+ self.assertTreeLayout([('', root_id), ('a', 'a-id'), ('b', 'b-id')],
+ tree)
+ # Passing after=True should work as well
+ tree.rename_one('a', 'b/foo', after=True)
+ self.assertTreeLayout([('', root_id), ('b', 'b-id'),
+ ('b/foo', 'a-id')], tree)
+ self.assertTreeLayout([('', root_id), ('a', 'a-id'), ('b', 'b-id')],
+ tree.basis_tree())
+
+ def test_rename_one_after_no_target(self):
+ tree = self.make_branch_and_tree('.')
+ self.build_tree(['a', 'b/'])
+ tree.add(['a', 'b'], ['a-id', 'b-id'])
+ tree.commit('initial', rev_id='rev-1')
+ root_id = tree.get_root_id()
+
+ # Passing after when the file hasn't been rename_one raises an exception
+ self.assertRaises(errors.BzrMoveFailedError,
+ tree.rename_one, 'a', 'b/foo', after=True)
+ self.assertTreeLayout([('', root_id), ('a', 'a-id'), ('b', 'b-id')],
+ tree)
+ self.assertTreeLayout([('', root_id), ('a', 'a-id'), ('b', 'b-id')],
+ tree.basis_tree())
+
+ def test_rename_one_after_source_and_dest(self):
+ tree = self.make_branch_and_tree('.')
+ self.build_tree(['a', 'b/', 'b/foo'])
+ tree.add(['a', 'b'], ['a-id', 'b-id'])
+ tree.commit('initial', rev_id='rev-1')
+ root_id = tree.get_root_id()
+
+ # TODO: jam 20070225 I would usually use 'rb', but assertFileEqual
+ # uses 'r'.
+ a_file = open('a', 'r')
+ try:
+ a_text = a_file.read()
+ finally:
+ a_file.close()
+ foo_file = open('b/foo', 'r')
+ try:
+ foo_text = foo_file.read()
+ finally:
+ foo_file.close()
+
+ self.assertTreeLayout([('', root_id), ('a', 'a-id'), ('b', 'b-id')],
+ tree)
+ self.assertRaises(errors.RenameFailedFilesExist,
+ tree.rename_one, 'a', 'b/foo', after=False)
+ self.assertTreeLayout([('', root_id), ('a', 'a-id'), ('b', 'b-id')],
+ tree)
+ self.assertFileEqual(a_text, 'a')
+ self.assertFileEqual(foo_text, 'b/foo')
+ # But you can pass after=True
+ tree.rename_one('a', 'b/foo', after=True)
+ self.assertTreeLayout([('', root_id), ('b', 'b-id'),
+ ('b/foo', 'a-id')], tree)
+ self.assertTreeLayout([('', root_id), ('a', 'a-id'), ('b', 'b-id')],
+ tree.basis_tree())
+ # But it shouldn't actually move anything
+ self.assertFileEqual(a_text, 'a')
+ self.assertFileEqual(foo_text, 'b/foo')
+
+ def test_rename_one_directory(self):
+ tree = self.make_branch_and_tree('.')
+ self.build_tree(['a/', 'a/b', 'a/c/', 'a/c/d', 'e/'])
+ tree.add(['a', 'a/b', 'a/c', 'a/c/d', 'e'],
+ ['a-id', 'b-id', 'c-id', 'd-id', 'e-id'])
+ tree.commit('initial', rev_id='rev-1')
+ root_id = tree.get_root_id()
+
+ tree.rename_one('a', 'e/f')
+ self.assertTreeLayout([('', root_id), ('e', 'e-id'), ('e/f', 'a-id'),
+ ('e/f/b', 'b-id'), ('e/f/c', 'c-id'),
+ ('e/f/c/d', 'd-id')], tree)
+ self.assertTreeLayout([('', root_id), ('a', 'a-id'), ('e', 'e-id'),
+ ('a/b', 'b-id'), ('a/c', 'c-id'),
+ ('a/c/d', 'd-id')], tree.basis_tree())
+
+ def test_rename_one_moved(self):
+ """Moving a moved entry works as expected."""
+ tree = self.make_branch_and_tree('.')
+ self.build_tree(['a/', 'a/b', 'c/'])
+ tree.add(['a', 'a/b', 'c'], ['a-id', 'b-id', 'c-id'])
+ tree.commit('initial', rev_id='rev-1')
+ root_id = tree.get_root_id()
+
+ tree.rename_one('a/b', 'c/foo')
+ self.assertTreeLayout([('', root_id), ('a', 'a-id'), ('c', 'c-id'),
+ ('c/foo', 'b-id')], tree)
+ self.assertTreeLayout([('', root_id), ('a', 'a-id'), ('c', 'c-id'),
+ ('a/b', 'b-id')], tree.basis_tree())
+
+ tree.rename_one('c/foo', 'bar')
+ self.assertTreeLayout([('', root_id), ('a', 'a-id'), ('bar', 'b-id'),
+ ('c', 'c-id')], tree)
+ self.assertTreeLayout([('', root_id), ('a', 'a-id'), ('c', 'c-id'),
+ ('a/b', 'b-id')], tree.basis_tree())
=== modified file 'bzrlib/tests/workingtree_implementations/__init__.py'
--- a/bzrlib/tests/workingtree_implementations/__init__.py 2007-02-25 17:20:22 +0000
+++ b/bzrlib/tests/workingtree_implementations/__init__.py 2007-02-26 16:19:02 +0000
@@ -1,6 +1,4 @@
-# Copyright (C) 2006 Canonical Ltd
-# Authors: Robert Collins <robert.collins at canonical.com>
-# -*- coding: utf-8 -*-
+# Copyright (C) 2006, 2007 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@@ -71,6 +69,7 @@
'bzrlib.tests.workingtree_implementations.test_put_file',
'bzrlib.tests.workingtree_implementations.test_readonly',
'bzrlib.tests.workingtree_implementations.test_read_working_inventory',
+ 'bzrlib.tests.workingtree_implementations.test_rename_one',
'bzrlib.tests.workingtree_implementations.test_revision_tree',
'bzrlib.tests.workingtree_implementations.test_set_root_id',
'bzrlib.tests.workingtree_implementations.test_smart_add',
=== modified file 'bzrlib/tests/workingtree_implementations/test_move.py'
--- a/bzrlib/tests/workingtree_implementations/test_move.py 2007-02-25 23:28:32 +0000
+++ b/bzrlib/tests/workingtree_implementations/test_move.py 2007-02-26 16:19:02 +0000
@@ -1,5 +1,4 @@
-# Copyright (C) 2006 Canonical Ltd
-# Authors: Robert Collins <robert.collins at canonical.com>
+# Copyright (C) 2006, 2007 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@@ -15,7 +14,7 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-"""Tests for interface conformance of 'workingtree.put_mkdir'"""
+"""Tests for interface conformance of 'WorkingTree.move'"""
import os
@@ -332,3 +331,26 @@
self.assertTreeLayout([('', root_id), ('e', 'e-id'), ('e/a', 'a-id'),
('e/a/b', 'b-id'), ('e/a/c', 'c-id'),
('e/a/c/d', 'd-id')], tree)
+ self.assertTreeLayout([('', root_id), ('a', 'a-id'), ('e', 'e-id'),
+ ('a/b', 'b-id'), ('a/c', 'c-id'),
+ ('a/c/d', 'd-id')], tree.basis_tree())
+
+ def test_move_moved(self):
+ """Moving a moved entry works as expected."""
+ tree = self.make_branch_and_tree('.')
+ self.build_tree(['a/', 'a/b', 'c/'])
+ tree.add(['a', 'a/b', 'c'], ['a-id', 'b-id', 'c-id'])
+ tree.commit('initial', rev_id='rev-1')
+ root_id = tree.get_root_id()
+
+ tree.move(['a/b'], 'c')
+ self.assertTreeLayout([('', root_id), ('a', 'a-id'), ('c', 'c-id'),
+ ('c/b', 'b-id')], tree)
+ self.assertTreeLayout([('', root_id), ('a', 'a-id'), ('c', 'c-id'),
+ ('a/b', 'b-id')], tree.basis_tree())
+
+ tree.move(['c/b'], '')
+ self.assertTreeLayout([('', root_id), ('a', 'a-id'), ('b', 'b-id'),
+ ('c', 'c-id')], tree)
+ self.assertTreeLayout([('', root_id), ('a', 'a-id'), ('c', 'c-id'),
+ ('a/b', 'b-id')], tree.basis_tree())
More information about the bazaar-commits
mailing list