/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
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
21
from . import (
6352.2.1 by Jelmer Vernooij
Add matcher for NoVfsCalls.
22
    CapturedCall,
5972.3.13 by Jelmer Vernooij
Add matcher for ancestry.
23
    TestCase,
24
    TestCaseWithTransport,
25
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
26
from .matchers import *
5200.3.1 by Robert Collins
Added ``bzrlib.tests.matchers`` as a place to put matchers, along with
27
28
29
class StubTree(object):
30
    """Stubg for testing."""
31
32
    def __init__(self, lock_status):
33
        self._is_locked = lock_status
34
35
    def __str__(self):
36
        return u'I am da tree'
37
38
    def is_locked(self):
39
        return self._is_locked
40
41
5200.3.2 by Robert Collins
Cleaner matcher matching revised unlocking protocol.
42
class FakeUnlockable(object):
43
    """Something that can be unlocked."""
44
45
    def unlock(self):
46
        pass
47
48
49
class TestReturnsUnlockable(TestCase):
5200.3.1 by Robert Collins
Added ``bzrlib.tests.matchers`` as a place to put matchers, along with
50
51
    def test___str__(self):
5200.3.2 by Robert Collins
Cleaner matcher matching revised unlocking protocol.
52
        matcher = ReturnsUnlockable(StubTree(True))
5200.3.1 by Robert Collins
Added ``bzrlib.tests.matchers`` as a place to put matchers, along with
53
        self.assertEqual(
5200.3.2 by Robert Collins
Cleaner matcher matching revised unlocking protocol.
54
            '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
55
            str(matcher))
56
57
    def test_match(self):
58
        stub_tree = StubTree(False)
5200.3.2 by Robert Collins
Cleaner matcher matching revised unlocking protocol.
59
        matcher = ReturnsUnlockable(stub_tree)
7143.15.2 by Jelmer Vernooij
Run autopep8.
60
        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
61
62
    def test_mismatch(self):
63
        stub_tree = StubTree(True)
5200.3.2 by Robert Collins
Cleaner matcher matching revised unlocking protocol.
64
        matcher = ReturnsUnlockable(stub_tree)
7143.15.2 by Jelmer Vernooij
Run autopep8.
65
        mismatch = matcher.match(lambda: FakeUnlockable())
5200.3.1 by Robert Collins
Added ``bzrlib.tests.matchers`` as a place to put matchers, along with
66
        self.assertNotEqual(None, mismatch)
67
        self.assertThat(mismatch.describe(), Equals("I am da tree is locked"))
68
5972.3.13 by Jelmer Vernooij
Add matcher for ancestry.
69
70
class TestMatchesAncestry(TestCaseWithTransport):
71
72
    def test__str__(self):
6973.5.10 by Jelmer Vernooij
Random bunch of python3 bee-improvements.
73
        matcher = MatchesAncestry("A repository", b"arevid")
5972.3.13 by Jelmer Vernooij
Add matcher for ancestry.
74
        self.assertEqual(
75
            "MatchesAncestry(repository='A repository', "
6973.5.12 by Jelmer Vernooij
Merge trunk.
76
            "revision_id=%r)" % (b'arevid', ),
5972.3.13 by Jelmer Vernooij
Add matcher for ancestry.
77
            str(matcher))
78
79
    def test_match(self):
80
        b = self.make_branch_builder('.')
81
        b.start_series()
82
        revid1 = b.build_commit()
83
        revid2 = b.build_commit()
84
        b.finish_series()
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)
6973.5.10 by Jelmer Vernooij
Random bunch of python3 bee-improvements.
91
        m = MatchesAncestry(branch.repository, b"unknown")
92
        self.assertThat([b"unknown"], m)
5972.3.13 by Jelmer Vernooij
Add matcher for ancestry.
93
94
    def test_mismatch(self):
95
        b = self.make_branch_builder('.')
96
        b.start_series()
97
        revid1 = b.build_commit()
98
        revid2 = b.build_commit()
99
        b.finish_series()
100
        branch = b.get_branch()
101
        m = MatchesAncestry(branch.repository, revid1)
102
        mismatch = m.match([])
103
        self.assertIsNot(None, mismatch)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
104
        self.assertEqual(
6973.5.12 by Jelmer Vernooij
Merge trunk.
105
            "mismatched ancestry for revision %r was [%r], expected []" % (
5972.3.13 by Jelmer Vernooij
Add matcher for ancestry.
106
                revid1, revid1),
107
            mismatch.describe())
6072.2.4 by Jelmer Vernooij
tests for matcher
108
109
110
class TestHasLayout(TestCaseWithTransport):
111
112
    def test__str__(self):
6973.5.10 by Jelmer Vernooij
Random bunch of python3 bee-improvements.
113
        matcher = HasLayout([(b"a", b"a-id")])
