67
85
def describe(self):
68
86
return "%s is locked" % self.lockable_thing
89
class _AncestryMismatch(Mismatch):
90
"""Ancestry matching mismatch."""
92
def __init__(self, tip_revision, got, expected):
93
self.tip_revision = tip_revision
95
self.expected = expected
98
return "mismatched ancestry for revision %r was %r, expected %r" % (
99
self.tip_revision, self.got, self.expected)
102
class MatchesAncestry(Matcher):
103
"""A matcher that checks the ancestry of a particular revision.
105
:ivar graph: Graph in which to check the ancestry
106
:ivar revision_id: Revision id of the revision
109
def __init__(self, repository, revision_id):
110
Matcher.__init__(self)
111
self.repository = repository
112
self.revision_id = revision_id
115
return ('MatchesAncestry(repository=%r, revision_id=%r)' % (
116
self.repository, self.revision_id))
118
def match(self, expected):
119
self.repository.lock_read()
121
graph = self.repository.get_graph()
122
got = [r for r, p in graph.iter_ancestry([self.revision_id])]
123
if _mod_revision.NULL_REVISION in got:
124
got.remove(_mod_revision.NULL_REVISION)
126
self.repository.unlock()
127
if sorted(got) != sorted(expected):
128
return _AncestryMismatch(self.revision_id, sorted(got),
132
class HasLayout(Matcher):
133
"""A matcher that checks if a tree has a specific layout.
135
:ivar entries: List of expected entries, as (path, file_id) pairs.
138
def __init__(self, entries):
139
Matcher.__init__(self)
140
self.entries = entries
142
def get_tree_layout(self, tree):
143
"""Get the (path, file_id) pairs for the current tree."""
146
for path, ie in tree.iter_entries_by_dir():
147
if ie.parent_id is None:
148
yield (u"", ie.file_id)
150
yield (path+ie.kind_character(), ie.file_id)
155
def _strip_unreferenced_directories(entries):
156
"""Strip all directories that don't (in)directly contain any files.
158
:param entries: List of path strings or (path, ie) tuples to process
161
for entry in entries:
162
if isinstance(entry, (str, text_type)):
166
if not path or path[-1] == "/":
168
directories.append((path, entry))
170
# Yield the referenced parent directories
171
for dirpath, direntry in directories:
172
if osutils.is_inside(dirpath, path):
178
return 'HasLayout(%r)' % self.entries
180
def match(self, tree):
181
actual = list(self.get_tree_layout(tree))
182
if self.entries and isinstance(self.entries[0], (str, text_type)):
183
actual = [path for (path, fileid) in actual]
184
if not tree.has_versioned_directories():
185
entries = list(self._strip_unreferenced_directories(self.entries))
187
entries = self.entries
188
return Equals(entries).match(actual)
191
class RevisionHistoryMatches(Matcher):
192
"""A matcher that checks if a branch has a specific revision history.
194
:ivar history: Revision history, as list of revisions. Oldest first.
197
def __init__(self, history):
198
Matcher.__init__(self)
199
self.expected = history
202
return 'RevisionHistoryMatches(%r)' % self.expected
204
def match(self, branch):
207
graph = branch.repository.get_graph()
208
history = list(graph.iter_lefthand_ancestry(
209
branch.last_revision(), [_mod_revision.NULL_REVISION]))
213
return Equals(self.expected).match(history)
216
class _NoVfsCallsMismatch(Mismatch):
217
"""Mismatch describing a list of HPSS calls which includes VFS requests."""
219
def __init__(self, vfs_calls):
220
self.vfs_calls = vfs_calls
223
return "no VFS calls expected, got: %s" % ",".join([
224
"%s(%s)" % (c.method,
225
", ".join([repr(a) for a in c.args])) for c in self.vfs_calls])
228
class ContainsNoVfsCalls(Matcher):
229
"""Ensure that none of the specified calls are HPSS calls."""
232
return 'ContainsNoVfsCalls()'
235
def match(cls, hpss_calls):
237
for call in hpss_calls:
239
request_method = smart_request_handlers.get(call.call.method)
241
# A method we don't know about doesn't count as a VFS method.
243
if issubclass(request_method, vfs.VfsRequest):
244
vfs_calls.append(call.call)
245
if len(vfs_calls) == 0:
247
return _NoVfsCallsMismatch(vfs_calls)