/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/test_matchers.py

  • Committer: Jelmer Vernooij
  • Date: 2020-07-18 15:20:23 UTC
  • mto: (7490.40.61 work)
  • mto: This revision was merged to the branch mainline in revision 7519.
  • Revision ID: jelmer@jelmer.uk-20200718152023-cabh92o24ke217te
Ignore missing revs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2010 Canonical Ltd
 
1
# Copyright (C) 2010, 2011, 2012, 2016 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
 
"""Tests of bzrlib test matchers."""
 
17
"""Tests of breezy test matchers."""
18
18
 
19
19
from testtools.matchers import *
20
20
 
21
 
from bzrlib.tests import TestCase
22
 
from bzrlib.tests.matchers import *
 
21
from ..sixish import PY3
 
22
 
 
23
from . import (
 
24
    CapturedCall,
 
25
    TestCase,
 
26
    TestCaseWithTransport,
 
27
    )
 
28
from .matchers import *
23
29
 
24
30
 
25
31
class StubTree(object):
53
59
    def test_match(self):
54
60
        stub_tree = StubTree(False)
55
61
        matcher = ReturnsUnlockable(stub_tree)
56
 
        self.assertThat(matcher.match(lambda:FakeUnlockable()), Equals(None))
 
62
        self.assertThat(matcher.match(lambda: FakeUnlockable()), Equals(None))
57
63
 
58
64
    def test_mismatch(self):
59
65
        stub_tree = StubTree(True)
60
66
        matcher = ReturnsUnlockable(stub_tree)
61
 
        mismatch = matcher.match(lambda:FakeUnlockable())
 
67
        mismatch = matcher.match(lambda: FakeUnlockable())
62
68
        self.assertNotEqual(None, mismatch)
63
69
        self.assertThat(mismatch.describe(), Equals("I am da tree is locked"))
64
70
 
 
71
 
 
72
class TestMatchesAncestry(TestCaseWithTransport):
 
73
 
 
74
    def test__str__(self):
 
75
        matcher = MatchesAncestry("A repository", b"arevid")
 
76
        self.assertEqual(
 
77
            "MatchesAncestry(repository='A repository', "
 
78
            "revision_id=%r)" % (b'arevid', ),
 
79
            str(matcher))
 
80
 
 
81
    def test_match(self):
 
82
        b = self.make_branch_builder('.')
 
83
        b.start_series()
 
84
        revid1 = b.build_commit()
 
85
        revid2 = b.build_commit()
 
86
        b.finish_series()
 
87
        branch = b.get_branch()
 
88
        m = MatchesAncestry(branch.repository, revid2)
 
89
        self.assertThat([revid2, revid1], m)
 
90
        self.assertThat([revid1, revid2], m)
 
91
        m = MatchesAncestry(branch.repository, revid1)
 
92
        self.assertThat([revid1], m)
 
93
        m = MatchesAncestry(branch.repository, b"unknown")
 
94
        self.assertThat([b"unknown"], m)
 
95
 
 
96
    def test_mismatch(self):
 
97
        b = self.make_branch_builder('.')
 
98
        b.start_series()
 
99
        revid1 = b.build_commit()
 
100
        revid2 = b.build_commit()
 
101
        b.finish_series()
 
102
        branch = b.get_branch()
 
103
        m = MatchesAncestry(branch.repository, revid1)
 
104
        mismatch = m.match([])
 
105
        self.assertIsNot(None, mismatch)
 
106
        self.assertEqual(
 
107
            "mismatched ancestry for revision %r was [%r], expected []" % (
 
108
                revid1, revid1),
 
109
            mismatch.describe())
 
110
 
 
111
 
 
112
class TestHasLayout(TestCaseWithTransport):
 
113
 
 
114
    def test__str__(self):
 
115
        matcher = HasLayout([(b"a", b"a-id")])
 
116
        self.assertEqual("HasLayout(%r)" % ([(b'a', b'a-id')], ), str(matcher))
 
117
 
 
118
    def test_match(self):
 
119
        t = self.make_branch_and_tree('.')
 
120
        self.build_tree(['a', 'b/', 'b/c'])
 
121
        t.add(['a', 'b', 'b/c'], [b'a-id', b'b-id', b'c-id'])
 
122
        self.assertThat(t, HasLayout(['', 'a', 'b/', 'b/c']))
 
123
        self.assertThat(t, HasLayout(
 
124
            [('', t.path2id('')),
 
125
             ('a', b'a-id'),
 
126
             ('b/', b'b-id'),
 
127
             ('b/c', b'c-id')]))
 
128
 
 
129
    def test_mismatch(self):
 
130
        t = self.make_branch_and_tree('.')
 
