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