~bzr-pqm/bzr/bzr.dev

5669.3.8 by Jelmer Vernooij
Refactor, move to bzrlib.controldir.
1
# Copyright (C) 2011 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 bzrlib import (
23
    controldir,
5712.3.13 by Jelmer Vernooij
Add Prober.known_formats().
24
    errors,
5669.3.11 by Jelmer Vernooij
review feedback from vila.
25
    tests,
5717.1.5 by Jelmer Vernooij
Use recommend_upgrade.
26
    ui,
5669.3.8 by Jelmer Vernooij
Refactor, move to bzrlib.controldir.
27
    )
5712.3.13 by Jelmer Vernooij
Add Prober.known_formats().
28
from bzrlib.tests.scenarios import load_tests_apply_scenarios
29
30
31
load_tests = load_tests_apply_scenarios
5669.3.8 by Jelmer Vernooij
Refactor, move to bzrlib.controldir.
32
33
5669.3.10 by Jelmer Vernooij
Use ControlComponentFormat.
34
class SampleComponentFormat(controldir.ControlComponentFormat):
5669.3.8 by Jelmer Vernooij
Refactor, move to bzrlib.controldir.
35
36
    def get_format_string(self):
37
        return "Example component format."
38
39
5669.3.10 by Jelmer Vernooij
Use ControlComponentFormat.
40
class SampleExtraComponentFormat(controldir.ControlComponentFormat):
5669.3.8 by Jelmer Vernooij
Refactor, move to bzrlib.controldir.
41
    """Extra format, no format string."""
42
43
5669.3.11 by Jelmer Vernooij
review feedback from vila.
44
class TestMetaComponentFormatRegistry(tests.TestCase):
5669.3.8 by Jelmer Vernooij
Refactor, move to bzrlib.controldir.
45
46
    def setUp(self):
47
        super(TestMetaComponentFormatRegistry, self).setUp()
5669.3.9 by Jelmer Vernooij
Consistent naming.
48
        self.registry = controldir.ControlComponentFormatRegistry()
5669.3.8 by Jelmer Vernooij
Refactor, move to bzrlib.controldir.
49
50
    def test_register_unregister_format(self):
51
        format = SampleComponentFormat()
52
        self.registry.register(format)
53
        self.assertEquals(format,
54
            self.registry.get("Example component format."))
55
        self.registry.remove(format)
56
        self.assertRaises(KeyError, self.registry.get,
57
            "Example component format.")
58
59
    def test_get_all(self):
60
        format = SampleComponentFormat()
61
        self.assertEquals([], self.registry._get_all())
62
        self.registry.register(format)
63
        self.assertEquals([format], self.registry._get_all())
64
5676.1.7 by Jelmer Vernooij
Add test for ControlComponentFormatRegistry._get_all_modules.
65
    def test_get_all_modules(self):
66
        format = SampleComponentFormat()
67
        self.assertEquals(set(), self.registry._get_all_modules())
68
        self.registry.register(format)
69
        self.assertEquals(
70
            set(["bzrlib.tests.test_controldir"]),
71
            self.registry._get_all_modules())
72
5669.3.8 by Jelmer Vernooij
Refactor, move to bzrlib.controldir.
73
    def test_register_extra(self):
74
        format = SampleExtraComponentFormat()
75
        self.assertEquals([], self.registry._get_all())
76
        self.registry.register_extra(format)
77
        self.assertEquals([format], self.registry._get_all())
78
79
    def test_register_extra_lazy(self):
80
        self.assertEquals([], self.registry._get_all())
81
        self.registry.register_extra_lazy("bzrlib.tests.test_controldir",
82
            "SampleExtraComponentFormat")
83
        formats = self.registry._get_all()
84
        self.assertEquals(1, len(formats))
85
        self.assertIsInstance(formats[0], SampleExtraComponentFormat)
