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