/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1
# Copyright (C) 2010, 2011, 2012, 2016 Canonical Ltd
5200.3.1 by Robert Collins
Added ``bzrlib.tests.matchers`` as a place to put matchers, along with
2
#
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.
7
#
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.
12
#
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
16
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
17
"""Tests of breezy test matchers."""
5200.3.1 by Robert Collins
Added ``bzrlib.tests.matchers`` as a place to put matchers, along with
18
19
from testtools.matchers import *
20
6670.4.16 by Jelmer Vernooij
Move smart to breezy.bzr.
21
from ..bzr.smart.client import CallHookParams
7045.4.21 by Jelmer Vernooij
Fix some msgeditor tests.
22
from ..sixish import PY3
6352.2.1 by Jelmer Vernooij
Add matcher for NoVfsCalls.
23
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
24
from . import (
6352.2.1 by Jelmer Vernooij
Add matcher for NoVfsCalls.
25
    CapturedCall,
5972.3.13 by Jelmer Vernooij
Add matcher for ancestry.
26
    TestCase,
27
    TestCaseWithTransport,
28
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
29
from .matchers import *
5200.3.1 by Robert Collins
Added ``bzrlib.tests.matchers`` as a place to put matchers, along with
30
31
32
class StubTree(object):
33
    """Stubg for testing."""
34
35
    def __init__(self, lock_status):
36
        self._is_locked = lock_status
37
38
    def __str__(self):
39
        return u'I am da tree'
40
41
    def is_locked(self):
42
        return self._is_locked
43
44
5200.3.2 by Robert Collins
Cleaner matcher matching revised unlocking protocol.
45
class FakeUnlockable(object):
46
    """Something that can be unlocked."""
47
48
    def unlock(self):
49
        pass
50
51
52
class TestReturnsUnlockable(TestCase):
5200.3.1 by Robert Collins
Added ``bzrlib.tests.matchers`` as a place to put matchers, along with
53
54
    def test___str__(self):
5200.3.2 by Robert Collins
Cleaner matcher matching revised unlocking protocol.
55
        matcher = ReturnsUnlockable(StubTree(True))
5200.3.1 by Robert Collins
Added ``bzrlib.tests.matchers`` as a place to put matchers, along with
56
        self.assertEqual(
5200.3.2 by Robert Collins
Cleaner matcher matching revised unlocking protocol.
57
            'ReturnsUnlockable(lockable_thing=I am da tree)',
5200.3.1 by Robert Collins
Added ``bzrlib.tests.matchers`` as a place to put matchers, along with
58
            str(matcher))
59
60
    def test_match(self):
61
        stub_tree = StubTree(False)
5200.3.2 by Robert Collins
Cleaner matcher matching revised unlocking protocol.
62
        matcher = ReturnsUnlockable(stub_tree)
7143.15.2 by Jelmer Vernooij
Run autopep8.
63
        self.assertThat(matcher.match(lambda: FakeUnlockable()), Equals(None))
5200.3.1 by Robert Collins
Added ``bzrlib.tests.matchers`` as a place to put matchers, along with
64
65
    def test_mismatch(self):
66
        stub_tree = StubTree(True)
5200.3.2 by Robert Collins
Cleaner matcher matching revised unlocking protocol.
67
        matcher = ReturnsUnlockable(stub_tree)
7143.15.2 by Jelmer Vernooij
Run autopep8.
68
        mismatch = matcher.match(lambda: FakeUnlockable())
5200.3.1 by Robert Collins
Added ``bzrlib.tests.matchers`` as a place to put matchers, along with
69
        self.assertNotEqual(None, mismatch)
70
        self.assertThat(mismatch.describe(), Equals("I am da tree is locked"))
71
5972.3.13 by Jelmer Vernooij
Add matcher for ancestry.
72
73
class TestMatchesAncestry(TestCaseWithTransport):
74
75
    def test__str__(self):
6973.5.10 by Jelmer Vernooij
Random bunch of python3 bee-improvements.
76
        matcher = MatchesAncestry("A repository", b"arevid")
5972.3.13 by Jelmer Vernooij
Add matcher for ancestry.
77
        self.assertEqual(
78
            "MatchesAncestry(repository='A repository', "
6973.5.12 by Jelmer Vernooij
Merge trunk.
79
            "revision_id=%r)" % (b'arevid', ),
5972.3.13 by Jelmer Vernooij
Add matcher for ancestry.
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)
6973.5.10 by Jelmer Vernooij
Random bunch of python3 bee-improvements.
94
        m = MatchesAncestry(branch.repository, b"unknown")
95
        self.assertThat([b"unknown"], m)
5972.3.13 by Jelmer Vernooij
Add matcher for ancestry.
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)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
107
        self.assertEqual(
6973.5.12 by Jelmer Vernooij
Merge trunk.
108
            "mismatched ancestry for revision %r was [%r], expected []" % (
5972.3.13 by Jelmer Vernooij
Add matcher for ancestry.
109
                revid1, revid1),
110
            mismatch.describe())