5712.3.3 by Jelmer Vernooij
Support lazy registration of ControlDirFormat.
86
87
5712.3.19 by Jelmer Vernooij
Raise exception from ControlDirFormat.register_format.
88
class TestControlDirFormatDeprecated(tests.TestCaseWithTransport):
89
    """Tests for removed registration method in the ControlDirFormat facility."""
90
91
    def test_register_format(self):
92
        self.assertRaises(errors.BzrError,
93
            controldir.ControlDirFormat.register_format, object())
5712.3.3 by Jelmer Vernooij
Support lazy registration of ControlDirFormat.
94
5712.3.13 by Jelmer Vernooij
Add Prober.known_formats().
95
96
class TestProber(tests.TestCaseWithTransport):
5712.3.14 by Jelmer Vernooij
Add Prober.known_formats.
97
    """Per-prober tests."""
5712.3.13 by Jelmer Vernooij
Add Prober.known_formats().
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_probe_transport_empty(self):
108
        transport = self.get_transport(".")
109
        self.assertRaises(errors.NotBranchError,
110
            self.prober.probe_transport, transport)
111
112
    def test_known_formats(self):
5712.3.15 by Jelmer Vernooij
Remove unused register format functions.
113
        known_formats = self.prober_cls.known_formats()
5712.3.13 by Jelmer Vernooij
Add Prober.known_formats().
114
        self.assertIsInstance(known_formats, set)
115
        for format in known_formats:
116
            self.assertIsInstance(format, controldir.ControlDirFormat,
117
                repr(format))
5712.3.18 by Jelmer Vernooij
Some more test fixes.
118
119
120
class NotBzrDir(controldir.ControlDir):
121
    """A non .bzr based control directory."""
122
123
    def __init__(self, transport, format):
124
        self._format = format
125
        self.root_transport = transport
126
        self.transport = transport.clone('.not')
127
128
129
class NotBzrDirFormat(controldir.ControlDirFormat):
130
    """A test class representing any non-.bzr based disk format."""
131
132
    def initialize_on_transport(self, transport):
133
        """Initialize a new .not dir in the base directory of a Transport."""
134
        transport.mkdir('.not')
135
        return self.open(transport)
136
137
    def open(self, transport):
138
        """Open this directory."""
139
        return NotBzrDir(transport, self)
140
141
142
class NotBzrDirProber(controldir.Prober):
143
144
    def probe_transport(self, transport):
145
        """Our format is present if the transport ends in '.not/'."""
146
        if transport.has('.not'):
147
            return NotBzrDirFormat()
148
149
    @classmethod
150
    def known_formats(cls):
151
        return set([NotBzrDirFormat()])
152
153
154
class TestNotBzrDir(tests.TestCaseWithTransport):
155
    """Tests for using the controldir api with a non .bzr based disk format.
156
157
    If/when one of these is in the core, we can let the implementation tests
158
    verify this works.
159
    """
160
161
    def test_create_and_find_format(self):
162
        # create a .notbzr dir
163
        format = NotBzrDirFormat()
164
        dir = format.initialize(self.get_url())
165
        self.assertIsInstance(dir, NotBzrDir)
166
        # now probe for it.
167
        controldir.ControlDirFormat.register_prober(NotBzrDirProber)
168
        try:
169
            found = controldir.ControlDirFormat.find_format(self.get_transport())
170
            self.assertIsInstance(found, NotBzrDirFormat)
171
        finally:
172
            controldir.ControlDirFormat.unregister_prober(NotBzrDirProber)
173
174
    def test_included_in_known_formats(self):
175
        controldir.ControlDirFormat.register_prober(NotBzrDirProber)
176
        self.addCleanup(controldir.ControlDirFormat.unregister_prober, NotBzrDirProber)
177
        formats = controldir.ControlDirFormat.known_formats()
178
        self.assertIsInstance(formats, set)
179
        for format in formats:
180
            if isinstance(format, NotBzrDirFormat):
