/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
5200.3.1 by Robert Collins
Added ``bzrlib.tests.matchers`` as a place to put matchers, along with
1
# Copyright (C) 2010 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
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
17
"""Matchers for breezy.
5200.3.1 by Robert Collins
Added ``bzrlib.tests.matchers`` as a place to put matchers, along with
18
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
19
Primarily test support, Matchers are used by self.assertThat in the breezy
5200.3.1 by Robert Collins
Added ``bzrlib.tests.matchers`` as a place to put matchers, along with
20
test suite. A matcher is a stateful test helper which can be used to determine
21
if a passed object 'matches', much like a regex. If the object does not match
22
the mismatch can be described in a human readable fashion. assertThat then
23
raises if a mismatch occurs, showing the description as the assertion error.
24
25
Matchers are designed to be more reusable and composable than layered
26
assertions in Test Case objects, so they are recommended for new testing work.
27
"""
28
29
__all__ = [
6072.2.4 by Jelmer Vernooij
tests for matcher
30
    'HasLayout',
6883.5.4 by Jelmer Vernooij
Add HasPathRelations.
31
    'HasPathRelations',
5972.3.13 by Jelmer Vernooij
Add matcher for ancestry.
32
    'MatchesAncestry',
5200.3.2 by Robert Collins
Cleaner matcher matching revised unlocking protocol.
33
    'ReturnsUnlockable',
6228.3.1 by Jelmer Vernooij
Add RevisionHistoryMatches.
34
    'RevisionHistoryMatches',
5200.3.1 by Robert Collins
Added ``bzrlib.tests.matchers`` as a place to put matchers, along with
35
    ]
36
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
37
from .. import (
6110.6.2 by Jelmer Vernooij
In HasLayout, take into consideration Tree.has_versioned_directories.
38
    osutils,
5972.3.13 by Jelmer Vernooij
Add matcher for ancestry.
39
    revision as _mod_revision,
40
    )
41
7510.1.3 by Jelmer Vernooij
Fix missing import.
42
from ..tree import InterTree
43
6072.2.1 by Jelmer Vernooij
Add HasLayout matcher.
44
from testtools.matchers import Equals, Mismatch, Matcher
5200.3.1 by Robert Collins
Added ``bzrlib.tests.matchers`` as a place to put matchers, along with
45
46
5200.3.2 by Robert Collins
Cleaner matcher matching revised unlocking protocol.
47
class ReturnsUnlockable(Matcher):
5200.3.1 by Robert Collins
Added ``bzrlib.tests.matchers`` as a place to put matchers, along with
48
    """A matcher that checks for the pattern we want lock* methods to have:
49
5200.3.2 by Robert Collins
Cleaner matcher matching revised unlocking protocol.
50
    They should return an object with an unlock() method.
51
    Calling that method should unlock the original object.
5200.3.1 by Robert Collins
Added ``bzrlib.tests.matchers`` as a place to put matchers, along with
52
53
    :ivar lockable_thing: The object which can be locked that will be
54
        inspected.
55
    """
56
57
    def __init__(self, lockable_thing):
58
        Matcher.__init__(self)
59
        self.lockable_thing = lockable_thing
60
61
    def __str__(self):
6110.6.2 by Jelmer Vernooij
In HasLayout, take into consideration Tree.has_versioned_directories.
62
        return ('ReturnsUnlockable(lockable_thing=%s)' %
7143.15.2 by Jelmer Vernooij
Run autopep8.
63
                self.lockable_thing)
5200.3.1 by Robert Collins
Added ``bzrlib.tests.matchers`` as a place to put matchers, along with
64
65
    def match(self, lock_method):
5200.3.2 by Robert Collins
Cleaner matcher matching revised unlocking protocol.
66
        lock_method().unlock()
5200.3.1 by Robert Collins
Added ``bzrlib.tests.matchers`` as a place to put matchers, along with
67
        if self.lockable_thing.is_locked():
68
            return _IsLocked(self.lockable_thing)
69
        return None
70
71
72
class _IsLocked(Mismatch):
73
    """Something is locked."""
