5200.3.1
by Robert Collins
Added ``bzrlib.tests.matchers`` as a place to put matchers, along with |
1 |
# Copyright (C) 2010 Canonical Ltd
|
2 |
#
|
|
3 |
# This program is free software; you can redistribute it and/or modify
|
|
4 |
# it under the terms of the GNU General Public License as published by
|
|
5 |
# the Free Software Foundation; either version 2 of the License, or
|
|
6 |
# (at your option) any later version.
|
|
7 |
#
|
|
8 |
# This program is distributed in the hope that it will be useful,
|
|
9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
11 |
# GNU General Public License for more details.
|
|
12 |
#
|
|
13 |
# You should have received a copy of the GNU General Public License
|
|
14 |
# along with this program; if not, write to the Free Software
|
|
15 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
16 |
||
17 |
"""Matchers for bzrlib.
|
|
18 |
||
19 |
Primarily test support, Matchers are used by self.assertThat in the bzrlib
|
|
20 |
test suite. A matcher is a stateful test helper which can be used to determine
|
|
21 |
if a passed object 'matches', much like a regex. If the object does not match
|
|
22 |
the mismatch can be described in a human readable fashion. assertThat then
|
|
23 |
raises if a mismatch occurs, showing the description as the assertion error.
|
|
24 |
||
25 |
Matchers are designed to be more reusable and composable than layered
|
|
26 |
assertions in Test Case objects, so they are recommended for new testing work.
|
|
27 |
"""
|
|
28 |
||
29 |
__all__ = [ |
|
6072.2.4
by Jelmer Vernooij
tests for matcher |
30 |
'HasLayout', |
5972.3.13
by Jelmer Vernooij
Add matcher for ancestry. |
31 |
'MatchesAncestry', |
6352.2.3
by Jelmer Vernooij
s/NoVfsCalls/ContainsNoVfsCalls/. |
32 |
'ContainsNoVfsCalls', |
5200.3.2
by Robert Collins
Cleaner matcher matching revised unlocking protocol. |
33 |
'ReturnsUnlockable', |
5200.3.1
by Robert Collins
Added ``bzrlib.tests.matchers`` as a place to put matchers, along with |
34 |
]
|
35 |
||
5972.3.13
by Jelmer Vernooij
Add matcher for ancestry. |
36 |
from bzrlib import ( |
6110.6.2
by Jelmer Vernooij
In HasLayout, take into consideration Tree.has_versioned_directories. |
37 |
osutils, |
5972.3.13
by Jelmer Vernooij
Add matcher for ancestry. |
38 |
revision as _mod_revision, |
39 |
)
|
|
6352.2.1
by Jelmer Vernooij
Add matcher for NoVfsCalls. |
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 |
""") |
|
5972.3.13
by Jelmer Vernooij
Add matcher for ancestry. |
46 |
|
6072.2.1
by Jelmer Vernooij
Add HasLayout matcher. |
47 |
from testtools.matchers import Equals, Mismatch, Matcher |
5200.3.1
by Robert Collins
Added ``bzrlib.tests.matchers`` as a place to put matchers, along with |
48 |
|
49 |
||
5200.3.2
by Robert Collins
Cleaner matcher matching revised unlocking protocol. |
50 |
class ReturnsUnlockable(Matcher): |
5200.3.1
by Robert Collins
Added ``bzrlib.tests.matchers`` as a place to put matchers, along with |
51 |
"""A matcher that checks for the pattern we want lock* methods to have:
|
52 |
||
5200.3.2
by Robert Collins
Cleaner matcher matching revised unlocking protocol. |
53 |
They should return an object with an unlock() method.
|
54 |
Calling that method should unlock the original object.
|
|
5200.3.1
by Robert Collins
Added ``bzrlib.tests.matchers`` as a place to put matchers, along with |
55 |
|
56 |
:ivar lockable_thing: The object which can be locked that will be
|
|
57 |
inspected.
|
|
58 |
"""
|
|
59 |
||
60 |
def __init__(self, lockable_thing): |
|
61 |
Matcher.__init__(self) |
|
62 |
self.lockable_thing = lockable_thing |
|
63 |
||
64 |
def __str__(self): |
|
6110.6.2
by Jelmer Vernooij
In HasLayout, take into consideration Tree.has_versioned_directories. |
65 |
return ('ReturnsUnlockable(lockable_thing=%s)' % |
5200.3.1
by Robert Collins
Added ``bzrlib.tests.matchers`` as a place to put matchers, along with |
66 |
self.lockable_thing) |
67 |
||
68 |
def match(self, lock_method): |
|
5200.3.2
by Robert Collins
Cleaner matcher matching revised unlocking protocol. |
69 |
lock_method().unlock() |
5200.3.1
by Robert Collins
Added ``bzrlib.tests.matchers`` as a place to put matchers, along with |
70 |
if self.lockable_thing.is_locked(): |
71 |
return _IsLocked(self.lockable_thing) |
|
72 |
return None |
|
73 |
||
74 |
||
75 |
class _IsLocked(Mismatch): |
|
76 |
"""Something is locked."""
|
|
77 |
||
78 |
def __init__(self, lockable_thing): |
|
79 |
self.lockable_thing = lockable_thing |
|
80 |
||
81 |
def describe(self): |
|
82 |
return "%s is locked" % self.lockable_thing |
|
5972.3.13
by Jelmer Vernooij
Add matcher for ancestry. |
83 |
|
84 |
||
85 |
class _AncestryMismatch(Mismatch): |
|
86 |
"""Ancestry matching mismatch."""
|
|
87 |
||
88 |
def __init__(self, tip_revision, got, expected): |
|
89 |
self.tip_revision = tip_revision |
|
90 |
self.got = got |
|
91 |
self.expected = expected |
|
92 |
||
93 |
def describe(self): |
|
94 |
return "mismatched ancestry for revision %r was %r, expected %r" % ( |
|
95 |
self.tip_revision, self.got, self.expected) |
|
96 |
||
97 |
||
98 |
class MatchesAncestry(Matcher): |
|
99 |
"""A matcher that checks the ancestry of a particular revision.
|
|
100 |
||
101 |
:ivar graph: Graph in which to check the ancestry
|
|
102 |
:ivar revision_id: Revision id of the revision
|
|
103 |
"""
|
|
104 |
||
105 |
def __init__(self, repository, revision_id): |
|
106 |
Matcher.__init__(self) |
|
107 |
self.repository = repository |
|
108 |
self.revision_id = revision_id |
|
109 |
||
110 |
def __str__(self): |
|
111 |
return ('MatchesAncestry(repository=%r, revision_id=%r)' % ( |
|
112 |
self.repository, self.revision_id)) |
|
113 |
||
114 |
def match(self, expected): |
|
115 |
self.repository.lock_read() |
|
116 |
try: |
|
117 |
graph = self.repository.get_graph() |
|
118 |
got = [r for r, p in graph.iter_ancestry([self.revision_id])] |
|
5972.3.20
by Jelmer Vernooij
fix test. |
119 |
if _mod_revision.NULL_REVISION in got: |
120 |
got.remove(_mod_revision.NULL_REVISION) |
|
5972.3.13
by Jelmer Vernooij
Add matcher for ancestry. |
121 |
finally: |
122 |
self.repository.unlock() |
|
123 |
if sorted(got) != sorted(expected): |
|
6072.2.1
by Jelmer Vernooij
Add HasLayout matcher. |
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: |
|
6110.6.2
by Jelmer Vernooij
In HasLayout, take into consideration Tree.has_versioned_directories. |
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) |
|
6072.2.1
by Jelmer Vernooij
Add HasLayout matcher. |
147 |
finally: |
148 |
tree.unlock() |
|
149 |
||
6110.6.2
by Jelmer Vernooij
In HasLayout, take into consideration Tree.has_versioned_directories. |
150 |
@staticmethod
|
151 |
def _strip_unreferenced_directories(entries): |
|
6110.6.3
by Jelmer Vernooij
review feedback from mgz |
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 |
"""
|
|
6110.6.2
by Jelmer Vernooij
In HasLayout, take into consideration Tree.has_versioned_directories. |
156 |
directories = [] |
157 |
for entry in entries: |
|
158 |
if isinstance(entry, basestring): |
|
159 |
path = entry |
|
160 |
else: |
|
161 |
path = entry[0] |
|
6110.6.3
by Jelmer Vernooij
review feedback from mgz |
162 |
if not path or path[-1] == "/": |
6110.6.2
by Jelmer Vernooij
In HasLayout, take into consideration Tree.has_versioned_directories. |
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 |
||
6072.2.1
by Jelmer Vernooij
Add HasLayout matcher. |
173 |
def __str__(self): |
6110.6.2
by Jelmer Vernooij
In HasLayout, take into consideration Tree.has_versioned_directories. |
174 |
return 'HasLayout(%r)' % self.entries |
6072.2.1
by Jelmer Vernooij
Add HasLayout matcher. |
175 |
|
176 |
def match(self, tree): |
|
6110.6.2
by Jelmer Vernooij
In HasLayout, take into consideration Tree.has_versioned_directories. |
177 |
actual = list(self.get_tree_layout(tree)) |
6072.2.3
by Jelmer Vernooij
Use the matcher in test_merge_from_branch. |
178 |
if self.entries and isinstance(self.entries[0], basestring): |
179 |
actual = [path for (path, fileid) in actual] |
|
6110.6.2
by Jelmer Vernooij
In HasLayout, take into consideration Tree.has_versioned_directories. |
180 |
if not tree.has_versioned_directories(): |
181 |
entries = list(self._strip_unreferenced_directories(self.entries)) |
|
182 |
else: |
|
183 |
entries = self.entries |
|
6110.6.3
by Jelmer Vernooij
review feedback from mgz |
184 |
return Equals(entries).match(actual) |
6352.2.1
by Jelmer Vernooij
Add matcher for NoVfsCalls. |
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 |
||
6352.2.3
by Jelmer Vernooij
s/NoVfsCalls/ContainsNoVfsCalls/. |
199 |
class ContainsNoVfsCalls(Matcher): |
6352.2.1
by Jelmer Vernooij
Add matcher for NoVfsCalls. |
200 |
"""Ensure that none of the specified calls are HPSS calls."""
|
201 |
||
202 |
def __str__(self): |
|
6352.2.3
by Jelmer Vernooij
s/NoVfsCalls/ContainsNoVfsCalls/. |
203 |
return 'ContainsNoVfsCalls()' |
6352.2.1
by Jelmer Vernooij
Add matcher for NoVfsCalls. |
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) |