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', |
5200.3.2
by Robert Collins
Cleaner matcher matching revised unlocking protocol. |
32 |
'ReturnsUnlockable', |
5200.3.1
by Robert Collins
Added ``bzrlib.tests.matchers`` as a place to put matchers, along with |
33 |
]
|
34 |
||
5972.3.13
by Jelmer Vernooij
Add matcher for ancestry. |
35 |
from bzrlib import ( |
6110.6.2
by Jelmer Vernooij
In HasLayout, take into consideration Tree.has_versioned_directories. |
36 |
osutils, |
5972.3.13
by Jelmer Vernooij
Add matcher for ancestry. |
37 |
revision as _mod_revision, |
38 |
)
|
|
39 |
||
6072.2.1
by Jelmer Vernooij
Add HasLayout matcher. |
40 |
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 |
41 |
|
42 |
||
5200.3.2
by Robert Collins
Cleaner matcher matching revised unlocking protocol. |
43 |
class ReturnsUnlockable(Matcher): |
5200.3.1
by Robert Collins
Added ``bzrlib.tests.matchers`` as a place to put matchers, along with |
44 |
"""A matcher that checks for the pattern we want lock* methods to have:
|
45 |
||
5200.3.2
by Robert Collins
Cleaner matcher matching revised unlocking protocol. |
46 |
They should return an object with an unlock() method.
|
47 |
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 |
48 |
|
49 |
:ivar lockable_thing: The object which can be locked that will be
|
|
50 |
inspected.
|
|
51 |
"""
|
|
52 |
||
53 |
def __init__(self, lockable_thing): |
|
54 |
Matcher.__init__(self) |
|
55 |
self.lockable_thing = lockable_thing |
|
56 |
||
57 |
def __str__(self): |
|
6110.6.2
by Jelmer Vernooij
In HasLayout, take into consideration Tree.has_versioned_directories. |
58 |
return ('ReturnsUnlockable(lockable_thing=%s)' % |
5200.3.1
by Robert Collins
Added ``bzrlib.tests.matchers`` as a place to put matchers, along with |
59 |
self.lockable_thing) |
60 |
||
61 |
def match(self, lock_method): |
|
5200.3.2
by Robert Collins
Cleaner matcher matching revised unlocking protocol. |
62 |
lock_method().unlock() |
5200.3.1
by Robert Collins
Added ``bzrlib.tests.matchers`` as a place to put matchers, along with |
63 |
if self.lockable_thing.is_locked(): |
64 |
return _IsLocked(self.lockable_thing) |
|
65 |
return None |
|
66 |
||
67 |
||
68 |
class _IsLocked(Mismatch): |
|
69 |
"""Something is locked."""
|
|
70 |
||
71 |
def __init__(self, lockable_thing): |
|
72 |
self.lockable_thing = lockable_thing |
|
73 |
||
74 |
def describe(self): |
|
75 |
return "%s is locked" % self.lockable_thing |
|
5972.3.13
by Jelmer Vernooij
Add matcher for ancestry. |
76 |
|
77 |
||
78 |
class _AncestryMismatch(Mismatch): |
|
79 |
"""Ancestry matching mismatch."""
|
|
80 |
||
81 |
def __init__(self, tip_revision, got, expected): |
|
82 |
self.tip_revision = tip_revision |
|
83 |
self.got = got |
|
84 |
self.expected = expected |
|
85 |
||
86 |
def describe(self): |
|
87 |
return "mismatched ancestry for revision %r was %r, expected %r" % ( |
|
88 |
self.tip_revision, self.got, self.expected) |
|
89 |
||
90 |
||
91 |
class MatchesAncestry(Matcher): |
|
92 |
"""A matcher that checks the ancestry of a particular revision.
|
|
93 |
||
94 |
:ivar graph: Graph in which to check the ancestry
|
|
95 |
:ivar revision_id: Revision id of the revision
|
|
96 |
"""
|
|
97 |
||
98 |
def __init__(self, repository, revision_id): |
|
99 |
Matcher.__init__(self) |
|
100 |
self.repository = repository |
|
101 |
self.revision_id = revision_id |
|
102 |
||
103 |
def __str__(self): |
|
104 |
return ('MatchesAncestry(repository=%r, revision_id=%r)' % ( |
|
105 |
self.repository, self.revision_id)) |
|
106 |
||
107 |
def match(self, expected): |
|
108 |
self.repository.lock_read() |
|
109 |
try: |
|
110 |
graph = self.repository.get_graph() |
|
111 |
got = [r for r, p in graph.iter_ancestry([self.revision_id])] |
|
5972.3.20
by Jelmer Vernooij
fix test. |
112 |
if _mod_revision.NULL_REVISION in got: |
113 |
got.remove(_mod_revision.NULL_REVISION) |
|
5972.3.13
by Jelmer Vernooij
Add matcher for ancestry. |
114 |
finally: |
115 |
self.repository.unlock() |
|
116 |
if sorted(got) != sorted(expected): |
|
6072.2.1
by Jelmer Vernooij
Add HasLayout matcher. |
117 |
return _AncestryMismatch(self.revision_id, sorted(got), |
118 |
sorted(expected)) |
|
119 |
||
120 |
||
121 |
class HasLayout(Matcher): |
|
122 |
"""A matcher that checks if a tree has a specific layout.
|
|
123 |
||
124 |
:ivar entries: List of expected entries, as (path, file_id) pairs.
|
|
125 |
"""
|
|
126 |
||
127 |
def __init__(self, entries): |
|
128 |
Matcher.__init__(self) |
|
129 |
self.entries = entries |
|
130 |
||
131 |
def get_tree_layout(self, tree): |
|
132 |
"""Get the (path, file_id) pairs for the current tree."""
|
|
133 |
tree.lock_read() |
|
134 |
try: |
|
6110.6.2
by Jelmer Vernooij
In HasLayout, take into consideration Tree.has_versioned_directories. |
135 |
for path, ie in tree.iter_entries_by_dir(): |
136 |
if ie.parent_id is None: |
|
137 |
yield (u"", ie.file_id) |
|
138 |
else: |
|
139 |
yield (path+ie.kind_character(), ie.file_id) |
|
6072.2.1
by Jelmer Vernooij
Add HasLayout matcher. |
140 |
finally: |
141 |
tree.unlock() |
|
142 |
||
6110.6.2
by Jelmer Vernooij
In HasLayout, take into consideration Tree.has_versioned_directories. |
143 |
@staticmethod
|
144 |
def _strip_unreferenced_directories(entries): |
|
6110.6.3
by Jelmer Vernooij
review feedback from mgz |
145 |
"""Strip all directories that don't (in)directly contain any files.
|
146 |
||
147 |
:param entries: List of path strings or (path, ie) tuples to process
|
|
148 |
"""
|
|
6110.6.2
by Jelmer Vernooij
In HasLayout, take into consideration Tree.has_versioned_directories. |
149 |
directories = [] |
150 |
for entry in entries: |
|
151 |
if isinstance(entry, basestring): |
|
152 |
path = entry |
|
153 |
else: |
|
154 |
path = entry[0] |
|
6110.6.3
by Jelmer Vernooij
review feedback from mgz |
155 |
if not path or path[-1] == "/": |
6110.6.2
by Jelmer Vernooij
In HasLayout, take into consideration Tree.has_versioned_directories. |
156 |
# directory
|
157 |
directories.append((path, entry)) |
|
158 |
else: |
|
159 |
# Yield the referenced parent directories
|
|
160 |
for dirpath, direntry in directories: |
|
161 |
if osutils.is_inside(dirpath, path): |
|
162 |
yield direntry |
|
163 |
directories = [] |
|
164 |
yield entry |
|
165 |
||
6072.2.1
by Jelmer Vernooij
Add HasLayout matcher. |
166 |
def __str__(self): |
6110.6.2
by Jelmer Vernooij
In HasLayout, take into consideration Tree.has_versioned_directories. |
167 |
return 'HasLayout(%r)' % self.entries |
6072.2.1
by Jelmer Vernooij
Add HasLayout matcher. |
168 |
|
169 |
def match(self, tree): |
|
6110.6.2
by Jelmer Vernooij
In HasLayout, take into consideration Tree.has_versioned_directories. |
170 |
actual = list(self.get_tree_layout(tree)) |
6072.2.3
by Jelmer Vernooij
Use the matcher in test_merge_from_branch. |
171 |
if self.entries and isinstance(self.entries[0], basestring): |
172 |
actual = [path for (path, fileid) in actual] |
|
6110.6.2
by Jelmer Vernooij
In HasLayout, take into consideration Tree.has_versioned_directories. |
173 |
if not tree.has_versioned_directories(): |
174 |
entries = list(self._strip_unreferenced_directories(self.entries)) |
|
175 |
else: |
|
176 |
entries = self.entries |
|
6110.6.3
by Jelmer Vernooij
review feedback from mgz |
177 |
return Equals(entries).match(actual) |