6072.2.4 by Jelmer Vernooij
tests for matcher
111
112
113
class TestHasLayout(TestCaseWithTransport):
114
115
    def test__str__(self):
6973.5.10 by Jelmer Vernooij
Random bunch of python3 bee-improvements.
116
        matcher = HasLayout([(b"a", b"a-id")])
6973.5.12 by Jelmer Vernooij
Merge trunk.
117
        self.assertEqual("HasLayout(%r)" % ([(b'a', b'a-id')], ), str(matcher))
6072.2.4 by Jelmer Vernooij
tests for matcher
118
119
    def test_match(self):
120
        t = self.make_branch_and_tree('.')
121
        self.build_tree(['a', 'b/', 'b/c'])
6855.4.1 by Jelmer Vernooij
Yet more bees.
122
        t.add(['a', 'b', 'b/c'], [b'a-id', b'b-id', b'c-id'])
6110.6.2 by Jelmer Vernooij
In HasLayout, take into consideration Tree.has_versioned_directories.
123
        self.assertThat(t, HasLayout(['', 'a', 'b/', 'b/c']))
6072.2.4 by Jelmer Vernooij
tests for matcher
124
        self.assertThat(t, HasLayout(
125
            [('', t.get_root_id()),
6855.4.1 by Jelmer Vernooij
Yet more bees.
126
             ('a', b'a-id'),
127
             ('b/', b'b-id'),
128
             ('b/c', b'c-id')]))
6072.2.4 by Jelmer Vernooij
tests for matcher
129
130
    def test_mismatch(self):
131
        t = self.make_branch_and_tree('.')
132
        self.build_tree(['a', 'b/', 'b/c'])
6855.4.1 by Jelmer Vernooij
Yet more bees.
133
        t.add(['a', 'b', 'b/c'], [b'a-id', b'b-id', b'c-id'])
6072.2.4 by Jelmer Vernooij
tests for matcher
134
        mismatch = HasLayout(['a']).match(t)
135
        self.assertIsNot(None, mismatch)
7045.4.21 by Jelmer Vernooij
Fix some msgeditor tests.
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(" != ")))
6110.6.2 by Jelmer Vernooij
In HasLayout, take into consideration Tree.has_versioned_directories.
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'])
6855.4.1 by Jelmer Vernooij
Yet more bees.
150
        t.add(['a', 'b', 'b/c'], [b'a-id', b'b-id', b'c-id'])
6110.6.2 by Jelmer Vernooij
In HasLayout, take into consideration Tree.has_versioned_directories.
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)
7045.4.25 by Jelmer Vernooij
Fix test_matchers.
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(" != ")))
6352.2.1 by Jelmer Vernooij
Add matcher for NoVfsCalls.
163
164
6883.5.4 by Jelmer Vernooij
Add HasPathRelations.
165
class TestHasPathRelations(TestCaseWithTransport):
166
167
    def test__str__(self):
168
        t = self.make_branch_and_tree('.')
169
        matcher = HasPathRelations(t, [("a", "b")])
7143.15.2 by Jelmer Vernooij
Run autopep8.
170
        self.assertEqual("HasPathRelations(%r, %r)" %
171
                         (t, [('a', 'b')]), str(matcher))
