82
67
def describe(self):
83
68
return "%s is locked" % self.lockable_thing
86
class _AncestryMismatch(Mismatch):
87
"""Ancestry matching mismatch."""
89
def __init__(self, tip_revision, got, expected):
90
self.tip_revision = tip_revision
92
self.expected = expected
95
return "mismatched ancestry for revision %r was %r, expected %r" % (
96
self.tip_revision, self.got, self.expected)
99
class MatchesAncestry(Matcher):
100
"""A matcher that checks the ancestry of a particular revision.
102
:ivar graph: Graph in which to check the ancestry
103
:ivar revision_id: Revision id of the revision
106
def __init__(self, repository, revision_id):
107
Matcher.__init__(self)
108
self.repository = repository
109
self.revision_id = revision_id
112
return ('MatchesAncestry(repository=%r, revision_id=%r)' % (
113
self.repository, self.revision_id))
115
def match(self, expected):
116
self.repository.lock_read()
118
graph = self.repository.get_graph()
119
got = [r for r, p in graph.iter_ancestry([self.revision_id])]
120
if _mod_revision.NULL_REVISION in got:
121
got.remove(_mod_revision.NULL_REVISION)
123
self.repository.unlock()
124
if sorted(got) != sorted(expected):
125
return _AncestryMismatch(self.revision_id, sorted(got),
129
class HasLayout(Matcher):
130
"""A matcher that checks if a tree has a specific layout.
132
:ivar entries: List of expected entries, as (path, file_id) pairs.
135
def __init__(self, entries):
136
Matcher.__init__(self)
137
self.entries = entries
139
def get_tree_layout(self, tree):
140
"""Get the (path, file_id) pairs for the current tree."""
143
for path, ie in tree.iter_entries_by_dir():
144
if ie.parent_id is None:
145
yield (u"", ie.file_id)
147
yield (path+ie.kind_character(), ie.file_id)
152
def _strip_unreferenced_directories(entries):
153
"""Strip all directories that don't (in)directly contain any files.
155
:param entries: List of path strings or (path, ie) tuples to process
158
for entry in entries:
159
if isinstance(entry, basestring):
163
if not path or path[-1] == "/":
165
directories.append((path, entry))
167
# Yield the referenced parent directories
168
for dirpath, direntry in directories:
169
if osutils.is_inside(dirpath, path):
175
return 'HasLayout(%r)' % self.entries
177
def match(self, tree):
178
actual = list(self.get_tree_layout(tree))
179
if self.entries and isinstance(self.entries[0], basestring):
180
actual = [path for (path, fileid) in actual]
181
if not tree.has_versioned_directories():
182
entries = list(self._strip_unreferenced_directories(self.entries))
184
entries = self.entries
185
return Equals(entries).match(actual)
188
class RevisionHistoryMatches(Matcher):
189
"""A matcher that checks if a branch has a specific revision history.
191
:ivar history: Revision history, as list of revisions. Oldest first.
194
def __init__(self, history):
195
Matcher.__init__(self)
196
self.expected = history
199
return 'RevisionHistoryMatches(%r)' % self.expected
201
def match(self, branch):
204
graph = branch.repository.get_graph()
205
history = list(graph.iter_lefthand_ancestry(
206
branch.last_revision(), [_mod_revision.NULL_REVISION]))
210
return Equals(self.expected).match(history)
213
class _NoVfsCallsMismatch(Mismatch):
214
"""Mismatch describing a list of HPSS calls which includes VFS requests."""
216
def __init__(self, vfs_calls):
217
self.vfs_calls = vfs_calls
220
return "no VFS calls expected, got: %s" % ",".join([
221
"%s(%s)" % (c.method,
222
", ".join([repr(a) for a in c.args])) for c in self.vfs_calls])
225
class ContainsNoVfsCalls(Matcher):
226
"""Ensure that none of the specified calls are HPSS calls."""
229
return 'ContainsNoVfsCalls()'
232
def match(cls, hpss_calls):
234
for call in hpss_calls:
236
request_method = smart_request_handlers.get(call.call.method)
238
# A method we don't know about doesn't count as a VFS method.
240
if issubclass(request_method, vfs.VfsRequest):
241
vfs_calls.append(call.call)
242
if len(vfs_calls) == 0:
244
return _NoVfsCallsMismatch(vfs_calls)