~bzr-pqm/bzr/bzr.dev

2052.3.2 by John Arbash Meinel
Change Copyright .. by Canonical to Copyright ... Canonical
1
# Copyright (C) 2006 Canonical Ltd
1852.8.2 by Robert Collins
Add InterTree class to represent InterTree operations.
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
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
16
17
"""Tests for Tree and InterTree."""
18
2655.2.1 by Marius Kruger
InterTree.compare and delta._compare_trees did not pass its
19
from bzrlib import errors
1852.8.2 by Robert Collins
Add InterTree class to represent InterTree operations.
20
from bzrlib.tests import TestCaseWithTransport
21
from bzrlib.tree import InterTree
22
23
24
class TestInterTree(TestCaseWithTransport):
25
26
    def test_revision_tree_revision_tree(self):
27
        # we should have an InterTree registered for RevisionTree to
28
        # RevisionTree.
29
        tree = self.make_branch_and_tree('.')
30
        rev_id = tree.commit('first post')
31
        rev_id2 = tree.commit('second post', allow_pointless=True)
32
        rev_tree = tree.branch.repository.revision_tree(rev_id)
33
        rev_tree2 = tree.branch.repository.revision_tree(rev_id2)
34
        optimiser = InterTree.get(rev_tree, rev_tree2)
35
        self.assertIsInstance(optimiser, InterTree)
36
        optimiser = InterTree.get(rev_tree2, rev_tree)
37
        self.assertIsInstance(optimiser, InterTree)
38
39
    def test_working_tree_revision_tree(self):
40
        # we should have an InterTree available for WorkingTree to 
41
        # RevisionTree.
42
        tree = self.make_branch_and_tree('.')
43
        rev_id = tree.commit('first post')
44
        rev_tree = tree.branch.repository.revision_tree(rev_id)
45
        optimiser = InterTree.get(rev_tree, tree)
46
        self.assertIsInstance(optimiser, InterTree)
47
        optimiser = InterTree.get(tree, rev_tree)
48
        self.assertIsInstance(optimiser, InterTree)
49
50
    def test_working_tree_working_tree(self):
51
        # we should have an InterTree available for WorkingTree to 
52
        # WorkingTree.
53
        tree = self.make_branch_and_tree('1')
54
        tree2 = self.make_branch_and_tree('2')
55
        optimiser = InterTree.get(tree, tree2)
56
        self.assertIsInstance(optimiser, InterTree)
57
        optimiser = InterTree.get(tree2, tree)
58
        self.assertIsInstance(optimiser, InterTree)
1852.8.4 by Robert Collins
Hook InterTree into Tree.
59
60
61
class RecordingOptimiser(InterTree):
62
63
    calls = []
64
1852.9.4 by Robert Collins
Add minimal test for Tree.compare(extra_trees=...).
65
    def compare(self, want_unchanged=False, specific_files=None,
2255.7.90 by Robert Collins
Add unversioned path reporting to TreeDelta.
66
        extra_trees=None, require_versioned=False, include_root=False,
67
        want_unversioned=False):
1852.9.4 by Robert Collins
Add minimal test for Tree.compare(extra_trees=...).
68
        self.calls.append(
1852.9.6 by Robert Collins
Merge the change from Tree.compare to Tree.changes_from.
69
            ('compare', self.source, self.target, want_unchanged,
1910.2.57 by Aaron Bentley
Got 0.9 bundles working, with root included by changes_from
70
             specific_files, extra_trees, require_versioned, 
2255.7.90 by Robert Collins
Add unversioned path reporting to TreeDelta.
71
             include_root, want_unversioned)
1852.9.5 by Robert Collins
Add tests for require_versioned to the InterTree.compare() test suite.
72
            )
1852.8.4 by Robert Collins
Hook InterTree into Tree.
73
    
74
    @classmethod
75
    def is_compatible(klass, source, target):
76
        return True
77
78
79
class TestTree(TestCaseWithTransport):
80
81
    def test_compare_calls_InterTree_compare(self):
1852.9.4 by Robert Collins
Add minimal test for Tree.compare(extra_trees=...).
82
        """This test tests the way Tree.compare() uses InterTree."""
1852.8.4 by Robert Collins
Hook InterTree into Tree.
83
        old_optimisers = InterTree._optimisers
84
        try:
1910.2.15 by Aaron Bentley
Back out inter.get changes, make optimizers an ordered list
85
            InterTree._optimisers = []
