/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/matchers.py

  • Committer: Jelmer Vernooij
  • Date: 2018-05-22 01:30:48 UTC
  • mto: (6973.5.4 python3-c)
  • mto: This revision was merged to the branch mainline in revision 6984.
  • Revision ID: jelmer@jelmer.uk-20180522013048-r825wjanpr2yqige
port breezy.bzr.weave to Python3.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
 
"""Matchers for bzrlib.
 
17
"""Matchers for breezy.
18
18
 
19
 
Primarily test support, Matchers are used by self.assertThat in the bzrlib
 
19
Primarily test support, Matchers are used by self.assertThat in the breezy
20
20
test suite. A matcher is a stateful test helper which can be used to determine
21
21
if a passed object 'matches', much like a regex. If the object does not match
22
22
the mismatch can be described in a human readable fashion. assertThat then
27
27
"""
28
28
 
29
29
__all__ = [
 
30
    'HasLayout',
 
31
    'HasPathRelations',
 
32
    'MatchesAncestry',
 
33
    'ContainsNoVfsCalls',
30
34
    'ReturnsUnlockable',
 
35
    'RevisionHistoryMatches',
31
36
    ]
32
37
 
33
 
from testtools.matchers import Mismatch, Matcher
 
38
from .. import (
 
39
    osutils,
 
40
    revision as _mod_revision,
 
41
    )
 
42
from .. import lazy_import
 
43
lazy_import.lazy_import(globals(),
 
44
"""
 
45
from breezy.bzr.smart.request import request_handlers as smart_request_handlers
 
46
from breezy.bzr.smart import vfs
 
47
""")
 
48
from ..sixish import (
 
49
    text_type,
 
50
    )
 
51
from ..tree import find_previous_path
 
52
 
 
53
from testtools.matchers import Equals, Mismatch, Matcher
34
54
 
35
55
 
36
56
class ReturnsUnlockable(Matcher):
48
68
        self.lockable_thing = lockable_thing
49
69
 
50
70
    def __str__(self):
51
 
        return ('ReturnsUnlockable(lockable_thing=%s)' % 
 
71
        return ('ReturnsUnlockable(lockable_thing=%s)' %
52
72
            self.lockable_thing)
53
73
 
54
74
    def match(self, lock_method):
66
86
 
67
87
    def describe(self):
68
88
        return "%s is locked" % self.lockable_thing
 
89
 
 
90
 
 
91
class _AncestryMismatch(Mismatch):
 
92
    """Ancestry matching mismatch."""
 
93
 
 
94
    def __init__(self, tip_revision, got, expected):
 
95
        self.tip_revision = tip_revision
 
96
        self.got = got
 
97
        self.expected = expected
 
98
 
 
99
    def describe(self):
 
100
        return "mismatched ancestry for revision %r was %r, expected %r" % (
 
101
            self.tip_revision, self.got, self.expected)
 
102
 
 
103
 
 
104
class MatchesAncestry(Matcher):
 
105
    """A matcher that checks the ancestry of a particular revision.
 
106
 
 
107
    :ivar graph: Graph in which to check the ancestry
 
108
    :ivar revision_id: Revision id of the revision
 
109
    """
 
110
 
 
111
    def __init__(self, repository, revision_id):
 
112
        Matcher.__init__(self)
 
113
        self.repository = repository
 
114
        self.revision_id = revision_id
 
115
 
 
116
    def __str__(self):
 
117
        return ('MatchesAncestry(repository=%r, revision_id=%r)' % (
 
118
            self.repository, self.revision_id))
 
119
 
 
120
    def match(self, expected):
 
121
        with self.repository.lock_read():
 
122
            graph = self.repository.get_graph()
 
123
            got = [r for r, p in graph.iter_ancestry([self.revision_id])]
 
124
            if _mod_revision.NULL_REVISION in got:
 
125
                got.remove(_mod_revision.NULL_REVISION)
 
126
        if sorted(got) != sorted(expected):
 
127
            return _AncestryMismatch(self.revision_id, sorted(got),
 
128
                sorted(expected))
 
129
 
 
130
 
 
131
class HasLayout(Matcher):
 
132
    """A matcher that checks if a tree has a specific layout.
 
