~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/per_intertree/test_compare.py

(robertc) Make iter_changes produce output that is always safe to
        generate inventory deltas of in the same direction as the
        changes. (Robert Collins, #347649)

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
#        -> that is, when the renamed parent is not processed by the function.
38
38
# TODO: test items are only emitted once when a specific_files list names a dir
39
39
#       whose parent is now a child.
40
 
# TODO: test specific_files when the target tree has a file and the source a
41
 
#       dir with children, same id and same path.
42
40
# TODO: test comparisons between trees with different root ids. mbp 20070301
43
41
#
44
42
# TODO: More comparisons between trees with subtrees in different states.
217
215
        d = self.intertree_class(tree1, tree2).compare(
218
216
            specific_files=['a', 'b/c'])
219
217
        self.assertEqual(
220
 
            [('a', 'a-id', 'file'), ('b/c', 'c-id', 'file')],
 
218
            [('a', 'a-id', 'file'),  (u'b', 'b-id', 'directory'),
 
219
             ('b/c', 'c-id', 'file')],
221
220
            d.added)
222
221
        self.assertEqual([], d.modified)
223
222
        self.assertEqual([], d.removed)
224
223
        self.assertEqual([], d.renamed)
225
224
        self.assertEqual([], d.unchanged)
226
225
 
 
226
    def test_empty_to_abc_content_c_only(self):
 
227
        tree1 = self.make_branch_and_tree('1')
 
228
        tree2 = self.make_to_branch_and_tree('2')
 
229
        tree1 = self.get_tree_no_parents_no_content(tree1)
 
230
        tree2 = self.get_tree_no_parents_abc_content(tree2)
 
231
        tree1, tree2 = self.mutable_trees_to_test_trees(self, tree1, tree2)
 
232
        d = self.intertree_class(tree1, tree2).compare(
 
233
            specific_files=['b/c'])
 
234
        self.assertEqual(
 
235
            [(u'b', 'b-id', 'directory'), ('b/c', 'c-id', 'file')], d.added)
 
236
        self.assertEqual([], d.modified)
 
237
        self.assertEqual([], d.removed)
 
238
        self.assertEqual([], d.renamed)
 
239
        self.assertEqual([], d.unchanged)
 
240
 
227
241
    def test_empty_to_abc_content_b_only(self):
228
242
        """Restricting to a dir matches the children of the dir."""
229
243
        tree1 = self.make_branch_and_tree('1')
233
247
        tree1, tree2 = self.mutable_trees_to_test_trees(self, tree1, tree2)
234
248
        d = self.intertree_class(tree1, tree2).compare(specific_files=['b'])
235
249
        self.assertEqual(
236
 
            [('b', 'b-id', 'directory'),('b/c', 'c-id', 'file')],
 
250
            [('b', 'b-id', 'directory'), ('b/c', 'c-id', 'file')],
237
251
            d.added)
238
252
        self.assertEqual([], d.modified)
239
253
        self.assertEqual([], d.removed)
350
364
class TestIterChanges(TestCaseWithTwoTrees):
351
365
    """Test the comparison iterator"""
352
366
 
 
367
    def assertEqualIterChanges(self, left_changes, right_changes):
 
368
        """Assert that left_changes == right_changes.
 
369
 
 
370
        :param left_changes: A list of the output from iter_changes.
 
371
        :param right_changes: A list of the output from iter_changes.
 
372
        """
 
373
        left_changes = sorted(left_changes)
 
374
        right_changes = sorted(right_changes)
 
375
        if left_changes == right_changes:
 
376
            return
 
377
        # setify to get item by item differences, but we can only do this
 
378
        # when all the ids are unique on both sides.
 
379
        left_dict = dict((item[0], item) for item in left_changes)
 
380
        right_dict = dict((item[0], item) for item in right_changes)
 
381
        if (len(left_dict) != len(left_changes) or
 
382
            len(right_dict) != len(right_changes)):
 
383
            # Can't do a direct comparison. We could do a sequence diff, but
 
384
            # for now just do a regular assertEqual for now.
 
385
            self.assertEqual(left_changes, right_changes)
 
386
        keys = set(left_dict).union(set(right_dict))
 
387
        different = []
 
388
        same = []
 
389
        for key in keys:
 
390
            left_item = left_dict.get(key)
 
391
            right_item = right_dict.get(key)
 
392
            if left_item == right_item:
 
393
                same.append(str(left_item))
 
394
            else:
 
395
                different.append(" %s\n %s" % (left_item, right_item))
 
396
        self.fail("iter_changes output different. Unchanged items:\n" +
 
397
            "\n".join(same) + "\nChanged items:\n" + "\n".join(different))
 
398
 
353
399
    def do_iter_changes(self, tree1, tree2, **extra_args):
354
400
        """Helper to run iter_changes from tree1 to tree2.
355
401
 
379
425
                # Neither tree can be used
380
426
                return
381
427
        tree1.lock_read()
382
 
        tree2.lock_read()
383
428
        try:
384
 
            return tree2.has_changes(tree1)
 
429
            tree2.lock_read()
 
430
            try:
 
431
                return tree2.has_changes(tree1)
 
432
            finally:
 
433
                tree2.unlock()
385
434
        finally:
386
435
            tree1.unlock()
387
 
            tree2.unlock()
388
436
 
389
437
    def mutable_trees_to_locked_test_trees(self, tree1, tree2):
390
438
        """Convert the working trees into test trees.
572
620
        tree2 = self.get_tree_no_parents_abc_content(tree2)
573
621
        tree1, tree2 = self.mutable_trees_to_locked_test_trees(tree1, tree2)
574
622
        self.assertEqual(
575
 
            [self.added(tree2, 'a-id')],
 
623
            sorted([self.added(tree2, 'root-id'),
 
624
             self.added(tree2, 'a-id'),
 
625
             self.deleted(tree1, 'empty-root-id')]),
576
626
            self.do_iter_changes(tree1, tree2, specific_files=['a']))
577
627
 
578
 
    def test_abc_content_to_empty_to_abc_content_a_only(self):
 
628
    def test_abc_content_to_empty_a_only(self):
 
629
        # For deletes we don't need to pickup parents.
579
630
        tree1 = self.make_branch_and_tree('1')
580
631
        tree2 = self.make_to_branch_and_tree('2')
581
632
        tree1 = self.get_tree_no_parents_abc_content(tree1)
585
636
            [self.deleted(tree1, 'a-id')],
586
637
            self.do_iter_changes(tree1, tree2, specific_files=['a']))
