~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Andrew Bennetts
  • Date: 2010-01-12 03:53:21 UTC
  • mfrom: (4948 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4964.
  • Revision ID: andrew.bennetts@canonical.com-20100112035321-hofpz5p10224ryj3
Merge lp:bzr, resolving conflicts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
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
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
"""Tests for the InterTree.compare() function."""
18
18
 
19
19
import os
20
20
import shutil
21
21
 
22
 
from bzrlib import errors, tests, workingtree_4
 
22
from bzrlib import (
 
23
    errors,
 
24
    mutabletree,
 
25
    tests,
 
26
    workingtree_4,
 
27
    )
23
28
from bzrlib.osutils import file_kind, has_symlinks
24
29
from bzrlib.tests import TestNotApplicable
25
 
from bzrlib.tests.intertree_implementations import TestCaseWithTwoTrees
 
30
from bzrlib.tests.per_intertree import TestCaseWithTwoTrees
26
31
 
27
32
# TODO: test the include_root option.
28
33
# TODO: test that renaming a directory x->y does not emit a rename for the
32
37
#        -> that is, when the renamed parent is not processed by the function.
33
38
# TODO: test items are only emitted once when a specific_files list names a dir
34
39
#       whose parent is now a child.
35
 
# TODO: test specific_files when the target tree has a file and the source a
36
 
#       dir with children, same id and same path. 
37
40
# TODO: test comparisons between trees with different root ids. mbp 20070301
38
41
#
39
42
# TODO: More comparisons between trees with subtrees in different states.
132
135
        self.assertEqual([], d.removed)
133
136
        self.assertEqual([], d.renamed)
134
137
        self.assertEqual([], d.unchanged)
135
 
        
 
138
 
136
139
    def test_meta_modification(self):
137
140
        tree1 = self.make_branch_and_tree('1')
138
141
        tree2 = self.make_to_branch_and_tree('2')
212
215
        d = self.intertree_class(tree1, tree2).compare(
213
216
            specific_files=['a', 'b/c'])
214
217
        self.assertEqual(
215
 
            [('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')],
216
220
            d.added)
217
221
        self.assertEqual([], d.modified)
218
222
        self.assertEqual([], d.removed)
219
223
        self.assertEqual([], d.renamed)
220
224
        self.assertEqual([], d.unchanged)
221
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
 
222
241
    def test_empty_to_abc_content_b_only(self):
223
242
        """Restricting to a dir matches the children of the dir."""
224
243
        tree1 = self.make_branch_and_tree('1')
228
247
        tree1, tree2 = self.mutable_trees_to_test_trees(self, tree1, tree2)
229
248
        d = self.intertree_class(tree1, tree2).compare(specific_files=['b'])
230
249
        self.assertEqual(
231
 
            [('b', 'b-id', 'directory'),('b/c', 'c-id', 'file')],
 
250
            [('b', 'b-id', 'directory'), ('b/c', 'c-id', 'file')],
232
251
            d.added)
233
252
        self.assertEqual([], d.modified)
234
253
        self.assertEqual([], d.removed)
286
305
    def test_require_versioned(self):
287
306
        # this does not quite robustly test, as it is passing in missing paths
288
307
        # rather than present-but-not-versioned paths. At the moment there is
289
 
        # no mechanism for managing the test trees (which are readonly) to 
 
308
        # no mechanism for managing the test trees (which are readonly) to
290
309
        # get present-but-not-versioned files for trees that can do that.
291
310
        tree1 = self.make_branch_and_tree('1')
292
311
        tree2 = self.make_to_branch_and_tree('2')
293
312
        tree1 = self.get_tree_no_parents_no_content(tree1)
294
313
        tree2 = self.get_tree_no_parents_abc_content(tree2)
295
314
        tree1, tree2 = self.mutable_trees_to_test_trees(self, tree1, tree2)
296
 
        self.assertRaises(errors.PathsNotVersionedError, 
 
315
        self.assertRaises(errors.PathsNotVersionedError,
297
316
            self.intertree_class(tree1, tree2).compare,
298
317
            specific_files=['d'],
299
318
            require_versioned=True)
328
347
        else:
329
348
            links_supported = False
330
349
        tree1, tree2 = self.mutable_trees_to_test_trees(self, tree1, tree2)
 
350
        self.not_applicable_if_cannot_represent_unversioned(tree2)
331
351
        d = self.intertree_class(tree1, tree2).compare(want_unversioned=True)
332
352
        self.assertEqual([], d.added)
333
353
        self.assertEqual([], d.modified)
344
364
class TestIterChanges(TestCaseWithTwoTrees):
345
365
    """Test the comparison iterator"""
346
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
 
347
399
    def do_iter_changes(self, tree1, tree2, **extra_args):
348
400
        """Helper to run iter_changes from tree1 to tree2.
349
 
        
 
401
 
350
402
        :param tree1, tree2:  The source and target trees. These will be locked
351
403
            automatically.
352
404
        :param **extra_args: Extra args to pass to iter_changes. This is not
362
414
            tree1.unlock()
363
415
            tree2.unlock()
364
416
 
 
417
    def check_has_changes(self, expected, tree1, tree2):
 
418
        # has_changes is defined for mutable trees only
 
419
        if not isinstance(tree2, mutabletree.MutableTree):
 
420
            if isinstance(tree1, mutabletree.MutableTree):
 
421
                # Let's switch the trees since has_changes() is commutative
 
422
                # (where we can apply it)
 
423
                tree2, tree1 = tree1, tree2
 
424
            else:
 
425
                # Neither tree can be used
 
426
                return
 
427
        tree1.lock_read()
 
428
        try:
 
429
            tree2.lock_read()
 
430
            try:
 
431
                return tree2.has_changes(tree1)
 
432
            finally:
 
433
                tree2.unlock()
 
434
        finally:
 
435
            tree1.unlock()
 
436
 
365
437
    def mutable_trees_to_locked_test_trees(self, tree1, tree2):
366
438
        """Convert the working trees into test trees.
367
439
 
431
503
        tree2 = self.get_tree_no_parents_no_content(tree2)
432
504
        tree1, tree2 = self.mutable_trees_to_test_trees(self, tree1, tree2)
433
505
        self.assertEqual([], self.do_iter_changes(tree1, tree2))
 
506
        self.check_has_changes(False, tree1, tree2)
434
507
 
435
508
    def added(self, tree, file_id):
436
509
        path, entry = self.get_path_entry(tree, file_id)
514
587
            self.added(tree2, 'c-id'),
515
588
            self.deleted(tree1, 'empty-root-id')])
