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