File: test_file_sources.py

package info (click to toggle)
python-setoptconf 0.3.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 252 kB
  • sloc: python: 1,427; sh: 7; makefile: 5
file content (260 lines) | stat: -rw-r--r-- 5,856 bytes parent folder | download | duplicates (5)
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
import atexit
import os.path
import tempfile

import setoptconf as soc


_TEMPFILES = []

def make_temp(content):
    name = tempfile.mkstemp()[1]
    fp = open(name, 'w')
    fp.write(content)
    fp.close()
    _TEMPFILES.append(name)
    return name

def cleanup_temps():
    for temp in _TEMPFILES:
        if os.path.exists(temp):
            os.remove(temp)

atexit.register(cleanup_temps)


def make_settings():
    settings = []
    setting = soc.StringSetting('foo')
    settings.append(setting)
    setting = soc.IntegerSetting('bar')
    settings.append(setting)
    setting = soc.BooleanSetting('baz', default=False)
    settings.append(setting)
    setting = soc.ListSetting('happy', soc.String)
    settings.append(setting)
    return settings


def test_directory_modifier():
    file1 = make_temp("""
[mytest]
foo = hello
bar = 123
""")

    source = soc.ConfigFileSource(
        [file1, soc.HomeDirectory('foobar')],
        section='mytest'
    )
    config = source.get_config(make_settings())

    assert config.foo == 'hello'
    assert config.bar == 123
    assert config.baz is False
    assert config.happy is None


def test_configfile():
    file1 = make_temp('')

    source = soc.ConfigFileSource(file1, section='mytest')
    config = source.get_config(make_settings())

    assert config.foo is None
    assert config.bar is None
    assert config.baz is False
    assert config.happy is None

    file2 = make_temp("""
[mytest]
foo = hello
bar = 123
""")

    source = soc.ConfigFileSource(file2, section='mytest')
    config = source.get_config(make_settings())

    assert config.foo == 'hello'
    assert config.bar == 123
    assert config.baz is False
    assert config.happy is None

    file3 = make_temp("""
[mytest]
foo = hello
bar: 123
baz = true
happy:foo,"bar"
""")

    source = soc.ConfigFileSource(file3, section='mytest')
    config = source.get_config(make_settings())

    assert config.foo == 'hello'
    assert config.bar == 123
    assert config.baz is True
    assert config.happy == ['foo', 'bar']

    file4 = make_temp("""
[mytest]
foo = goodbye
""")

    source = soc.ConfigFileSource([file3,file4], section='mytest')
    config = source.get_config(make_settings())

    assert config.foo == 'hello'
    assert config.bar == 123
    assert config.baz is True
    assert config.happy == ['foo', 'bar']

    source = soc.ConfigFileSource([file4,file3], section='mytest', combine=True)
    config = source.get_config(make_settings())

    assert config.foo == 'goodbye'
    assert config.bar == 123
    assert config.baz is True
    assert config.happy == ['foo', 'bar']

    try:
        source = soc.ConfigFileSource(123, section='mytest')
    except TypeError:
        pass
    else:
        assert False, 'Expected TypeError for bogus file name'

    try:
        source = soc.ConfigFileSource(['foobar', 123], section='mytest')
    except TypeError:
        pass
    else:
        assert False, 'Expected TypeError for bogus file name'


def test_jsonfile():
    file1 = make_temp('')

    source = soc.JsonFileSource(file1)
    config = source.get_config(make_settings())

    assert config.foo is None
    assert config.bar is None
    assert config.baz is False
    assert config.happy is None

    file2 = make_temp("""
{
    "foo": "hello",
    "bar": 123
}
""")

    source = soc.JsonFileSource(file2)
    config = source.get_config(make_settings())

    assert config.foo == 'hello'
    assert config.bar == 123
    assert config.baz is False
    assert config.happy is None

    file3 = make_temp("""
{
    "foo": "hello",
    "bar": 123,
    "baz": true,
    "happy": ["foo", "bar"]
}
""")

    source = soc.JsonFileSource(file3)
    config = source.get_config(make_settings())

    assert config.foo == 'hello'
    assert config.bar == 123
    assert config.baz is True
    assert config.happy == ['foo', 'bar']

    file4 = make_temp('{}')

    source = soc.JsonFileSource(file4)
    config = source.get_config(make_settings())

    assert config.foo is None
    assert config.bar is None
    assert config.baz is False
    assert config.happy is None

    file5 = make_temp('"foo"')

    source = soc.JsonFileSource(file5)
    try:
        config = source.get_config(make_settings())
    except TypeError:
        pass
    else:
        assert False, 'Expected TypeError for non-objects'


if hasattr(soc, 'YamlFileSource'):
    def test_yamlfile():
        file1 = make_temp('')

        source = soc.YamlFileSource(file1)
        config = source.get_config(make_settings())

        assert config.foo is None
        assert config.bar is None
        assert config.baz is False
        assert config.happy is None

        file2 = make_temp("""
foo: hello
bar: 123
""")

        source = soc.YamlFileSource(file2)
        config = source.get_config(make_settings())

        assert config.foo == 'hello'
        assert config.bar == 123
        assert config.baz is False
        assert config.happy is None

        file3 = make_temp("""
foo: hello
bar: 123
baz: true
happy:
  - foo
  - bar
""")

        source = soc.YamlFileSource(file3)
        config = source.get_config(make_settings())

        assert config.foo == 'hello'
        assert config.bar == 123
        assert config.baz is True
        assert config.happy == ['foo', 'bar']

        file4 = make_temp('{}')

        source = soc.YamlFileSource(file4)
        config = source.get_config(make_settings())

        assert config.foo is None
        assert config.bar is None
        assert config.baz is False
        assert config.happy is None

        file5 = make_temp('"foo"')

        source = soc.YamlFileSource(file5)
        try:
            config = source.get_config(make_settings())
        except TypeError:
            pass
        else:
            assert False, 'Expected TypeError for non-objects'