~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/matchers.py

  • Committer: Patch Queue Manager
  • Date: 2011-10-09 13:52:06 UTC
  • mfrom: (6202.1.3 revno-revision)
  • Revision ID: pqm@pqm.ubuntu.com-20111009135206-t3utsln6mtzv9eut
(jelmer) Add a --revision argument to 'bzr revno'. (Jelmer Vernooij)

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
"""
28
28
 
29
29
__all__ = [
 
30
    'HasLayout',
30
31
    'MatchesAncestry',
31
32
    'ReturnsUnlockable',
32
33
    ]
33
34
 
34
35
from bzrlib import (
 
36
    osutils,
35
37
    revision as _mod_revision,
36
38
    )
37
39
 
38
 
from testtools.matchers import Mismatch, Matcher
 
40
from testtools.matchers import Equals, Mismatch, Matcher
39
41
 
40
42
 
41
43
class ReturnsUnlockable(Matcher):
53
55
        self.lockable_thing = lockable_thing
54
56
 
55
57
    def __str__(self):
56
 
        return ('ReturnsUnlockable(lockable_thing=%s)' % 
 
58
        return ('ReturnsUnlockable(lockable_thing=%s)' %
57
59
            self.lockable_thing)
58
60
 
59
61
    def match(self, lock_method):
112
114
        finally:
113
115
            self.repository.unlock()
114
116
        if sorted(got) != sorted(expected):
115
 
            return _AncestryMismatch(self.revision_id, sorted(got), sorted(expected))
 
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:
 
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)
 
140
        finally:
 
141
            tree.unlock()
 
142
 
 
143
    @staticmethod
 
144
    def _strip_unreferenced_directories(entries):
 
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
        """
 
149
        directories = []
 
150
        for entry in entries:
 
151
            if isinstance(entry, basestring):
 
152
                path = entry
 
153
            else:
 
154
                path = entry[0]
 
155
            if not path or path[-1] == "/":
 
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
 
 
166
    def __str__(self):
 
167
        return 'HasLayout(%r)' % self.entries
 
168
 
 
169
    def match(self, tree):
 
170
        actual = list(self.get_tree_layout(tree))
 
171
        if self.entries and isinstance(self.entries[0], basestring):
 
172
            actual = [path for (path, fileid) in actual]
 
173
        if not tree.has_versioned_directories():
 
174
            entries = list(self._strip_unreferenced_directories(self.entries))
 
175
        else:
 
176
            entries = self.entries
 
177
        return Equals(entries).match(actual)