##// END OF EJS Templates
Fix config loader test for Python 3.
Thomas Kluyver -
Show More
@@ -1,225 +1,225 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """
2 """
3 Tests for IPython.config.loader
3 Tests for IPython.config.loader
4
4
5 Authors:
5 Authors:
6
6
7 * Brian Granger
7 * Brian Granger
8 * Fernando Perez (design help)
8 * Fernando Perez (design help)
9 """
9 """
10
10
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12 # Copyright (C) 2008-2009 The IPython Development Team
12 # Copyright (C) 2008-2009 The IPython Development Team
13 #
13 #
14 # Distributed under the terms of the BSD License. The full license is in
14 # Distributed under the terms of the BSD License. The full license is in
15 # the file COPYING, distributed as part of this software.
15 # the file COPYING, distributed as part of this software.
16 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
17
17
18 #-----------------------------------------------------------------------------
18 #-----------------------------------------------------------------------------
19 # Imports
19 # Imports
20 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
21
21
22 import os
22 import os
23 import sys
23 import sys
24 from tempfile import mkstemp
24 from tempfile import mkstemp
25 from unittest import TestCase
25 from unittest import TestCase
26
26
27 from nose import SkipTest
27 from nose import SkipTest
28
28
29 from IPython.testing.tools import mute_warn
29 from IPython.testing.tools import mute_warn
30
30
31 from IPython.utils.traitlets import Int, Unicode
31 from IPython.utils.traitlets import Int, Unicode
32 from IPython.config.configurable import Configurable
32 from IPython.config.configurable import Configurable
33 from IPython.config.loader import (
33 from IPython.config.loader import (
34 Config,
34 Config,
35 PyFileConfigLoader,
35 PyFileConfigLoader,
36 KeyValueConfigLoader,
36 KeyValueConfigLoader,
37 ArgParseConfigLoader,
37 ArgParseConfigLoader,
38 ConfigError
38 ConfigError
39 )
39 )
40
40
41 #-----------------------------------------------------------------------------
41 #-----------------------------------------------------------------------------
42 # Actual tests
42 # Actual tests
43 #-----------------------------------------------------------------------------
43 #-----------------------------------------------------------------------------
44
44
45
45
46 pyfile = """
46 pyfile = """
47 c = get_config()
47 c = get_config()
48 c.a=10
48 c.a=10
49 c.b=20
49 c.b=20
50 c.Foo.Bar.value=10
50 c.Foo.Bar.value=10
51 c.Foo.Bam.value=range(10)
51 c.Foo.Bam.value=list(range(10)) # list() is just so it's the same on Python 3
52 c.D.C.value='hi there'
52 c.D.C.value='hi there'
53 """
53 """
54
54
55 class TestPyFileCL(TestCase):
55 class TestPyFileCL(TestCase):
56
56
57 def test_basic(self):
57 def test_basic(self):
58 fd, fname = mkstemp('.py')
58 fd, fname = mkstemp('.py')
59 f = os.fdopen(fd, 'w')
59 f = os.fdopen(fd, 'w')
60 f.write(pyfile)
60 f.write(pyfile)
61 f.close()
61 f.close()
62 # Unlink the file
62 # Unlink the file
63 cl = PyFileConfigLoader(fname)
63 cl = PyFileConfigLoader(fname)
64 config = cl.load_config()
64 config = cl.load_config()
65 self.assertEquals(config.a, 10)
65 self.assertEquals(config.a, 10)
66 self.assertEquals(config.b, 20)
66 self.assertEquals(config.b, 20)
67 self.assertEquals(config.Foo.Bar.value, 10)
67 self.assertEquals(config.Foo.Bar.value, 10)
68 self.assertEquals(config.Foo.Bam.value, range(10))
68 self.assertEquals(config.Foo.Bam.value, range(10))
69 self.assertEquals(config.D.C.value, 'hi there')
69 self.assertEquals(config.D.C.value, 'hi there')
70
70
71 class MyLoader1(ArgParseConfigLoader):
71 class MyLoader1(ArgParseConfigLoader):
72 def _add_arguments(self, aliases=None, flags=None):
72 def _add_arguments(self, aliases=None, flags=None):
73 p = self.parser
73 p = self.parser
74 p.add_argument('-f', '--foo', dest='Global.foo', type=str)
74 p.add_argument('-f', '--foo', dest='Global.foo', type=str)
75 p.add_argument('-b', dest='MyClass.bar', type=int)
75 p.add_argument('-b', dest='MyClass.bar', type=int)
76 p.add_argument('-n', dest='n', action='store_true')
76 p.add_argument('-n', dest='n', action='store_true')
77 p.add_argument('Global.bam', type=str)
77 p.add_argument('Global.bam', type=str)
78
78
79 class MyLoader2(ArgParseConfigLoader):
79 class MyLoader2(ArgParseConfigLoader):
80 def _add_arguments(self, aliases=None, flags=None):
80 def _add_arguments(self, aliases=None, flags=None):
81 subparsers = self.parser.add_subparsers(dest='subparser_name')
81 subparsers = self.parser.add_subparsers(dest='subparser_name')
82 subparser1 = subparsers.add_parser('1')
82 subparser1 = subparsers.add_parser('1')
83 subparser1.add_argument('-x',dest='Global.x')
83 subparser1.add_argument('-x',dest='Global.x')
84 subparser2 = subparsers.add_parser('2')
84 subparser2 = subparsers.add_parser('2')
85 subparser2.add_argument('y')
85 subparser2.add_argument('y')
86
86
87 class TestArgParseCL(TestCase):
87 class TestArgParseCL(TestCase):
88
88
89 def test_basic(self):
89 def test_basic(self):
90 cl = MyLoader1()
90 cl = MyLoader1()
91 config = cl.load_config('-f hi -b 10 -n wow'.split())
91 config = cl.load_config('-f hi -b 10 -n wow'.split())
92 self.assertEquals(config.Global.foo, 'hi')
92 self.assertEquals(config.Global.foo, 'hi')
93 self.assertEquals(config.MyClass.bar, 10)
93 self.assertEquals(config.MyClass.bar, 10)
94 self.assertEquals(config.n, True)
94 self.assertEquals(config.n, True)
95 self.assertEquals(config.Global.bam, 'wow')
95 self.assertEquals(config.Global.bam, 'wow')
96 config = cl.load_config(['wow'])
96 config = cl.load_config(['wow'])
97 self.assertEquals(config.keys(), ['Global'])
97 self.assertEquals(config.keys(), ['Global'])
98 self.assertEquals(config.Global.keys(), ['bam'])
98 self.assertEquals(config.Global.keys(), ['bam'])
99 self.assertEquals(config.Global.bam, 'wow')
99 self.assertEquals(config.Global.bam, 'wow')
100
100
101 def test_add_arguments(self):
101 def test_add_arguments(self):
102 cl = MyLoader2()
102 cl = MyLoader2()
103 config = cl.load_config('2 frobble'.split())
103 config = cl.load_config('2 frobble'.split())
104 self.assertEquals(config.subparser_name, '2')
104 self.assertEquals(config.subparser_name, '2')
105 self.assertEquals(config.y, 'frobble')
105 self.assertEquals(config.y, 'frobble')
106 config = cl.load_config('1 -x frobble'.split())
106 config = cl.load_config('1 -x frobble'.split())
107 self.assertEquals(config.subparser_name, '1')
107 self.assertEquals(config.subparser_name, '1')
108 self.assertEquals(config.Global.x, 'frobble')
108 self.assertEquals(config.Global.x, 'frobble')
109
109
110 def test_argv(self):
110 def test_argv(self):
111 cl = MyLoader1(argv='-f hi -b 10 -n wow'.split())
111 cl = MyLoader1(argv='-f hi -b 10 -n wow'.split())
112 config = cl.load_config()
112 config = cl.load_config()
113 self.assertEquals(config.Global.foo, 'hi')
113 self.assertEquals(config.Global.foo, 'hi')
114 self.assertEquals(config.MyClass.bar, 10)
114 self.assertEquals(config.MyClass.bar, 10)
115 self.assertEquals(config.n, True)
115 self.assertEquals(config.n, True)
116 self.assertEquals(config.Global.bam, 'wow')
116 self.assertEquals(config.Global.bam, 'wow')
117
117
118
118
119 class TestKeyValueCL(TestCase):
119 class TestKeyValueCL(TestCase):
120
120
121 def test_basic(self):
121 def test_basic(self):
122 cl = KeyValueConfigLoader()
122 cl = KeyValueConfigLoader()
123 argv = ['--'+s.strip('c.') for s in pyfile.split('\n')[2:-1]]
123 argv = ['--'+s.strip('c.') for s in pyfile.split('\n')[2:-1]]
124 with mute_warn():
124 with mute_warn():
125 config = cl.load_config(argv)
125 config = cl.load_config(argv)
126 self.assertEquals(config.a, 10)
126 self.assertEquals(config.a, 10)
127 self.assertEquals(config.b, 20)
127 self.assertEquals(config.b, 20)
128 self.assertEquals(config.Foo.Bar.value, 10)
128 self.assertEquals(config.Foo.Bar.value, 10)
129 self.assertEquals(config.Foo.Bam.value, range(10))
129 self.assertEquals(config.Foo.Bam.value, range(10))
130 self.assertEquals(config.D.C.value, 'hi there')
130 self.assertEquals(config.D.C.value, 'hi there')
131
131
132 def test_extra_args(self):
132 def test_extra_args(self):
133 cl = KeyValueConfigLoader()
133 cl = KeyValueConfigLoader()
134 with mute_warn():
134 with mute_warn():
135 config = cl.load_config(['--a=5', 'b', '--c=10', 'd'])
135 config = cl.load_config(['--a=5', 'b', '--c=10', 'd'])
136 self.assertEquals(cl.extra_args, ['b', 'd'])
136 self.assertEquals(cl.extra_args, ['b', 'd'])
137 self.assertEquals(config.a, 5)
137 self.assertEquals(config.a, 5)
138 self.assertEquals(config.c, 10)
138 self.assertEquals(config.c, 10)
139 with mute_warn():
139 with mute_warn():
140 config = cl.load_config(['--', '--a=5', '--c=10'])
140 config = cl.load_config(['--', '--a=5', '--c=10'])
141 self.assertEquals(cl.extra_args, ['--a=5', '--c=10'])
141 self.assertEquals(cl.extra_args, ['--a=5', '--c=10'])
142
142
143 def test_unicode_args(self):
143 def test_unicode_args(self):
144 cl = KeyValueConfigLoader()
144 cl = KeyValueConfigLoader()
145 argv = [u'--a=épsîlön']
145 argv = [u'--a=épsîlön']
146 with mute_warn():
146 with mute_warn():
147 config = cl.load_config(argv)
147 config = cl.load_config(argv)
148 self.assertEquals(config.a, u'épsîlön')
148 self.assertEquals(config.a, u'épsîlön')
149
149
150 def test_unicode_bytes_args(self):
150 def test_unicode_bytes_args(self):
151 uarg = u'--a=é'
151 uarg = u'--a=é'
152 try:
152 try:
153 barg = uarg.encode(sys.stdin.encoding)
153 barg = uarg.encode(sys.stdin.encoding)
154 except (TypeError, UnicodeEncodeError):
154 except (TypeError, UnicodeEncodeError):
155 raise SkipTest("sys.stdin.encoding can't handle 'é'")
155 raise SkipTest("sys.stdin.encoding can't handle 'é'")
156
156
157 cl = KeyValueConfigLoader()
157 cl = KeyValueConfigLoader()
158 with mute_warn():
158 with mute_warn():
159 config = cl.load_config([barg])
159 config = cl.load_config([barg])
160 self.assertEquals(config.a, u'é')
160 self.assertEquals(config.a, u'é')
161
161
162
162
163 class TestConfig(TestCase):
163 class TestConfig(TestCase):
164
164
165 def test_setget(self):
165 def test_setget(self):
166 c = Config()
166 c = Config()
167 c.a = 10
167 c.a = 10
168 self.assertEquals(c.a, 10)
168 self.assertEquals(c.a, 10)
169 self.assertEquals(c.has_key('b'), False)
169 self.assertEquals(c.has_key('b'), False)
170
170
171 def test_auto_section(self):
171 def test_auto_section(self):
172 c = Config()
172 c = Config()
173 self.assertEquals(c.has_key('A'), True)
173 self.assertEquals(c.has_key('A'), True)
174 self.assertEquals(c._has_section('A'), False)
174 self.assertEquals(c._has_section('A'), False)
175 A = c.A
175 A = c.A
176 A.foo = 'hi there'
176 A.foo = 'hi there'
177 self.assertEquals(c._has_section('A'), True)
177 self.assertEquals(c._has_section('A'), True)
178 self.assertEquals(c.A.foo, 'hi there')
178 self.assertEquals(c.A.foo, 'hi there')
179 del c.A
179 del c.A
180 self.assertEquals(len(c.A.keys()),0)
180 self.assertEquals(len(c.A.keys()),0)
181
181
182 def test_merge_doesnt_exist(self):
182 def test_merge_doesnt_exist(self):
183 c1 = Config()
183 c1 = Config()
184 c2 = Config()
184 c2 = Config()
185 c2.bar = 10
185 c2.bar = 10
186 c2.Foo.bar = 10
186 c2.Foo.bar = 10
187 c1._merge(c2)
187 c1._merge(c2)
188 self.assertEquals(c1.Foo.bar, 10)
188 self.assertEquals(c1.Foo.bar, 10)
189 self.assertEquals(c1.bar, 10)
189 self.assertEquals(c1.bar, 10)
190 c2.Bar.bar = 10
190 c2.Bar.bar = 10
191 c1._merge(c2)
191 c1._merge(c2)
192 self.assertEquals(c1.Bar.bar, 10)
192 self.assertEquals(c1.Bar.bar, 10)
193
193
194 def test_merge_exists(self):
194 def test_merge_exists(self):
195 c1 = Config()
195 c1 = Config()
196 c2 = Config()
196 c2 = Config()
197 c1.Foo.bar = 10
197 c1.Foo.bar = 10
198 c1.Foo.bam = 30
198 c1.Foo.bam = 30
199 c2.Foo.bar = 20
199 c2.Foo.bar = 20
200 c2.Foo.wow = 40
200 c2.Foo.wow = 40
201 c1._merge(c2)
201 c1._merge(c2)
202 self.assertEquals(c1.Foo.bam, 30)
202 self.assertEquals(c1.Foo.bam, 30)
203 self.assertEquals(c1.Foo.bar, 20)
203 self.assertEquals(c1.Foo.bar, 20)
204 self.assertEquals(c1.Foo.wow, 40)
204 self.assertEquals(c1.Foo.wow, 40)
205 c2.Foo.Bam.bam = 10
205 c2.Foo.Bam.bam = 10
206 c1._merge(c2)
206 c1._merge(c2)
207 self.assertEquals(c1.Foo.Bam.bam, 10)
207 self.assertEquals(c1.Foo.Bam.bam, 10)
208
208
209 def test_deepcopy(self):
209 def test_deepcopy(self):
210 c1 = Config()
210 c1 = Config()
211 c1.Foo.bar = 10
211 c1.Foo.bar = 10
212 c1.Foo.bam = 30
212 c1.Foo.bam = 30
213 c1.a = 'asdf'
213 c1.a = 'asdf'
214 c1.b = range(10)
214 c1.b = range(10)
215 import copy
215 import copy
216 c2 = copy.deepcopy(c1)
216 c2 = copy.deepcopy(c1)
217 self.assertEquals(c1, c2)
217 self.assertEquals(c1, c2)
218 self.assert_(c1 is not c2)
218 self.assert_(c1 is not c2)
219 self.assert_(c1.Foo is not c2.Foo)
219 self.assert_(c1.Foo is not c2.Foo)
220
220
221 def test_builtin(self):
221 def test_builtin(self):
222 c1 = Config()
222 c1 = Config()
223 exec 'foo = True' in c1
223 exec 'foo = True' in c1
224 self.assertEquals(c1.foo, True)
224 self.assertEquals(c1.foo, True)
225 self.assertRaises(ConfigError, setattr, c1, 'ValueError', 10)
225 self.assertRaises(ConfigError, setattr, c1, 'ValueError', 10)
General Comments 0
You need to be logged in to leave comments. Login now