131
        self.build_tree(['a', 'b/', 'b/c'])
 
132
        t.add(['a', 'b', 'b/c'], [b'a-id', b'b-id', b'c-id'])
 
133
        mismatch = HasLayout(['a']).match(t)
 
134
        self.assertIsNot(None, mismatch)
 
135
        if PY3:
 
136
            self.assertEqual(
 
137
                set(("['', 'a', 'b/', 'b/c']", "['a']")),
 
138
                set(mismatch.describe().split(" != ")))
 
139
        else:
 
140
            self.assertEqual(
 
141
                set(("[u'', u'a', u'b/', u'b/c']", "['a']")),
 
142
                set(mismatch.describe().split(" != ")))
 
143
 
 
144
    def test_no_dirs(self):
 
145
        # Some tree/repository formats do not support versioned directories
 
146
        t = self.make_branch_and_tree('.')
 
147
        t.has_versioned_directories = lambda: False
 
148
        self.build_tree(['a', 'b/', 'b/c'])
 
149
        t.add(['a', 'b', 'b/c'], [b'a-id', b'b-id', b'c-id'])
 
150
        self.assertIs(None, HasLayout(['', 'a', 'b/', 'b/c']).match(t))
 
151
        self.assertIs(None, HasLayout(['', 'a', 'b/', 'b/c', 'd/']).match(t))
 
152
        mismatch = HasLayout([u'', u'a', u'd/']).match(t)
 
153
        self.assertIsNot(None, mismatch)
 
154
        if PY3:
 
155
            self.assertEqual(
 
156
                set(("['', 'a', 'b/', 'b/c']", "['', 'a']")),
 
157
                set(mismatch.describe().split(" != ")))
 
158
        else:
 
159
            self.assertEqual(
 
160
                set(("[u'', u'a', u'b/', u'b/c']", "[u'', u'a']")),
 
161
                set(mismatch.describe().split(" != ")))
 
162
 
 
163
 
 
164
class TestHasPathRelations(TestCaseWithTransport):
 
165
 
 
166
    def test__str__(self):
 
167
        t = self.make_branch_and_tree('.')
 
168
        matcher = HasPathRelations(t, [("a", "b")])
 
169
        self.assertEqual("HasPathRelations(%r, %r)" %
 
170
                         (t, [('a', 'b')]), str(matcher))
 
171
 
 
172
    def test_match(self):
 
173
        t = self.make_branch_and_tree('.')
 
174
        self.build_tree(['a', 'b/', 'b/c'])
 
175
        t.add(['a', 'b', 'b/c'])
 
176
        self.assertThat(t, HasPathRelations(t,
 
177
                                            [('', ''),
 
178
                                             ('a', 'a'),
 
179
                                                ('b/', 'b/'),
 
180
                                                ('b/c', 'b/c')]))
 
181
 
 
182
    def test_mismatch(self):
 
183
        t = self.make_branch_and_tree('.')
 
184
        self.build_tree(['a', 'b/', 'b/c'])
 
185
        t.add(['a', 'b', 'b/c'])
 
186
        mismatch = HasPathRelations(t, [('a', 'a')]).match(t)
 
187
        self.assertIsNot(None, mismatch)
 
188
 
 
189
 
 
190
class TestRevisionHistoryMatches(TestCaseWithTransport):
 
191
 
 
192
    def test_empty(self):
 
193
        tree = self.make_branch_and_tree('.')
 
194
        matcher = RevisionHistoryMatches([])
 
195
        self.assertIs(None, matcher.match(tree.branch))
 
196
 
 
197
    def test_matches(self):
 
198
        tree = self.make_branch_and_tree('.')
 
199
        tree.commit('msg1', rev_id=b'a')
 
200
        tree.commit('msg2', rev_id=b'b')
 
201
        matcher = RevisionHistoryMatches([b'a', b'b'])
 
202
        self.assertIs(None, matcher.match(tree.branch))
 
203
 
 
204
    def test_mismatch(self):
 
205
        tree = self.make_branch_and_tree('.')
 
206
        tree.commit('msg1', rev_id=b'a')
 
207
        tree.commit('msg2', rev_id=b'b')
 
208
        matcher = RevisionHistoryMatches([b'a', b'b', b'c'])
 
209
        if PY3:
 
210
            self.assertEqual(
 
211
                set(("[b'a', b'b']", "[b'a', b'b', b'c']")),
 
212
                set(matcher.match(tree.branch).describe().split(" != ")))
 
213
        else:
 
214
            self.assertEqual(
 
215
                set(("['a', 'b']", "['a', 'b', 'c']")),
 
216
                set(matcher.match(tree.branch).describe().split(" != ")))