/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_controldir.py

  • Committer: Jelmer Vernooij
  • Date: 2020-04-05 19:11:34 UTC
  • mto: (7490.7.16 work)
  • mto: This revision was merged to the branch mainline in revision 7501.
  • Revision ID: jelmer@jelmer.uk-20200405191134-0aebh8ikiwygxma5
Populate the .gitignore file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2011, 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 for the ControlDir facility.
 
18
 
 
19
For interface contract tests, see tests/per_control_dir.
 
20
"""
 
21
 
 
22
from .. import (
 
23
    controldir,
 
24
    errors,
 
25
    tests,
 
26
    ui,
 
27
    )
 
28
from .scenarios import load_tests_apply_scenarios
 
29
 
 
30
 
 
31
load_tests = load_tests_apply_scenarios
 
32
 
 
33
 
 
34
class TestErrors(tests.TestCase):
 
35
 
 
36
    def test_must_have_working_tree(self):
 
37
        err = controldir.MustHaveWorkingTree('foo', 'bar')
 
38
        self.assertEqual(str(err), "Branching 'bar'(foo) must create a"
 
39
                                   " working tree.")
 
40
 
 
41
 
 
42
class SampleComponentFormat(controldir.ControlComponentFormat):
 
43
 
 
44
    def get_format_string(self):
 
45
        return b"Example component format."
 
46
 
 
47
 
 
48
class SampleExtraComponentFormat(controldir.ControlComponentFormat):
 
49
    """Extra format, no format string."""
 
50
 
 
51
 
 
52
class TestMetaComponentFormatRegistry(tests.TestCase):
 
53
 
 
54
    def setUp(self):
 
55
        super(TestMetaComponentFormatRegistry, self).setUp()
 
56
        self.registry = controldir.ControlComponentFormatRegistry()
 
57
 
 
58
    def test_register_unregister_format(self):
 
59
        format = SampleComponentFormat()
 
60
        self.registry.register(format)
 
61
        self.assertEqual(format,
 
62
                         self.registry.get(b"Example component format."))
 
63
        self.registry.remove(format)
 
64
        self.assertRaises(KeyError, self.registry.get,
 
65
                          b"Example component format.")
 
66
 
 
67
    def test_get_all(self):
 
68
        format = SampleComponentFormat()
 
69
        self.assertEqual([], self.registry._get_all())
 
70
        self.registry.register(format)
 
71
        self.assertEqual([format], self.registry._get_all())
 
72
 
 
73
    def test_get_all_modules(self):
 
74
        format = SampleComponentFormat()
 
75
        self.assertEqual(set(), self.registry._get_all_modules())
 
76
        self.registry.register(format)
 
77
        self.assertEqual(
 
78
            {"breezy.tests.test_controldir"},
 
79
            self.registry._get_all_modules())
 
80
 
 
81
    def test_register_extra(self):
 
82
        format = SampleExtraComponentFormat()
 
83
        self.assertEqual([], self.registry._get_all())
 
84
        self.registry.register_extra(format)
 
85
        self.assertEqual([format], self.registry._get_all())
 
86
 
 
87
    def test_register_extra_lazy(self):
 
88
        self.assertEqual([], self.registry._get_all())
 
89
        self.registry.register_extra_lazy("breezy.tests.test_controldir",
 
90
                                          "SampleExtraComponentFormat")
 
91
        formats = self.registry._get_all()
 
92
        self.assertEqual(1, len(formats))
 
93
        self.assertIsInstance(formats[0], SampleExtraComponentFormat)
 
94
 
 
95
 
 
96
class TestProber(tests.TestCaseWithTransport):
 
97
    """Per-prober tests."""
 
98
 
 
99
    scenarios = [
 
100
        (prober_cls.__name__, {'prober_cls': prober_cls})
 
101
        for prober_cls in controldir.ControlDirFormat._probers]
 
102
 
 
103
    def setUp(self):
 
104
        super(TestProber, self).setUp()
 
105
        self.prober = self.prober_cls()
 
106
 
 
107
    def test_priority(self):
 
108
        transport = self.get_transport(".")
 
109
        self.assertIsInstance(self.prober.priority(transport), int)
 
110
 
 
111
    def test_probe_transport_empty(self):
 
112
        transport = self.get_transport(".")
 
113
        self.assertRaises(errors.NotBranchError,
 
114
                          self.prober.probe_transport, transport)
 
115
 
 
116
    def test_known_formats(self):
 
117
        known_formats = self.prober_cls.known_formats()
 
118
        self.assertIsInstance(known_formats, list)
 
119
        for format in known_formats:
 
120
            self.assertIsInstance(format, controldir.ControlDirFormat,
 
121
                                  repr(format))
 
122
 
 
123
 
 
124
class NotBzrDir(controldir.ControlDir):
 
125
    """A non .bzr based control directory."""
 
126
 
 
127
    def __init__(self, transport, format):
 
128
        self._format = format
 
129
        self.root_transport = transport
 
130
        self.transport = transport.clone('.not')
 
131
 
 
132
 
 
133
class NotBzrDirFormat(controldir.ControlDirFormat):
 
134
    """A test class representing any non-.bzr based disk format."""
 
135
 
 
136
    def initialize_on_transport(self, transport):
 
137
        """Initialize a new .not dir in the base directory of a Transport."""
 
138
        transport.mkdir('.not')
 
139
        return self.open(transport)
 
140
 
 
141
    def open(self, transport):
 
142
        """Open this directory."""
 
143
        return NotBzrDir(transport, self)
 
144
 
 
145
 
 
146
class NotBzrDirProber(controldir.Prober):
 
147
 
 
148
    def probe_transport(self, transport):
 
149
        """Our format is present if the transport ends in '.not/'."""
 
150
        if transport.has('.not'):
 
151
            return NotBzrDirFormat()
 
152
 
 
153
    @classmethod
 
154
    def known_formats(cls):
 
155
        return [NotBzrDirFormat()]
 
156
 
 
157
 
 
158
class TestNotBzrDir(tests.TestCaseWithTransport):
 
159
    """Tests for using the controldir api with a non .bzr based disk format.
 