74
75
    def __init__(self, lockable_thing):
76
        self.lockable_thing = lockable_thing
77
78
    def describe(self):
79
        return "%s is locked" % self.lockable_thing
5972.3.13 by Jelmer Vernooij
Add matcher for ancestry.
80
81
82
class _AncestryMismatch(Mismatch):
83
    """Ancestry matching mismatch."""
84
85
    def __init__(self, tip_revision, got, expected):
86
        self.tip_revision = tip_revision
87
        self.got = got
88
        self.expected = expected
89
90
    def describe(self):
91
        return "mismatched ancestry for revision %r was %r, expected %r" % (
92
            self.tip_revision, self.got, self.expected)
93
94
95
class MatchesAncestry(Matcher):
96
    """A matcher that checks the ancestry of a particular revision.
97
98
    :ivar graph: Graph in which to check the ancestry
99
    :ivar revision_id: Revision id of the revision
100
    """
101
102
    def __init__(self, repository, revision_id):
103
        Matcher.__init__(self)
104
        self.repository = repository
105
        self.revision_id = revision_id
106
107
    def __str__(self):
108
        return ('MatchesAncestry(repository=%r, revision_id=%r)' % (
109
            self.repository, self.revision_id))
110
111
    def match(self, expected):
6754.8.4 by Jelmer Vernooij
Use new context stuff.
112
        with self.repository.lock_read():
5972.3.13 by Jelmer Vernooij
Add matcher for ancestry.
113
            graph = self.repository.get_graph()
114
            got = [r for r, p in graph.iter_ancestry([self.revision_id])]
5972.3.20 by Jelmer Vernooij
fix test.
115
            if _mod_revision.NULL_REVISION in got:
116
                got.remove(_mod_revision.NULL_REVISION)
5972.3.13 by Jelmer Vernooij
Add matcher for ancestry.
117
        if sorted(got) != sorted(expected):
6072.2.1 by Jelmer Vernooij
Add HasLayout matcher.
118
            return _AncestryMismatch(self.revision_id, sorted(got),
7143.15.2 by Jelmer Vernooij
Run autopep8.
119
                                     sorted(expected))
6072.2.1 by Jelmer Vernooij
Add HasLayout matcher.
120
121
122
class HasLayout(Matcher):
123
    """A matcher that checks if a tree has a specific layout.
124
125
    :ivar entries: List of expected entries, as (path, file_id) pairs.
126
    """
127
128
    def __init__(self, entries):
129
        Matcher.__init__(self)
130
        self.entries = entries
131
6973.3.3 by Jelmer Vernooij
Don't require file ids in matchers.
132
    def get_tree_layout(self, tree, include_file_ids):
6072.2.1 by Jelmer Vernooij
Add HasLayout matcher.
133
        """Get the (path, file_id) pairs for the current tree."""
6754.8.4 by Jelmer Vernooij
Use new context stuff.
134
        with tree.lock_read():
6110.6.2 by Jelmer Vernooij
In HasLayout, take into consideration Tree.has_versioned_directories.
135
            for path, ie in tree.iter_entries_by_dir():
6973.3.3 by Jelmer Vernooij
Don't require file ids in matchers.
136
                if path != u'':
137
                    path += ie.kind_character()
138
                if include_file_ids:
139
                    yield (path, ie.file_id)
6110.6.2 by Jelmer Vernooij
In HasLayout, take into consideration Tree.has_versioned_directories.
140
                else:
6973.3.3 by Jelmer Vernooij
Don't require file ids in matchers.
141
                    yield path
6072.2.1 by Jelmer Vernooij
Add HasLayout matcher.
142
6110.6.2 by Jelmer Vernooij
In HasLayout, take into consideration Tree.has_versioned_directories.
143
    @staticmethod
144
    def _strip_unreferenced_directories(entries):
6110.6.3 by Jelmer Vernooij
review feedback from mgz
145
        """Strip all directories that don't (in)directly contain any files.
146
147
        :param entries: List of path strings or (path, ie) tuples to process
148
        """
