/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 breezy/tests/test_tree.py

  • Committer: Jelmer Vernooij
  • Date: 2020-03-22 01:35:14 UTC
  • mfrom: (7490.7.6 work)
  • mto: This revision was merged to the branch mainline in revision 7499.
  • Revision ID: jelmer@jelmer.uk-20200322013514-7vw1ntwho04rcuj3
merge lp:brz/3.1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006-2009, 2011 Canonical Ltd
 
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""Tests for Tree and InterTree."""
 
18
 
 
19
from breezy import (
 
20
    errors,
 
21
    revision,
 
22
    tree as _mod_tree,
 
23
    )
 
24
from breezy.tests import (
 
25
    TestCase,
 
26
    TestCaseWithTransport,
 
27
    )
 
28
from breezy.tree import (
 
29
    FileTimestampUnavailable,
 
30
    InterTree,
 
31
    find_previous_paths,
 
32
    get_canonical_path,
 
33
    )
 
34
 
 
35
 
 
36
class TestErrors(TestCase):
 
37
 
 
38
    def test_file_timestamp_unavailable(self):
 
39
        e = FileTimestampUnavailable("/path/foo")
 
40
        self.assertEqual("The filestamp for /path/foo is not available.",
 
41
                         str(e))
 
42
 
 
43
 
 
44
class TestInterTree(TestCaseWithTransport):
 
45
 
 
46
    def test_revision_tree_revision_tree(self):
 
47
        # we should have an InterTree registered for RevisionTree to
 
48
        # RevisionTree.
 
49
        tree = self.make_branch_and_tree('.')
 
50
        rev_id = tree.commit('first post')
 
51
        rev_id2 = tree.commit('second post', allow_pointless=True)
 
52
        rev_tree = tree.branch.repository.revision_tree(rev_id)
 
53
        rev_tree2 = tree.branch.repository.revision_tree(rev_id2)
 
54
        optimiser = InterTree.get(rev_tree, rev_tree2)
 
55
        self.assertIsInstance(optimiser, InterTree)
 
56
        optimiser = InterTree.get(rev_tree2, rev_tree)
 
57
        self.assertIsInstance(optimiser, InterTree)
 
58
 
 
59
    def test_working_tree_revision_tree(self):
 
60
        # we should have an InterTree available for WorkingTree to
 
61
        # RevisionTree.
 
62
        tree = self.make_branch_and_tree('.')
 
63
        rev_id = tree.commit('first post')
 
64
        rev_tree = tree.branch.repository.revision_tree(rev_id)
 
65
        optimiser = InterTree.get(rev_tree, tree)
 
66
        self.assertIsInstance(optimiser, InterTree)
 
67
        optimiser = InterTree.get(tree, rev_tree)
 
68
        self.assertIsInstance(optimiser, InterTree)
 
69
 
 
70
    def test_working_tree_working_tree(self):
 
71
        # we should have an InterTree available for WorkingTree to
 
72
        # WorkingTree.
 
73
        tree = self.make_branch_and_tree('1')
 
74
        tree2 = self.make_branch_and_tree('2')
 
75
        optimiser = InterTree.get(tree, tree2)
 
76
        self.assertIsInstance(optimiser, InterTree)
 
77
        optimiser = InterTree.get(tree2, tree)
 
78
        self.assertIsInstance(optimiser, InterTree)
 
79
 
 
80
 
 
81
class RecordingOptimiser(InterTree):
 
82
 
 
83
    calls = []
 
84
 
 
85
    def compare(self, want_unchanged=False, specific_files=None,
 
86
                extra_trees=None, require_versioned=False, include_root=False,
 
87
                want_unversioned=False):
 
88
        self.calls.append(
 
89
            ('compare', self.source, self.target, want_unchanged,
 
90
             specific_files, extra_trees, require_versioned,
 
91
             include_root, want_unversioned)
 
92
            )
 
93
 
 
94
    @classmethod
 
95
    def is_compatible(klass, source, target):
 
96
        return True
 
97
 
 
98
 
 
99
class TestTree(TestCaseWithTransport):
 
100
 
 
101
    def test_compare_calls_InterTree_compare(self):
 
102
        """This test tests the way Tree.compare() uses InterTree."""
 
103
        old_optimisers = InterTree._optimisers
 
104
        try:
 
105
            InterTree._optimisers = []
 
106
            RecordingOptimiser.calls = []
 
107
            InterTree.register_optimiser(RecordingOptimiser)
 
108
            tree = self.make_branch_and_tree('1')
 
109
            tree2 = self.make_branch_and_tree('2')
 
110
            # do a series of calls:
 
111
            # trivial usage
 
112
            tree.changes_from(tree2)
 
113
            # pass in all optional arguments by position
 
114
            tree.changes_from(tree2, 'unchanged', 'specific', 'extra',
 
115
                              'require', True)
 
116
            # pass in all optional arguments by keyword
 
117
            tree.changes_from(tree2,
 
118
                              specific_files='specific',
 
119
                              want_unchanged='unchanged',
 
120
                              extra_trees='extra',
 
121
                              require_versioned='require',
 
122
                              include_root=True,
 
123
                              want_unversioned=True,
 
124
                              )
 
125
        finally:
 
126
            InterTree._optimisers = old_optimisers
 
127
        self.assertEqual(
 
128
            [
 
129
                ('compare', tree2, tree, False, None, None, False, False,
 
130
                    False),
 
131
                ('compare', tree2, tree, 'unchanged', 'specific', 'extra',
 
132
                    'require', True, False),
 
133
                ('compare', tree2, tree, 'unchanged', 'specific', 'extra',
 
134
                    'require', True, True),
 
135
            ], RecordingOptimiser.calls)
 
136
 
 
137
    def test_changes_from_with_root(self):
 
138
        """Ensure the include_root option does what's expected."""
 
