/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: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2020-08-23 01:15:41 UTC
  • mfrom: (7520.1.4 merge-3.1)
  • Revision ID: breezy.the.bot@gmail.com-20200823011541-nv0oh7nzaganx2qy
Merge lp:brz/3.1.

Merged from https://code.launchpad.net/~jelmer/brz/merge-3.1/+merge/389690

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2010, 2011, 2012, 2016 Canonical Ltd
 
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
 
 
17
"""Tests of breezy test matchers."""
 
18
 
 
19
from testtools.matchers import *
 
20
 
 
21
from . import (
 
22
    CapturedCall,
 
23
    TestCase,
 
24
    TestCaseWithTransport,
 
25
    )
 
26
from .matchers import *
 
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
 
 
42
class FakeUnlockable(object):
 
43
    """Something that can be unlocked."""
 
44
 
 
45
    def unlock(self):
 
46
        pass
 
47
 
 
48
 
 
49
class TestReturnsUnlockable(TestCase):
 
50
 
 
51
    def test___str__(self):
 
52
        matcher = ReturnsUnlockable(StubTree(True))
 
53
        self.assertEqual(
 
54
            'ReturnsUnlockable(lockable_thing=I am da tree)',
 
55
            str(matcher))
 
56
 
 
57
    def test_match(self):
 
58
        stub_tree = StubTree(False)
 
59
        matcher = ReturnsUnlockable(stub_tree)
 
60
        self.assertThat(matcher.match(lambda: FakeUnlockable()), Equals(None))
 
61
 
 
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"))
 
68
 
 
69
 
 
70
class TestMatchesAncestry(TestCaseWithTransport):
 
71
 
 
72
    def test__str__(self):
 
73
        matcher = MatchesAncestry("A repository", b"arevid")
 
74
        self.assertEqual(
 
75
            "MatchesAncestry(repository='A repository', "
 
76
            "revision_id=%r)" % (b'arevid', ),
 
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)
 
91
        m = MatchesAncestry(branch.repository, b"unknown")
 
92
        self.assertThat([b"unknown"], m)
 
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)
 
104
        self.assertEqual(
 
105
            "mismatched ancestry for revision %r was [%r], expected []" % (
 
106
                revid1, revid1),
 
107
            mismatch.describe())
 
108
 
 
109
 
 
110
class TestHasLayout(TestCaseWithTransport):
 
111
 
 
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))
 
115
 
 
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('')),
 
123
             ('a', b'a-id'),
 
124
             ('b/', b'b-id'),
 
125
             ('b/c', b'c-id')]))
 
126
 
 
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)
 
133
        self.assertEqual(
 
134
            set(("['', 'a', 'b/', 'b/c']", "['a']")),
 
135
            set(mismatch.describe().split(" != ")))
 
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'])
 
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)
 
147
        self.assertEqual(
 
148
            set(("['', 'a', 'b/', 'b/c']", "['', 'a']")),
 
149
            set(mismatch.describe().split(" != ")))
 
150
 
 
151
 
 
152
class TestHasPathRelations(TestCaseWithTransport):
 
153
 
 
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))
 
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,
 
165
                                            [('', ''),
 
166
                                             ('a', 'a'),
 
167
                                                ('b/', 'b/'),
 
168
                                                ('b/c', 'b/c')]))
 
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
 
 
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('.')
 
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))
 
191
 
 
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'])
 
197
        self.assertEqual(
 
198
            set(("[b'a', b'b']", "[b'a', b'b', b'c']")),
 
199
            set(matcher.match(tree.branch).describe().split(" != ")))