summaryrefslogtreecommitdiff
path: root/tests/test_configobj.py
blob: 319949dd6f66d76efa5a26fe9467afb1e29660b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
from codecs import BOM_UTF8
import os
import sys
from configobj import ConfigObj, flatten_errors
from validate import Validator, VdtValueTooSmallError

import pytest
import six

try:
    #TODO(robdennis): determine if this is really 2.6 and newer
    # Python 2.6 only
    from warnings import catch_warnings
except ImportError:
    # this will cause an error, but at least the other tests
    # will run on Python 2.5
    catch_warnings = None


def test_order_preserved():
    c = ConfigObj()
    c['a'] = 1
    c['b'] = 2
    c['c'] = 3
    c['section'] = {}
    c['section']['a'] = 1
    c['section']['b'] = 2
    c['section']['c'] = 3
    c['section']['section'] = {}
    c['section']['section2'] = {}
    c['section']['section3'] = {}
    c['section2'] = {}
    c['section3'] = {}

    c2 = ConfigObj(c)
    assert c2.scalars == ['a', 'b', 'c']
    assert c2.sections == ['section', 'section2', 'section3']
    assert c2['section'].scalars == ['a', 'b', 'c']
    assert c2['section'].sections == ['section', 'section2', 'section3']

    assert c['section'] is not c2['section']
    assert c['section']['section'] is not c2['section']['section']


@pytest.mark.skipif(catch_warnings is None,
                    reason='catch_warnings is required')
def test_options_deprecation():
    with catch_warnings(record=True) as log:
        ConfigObj(options={})

    # unpack the only member of log
    warning, = log
    assert warning.category == DeprecationWarning


def test_list_members():
    c = ConfigObj()
    c['a'] = []
    c['a'].append('foo')
    assert c['a'] == ['foo']


def test_list_interpolation_with_pop():
    c = ConfigObj()
    c['a'] = []
    c['a'].append('%(b)s')
    c['b'] = 'bar'
    assert c.pop('a') == ['bar']


def test_with_default():
    c = ConfigObj()
    c['a'] = 3

    assert c.pop('a') == 3
    assert c.pop('b', 3) == 3
    with pytest.raises(KeyError):
        c.pop('c')


def test_interpolation_with_section_names():
    cfg = """
item1 = 1234
[section]
    [[item1]]
    foo='bar'
    [[DEFAULT]]
        [[[item1]]]
        why = would you do this?
    [[other-subsection]]
    item2 = '$item1'""".splitlines()
    c = ConfigObj(cfg, interpolation='Template')

    # This raises an exception in 4.7.1 and earlier due to the section
    # being found as the interpolation value
    repr(c)


def test_interoplation_repr():
    c = ConfigObj(['foo = $bar'], interpolation='Template')
    c['baz'] = {}
    c['baz']['spam'] = '%(bar)s'

    # This raises a MissingInterpolationOption exception in 4.7.1 and earlier
    repr(c)


#issue #18
def test_unicode_conversion_when_encoding_is_set():
    cfg = """
test = some string
    """.splitlines()

    c = ConfigObj(cfg, encoding='utf8')

    if six.PY2:
        assert not isinstance(c['test'], str)
        assert isinstance(c['test'], unicode)
    else:
        assert isinstance(c['test'], str)


#issue #18
def test_no_unicode_conversion_when_encoding_is_omitted():
    cfg = """
test = some string
    """.splitlines()

    c = ConfigObj(cfg)
    if six.PY2:
        assert isinstance(c['test'], str)
        assert not isinstance(c['test'], unicode)
    else:
        assert isinstance(c['test'], str)


@pytest.fixture
def testconfig1():
    """
    copied from the main doctest
    """
    return """\
    key1= val    # comment 1
    key2= val    # comment 2
    # comment 3
    [lev1a]     # comment 4
    key1= val    # comment 5
    key2= val    # comment 6
    # comment 7
    [lev1b]    # comment 8
    key1= val    # comment 9
    key2= val    # comment 10
    # comment 11
        [[lev2ba]]    # comment 12
        key1= val    # comment 13
        # comment 14
        [[lev2bb]]    # comment 15
        key1= val    # comment 16
    # comment 17
    [lev1c]    # comment 18
    # comment 19
        [[lev2c]]    # comment 20
        # comment 21
            [[[lev3c]]]    # comment 22
            key1 = val    # comment 23"""


@pytest.fixture
def testconfig2():
    return """\
        key1 = 'val1'
        key2 =   "val2"
        key3 = val3
        ["section 1"] # comment
        keys11 = val1
        keys12 = val2
        keys13 = val3
        [section 2]
        keys21 = val1
        keys22 = val2
        keys23 = val3

            [['section 2 sub 1']]
            fish = 3
    """

@pytest.fixture
def a(testconfig1):
    """
    also copied from main doc tests
    """
    return ConfigObj(testconfig1.split('\n'), raise_errors=True)


@pytest.fixture
def b(testconfig2):
    """
    also copied from main doc tests
    """
    return ConfigObj(testconfig2.split('\n'), raise_errors=True)


