/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_transform.py

  • Committer: Marius Kruger
  • Date: 2007-08-12 08:15:15 UTC
  • mfrom: (2695 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2979.
  • Revision ID: amanic@gmail.com-20070812081515-vgekipfhohcuj6rn
merge with bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
from bzrlib.tests import TestCaseInTempDir, TestSkipped, TestCase
38
38
from bzrlib.transform import (TreeTransform, ROOT_PARENT, FinalPaths, 
39
39
                              resolve_conflicts, cook_conflicts, 
40
 
                              find_interesting, build_tree, get_backup_name)
 
40
                              find_interesting, build_tree, get_backup_name,
 
41
                              change_entry)
41
42
 
42
43
 
43
44
class TestTreeTransform(tests.TestCaseWithTransport):
394
395
        self.assertEqual(os.readlink(self.wt.abspath('oz/wizard')),
395
396
                         'wizard-target')
396
397
 
397
 
 
398
398
    def get_conflicted(self):
399
399
        create,root = self.get_transform()
400
400
        create.new_file('dorothy', root, 'dorothy', 'dorothy-id')
545
545
        wt = transform._tree
546
546
        transform.new_file('set_on_creation', root, 'Set on creation', 'soc',
547
547
                           True)
548
 
        sac = transform.new_file('set_after_creation', root, 'Set after creation', 'sac')
 
548
        sac = transform.new_file('set_after_creation', root,
 
549
                                 'Set after creation', 'sac')
549
550
        transform.set_executability(True, sac)
550
 
        uws = transform.new_file('unset_without_set', root, 'Unset badly', 'uws')
 
551
        uws = transform.new_file('unset_without_set', root, 'Unset badly',
 
552
                                 'uws')
551
553
        self.assertRaises(KeyError, transform.set_executability, None, uws)
552
554
        transform.apply()
553
555
        self.assertTrue(wt.is_executable('soc'))
774
776
        finally:
775
777
            transform.finalize()
776
778
 
 
779
    def test_rename_count(self):
 
780
        transform, root = self.get_transform()
 
781
        transform.new_file('name1', root, 'contents')
 
782
        self.assertEqual(transform.rename_count, 0)
 
783
        transform.apply()
 
784
        self.assertEqual(transform.rename_count, 1)
 
785
        transform2, root = self.get_transform()
 
786
        transform2.adjust_path('name2', root,
 
787
                               transform2.trans_id_tree_path('name1'))
 
788
        self.assertEqual(transform2.rename_count, 0)
 
789
        transform2.apply()
 
790
        self.assertEqual(transform2.rename_count, 2)
 
791
 
 
792
    def test_change_parent(self):
 
793
        """Ensure that after we change a parent, the results are still right.
 
794
 
 
795
        Renames and parent changes on pending transforms can happen as part
 
796
        of conflict resolution, and are explicitly permitted by the
 
797
        TreeTransform API.
 
798
 
 
799
        This test ensures they work correctly with the rename-avoidance
 
800
        optimization.
 
801
        """
 
802
        transform, root = self.get_transform()
 
803
        parent1 = transform.new_directory('parent1', root)
 
804
        child1 = transform.new_file('child1', parent1, 'contents')
 
805
        parent2 = transform.new_directory('parent2', root)
 
806
        transform.adjust_path('child1', parent2, child1)
 
807
        transform.apply()
 
808
        self.failIfExists(self.wt.abspath('parent1/child1'))
 
809
        self.failUnlessExists(self.wt.abspath('parent2/child1'))
 
810
        # rename limbo/new-1 => parent1, rename limbo/new-3 => parent2
 
811
        # no rename for child1 (counting only renames during apply)
 
812
        self.failUnlessEqual(2, transform.rename_count)
 
813
 
 
814
    def test_cancel_parent(self):
 
815
        """Cancelling a parent doesn't cause deletion of a non-empty directory
 
816
 
 
817
        This is like the test_change_parent, except that we cancel the parent
 
818
        before adjusting the path.  The transform must detect that the
 
819
        directory is non-empty, and move children to safe locations.
 
820
        """
 