516
589
        self.assertEqual(expected_results, self.do_iter_changes(tree1, tree2))
 
590
        self.check_has_changes(True, tree1, tree2)
517
591
 
518
592
    def test_empty_specific_files(self):
519
593
        tree1 = self.make_branch_and_tree('1')
537
611
            self.added(tree2, 'c-id'),
538
612
            self.deleted(tree1, 'empty-root-id')])
539
613
        self.assertEqual(expected_results, self.do_iter_changes(tree1, tree2))
 
614
        self.check_has_changes(True, tree1, tree2)
540
615
 
541
616
    def test_empty_to_abc_content_a_only(self):
542
617
        tree1 = self.make_branch_and_tree('1')
545
620
        tree2 = self.get_tree_no_parents_abc_content(tree2)
546
621
        tree1, tree2 = self.mutable_trees_to_locked_test_trees(tree1, tree2)
547
622
        self.assertEqual(
548
 
            [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')]),
549
626
            self.do_iter_changes(tree1, tree2, specific_files=['a']))
550
627
 
551
 
    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.
552
630
        tree1 = self.make_branch_and_tree('1')
553
631
        tree2 = self.make_to_branch_and_tree('2')
554
632
        tree1 = self.get_tree_no_parents_abc_content(tree1)
558
636
            [self.deleted(tree1, 'a-id')],
559
637
            self.do_iter_changes(tree1, tree2, specific_files=['a']))
560
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
 
561
650
    def test_empty_to_abc_content_a_and_c_only(self):
562
651
        tree1 = self.make_branch_and_tree('1')
563
652
        tree2 = self.make_to_branch_and_tree('2')
564
653
        tree1 = self.get_tree_no_parents_no_content(tree1)
565
654
        tree2 = self.get_tree_no_parents_abc_content(tree2)
566
655
        tree1, tree2 = self.mutable_trees_to_locked_test_trees(tree1, tree2)