def test_configobj_dict_representation(a, b):

    assert a.depth == 0
    assert a == {
        'key2': 'val',
        'key1': 'val',
        'lev1c': {
            'lev2c': {
                'lev3c': {
                    'key1': 'val',
                    },
                },
            },
        'lev1b': {
            'key2': 'val',
            'key1': 'val',
            'lev2ba': {
                'key1': 'val',
                },
            'lev2bb': {
                'key1': 'val',
                },
            },
        'lev1a': {
            'key2': 'val',
            'key1': 'val',
            },
        }
    
    assert b.depth == 0
    assert b == {
        'key3': 'val3',
        'key2': 'val2',
        'key1': 'val1',
        'section 1': {
            'keys11': 'val1',
            'keys13': 'val3',
            'keys12': 'val2',
            },
        'section 2': {
            'section 2 sub 1': {
                'fish': '3',
                },
            'keys21': 'val1',
            'keys22': 'val2',
            'keys23': 'val3',
            },
        }

    t = '''
        'a' = b # !"$%^&*(),::;'@~#= 33
        "b" = b #= 6, 33
''' .split('\n')
    t2 = ConfigObj(t)
    assert t2 == {'a': 'b', 'b': 'b'}
    t2.inline_comments['b'] = ''
    del t2['a']
    assert t2.write() == ['','b = b', '']


def test_behavior_when_list_values_is_false():
    c = '''
       key1 = no quotes
       key2 = 'single quotes'
       key3 = "double quotes"
       key4 = "list", 'with', several, "quotes"
       '''
    cfg = ConfigObj(c.splitlines(), list_values=False)
    assert cfg == {
        'key1': 'no quotes',
        'key2': "'single quotes'",
        'key3': '"double quotes"',
        'key4': '"list", \'with\', several, "quotes"'
    }

    cfg2 = ConfigObj(list_values=False)
    cfg2['key1'] = 'Multiline\nValue'
    cfg2['key2'] = '''"Value" with 'quotes' !'''
    assert cfg2.write() == [
        "key1 = '''Multiline\nValue'''",
        'key2 = "Value" with \'quotes\' !'
    ]

    cfg2.list_values = True
    assert cfg2.write() == [
        "key1 = '''Multiline\nValue'''",
        'key2 = \'\'\'"Value" with \'quotes\' !\'\'\''
    ]


def test_flatten_errors():
    config = '''
       test1=40
       test2=hello
       test3=3
       test4=5.0
       [section]
           test1=40
           test2=hello
           test3=3
           test4=5.0
           [[sub section]]
               test1=40
               test2=hello
               test3=3
               test4=5.0
    '''.split('\n')
    configspec = '''
       test1= integer(30,50)
       test2= string
       test3=integer
       test4=float(6.0)
       [section]
           test1=integer(30,50)
           test2=string
           test3=integer
           test4=float(6.0)
           [[sub section]]
               test1=integer(30,50)
               test2=string
               test3=integer
               test4=float(6.0)
       '''.split('\n')
    val = Validator()
    c1 = ConfigObj(config, configspec=configspec)
    res = c1.validate(val)
    assert flatten_errors(c1, res) == [([], 'test4', False), (['section'], 'test4', False), (['section', 'sub section'], 'test4', False)]
    res = c1.validate(val, preserve_errors=True)
    check = flatten_errors(c1, res)
    assert check[0][:2] == ([], 'test4')
    assert check[1][:2] == (['section'], 'test4')
    assert check[2][:2] == (['section', 'sub section'], 'test4')
    for entry in check:
        assert isinstance(entry[2], VdtValueTooSmallError)
        assert str(entry[2]) == 'the value "5.0" is too small.'


def test_unicode_handling():
    u_base = '''
    # initial comment
       # inital comment 2
    test1 = some value
    # comment
    test2 = another value    # inline comment
    # section comment
    [section]    # inline comment
       test = test    # another inline comment
       test2 = test2
    # final comment
    # final comment2
    '''

    u = u_base.encode('utf_8').splitlines(True)
    u[0] = BOM_UTF8 + u[0]
    uc = ConfigObj(u)
    uc.encoding = None
    assert uc.BOM
    assert uc == {'test1': 'some value', 'test2': 'another value',
                  'section': {'test': 'test', 'test2': 'test2'}}
    uc = ConfigObj(u, encoding='utf_8', default_encoding='latin-1')
    assert uc.BOM
    assert isinstance(uc['test1'], six.text_type)
    assert uc.encoding == 'utf_8'
    assert uc.newlines == '\n'
    assert len(uc.write()) == 13
    uc['latin1'] = "This costs lot's of "
    a_list = uc.write()
    assert 'latin1' in str(a_list)
    assert len(a_list) == 14
    assert isinstance(a_list[0], six.binary_type)
    assert a_list[0].startswith(BOM_UTF8)

    u = u_base.replace('\n', '\r\n').encode('utf-8').splitlines(True)
    uc = ConfigObj(u)
    assert uc.newlines == '\r\n'
    uc.newlines = '\r'
    file_like = six.StringIO()
    uc.write(file_like)
    file_like.seek(0)
    uc2 = ConfigObj(file_like)
    assert uc2 == uc
    assert uc2.filename == None
    assert uc2.newlines == '\r'


