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