587
638
 
 
639
    def test_abc_content_to_empty_b_only(self):
 
640
        # When b stops being a directory we have to pick up b/c as well.
 
641
        tree1 = self.make_branch_and_tree('1')
 
642
        tree2 = self.make_to_branch_and_tree('2')
 
643
        tree1 = self.get_tree_no_parents_abc_content(tree1)
 
644
        tree2 = self.get_tree_no_parents_no_content(tree2)
 
645
        tree1, tree2 = self.mutable_trees_to_locked_test_trees(tree1, tree2)
 
646
        self.assertEqual(
 
647
            [self.deleted(tree1, 'b-id'), self.deleted(tree1, 'c-id')],
 
648
            self.do_iter_changes(tree1, tree2, specific_files=['b']))
 
649
 
588
650
    def test_empty_to_abc_content_a_and_c_only(self):
589
651
        tree1 = self.make_branch_and_tree('1')
590
652
        tree2 = self.make_to_branch_and_tree('2')
591
653
        tree1 = self.get_tree_no_parents_no_content(tree1)
592
654
        tree2 = self.get_tree_no_parents_abc_content(tree2)
593
655
        tree1, tree2 = self.mutable_trees_to_locked_test_trees(tree1, tree2)
594
 
        expected_result = [self.added(tree2, 'a-id'), self.added(tree2, 'c-id')]
 
656
        expected_result = sorted([self.added(tree2, 'root-id'),
 
657
            self.added(tree2, 'a-id'), self.added(tree2, 'b-id'),
 
658
            self.added(tree2, 'c-id'), self.deleted(tree1, 'empty-root-id')])
595
659
        self.assertEqual(expected_result,
596
660
            self.do_iter_changes(tree1, tree2, specific_files=['a', 'b/c']))
597
661
 
601
665
        tree1 = self.get_tree_no_parents_abc_content(tree1)