6883.5.4 by Jelmer Vernooij
Add HasPathRelations.
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,
7143.15.2 by Jelmer Vernooij
Run autopep8.
178
                                            [('', ''),
179
                                             ('a', 'a'),
180
                                                ('b/', 'b/'),
181
                                                ('b/c', 'b/c')]))
6883.5.4 by Jelmer Vernooij
Add HasPathRelations.
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
6352.2.3 by Jelmer Vernooij
s/NoVfsCalls/ContainsNoVfsCalls/.
191
class TestContainsNoVfsCalls(TestCase):
6352.2.1 by Jelmer Vernooij
Add matcher for NoVfsCalls.
192
193
    def _make_call(self, method, args):
194
        return CapturedCall(CallHookParams(method, args, None, None, None), 0)
195
196
    def test__str__(self):
6352.2.3 by Jelmer Vernooij
s/NoVfsCalls/ContainsNoVfsCalls/.
197
        self.assertEqual("ContainsNoVfsCalls()", str(ContainsNoVfsCalls()))
6352.2.1 by Jelmer Vernooij
Add matcher for NoVfsCalls.
198
199
    def test_empty(self):
6352.2.3 by Jelmer Vernooij
s/NoVfsCalls/ContainsNoVfsCalls/.
200
        self.assertIs(None, ContainsNoVfsCalls().match([]))
6352.2.1 by Jelmer Vernooij
Add matcher for NoVfsCalls.
201
202
    def test_no_vfs_calls(self):
203
        calls = [self._make_call("Branch.get_config_file", [])]
6352.2.3 by Jelmer Vernooij
s/NoVfsCalls/ContainsNoVfsCalls/.
204
        self.assertIs(None, ContainsNoVfsCalls().match(calls))
6352.2.1 by Jelmer Vernooij
Add matcher for NoVfsCalls.
205
206
    def test_ignores_unknown(self):
207
        calls = [self._make_call("unknown", [])]
6352.2.3 by Jelmer Vernooij
s/NoVfsCalls/ContainsNoVfsCalls/.
208
        self.assertIs(None, ContainsNoVfsCalls().match(calls))
6352.2.1 by Jelmer Vernooij
Add matcher for NoVfsCalls.
209
210
    def test_match(self):
7045.4.25 by Jelmer Vernooij
Fix test_matchers.
211
        calls = [self._make_call(b"append", [b"file"]),
212
                 self._make_call(b"Branch.get_config_file", [])]
6352.2.3 by Jelmer Vernooij
s/NoVfsCalls/ContainsNoVfsCalls/.
213
        mismatch = ContainsNoVfsCalls().match(calls)
6352.2.1 by Jelmer Vernooij
Add matcher for NoVfsCalls.
214
        self.assertIsNot(None, mismatch)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
215
        self.assertEqual([calls[0].call], mismatch.vfs_calls)
7045.4.32 by Jelmer Vernooij
Fix tests.
216
        self.assertIn(mismatch.describe(), [
7143.15.2 by Jelmer Vernooij
Run autopep8.
217
            "no VFS calls expected, got: b'append'(b'file')",
218
            "no VFS calls expected, got: append('file')"])
6228.3.5 by Jelmer Vernooij
Add tests.
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('.')
6855.4.1 by Jelmer Vernooij
Yet more bees.
230
        tree.commit('msg1', rev_id=b'a')
231
        tree.commit('msg2', rev_id=b'b')
7045.4.25 by Jelmer Vernooij
Fix test_matchers.
232
        matcher = RevisionHistoryMatches([b'a', b'b'])
6228.3.5 by Jelmer Vernooij
Add tests.
233
        self.assertIs(None, matcher.match(tree.branch))
234
235
    def test_mismatch(self):
236
        tree = self.make_branch_and_tree('.')
6855.4.1 by Jelmer Vernooij
Yet more bees.
237
        tree.commit('msg1', rev_id=b'a')
238
        tree.commit('msg2', rev_id=b'b')
7045.4.25 by Jelmer Vernooij
Fix test_matchers.
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(" != ")))