160
 
 
161
    If/when one of these is in the core, we can let the implementation tests
 
162
    verify this works.
 
163
    """
 
164
 
 
165
    def test_create_and_find_format(self):
 
166
        # create a .notbzr dir
 
167
        format = NotBzrDirFormat()
 
168
        dir = format.initialize(self.get_url())
 
169
        self.assertIsInstance(dir, NotBzrDir)
 
170
        # now probe for it.
 
171
        controldir.ControlDirFormat.register_prober(NotBzrDirProber)
 
172
        try:
 
173
            found = controldir.ControlDirFormat.find_format(
 
174
                self.get_transport())
 
175
            self.assertIsInstance(found, NotBzrDirFormat)
 
176
        finally:
 
177
            controldir.ControlDirFormat.unregister_prober(NotBzrDirProber)
 
178
 
 
179
    def test_included_in_known_formats(self):
 
180
        controldir.ControlDirFormat.register_prober(NotBzrDirProber)
 
181
        self.addCleanup(
 
182
            controldir.ControlDirFormat.unregister_prober, NotBzrDirProber)
 
183
        formats = controldir.ControlDirFormat.known_formats()
 
184
        self.assertIsInstance(formats, list)
 
185
        for format in formats:
 
186
            if isinstance(format, NotBzrDirFormat):
 
187
                break
 
188
        else:
 
189
            self.fail("No NotBzrDirFormat in %s" % formats)
 
190
 
 
191
 
 
192
class UnsupportedControlComponentFormat(controldir.ControlComponentFormat):
 
193
 
 
194
    def is_supported(self):
 
195
        return False
 
196
 
 
197
 
 
198
class OldControlComponentFormat(controldir.ControlComponentFormat):
 
199
 
 
200
    def get_format_description(self):
 
201
        return "An old format that is slow"
 
202
 
 
203
    upgrade_recommended = True
 
204
 
 
205
 
 
206
class DefaultControlComponentFormatTests(tests.TestCase):
 
207
    """Tests for default ControlComponentFormat implementation."""
 
208
 
 
209
    def test_check_support_status_unsupported(self):
 
210
        self.assertRaises(errors.UnsupportedFormatError,
 
211
                          UnsupportedControlComponentFormat().check_support_status,
 
212
                          allow_unsupported=False)
 
213
        UnsupportedControlComponentFormat().check_support_status(
 
214
            allow_unsupported=True)
 
215
 
 
216
    def test_check_support_status_supported(self):
 
217
        controldir.ControlComponentFormat().check_support_status(
 
218
            allow_unsupported=False)
 
219
        controldir.ControlComponentFormat().check_support_status(
 
220
            allow_unsupported=True)
 
221
 
 
222
    def test_recommend_upgrade_current_format(self):
 
223
        ui.ui_factory = tests.TestUIFactory()
 
224
        format = controldir.ControlComponentFormat()
 
225
        format.check_support_status(allow_unsupported=False,
 
226
                                    recommend_upgrade=True)
 
227
        self.assertEqual("", ui.ui_factory.stderr.getvalue())
 
228
 
 
229
    def test_recommend_upgrade_old_format(self):
 
230
        ui.ui_factory = tests.TestUIFactory()
 
231
        format = OldControlComponentFormat()
 
232
        format.check_support_status(allow_unsupported=False,
 
233
                                    recommend_upgrade=False)
 
234
        self.assertEqual("", ui.ui_factory.stderr.getvalue())
 
235
        format.check_support_status(allow_unsupported=False,
 
236
                                    recommend_upgrade=True, basedir='apath')
 
237
        self.assertEqual(
 
238
            'An old format that is slow is deprecated and a better format '
 
239
            'is available.\nIt is recommended that you upgrade by running '
 
240
            'the command\n  brz upgrade apath\n',
 
241
            ui.ui_factory.stderr.getvalue())
 
242
 
 
243
 
 
244
class IsControlFilenameTest(tests.TestCase):
 
245
 
 
246
    def test_is_bzrdir(self):
 
247
        self.assertTrue(controldir.is_control_filename('.bzr'))
 
248
        self.assertTrue(controldir.is_control_filename('.git'))
 
249
 
 
250
    def test_is_not_bzrdir(self):
 
251
        self.assertFalse(controldir.is_control_filename('bla'))