~jelmer/dulwich/lp-pqm

« back to all changes in this revision

Viewing changes to dulwich/tests/test_config.py

  • Committer: Jelmer Vernooij
  • Date: 2012-02-01 22:13:51 UTC
  • mfrom: (413.11.554)
  • Revision ID: jelmer@samba.org-20120201221351-b3n2p9zttzh62dwu
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 configuraiton files."""
 
20
 
 
21
from cStringIO import StringIO
 
22
from dulwich.config import (
 
23
    ConfigDict,
 
24
    ConfigFile,
 
25
    StackedConfig,
 
26
    _check_section_name,
 
27
    _check_variable_name,
 
28
    _format_string,
 
29
    _escape_value,
 
30
    _parse_string,
 
31
    _unescape_value,
 
32
    )
 
33
from dulwich.tests import TestCase
 
34
import os
 
35
 
 
36
 
 
37
class ConfigFileTests(TestCase):
 
38
 
 
39
    def from_file(self, text):
 
40
        return ConfigFile.from_file(StringIO(text))
 
41
 
 
42
    def test_empty(self):
 
43
        ConfigFile()
 
44
 
 
45
    def test_eq(self):
 
46
        self.assertEquals(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.assertEquals(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.assertEquals(ConfigFile(), cf)
 
64
 
 
65
    def test_empty_line_before_section(self):
 
66
        cf = self.from_file("\n[section]\n")
 
67
        self.assertEquals(ConfigFile({("section", ): {}}), cf)
 
68
 
 
69
    def test_comment_before_section(self):
 
70
        cf = self.from_file("# foo\n[section]\n")
 
71
        self.assertEquals(ConfigFile({("section", ): {}}), cf)
 
72
 
 
73
    def test_comment_after_section(self):
 
74
        cf = self.from_file("[section] # foo\n")
 
75
        self.assertEquals(ConfigFile({("section", ): {}}), cf)
 
76
 
 
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)
 
80
 
 
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"))
 
85
 
 
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"))
 
90
 
 
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"))
 
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.assertEquals(" 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.assertEquals("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.assertEquals("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.assertEquals("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.assertEquals("bar", cf.get(("branch", "foo"), "foo"))
 
129
 
 
130
    def test_write_to_file_empty(self):
 
131
        c = ConfigFile()
 
132
        f = StringIO()
 
133
        c.write_to_file(f)
 
134
        self.assertEquals("", f.getvalue())
 
135
 
 
136
    def test_write_to_file_section(self):
 
137
        c = ConfigFile()
 
138
        c.set(("core", ), "foo", "bar")
 
139
        f = StringIO()
 
140
        c.write_to_file(f)
 
141
        self.assertEquals("[core]\nfoo = 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 = StringIO()
 
147
        c.write_to_file(f)
 
148
        self.assertEquals("[branch \"blie\"]\nfoo = bar\n", f.getvalue())
 
149
 
 
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"))
 
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.assertEquals("bla", cd.get(("core", ), "foo"))
 
162
        cd.set(("core", ), "foo", "bloe")
 
163
        self.assertEquals("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
 
 
175
class StackedConfigTests(TestCase):
 
176
 
 
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()
 
181
 
 
182
 
 
183
class UnescapeTests(TestCase):
 
184
 
 
185
    def test_nothing(self):
 
186
        self.assertEquals("", _unescape_value(""))
 
187
 
 
188
    def test_tab(self):
 
189
        self.assertEquals("\tbar\t", _unescape_value("\\tbar\\t"))
 
190
 
 
191
    def test_newline(self):
 
192
        self.assertEquals("\nbar\t", _unescape_value("\\nbar\\t"))
 
193
 
 
194
    def test_quote(self):
 
195
        self.assertEquals("\"foo\"", _unescape_value("\\\"foo\\\""))
 
196
 
 
197
 
 
198
class EscapeValueTests(TestCase):
 
199
 
 
200
    def test_nothing(self):
 
201
        self.assertEquals("foo", _escape_value("foo"))
 
202
 
 
203
    def test_backslash(self):
 
204
        self.assertEquals("foo\\\\", _escape_value("foo\\"))
 
205
 
 
206
    def test_newline(self):
 
207
        self.assertEquals("foo\\n", _escape_value("foo\n"))
 
208
 
 
209
 
 
210
class FormatStringTests(TestCase):
 
211
 
 
212
    def test_quoted(self):
 
213
        self.assertEquals('" foo"', _format_string(" foo"))
 
214
        self.assertEquals('"\\tfoo"', _format_string("\tfoo"))
 
215
 
 
216
    def test_not_quoted(self):
 
217
        self.assertEquals('foo', _format_string("foo"))
 
218
        self.assertEquals('foo bar', _format_string("foo bar"))
 
219
 
 
220
 
 
221
class ParseStringTests(TestCase):
 
222
 
 
223
    def test_quoted(self):
 
224
        self.assertEquals(' foo', _parse_string('" foo"'))
 
225
        self.assertEquals('\tfoo', _parse_string('"\\tfoo"'))
 
226
 
 
227
    def test_not_quoted(self):
 
228
        self.assertEquals('foo', _parse_string("foo"))
 
229
        self.assertEquals('foo bar', _parse_string("foo bar"))
 
230
 
 
231
 
 
232
class CheckVariableNameTests(TestCase):
 
233
 
 
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"))
 
238
 
 
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"))
 
243
 
 
244
 
 
245
class CheckSectionNameTests(TestCase):
 
246
 
 
247
    def test_invalid(self):
 
248
        self.assertFalse(_check_section_name("foo "))
 
249
        self.assertFalse(_check_section_name("bar,bar"))
 
250
 
 
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"))