133
 
 
134
    :ivar entries: List of expected entries, as (path, file_id) pairs.
 
135
    """
 
136
 
 
137
    def __init__(self, entries):
 
138
        Matcher.__init__(self)
 
139
        self.entries = entries
 
140
 
 
141
    def get_tree_layout(self, tree):
 
142
        """Get the (path, file_id) pairs for the current tree."""
 
143
        with tree.lock_read():
 
144
            for path, ie in tree.iter_entries_by_dir():
 
145
                if ie.parent_id is None:
 
146
                    yield (u"", ie.file_id)
 
147
                else:
 
148
                    yield (path+ie.kind_character(), ie.file_id)
 
149
 
 
150
    @staticmethod
 
151
    def _strip_unreferenced_directories(entries):
 
152
        """Strip all directories that don't (in)directly contain any files.
 
153
 
 
154
        :param entries: List of path strings or (path, ie) tuples to process
 
155
        """
 
156
        directories = []
 
157
        for entry in entries:
 
158
            if isinstance(entry, (str, text_type)):
 
159
                path = entry
 
160
            else:
 
161
                path = entry[0]
 
162
            if not path or path[-1] == "/":
 
163
                # directory
 
164
                directories.append((path, entry))
 
165
            else:
 
166
                # Yield the referenced parent directories
 
167
                for dirpath, direntry in directories:
 
168
                    if osutils.is_inside(dirpath, path):
 
169
                        yield direntry
 
170
                directories = []
 
171
                yield entry
 
172
 
 
173
    def __str__(self):
 
174
        return 'HasLayout(%r)' % self.entries
 
175
 
 
176
    def match(self, tree):
 
177
        actual = list(self.get_tree_layout(tree))
 
178
        if self.entries and isinstance(self.entries[0], (str, text_type)):
 
179
            actual = [path for (path, fileid) in actual]
 
180
        if not tree.has_versioned_directories():
 
181
            entries = list(self._strip_unreferenced_directories(self.entries))
 
182
        else:
 
183
            entries = self.entries
 
184
        return Equals(entries).match(actual)
 
185
 
 
186
 
 
187
class HasPathRelations(Matcher):
 
188
    """Matcher verifies that paths have a relation to those in another tree.
 
189
 
 
190
    :ivar previous_tree: tree to compare to
 
191
    :ivar previous_entries: List of expected entries, as (path, previous_path) pairs.
 
192
    """
 
193
 
 
194
    def __init__(self, previous_tree, previous_entries):
 
195
        Matcher.__init__(self)
 
196
        self.previous_tree = previous_tree
 
197
        self.previous_entries = previous_entries
 
198
 
 
199
    def get_path_map(self, tree):
 
200
        """Get the (path, previous_path) pairs for the current tree."""
 
201
        with tree.lock_read(), self.previous_tree.lock_read():
 
202
            for path, ie in tree.iter_entries_by_dir():
 
203
                if tree.supports_rename_tracking():
 
204
                    previous_path = find_previous_path(tree, self.previous_tree, path)
 
205
                else:
 
206
                    if self.previous_tree.is_versioned(path):
 
207
                        previous_path = path
 
208
                    else:
 
209
                        previous_path = None
 
210
                if previous_path:
 
211
                    kind = self.previous_tree.kind(previous_path)
 
212
                    if kind == 'directory':
 
213
                        previous_path += '/'
 
214
                if ie.parent_id is None:
 
215
                    yield (u"", previous_path)
 
216
                else:
 
217
                    yield (path+ie.kind_character(), previous_path)
 
218
 
 
219
    @staticmethod
 
220
    def _strip_unreferenced_directories(entries):
 
221
        """Strip all directories that don't (in)directly contain any files.
 