6110.6.2 by Jelmer Vernooij
In HasLayout, take into consideration Tree.has_versioned_directories.
149
        directories = []
150
        for entry in entries:
7479.2.1 by Jelmer Vernooij
Drop python2 support.
151
            if isinstance(entry, str):
6110.6.2 by Jelmer Vernooij
In HasLayout, take into consideration Tree.has_versioned_directories.
152
                path = entry
153
            else:
154
                path = entry[0]
6110.6.3 by Jelmer Vernooij
review feedback from mgz
155
            if not path or path[-1] == "/":
6110.6.2 by Jelmer Vernooij
In HasLayout, take into consideration Tree.has_versioned_directories.
156
                # directory
157
                directories.append((path, entry))
158
            else:
159
                # Yield the referenced parent directories
160
                for dirpath, direntry in directories:
161
                    if osutils.is_inside(dirpath, path):
162
                        yield direntry
163
                directories = []
164
                yield entry
165
6072.2.1 by Jelmer Vernooij
Add HasLayout matcher.
166
    def __str__(self):
6110.6.2 by Jelmer Vernooij
In HasLayout, take into consideration Tree.has_versioned_directories.
167
        return 'HasLayout(%r)' % self.entries
6072.2.1 by Jelmer Vernooij
Add HasLayout matcher.
168
169
    def match(self, tree):
7143.15.2 by Jelmer Vernooij
Run autopep8.
170
        include_file_ids = self.entries and not isinstance(
7479.2.1 by Jelmer Vernooij
Drop python2 support.
171
            self.entries[0], str)
7143.15.2 by Jelmer Vernooij
Run autopep8.
172
        actual = list(self.get_tree_layout(
173
            tree, include_file_ids=include_file_ids))
6110.6.2 by Jelmer Vernooij
In HasLayout, take into consideration Tree.has_versioned_directories.
174
        if not tree.has_versioned_directories():
175
            entries = list(self._strip_unreferenced_directories(self.entries))
176
        else:
177
            entries = self.entries
6110.6.3 by Jelmer Vernooij
review feedback from mgz
178
        return Equals(entries).match(actual)
6228.3.1 by Jelmer Vernooij
Add RevisionHistoryMatches.
179
180
6883.5.4 by Jelmer Vernooij
Add HasPathRelations.
181
class HasPathRelations(Matcher):
182
    """Matcher verifies that paths have a relation to those in another tree.
183
184
    :ivar previous_tree: tree to compare to
185
    :ivar previous_entries: List of expected entries, as (path, previous_path) pairs.
186
    """
187
188
    def __init__(self, previous_tree, previous_entries):
189
        Matcher.__init__(self)
190
        self.previous_tree = previous_tree
191
        self.previous_entries = previous_entries
192
193
    def get_path_map(self, tree):
194
        """Get the (path, previous_path) pairs for the current tree."""
7357.1.8 by Jelmer Vernooij
Remove the InterTree object.
195
        previous_intertree = InterTree.get(self.previous_tree, tree)
6883.5.5 by Jelmer Vernooij
Add HasPathRelations.
196
        with tree.lock_read(), self.previous_tree.lock_read():
6883.5.4 by Jelmer Vernooij
Add HasPathRelations.
197
            for path, ie in tree.iter_entries_by_dir():
6883.5.19 by Jelmer Vernooij
Don't look for previous path if rename tracking is not supported.
198
                if tree.supports_rename_tracking():
7357.1.8 by Jelmer Vernooij
Remove the InterTree object.
199
                    previous_path = previous_intertree.find_source_path(path)
6883.5.19 by Jelmer Vernooij
Don't look for previous path if rename tracking is not supported.
200
                else:
201
                    if self.previous_tree.is_versioned(path):
202
                        previous_path = path
203
                    else:
204
                        previous_path = None
6883.5.15 by Jelmer Vernooij
Add kind characters.
205
                if previous_path:
206
                    kind = self.previous_tree.kind(previous_path)
207
                    if kind == 'directory':
