/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-04-05 19:11:34 UTC
  • mto: (7490.7.16 work)
  • mto: This revision was merged to the branch mainline in revision 7501.
  • Revision ID: jelmer@jelmer.uk-20200405191134-0aebh8ikiwygxma5
Populate the .gitignore file.

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 ..bzr.smart.client import CallHookParams
 
22
from ..sixish import PY3
 
23
 
 
24
from . import (
 
25
    CapturedCall,
 
26
    TestCase,
 
27
    TestCaseWithTransport,
 
28
    )
 
29
from .matchers import *
23
30
 
24
31
 
25
32
class StubTree(object):
53
60
    def test_match(self):
54
61
        stub_tree = StubTree(False)
55
62
        matcher = ReturnsUnlockable(stub_tree)
56
 
        self.assertThat(matcher.match(lambda:FakeUnlockable()), Equals(None))
 
63
        self.assertThat(matcher.match(lambda: FakeUnlockable()), Equals(None))
57
64
 
58
65
    def test_mismatch(self):
59
66
        stub_tree = StubTree(True)
60
67
        matcher = ReturnsUnlockable(stub_tree)
61
 
        mismatch = matcher.match(lambda:FakeUnlockable())
 
68
        mismatch = matcher.match(lambda: FakeUnlockable())
62
69
        self.assertNotEqual(None, mismatch)
63
70
        self.assertThat(mismatch.describe(), Equals("I am da tree is locked"))
64
71
 
 
72
 
 
73
class TestMatchesAncestry(TestCaseWithTransport):
 
74
 
 
75
    def test__str__(self):
 
76
        matcher = MatchesAncestry("A repository", b"arevid")
 
77
        self.assertEqual(
 
78
            "MatchesAncestry(repository='A repository', "
 
79
            "revision_id=%r)" % (b'arevid', ),
 
80
            str(matcher))
 
81
 
 
82
    def test_match(self):
 
83
        b = self.make_branch_builder('.')
 
84
        b.start_series()
 
85
        revid1 = b.build_commit()
 
86
        revid2 = b.build_commit()
 
87
        b.finish_series()
 
88
        branch = b.get_branch()
 
89
        m = MatchesAncestry(branch.repository, revid2)
 
90
        self.assertThat([revid2, revid1], m)
 
91
        self.assertThat([revid1, revid2], m)
 
92
        m = MatchesAncestry(branch.repository, revid1)
 
93
        self.assertThat([revid1], m)
 
94
        m = MatchesAncestry(branch.repository, b"unknown")
 
95
        self.assertThat([b"unknown"], m)
 
96
 
 
97
    def test_mismatch(self):
 
98
        b = self.make_branch_builder('.')
 
99
        b.start_series()
 
100
        revid1 = b.build_commit()
 
101
        revid2 = b.build_commit()
 
102
        b.finish_series()
 
103
        branch = b.get_branch()
 
104
        m = MatchesAncestry(branch.repository, revid1)
 
105
        mismatch = m.match([])
 
106
        self.assertIsNot(None, mismatch)
 
107
        self.assertEqual(
 
108
            "mismatched ancestry for revision %r was [%r], expected []" % (
 
109
                revid1, revid1),
 
110
            mismatch.describe())
 
111
 
 
112
 
 
113
class TestHasLayout(TestCaseWithTransport):
 
114
 
 
115
    def test__str__(self):
 
116
        matcher = HasLayout([(b"a", b"a-id")])
 
117
        self.assertEqual("HasLayout(%r)" % ([(b'a', b'a-id')], ), str(matcher))
 
118
 
 
119
    def test_match(self):
 
120
        t = self.make_branch_and_tree('.')
 
121
        self.build_tree(['a', 'b/', 'b/c'])
 
122
        t.add(['a', 'b', 'b/c'], [b'a-id', b'b-id', b'c-id'])
 
123
        self.assertThat(t, HasLayout(['', 'a', 'b/', 'b/c']))
 
124
        self.assertThat(t, HasLayout(
 
125
            [('', t.path2id('')),
 
126
             ('a', b'a-id'),
 
127
             ('b/', b'b-id'),
 
128
             ('b/c', b'c-id')]))
 
129
 
 
130
    def test_mismatch(self):
 
131
        t = self.make_branch_and_tree('.')
 
132
        self.build_tree(['a', 'b/', 'b/c'])
 
133
        t.add(['a', 'b', 'b/c'], [b'a-id', b'b-id', b'c-id'])
 
134
        mismatch = HasLayout(['a']).match(t)
 
135
        self.assertIsNot(None, mismatch)
 
136
        if PY3:
 
137
            self.assertEqual(
 
138
                set(("['', 'a', 'b/', 'b/c']", "['a']")),
 
139
                set(mismatch.describe().split(" != ")))
 
140
        else:
 
141
            self.assertEqual(
 
142
                set(("[u'', u'a', u'b/', u'b/c']", "['a']")),
 
143
                set(mismatch.describe().split(" != ")))
 
144
 
 
145
    def test_no_dirs(self):
 
146
        # Some tree/repository formats do not support versioned directories
 
147
        t = self.make_branch_and_tree('.')
 
148
        t.has_versioned_directories = lambda: False
 