181
                break
182
        else:
183
            self.fail("No NotBzrDirFormat in %s" % formats)
5717.1.4 by Jelmer Vernooij
Test default control component format implementation.
184
185
186
class UnsupportedControlComponentFormat(controldir.ControlComponentFormat):
187
188
    def is_supported(self):
189
        return False
190
191
5717.1.5 by Jelmer Vernooij
Use recommend_upgrade.
192
class OldControlComponentFormat(controldir.ControlComponentFormat):
193
194
    def get_format_description(self):
195
        return "An old format that is slow"
196
197
    upgrade_recommended = True
198
199
5717.1.4 by Jelmer Vernooij
Test default control component format implementation.
200
class DefaultControlComponentFormatTests(tests.TestCase):
201
    """Tests for default ControlComponentFormat implementation."""
202
5717.1.7 by Jelmer Vernooij
Rename check_status -> check_support_status.
203
    def test_check_support_status_unsupported(self):
5717.1.4 by Jelmer Vernooij
Test default control component format implementation.
204
        self.assertRaises(errors.UnsupportedFormatError,
5717.1.7 by Jelmer Vernooij
Rename check_status -> check_support_status.
205
            UnsupportedControlComponentFormat().check_support_status,
5717.1.4 by Jelmer Vernooij
Test default control component format implementation.
206
            allow_unsupported=False)
5717.1.7 by Jelmer Vernooij
Rename check_status -> check_support_status.
207
        UnsupportedControlComponentFormat().check_support_status(
5717.1.4 by Jelmer Vernooij
Test default control component format implementation.
208
            allow_unsupported=True)
209
5717.1.7 by Jelmer Vernooij
Rename check_status -> check_support_status.
210
    def test_check_support_status_supported(self):
211
        controldir.ControlComponentFormat().check_support_status(
5717.1.4 by Jelmer Vernooij
Test default control component format implementation.
212
            allow_unsupported=False)
5717.1.7 by Jelmer Vernooij
Rename check_status -> check_support_status.
213
        controldir.ControlComponentFormat().check_support_status(
5717.1.4 by Jelmer Vernooij
Test default control component format implementation.
214
            allow_unsupported=True)
5717.1.5 by Jelmer Vernooij
Use recommend_upgrade.
215
216
    def test_recommend_upgrade_current_format(self):
217
        stderr = tests.StringIOWrapper()
218
        ui.ui_factory = tests.TestUIFactory(stderr=stderr)
219
        format = controldir.ControlComponentFormat()
5717.1.7 by Jelmer Vernooij
Rename check_status -> check_support_status.
220
        format.check_support_status(allow_unsupported=False,
221
            recommend_upgrade=True)
5717.1.5 by Jelmer Vernooij
Use recommend_upgrade.
222
        self.assertEquals("", stderr.getvalue())
223
224
    def test_recommend_upgrade_old_format(self):
225
        stderr = tests.StringIOWrapper()
226
        ui.ui_factory = tests.TestUIFactory(stderr=stderr)
227
        format = OldControlComponentFormat()
5717.1.7 by Jelmer Vernooij
Rename check_status -> check_support_status.
228
        format.check_support_status(allow_unsupported=False,
229
            recommend_upgrade=False)
5717.1.5 by Jelmer Vernooij
Use recommend_upgrade.
230
        self.assertEquals("", stderr.getvalue())
5717.1.7 by Jelmer Vernooij
Rename check_status -> check_support_status.
231
        format.check_support_status(allow_unsupported=False,
232
            recommend_upgrade=True, basedir='apath')
5717.1.5 by Jelmer Vernooij
Use recommend_upgrade.
233
        self.assertEquals(
234
            'An old format that is slow is deprecated and a better format '
235
            'is available.\nIt is recommended that you upgrade by running '
236
            'the command\n  bzr upgrade apath\n',
237
            stderr.getvalue())