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 configuraiton files."""
21
from cStringIO import StringIO
22
from dulwich.config import (
33
from dulwich.tests import TestCase
37
class ConfigFileTests(TestCase):
39
def from_file(self, text):
40
return ConfigFile.from_file(StringIO(text))
46
self.assertEquals(ConfigFile(), ConfigFile())
48
def test_default_config(self):
49
cf = self.from_file("""[core]
50
repositoryformatversion = 0
53
logallrefupdates = true
55
self.assertEquals(ConfigFile({("core", ): {
56
"repositoryformatversion": "0",
59
"logallrefupdates": "true"}}), cf)
61
def test_from_file_empty(self):
62
cf = self.from_file("")
63
self.assertEquals(ConfigFile(), cf)
65
def test_empty_line_before_section(self):
66
cf = self.from_file("\n[section]\n")
67
self.assertEquals(ConfigFile({("section", ): {}}), cf)
69
def test_comment_before_section(self):
70
cf = self.from_file("# foo\n[section]\n")
71
self.assertEquals(ConfigFile({("section", ): {}}), cf)
73
def test_comment_after_section(self):
74
cf = self.from_file("[section] # foo\n")
75
self.assertEquals(ConfigFile({("section", ): {}}), cf)
77
def test_comment_after_variable(self):
78
cf = self.from_file("[section]\nbar= foo # a comment\n")
79
self.assertEquals(ConfigFile({("section", ): {"bar": "foo"}}), cf)
81
def test_from_file_section(self):
82
cf = self.from_file("[core]\nfoo = bar\n")
83
self.assertEquals("bar", cf.get(("core", ), "foo"))
84
self.assertEquals("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.assertEquals("bar", cf.get(("core", ), "foo"))
89
self.assertEquals("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.assertEquals("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.assertEquals(" bar", cf.get(("core", ), "foo"))
105
def test_from_file_with_interrupted_line(self):
110
self.assertEquals("barla", cf.get(("core", ), "foo"))
112
def test_from_file_with_boolean_setting(self):
116
self.assertEquals("true", cf.get(("core", ), "foo"))
118
def test_from_file_subsection(self):
119
cf = self.from_file("[branch \"foo\"]\nfoo = bar\n")
120
self.assertEquals("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.assertEquals("bar", cf.get(("branch", "foo"), "foo"))
130
def test_write_to_file_empty(self):
134
self.assertEquals("", f.getvalue())
136
def test_write_to_file_section(self):
138
c.set(("core", ), "foo", "bar")
141
self.assertEquals("[core]\nfoo = bar\n", f.getvalue())
143
def test_write_to_file_subsection(self):
145
c.set(("branch", "blie"), "foo", "bar")
148
self.assertEquals("[branch \"blie\"]\nfoo = bar\n", f.getvalue())
150
def test_same_line(self):
151
cf = self.from_file("[branch.foo] foo = bar\n")
152
self.assertEquals("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.assertEquals("bla", cd.get(("core", ), "foo"))
162
cd.set(("core", ), "foo", "bloe")
163
self.assertEquals("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")
175
class StackedConfigTests(TestCase):
177
def test_default_backends(self):
178
self.addCleanup(os.environ.__setitem__, "HOME", os.environ["HOME"])
179
os.environ["HOME"] = "/nonexistant"
180
StackedConfig.default_backends()
183
class UnescapeTests(TestCase):
185
def test_nothing(self):
186
self.assertEquals("", _unescape_value(""))
189
self.assertEquals("\tbar\t", _unescape_value("\\tbar\\t"))
191
def test_newline(self):
192
self.assertEquals("\nbar\t", _unescape_value("\\nbar\\t"))
194
def test_quote(self):
195
self.assertEquals("\"foo\"", _unescape_value("\\\"foo\\\""))
198
class EscapeValueTests(TestCase):
200
def test_nothing(self):
201
self.assertEquals("foo", _escape_value("foo"))
203
def test_backslash(self):
204
self.assertEquals("foo\\\\", _escape_value("foo\\"))
206
def test_newline(self):
207
self.assertEquals("foo\\n", _escape_value("foo\n"))
210
class FormatStringTests(TestCase):
212
def test_quoted(self):
213
self.assertEquals('" foo"', _format_string(" foo"))
214
self.assertEquals('"\\tfoo"', _format_string("\tfoo"))
216
def test_not_quoted(self):
217
self.assertEquals('foo', _format_string("foo"))
218
self.assertEquals('foo bar', _format_string("foo bar"))
221
class ParseStringTests(TestCase):
223
def test_quoted(self):
224
self.assertEquals(' foo', _parse_string('" foo"'))
225
self.assertEquals('\tfoo', _parse_string('"\\tfoo"'))
227
def test_not_quoted(self):
228
self.assertEquals('foo', _parse_string("foo"))
229
self.assertEquals('foo bar', _parse_string("foo bar"))
232
class CheckVariableNameTests(TestCase):
234
def test_invalid(self):
235
self.assertFalse(_check_variable_name("foo "))
236
self.assertFalse(_check_variable_name("bar,bar"))
237
self.assertFalse(_check_variable_name("bar.bar"))
239
def test_valid(self):
240
self.assertTrue(_check_variable_name("FOO"))
241
self.assertTrue(_check_variable_name("foo"))
242
self.assertTrue(_check_variable_name("foo-bar"))
245
class CheckSectionNameTests(TestCase):
247
def test_invalid(self):
248
self.assertFalse(_check_section_name("foo "))
249
self.assertFalse(_check_section_name("bar,bar"))
251
def test_valid(self):
252
self.assertTrue(_check_section_name("FOO"))
253
self.assertTrue(_check_section_name("foo"))
254
self.assertTrue(_check_section_name("foo-bar"))
255
self.assertTrue(_check_section_name("bar.bar"))