/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-03-01 23:44:14 UTC
  • mto: This revision was merged to the branch mainline in revision 6871.
  • Revision ID: jelmer@jelmer.uk-20180301234414-5btxmc1z8pz131ob
Fix context manager for RecordingUIFactory.

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
    'MatchesAncestry',
 
32
    'ContainsNoVfsCalls',
30
33
    'ReturnsUnlockable',
 
34
    'RevisionHistoryMatches',
31
35
    ]
32
36
 
33
 
from testtools.matchers import Mismatch, Matcher
 
37
from .. import (
 
38
    osutils,
 
39
    revision as _mod_revision,
 
40
    )
 
41
from .. import lazy_import
 
42
lazy_import.lazy_import(globals(),
 
43
"""
 
44
from breezy.bzr.smart.request import request_handlers as smart_request_handlers
 
45
from breezy.bzr.smart import vfs
 
46
""")
 
47
from ..sixish import (
 
48
    text_type,
 
49
    )
 
50
 
 
51
from testtools.matchers import Equals, Mismatch, Matcher
34
52
 
35
53
 
36
54
class ReturnsUnlockable(Matcher):
48
66
        self.lockable_thing = lockable_thing
49
67
 
50
68
    def __str__(self):
51
 
        return ('ReturnsUnlockable(lockable_thing=%s)' % 
 
69
        return ('ReturnsUnlockable(lockable_thing=%s)' %
52
70
            self.lockable_thing)
53
71
 
54
72
    def match(self, lock_method):
66
84
 
67
85
    def describe(self):
68
86
        return "%s is locked" % self.lockable_thing
 
87
 
 
88
 
 
89
class _AncestryMismatch(Mismatch):
 
90
    """Ancestry matching mismatch."""
 
91
 
 
92
    def __init__(self, tip_revision, got, expected):
 
93
        self.tip_revision = tip_revision
 
94
        self.got = got
 
95
        self.expected = expected
 
96
 
 
97
    def describe(self):
 
98
        return "mismatched ancestry for revision %r was %r, expected %r" % (
 
99
            self.tip_revision, self.got, self.expected)
 
100
 
 
101
 
 
102
class MatchesAncestry(Matcher):
 
103
    """A matcher that checks the ancestry of a particular revision.
 
104
 
 
105
    :ivar graph: Graph in which to check the ancestry
 
106
    :ivar revision_id: Revision id of the revision
 
107
    """
 
108
 
 
109
    def __init__(self, repository, revision_id):
 
110
        Matcher.__init__(self)
 
111
        self.repository = repository
 
112
        self.revision_id = revision_id
 
113
 
 
114
    def __str__(self):
 
115
        return ('MatchesAncestry(repository=%r, revision_id=%r)' % (
 
116
            self.repository, self.revision_id))
 
117
 
 
118
    def match(self, expected):
 
119
        with self.repository.lock_read():
 
120
            graph = self.repository.get_graph()
 
121
            got = [r for r, p in graph.iter_ancestry([self.revision_id])]
 
122
            if _mod_revision.NULL_REVISION in got:
 
123
                got.remove(_mod_revision.NULL_REVISION)
 
124
        if sorted(got) != sorted(expected):
 
125
            return _AncestryMismatch(self.revision_id, sorted(got),
 
126
                sorted(expected))
 
127
 
 
128
 
 
129
class HasLayout(Matcher):
 
130
    """A matcher that checks if a tree has a specific layout.
 
131
 
 
132
    :ivar entries: List of expected entries, as (path, file_id) pairs.
 
133
    """
 
134
 
 
135
    def __init__(self, entries):
 
136
        Matcher.__init__(self)
 
137
        self.entries = entries
 
138
 
 
139
    def get_tree_layout(self, tree):
 
140
        """Get the (path, file_id) pairs for the current tree."""
 
141
        with tree.lock_read():
 
142
            for path, ie in tree.iter_entries_by_dir():
 
143
                if ie.parent_id is None:
 
144
                    yield (u"", ie.file_id)
 
145
                else:
 
146
                    yield (path+ie.kind_character(), ie.file_id)
 
147
 
 
148
    @staticmethod
 
149
    def _strip_unreferenced_directories(entries):
 
150
        """Strip all directories that don't (in)directly contain any files.
 
151
 
 
152
        :param entries: List of path strings or (path, ie) tuples to process
 
153
        """
 
154
        directories = []
 
155
        for entry in entries:
 
156
            if isinstance(entry, (str, text_type)):
 
157
                path = entry
 
158
            else:
 
159
                path = entry[0]
 
160
            if not path or path[-1] == "/":
 
161
                # directory
 
162
                directories.append((path, entry))
 
163
            else:
 
164
                # Yield the referenced parent directories
 
165
                for dirpath, direntry in directories:
 
166
                    if osutils.is_inside(dirpath, path):
 
167
                        yield direntry
 
168
                directories = []
 
169
                yield entry
 
170
 
 
171
    def __str__(self):
 
172
        return 'HasLayout(%r)' % self.entries
 
173
 
 
174
    def match(self, tree):
 
175
        actual = list(self.get_tree_layout(tree))
 
176
        if self.entries and isinstance(self.entries[0], (str, text_type)):
 
177
            actual = [path for (path, fileid) in actual]
 
178
        if not tree.has_versioned_directories():
 
179
            entries = list(self._strip_unreferenced_directories(self.entries))
 
180
        else:
 
181
            entries = self.entries
 
182
        return Equals(entries).match(actual)
 
183
 
 
184
 
 
185
class RevisionHistoryMatches(Matcher):
 
186
    """A matcher that checks if a branch has a specific revision history.
 
187
 
 
188
    :ivar history: Revision history, as list of revisions. Oldest first.
 
189
    """
 
190
 
 
191
    def __init__(self, history):
 
192
        Matcher.__init__(self)
 
193
        self.expected = history
 
194
 
 
195
    def __str__(self):
 
196
        return 'RevisionHistoryMatches(%r)' % self.expected
 
197
 
 
198
    def match(self, branch):
 
199
        with branch.lock_read():
 
200
            graph = branch.repository.get_graph()
 
201
            history = list(graph.iter_lefthand_ancestry(
 
202
                branch.last_revision(), [_mod_revision.NULL_REVISION]))
 
203
            history.reverse()
 
204
        return Equals(self.expected).match(history)
 
205
 
 
206
 
 
207
class _NoVfsCallsMismatch(Mismatch):
 
208
    """Mismatch describing a list of HPSS calls which includes VFS requests."""
 
209
 
 
210
    def __init__(self, vfs_calls):
 
211
        self.vfs_calls = vfs_calls
 
212
 
 
213
    def describe(self):
 
214
        return "no VFS calls expected, got: %s" % ",".join([
 
215
            "%s(%s)" % (c.method,
 
216
                ", ".join([repr(a) for a in c.args])) for c in self.vfs_calls])
 
217
 
 
218
 
 
219
class ContainsNoVfsCalls(Matcher):
 
220
    """Ensure that none of the specified calls are HPSS calls."""
 
221
 
 
222
    def __str__(self):
 
223
        return 'ContainsNoVfsCalls()'
 
224
 
 
225
    @classmethod
 
226
    def match(cls, hpss_calls):
 
227
        vfs_calls = []
 
228
        for call in hpss_calls:
 
229
            try:
 
230
                request_method = smart_request_handlers.get(call.call.method)
 
231
            except KeyError:
 
232
                # A method we don't know about doesn't count as a VFS method.
 
233
                continue
 
234
            if issubclass(request_method, vfs.VfsRequest):
 
235
                vfs_calls.append(call.call)
 
236
        if len(vfs_calls) == 0:
 
237
            return None
 
238
        return _NoVfsCallsMismatch(vfs_calls)