602
666
        tree2 = self.get_tree_no_parents_no_content(tree2)
603
667
        tree1, tree2 = self.mutable_trees_to_locked_test_trees(tree1, tree2)
604
 
        def deleted(file_id):
605
 
            entry = tree1.inventory[file_id]
606
 
            path = tree1.id2path(file_id)
607
 
            return (file_id, (path, None), True, (True, False),
608
 
                    (entry.parent_id, None),
609
 
                    (entry.name, None), (entry.kind, None),
610
 
                    (entry.executable, None))
611
668
        expected_results = sorted([
612
669
            self.added(tree2, 'empty-root-id'),
613
 
            deleted('root-id'), deleted('a-id'),
614
 
            deleted('b-id'), deleted('c-id')])
 
670
            self.deleted(tree1, 'root-id'), self.deleted(tree1, 'a-id'),
 
671
            self.deleted(tree1, 'b-id'), self.deleted(tree1, 'c-id')])
615
672
        self.assertEqual(
616
673
            expected_results,
617
674
            self.do_iter_changes(tree1, tree2))
679
736
                           (False, False))],
680
737
                         self.do_iter_changes(tree1, tree2))
681
738
 
 
739
    def test_specific_content_modification_grabs_parents(self):
 
740
        # WHen the only direct change to a specified file is a content change,
 
741
        # and its in a reparented subtree, the parents are grabbed.
 
742
        tree1 = self.make_branch_and_tree('1')
 
743
        tree1.mkdir('changing', 'parent-id')
 
744
        tree1.mkdir('changing/unchanging', 'mid-id')
 
745
        tree1.add(['changing/unchanging/file'], ['file-id'], ['file'])
 
746
        tree1.put_file_bytes_non_atomic('file-id', 'a file')
 
747
        tree2 = self.make_to_branch_and_tree('2')
 
748
        tree2.set_root_id(tree1.get_root_id())
 
749
        tree2.mkdir('changed', 'parent-id')
 
750
        tree2.mkdir('changed/unchanging', 'mid-id')
 
751
        tree2.add(['changed/unchanging/file'], ['file-id'], ['file'])
 
752
        tree2.put_file_bytes_non_atomic('file-id', 'changed content')
 
753
        tree1, tree2 = self.mutable_trees_to_test_trees(self, tree1, tree2)
 
754
        # parent-id has changed, as has file-id
 
755
        root_id = tree1.path2id('')
 
756
        self.assertEqualIterChanges(
 
757
            [self.renamed(tree1, tree2, 'parent-id', False),
 
758
             self.renamed(tree1, tree2, 'file-id', True)],
 
759
             self.do_iter_changes(tree1, tree2,
 
760
             specific_files=['changed/unchanging/file']))
 
761
 
 
762
    def test_specific_content_modification_grabs_parents_root_changes(self):
 
763
        # WHen the only direct change to a specified file is a content change,
 
764
        # and its in a reparented subtree, the parents are grabbed, even if
 
765
        # that includes the root.
 
766
        tree1 = self.make_branch_and_tree('1')
 
767
        tree1.set_root_id('old')
 
768
        tree1.mkdir('changed', 'parent-id')
 
769
        tree1.mkdir('changed/unchanging', 'mid-id')
 
770
        tree1.add(['changed/unchanging/file'], ['file-id'], ['file'])
 
771
        tree1.put_file_bytes_non_atomic('file-id', 'a file')
 
772
        tree2 = self.make_to_branch_and_tree('2')
 
773
        tree2.set_root_id('new')
 
774
        tree2.mkdir('changed', 'parent-id')
 
775
        tree2.mkdir('changed/unchanging', 'mid-id')
 
776
        tree2.add(['changed/unchanging/file'], ['file-id'], ['file'])
 
777
        tree2.put_file_bytes_non_atomic('file-id', 'changed content')
 
778
        tree1, tree2 = self.mutable_trees_to_locked_test_trees(tree1, tree2)
 
779
        # old is gone, new is added, parent-id has changed(reparented), as has
 
780
        # file-id(content)
 
781
        root_id = tree1.path2id('')
 
