1
# Copyright (C) 2010, 2011, 2012, 2016 Canonical Ltd
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.
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.
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
17
"""Tests of breezy test matchers."""
19
from testtools.matchers import *
21
from ..bzr.smart.client import CallHookParams
22
from ..sixish import PY3
27
TestCaseWithTransport,
29
from .matchers import *
32
class StubTree(object):
33
"""Stubg for testing."""
35
def __init__(self, lock_status):
36
self._is_locked = lock_status
39
return u'I am da tree'
42
return self._is_locked
45
class FakeUnlockable(object):
46
"""Something that can be unlocked."""
52
class TestReturnsUnlockable(TestCase):
54
def test___str__(self):
55
matcher = ReturnsUnlockable(StubTree(True))
57
'ReturnsUnlockable(lockable_thing=I am da tree)',
61
stub_tree = StubTree(False)
62
matcher = ReturnsUnlockable(stub_tree)
63
self.assertThat(matcher.match(lambda: FakeUnlockable()), Equals(None))
65
def test_mismatch(self):
66
stub_tree = StubTree(True)
67
matcher = ReturnsUnlockable(stub_tree)
68
mismatch = matcher.match(lambda: FakeUnlockable())
69
self.assertNotEqual(None, mismatch)
70
self.assertThat(mismatch.describe(), Equals("I am da tree is locked"))
73
class TestMatchesAncestry(TestCaseWithTransport):
75
def test__str__(self):
76
matcher = MatchesAncestry("A repository", b"arevid")
78
"MatchesAncestry(repository='A repository', "
79
"revision_id=%r)" % (b'arevid', ),
83
b = self.make_branch_builder('.')
85
revid1 = b.build_commit()
86
revid2 = b.build_commit()
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)
97
def test_mismatch(self):
98
b = self.make_branch_builder('.')
100
revid1 = b.build_commit()
101
revid2 = b.build_commit()
103
branch = b.get_branch()
104
m = MatchesAncestry(branch.repository, revid1)
105
mismatch = m.match([])
106
self.assertIsNot(None, mismatch)
108
"mismatched ancestry for revision %r was [%r], expected []" % (
113
class TestHasLayout(TestCaseWithTransport):
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))
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('')),
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)
138
set(("['', 'a', 'b/', 'b/c']", "['a']")),
139
set(mismatch.describe().split(" != ")))
142
set(("[u'', u'a', u'b/', u'b/c']", "['a']")),
143
set(mismatch.describe().split(" != ")))
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)
157
set(("['', 'a', 'b/', 'b/c']", "['', 'a']")),
158
set(mismatch.describe().split(" != ")))
161
set(("[u'', u'a', u'b/', u'b/c']", "[u'', u'a']")),
162
set(mismatch.describe().split(" != ")))
165
class TestHasPathRelations(TestCaseWithTransport):
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))
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,
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)
191
class TestContainsNoVfsCalls(TestCase):
193
def _make_call(self, method, args):
194
return CapturedCall(CallHookParams(method, args, None, None, None), 0)
196
def test__str__(self):
197
self.assertEqual("ContainsNoVfsCalls()", str(ContainsNoVfsCalls()))
199
def test_empty(self):
200
self.assertIs(None, ContainsNoVfsCalls().match([]))
202
def test_no_vfs_calls(self):
203
calls = [self._make_call("Branch.get_config_file", [])]
204
self.assertIs(None, ContainsNoVfsCalls().match(calls))
206
def test_ignores_unknown(self):
207
calls = [self._make_call("unknown", [])]
208
self.assertIs(None, ContainsNoVfsCalls().match(calls))
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')"])
221
class TestRevisionHistoryMatches(TestCaseWithTransport):
223
def test_empty(self):
224
tree = self.make_branch_and_tree('.')
225
matcher = RevisionHistoryMatches([])
226
self.assertIs(None, matcher.match(tree.branch))
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))
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'])
242
set(("[b'a', b'b']", "[b'a', b'b', b'c']")),
243
set(matcher.match(tree.branch).describe().split(" != ")))
246
set(("['a', 'b']", "['a', 'b', 'c']")),
247
set(matcher.match(tree.branch).describe().split(" != ")))