~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/matchers.py

(gz) Fix deprecations of win32utils path function unicode wrappers (Martin
 Packman)

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
"""
28
28
 
29
29
__all__ = [
 
30
    'HasLayout',
30
31
    'MatchesAncestry',
 
32
    'ContainsNoVfsCalls',
31
33
    'ReturnsUnlockable',
32
34
    ]
33
35
 
34
36
from bzrlib import (
 
37
    osutils,
35
38
    revision as _mod_revision,
36
39
    )
 
40
from bzrlib import lazy_import
 
41
lazy_import.lazy_import(globals(),
 
42
"""
 
43
from bzrlib.smart.request import request_handlers as smart_request_handlers
 
44
from bzrlib.smart import vfs
 
45
""")
37
46
 
38
 
from testtools.matchers import Mismatch, Matcher
 
47
from testtools.matchers import Equals, Mismatch, Matcher
39
48
 
40
49
 
41
50
class ReturnsUnlockable(Matcher):
53
62
        self.lockable_thing = lockable_thing
54
63
 
55
64
    def __str__(self):
56
 
        return ('ReturnsUnlockable(lockable_thing=%s)' % 
 
65
        return ('ReturnsUnlockable(lockable_thing=%s)' %
57
66
            self.lockable_thing)
58
67
 
59
68
    def match(self, lock_method):
112
121
        finally:
113
122
            self.repository.unlock()
114
123
        if sorted(got) != sorted(expected):
115
 
            return _AncestryMismatch(self.revision_id, sorted(got), sorted(expected))
 
124
            return _AncestryMismatch(self.revision_id, sorted(got),
 
125
                sorted(expected))
 
126
 
 
127
 
 
128
class HasLayout(Matcher):
 
129
    """A matcher that checks if a tree has a specific layout.
 
130
 
 
131
    :ivar entries: List of expected entries, as (path, file_id) pairs.
 
132
    """
 
133
 
 
134
    def __init__(self, entries):
 
135
        Matcher.__init__(self)
 
136
        self.entries = entries
 
137
 
 
138
    def get_tree_layout(self, tree):
 
139
        """Get the (path, file_id) pairs for the current tree."""
 
140
        tree.lock_read()
 
141
        try:
 
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
        finally:
 
148
            tree.unlock()
 
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, basestring):
 
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], basestring):
 
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 _NoVfsCallsMismatch(Mismatch):
 
188
    """Mismatch describing a list of HPSS calls which includes VFS requests."""
 
189
 
 
190
    def __init__(self, vfs_calls):
 
191
        self.vfs_calls = vfs_calls
 
192
 
 
193
    def describe(self):
 
194
        return "no VFS calls expected, got: %s" % ",".join([
 
195
            "%s(%s)" % (c.method,
 
196
                ", ".join([repr(a) for a in c.args])) for c in self.vfs_calls])
 
197
 
 
198
 
 
199
class ContainsNoVfsCalls(Matcher):
 
200
    """Ensure that none of the specified calls are HPSS calls."""
 
201
 
 
202
    def __str__(self):
 
203
        return 'ContainsNoVfsCalls()'
 
204
 
 
205
    @classmethod
 
206
    def match(cls, hpss_calls):
 
207
        vfs_calls = []
 
208
        for call in hpss_calls:
 
209
            try:
 
210
                request_method = smart_request_handlers.get(call.call.method)
 
211
            except KeyError:
 
212
                # A method we don't know about doesn't count as a VFS method.
 
213
                continue
 
214
            if issubclass(request_method, vfs.VfsRequest):
 
215
                vfs_calls.append(call.call)
 
216
        if len(vfs_calls) == 0:
 
217
            return None
 
218
        return _NoVfsCallsMismatch(vfs_calls)