1852.8.4 by Robert Collins
Hook InterTree into Tree.
86
            RecordingOptimiser.calls = []
87
            InterTree.register_optimiser(RecordingOptimiser)
88
            tree = self.make_branch_and_tree('1')
89
            tree2 = self.make_branch_and_tree('2')
1852.9.3 by Robert Collins
Convert the test_delta tests to intertree_implementation and workingtree_implementation tests as appropriate.
90
            # do a series of calls:
91
            # trivial usage
1852.8.8 by Robert Collins
change Tree.compare to Tree.changes_from - its better for the common case.
92
            tree.changes_from(tree2)
1852.9.3 by Robert Collins
Convert the test_delta tests to intertree_implementation and workingtree_implementation tests as appropriate.
93
            # pass in all optional arguments by position
1731.1.33 by Aaron Bentley
Revert no-special-root changes
94
            tree.changes_from(tree2, 'unchanged', 'specific', 'extra', 
1910.2.57 by Aaron Bentley
Got 0.9 bundles working, with root included by changes_from
95
                              'require', True)
1852.9.3 by Robert Collins
Convert the test_delta tests to intertree_implementation and workingtree_implementation tests as appropriate.
96
            # pass in all optional arguments by keyword
1852.9.6 by Robert Collins
Merge the change from Tree.compare to Tree.changes_from.
97
            tree.changes_from(tree2,
1852.9.4 by Robert Collins
Add minimal test for Tree.compare(extra_trees=...).
98
                specific_files='specific',
99
                want_unchanged='unchanged',
100
                extra_trees='extra',
1852.9.5 by Robert Collins
Add tests for require_versioned to the InterTree.compare() test suite.
101
                require_versioned='require',
1910.2.57 by Aaron Bentley
Got 0.9 bundles working, with root included by changes_from
102
                include_root=True,
2255.7.90 by Robert Collins
Add unversioned path reporting to TreeDelta.
103
                want_unversioned=True,
1852.9.4 by Robert Collins
Add minimal test for Tree.compare(extra_trees=...).
104
                )
1852.8.4 by Robert Collins
Hook InterTree into Tree.
105
        finally:
106
            InterTree._optimisers = old_optimisers
1852.9.3 by Robert Collins
Convert the test_delta tests to intertree_implementation and workingtree_implementation tests as appropriate.
107
        self.assertEqual(
108
            [
2255.7.90 by Robert Collins
Add unversioned path reporting to TreeDelta.
109
             ('compare', tree2, tree, False, None, None, False, False, False),
110
             ('compare', tree2, tree, 'unchanged', 'specific', 'extra',
111
              'require', True, False),
112
             ('compare', tree2, tree, 'unchanged', 'specific', 'extra',
113
              'require', True, True),
1852.9.3 by Robert Collins
Convert the test_delta tests to intertree_implementation and workingtree_implementation tests as appropriate.
114
            ], RecordingOptimiser.calls)
1731.1.33 by Aaron Bentley
Revert no-special-root changes
115
116
    def test_changes_from_with_root(self):
117
        """Ensure the include_root option does what's expected."""
118
        wt = self.make_branch_and_tree('.')
119
        delta = wt.changes_from(wt.basis_tree())
120
        self.assertEqual(len(delta.added), 0)
121
        delta = wt.changes_from(wt.basis_tree(), wt, include_root=True)
122
        self.assertEqual(len(delta.added), 1)
123
        self.assertEqual(delta.added[0][0], '')
2655.2.1 by Marius Kruger
InterTree.compare and delta._compare_trees did not pass its
124
125
    def test_changes_from_with_require_versioned(self):
126
        """Ensure the require_versioned option does what's expected."""
127
        wt = self.make_branch_and_tree('.')
128
        self.build_tree(['known_file', 'unknown_file'])
129
        wt.add('known_file')
130
2655.2.2 by Marius Kruger
Rather use assertRaises in test_changes_from_with_require_versioned
131
        self.assertRaises(errors.PathsNotVersionedError,
132
            wt.changes_from, wt.basis_tree(), wt, specific_files=['known_file',
133
            'unknown_file'], require_versioned=True)
2655.2.1 by Marius Kruger
InterTree.compare and delta._compare_trees did not pass its
134
135
        # we need to pass a known file with an unknown file to get this to
136
        # fail when expected.
137
        delta = wt.changes_from(wt.basis_tree(), wt, 
138
            specific_files=['known_file', 'unknown_file'] ,
139
            require_versioned=False)
140
        self.assertEqual(len(delta.added), 1)