Statistics
| Revision:

gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.app / org.gvsig.scripting.app.mainplugin / src / main / resources-plugin / scripting / lib / dulwich / tests / test_config.py @ 959

History | View | Annotate | Download (9.96 KB)

1
# test_config.py -- Tests for reading and writing configuration files
2
# Copyright (C) 2011 Jelmer Vernooij <jelmer@samba.org>
3
#
4
# Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
5
# General Public License as public by the Free Software Foundation; version 2.0
6
# or (at your option) any later version. You can redistribute it and/or
7
# modify it under the terms of either of these two licenses.
8
#
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS,
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
# See the License for the specific language governing permissions and
13
# limitations under the License.
14
#
15
# You should have received a copy of the licenses; if not, see
16
# <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
17
# and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
18
# License, Version 2.0.
19
#
20

    
21
"""Tests for reading and writing configuration files."""
22

    
23
from io import BytesIO
24
import os
25
from dulwich.config import (
26
    ConfigDict,
27
    ConfigFile,
28
    StackedConfig,
29
    _check_section_name,
30
    _check_variable_name,
31
    _format_string,
32
    _escape_value,
33
    _parse_string,
34
    parse_submodules,
35
    )
36
from dulwich.tests import (
37
    TestCase,
38
    )
39

    
40

    
41
class ConfigFileTests(TestCase):
42

    
43
    def from_file(self, text):
44
        return ConfigFile.from_file(BytesIO(text))
45

    
46
    def test_empty(self):
47
        ConfigFile()
48

    
49
    def test_eq(self):
50
        self.assertEqual(ConfigFile(), ConfigFile())
51

    
52
    def test_default_config(self):
53
        cf = self.from_file(b"""[core]
54
        repositoryformatversion = 0
55
        filemode = true
56
        bare = false
57
        logallrefupdates = true
58
""")
59
        self.assertEqual(ConfigFile({(b"core", ): {
60
            b"repositoryformatversion": b"0",
61
            b"filemode": b"true",
62
            b"bare": b"false",
63
            b"logallrefupdates": b"true"}}), cf)
64

    
65
    def test_from_file_empty(self):
66
        cf = self.from_file(b"")
67
        self.assertEqual(ConfigFile(), cf)
68

    
69
    def test_empty_line_before_section(self):
70
        cf = self.from_file(b"\n[section]\n")
71
        self.assertEqual(ConfigFile({(b"section", ): {}}), cf)
72

    
73
    def test_comment_before_section(self):
74
        cf = self.from_file(b"# foo\n[section]\n")
75
        self.assertEqual(ConfigFile({(b"section", ): {}}), cf)
76

    
77
    def test_comment_after_section(self):
78
        cf = self.from_file(b"[section] # foo\n")
79
        self.assertEqual(ConfigFile({(b"section", ): {}}), cf)
80

    
81
    def test_comment_after_variable(self):
82
        cf = self.from_file(b"[section]\nbar= foo # a comment\n")
83
        self.assertEqual(ConfigFile({(b"section", ): {b"bar": b"foo"}}), cf)
84

    
85
    def test_from_file_section(self):
86
        cf = self.from_file(b"[core]\nfoo = bar\n")
87
        self.assertEqual(b"bar", cf.get((b"core", ), b"foo"))
88
        self.assertEqual(b"bar", cf.get((b"core", b"foo"), b"foo"))
89

    
90
    def test_from_file_section_case_insensitive(self):
91
        cf = self.from_file(b"[cOre]\nfOo = bar\n")
92
        self.assertEqual(b"bar", cf.get((b"core", ), b"foo"))
93
        self.assertEqual(b"bar", cf.get((b"core", b"foo"), b"foo"))
94

    
95
    def test_from_file_with_mixed_quoted(self):
96
        cf = self.from_file(b"[core]\nfoo = \"bar\"la\n")
97
        self.assertEqual(b"barla", cf.get((b"core", ), b"foo"))
98

    
99
    def test_from_file_with_open_quoted(self):
100
        self.assertRaises(ValueError,
101
            self.from_file, b"[core]\nfoo = \"bar\n")
102

    
103
    def test_from_file_with_quotes(self):
104
        cf = self.from_file(
105
            b"[core]\n"
106
            b'foo = " bar"\n')
107
        self.assertEqual(b" bar", cf.get((b"core", ), b"foo"))
108

    
109
    def test_from_file_with_interrupted_line(self):