782
        self.assertEqualIterChanges(
 
783
            [self.renamed(tree1, tree2, 'parent-id', False),
 
784
             self.added(tree2, 'new'),
 
785
             self.deleted(tree1, 'old'),
 
786
             self.renamed(tree1, tree2, 'file-id', True)],
 
787
             self.do_iter_changes(tree1, tree2,
 
788
             specific_files=['changed/unchanging/file']))
 
789
 
 
790
    def test_specific_with_rename_under_new_dir_reports_new_dir(self):
 
791
        tree1 = self.make_branch_and_tree('1')
 
792
        tree2 = self.make_to_branch_and_tree('2')
 
793
        tree1 = self.get_tree_no_parents_abc_content(tree1)
 
794
        tree2 = self.get_tree_no_parents_abc_content_7(tree2)
 
795
        tree1, tree2 = self.mutable_trees_to_test_trees(self, tree1, tree2)
 
796
        # d(d-id) is new, e is b-id renamed. 
 
797
        root_id = tree1.path2id('')
 
798
        self.assertEqualIterChanges(
 
799
            [self.renamed(tree1, tree2, 'b-id', False),
 
800
             self.added(tree2, 'd-id')],
 
801
             self.do_iter_changes(tree1, tree2, specific_files=['d/e']))
 
802
 
 
803
    def test_specific_with_rename_under_dir_under_new_dir_reports_new_dir(self):
 
804
        tree1 = self.make_branch_and_tree('1')
 
805
        tree2 = self.make_to_branch_and_tree('2')
 
806
        tree1 = self.get_tree_no_parents_abc_content(tree1)
 
807
        tree2 = self.get_tree_no_parents_abc_content_7(tree2)
 
808
        tree2.rename_one('a', 'd/e/a')
 
809
        tree1, tree2 = self.mutable_trees_to_test_trees(self, tree1, tree2)
 
810
        # d is new, d/e is b-id renamed, d/e/a is a-id renamed 
 
811
        root_id = tree1.path2id('')
 
812
        self.assertEqualIterChanges(
 
813
            [self.renamed(tree1, tree2, 'b-id', False),
 
814
             self.added(tree2, 'd-id'),
 
815
             self.renamed(tree1, tree2, 'a-id', False)],
 
816
             self.do_iter_changes(tree1, tree2, specific_files=['d/e/a']))
 
817
 
 
818
    def test_specific_old_parent_same_path_new_parent(self):
 
819
        # when a parent is new at its path, if the path was used in the source
 
820
        # it must be emitted as a change.
 
821
        tree1 = self.make_branch_and_tree('1')
 
822
        tree1.add(['a'], ['a-id'], ['file'])
 
823
        tree1.put_file_bytes_non_atomic('a-id', 'a file')
 
824
        tree2 = self.make_to_branch_and_tree('2')
 
825
        tree2.set_root_id(tree1.get_root_id())
 
826
        tree2.mkdir('a', 'b-id')
 
827
        tree2.add(['a/c'], ['c-id'], ['file'])
 
828
        tree2.put_file_bytes_non_atomic('c-id', 'another file')
 
829
        tree1, tree2 = self.mutable_trees_to_locked_test_trees(tree1, tree2)
 
830
        # a-id is gone, b-id and c-id are added.
 
831
        self.assertEqualIterChanges(
 
832
            [self.deleted(tree1, 'a-id'),
 
833
             self.added(tree2, 'b-id'),
 
834
             self.added(tree2, 'c-id')],
 
835
             self.do_iter_changes(tree1, tree2, specific_files=['a/c']))
 
836
 
 
837
    def test_specific_old_parent_becomes_file(self):
 
838
        # When an old parent included because of a path conflict becomes a
 
839
        # non-directory, its children have to be all included in the delta.
 
840
        tree1 = self.make_branch_and_tree('1')
 
841
        tree1.mkdir('a', 'a-old-id')
 
842
        tree1.mkdir('a/reparented', 'reparented-id')
 
843
        tree1.mkdir('a/deleted', 'deleted-id')
 
844
        tree2 = self.make_to_branch_and_tree('2')
 
845
        tree2.set_root_id(tree1.get_root_id())
 
846
        tree2.mkdir('a', 'a-new-id')
 
847
        tree2.mkdir('a/reparented', 'reparented-id')
 
848
        tree2.add(['b'], ['a-old-id'], ['file'])
 