567
 
        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')])
568
659
        self.assertEqual(expected_result,
569
660
            self.do_iter_changes(tree1, tree2, specific_files=['a', 'b/c']))
570
661
 
574
665
        tree1 = self.get_tree_no_parents_abc_content(tree1)
575
666
        tree2 = self.get_tree_no_parents_no_content(tree2)
576
667
        tree1, tree2 = self.mutable_trees_to_locked_test_trees(tree1, tree2)
577
 
        def deleted(file_id):
578
 
            entry = tree1.inventory[file_id]
579
 
            path = tree1.id2path(file_id)
580
 
            return (file_id, (path, None), True, (True, False),
581
 
                    (entry.parent_id, None),
582
 
                    (entry.name, None), (entry.kind, None),
583
 
                    (entry.executable, None))
584
668
        expected_results = sorted([
585
669
            self.added(tree2, 'empty-root-id'),
586
 
            deleted('root-id'), deleted('a-id'),
587
 
            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')])
588
672
        self.assertEqual(
589
673
            expected_results,
590
674
            self.do_iter_changes(tree1, tree2))
 
675
        self.check_has_changes(True, tree1, tree2)
591
676
 
592
677
    def test_content_modification(self):
593
678
        tree1 = self.make_branch_and_tree('1')
600
685
                           (root_id, root_id), ('a', 'a'),
601
686
                           ('file', 'file'), (False, False))],
602
687
                         self.do_iter_changes(tree1, tree2))
 
688
        self.check_has_changes(True, tree1, tree2)
603
689
 
604
690
    def test_meta_modification(self):
605
691
        tree1 = self.make_branch_and_tree('1')
650
736
                           (False, False))],
651
737
                         self.do_iter_changes(tree1, tree2))
652
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
 
653
939
    def test_file_rename_and_meta_modification(self):
654
940
        tree1 = self.make_branch_and_tree('1')
655
941
        tree2 = self.make_to_branch_and_tree('2')
662
948
                           (False, True))],
663
949
                         self.do_iter_changes(tree1, tree2))
664
950
 
 
951
    def test_file_becomes_unversionable_bug_438569(self):
 
952
        # This isn't strictly a intertree problem, but its the intertree code
 
953
        # path that triggers all stat cache updates on both xml and dirstate
 
954
        # trees.
 
955
        # In bug 438569, a file becoming a fifo causes an assert. Fifo's are
 
956
        # not versionable or diffable. For now, we simply stop cold when they
 
957
        # are detected (because we don't know how far through the code the 
 
958
        # assumption 'fifo's do not exist' goes). In future we could report 
 
959
        # the kind change and have commit refuse to go futher, or something
 
960
        # similar. One particular reason for choosing this approach is that
 
961
        # there is no minikind for 'fifo' in dirstate today, so we can't 
 
962
        # actually update records that way.
 
963
        # To add confusion, the totally generic code path works - but it
 
964
        # doesn't update persistent metadata. So this test permits InterTrees
 
965
        # to either work, or fail with BadFileKindError.
 
966
        self.requireFeature(tests.OsFifoFeature)
 
967
        tree1 = self.make_branch_and_tree('1')
 
968
        self.build_tree(['1/a'])
 
969
        tree1.set_root_id('root-id')
 
970
        tree1.add(['a'], ['a-id'])
 
971
        tree2 = self.make_branch_and_tree('2')
 
972
        os.mkfifo('2/a')
 
973
        tree2.add(['a'], ['a-id'], ['file'])
 
974
        try:
 
975
            tree1, tree2 = self.mutable_trees_to_test_trees(self, tree1, tree2)
 
976
        except (KeyError,):
 
977
            raise tests.TestNotApplicable(
 
978
                "Cannot represent a FIFO in this case %s" % self.id())
 
979
        try:
 
980
            self.do_iter_changes(tree1, tree2)
 
981
        except errors.BadFileKindError:
 
982
            pass
 
983
 
665
984
    def test_missing_in_target(self):
666
985
        """Test with the target files versioned but absent from disk."""
667
986
        tree1 = self.make_branch_and_tree('1')
672
991
        shutil.rmtree('2/b')
673
992
        # TODO ? have a symlink here?
