##// END OF EJS Templates
don't instantiate IPython shell as class attr...
don't instantiate IPython shell as class attr in sphinxext.ipython_directive also fix capitalization, because reasons.

File last commit:

r12650:34fc550d
r13150:e5f05d22
Show More
test_application.py
193 lines | 6.3 KiB | text/x-python | PythonLexer
MinRK
test Application.parse_command_line with unicode argv...
r12650 # coding: utf-8
Brian Granger
Adding more documentation to config.application related files.
r3796 """
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
#-----------------------------------------------------------------------------
MinRK
promote aliases and flags, to ensure they have priority over config files...
r4977 import logging
MinRK
test that Application logging works
r12107 from io import StringIO
Brian Granger
Adding more documentation to config.application related files.
r3796 from unittest import TestCase
MinRK
test that Application logging works
r12107 import nose.tools as nt
Brian Granger
Adding more documentation to config.application related files.
r3796 from IPython.config.configurable import Configurable
MinRK
promote aliases and flags, to ensure they have priority over config files...
r4977 from IPython.config.loader import Config
Brian Granger
Adding more documentation to config.application related files.
r3796
from IPython.config.application import (
Application
)
from IPython.utils.traitlets import (
Thomas Kluyver
Remove unused imports from IPython.config
r11125 Bool, Unicode, Integer, List, Dict
Brian Granger
Adding more documentation to config.application related files.
r3796 )
#-----------------------------------------------------------------------------
# Code
#-----------------------------------------------------------------------------
class Foo(Configurable):
MinRK
add Integer traitlet...
r5344 i = Integer(0, config=True, help="The integer i.")
j = Integer(1, config=True, help="The integer j.")
MinRK
move shortname to Application...
r3852 name = Unicode(u'Brian', config=True, help="First name.")
Brian Granger
Adding more documentation to config.application related files.
r3796
class Bar(Configurable):
MinRK
add Integer traitlet...
r5344 b = Integer(0, config=True, help="The integer b.")
MinRK
move shortname to Application...
r3852 enabled = Bool(True, config=True, help="Enable bar.")
Brian Granger
Adding more documentation to config.application related files.
r3796
class MyApp(Application):
Brian Granger
Fixing bugs in BaseIPythonApplication....
r3942 name = Unicode(u'myapp')
MinRK
rename macros/shortnames to flags/aliases...
r3861 running = Bool(False, config=True,
Brian Granger
Adding more documentation to config.application related files.
r3796 help="Is the app running?")
classes = List([Bar, Foo])
MinRK
rename macros/shortnames to flags/aliases...
r3861 config_file = Unicode(u'', config=True,
Brian Granger
Adding more documentation to config.application related files.
r3796 help="Load this config file")
MinRK
aliases match flag pattern ('-' as wordsep, not '_')...
r4214 aliases = Dict({
'i' : 'Foo.i',
'j' : 'Foo.j',
'name' : 'Foo.name',
'enabled' : 'Bar.enabled',
MinRK
promote aliases and flags, to ensure they have priority over config files...
r4977 'log-level' : 'Application.log_level',
MinRK
aliases match flag pattern ('-' as wordsep, not '_')...
r4214 })
MinRK
rename macros/shortnames to flags/aliases...
r3861
flags = Dict(dict(enable=({'Bar': {'enabled' : True}}, "Set Bar.enabled to True"),
MinRK
promote aliases and flags, to ensure they have priority over config files...
r4977 disable=({'Bar': {'enabled' : False}}, "Set Bar.enabled to False"),
crit=({'Application' : {'log_level' : logging.CRITICAL}},
"set level=CRITICAL"),
))
MinRK
move shortname to Application...
r3852
Brian Granger
Adding more documentation to config.application related files.
r3796 def init_foo(self):
MinRK
use `parent=self` throughout IPython...
r11064 self.foo = Foo(parent=self)
Brian Granger
Adding more documentation to config.application related files.
r3796
def init_bar(self):
MinRK
use `parent=self` throughout IPython...
r11064 self.bar = Bar(parent=self)
Brian Granger
Adding more documentation to config.application related files.
r3796
class TestApplication(TestCase):
MinRK
test that Application logging works
r12107 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())
Brian Granger
Adding more documentation to config.application related files.
r3796 def test_basic(self):
app = MyApp()
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(app.name, u'myapp')
self.assertEqual(app.running, False)
self.assertEqual(app.classes, [MyApp,Bar,Foo])
self.assertEqual(app.config_file, u'')
Brian Granger
Adding more documentation to config.application related files.
r3796
def test_config(self):
app = MyApp()
MinRK
aliases match flag pattern ('-' as wordsep, not '_')...
r4214 app.parse_command_line(["--i=10","--Foo.j=10","--enabled=False","--log-level=50"])
Brian Granger
Adding more documentation to config.application related files.
r3796 config = app.config
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 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)
Brian Granger
Adding more documentation to config.application related files.
r3796
def test_config_propagation(self):
app = MyApp()
MinRK
aliases match flag pattern ('-' as wordsep, not '_')...
r4214 app.parse_command_line(["--i=10","--Foo.j=10","--enabled=False","--log-level=50"])
Brian Granger
Adding more documentation to config.application related files.
r3796 app.init_foo()
app.init_bar()
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(app.foo.i, 10)
self.assertEqual(app.foo.j, 10)
self.assertEqual(app.bar.enabled, False)
Brian Granger
Adding more documentation to config.application related files.
r3796
MinRK
prevent flags from clobbering entire config sections...
r3957 def test_flags(self):
MinRK
move shortname to Application...
r3852 app = MyApp()
MinRK
disallow no-prefix `ipython foo=bar` argument style....
r4197 app.parse_command_line(["--disable"])
MinRK
move shortname to Application...
r3852 app.init_bar()
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(app.bar.enabled, False)
MinRK
move shortname to Application...
r3852 app.parse_command_line(["--enable"])
app.init_bar()
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(app.bar.enabled, True)
MinRK
move shortname to Application...
r3852
MinRK
prevent flags from clobbering entire config sections...
r3957 def test_aliases(self):
app = MyApp()
MinRK
disallow no-prefix `ipython foo=bar` argument style....
r4197 app.parse_command_line(["--i=5", "--j=10"])
MinRK
prevent flags from clobbering entire config sections...
r3957 app.init_foo()
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(app.foo.i, 5)
MinRK
prevent flags from clobbering entire config sections...
r3957 app.init_foo()
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(app.foo.j, 10)
MinRK
prevent flags from clobbering entire config sections...
r3957
def test_flag_clobber(self):
"""test that setting flags doesn't clobber existing settings"""
app = MyApp()
MinRK
disallow no-prefix `ipython foo=bar` argument style....
r4197 app.parse_command_line(["--Bar.b=5", "--disable"])
MinRK
prevent flags from clobbering entire config sections...
r3957 app.init_bar()
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(app.bar.enabled, False)
self.assertEqual(app.bar.b, 5)
MinRK
disallow no-prefix `ipython foo=bar` argument style....
r4197 app.parse_command_line(["--enable", "--Bar.b=10"])
MinRK
prevent flags from clobbering entire config sections...
r3957 app.init_bar()
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(app.bar.enabled, True)
self.assertEqual(app.bar.b, 10)
MinRK
prevent flags from clobbering entire config sections...
r3957
MinRK
promote aliases and flags, to ensure they have priority over config files...
r4977 def test_flatten_flags(self):
cfg = Config()
cfg.MyApp.log_level = logging.WARN
app = MyApp()
app.update_config(cfg)
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(app.log_level, logging.WARN)
self.assertEqual(app.config.MyApp.log_level, logging.WARN)
MinRK
promote aliases and flags, to ensure they have priority over config files...
r4977 app.initialize(["--crit"])
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(app.log_level, logging.CRITICAL)
MinRK
promote aliases and flags, to ensure they have priority over config files...
r4977 # this would be app.config.Application.log_level if it failed:
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(app.config.MyApp.log_level, logging.CRITICAL)
MinRK
promote aliases and flags, to ensure they have priority over config files...
r4977
def test_flatten_aliases(self):
cfg = Config()
cfg.MyApp.log_level = logging.WARN
app = MyApp()
app.update_config(cfg)
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(app.log_level, logging.WARN)
self.assertEqual(app.config.MyApp.log_level, logging.WARN)
MinRK
promote aliases and flags, to ensure they have priority over config files...
r4977 app.initialize(["--log-level", "CRITICAL"])
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(app.log_level, logging.CRITICAL)
MinRK
promote aliases and flags, to ensure they have priority over config files...
r4977 # this would be app.config.Application.log_level if it failed:
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(app.config.MyApp.log_level, "CRITICAL")
MinRK
promote aliases and flags, to ensure they have priority over config files...
r4977
MinRK
allow extra_args in applications
r3958 def test_extra_args(self):
app = MyApp()
MinRK
disallow no-prefix `ipython foo=bar` argument style....
r4197 app.parse_command_line(["--Bar.b=5", 'extra', "--disable", 'args'])
MinRK
allow extra_args in applications
r3958 app.init_bar()
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(app.bar.enabled, False)
self.assertEqual(app.bar.b, 5)
self.assertEqual(app.extra_args, ['extra', 'args'])
MinRK
don't stop parsing on unrecognized args...
r4211 app = MyApp()
app.parse_command_line(["--Bar.b=5", '--', 'extra', "--disable", 'args'])
app.init_bar()
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(app.bar.enabled, True)
self.assertEqual(app.bar.b, 5)
self.assertEqual(app.extra_args, ['extra', '--disable', 'args'])
MinRK
allow extra_args in applications
r3958
MinRK
test Application.parse_command_line with unicode argv...
r12650 def test_unicode_argv(self):
app = MyApp()
app.parse_command_line(['ünîcødé'])
MinRK
prevent flags from clobbering entire config sections...
r3957