110
        cf = self.from_file(
111
            b"[core]\n"
112
            b'foo = bar\\\n'
113
            b' la\n')
114
        self.assertEqual(b"barla", cf.get((b"core", ), b"foo"))
115

    
116
    def test_from_file_with_boolean_setting(self):
117
        cf = self.from_file(
118
            b"[core]\n"
119
            b'foo\n')
120
        self.assertEqual(b"true", cf.get((b"core", ), b"foo"))
121

    
122
    def test_from_file_subsection(self):
123
        cf = self.from_file(b"[branch \"foo\"]\nfoo = bar\n")
124
        self.assertEqual(b"bar", cf.get((b"branch", b"foo"), b"foo"))
125

    
126
    def test_from_file_subsection_invalid(self):
127
        self.assertRaises(ValueError,
128
            self.from_file, b"[branch \"foo]\nfoo = bar\n")
129

    
130
    def test_from_file_subsection_not_quoted(self):
131
        cf = self.from_file(b"[branch.foo]\nfoo = bar\n")
132
        self.assertEqual(b"bar", cf.get((b"branch", b"foo"), b"foo"))
133

    
134
    def test_write_to_file_empty(self):
135
        c = ConfigFile()
136
        f = BytesIO()
137
        c.write_to_file(f)
138
        self.assertEqual(b"", f.getvalue())
139

    
140
    def test_write_to_file_section(self):
141
        c = ConfigFile()
142
        c.set((b"core", ), b"foo", b"bar")
143
        f = BytesIO()
144
        c.write_to_file(f)
145
        self.assertEqual(b"[core]\n\tfoo = bar\n", f.getvalue())
146

    
147
    def test_write_to_file_subsection(self):
148
        c = ConfigFile()
149
        c.set((b"branch", b"blie"), b"foo", b"bar")
150
        f = BytesIO()
151
        c.write_to_file(f)
152
        self.assertEqual(b"[branch \"blie\"]\n\tfoo = bar\n", f.getvalue())
153

    
154
    def test_same_line(self):
155
        cf = self.from_file(b"[branch.foo] foo = bar\n")
156
        self.assertEqual(b"bar", cf.get((b"branch", b"foo"), b"foo"))
157

    
158
    #@expectedFailure
159
    def test_quoted(self):
160
        cf = self.from_file(b"""[gui]
161
        fontdiff = -family \\\"Ubuntu Mono\\\" -size 11 -weight normal -slant roman -underline 0 -overstrike 0
162
""")
163
        self.assertEqual(ConfigFile({(b'gui', ): {
164
            b'fontdiff': b'-family "Ubuntu Mono" -size 11 -weight normal -slant roman -underline 0 -overstrike 0',
165
        }}), cf)
166

    
167

    
168
class ConfigDictTests(TestCase):
169

    
170
    def test_get_set(self):
171
        cd = ConfigDict()
172
        self.assertRaises(KeyError, cd.get, b"foo", b"core")
173
        cd.set((b"core", ), b"foo", b"bla")
174
        self.assertEqual(b"bla", cd.get((b"core", ), b"foo"))
175
        cd.set((b"core", ), b"foo", b"bloe")
176
        self.assertEqual(b"bloe", cd.get((b"core", ), b"foo"))
177

    
178
    def test_get_boolean(self):
179
        cd = ConfigDict()
180
        cd.set((b"core", ), b"foo", b"true")
181
        self.assertTrue(cd.get_boolean((b"core", ), b"foo"))
182
        cd.set((b"core", ), b"foo", b"false")
183
        self.assertFalse(cd.get_boolean((b"core", ), b"foo"))
184
        cd.set((b"core", ), b"foo", b"invalid")
185
        self.assertRaises(ValueError, cd.get_boolean, (b"core", ), b"foo")
186

    
187
    def test_dict(self):
188
        cd = ConfigDict()
189
        cd.set((b"core", ), b"foo", b"bla")
190
        cd.set((b"core2", ), b"foo", b"bloe")
191

    
192
        self.assertEqual([(b"core", ), (b"core2", )], list(cd.keys()))
193
        self.assertEqual(cd[(b"core", )], {b'foo': b'bla'})
194

    
195
        cd[b'a'] = b'b'
196
        self.assertEqual(cd[b'a'], b'b')
197

    
198
    def test_iteritems(self):
199
        cd = ConfigDict()
200
        cd.set((b"core", ), b"foo", b"bla")