821
        transform, root = self.get_transform()
 
822
        parent1 = transform.new_directory('parent1', root)
 
823
        child1 = transform.new_file('child1', parent1, 'contents')
 
824
        child2 = transform.new_file('child2', parent1, 'contents')
 
825
        try:
 
826
            transform.cancel_creation(parent1)
 
827
        except OSError:
 
828
            self.fail('Failed to move child1 before deleting parent1')
 
829
        transform.cancel_creation(child2)
 
830
        transform.create_directory(parent1)
 
831
        try:
 
832
            transform.cancel_creation(parent1)
 
833
        # If the transform incorrectly believes that child2 is still in
 
834
        # parent1's limbo directory, it will try to rename it and fail
 
835
        # because was already moved by the first cancel_creation.
 
836
        except OSError:
 
837
            self.fail('Transform still thinks child2 is a child of parent1')
 
838
        parent2 = transform.new_directory('parent2', root)
 
839
        transform.adjust_path('child1', parent2, child1)
 
840
        transform.apply()
 
841
        self.failIfExists(self.wt.abspath('parent1'))
 
842
        self.failUnlessExists(self.wt.abspath('parent2/child1'))
 
843
        # rename limbo/new-3 => parent2, rename limbo/new-2 => child1
 
844
        self.failUnlessEqual(2, transform.rename_count)
 
845
 
 
846
    def test_adjust_and_cancel(self):
 
847
        """Make sure adjust_path keeps track of limbo children properly"""
 
848
        transform, root = self.get_transform()
 
849
        parent1 = transform.new_directory('parent1', root)
 
850
        child1 = transform.new_file('child1', parent1, 'contents')
 
851
        parent2 = transform.new_directory('parent2', root)
 
852
        transform.adjust_path('child1', parent2, child1)
 
853
        transform.cancel_creation(child1)
 
854
        try:
 
855
            transform.cancel_creation(parent1)
 
856
        # if the transform thinks child1 is still in parent1's limbo
 
857
        # directory, it will attempt to move it and fail.
 
858
        except OSError:
 
859
            self.fail('Transform still thinks child1 is a child of parent1')
 
860
        transform.finalize()
 
861
 
 
862
    def test_noname_contents(self):
 
863
        """TreeTransform should permit deferring naming files."""
 
864
        transform, root = self.get_transform()
 
865
        parent = transform.trans_id_file_id('parent-id')
 
866
        try:
 
867
            transform.create_directory(parent)
 
868
        except KeyError:
 
869
            self.fail("Can't handle contents with no name")
 
870
        transform.finalize()
 
871
 
 
872
    def test_noname_contents_nested(self):
 
873
        """TreeTransform should permit deferring naming files."""
 
874
        transform, root = self.get_transform()
 
875
        parent = transform.trans_id_file_id('parent-id')
 
876
        try:
 
877
            transform.create_directory(parent)
 
878
        except KeyError:
 
879
            self.fail("Can't handle contents with no name")
 
880
        child = transform.new_directory('child', parent)
 
881
        transform.adjust_path('parent', root, parent)
 
882
        transform.apply()
 
883
        self.failUnlessExists(self.wt.abspath('parent/child'))
 
884
        self.assertEqual(1, transform.rename_count)
 
885
 
 
886
    def test_reuse_name(self):
 
887
        """Avoid reusing the same limbo name for different files"""
 
888
        transform, root = self.get_transform()
 
889
        parent = transform.new_directory('parent', root)
 
890
        child1 = transform.new_directory('child', parent)
 
891
        try:
 
892
            child2 = transform.new_directory('child', parent)
 
893
        except OSError:
 
894
            self.fail('Tranform tried to use the same limbo name twice')
 
895
        transform.adjust_path('child2', parent, child2)
 
896
        transform.apply()
 
897
        # limbo/new-1 => parent, limbo/new-3 => parent/child2
 
898
        # child2 is put into top-level limbo because child1 has already
 
899
        # claimed the direct limbo path when child2 is created.  There is no
 