def _doctest():
    """
    Dummy function to hold some of the doctests.

    Test flatten_errors:

    Test unicode handling, BOM, write witha file like object and line endings :



    Test validate in copy mode
    a = '''
    # Initial Comment
    ...
    key1 = string(default=Hello)
    ...
    # section comment
    [section] # inline comment
    # key1 comment
    key1 = integer(default=6)
    # key2 comment
    key2 = boolean(default=True)
    ...
       # subsection comment
       [[sub-section]] # inline comment
       # another key1 comment
       key1 = float(default=3.0)'''.splitlines()
    b = ConfigObj(configspec=a)
    b.validate(val, copy=True)
    1
    b.write() == ['',
    '# Initial Comment',
    '',
    'key1 = Hello',
    '',
    '# section comment',
    '[section]    # inline comment',
    '    # key1 comment',
    '    key1 = 6',
    '    # key2 comment',
    '    key2 = True',
    '    ',
    '    # subsection comment',
    '    [[sub-section]]    # inline comment',
    '        # another key1 comment',
    '        key1 = 3.0']
    1

    Test Writing Empty Values
    a = '''
       key1 =
       key2 =# a comment'''
    b = ConfigObj(a.splitlines())
    b.write()
    ['', 'key1 = ""', 'key2 = ""    # a comment']
    b.write_empty_values = True
    b.write()
    ['', 'key1 = ', 'key2 =     # a comment']

    Test unrepr when reading
    a = '''
       key1 = (1, 2, 3)    # comment
       key2 = True
       key3 = 'a string'
       key4 = [1, 2, 3, 'a mixed list']
    '''.splitlines()
    b = ConfigObj(a, unrepr=True)
    b == {'key1': (1, 2, 3),
    'key2': True,
    'key3': 'a string',
    'key4': [1, 2, 3, 'a mixed list']}
    1

    Test unrepr when writing
    c = ConfigObj(b.write(), unrepr=True)
    c == b
    1

    Test unrepr with multiline values
    a = '''k = \"""{
    'k1': 3,
    'k2': 6.0}\"""
    '''.splitlines()
    c = ConfigObj(a, unrepr=True)
    c == {'k': {'k1': 3, 'k2': 6.0}}
    1

    Test unrepr with a dictionary
    a = 'k = {"a": 1}'.splitlines()
    c = ConfigObj(a, unrepr=True)
    isinstance(c['k'], dict)
    True

    a = ConfigObj()
    a['a'] = 'fish'
    a.as_bool('a')
    Traceback (most recent call last):
    ValueError: Value "fish" is neither True nor False
    a['b'] = 'True'
    a.as_bool('b')
    1
    a['b'] = 'off'
    a.as_bool('b')
    0

    a = ConfigObj()
    a['a'] = 'fish'
    try:
       a.as_int('a')
    except ValueError as e:
       err_mess = str(e)
    err_mess.startswith('invalid literal for int()')
    1
    a['b'] = '1'
    a.as_int('b')
    1
    a['b'] = '3.2'
    try:
       a.as_int('b')
    except ValueError as e:
       err_mess = str(e)
    err_mess.startswith('invalid literal for int()')
    1

    a = ConfigObj()
    a['a'] = 'fish'
    a.as_float('a')
    Traceback (most recent call last):
    ValueError: invalid literal for float(): fish
    a['b'] = '1'
    a.as_float('b')
    1.0
    a['b'] = '3.2'
    a.as_float('b')
    3.2...

     Test # with unrepr
     a = '''
        key1 = (1, 2, 3)    # comment
        key2 = True
        key3 = 'a string'
        key4 = [1, 2, 3, 'a mixed list#']
     '''.splitlines()
     b = ConfigObj(a, unrepr=True)
     b == {'key1': (1, 2, 3),
     'key2': True,
     'key3': 'a string',
     'key4': [1, 2, 3, 'a mixed list#']}
     1
    """

    # Comments are no longer parsed from values in configspecs
    # so the following test fails and is disabled
    untested = """
    Test validate in copy mode
    a = '''
    # Initial Comment
    ...
    key1 = string(default=Hello)    # comment 1
    ...
    # section comment
    [section] # inline comment
    # key1 comment
    key1 = integer(default=6) # an integer value
    # key2 comment
    key2 = boolean(default=True) # a boolean
    ...
       # subsection comment
       [[sub-section]] # inline comment
       # another key1 comment
       key1 = float(default=3.0) # a float'''.splitlines()
    b = ConfigObj(configspec=a)
    b.validate(val, copy=True)
    1
    b.write()
    b.write() == ['',
    '# Initial Comment',
    '',
    'key1 = Hello    # comment 1',
    '',
    '# section comment',
    '[section]    # inline comment',
    '    # key1 comment',
    '    key1 = 6    # an integer value',
    '    # key2 comment',
    '    key2 = True    # a boolean',
    '    ',
    '    # subsection comment',
    '    [[sub-section]]    # inline comment',
    '        # another key1 comment',
    '        key1 = 3.0    # a float']
    1
    """