201
        cd.set((b"core2", ), b"foo", b"bloe")
202

    
203
        self.assertEqual(
204
            [(b'foo', b'bla')],
205
            list(cd.iteritems((b"core", ))))
206

    
207
    def test_iteritems_nonexistant(self):
208
        cd = ConfigDict()
209
        cd.set((b"core2", ), b"foo", b"bloe")
210

    
211
        self.assertEqual([],
212
            list(cd.iteritems((b"core", ))))
213

    
214
    def test_itersections(self):
215
        cd = ConfigDict()
216
        cd.set((b"core2", ), b"foo", b"bloe")
217

    
218
        self.assertEqual([(b"core2", )],
219
            list(cd.itersections()))
220

    
221

    
222
class StackedConfigTests(TestCase):
223

    
224
    def test_default_backends(self):
225
        StackedConfig.default_backends()
226

    
227

    
228
class EscapeValueTests(TestCase):
229

    
230
    def test_nothing(self):
231
        self.assertEqual(b"foo", _escape_value(b"foo"))
232

    
233
    def test_backslash(self):
234
        self.assertEqual(b"foo\\\\", _escape_value(b"foo\\"))
235

    
236
    def test_newline(self):
237
        self.assertEqual(b"foo\\n", _escape_value(b"foo\n"))
238

    
239

    
240
class FormatStringTests(TestCase):
241

    
242
    def test_quoted(self):
243
        self.assertEqual(b'" foo"', _format_string(b" foo"))
244
        self.assertEqual(b'"\\tfoo"', _format_string(b"\tfoo"))
245

    
246
    def test_not_quoted(self):
247
        self.assertEqual(b'foo', _format_string(b"foo"))
248
        self.assertEqual(b'foo bar', _format_string(b"foo bar"))
249

    
250

    
251
class ParseStringTests(TestCase):
252

    
253
    def test_quoted(self):
254
        self.assertEqual(b' foo', _parse_string(b'" foo"'))
255
        self.assertEqual(b'\tfoo', _parse_string(b'"\\tfoo"'))
256

    
257
    def test_not_quoted(self):
258
        self.assertEqual(b'foo', _parse_string(b"foo"))
259
        self.assertEqual(b'foo bar', _parse_string(b"foo bar"))
260

    
261
    def test_nothing(self):
262
        self.assertEqual(b"", _parse_string(b''))
263

    
264
    def test_tab(self):
265
        self.assertEqual(b"\tbar\t", _parse_string(b"\\tbar\\t"))
266

    
267
    def test_newline(self):
268
        self.assertEqual(b"\nbar\t", _parse_string(b"\\nbar\\t\t"))
269

    
270
    def test_quote(self):
271
        self.assertEqual(b"\"foo\"", _parse_string(b"\\\"foo\\\""))
272

    
273

    
274
class CheckVariableNameTests(TestCase):
275

    
276
    def test_invalid(self):
277
        self.assertFalse(_check_variable_name(b"foo "))
278
        self.assertFalse(_check_variable_name(b"bar,bar"))
279
        self.assertFalse(_check_variable_name(b"bar.bar"))
280

    
281
    def test_valid(self):
282
        self.assertTrue(_check_variable_name(b"FOO"))
283
        self.assertTrue(_check_variable_name(b"foo"))
284
        self.assertTrue(_check_variable_name(b"foo-bar"))
285

    
286

    
287
class CheckSectionNameTests(TestCase):
288

    
289
    def test_invalid(self):
290
        self.assertFalse(_check_section_name(b"foo "))
291
        self.assertFalse(_check_section_name(b"bar,bar"))
292

    
293
    def test_valid(self):
294
        self.assertTrue(_check_section_name(b"FOO"))
295
        self.assertTrue(_check_section_name(b"foo"))
296
        self.assertTrue(_check_section_name(b"foo-bar"))
297
        self.assertTrue(_check_section_name(b"bar.bar"))
298

    
299

    
300
class SubmodulesTests(TestCase):
301

    
302
    def testSubmodules(self):
303
        cf = ConfigFile.from_file(BytesIO(b"""\
304
[submodule "core/lib"]
305
        path = core/lib
306
        url = https://github.com/phhusson/QuasselC.git
307
"""))
308
        got = list(parse_submodules(cf))
309
        self.assertEqual([
310
            (b'core/lib', b'https://github.com/phhusson/QuasselC.git', b'core/lib')], got)