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