149
        self.build_tree(['a', 'b/', 'b/c'])
 
150
        t.add(['a', 'b', 'b/c'], [b'a-id', b'b-id', b'c-id'])
 
151
        self.assertIs(None, HasLayout(['', 'a', 'b/', 'b/c']).match(t))
 
152
        self.assertIs(None, HasLayout(['', 'a', 'b/', 'b/c', 'd/']).match(t))
 
153
        mismatch = HasLayout([u'', u'a', u'd/']).match(t)
 
154
        self.assertIsNot(None, mismatch)
 
155
        if PY3:
 
156
            self.assertEqual(
 
157
                set(("['', 'a', 'b/', 'b/c']", "['', 'a']")),
 
158
                set(mismatch.describe().split(" != ")))
 
159
        else:
 
160
            self.assertEqual(
 
161
                set(("[u'', u'a', u'b/', u'b/c']", "[u'', u'a']")),
 
162
                set(mismatch.describe().split(" != ")))
 
163
 
 
164
 
 
165
class TestHasPathRelations(TestCaseWithTransport):
 
166
 
 
167
    def test__str__(self):
 
168
        t = self.make_branch_and_tree('.')
 
169
        matcher = HasPathRelations(t, [("a", "b")])
 
170
        self.assertEqual("HasPathRelations(%r, %r)" %
 
171
                         (t, [('a', 'b')]), str(matcher))
 
172
 
 
173
    def test_match(self):
 
174
        t = self.make_branch_and_tree('.')
 
175
        self.build_tree(['a', 'b/', 'b/c'])
 
176
        t.add(['a', 'b', 'b/c'])
 
177
        self.assertThat(t, HasPathRelations(t,
 
178
                                            [('', ''),
 
179
                                             ('a', 'a'),
 
180
                                                ('b/', 'b/'),
 
181
                                                ('b/c', 'b/c')]))
 
182
 
 
183
    def test_mismatch(self):
 
184
        t = self.make_branch_and_tree('.')
 
185
        self.build_tree(['a', 'b/', 'b/c'])
 
186
        t.add(['a', 'b', 'b/c'])
 
187
        mismatch = HasPathRelations(t, [('a', 'a')]).match(t)
 
188
        self.assertIsNot(None, mismatch)
 
189
 
 
190
 
 
191
class TestContainsNoVfsCalls(TestCase):
 
192
 
 
193
    def _make_call(self, method, args):
 
194
        return CapturedCall(CallHookParams(method, args, None, None, None), 0)
 
195
 
 
196
    def test__str__(self):
 
197
        self.assertEqual("ContainsNoVfsCalls()", str(ContainsNoVfsCalls()))
 
198
 
 
199
    def test_empty(self):
 
200
        self.assertIs(None, ContainsNoVfsCalls().match([]))
 
201
 
 
202
    def test_no_vfs_calls(self):
 
203
        calls = [self._make_call("Branch.get_config_file", [])]
 
204
        self.assertIs(None, ContainsNoVfsCalls().match(calls))
 
205
 
 
206
    def test_ignores_unknown(self):
 
207
        calls = [self._make_call("unknown", [])]
 
208
        self.assertIs(None, ContainsNoVfsCalls().match(calls))
 
209
 
 
210
    def test_match(self):
 
211
        calls = [self._make_call(b"append", [b"file"]),
 
212
                 self._make_call(b"Branch.get_config_file", [])]
 
213
        mismatch = ContainsNoVfsCalls().match(calls)
 
214
        self.assertIsNot(None, mismatch)
 
215
        self.assertEqual([calls[0].call], mismatch.vfs_calls)
 
216
        self.assertIn(mismatch.describe(), [
 
217
            "no VFS calls expected, got: b'append'(b'file')",
 
218
            "no VFS calls expected, got: append('file')"])
 
219
 
 
220
 
 
221
class TestRevisionHistoryMatches(TestCaseWithTransport):
 
222
 
 
223
    def test_empty(self):
 
224
        tree = self.make_branch_and_tree('.')
 
225
        matcher = RevisionHistoryMatches([])
 
226
        self.assertIs(None, matcher.match(tree.branch))
 
227
 
 
228
    def test_matches(self):
 
229
        tree = self.make_branch_and_tree('.')
 
230
        tree.commit('msg1', rev_id=b'a')
 
231
        tree.commit('msg2', rev_id=b'b')
 
232
        matcher = RevisionHistoryMatches([b'a', b'b'])
 
233
        self.assertIs(None, matcher.match(tree.branch))
 
234
 
 
235
    def test_mismatch(self):
 
236
        tree = self.make_branch_and_tree('.')
 
237
        tree.commit('msg1', rev_id=b'a')
 
238
        tree.commit('msg2', rev_id=b'b')
 
239
        matcher = RevisionHistoryMatches([b'a', b'b', b'c'])
 
240
        if PY3:
 
241
            self.assertEqual(
 
242
                set(("[b'a', b'b']", "[b'a', b'b', b'c']")),
 
243
                set(matcher.match(tree.branch).describe().split(" != ")))
 
244
        else:
 
245
            self.assertEqual(
 
246
                set(("['a', 'b']", "['a', 'b', 'c']")),
 
247
                set(matcher.match(tree.branch).describe().split(" != ")))