849
        tree2.put_file_bytes_non_atomic('a-old-id', '')
 
850
        tree1, tree2 = self.mutable_trees_to_locked_test_trees(tree1, tree2)
 
851
        # a-old-id is kind-changed, a-new-id is added, reparented-id is renamed,
 
852
        # deleted-id is gone
 
853
        self.assertEqualIterChanges(
 
854
            [self.kind_changed(tree1, tree2, 'a-old-id'),
 
855
             self.added(tree2, 'a-new-id'),
 
856
             self.renamed(tree1, tree2, 'reparented-id', False),
 
857
             self.deleted(tree1, 'deleted-id')],
 
858
             self.do_iter_changes(tree1, tree2,
 
859
                specific_files=['a/reparented']))
 
860
 
 
861
    def test_specific_old_parent_is_deleted(self):
 
862
        # When an old parent included because of a path conflict is removed,
 
863
        # its children have to be all included in the delta.
 
864
        tree1 = self.make_branch_and_tree('1')
 
865
        tree1.mkdir('a', 'a-old-id')
 
866
        tree1.mkdir('a/reparented', 'reparented-id')
 
867
        tree1.mkdir('a/deleted', 'deleted-id')
 
868
        tree2 = self.make_to_branch_and_tree('2')
 
869
        tree2.set_root_id(tree1.get_root_id())
 
870
        tree2.mkdir('a', 'a-new-id')
 
871
        tree2.mkdir('a/reparented', 'reparented-id')
 
872
        tree1, tree2 = self.mutable_trees_to_locked_test_trees(tree1, tree2)
 
873
        # a-old-id is gone, a-new-id is added, reparented-id is renamed,
 
874
        # deleted-id is gone
 
875
        self.assertEqualIterChanges(
 
876
            [self.deleted(tree1, 'a-old-id'),
 
877
             self.added(tree2, 'a-new-id'),
 
878
             self.renamed(tree1, tree2, 'reparented-id', False),
 
879
             self.deleted(tree1, 'deleted-id')],
 
880
             self.do_iter_changes(tree1, tree2,
 
881
                specific_files=['a/reparented']))
 
882
 
 
883
    def test_specific_old_parent_child_collides_with_unselected_new(self):
 
884
        # When the child of an old parent because of a path conflict becomes a
 
885
        # path conflict with some unselected item in the source, that item also
 
886
        # needs to be included (because otherwise the output of applying the
 
887
        # delta to the source would have two items at that path).
 
888
        tree1 = self.make_branch_and_tree('1')
 
889
        tree1.mkdir('a', 'a-old-id')
 
890
        tree1.mkdir('a/reparented', 'reparented-id')
 
891
        tree1.mkdir('collides', 'collides-id')
 
892
        tree2 = self.make_to_branch_and_tree('2')
 
893
        tree2.set_root_id(tree1.get_root_id())
 
894
        tree2.mkdir('a', 'a-new-id')
 
895
        tree2.mkdir('a/selected', 'selected-id')
 
896
        tree2.mkdir('collides', 'reparented-id')
 
897
        tree1, tree2 = self.mutable_trees_to_locked_test_trees(tree1, tree2)
 
898
        # a-old-id is one, a-new-id is added, reparented-id is renamed,
 
899
        # collides-id is gone, selected-id is new.
 
900
        self.assertEqualIterChanges(
 
901
            [self.deleted(tree1, 'a-old-id'),
 
902
             self.added(tree2, 'a-new-id'),
 
903
             self.renamed(tree1, tree2, 'reparented-id', False),
 
904
             self.deleted(tree1, 'collides-id'),
 
905
             self.added(tree2, 'selected-id')],
 
906
             self.do_iter_changes(tree1, tree2,
 
907
                specific_files=['a/selected']))
 
908
 
 
909
    def test_specific_old_parent_child_dir_stops_being_dir(self):
 
910
        # When the child of an old parent also stops being a directory, its
 
911
        # children must also be included. This test checks that downward
 
912
        # recursion is done appropriately by starting at a child of the root of
 
913
        # a deleted subtree (a/reparented), and checking that a sibling
 
914
        # directory (a/deleted) has its children included in the delta.
 
