Debian Codegraph @

src/lazr/config/tests/test_config.py

raw symbols SCIP
1 # Copyright 2008-2015 Canonical Ltd. All rights reserved.
2 #
3 # This file is part of lazr.config.
4 #
5 # lazr.config is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU Lesser General Public License as published by
7 # the Free Software Foundation, version 3 of the License.
8 #
9 # lazr.config is distributed in the hope that it will be useful, but WITHOUT
10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
12 # License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public License
15 # along with lazr.config. If not, see <http://www.gnu.org/licenses/>.
16
17 """Tests of lazr.config."""
18
19 __all__ = [
20 "TestConfig",
21 ]
22
23
24 import importlib.util
25 import unittest
26 from configparser import MissingSectionHeaderError, NoSectionError
27 from io import StringIO
28 from operator import attrgetter
29 from pathlib import Path
30
31 from zope.interface.exceptions import DoesNotImplement
32 from zope.interface.verify import verifyObject
33
34 from lazr.config import ConfigSchema, ImplicitTypeSchema
35 from lazr.config.interfaces import (
36 ConfigErrors,
37 InvalidSectionNameError,
38 IStackableConfig,
39 NoCategoryError,
40 NoConfigError,
41 RedefinedSectionError,
42 UnknownKeyError,
43 UnknownSectionError,
44 )
45
46
47 class TestConfig(unittest.TestCase):
48 def _testfile(self, conf_file):
49 spec = importlib.util.find_spec("lazr.config.tests.testdata")
50 return str(Path(spec.origin).parent / conf_file)
51
52 def test_missing_category(self):
53 schema = ConfigSchema(self._testfile("base.conf"))
54 self.assertRaises(NoCategoryError, schema.getByCategory, "non-section")
55
56 def test_missing_file(self):
57 self.assertRaises(IOError, ConfigSchema, "/does/not/exist")
58
59 def test_must_be_ascii(self):
60 self.assertRaises(
61 UnicodeError, ConfigSchema, self._testfile("bad-nonascii.conf")
62 )
63
64 def test_missing_schema_section(self):
65 schema = ConfigSchema(self._testfile("base.conf"))
66 self.assertRaises(NoSectionError, schema.__getitem__, "section-4")
67
68 def test_missing_header_section(self):
69 self.assertRaises(
70 MissingSectionHeaderError,
71 ConfigSchema,
72 self._testfile("bad-sectionless.conf"),
73 )
74
75 def test_redefined_section(self):
76 self.assertRaises(
77 RedefinedSectionError,
78 ConfigSchema,
79 self._testfile("bad-redefined-section.conf"),
80 )
81 # XXX sinzui 2007-12-13:
82 # ConfigSchema should raise RedefinedKeyError when a section redefines
83 # a key.
84
85 def test_invalid_section_name(self):
86 self.assertRaises(
87 InvalidSectionNameError,
88 ConfigSchema,
89 self._testfile("bad-invalid-name.conf"),
90 )
91
92 def test_invalid_characters(self):
93 self.assertRaises(
94 InvalidSectionNameError,
95 ConfigSchema,
96 self._testfile("bad-invalid-name-chars.conf"),
97 )
98
99 def test_load_missing_file(self):
100 schema = ConfigSchema(self._testfile("base.conf"))
101 self.assertRaises(IOError, schema.load, "/no/such/file.conf")
102
103 def test_no_name_argument(self):
104 config = """
105 [meta]
106 metakey: unsupported
107 [unknown-section]
108 key1 = value1
109 [section_1]
110 keyn: unknown key
111 key1: bad character in caf\xc3)
112 [section_3.template]
113 key1: schema suffixes are not permitted
114 """
115 schema = ConfigSchema(self._testfile("base.conf"))
116 self.assertRaises(AttributeError, schema.loadFile, StringIO(config))
117
118 def test_missing_section(self):
119 schema = ConfigSchema(self._testfile("base.conf"))
120 config = schema.load(self._testfile("local.conf"))
121 self.assertRaises(NoSectionError, config.__getitem__, "section-4")
122
123 def test_undeclared_optional_section(self):
124 schema = ConfigSchema(self._testfile("base.conf"))
125 config = schema.load(self._testfile("local.conf"))
126 self.assertRaises(
127 NoSectionError, config.__getitem__, "section_3.app_a"
128 )
129
130 def test_nonexistent_category_name(self):
131 schema = ConfigSchema(self._testfile("base.conf"))
132 config = schema.load(self._testfile("local.conf"))
133 self.assertRaises(NoCategoryError, config.getByCategory, "non-section")
134
135 def test_all_config_errors(self):
136 schema = ConfigSchema(self._testfile("base.conf"))
137 config = schema.loadFile(
138 StringIO("""
139 [meta]
140 metakey: unsupported
141 [unknown-section]
142 key1 = value1
143 [section_1]
144 keyn: unknown key
145 key1: bad character in caf\xc3)
146 [section_3.template]
147 key1: schema suffixes are not permitted
148 """),
149 "bad config",
150 )
151 try:
152 config.validate()
153 except ConfigErrors as errors:
154 sorted_errors = sorted(
155 errors.errors, key=attrgetter("__class__.__name__")
156 )
157 self.assertEqual(
158 str(errors), "ConfigErrors: bad config is not valid."
159 )
160 else:
161 self.fail("ConfigErrors expected")
162 self.assertEqual(len(sorted_errors), 4)
163 self.assertEqual(
164 [error.__class__ for error in sorted_errors],
165 [
166 UnicodeEncodeError,
167 UnknownKeyError,
168 UnknownKeyError,
169 UnknownSectionError,
170 ],
171 )
172
173 def test_not_stackable(self):
174 try:
175 from zope.interface.exceptions import MultipleInvalid
176 except ImportError: # zope.interface < 5.0.0
177
178 class MultipleInvalid(Exception):
179 pass
180
181 schema = ConfigSchema(self._testfile("base.conf"))
182 config = schema.load(self._testfile("local.conf"))
183 try:
184 verifyObject(IStackableConfig, config.extends)
185 except DoesNotImplement:
186 pass
187 except MultipleInvalid as e:
188 if not any(
189 isinstance(invalid, DoesNotImplement)
190 for invalid in e.exceptions
191 ):
192 self.fail("MultipleInvalid raised without DoesNotImplement")
193 else:
194 self.fail("DoesNotImplement not raised")
195
196 def test_bad_pop(self):
197 schema = ConfigSchema(self._testfile("base.conf"))
198 config = schema.load(self._testfile("local.conf"))
199 config.push("one", "")
200 config.push("two", "")
201 self.assertRaises(NoConfigError, config.pop, "bad-name")
202
203 def test_cannot_pop_bottom(self):
204 schema = ConfigSchema(self._testfile("base.conf"))
205 config = schema.load(self._testfile("local.conf"))
206 config.pop("local.conf")
207 self.assertRaises(NoConfigError, config.pop, "base.conf")
208
209 def test_multiline_preserves_indentation(self):
210 schema = ImplicitTypeSchema(self._testfile("base.conf"))
211 config = schema.load(self._testfile("local.conf"))
212 convert = config["section_1"]._convert
213 orig = """\
214 multiline value 1
215 multiline value 2"""
216 new = convert(orig)
217 self.assertMultiLineEqual(new, orig)
218
219 def test_multiline_strips_leading_and_trailing_whitespace(self):
220 schema = ImplicitTypeSchema(self._testfile("base.conf"))
221 config = schema.load(self._testfile("local.conf"))
222 convert = config["section_1"]._convert
223 orig = """
224 multiline value 1
225 multiline value 2
226 """
227 new = convert(orig)
228 self.assertMultiLineEqual(new, orig.strip())
229
230 def test_multiline_key(self):
231 schema = ImplicitTypeSchema(self._testfile("base.conf"))
232 config = schema.load(self._testfile("local.conf"))
233 self.assertMultiLineEqual(
234 config["section_33"].key2,
235 """\
236 multiline value 1
237 multiline value 2""",
238 )
239
240 def test_lp1397779(self):
241 # Fix DuplicateSectionErrors when you .push() a config that has a
242 # section already defined in the config.
243 schema = ConfigSchema(self._testfile("base.conf"))
244 config = schema.load(self._testfile("local.conf"))
245 self.assertEqual(config["section_1"]["key1"], "foo")
246 config.push(
247 "dupsec",
248 """\
249 [section_1]
250 key1: baz
251 [section_1]
252 key1: qux
253 """,
254 )
255 self.assertEqual(config["section_1"]["key1"], "qux")