~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_transform.py

  • Committer: Alexander Belchenko
  • Date: 2007-06-06 07:45:14 UTC
  • mfrom: (2511 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2512.
  • Revision ID: bialix@ukr.net-20070606074514-qq7bxr0x2uj8c5c2
merge bzr.dev; fix ReST formatting in planned-performance-changes.txt

Show diffs side-by-side

added added

removed removed

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