1 # test_config.py -- Tests for reading and writing configuration files
2 # Copyright (C) 2011 Jelmer Vernooij <jelmer@samba.org>
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # or (at your option) a later version of the License.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 """Tests for reading and writing configuration files."""
21 from io import BytesIO
22 from dulwich.config import (
34 from dulwich.tests import TestCase
37 class ConfigFileTests(TestCase):
39 def from_file(self, text):
40 return ConfigFile.from_file(BytesIO(text))
46 self.assertEqual(ConfigFile(), ConfigFile())
48 def test_default_config(self):
49 cf = self.from_file("""[core]
50 repositoryformatversion = 0
53 logallrefupdates = true
55 self.assertEqual(ConfigFile({("core", ): {
56 "repositoryformatversion": "0",
59 "logallrefupdates": "true"}}), cf)
61 def test_from_file_empty(self):
62 cf = self.from_file("")
63 self.assertEqual(ConfigFile(), cf)
65 def test_empty_line_before_section(self):
66 cf = self.from_file("\n[section]\n")
67 self.assertEqual(ConfigFile({("section", ): {}}), cf)
69 def test_comment_before_section(self):
70 cf = self.from_file("# foo\n[section]\n")
71 self.assertEqual(ConfigFile({("section", ): {}}), cf)
73 def test_comment_after_section(self):
74 cf = self.from_file("[section] # foo\n")
75 self.assertEqual(ConfigFile({("section", ): {}}), cf)
77 def test_comment_after_variable(self):
78 cf = self.from_file("[section]\nbar= foo # a comment\n")
79 self.assertEqual(ConfigFile({("section", ): {"bar": "foo"}}), cf)
81 def test_from_file_section(self):
82 cf = self.from_file("[core]\nfoo = bar\n")
83 self.assertEqual("bar", cf.get(("core", ), "foo"))
84 self.assertEqual("bar", cf.get(("core", "foo"), "foo"))
86 def test_from_file_section_case_insensitive(self):
87 cf = self.from_file("[cOre]\nfOo = bar\n")
88 self.assertEqual("bar", cf.get(("core", ), "foo"))
89 self.assertEqual("bar", cf.get(("core", "foo"), "foo"))
91 def test_from_file_with_mixed_quoted(self):
92 cf = self.from_file("[core]\nfoo = \"bar\"la\n")
93 self.assertEqual("barla", cf.get(("core", ), "foo"))
95 def test_from_file_with_open_quoted(self):
96 self.assertRaises(ValueError,
97 self.from_file, "[core]\nfoo = \"bar\n")
99 def test_from_file_with_quotes(self):
103 self.assertEqual(" bar", cf.get(("core", ), "foo"))
105 def test_from_file_with_interrupted_line(self):
110 self.assertEqual("barla", cf.get(("core", ), "foo"))
112 def test_from_file_with_boolean_setting(self):
116 self.assertEqual("true", cf.get(("core", ), "foo"))
118 def test_from_file_subsection(self):
119 cf = self.from_file("[branch \"foo\"]\nfoo = bar\n")
120 self.assertEqual("bar", cf.get(("branch", "foo"), "foo"))
122 def test_from_file_subsection_invalid(self):
123 self.assertRaises(ValueError,
124 self.from_file, "[branch \"foo]\nfoo = bar\n")
126 def test_from_file_subsection_not_quoted(self):
127 cf = self.from_file("[branch.foo]\nfoo = bar\n")
128 self.assertEqual("bar", cf.get(("branch", "foo"), "foo"))
130 def test_write_to_file_empty(self):
134 self.assertEqual("", f.getvalue())
136 def test_write_to_file_section(self):
138 c.set(("core", ), "foo", "bar")
141 self.assertEqual("[core]\n\tfoo = bar\n", f.getvalue())
143 def test_write_to_file_subsection(self):
145 c.set(("branch", "blie"), "foo", "bar")
148 self.assertEqual("[branch \"blie\"]\n\tfoo = bar\n", f.getvalue())
150 def test_same_line(self):
151 cf = self.from_file("[branch.foo] foo = bar\n")
152 self.assertEqual("bar", cf.get(("branch", "foo"), "foo"))
155 class ConfigDictTests(TestCase):
157 def test_get_set(self):
159 self.assertRaises(KeyError, cd.get, "foo", "core")
160 cd.set(("core", ), "foo", "bla")
161 self.assertEqual("bla", cd.get(("core", ), "foo"))
162 cd.set(("core", ), "foo", "bloe")
163 self.assertEqual("bloe", cd.get(("core", ), "foo"))
165 def test_get_boolean(self):
167 cd.set(("core", ), "foo", "true")
168 self.assertTrue(cd.get_boolean(("core", ), "foo"))
169 cd.set(("core", ), "foo", "false")
170 self.assertFalse(cd.get_boolean(("core", ), "foo"))
171 cd.set(("core", ), "foo", "invalid")
172 self.assertRaises(ValueError, cd.get_boolean, ("core", ), "foo")
176 cd.set(("core", ), "foo", "bla")
177 cd.set(("core2", ), "foo", "bloe")
179 self.assertEqual([("core", ), ("core2", )], cd.keys())
180 self.assertEqual(cd[("core", )], {'foo': 'bla'})
183 self.assertEqual(cd['a'], 'b')
185 def test_iteritems(self):
187 cd.set(("core", ), "foo", "bla")
188 cd.set(("core2", ), "foo", "bloe")
192 list(cd.iteritems(("core", ))))
194 def test_iteritems_nonexistant(self):
196 cd.set(("core2", ), "foo", "bloe")
199 list(cd.iteritems(("core", ))))
201 def test_itersections(self):
203 cd.set(("core2", ), "foo", "bloe")
205 self.assertEqual([("core2", )],
206 list(cd.itersections()))
210 class StackedConfigTests(TestCase):
212 def test_default_backends(self):
213 StackedConfig.default_backends()
216 class UnescapeTests(TestCase):
218 def test_nothing(self):
219 self.assertEqual("", _unescape_value(""))
222 self.assertEqual("\tbar\t", _unescape_value("\\tbar\\t"))
224 def test_newline(self):
225 self.assertEqual("\nbar\t", _unescape_value("\\nbar\\t"))
227 def test_quote(self):
228 self.assertEqual("\"foo\"", _unescape_value("\\\"foo\\\""))
231 class EscapeValueTests(TestCase):
233 def test_nothing(self):
234 self.assertEqual("foo", _escape_value("foo"))
236 def test_backslash(self):
237 self.assertEqual("foo\\\\", _escape_value("foo\\"))
239 def test_newline(self):
240 self.assertEqual("foo\\n", _escape_value("foo\n"))
243 class FormatStringTests(TestCase):
245 def test_quoted(self):
246 self.assertEqual('" foo"', _format_string(" foo"))
247 self.assertEqual('"\\tfoo"', _format_string("\tfoo"))
249 def test_not_quoted(self):
250 self.assertEqual('foo', _format_string("foo"))
251 self.assertEqual('foo bar', _format_string("foo bar"))
254 class ParseStringTests(TestCase):
256 def test_quoted(self):
257 self.assertEqual(' foo', _parse_string('" foo"'))
258 self.assertEqual('\tfoo', _parse_string('"\\tfoo"'))
260 def test_not_quoted(self):
261 self.assertEqual('foo', _parse_string("foo"))
262 self.assertEqual('foo bar', _parse_string("foo bar"))
265 class CheckVariableNameTests(TestCase):
267 def test_invalid(self):
268 self.assertFalse(_check_variable_name("foo "))
269 self.assertFalse(_check_variable_name("bar,bar"))
270 self.assertFalse(_check_variable_name("bar.bar"))
272 def test_valid(self):
273 self.assertTrue(_check_variable_name("FOO"))
274 self.assertTrue(_check_variable_name("foo"))
275 self.assertTrue(_check_variable_name("foo-bar"))
278 class CheckSectionNameTests(TestCase):
280 def test_invalid(self):
281 self.assertFalse(_check_section_name("foo "))
282 self.assertFalse(_check_section_name("bar,bar"))
284 def test_valid(self):
285 self.assertTrue(_check_section_name("FOO"))
286 self.assertTrue(_check_section_name("foo"))
287 self.assertTrue(_check_section_name("foo-bar"))
288 self.assertTrue(_check_section_name("bar.bar"))