Show More
@@ -1,297 +1,327 b'' | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | """ |
|
3 | 3 | Tests for IPython.config.loader |
|
4 | 4 | |
|
5 | 5 | Authors: |
|
6 | 6 | |
|
7 | 7 | * Brian Granger |
|
8 | 8 | * Fernando Perez (design help) |
|
9 | 9 | """ |
|
10 | 10 | |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | # Copyright (C) 2008 The IPython Development Team |
|
13 | 13 | # |
|
14 | 14 | # Distributed under the terms of the BSD License. The full license is in |
|
15 | 15 | # the file COPYING, distributed as part of this software. |
|
16 | 16 | #----------------------------------------------------------------------------- |
|
17 | 17 | |
|
18 | 18 | #----------------------------------------------------------------------------- |
|
19 | 19 | # Imports |
|
20 | 20 | #----------------------------------------------------------------------------- |
|
21 | 21 | |
|
22 | 22 | import os |
|
23 | 23 | import pickle |
|
24 | 24 | import sys |
|
25 | 25 | from tempfile import mkstemp |
|
26 | 26 | from unittest import TestCase |
|
27 | 27 | |
|
28 | 28 | from nose import SkipTest |
|
29 | 29 | |
|
30 | 30 | from IPython.testing.tools import mute_warn |
|
31 | 31 | |
|
32 | 32 | from IPython.config.loader import ( |
|
33 | 33 | Config, |
|
34 | LazyConfigValue, | |
|
34 | 35 | PyFileConfigLoader, |
|
35 | 36 | KeyValueConfigLoader, |
|
36 | 37 | ArgParseConfigLoader, |
|
37 | 38 | KVArgParseConfigLoader, |
|
38 | ConfigError | |
|
39 | ConfigError, | |
|
39 | 40 | ) |
|
40 | 41 | |
|
41 | 42 | #----------------------------------------------------------------------------- |
|
42 | 43 | # Actual tests |
|
43 | 44 | #----------------------------------------------------------------------------- |
|
44 | 45 | |
|
45 | 46 | |
|
46 | 47 | pyfile = """ |
|
47 | 48 | c = get_config() |
|
48 | 49 | c.a=10 |
|
49 | 50 | c.b=20 |
|
50 | 51 | c.Foo.Bar.value=10 |
|
51 | 52 | c.Foo.Bam.value=list(range(10)) # list() is just so it's the same on Python 3 |
|
52 | 53 | c.D.C.value='hi there' |
|
53 | 54 | """ |
|
54 | 55 | |
|
55 | 56 | class TestPyFileCL(TestCase): |
|
56 | 57 | |
|
57 | 58 | def test_basic(self): |
|
58 | 59 | fd, fname = mkstemp('.py') |
|
59 | 60 | f = os.fdopen(fd, 'w') |
|
60 | 61 | f.write(pyfile) |
|
61 | 62 | f.close() |
|
62 | 63 | # Unlink the file |
|
63 | 64 | cl = PyFileConfigLoader(fname) |
|
64 | 65 | config = cl.load_config() |
|
65 | 66 | self.assertEqual(config.a, 10) |
|
66 | 67 | self.assertEqual(config.b, 20) |
|
67 | 68 | self.assertEqual(config.Foo.Bar.value, 10) |
|
68 | 69 | self.assertEqual(config.Foo.Bam.value, list(range(10))) |
|
69 | 70 | self.assertEqual(config.D.C.value, 'hi there') |
|
70 | 71 | |
|
71 | 72 | class MyLoader1(ArgParseConfigLoader): |
|
72 | 73 | def _add_arguments(self, aliases=None, flags=None): |
|
73 | 74 | p = self.parser |
|
74 | 75 | p.add_argument('-f', '--foo', dest='Global.foo', type=str) |
|
75 | 76 | p.add_argument('-b', dest='MyClass.bar', type=int) |
|
76 | 77 | p.add_argument('-n', dest='n', action='store_true') |
|
77 | 78 | p.add_argument('Global.bam', type=str) |
|
78 | 79 | |
|
79 | 80 | class MyLoader2(ArgParseConfigLoader): |
|
80 | 81 | def _add_arguments(self, aliases=None, flags=None): |
|
81 | 82 | subparsers = self.parser.add_subparsers(dest='subparser_name') |
|
82 | 83 | subparser1 = subparsers.add_parser('1') |
|
83 | 84 | subparser1.add_argument('-x',dest='Global.x') |
|
84 | 85 | subparser2 = subparsers.add_parser('2') |
|
85 | 86 | subparser2.add_argument('y') |
|
86 | 87 | |
|
87 | 88 | class TestArgParseCL(TestCase): |
|
88 | 89 | |
|
89 | 90 | def test_basic(self): |
|
90 | 91 | cl = MyLoader1() |
|
91 | 92 | config = cl.load_config('-f hi -b 10 -n wow'.split()) |
|
92 | 93 | self.assertEqual(config.Global.foo, 'hi') |
|
93 | 94 | self.assertEqual(config.MyClass.bar, 10) |
|
94 | 95 | self.assertEqual(config.n, True) |
|
95 | 96 | self.assertEqual(config.Global.bam, 'wow') |
|
96 | 97 | config = cl.load_config(['wow']) |
|
97 | 98 | self.assertEqual(list(config.keys()), ['Global']) |
|
98 | 99 | self.assertEqual(list(config.Global.keys()), ['bam']) |
|
99 | 100 | self.assertEqual(config.Global.bam, 'wow') |
|
100 | 101 | |
|
101 | 102 | def test_add_arguments(self): |
|
102 | 103 | cl = MyLoader2() |
|
103 | 104 | config = cl.load_config('2 frobble'.split()) |
|
104 | 105 | self.assertEqual(config.subparser_name, '2') |
|
105 | 106 | self.assertEqual(config.y, 'frobble') |
|
106 | 107 | config = cl.load_config('1 -x frobble'.split()) |
|
107 | 108 | self.assertEqual(config.subparser_name, '1') |
|
108 | 109 | self.assertEqual(config.Global.x, 'frobble') |
|
109 | 110 | |
|
110 | 111 | def test_argv(self): |
|
111 | 112 | cl = MyLoader1(argv='-f hi -b 10 -n wow'.split()) |
|
112 | 113 | config = cl.load_config() |
|
113 | 114 | self.assertEqual(config.Global.foo, 'hi') |
|
114 | 115 | self.assertEqual(config.MyClass.bar, 10) |
|
115 | 116 | self.assertEqual(config.n, True) |
|
116 | 117 | self.assertEqual(config.Global.bam, 'wow') |
|
117 | 118 | |
|
118 | 119 | |
|
119 | 120 | class TestKeyValueCL(TestCase): |
|
120 | 121 | klass = KeyValueConfigLoader |
|
121 | 122 | |
|
122 | 123 | def test_basic(self): |
|
123 | 124 | cl = self.klass() |
|
124 | 125 | argv = ['--'+s.strip('c.') for s in pyfile.split('\n')[2:-1]] |
|
125 | 126 | with mute_warn(): |
|
126 | 127 | config = cl.load_config(argv) |
|
127 | 128 | self.assertEqual(config.a, 10) |
|
128 | 129 | self.assertEqual(config.b, 20) |
|
129 | 130 | self.assertEqual(config.Foo.Bar.value, 10) |
|
130 | 131 | self.assertEqual(config.Foo.Bam.value, list(range(10))) |
|
131 | 132 | self.assertEqual(config.D.C.value, 'hi there') |
|
132 | 133 | |
|
133 | 134 | def test_expanduser(self): |
|
134 | 135 | cl = self.klass() |
|
135 | 136 | argv = ['--a=~/1/2/3', '--b=~', '--c=~/', '--d="~/"'] |
|
136 | 137 | with mute_warn(): |
|
137 | 138 | config = cl.load_config(argv) |
|
138 | 139 | self.assertEqual(config.a, os.path.expanduser('~/1/2/3')) |
|
139 | 140 | self.assertEqual(config.b, os.path.expanduser('~')) |
|
140 | 141 | self.assertEqual(config.c, os.path.expanduser('~/')) |
|
141 | 142 | self.assertEqual(config.d, '~/') |
|
142 | 143 | |
|
143 | 144 | def test_extra_args(self): |
|
144 | 145 | cl = self.klass() |
|
145 | 146 | with mute_warn(): |
|
146 | 147 | config = cl.load_config(['--a=5', 'b', '--c=10', 'd']) |
|
147 | 148 | self.assertEqual(cl.extra_args, ['b', 'd']) |
|
148 | 149 | self.assertEqual(config.a, 5) |
|
149 | 150 | self.assertEqual(config.c, 10) |
|
150 | 151 | with mute_warn(): |
|
151 | 152 | config = cl.load_config(['--', '--a=5', '--c=10']) |
|
152 | 153 | self.assertEqual(cl.extra_args, ['--a=5', '--c=10']) |
|
153 | 154 | |
|
154 | 155 | def test_unicode_args(self): |
|
155 | 156 | cl = self.klass() |
|
156 | 157 | argv = [u'--a=épsîlön'] |
|
157 | 158 | with mute_warn(): |
|
158 | 159 | config = cl.load_config(argv) |
|
159 | 160 | self.assertEqual(config.a, u'épsîlön') |
|
160 | 161 | |
|
161 | 162 | def test_unicode_bytes_args(self): |
|
162 | 163 | uarg = u'--a=é' |
|
163 | 164 | try: |
|
164 | 165 | barg = uarg.encode(sys.stdin.encoding) |
|
165 | 166 | except (TypeError, UnicodeEncodeError): |
|
166 | 167 | raise SkipTest("sys.stdin.encoding can't handle 'é'") |
|
167 | 168 | |
|
168 | 169 | cl = self.klass() |
|
169 | 170 | with mute_warn(): |
|
170 | 171 | config = cl.load_config([barg]) |
|
171 | 172 | self.assertEqual(config.a, u'é') |
|
172 | 173 | |
|
173 | 174 | def test_unicode_alias(self): |
|
174 | 175 | cl = self.klass() |
|
175 | 176 | argv = [u'--a=épsîlön'] |
|
176 | 177 | with mute_warn(): |
|
177 | 178 | config = cl.load_config(argv, aliases=dict(a='A.a')) |
|
178 | 179 | self.assertEqual(config.A.a, u'épsîlön') |
|
179 | 180 | |
|
180 | 181 | |
|
181 | 182 | class TestArgParseKVCL(TestKeyValueCL): |
|
182 | 183 | klass = KVArgParseConfigLoader |
|
183 | 184 | |
|
184 | 185 | def test_expanduser2(self): |
|
185 | 186 | cl = self.klass() |
|
186 | 187 | argv = ['-a', '~/1/2/3', '--b', "'~/1/2/3'"] |
|
187 | 188 | with mute_warn(): |
|
188 | 189 | config = cl.load_config(argv, aliases=dict(a='A.a', b='A.b')) |
|
189 | 190 | self.assertEqual(config.A.a, os.path.expanduser('~/1/2/3')) |
|
190 | 191 | self.assertEqual(config.A.b, '~/1/2/3') |
|
191 | 192 | |
|
192 | 193 | def test_eval(self): |
|
193 | 194 | cl = self.klass() |
|
194 | 195 | argv = ['-c', 'a=5'] |
|
195 | 196 | with mute_warn(): |
|
196 | 197 | config = cl.load_config(argv, aliases=dict(c='A.c')) |
|
197 | 198 | self.assertEqual(config.A.c, u"a=5") |
|
198 | 199 | |
|
199 | 200 | |
|
200 | 201 | class TestConfig(TestCase): |
|
201 | 202 | |
|
202 | 203 | def test_setget(self): |
|
203 | 204 | c = Config() |
|
204 | 205 | c.a = 10 |
|
205 | 206 | self.assertEqual(c.a, 10) |
|
206 | 207 | self.assertEqual('b' in c, False) |
|
207 | 208 | |
|
208 | 209 | def test_auto_section(self): |
|
209 | 210 | c = Config() |
|
210 |
self.assert |
|
|
211 |
|
|
|
211 | self.assertNotIn('A', c) | |
|
212 | assert not c._has_section('A') | |
|
212 | 213 | A = c.A |
|
213 | 214 | A.foo = 'hi there' |
|
214 |
self.assert |
|
|
215 | self.assertIn('A', c) | |
|
216 | assert c._has_section('A') | |
|
215 | 217 | self.assertEqual(c.A.foo, 'hi there') |
|
216 | 218 | del c.A |
|
217 |
self.assertEqual( |
|
|
219 | self.assertEqual(c.A, Config()) | |
|
218 | 220 | |
|
219 | 221 | def test_merge_doesnt_exist(self): |
|
220 | 222 | c1 = Config() |
|
221 | 223 | c2 = Config() |
|
222 | 224 | c2.bar = 10 |
|
223 | 225 | c2.Foo.bar = 10 |
|
224 | 226 | c1.merge(c2) |
|
225 | 227 | self.assertEqual(c1.Foo.bar, 10) |
|
226 | 228 | self.assertEqual(c1.bar, 10) |
|
227 | 229 | c2.Bar.bar = 10 |
|
228 | 230 | c1.merge(c2) |
|
229 | 231 | self.assertEqual(c1.Bar.bar, 10) |
|
230 | 232 | |
|
231 | 233 | def test_merge_exists(self): |
|
232 | 234 | c1 = Config() |
|
233 | 235 | c2 = Config() |
|
234 | 236 | c1.Foo.bar = 10 |
|
235 | 237 | c1.Foo.bam = 30 |
|
236 | 238 | c2.Foo.bar = 20 |
|
237 | 239 | c2.Foo.wow = 40 |
|
238 | 240 | c1.merge(c2) |
|
239 | 241 | self.assertEqual(c1.Foo.bam, 30) |
|
240 | 242 | self.assertEqual(c1.Foo.bar, 20) |
|
241 | 243 | self.assertEqual(c1.Foo.wow, 40) |
|
242 | 244 | c2.Foo.Bam.bam = 10 |
|
243 | 245 | c1.merge(c2) |
|
244 | 246 | self.assertEqual(c1.Foo.Bam.bam, 10) |
|
245 | 247 | |
|
246 | 248 | def test_deepcopy(self): |
|
247 | 249 | c1 = Config() |
|
248 | 250 | c1.Foo.bar = 10 |
|
249 | 251 | c1.Foo.bam = 30 |
|
250 | 252 | c1.a = 'asdf' |
|
251 | 253 | c1.b = range(10) |
|
252 | 254 | import copy |
|
253 | 255 | c2 = copy.deepcopy(c1) |
|
254 | 256 | self.assertEqual(c1, c2) |
|
255 | 257 | self.assertTrue(c1 is not c2) |
|
256 | 258 | self.assertTrue(c1.Foo is not c2.Foo) |
|
257 | 259 | |
|
258 | 260 | def test_builtin(self): |
|
259 | 261 | c1 = Config() |
|
260 | 262 | c1.format = "json" |
|
261 | 263 | |
|
262 | 264 | def test_fromdict(self): |
|
263 | 265 | c1 = Config({'Foo' : {'bar' : 1}}) |
|
264 | 266 | self.assertEqual(c1.Foo.__class__, Config) |
|
265 | 267 | self.assertEqual(c1.Foo.bar, 1) |
|
266 | 268 | |
|
267 | 269 | def test_fromdictmerge(self): |
|
268 | 270 | c1 = Config() |
|
269 | 271 | c2 = Config({'Foo' : {'bar' : 1}}) |
|
270 | 272 | c1.merge(c2) |
|
271 | 273 | self.assertEqual(c1.Foo.__class__, Config) |
|
272 | 274 | self.assertEqual(c1.Foo.bar, 1) |
|
273 | 275 | |
|
274 | 276 | def test_fromdictmerge2(self): |
|
275 | 277 | c1 = Config({'Foo' : {'baz' : 2}}) |
|
276 | 278 | c2 = Config({'Foo' : {'bar' : 1}}) |
|
277 | 279 | c1.merge(c2) |
|
278 | 280 | self.assertEqual(c1.Foo.__class__, Config) |
|
279 | 281 | self.assertEqual(c1.Foo.bar, 1) |
|
280 | 282 | self.assertEqual(c1.Foo.baz, 2) |
|
281 | 283 | self.assertNotIn('baz', c2.Foo) |
|
282 | 284 | |
|
283 | 285 | def test_contains(self): |
|
284 | 286 | c1 = Config({'Foo' : {'baz' : 2}}) |
|
285 | 287 | c2 = Config({'Foo' : {'bar' : 1}}) |
|
286 | 288 | self.assertIn('Foo', c1) |
|
287 | 289 | self.assertIn('Foo.baz', c1) |
|
288 | 290 | self.assertIn('Foo.bar', c2) |
|
289 | 291 | self.assertNotIn('Foo.bar', c1) |
|
290 | 292 | |
|
291 | 293 | def test_pickle_config(self): |
|
292 | 294 | cfg = Config() |
|
293 | 295 | cfg.Foo.bar = 1 |
|
294 | 296 | pcfg = pickle.dumps(cfg) |
|
295 | 297 | cfg2 = pickle.loads(pcfg) |
|
296 | 298 | self.assertEqual(cfg2, cfg) |
|
299 | ||
|
300 | def test_getattr_section(self): | |
|
301 | cfg = Config() | |
|
302 | self.assertNotIn('Foo', cfg) | |
|
303 | Foo = cfg.Foo | |
|
304 | assert isinstance(Foo, Config) | |
|
305 | self.assertIn('Foo', cfg) | |
|
306 | ||
|
307 | def test_getitem_section(self): | |
|
308 | cfg = Config() | |
|
309 | self.assertNotIn('Foo', cfg) | |
|
310 | Foo = cfg['Foo'] | |
|
311 | assert isinstance(Foo, Config) | |
|
312 | self.assertIn('Foo', cfg) | |
|
313 | ||
|
314 | def test_getattr_not_section(self): | |
|
315 | cfg = Config() | |
|
316 | self.assertNotIn('foo', cfg) | |
|
317 | foo = cfg.foo | |
|
318 | assert isinstance(foo, LazyConfigValue) | |
|
319 | self.assertIn('foo', cfg) | |
|
320 | ||
|
321 | def test_getitem_not_section(self): | |
|
322 | cfg = Config() | |
|
323 | self.assertNotIn('foo', cfg) | |
|
324 | foo = cfg['foo'] | |
|
325 | assert isinstance(foo, LazyConfigValue) | |
|
326 | self.assertIn('foo', cfg) | |
|
297 | 327 |
General Comments 0
You need to be logged in to leave comments.
Login now