915
        tree1 = self.make_branch_and_tree('1')
 
916
        tree1.mkdir('a', 'a-old-id')
 
917
        tree1.mkdir('a/reparented', 'reparented-id-1')
 
918
        tree1.mkdir('a/deleted', 'deleted-id-1')
 
919
        tree1.mkdir('a/deleted/reparented', 'reparented-id-2')
 
920
        tree1.mkdir('a/deleted/deleted', 'deleted-id-2')
 
921
        tree2 = self.make_to_branch_and_tree('2')
 
922
        tree2.set_root_id(tree1.get_root_id())
 
923
        tree2.mkdir('a', 'a-new-id')
 
924
        tree2.mkdir('a/reparented', 'reparented-id-1')
 
925
        tree2.mkdir('reparented', 'reparented-id-2')
 
926
        tree1, tree2 = self.mutable_trees_to_locked_test_trees(tree1, tree2)
 
927
        # a-old-id is gone, a-new-id is added, reparented-id-1, -2 are renamed,
 
928
        # deleted-id-1 and -2 are gone.
 
929
        self.assertEqualIterChanges(
 
930
            [self.deleted(tree1, 'a-old-id'),
 
931
             self.added(tree2, 'a-new-id'),
 
932
             self.renamed(tree1, tree2, 'reparented-id-1', False),
 
933
             self.renamed(tree1, tree2, 'reparented-id-2', False),
 
934
             self.deleted(tree1, 'deleted-id-1'),
 
935
             self.deleted(tree1, 'deleted-id-2')],
 
936
             self.do_iter_changes(tree1, tree2,
 
937
                specific_files=['a/reparented']))
 
938
 
682
939
    def test_file_rename_and_meta_modification(self):
683
940
        tree1 = self.make_branch_and_tree('1')
684
941
        tree2 = self.make_to_branch_and_tree('2')
1148
1405
 
1149
1406
        tree1, tree2 = self.mutable_trees_to_locked_test_trees(tree1, tree2)
1150
1407
        # We should notice that 'b' and all its children are deleted
1151
 
        expected = sorted([
 
1408
        expected = [
1152
1409
            self.content_changed(tree2, 'a-id'),
1153
1410
            self.content_changed(tree2, 'g-id'),
1154
1411
            self.deleted(tree1, 'b-id'),
1155
1412
            self.deleted(tree1, 'c-id'),
1156
1413
            self.deleted(tree1, 'd-id'),
1157
1414
            self.deleted(tree1, 'e-id'),
1158
 
            ])
1159
 
        self.assertEqual(expected, self.do_iter_changes(tree1, tree2))
 
1415
            ]
 
1416
        self.assertEqualIterChanges(expected,
 
1417
            self.do_iter_changes(tree1, tree2))
1160
1418
        self.check_has_changes(True, tree1, tree2)
1161
1419
 
1162
1420
    def test_added_unicode(self):
1273
1531
 
1274
1532
        self.assertEqual([self.renamed(tree1, tree2, rename_id, False)],
1275
1533
                         self.do_iter_changes(tree1, tree2))
1276
 
        self.assertEqual([self.renamed(tree1, tree2, rename_id, False)],
1277
 
                         self.do_iter_changes(tree1, tree2,
1278
 
                                              specific_files=[u'\u03b1']))
 
1534
        self.assertEqualIterChanges(
 
1535
            [self.renamed(tree1, tree2, rename_id, False)],
 
1536
            self.do_iter_changes(tree1, tree2, specific_files=[u'\u03b1']))
1279
1537
        self.check_has_changes(True, tree1, tree2)
1280
1538
 
1281
1539
    def test_unchanged_unicode(self):
1323
1581
            self.unchanged(tree1, subfile_id),
1324
1582
            ])
1325
1583
        self.assertEqual(expected,
1326
 
                         self.do_iter_changes(tree1, tree2,
1327
 
                                              specific_files=[u'\u03b1'],
1328
 
                                              include_unchanged=True))
 
1584
            self.do_iter_changes(tree1, tree2, specific_files=[u'\u03b1'],
 
1585
                include_unchanged=True))
1329
1586
 
1330
1587
    def test_unknown_unicode(self):
1331
1588
        tree1 = self.make_branch_and_tree('tree1')