6973.5.12 by Jelmer Vernooij
Merge trunk.
114
        self.assertEqual("HasLayout(%r)" % ([(b'a', b'a-id')], ), str(matcher))
6072.2.4 by Jelmer Vernooij
tests for matcher
115
116
    def test_match(self):
117
        t = self.make_branch_and_tree('.')
118
        self.build_tree(['a', 'b/', 'b/c'])
6855.4.1 by Jelmer Vernooij
Yet more bees.
119
        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.
120
        self.assertThat(t, HasLayout(['', 'a', 'b/', 'b/c']))
6072.2.4 by Jelmer Vernooij
tests for matcher
121
        self.assertThat(t, HasLayout(
7358.14.1 by Jelmer Vernooij
Remove Tree.get_root_id() in favour of Tree.path2id('').
122
            [('', t.path2id('')),
6855.4.1 by Jelmer Vernooij
Yet more bees.
123
             ('a', b'a-id'),
124
             ('b/', b'b-id'),
125
             ('b/c', b'c-id')]))
6072.2.4 by Jelmer Vernooij
tests for matcher
126
127
    def test_mismatch(self):
128
        t = self.make_branch_and_tree('.')
129
        self.build_tree(['a', 'b/', 'b/c'])
6855.4.1 by Jelmer Vernooij
Yet more bees.
130
        t.add(['a', 'b', 'b/c'], [b'a-id', b'b-id', b'c-id'])
6072.2.4 by Jelmer Vernooij
tests for matcher
131
        mismatch = HasLayout(['a']).match(t)
132
        self.assertIsNot(None, mismatch)
7479.2.1 by Jelmer Vernooij
Drop python2 support.
133
        self.assertEqual(
134
            set(("['', 'a', 'b/', 'b/c']", "['a']")),
135
            set(mismatch.describe().split(" != ")))
6110.6.2 by Jelmer Vernooij
In HasLayout, take into consideration Tree.has_versioned_directories.
136
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'])
6855.4.1 by Jelmer Vernooij
Yet more bees.
142
        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.
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)
7479.2.1 by Jelmer Vernooij
Drop python2 support.
147
        self.assertEqual(
148
            set(("['', 'a', 'b/', 'b/c']", "['', 'a']")),
149
            set(mismatch.describe().split(" != ")))
6352.2.1 by Jelmer Vernooij
Add matcher for NoVfsCalls.
150
151
6883.5.4 by Jelmer Vernooij
Add HasPathRelations.
152
class TestHasPathRelations(TestCaseWithTransport):
153
154
    def test__str__(self):
155
        t = self.make_branch_and_tree('.')
156
        matcher = HasPathRelations(t, [("a", "b")])
7143.15.2 by Jelmer Vernooij
Run autopep8.
157
        self.assertEqual("HasPathRelations(%r, %r)" %
158
                         (t, [('a', 'b')]), str(matcher))
6883.5.4 by Jelmer Vernooij
Add HasPathRelations.
159
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,
7143.15.2 by Jelmer Vernooij
Run autopep8.
165
                                            [('', ''),
166
                                             ('a', 'a'),
167
                                                ('b/', 'b/'),
168
                                                ('b/c', 'b/c')]))
6883.5.4 by Jelmer Vernooij
Add HasPathRelations.
169
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)
176
177
6228.3.5 by Jelmer Vernooij
Add tests.
178
class TestRevisionHistoryMatches(TestCaseWithTransport):
179
180
    def test_empty(self):
181
        tree = self.make_branch_and_tree('.')
182
        matcher = RevisionHistoryMatches([])
183
        self.assertIs(None, matcher.match(tree.branch))
184
185
    def test_matches(self):
186
        tree = self.make_branch_and_tree('.')
6855.4.1 by Jelmer Vernooij
Yet more bees.
187
        tree.commit('msg1', rev_id=b'a')
188
        tree.commit('msg2', rev_id=b'b')
7045.4.25 by Jelmer Vernooij
Fix test_matchers.
189
        matcher = RevisionHistoryMatches([b'a', b'b'])
6228.3.5 by Jelmer Vernooij
Add tests.
190
        self.assertIs(None, matcher.match(tree.branch))
191
192
    def test_mismatch(self):
193
        tree = self.make_branch_and_tree('.')
6855.4.1 by Jelmer Vernooij
Yet more bees.
194
        tree.commit('msg1', rev_id=b'a')
195
        tree.commit('msg2', rev_id=b'b')
7045.4.25 by Jelmer Vernooij
Fix test_matchers.
196
        matcher = RevisionHistoryMatches([b'a', b'b', b'c'])
7479.2.1 by Jelmer Vernooij
Drop python2 support.
197
        self.assertEqual(
198
            set(("[b'a', b'b']", "[b'a', b'b', b'c']")),
199
            set(matcher.match(tree.branch).describe().split(" != ")))