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