/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/matchers.py

  • Committer: Jelmer Vernooij
  • Date: 2018-04-02 00:52:27 UTC
  • mfrom: (6939 work)
  • mto: This revision was merged to the branch mainline in revision 7274.
  • Revision ID: jelmer@jelmer.uk-20180402005227-pecflp1mvdjrjqd6
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
 
29
29
__all__ = [
30
30
    'HasLayout',
 
31
    'HasPathRelations',
31
32
    'MatchesAncestry',
32
33
    'ContainsNoVfsCalls',
33
34
    'ReturnsUnlockable',
47
48
from ..sixish import (
48
49
    text_type,
49
50
    )
 
51
from ..tree import find_previous_path
50
52
 
51
53
from testtools.matchers import Equals, Mismatch, Matcher
52
54
 
116
118
            self.repository, self.revision_id))
117
119
 
118
120
    def match(self, expected):
119
 
        self.repository.lock_read()
120
 
        try:
 
121
        with self.repository.lock_read():
121
122
            graph = self.repository.get_graph()
122
123
            got = [r for r, p in graph.iter_ancestry([self.revision_id])]
123
124
            if _mod_revision.NULL_REVISION in got:
124
125
                got.remove(_mod_revision.NULL_REVISION)
125
 
        finally:
126
 
            self.repository.unlock()
127
126
        if sorted(got) != sorted(expected):
128
127
            return _AncestryMismatch(self.revision_id, sorted(got),
129
128
                sorted(expected))
141
140
 
142
141
    def get_tree_layout(self, tree):
143
142
        """Get the (path, file_id) pairs for the current tree."""
144
 
        tree.lock_read()
145
 
        try:
 
143
        with tree.lock_read():
146
144
            for path, ie in tree.iter_entries_by_dir():
147
145
                if ie.parent_id is None:
148
146
                    yield (u"", ie.file_id)
149
147
                else:
150
148
                    yield (path+ie.kind_character(), ie.file_id)
151
 
        finally:
152
 
            tree.unlock()
153
149
 
154
150
    @staticmethod
155
151
    def _strip_unreferenced_directories(entries):
188
184
        return Equals(entries).match(actual)
189
185
 
190
186
 
 
187
class HasPathRelations(Matcher):
 
188
    """Matcher verifies that paths have a relation to those in another tree.
 
189
 
 
190
    :ivar previous_tree: tree to compare to
 
191
    :ivar previous_entries: List of expected entries, as (path, previous_path) pairs.
 
192
    """
 
193
 
 
194
    def __init__(self, previous_tree, previous_entries):
 
195
        Matcher.__init__(self)
 
196
        self.previous_tree = previous_tree
 
197
        self.previous_entries = previous_entries
 
198
 
 
199
    def get_path_map(self, tree):
 
200
        """Get the (path, previous_path) pairs for the current tree."""
 
201
        with tree.lock_read(), self.previous_tree.lock_read():
 
202
            for path, ie in tree.iter_entries_by_dir():
 
203
                if tree.supports_rename_tracking():
 
204
                    previous_path = find_previous_path(tree, self.previous_tree, path)
 
205
                else:
 
206
                    if self.previous_tree.is_versioned(path):
 
207
                        previous_path = path
 
208
                    else:
 
209
                        previous_path = None
 
210
                if previous_path:
 
211
                    kind = self.previous_tree.kind(previous_path)
 
212
                    if kind == 'directory':
 
213
                        previous_path += '/'
 
214
                if ie.parent_id is None:
 
215
                    yield (u"", previous_path)
 
216
                else:
 
217
                    yield (path+ie.kind_character(), previous_path)
 
218
 
 
219
    @staticmethod
 
220
    def _strip_unreferenced_directories(entries):
 
221
        """Strip all directories that don't (in)directly contain any files.
 
222
 
 
223
        :param entries: List of path strings or (path, previous_path) tuples to process
 
224
        """
 
225
        directory_used = set()
 
226
        directories = []
 
227
        for (path, previous_path) in entries:
 
228
            if not path or path[-1] == "/":
 
229
                # directory
 
230
                directories.append((path, previous_path))
 
231
            else:
 
232
                # Yield the referenced parent directories
 
233
                for direntry in directories:
 
234
                    if osutils.is_inside(direntry[0], path):
 
235
                        directory_used.add(direntry[0])
 
236
        for (path, previous_path) in entries:
 
237
            if (not path.endswith("/")) or path in directory_used:
 
238
                yield (path, previous_path)
 
239
 
 
240
    def __str__(self):
 
241
        return 'HasPathRelations(%r, %r)' % (self.previous_tree, self.previous_entries)
 
242
 
 
243
    def match(self, tree):
 
244
        actual = list(self.get_path_map(tree))
 
245
        if not tree.has_versioned_directories():
 
246
            entries = list(self._strip_unreferenced_directories(self.previous_entries))
 
247
        else:
 
248
            entries = self.previous_entries
 
249
        if not tree.supports_rename_tracking():
 
250
            entries = [
 
251
                (path, path if self.previous_tree.is_versioned(path) else None)
 
252
                for (path, previous_path) in entries]
 
253
        return Equals(entries).match(actual)
 
254
 
 
255
 
191
256
class RevisionHistoryMatches(Matcher):
192
257
    """A matcher that checks if a branch has a specific revision history.
193
258
 
202
267
        return 'RevisionHistoryMatches(%r)' % self.expected
203
268
 
204
269
    def match(self, branch):
205
 
        branch.lock_read()
206
 
        try:
 
270
        with branch.lock_read():
207
271
            graph = branch.repository.get_graph()
208
272
            history = list(graph.iter_lefthand_ancestry(
209
273
                branch.last_revision(), [_mod_revision.NULL_REVISION]))
210
274
            history.reverse()
211
 
        finally:
212
 
            branch.unlock()
213
275
        return Equals(self.expected).match(history)
214
276
 
215
277