forked from alfred82santa/configure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
312 lines (263 loc) · 8.17 KB
/
tests.py
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
""" Tests for configure"""
import os
import re
from datetime import timedelta
from os import path
from pathlib import Path
from unittest import TestCase as BaseTestCase
from configure import Configuration, ConfigurationError, Factory
TEST_CONCAT_STRING = "base_test"
class A(object):
def __init__(self, a, b=3):
self.a = a
self.b = b
def a(a, b=4):
return A(a, b=b)
def kw(**kw):
return kw
class TestCase(BaseTestCase):
def config(self, v, ctx=None):
return Configuration.from_string(v.strip(), ctx=ctx)
def test_interpolation(self):
c = self.config("""
a: "%(a)s"
b: "%(b)s"
""", {"a": "aa", "b": "bb"})
self.assertEqual(c.a, "aa")
self.assertEqual(c.b, "bb")
def test_ref(self):
c = self.config("""
a: 1
b:
c: !ref:a
d: !ref:..a
e: !ref:.b
""")
self.assertEqual(c.a, 1)
# self.assertEqual(c.e(c), c.b)
# self.assertEqual(c.b.c(c.b), c.a)
# self.assertEqual(c.b.d(c.b), c.a)
def test_factory(self):
c = self.config("""
a1:
a: 1
a2:
a: 2
b: 4
a3:
a: 3
a4:
a: 4
b: 5
""")
o = Factory(A, c.a1)(c)
self.assertTrue(isinstance(o, A))
self.assertEqual(o.a, 1)
self.assertEqual(o.b, 3)
o = Factory(A, c.a2)(c)
self.assertTrue(isinstance(o, A))
self.assertEqual(o.a, 2)
self.assertEqual(o.b, 4)
o = Factory(a, c.a3)(c)
self.assertTrue(isinstance(o, A))
self.assertEqual(o.a, 3)
self.assertEqual(o.b, 4)
o = Factory(a, c.a4)(c)
self.assertTrue(isinstance(o, A))
self.assertEqual(o.a, 4)
self.assertEqual(o.b, 5)
def test_factory_kw(self):
c = self.config("""
a: !factory:tests.kw
a: 4
b: 5
""")
c.configure()
self.assertTrue('a' in c.a)
self.assertTrue('b' in c.a)
def test_graph(self):
c = self.config("""
a: !factory:tests.A
a: 1
b: !ref:.b
b: !factory:tests.a
a: 3
""")
c.configure()
self.assertTrue(isinstance(c.a, A))
self.assertEqual(c.a.a, 1)
self.assertTrue(isinstance(c.b, A))
self.assertEqual(c.b.a, 3)
self.assertTrue(isinstance(c.a.b, A))
self.assertEqual(c.a.b.a, c.b.a)
self.assertEqual(c.a.b.b, c.b.b)
self.assertTrue(c.a.b is c.b)
def test_obj(self):
c = self.config("""
a: !obj:tests.A
b: !obj:tests.a
""")
c.configure()
self.assertTrue(c.a is A)
self.assertTrue(c.b is a)
def test_concat(self):
os.environ['TEST_CONCAT_ENVVAR_1_1'] = 'test_1'
os.environ['TEST_CONCAT_ENVVAR_1_2'] = 'test_2'
c = self.config("""
a: !concat tests.TEST_CONCAT_STRING "/test1"
b: !concat ENV:TEST_CONCAT_ENVVAR_1_1 '/test2' "/test3/" ENV:TEST_CONCAT_ENVVAR_1_2
""")
c.configure()
self.assertEqual(c.a, "base_test/test1")
self.assertEqual(c.b, "test_1/test2/test3/test_2")
def test_concat_implicit_resolver(self):
os.environ['TEST_CONCAT_ENVVAR_2_1'] = 'test_1'
os.environ['TEST_CONCAT_ENVVAR_2_2'] = 'test_2'
c = self.config("""
a: tests.TEST_CONCAT_STRING "/test1"
b: ENV:TEST_CONCAT_ENVVAR_2_1 '/test2' "/test3/" ENV:TEST_CONCAT_ENVVAR_2_2
""")
c.configure()
self.assertEqual(c.a, "base_test/test1")
self.assertEqual(c.b, "test_1/test2/test3/test_2")
def test_load_from_file(self):
filename = path.join(path.dirname(__file__), 'examples', 'example.default.conf')
c = Configuration.from_file(filename)
c.configure()
self.assertEqual(c.a, 1)
self.assertIsInstance(c.b, timedelta)
self.assertEqual(c.b, timedelta(days=1))
def test_load_from_file_inheritance(self):
filename = path.join(path.dirname(__file__), 'examples', 'example.conf')
c = Configuration.from_file(filename)
c.configure()
self.assertEqual(c.a, 100)
self.assertIsInstance(c.b, timedelta)
self.assertEqual(c.b, timedelta(days=1))
self.assertEqual(c.c, "value")
def test_envvar(self):
os.environ['TEST_ENVVAR_1_1'] = 'test_1'
os.environ['TEST_ENVVAR_1_2'] = 'test_2'
c = self.config("""
a: !envvar TEST_ENVVAR_1_1
b: !envvar TEST_ENVVAR_1_2
""")
c.configure()
self.assertEqual(c.a, "test_1")
self.assertEqual(c.b, "test_2")
def test_envvar_implicit_resolver(self):
os.environ['TEST_ENVVAR_2_1'] = 'test_1'
os.environ['TEST_ENVVAR_2_2'] = 'test_2'
c = self.config("""
a: ENV:TEST_ENVVAR_2_1
b: ENV:TEST_ENVVAR_2_2
""")
c.configure()
self.assertEqual(c.a, "test_1")
self.assertEqual(c.b, "test_2")
def test_envvar_with_default(self):
c = self.config("""
a: !envvar TEST_ENVVAR_DEF_1?=1
b: !envvar TEST_ENVVAR_DEF_2?="test 2"
""")
c.configure()
self.assertEqual(c.a, "1")
self.assertEqual(c.b, "test 2")
def test_envvar_with_default_escaped(self):
c = self.config("""
a: !envvar TEST_ENVVAR_DEF_2?="test \\"1\\""
b: !envvar TEST_ENVVAR_DEF_2?="test\\\\2"
""")
c.configure()
self.assertEqual(c.a, 'test "1"')
self.assertEqual(c.b, "test\\2")
def test_envvar_with_default_none(self):
c = self.config("""
a: !envvar TEST_ENVVAR_DEF_NONE?=
b: !envvar TEST_ENVVAR_DEF_EMPTY?=""
""")
c.configure()
self.assertIsNone(c.a)
self.assertEqual(c.b, "")
def test_envvar_with_no_default(self):
with self.assertRaises(ConfigurationError):
self.config("""
a: !envvar TEST_ENVVAR_NO_DEF
""")
def test_envvar_implicit_resolver_with_default(self):
c = self.config("""
a: ENV:TEST_ENVVAR_IMP_RESOLVER_DEF_1?=1
b: ENV:TEST_ENVVAR_IMP_RESOLVER_DEF_2?="test_2"
""")
c.configure()
self.assertEqual(c.a, "1")
self.assertEqual(c.b, "test_2")
def test_envvar_implicit_resolver_with_no_default(self):
with self.assertRaises(ConfigurationError):
self.config("""
a: ENV:TEST_ENVVAR_NO_DEF
""")
def test_bytesize(self):
c = self.config("""
a: !bytesize 13
b: !bytesize 54k
c: !bytesize 4.5PB
""")
c.configure()
self.assertEqual(c.a, 13)
self.assertEqual(c.b, 54 * 1024)
self.assertEqual(c.c, round(4.5 * 1024 ** 5))
def test_bytesize_invalid_float(self):
with self.assertRaises(ConfigurationError):
self.config("""
a: !bytesize 13.4
""")
def test_bytesize_invalid_level(self):
with self.assertRaises(ConfigurationError):
self.config("""
a: !bytesize 13.4ub
""")
def test_regex(self):
c = self.config("""
a: !re ^\\d$
""")
c.configure()
if hasattr(re, 'Pattern'):
self.assertIsInstance(c.a, re.Pattern)
else:
self.assertTrue(hasattr(c.a, 'match'))
self.assertIsNotNone(c.a.match('5'))
self.assertIsNone(c.a.match('56'))
def test_regex_scalar_fail(self):
with self.assertRaises(ConfigurationError):
self.config("""
a: !re
""")
def test_directory(self):
base_path = Path(__file__).parent.absolute()
c = self.config("""
a: !directory /tmp
b: !directory data/test
""",
ctx={'pwd': str(base_path)})
c.configure()
self.assertEqual(c.a, '/tmp')
b_path = Path(__file__).parent.absolute() / 'data' / 'test'
self.assertEqual(c.b, str(b_path))
self.assertTrue(b_path.exists())
self.assertTrue(b_path.is_dir())
b_path.rmdir()
def test_directory_is_file_fail(self):
base_path = Path(__file__).parent.absolute()
with self.assertRaises(ConfigurationError):
self.config("""
a: !directory tests.py
""",
ctx={'pwd': str(base_path)})
def test_directory_none_fail(self):
base_path = Path(__file__).parent.absolute()
with self.assertRaises(ConfigurationError):
self.config("""
a: !directory
""",
ctx={'pwd': str(base_path)})