222
 
 
223
        :param entries: List of path strings or (path, previous_path) tuples to process
 
224
        """
 
225
        directory_used = set()
 
226
        directories = []
 
227
        for (path, previous_path) in entries:
 
228
            if not path or path[-1] == "/":
 
229
                # directory
 
230
                directories.append((path, previous_path))
 
231
            else:
 
232
                # Yield the referenced parent directories
 
233
                for direntry in directories:
 
234
                    if osutils.is_inside(direntry[0], path):
 
235
                        directory_used.add(direntry[0])
 
236
        for (path, previous_path) in entries:
 
237
            if (not path.endswith("/")) or path in directory_used:
 
238
                yield (path, previous_path)
 
239
 
 
240
    def __str__(self):
 
241
        return 'HasPathRelations(%r, %r)' % (self.previous_tree, self.previous_entries)
 
242
 
 
243
    def match(self, tree):
 
244
        actual = list(self.get_path_map(tree))
 
245
        if not tree.has_versioned_directories():
 
246
            entries = list(self._strip_unreferenced_directories(self.previous_entries))
 
247
        else:
 
248
            entries = self.previous_entries
 
249
        if not tree.supports_rename_tracking():
 
250
            entries = [
 
251
                (path, path if self.previous_tree.is_versioned(path) else None)
 
252
                for (path, previous_path) in entries]
 
253
        return Equals(entries).match(actual)
 
254
 
 
255
 
 
256
class RevisionHistoryMatches(Matcher):
 
257
    """A matcher that checks if a branch has a specific revision history.
 
258
 
 
259
    :ivar history: Revision history, as list of revisions. Oldest first.
 
260
    """
 
261
 
 
262
    def __init__(self, history):
 
263
        Matcher.__init__(self)
 
264
        self.expected = history
 
265
 
 
266
    def __str__(self):
 
267
        return 'RevisionHistoryMatches(%r)' % self.expected
 
268
 
 
269
    def match(self, branch):
 
270
        with branch.lock_read():
 
271
            graph = branch.repository.get_graph()
 
272
            history = list(graph.iter_lefthand_ancestry(
 
273
                branch.last_revision(), [_mod_revision.NULL_REVISION]))
 
274
            history.reverse()
 
275
        return Equals(self.expected).match(history)
 
276
 
 
277
 
 
278
class _NoVfsCallsMismatch(Mismatch):
 
279
    """Mismatch describing a list of HPSS calls which includes VFS requests."""
 
280
 
 
281
    def __init__(self, vfs_calls):
 
282
        self.vfs_calls = vfs_calls
 
283
 
 
284
    def describe(self):
 
285
        return "no VFS calls expected, got: %s" % ",".join([
 
286
            "%s(%s)" % (c.method,
 
287
                ", ".join([repr(a) for a in c.args])) for c in self.vfs_calls])
 
288
 
 
289
 
 
290
class ContainsNoVfsCalls(Matcher):
 
291
    """Ensure that none of the specified calls are HPSS calls."""
 
292
 
 
293
    def __str__(self):
 
294
        return 'ContainsNoVfsCalls()'
 
295
 
 
296
    @classmethod
 
297
    def match(cls, hpss_calls):
 
298
        vfs_calls = []
 
299
        for call in hpss_calls:
 
300
            try:
 
301
                request_method = smart_request_handlers.get(call.call.method)
 
302
            except KeyError:
 
303
                # A method we don't know about doesn't count as a VFS method.
 
304
                continue
 
305
            if issubclass(request_method, vfs.VfsRequest):
 
306
                vfs_calls.append(call.call)
 
307
        if len(vfs_calls) == 0:
 
308
            return None
 
309
        return _NoVfsCallsMismatch(vfs_calls)