139
        wt = self.make_branch_and_tree('.')
 
140
        delta = wt.changes_from(wt.basis_tree())
 
141
        self.assertEqual(len(delta.added), 0)
 
142
        delta = wt.changes_from(wt.basis_tree(), include_root=True)
 
143
        self.assertEqual(len(delta.added), 1)
 
144
        self.assertEqual(delta.added[0].path[1], '')
 
145
 
 
146
    def test_changes_from_with_require_versioned(self):
 
147
        """Ensure the require_versioned option does what's expected."""
 
148
        wt = self.make_branch_and_tree('.')
 
149
        self.build_tree(['known_file', 'unknown_file'])
 
150
        wt.add('known_file')
 
151
 
 
152
        self.assertRaises(
 
153
            errors.PathsNotVersionedError,
 
154
            wt.changes_from, wt.basis_tree(), wt,
 
155
            specific_files=['known_file', 'unknown_file'],
 
156
            require_versioned=True)
 
157
 
 
158
        # we need to pass a known file with an unknown file to get this to
 
159
        # fail when expected.
 
160
        delta = wt.changes_from(wt.basis_tree(),
 
161
                                specific_files=['known_file', 'unknown_file'],
 
162
                                require_versioned=False)
 
163
        self.assertEqual(len(delta.added), 1)
 
164
 
 
165
 
 
166
class FindPreviousPathsTests(TestCaseWithTransport):
 
167
 
 
168
    def test_new(self):
 
169
        tree = self.make_branch_and_tree('tree')
 
170
        self.build_tree(['tree/b'])
 
171
        tree.add(['b'])
 
172
        revid1 = tree.commit('first')
 
173
        tree1 = tree.branch.repository.revision_tree(revid1)
 
174
 
 
175
        tree0 = tree.branch.repository.revision_tree(revision.NULL_REVISION)
 
176
 
 
177
        self.assertEqual({'b': None}, find_previous_paths(tree1, tree0, ['b']))
 
178
 
 
179
    def test_find_previous_paths(self):
 
180
        tree = self.make_branch_and_tree('tree')
 
181
        self.build_tree(['tree/b'])
 
182
        tree.add(['b'])
 
183
        revid1 = tree.commit('first')
 
184
        tree1 = tree.branch.repository.revision_tree(revid1)
 
185
 
 
186
        tree.rename_one('b', 'c')
 
187
        self.build_tree(['tree/b'])
 
188
        tree.add(['b'])
 
189
        revid2 = tree.commit('second')
 
190
        tree2 = tree.branch.repository.revision_tree(revid2)
 
191
 
 
192
        self.assertEqual({'c': 'b', 'b': None},
 
193
                         find_previous_paths(tree2, tree1, ['b', 'c']))
 
194
 
 
195
 
 
196
class GetCanonicalPath(TestCaseWithTransport):
 
197
 
 
198
    def test_existing_case(self):
 
199
        # Test that we can find a file from a path with different case
 
200
        tree = self.make_branch_and_tree('tree')
 
201
        self.build_tree(['tree/b'])
 
202
        tree.add(['b'])
 
203
        self.assertEqual(
 
204
            'b',
 
205
            get_canonical_path(tree, 'b', lambda x: x.lower()))
 
206
        self.assertEqual(
 
207
            'b',
 
208
            get_canonical_path(tree, 'B', lambda x: x.lower()))
 
209
 
 
210
    def test_nonexistant_preserves_case(self):
 
211
        tree = self.make_branch_and_tree('tree')
 
212
        self.assertEqual(
 
213
            'b',
 
214
            get_canonical_path(tree, 'b', lambda x: x.lower()))
 
215
        self.assertEqual(
 
216
            'B',
 
217
            get_canonical_path(tree, 'B', lambda x: x.lower()))
 
218
 
 
219
    def test_in_directory_with_case(self):
 
220
        tree = self.make_branch_and_tree('tree')
 
221
        self.build_tree(['tree/a/', 'tree/a/b'])
 
222
        tree.add(['a', 'a/b'])
 
223
        self.assertEqual(
 
224
            'a/b',
 
225
            get_canonical_path(tree, 'a/b', lambda x: x.lower()))
 
226
        self.assertEqual(
 
227
            'a/b',
 
228
            get_canonical_path(tree, 'A/B', lambda x: x.lower()))
 
229
        self.assertEqual(
 
230
            'a/b',
 
231
            get_canonical_path(tree, 'A/b', lambda x: x.lower()))
 
232
        self.assertEqual(
 
233
            'a/C',
 
234
            get_canonical_path(tree, 'A/C', lambda x: x.lower()))