Use io.BytesIO rather than cStringIO.
[jelmer/dulwich.git] / dulwich / tests / test_config.py
1 # test_config.py -- Tests for reading and writing configuration files
2 # Copyright (C) 2011 Jelmer Vernooij <jelmer@samba.org>
3 #
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.
8 #
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.
13 #
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,
17 # MA  02110-1301, USA.
18
19 """Tests for reading and writing configuration files."""
20
21 from io import BytesIO
22 from dulwich.config import (
23     ConfigDict,
24     ConfigFile,
25     OrderedDict,
26     StackedConfig,
27     _check_section_name,
28     _check_variable_name,
29     _format_string,
30     _escape_value,
31     _parse_string,
32     _unescape_value,
33     )
34 from dulwich.tests import TestCase
35
36
37 class ConfigFileTests(TestCase):
38
39     def from_file(self, text):
40         return ConfigFile.from_file(BytesIO(text))
41
42     def test_empty(self):
43         ConfigFile()
44
45     def test_eq(self):
46         self.assertEqual(ConfigFile(), ConfigFile())
47
48     def test_default_config(self):
49         cf = self.from_file("""[core]
50         repositoryformatversion = 0
51         filemode = true
52         bare = false
53         logallrefupdates = true
54 """)
55         self.assertEqual(ConfigFile({("core", ): {
56             "repositoryformatversion": "0",
57             "filemode": "true",
58             "bare": "false",
59             "logallrefupdates": "true"}}), cf)
60
61     def test_from_file_empty(self):
62         cf = self.from_file("")
63         self.assertEqual(ConfigFile(), cf)
64
65     def test_empty_line_before_section(self):
66         cf = self.from_file("\n[section]\n")
67         self.assertEqual(ConfigFile({("section", ): {}}), cf)
68
69     def test_comment_before_section(self):
70         cf = self.from_file("# foo\n[section]\n")
71         self.assertEqual(ConfigFile({("section", ): {}}), cf)
72
73     def test_comment_after_section(self):
74         cf = self.from_file("[section] # foo\n")
75         self.assertEqual(ConfigFile({("section", ): {}}), cf)
76
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)
80
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"))
85
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"))
90
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"))
94
95     def test_from_file_with_open_quoted(self):
96         self.assertRaises(ValueError,
97             self.from_file, "[core]\nfoo = \"bar\n")
98
99     def test_from_file_with_quotes(self):
100         cf = self.from_file(
101             "[core]\n"
102             'foo = " bar"\n')
103         self.assertEqual(" bar", cf.get(("core", ), "foo"))
104
105     def test_from_file_with_interrupted_line(self):
106         cf = self.from_file(
107             "[core]\n"
108             'foo = bar\\\n'
109             ' la\n')
110         self.assertEqual("barla", cf.get(("core", ), "foo"))
111
112     def test_from_file_with_boolean_setting(self):
113         cf = self.from_file(
114             "[core]\n"
115             'foo\n')
116         self.assertEqual("true", cf.get(("core", ), "foo"))
117
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"))
121
122     def test_from_file_subsection_invalid(self):
123         self.assertRaises(ValueError,
124             self.from_file, "[branch \"foo]\nfoo = bar\n")
125
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"))
129
130     def test_write_to_file_empty(self):
131         c = ConfigFile()
132         f = BytesIO()
133         c.write_to_file(f)
134         self.assertEqual("", f.getvalue())
135
136     def test_write_to_file_section(self):
137         c = ConfigFile()
138         c.set(("core", ), "foo", "bar")
139         f = BytesIO()
140         c.write_to_file(f)
141         self.assertEqual("[core]\n\tfoo = bar\n", f.getvalue())
142
143     def test_write_to_file_subsection(self):
144         c = ConfigFile()
145         c.set(("branch", "blie"), "foo", "bar")
146         f = BytesIO()
147         c.write_to_file(f)
148         self.assertEqual("[branch \"blie\"]\n\tfoo = bar\n", f.getvalue())
149
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"))
153
154
155 class ConfigDictTests(TestCase):
156
157     def test_get_set(self):
158         cd = ConfigDict()
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"))
164
165     def test_get_boolean(self):
166         cd = ConfigDict()
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")
173
174     def test_dict(self):
175         cd = ConfigDict()
176         cd.set(("core", ), "foo", "bla")
177         cd.set(("core2", ), "foo", "bloe")
178
179         self.assertEqual([("core", ), ("core2", )], cd.keys())
180         self.assertEqual(cd[("core", )], {'foo': 'bla'})
181
182         cd['a'] = 'b'
183         self.assertEqual(cd['a'], 'b')
184
185     def test_iteritems(self):
186         cd = ConfigDict()
187         cd.set(("core", ), "foo", "bla")
188         cd.set(("core2", ), "foo", "bloe")
189
190         self.assertEqual(
191             [('foo', 'bla')],
192             list(cd.iteritems(("core", ))))
193
194     def test_iteritems_nonexistant(self):
195         cd = ConfigDict()
196         cd.set(("core2", ), "foo", "bloe")
197
198         self.assertEqual([],
199             list(cd.iteritems(("core", ))))
200
201     def test_itersections(self):
202         cd = ConfigDict()
203         cd.set(("core2", ), "foo", "bloe")
204
205         self.assertEqual([("core2", )],
206             list(cd.itersections()))
207
208
209
210 class StackedConfigTests(TestCase):
211
212     def test_default_backends(self):
213         StackedConfig.default_backends()
214
215
216 class UnescapeTests(TestCase):
217
218     def test_nothing(self):
219         self.assertEqual("", _unescape_value(""))
220
221     def test_tab(self):
222         self.assertEqual("\tbar\t", _unescape_value("\\tbar\\t"))
223
224     def test_newline(self):
225         self.assertEqual("\nbar\t", _unescape_value("\\nbar\\t"))
226
227     def test_quote(self):
228         self.assertEqual("\"foo\"", _unescape_value("\\\"foo\\\""))
229
230
231 class EscapeValueTests(TestCase):
232
233     def test_nothing(self):
234         self.assertEqual("foo", _escape_value("foo"))
235
236     def test_backslash(self):
237         self.assertEqual("foo\\\\", _escape_value("foo\\"))
238
239     def test_newline(self):
240         self.assertEqual("foo\\n", _escape_value("foo\n"))
241
242
243 class FormatStringTests(TestCase):
244
245     def test_quoted(self):
246         self.assertEqual('" foo"', _format_string(" foo"))
247         self.assertEqual('"\\tfoo"', _format_string("\tfoo"))
248
249     def test_not_quoted(self):
250         self.assertEqual('foo', _format_string("foo"))
251         self.assertEqual('foo bar', _format_string("foo bar"))
252
253
254 class ParseStringTests(TestCase):
255
256     def test_quoted(self):
257         self.assertEqual(' foo', _parse_string('" foo"'))
258         self.assertEqual('\tfoo', _parse_string('"\\tfoo"'))
259
260     def test_not_quoted(self):
261         self.assertEqual('foo', _parse_string("foo"))
262         self.assertEqual('foo bar', _parse_string("foo bar"))
263
264
265 class CheckVariableNameTests(TestCase):
266
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"))
271
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"))
276
277
278 class CheckSectionNameTests(TestCase):
279
280     def test_invalid(self):
281         self.assertFalse(_check_section_name("foo "))
282         self.assertFalse(_check_section_name("bar,bar"))
283
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"))