208
                        previous_path += '/'
6973.3.3 by Jelmer Vernooij
Don't require file ids in matchers.
209
                if path == u'':
6883.5.4 by Jelmer Vernooij
Add HasPathRelations.
210
                    yield (u"", previous_path)
211
                else:
7143.15.2 by Jelmer Vernooij
Run autopep8.
212
                    yield (path + ie.kind_character(), previous_path)
6883.5.4 by Jelmer Vernooij
Add HasPathRelations.
213
214
    @staticmethod
215
    def _strip_unreferenced_directories(entries):
216
        """Strip all directories that don't (in)directly contain any files.
217
218
        :param entries: List of path strings or (path, previous_path) tuples to process
219
        """
6913.5.3 by Jelmer Vernooij
Simplify HasPathRelations behaviour; always require previous paths.
220
        directory_used = set()
6883.5.4 by Jelmer Vernooij
Add HasPathRelations.
221
        directories = []
6913.5.3 by Jelmer Vernooij
Simplify HasPathRelations behaviour; always require previous paths.
222
        for (path, previous_path) in entries:
6883.5.4 by Jelmer Vernooij
Add HasPathRelations.
223
            if not path or path[-1] == "/":
224
                # directory
6913.5.3 by Jelmer Vernooij
Simplify HasPathRelations behaviour; always require previous paths.
225
                directories.append((path, previous_path))
6883.5.4 by Jelmer Vernooij
Add HasPathRelations.
226
            else:
227
                # Yield the referenced parent directories
6913.5.3 by Jelmer Vernooij
Simplify HasPathRelations behaviour; always require previous paths.
228
                for direntry in directories:
229
                    if osutils.is_inside(direntry[0], path):
230
                        directory_used.add(direntry[0])
231
        for (path, previous_path) in entries:
232
            if (not path.endswith("/")) or path in directory_used:
233
                yield (path, previous_path)
6883.5.4 by Jelmer Vernooij
Add HasPathRelations.
234
235
    def __str__(self):
236
        return 'HasPathRelations(%r, %r)' % (self.previous_tree, self.previous_entries)
237
238
    def match(self, tree):
239
        actual = list(self.get_path_map(tree))
240
        if not tree.has_versioned_directories():
7143.15.2 by Jelmer Vernooij
Run autopep8.
241
            entries = list(self._strip_unreferenced_directories(
242
                self.previous_entries))
6883.5.4 by Jelmer Vernooij
Add HasPathRelations.
243
        else:
244
            entries = self.previous_entries
6883.5.17 by Jelmer Vernooij
Add Tree.supports_rename_tracking().
245
        if not tree.supports_rename_tracking():
246
            entries = [
6883.5.18 by Jelmer Vernooij
Fix test for rename tracking.
247
                (path, path if self.previous_tree.is_versioned(path) else None)
6883.5.17 by Jelmer Vernooij
Add Tree.supports_rename_tracking().
248
                for (path, previous_path) in entries]
6883.5.4 by Jelmer Vernooij
Add HasPathRelations.
249
        return Equals(entries).match(actual)
250
251
6228.3.1 by Jelmer Vernooij
Add RevisionHistoryMatches.
252
class RevisionHistoryMatches(Matcher):
253
    """A matcher that checks if a branch has a specific revision history.
254
255
    :ivar history: Revision history, as list of revisions. Oldest first.
256
    """
257
258
    def __init__(self, history):
259
        Matcher.__init__(self)
260
        self.expected = history
261
262
    def __str__(self):
263
        return 'RevisionHistoryMatches(%r)' % self.expected
264
265
    def match(self, branch):
6754.8.4 by Jelmer Vernooij
Use new context stuff.
266
        with branch.lock_read():
6228.3.1 by Jelmer Vernooij
Add RevisionHistoryMatches.
267
            graph = branch.repository.get_graph()
268
            history = list(graph.iter_lefthand_ancestry(
269
                branch.last_revision(), [_mod_revision.NULL_REVISION]))
270
            history.reverse()
271
        return Equals(self.expected).match(history)