Show More
@@ -1,175 +1,188 b'' | |||||
1 | """ |
|
1 | """ | |
2 | Tests for IPython.config.application.Application |
|
2 | Tests for IPython.config.application.Application | |
3 |
|
3 | |||
4 | Authors: |
|
4 | Authors: | |
5 |
|
5 | |||
6 | * Brian Granger |
|
6 | * Brian Granger | |
7 | """ |
|
7 | """ | |
8 |
|
8 | |||
9 | #----------------------------------------------------------------------------- |
|
9 | #----------------------------------------------------------------------------- | |
10 | # Copyright (C) 2008-2011 The IPython Development Team |
|
10 | # Copyright (C) 2008-2011 The IPython Development Team | |
11 | # |
|
11 | # | |
12 | # Distributed under the terms of the BSD License. The full license is in |
|
12 | # Distributed under the terms of the BSD License. The full license is in | |
13 | # the file COPYING, distributed as part of this software. |
|
13 | # the file COPYING, distributed as part of this software. | |
14 | #----------------------------------------------------------------------------- |
|
14 | #----------------------------------------------------------------------------- | |
15 |
|
15 | |||
16 | #----------------------------------------------------------------------------- |
|
16 | #----------------------------------------------------------------------------- | |
17 | # Imports |
|
17 | # Imports | |
18 | #----------------------------------------------------------------------------- |
|
18 | #----------------------------------------------------------------------------- | |
19 |
|
19 | |||
20 | import logging |
|
20 | import logging | |
|
21 | from io import StringIO | |||
21 | from unittest import TestCase |
|
22 | from unittest import TestCase | |
22 |
|
23 | |||
|
24 | import nose.tools as nt | |||
|
25 | ||||
23 | from IPython.config.configurable import Configurable |
|
26 | from IPython.config.configurable import Configurable | |
24 | from IPython.config.loader import Config |
|
27 | from IPython.config.loader import Config | |
25 |
|
28 | |||
26 | from IPython.config.application import ( |
|
29 | from IPython.config.application import ( | |
27 | Application |
|
30 | Application | |
28 | ) |
|
31 | ) | |
29 |
|
32 | |||
30 | from IPython.utils.traitlets import ( |
|
33 | from IPython.utils.traitlets import ( | |
31 | Bool, Unicode, Integer, List, Dict |
|
34 | Bool, Unicode, Integer, List, Dict | |
32 | ) |
|
35 | ) | |
33 |
|
36 | |||
34 | #----------------------------------------------------------------------------- |
|
37 | #----------------------------------------------------------------------------- | |
35 | # Code |
|
38 | # Code | |
36 | #----------------------------------------------------------------------------- |
|
39 | #----------------------------------------------------------------------------- | |
37 |
|
40 | |||
38 | class Foo(Configurable): |
|
41 | class Foo(Configurable): | |
39 |
|
42 | |||
40 | i = Integer(0, config=True, help="The integer i.") |
|
43 | i = Integer(0, config=True, help="The integer i.") | |
41 | j = Integer(1, config=True, help="The integer j.") |
|
44 | j = Integer(1, config=True, help="The integer j.") | |
42 | name = Unicode(u'Brian', config=True, help="First name.") |
|
45 | name = Unicode(u'Brian', config=True, help="First name.") | |
43 |
|
46 | |||
44 |
|
47 | |||
45 | class Bar(Configurable): |
|
48 | class Bar(Configurable): | |
46 |
|
49 | |||
47 | b = Integer(0, config=True, help="The integer b.") |
|
50 | b = Integer(0, config=True, help="The integer b.") | |
48 | enabled = Bool(True, config=True, help="Enable bar.") |
|
51 | enabled = Bool(True, config=True, help="Enable bar.") | |
49 |
|
52 | |||
50 |
|
53 | |||
51 | class MyApp(Application): |
|
54 | class MyApp(Application): | |
52 |
|
55 | |||
53 | name = Unicode(u'myapp') |
|
56 | name = Unicode(u'myapp') | |
54 | running = Bool(False, config=True, |
|
57 | running = Bool(False, config=True, | |
55 | help="Is the app running?") |
|
58 | help="Is the app running?") | |
56 | classes = List([Bar, Foo]) |
|
59 | classes = List([Bar, Foo]) | |
57 | config_file = Unicode(u'', config=True, |
|
60 | config_file = Unicode(u'', config=True, | |
58 | help="Load this config file") |
|
61 | help="Load this config file") | |
59 |
|
62 | |||
60 | aliases = Dict({ |
|
63 | aliases = Dict({ | |
61 | 'i' : 'Foo.i', |
|
64 | 'i' : 'Foo.i', | |
62 | 'j' : 'Foo.j', |
|
65 | 'j' : 'Foo.j', | |
63 | 'name' : 'Foo.name', |
|
66 | 'name' : 'Foo.name', | |
64 | 'enabled' : 'Bar.enabled', |
|
67 | 'enabled' : 'Bar.enabled', | |
65 | 'log-level' : 'Application.log_level', |
|
68 | 'log-level' : 'Application.log_level', | |
66 | }) |
|
69 | }) | |
67 |
|
70 | |||
68 | flags = Dict(dict(enable=({'Bar': {'enabled' : True}}, "Set Bar.enabled to True"), |
|
71 | flags = Dict(dict(enable=({'Bar': {'enabled' : True}}, "Set Bar.enabled to True"), | |
69 | disable=({'Bar': {'enabled' : False}}, "Set Bar.enabled to False"), |
|
72 | disable=({'Bar': {'enabled' : False}}, "Set Bar.enabled to False"), | |
70 | crit=({'Application' : {'log_level' : logging.CRITICAL}}, |
|
73 | crit=({'Application' : {'log_level' : logging.CRITICAL}}, | |
71 | "set level=CRITICAL"), |
|
74 | "set level=CRITICAL"), | |
72 | )) |
|
75 | )) | |
73 |
|
76 | |||
74 | def init_foo(self): |
|
77 | def init_foo(self): | |
75 | self.foo = Foo(parent=self) |
|
78 | self.foo = Foo(parent=self) | |
76 |
|
79 | |||
77 | def init_bar(self): |
|
80 | def init_bar(self): | |
78 | self.bar = Bar(parent=self) |
|
81 | self.bar = Bar(parent=self) | |
79 |
|
82 | |||
80 |
|
83 | |||
81 | class TestApplication(TestCase): |
|
84 | class TestApplication(TestCase): | |
82 |
|
85 | |||
|
86 | def test_log(self): | |||
|
87 | stream = StringIO() | |||
|
88 | app = MyApp(log_level=logging.INFO) | |||
|
89 | handler = logging.StreamHandler(stream) | |||
|
90 | # trigger reconstruction of the log formatter | |||
|
91 | app.log.handlers = [handler] | |||
|
92 | app.log_format = "%(message)s" | |||
|
93 | app.log.info("hello") | |||
|
94 | nt.assert_in("hello", stream.getvalue()) | |||
|
95 | ||||
83 | def test_basic(self): |
|
96 | def test_basic(self): | |
84 | app = MyApp() |
|
97 | app = MyApp() | |
85 | self.assertEqual(app.name, u'myapp') |
|
98 | self.assertEqual(app.name, u'myapp') | |
86 | self.assertEqual(app.running, False) |
|
99 | self.assertEqual(app.running, False) | |
87 | self.assertEqual(app.classes, [MyApp,Bar,Foo]) |
|
100 | self.assertEqual(app.classes, [MyApp,Bar,Foo]) | |
88 | self.assertEqual(app.config_file, u'') |
|
101 | self.assertEqual(app.config_file, u'') | |
89 |
|
102 | |||
90 | def test_config(self): |
|
103 | def test_config(self): | |
91 | app = MyApp() |
|
104 | app = MyApp() | |
92 | app.parse_command_line(["--i=10","--Foo.j=10","--enabled=False","--log-level=50"]) |
|
105 | app.parse_command_line(["--i=10","--Foo.j=10","--enabled=False","--log-level=50"]) | |
93 | config = app.config |
|
106 | config = app.config | |
94 | self.assertEqual(config.Foo.i, 10) |
|
107 | self.assertEqual(config.Foo.i, 10) | |
95 | self.assertEqual(config.Foo.j, 10) |
|
108 | self.assertEqual(config.Foo.j, 10) | |
96 | self.assertEqual(config.Bar.enabled, False) |
|
109 | self.assertEqual(config.Bar.enabled, False) | |
97 | self.assertEqual(config.MyApp.log_level,50) |
|
110 | self.assertEqual(config.MyApp.log_level,50) | |
98 |
|
111 | |||
99 | def test_config_propagation(self): |
|
112 | def test_config_propagation(self): | |
100 | app = MyApp() |
|
113 | app = MyApp() | |
101 | app.parse_command_line(["--i=10","--Foo.j=10","--enabled=False","--log-level=50"]) |
|
114 | app.parse_command_line(["--i=10","--Foo.j=10","--enabled=False","--log-level=50"]) | |
102 | app.init_foo() |
|
115 | app.init_foo() | |
103 | app.init_bar() |
|
116 | app.init_bar() | |
104 | self.assertEqual(app.foo.i, 10) |
|
117 | self.assertEqual(app.foo.i, 10) | |
105 | self.assertEqual(app.foo.j, 10) |
|
118 | self.assertEqual(app.foo.j, 10) | |
106 | self.assertEqual(app.bar.enabled, False) |
|
119 | self.assertEqual(app.bar.enabled, False) | |
107 |
|
120 | |||
108 | def test_flags(self): |
|
121 | def test_flags(self): | |
109 | app = MyApp() |
|
122 | app = MyApp() | |
110 | app.parse_command_line(["--disable"]) |
|
123 | app.parse_command_line(["--disable"]) | |
111 | app.init_bar() |
|
124 | app.init_bar() | |
112 | self.assertEqual(app.bar.enabled, False) |
|
125 | self.assertEqual(app.bar.enabled, False) | |
113 | app.parse_command_line(["--enable"]) |
|
126 | app.parse_command_line(["--enable"]) | |
114 | app.init_bar() |
|
127 | app.init_bar() | |
115 | self.assertEqual(app.bar.enabled, True) |
|
128 | self.assertEqual(app.bar.enabled, True) | |
116 |
|
129 | |||
117 | def test_aliases(self): |
|
130 | def test_aliases(self): | |
118 | app = MyApp() |
|
131 | app = MyApp() | |
119 | app.parse_command_line(["--i=5", "--j=10"]) |
|
132 | app.parse_command_line(["--i=5", "--j=10"]) | |
120 | app.init_foo() |
|
133 | app.init_foo() | |
121 | self.assertEqual(app.foo.i, 5) |
|
134 | self.assertEqual(app.foo.i, 5) | |
122 | app.init_foo() |
|
135 | app.init_foo() | |
123 | self.assertEqual(app.foo.j, 10) |
|
136 | self.assertEqual(app.foo.j, 10) | |
124 |
|
137 | |||
125 | def test_flag_clobber(self): |
|
138 | def test_flag_clobber(self): | |
126 | """test that setting flags doesn't clobber existing settings""" |
|
139 | """test that setting flags doesn't clobber existing settings""" | |
127 | app = MyApp() |
|
140 | app = MyApp() | |
128 | app.parse_command_line(["--Bar.b=5", "--disable"]) |
|
141 | app.parse_command_line(["--Bar.b=5", "--disable"]) | |
129 | app.init_bar() |
|
142 | app.init_bar() | |
130 | self.assertEqual(app.bar.enabled, False) |
|
143 | self.assertEqual(app.bar.enabled, False) | |
131 | self.assertEqual(app.bar.b, 5) |
|
144 | self.assertEqual(app.bar.b, 5) | |
132 | app.parse_command_line(["--enable", "--Bar.b=10"]) |
|
145 | app.parse_command_line(["--enable", "--Bar.b=10"]) | |
133 | app.init_bar() |
|
146 | app.init_bar() | |
134 | self.assertEqual(app.bar.enabled, True) |
|
147 | self.assertEqual(app.bar.enabled, True) | |
135 | self.assertEqual(app.bar.b, 10) |
|
148 | self.assertEqual(app.bar.b, 10) | |
136 |
|
149 | |||
137 | def test_flatten_flags(self): |
|
150 | def test_flatten_flags(self): | |
138 | cfg = Config() |
|
151 | cfg = Config() | |
139 | cfg.MyApp.log_level = logging.WARN |
|
152 | cfg.MyApp.log_level = logging.WARN | |
140 | app = MyApp() |
|
153 | app = MyApp() | |
141 | app.update_config(cfg) |
|
154 | app.update_config(cfg) | |
142 | self.assertEqual(app.log_level, logging.WARN) |
|
155 | self.assertEqual(app.log_level, logging.WARN) | |
143 | self.assertEqual(app.config.MyApp.log_level, logging.WARN) |
|
156 | self.assertEqual(app.config.MyApp.log_level, logging.WARN) | |
144 | app.initialize(["--crit"]) |
|
157 | app.initialize(["--crit"]) | |
145 | self.assertEqual(app.log_level, logging.CRITICAL) |
|
158 | self.assertEqual(app.log_level, logging.CRITICAL) | |
146 | # this would be app.config.Application.log_level if it failed: |
|
159 | # this would be app.config.Application.log_level if it failed: | |
147 | self.assertEqual(app.config.MyApp.log_level, logging.CRITICAL) |
|
160 | self.assertEqual(app.config.MyApp.log_level, logging.CRITICAL) | |
148 |
|
161 | |||
149 | def test_flatten_aliases(self): |
|
162 | def test_flatten_aliases(self): | |
150 | cfg = Config() |
|
163 | cfg = Config() | |
151 | cfg.MyApp.log_level = logging.WARN |
|
164 | cfg.MyApp.log_level = logging.WARN | |
152 | app = MyApp() |
|
165 | app = MyApp() | |
153 | app.update_config(cfg) |
|
166 | app.update_config(cfg) | |
154 | self.assertEqual(app.log_level, logging.WARN) |
|
167 | self.assertEqual(app.log_level, logging.WARN) | |
155 | self.assertEqual(app.config.MyApp.log_level, logging.WARN) |
|
168 | self.assertEqual(app.config.MyApp.log_level, logging.WARN) | |
156 | app.initialize(["--log-level", "CRITICAL"]) |
|
169 | app.initialize(["--log-level", "CRITICAL"]) | |
157 | self.assertEqual(app.log_level, logging.CRITICAL) |
|
170 | self.assertEqual(app.log_level, logging.CRITICAL) | |
158 | # this would be app.config.Application.log_level if it failed: |
|
171 | # this would be app.config.Application.log_level if it failed: | |
159 | self.assertEqual(app.config.MyApp.log_level, "CRITICAL") |
|
172 | self.assertEqual(app.config.MyApp.log_level, "CRITICAL") | |
160 |
|
173 | |||
161 | def test_extra_args(self): |
|
174 | def test_extra_args(self): | |
162 | app = MyApp() |
|
175 | app = MyApp() | |
163 | app.parse_command_line(["--Bar.b=5", 'extra', "--disable", 'args']) |
|
176 | app.parse_command_line(["--Bar.b=5", 'extra', "--disable", 'args']) | |
164 | app.init_bar() |
|
177 | app.init_bar() | |
165 | self.assertEqual(app.bar.enabled, False) |
|
178 | self.assertEqual(app.bar.enabled, False) | |
166 | self.assertEqual(app.bar.b, 5) |
|
179 | self.assertEqual(app.bar.b, 5) | |
167 | self.assertEqual(app.extra_args, ['extra', 'args']) |
|
180 | self.assertEqual(app.extra_args, ['extra', 'args']) | |
168 | app = MyApp() |
|
181 | app = MyApp() | |
169 | app.parse_command_line(["--Bar.b=5", '--', 'extra', "--disable", 'args']) |
|
182 | app.parse_command_line(["--Bar.b=5", '--', 'extra', "--disable", 'args']) | |
170 | app.init_bar() |
|
183 | app.init_bar() | |
171 | self.assertEqual(app.bar.enabled, True) |
|
184 | self.assertEqual(app.bar.enabled, True) | |
172 | self.assertEqual(app.bar.b, 5) |
|
185 | self.assertEqual(app.bar.b, 5) | |
173 | self.assertEqual(app.extra_args, ['extra', '--disable', 'args']) |
|
186 | self.assertEqual(app.extra_args, ['extra', '--disable', 'args']) | |
174 |
|
187 | |||
175 |
|
188 |
General Comments 0
You need to be logged in to leave comments.
Login now