674
993
        tree1, tree2 = self.mutable_trees_to_test_trees(self, tree1, tree2)
 
994
        self.not_applicable_if_missing_in('a', tree2)
 
995
        self.not_applicable_if_missing_in('b', tree2)
675
996
        root_id = tree1.path2id('')
676
997
        expected = sorted([
677
998
            self.missing('a-id', 'a', 'a', root_id, 'file'),
690
1011
        tree2.add(['directory'], ['file-id'])
691
1012
        os.rmdir('tree2/directory')
692
1013
        tree1, tree2 = self.mutable_trees_to_locked_test_trees(tree1, tree2)
 
1014
        self.not_applicable_if_missing_in('directory', tree2)
693
1015
 
694
1016
        root_id = tree1.path2id('')
695
1017
        expected = sorted([
705
1027
        tree1.add(['file'], ['file-id'])
706
1028
        os.unlink('tree1/file')
707
1029
        tree1, tree2 = self.mutable_trees_to_locked_test_trees(tree1, tree2)
 
1030
        self.not_applicable_if_missing_in('file', tree1)
708
1031
        root_id = tree1.path2id('')
709
 
        if not tree1.path2id('file'):
710
 
            # The locked test trees conversion could not preserve the missing
711
 
            # file status. This is normal (e.g. InterDirstateTree falls back
712
 
            # to InterTree if the basis is not a DirstateRevisionTree, and
713
 
            # revision trees cannot have missing files. 
714
 
            raise TestNotApplicable()
715
1032
        expected = [('file-id', ('file', None), False, (True, False),
716
1033
            (root_id, None), ('file', None), (None, None), (False, None))]
717
1034
        self.assertEqual(expected, self.do_iter_changes(tree1, tree2))
724
1041
        tree2.add(['file'], ['file-id'])
725
1042
        os.unlink('tree2/file')
726
1043
        tree1, tree2 = self.mutable_trees_to_locked_test_trees(tree1, tree2)
 
1044
        self.not_applicable_if_missing_in('file', tree2)
727
1045
        root_id = tree1.path2id('')
728
1046
        expected = [('file-id', (None, 'file'), False, (False, True),
729
1047
            (None, root_id), (None, 'file'), (None, None), (None, False))]
730
1048
        self.assertEqual(expected, self.do_iter_changes(tree1, tree2))
731
1049
 
 
1050
    def test_only_in_target_missing_subtree_specific_bug_367632(self):
 
1051
        tree1 = self.make_branch_and_tree('tree1')
 
1052
        tree2 = self.make_to_branch_and_tree('tree2')
 
1053
        tree2.set_root_id(tree1.get_root_id())
 
1054
        self.build_tree(['tree2/a-dir/', 'tree2/a-dir/a-file'])
 
1055
        tree2.add(['a-dir', 'a-dir/a-file'], ['dir-id', 'file-id'])
 
1056
        os.unlink('tree2/a-dir/a-file')
 
1057
        os.rmdir('tree2/a-dir')
 
1058
        tree1, tree2 = self.mutable_trees_to_locked_test_trees(tree1, tree2)
 
1059
        self.not_applicable_if_missing_in('a-dir', tree2)
 
1060
        root_id = tree1.path2id('')
 
1061
        expected = [
 
1062
            ('dir-id', (None, 'a-dir'), False, (False, True),
 
1063
            (None, root_id), (None, 'a-dir'), (None, None), (None, False)),
 
1064
            ('file-id', (None, 'a-dir/a-file'), False, (False, True),
 
1065
            (None, 'dir-id'), (None, 'a-file'), (None, None), (None, False))
 
1066
            ]
 
1067
        # bug 367632 showed that specifying the root broke some code paths,
 
1068
        # so we check this contract with and without it.
 
1069
        self.assertEqual(expected, self.do_iter_changes(tree1, tree2))
 
1070
        self.assertEqual(expected,
 
1071
            self.do_iter_changes(tree1, tree2, specific_files=['']))
 
1072
 
732
1073
    def test_unchanged_with_renames_and_modifications(self):
733
1074
        """want_unchanged should generate a list of unchanged entries."""
734
1075
        tree1 = self.make_branch_and_tree('1')
737
1078
        tree2 = self.get_tree_no_parents_abc_content_5(tree2)
738
1079
        tree1, tree2 = self.mutable_trees_to_locked_test_trees(tree1, tree2)
739
1080
        root_id = tree1.path2id('')
740
 
 
741
1081
        self.assertEqual(sorted([self.unchanged(tree1, root_id),
742
1082
            self.unchanged(tree1, 'b-id'),
743
1083
            ('a-id', ('a', 'd'), True, (True, True),
787
1127
 
788
1128
    def test_disk_in_subtrees_skipped(self):
789
1129
        """subtrees are considered not-in-the-current-tree.
790
 
        
 
1130
 
791
1131
        This test tests the trivial case, where the basis has no paths in the
792
1132
        current trees subtree.
793
1133
        """
799
1139
        tree2.set_root_id('root-id')
800
1140
        subtree2 = self.make_to_branch_and_tree('2/sub')
801
1141
        subtree2.set_root_id('subtree-id')
802
 
        tree2.add(['sub'], ['subtree-id'])
 
1142
        tree2.add_reference(subtree2)
803
1143
        self.build_tree(['2/sub/file'])
804
1144
        subtree2.add(['file'])
805
1145
 
830
1170
            self.content_changed(tree2, 'c-id'),
831
1171
            ])
832
1172
        self.assertEqual(expected, self.do_iter_changes(tree1, tree2))
 
1173
        self.check_has_changes(True, tree1, tree2)
833
1174
 
834
1175
    def test_unversioned_paths_in_tree(self):
835
1176
        tree1 = self.make_branch_and_tree('tree1')
842
1183
        else:
843
1184
            links_supported = False
844
1185
        tree1, tree2 = self.mutable_trees_to_locked_test_trees(tree1, tree2)
 
1186
        self.not_applicable_if_cannot_represent_unversioned(tree2)
845
1187
        expected = [
846
1188
            self.unversioned(tree2, 'file'),
847
1189
            self.unversioned(tree2, 'dir'),
862
1204
        else:
863
1205
            links_supported = False
864
1206
        tree1, tree2 = self.mutable_trees_to_locked_test_trees(tree1, tree2)
 
1207
        self.not_applicable_if_cannot_represent_unversioned(tree2)
865
1208
        expected = [
866
1209
            self.unversioned(tree2, 'file'),
867
1210
            self.unversioned(tree2, 'dir'),
877
1220
 
878
1221
    def test_unversioned_paths_in_target_matching_source_old_names(self):
879
1222
        # its likely that naive implementations of unversioned file support
880
 
        # will fail if the path was versioned, but is not any more, 
 
1223
        # will fail if the path was versioned, but is not any more,
881
1224
        # due to a rename, not due to unversioning it.
882
1225
        # That is, if the old tree has a versioned file 'foo', and
883
1226
        # the new tree has the same file but versioned as 'bar', and also
902
1245
            tree1.add(['link'], ['link-id'])
903
1246
            tree2.add(['movedlink'], ['link-id'])
904
1247
        tree1, tree2 = self.mutable_trees_to_locked_test_trees(tree1, tree2)
 
1248
        self.not_applicable_if_cannot_represent_unversioned(tree2)
905
1249
        root_id = tree1.path2id('')
906
1250
        expected = [
907
1251
            self.renamed(tree1, tree2, 'dir-id', False),
951
1295
                  ['a-id', 'b-id', 'c-id', 'd-id', 'a-c-id', 'e-id'])
952
1296
 
953
1297
        tree1, tree2 = self.mutable_trees_to_locked_test_trees(tree1, tree2)
 
1298
        self.not_applicable_if_cannot_represent_unversioned(tree2)
954
1299
 
955
1300
        self.assertEqual([], self.do_iter_changes(tree1, tree2,
956
1301
                                                  want_unversioned=True))
975
1320
        tree2.set_root_id(tree1.get_root_id())
976
1321
        self.build_tree(['tree2/dir/', 'tree2/dir/file'])
977
1322
        tree1, tree2 = self.mutable_trees_to_test_trees(self, tree1, tree2)
 
1323
        self.not_applicable_if_cannot_represent_unversioned(tree2)
978
1324
        expected = [
979
1325
            self.unversioned(tree2, 'dir'),
980
1326
            ]
1024
1370
    def test_versioned_symlinks(self):
1025
1371
        self.requireFeature(tests.SymlinkFeature)
1026
1372
        tree1, tree2 = self.make_trees_with_symlinks()
 
1373
        self.not_applicable_if_cannot_represent_unversioned(tree2)
1027
1374
        root_id = tree1.path2id('')
1028
1375
        expected = [
1029
1376
            self.unchanged(tree1, tree1.path2id('')),
1041
1388
        self.assertEqual(expected,
1042
1389
            self.do_iter_changes(tree1, tree2, include_unchanged=True,
1043
1390
                want_unversioned=True))
 
1391
        self.check_has_changes(True, tree1, tree2)
1044
1392
 
1045
1393
    def test_versioned_symlinks_specific_files(self):
1046
1394
        self.requireFeature(tests.SymlinkFeature)
1062
1410
        self.assertEqual(expected, self.do_iter_changes(tree1, tree2,
1063
1411
            specific_files=['added', 'changed', 'fromdir', 'fromfile',
1064
1412
            'removed', 'unchanged', 'todir', 'tofile']))
 
1413
        self.check_has_changes(True, tree1, tree2)
1065
1414
 
1066
1415
    def test_tree_with_special_names(self):
1067
1416
        tree1, tree2, paths, path_ids = self.make_tree_with_special_names()
1068
1417
        expected = sorted(self.added(tree2, f_id) for f_id in path_ids)
1069
1418
        self.assertEqual(expected, self.do_iter_changes(tree1, tree2))
 
1419
        self.check_has_changes(True, tree1, tree2)
1070
1420
 
1071
1421
    def test_trees_with_special_names(self):
1072
1422
        tree1, tree2, paths, path_ids = self.make_trees_with_special_names()
1073
1423
        expected = sorted(self.content_changed(tree2, f_id) for f_id in path_ids
1074
1424
                          if f_id.endswith('_f-id'))
1075
1425
        self.assertEqual(expected, self.do_iter_changes(tree1, tree2))
 
1426
        self.check_has_changes(True, tree1, tree2)
1076
1427
 
1077
1428
    def test_trees_with_deleted_dir(self):
1078
1429
        tree1 = self.make_branch_and_tree('tree1')
1087
1438
 
1088
1439
        tree1, tree2 = self.mutable_trees_to_locked_test_trees(tree1, tree2)
1089
1440
        # We should notice that 'b' and all its children are deleted
1090
 
        expected = sorted([
 
1441
        expected = [
1091
1442
            self.content_changed(tree2, 'a-id'),
1092
1443
            self.content_changed(tree2, 'g-id'),
1093
1444
            self.deleted(tree1, 'b-id'),
1094
1445
            self.deleted(tree1, 'c-id'),
1095
1446
            self.deleted(tree1, 'd-id'),
1096
1447
            self.deleted(tree1, 'e-id'),
1097
 
            ])
1098
 
        self.assertEqual(expected, self.do_iter_changes(tree1, tree2))
 
1448
            ]
 
1449
        self.assertEqualIterChanges(expected,
 
1450
            self.do_iter_changes(tree1, tree2))
 
1451
        self.check_has_changes(True, tree1, tree2)
1099
1452
 
1100
1453
    def test_added_unicode(self):
1101
1454
        tree1 = self.make_branch_and_tree('tree1')
1124
1477
        self.assertEqual([self.added(tree2, added_id)],
1125
1478
                         self.do_iter_changes(tree1, tree2,
1126
1479
                                              specific_files=[u'\u03b1']))
 
1480
        self.check_has_changes(True, tree1, tree2)
1127
1481
 
1128
1482
    def test_deleted_unicode(self):
1129
1483
        tree1 = self.make_branch_and_tree('tree1')
1152
1506
        self.assertEqual([self.deleted(tree1, deleted_id)],
1153
1507
                         self.do_iter_changes(tree1, tree2,
1154
1508
                                              specific_files=[u'\u03b1']))
 
1509
        self.check_has_changes(True, tree1, tree2)
1155
1510
 
1156
1511
    def test_modified_unicode(self):
1157
1512
        tree1 = self.make_branch_and_tree('tree1')
1181
1536
        self.assertEqual([self.content_changed(tree1, mod_id)],
1182
1537
                         self.do_iter_changes(tree1, tree2,
1183
1538
                                              specific_files=[u'\u03b1']))
 
1539
        self.check_has_changes(True, tree1, tree2)
1184
1540
 
1185
1541
    def test_renamed_unicode(self):
1186
1542
        tree1 = self.make_branch_and_tree('tree1')
1208
1564
 
1209
1565
        self.assertEqual([self.renamed(tree1, tree2, rename_id, False)],
1210
1566
                         self.do_iter_changes(tree1, tree2))
1211
 
        self.assertEqual([self.renamed(tree1, tree2, rename_id, False)],
1212
 
                         self.do_iter_changes(tree1, tree2,
1213
 
                                              specific_files=[u'\u03b1']))
 
1567
        self.assertEqualIterChanges(
 
1568
            [self.renamed(tree1, tree2, rename_id, False)],
 
1569
            self.do_iter_changes(tree1, tree2, specific_files=[u'\u03b1']))
 
1570
        self.check_has_changes(True, tree1, tree2)
1214
1571
 
1215
1572
    def test_unchanged_unicode(self):
1216
1573
        tree1 = self.make_branch_and_tree('tree1')
1257
1614
            self.unchanged(tree1, subfile_id),
1258
1615
            ])
1259
1616
        self.assertEqual(expected,
1260
 
                         self.do_iter_changes(tree1, tree2,
1261
 
                                              specific_files=[u'\u03b1'],
1262
 
                                              include_unchanged=True))
 
1617
            self.do_iter_changes(tree1, tree2, specific_files=[u'\u03b1'],
 
1618
                include_unchanged=True))
1263
1619
 
1264
1620
    def test_unknown_unicode(self):
1265
1621
        tree1 = self.make_branch_and_tree('tree1')
1283
1639
        tree2.add([u'\u03b1'], [a_id])
1284
1640
 
1285
1641
        tree1, tree2 = self.mutable_trees_to_locked_test_trees(tree1, tree2)
 
1642
        self.not_applicable_if_cannot_represent_unversioned(tree2)
1286
1643
 
1287
1644
        expected = sorted([
1288
1645
            self.unversioned(tree2, u'\u03b1/unknown_dir'),
1298
1655
                                              want_unversioned=True))
1299
1656
        self.assertEqual([], # Without want_unversioned we should get nothing
1300
1657
                         self.do_iter_changes(tree1, tree2))
 
1658
        self.check_has_changes(False, tree1, tree2)
1301
1659
 
1302
1660
        # We should also be able to select just a subset
1303
1661
        expected = sorted([
1333
1691
        self.build_tree(['tree2/a/file', 'tree2/a/dir/', 'tree2/a/dir/subfile'])
1334
1692
 
1335
1693
        tree1, tree2 = self.mutable_trees_to_locked_test_trees(tree1, tree2)
 
1694
        self.not_applicable_if_cannot_represent_unversioned(tree2)
1336
1695
 
1337
1696
        expected = sorted([
1338
1697
            self.unversioned(tree2, u'a/file'),
1377
1736
            ])
1378
1737
        self.assertEqual(expected,
1379
1738
                         self.do_iter_changes(tree1, tree2))
 
1739
        self.check_has_changes(True, tree1, tree2)
1380
1740
 
1381
1741
    def test_deleted_and_unknown(self):
1382
1742
        """Test a file marked removed, but still present on disk."""
1400
1760
        tree2.add(['a', 'c'], ['a-id', 'c-id'])
1401
1761
 
1402
1762
        tree1, tree2 = self.mutable_trees_to_locked_test_trees(tree1, tree2)
 
1763
        self.not_applicable_if_cannot_represent_unversioned(tree2)
1403
1764
 
1404
1765
        expected = sorted([
1405
1766
            self.deleted(tree1, 'b-id'),
1473
1834
        os.rename('tree2/a', 'tree2/a2')
1474
1835
 
1475
1836
        tree1, tree2 = self.mutable_trees_to_locked_test_trees(tree1, tree2)
 
1837
        self.not_applicable_if_missing_in('a', tree2)
1476
1838
 
1477
1839
        expected = sorted([
1478
1840
            self.missing('a-id', 'a', 'a', tree2.get_root_id(), 'file'),