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 *
24
TestCaseWithTransport,
26
from .matchers import *
29
class StubTree(object):
30
"""Stubg for testing."""
32
def __init__(self, lock_status):
33
self._is_locked = lock_status
36
return u'I am da tree'
39
return self._is_locked
42
class FakeUnlockable(object):
43
"""Something that can be unlocked."""
49
class TestReturnsUnlockable(TestCase):
51
def test___str__(self):
52
matcher = ReturnsUnlockable(StubTree(True))
54
'ReturnsUnlockable(lockable_thing=I am da tree)',
58
stub_tree = StubTree(False)
59
matcher = ReturnsUnlockable(stub_tree)
60
self.assertThat(matcher.match(lambda: FakeUnlockable()), Equals(None))
62
def test_mismatch(self):
63
stub_tree = StubTree(True)
64
matcher = ReturnsUnlockable(stub_tree)
65
mismatch = matcher.match(lambda: FakeUnlockable())
66
self.assertNotEqual(None, mismatch)
67
self.assertThat(mismatch.describe(), Equals("I am da tree is locked"))
70
class TestMatchesAncestry(TestCaseWithTransport):
72
def test__str__(self):
73
matcher = MatchesAncestry("A repository", b"arevid")
75
"MatchesAncestry(repository='A repository', "
76
"revision_id=%r)" % (b'arevid', ),
80
b = self.make_branch_builder('.')
82
revid1 = b.build_commit()
83
revid2 = b.build_commit()
85
branch = b.get_branch()
86
m = MatchesAncestry(branch.repository, revid2)
87
self.assertThat([revid2, revid1], m)
88
self.assertThat([revid1, revid2], m)
89
m = MatchesAncestry(branch.repository, revid1)
90
self.assertThat([revid1], m)
91
m = MatchesAncestry(branch.repository, b"unknown")
92
self.assertThat([b"unknown"], m)
94
def test_mismatch(self):
95
b = self.make_branch_builder('.')
97
revid1 = b.build_commit()
98
revid2 = b.build_commit()
100
branch = b.get_branch()
101
m = MatchesAncestry(branch.repository, revid1)
102
mismatch = m.match([])
103
self.assertIsNot(None, mismatch)
105
"mismatched ancestry for revision %r was [%r], expected []" % (
110
class TestHasLayout(TestCaseWithTransport):
112
def test__str__(self):
113
matcher = HasLayout([(b"a", b"a-id")])
114
self.assertEqual("HasLayout(%r)" % ([(b'a', b'a-id')], ), str(matcher))
116
def test_match(self):
117
t = self.make_branch_and_tree('.')
118
self.build_tree(['a', 'b/', 'b/c'])
119
t.add(['a', 'b', 'b/c'], [b'a-id', b'b-id', b'c-id'])
120
self.assertThat(t, HasLayout(['', 'a', 'b/', 'b/c']))
121
self.assertThat(t, HasLayout(
122
[('', t.path2id('')),
127
def test_mismatch(self):
128
t = self.make_branch_and_tree('.')
129
self.build_tree(['a', 'b/', 'b/c'])
130
t.add(['a', 'b', 'b/c'], [b'a-id', b'b-id', b'c-id'])
131
mismatch = HasLayout(['a']).match(t)
132
self.assertIsNot(None, mismatch)
134
set(("['', 'a', 'b/', 'b/c']", "['a']")),
135
set(mismatch.describe().split(" != ")))
137
def test_no_dirs(self):
138
# Some tree/repository formats do not support versioned directories
139
t = self.make_branch_and_tree('.')
140
t.has_versioned_directories = lambda: False
141
self.build_tree(['a', 'b/', 'b/c'])
142
t.add(['a', 'b', 'b/c'], [b'a-id', b'b-id', b'c-id'])
143
self.assertIs(None, HasLayout(['', 'a', 'b/', 'b/c']).match(t))
144
self.assertIs(None, HasLayout(['', 'a', 'b/', 'b/c', 'd/']).match(t))
145
mismatch = HasLayout([u'', u'a', u'd/']).match(t)
146
self.assertIsNot(None, mismatch)
148
set(("['', 'a', 'b/', 'b/c']", "['', 'a']")),
149
set(mismatch.describe().split(" != ")))
152
class TestHasPathRelations(TestCaseWithTransport):
154
def test__str__(self):
155
t = self.make_branch_and_tree('.')
156
matcher = HasPathRelations(t, [("a", "b")])
157
self.assertEqual("HasPathRelations(%r, %r)" %
158
(t, [('a', 'b')]), str(matcher))
160
def test_match(self):
161
t = self.make_branch_and_tree('.')
162
self.build_tree(['a', 'b/', 'b/c'])
163
t.add(['a', 'b', 'b/c'])
164
self.assertThat(t, HasPathRelations(t,
170
def test_mismatch(self):
171
t = self.make_branch_and_tree('.')
172
self.build_tree(['a', 'b/', 'b/c'])
173
t.add(['a', 'b', 'b/c'])
174
mismatch = HasPathRelations(t, [('a', 'a')]).match(t)
175
self.assertIsNot(None, mismatch)
178
class TestRevisionHistoryMatches(TestCaseWithTransport):
180
def test_empty(self):
181
tree = self.make_branch_and_tree('.')
182
matcher = RevisionHistoryMatches([])
183
self.assertIs(None, matcher.match(tree.branch))
185
def test_matches(self):
186
tree = self.make_branch_and_tree('.')
187
tree.commit('msg1', rev_id=b'a')
188
tree.commit('msg2', rev_id=b'b')
189
matcher = RevisionHistoryMatches([b'a', b'b'])
190
self.assertIs(None, matcher.match(tree.branch))
192
def test_mismatch(self):
193
tree = self.make_branch_and_tree('.')
194
tree.commit('msg1', rev_id=b'a')
195
tree.commit('msg2', rev_id=b'b')
196
matcher = RevisionHistoryMatches([b'a', b'b', b'c'])
198
set(("[b'a', b'b']", "[b'a', b'b', b'c']")),
199
set(matcher.match(tree.branch).describe().split(" != ")))