900
        # advantage in renaming files once they're in top-level limbo, except
 
901
        # as part of apply.
 
902
        self.assertEqual(2, transform.rename_count)
 
903
 
 
904
    def test_reuse_when_first_moved(self):
 
905
        """Don't avoid direct paths when it is safe to use them"""
 
906
        transform, root = self.get_transform()
 
907
        parent = transform.new_directory('parent', root)
 
908
        child1 = transform.new_directory('child', parent)
 
909
        transform.adjust_path('child1', parent, child1)
 
910
        child2 = transform.new_directory('child', parent)
 
911
        transform.apply()
 
912
        # limbo/new-1 => parent
 
913
        self.assertEqual(1, transform.rename_count)
 
914
 
 
915
    def test_reuse_after_cancel(self):
 
916
        """Don't avoid direct paths when it is safe to use them"""
 
917
        transform, root = self.get_transform()
 
918
        parent2 = transform.new_directory('parent2', root)
 
919
        child1 = transform.new_directory('child1', parent2)
 
920
        transform.cancel_creation(parent2)
 
921
        transform.create_directory(parent2)
 
922
        child2 = transform.new_directory('child1', parent2)
 
923
        transform.adjust_path('child2', parent2, child1)
 
924
        transform.apply()
 
925
        # limbo/new-1 => parent2, limbo/new-2 => parent2/child1
 
926
        self.assertEqual(2, transform.rename_count)
 
927
 
 
928
    def test_finalize_order(self):
 
929
        """Finalize must be done in child-to-parent order"""
 
930
        transform, root = self.get_transform()
 
931
        parent = transform.new_directory('parent', root)
 
932
        child = transform.new_directory('child', parent)
 
933
        try:
 
934
            transform.finalize()
 
935
        except OSError:
 
936
            self.fail('Tried to remove parent before child1')
 
937
 
 
938
    def test_cancel_with_cancelled_child_should_succeed(self):
 
939
        transform, root = self.get_transform()
 
940
        parent = transform.new_directory('parent', root)
 
941
        child = transform.new_directory('child', parent)
 
942
        transform.cancel_creation(child)
 
943
        transform.cancel_creation(parent)
 
944
        transform.finalize()
 
945
 
 
946
    def test_change_entry(self):
 
947
        txt = 'bzrlib.transform.change_entry was deprecated in version 0.90.'
 
948
        self.callDeprecated([txt], change_entry, None, None, None, None, None,
 
949
            None, None, None)
 
950
 
 
951
 
777
952
class TransformGroup(object):
778
953
    def __init__(self, dirname, root_id):
779
954
        self.name = dirname
1115
1290
        self.assertRaises(errors.WorkingTreeAlreadyPopulated, 
1116
1291
            build_tree, source.basis_tree(), target)
1117
1292
 
 
1293
    def test_build_tree_rename_count(self):
 
1294
        source = self.make_branch_and_tree('source')
 
1295
        self.build_tree(['source/file1', 'source/dir1/'])
 
1296
        source.add(['file1', 'dir1'])
 
1297
        source.commit('add1')
 
1298
        target1 = self.make_branch_and_tree('target1')
 
1299
        transform_result = build_tree(source.basis_tree(), target1)
 
1300
        self.assertEqual(2, transform_result.rename_count)
 
1301
 
 
1302
        self.build_tree(['source/dir1/file2'])
 
1303
        source.add(['dir1/file2'])
 
1304
        source.commit('add3')
 
1305
        target2 = self.make_branch_and_tree('target2')
 
1306
        transform_result = build_tree(source.basis_tree(), target2)
 
1307
        # children of non-root directories should not be renamed
 
1308
        self.assertEqual(2, transform_result.rename_count)
 
1309
 
1118
1310
 
1119
1311
class MockTransform(object):
1120
1312
 
1127
1319
                return True
1128
1320
        return False
1129
1321
 
 
1322
 
1130
1323
class MockEntry(object):
1131
1324
    def __init__(self):
1132
1325
        object.__init__(self)