/usr/lib/python2.7/dist-packages/traitlets/config/tests/test_application.py is in python-traitlets 4.3.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 | # coding: utf-8
"""
Tests for traitlets.config.application.Application
"""
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
import json
import logging
import os
from io import StringIO
from unittest import TestCase
try:
from unittest import mock
except ImportError:
import mock
pjoin = os.path.join
from pytest import mark
from traitlets.config.configurable import Configurable
from traitlets.config.loader import Config
from traitlets.tests.utils import check_help_output, check_help_all_output
from traitlets.config.application import (
Application
)
from ipython_genutils.tempdir import TemporaryDirectory
from traitlets.traitlets import (
Bool, Unicode, Integer, List, Dict
)
class Foo(Configurable):
i = Integer(0, help="The integer i.").tag(config=True)
j = Integer(1, help="The integer j.").tag(config=True)
name = Unicode(u'Brian', help="First name.").tag(config=True)
class Bar(Configurable):
b = Integer(0, help="The integer b.").tag(config=True)
enabled = Bool(True, help="Enable bar.").tag(config=True)
class MyApp(Application):
name = Unicode(u'myapp')
running = Bool(False, help="Is the app running?").tag(config=True)
classes = List([Bar, Foo])
config_file = Unicode(u'', help="Load this config file").tag(config=True)
warn_tpyo = Unicode(u"yes the name is wrong on purpose", config=True,
help="Should print a warning if `MyApp.warn-typo=...` command is passed")
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_datefmt = "%Y-%m-%d %H:%M"
app.log.info("hello")
assert "hello" in 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_cli_priority(self):
"""Test that loading config files does not override CLI options"""
name = 'config.py'
class TestApp(Application):
value = Unicode().tag(config=True)
config_file_loaded = Bool().tag(config=True)
aliases = {'v': 'TestApp.value'}
app = TestApp()
with TemporaryDirectory() as td:
config_file = pjoin(td, name)
with open(config_file, 'w') as f:
f.writelines([
"c.TestApp.value = 'config file'\n",
"c.TestApp.config_file_loaded = True\n"
])
app.parse_command_line(['--v=cli'])
assert 'value' in app.config.TestApp
assert app.config.TestApp.value == 'cli'
assert app.value == 'cli'
app.load_config_file(name, path=[td])
assert app.config_file_loaded
assert app.config.TestApp.value == 'cli'
assert app.value == 'cli'
def test_ipython_cli_priority(self):
# this test is almost entirely redundant with above,
# but we can keep it around in case of subtle issues creeping into
# the exact sequence IPython follows.
name = 'config.py'
class TestApp(Application):
value = Unicode().tag(config=True)
config_file_loaded = Bool().tag(config=True)
aliases = {'v': 'TestApp.value'}
app = TestApp()
with TemporaryDirectory() as td:
config_file = pjoin(td, name)
with open(config_file, 'w') as f:
f.writelines([
"c.TestApp.value = 'config file'\n",
"c.TestApp.config_file_loaded = True\n"
])
# follow IPython's config-loading sequence to ensure CLI priority is preserved
app.parse_command_line(['--v=cli'])
# this is where IPython makes a mistake:
# it assumes app.config will not be modified,
# and storing a reference is storing a copy
cli_config = app.config
assert 'value' in app.config.TestApp
assert app.config.TestApp.value == 'cli'
assert app.value == 'cli'
app.load_config_file(name, path=[td])
assert app.config_file_loaded
# enforce cl-opts override config file opts:
# this is where IPython makes a mistake: it assumes
# that cl_config is a different object, but it isn't.
app.update_config(cli_config)
assert app.config.TestApp.value == 'cli'
assert app.value == 'cli'
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_warn_autocorrect(self):
stream = StringIO()
app = MyApp(log_level=logging.INFO)
app.log.handlers = [logging.StreamHandler(stream)]
cfg = Config()
cfg.MyApp.warn_typo = "WOOOO"
app.config = cfg
self.assertIn("warn_typo", stream.getvalue())
self.assertIn("warn_tpyo", stream.getvalue())
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é'])
def test_document_config_option(self):
app = MyApp()
app.document_config_options()
def test_generate_config_file(self):
app = MyApp()
assert 'The integer b.' in app.generate_config_file()
def test_generate_config_file_classes_to_include(self):
class NoTraits(Foo, Bar):
pass
app = MyApp()
app.classes.append(NoTraits)
conf_txt = app.generate_config_file()
self.assertIn('The integer b.', conf_txt)
self.assertIn('# Bar(Configurable)', conf_txt)
self.assertIn('# Foo(Configurable)', conf_txt)
self.assertNotIn('# Configurable', conf_txt)
self.assertIn('# NoTraits(Foo,Bar)', conf_txt)
def test_multi_file(self):
app = MyApp()
app.log = logging.getLogger()
name = 'config.py'
with TemporaryDirectory('_1') as td1:
with open(pjoin(td1, name), 'w') as f1:
f1.write("get_config().MyApp.Bar.b = 1")
with TemporaryDirectory('_2') as td2:
with open(pjoin(td2, name), 'w') as f2:
f2.write("get_config().MyApp.Bar.b = 2")
app.load_config_file(name, path=[td2, td1])
app.init_bar()
self.assertEqual(app.bar.b, 2)
app.load_config_file(name, path=[td1, td2])
app.init_bar()
self.assertEqual(app.bar.b, 1)
@mark.skipif(not hasattr(TestCase, 'assertLogs'), reason='requires TestCase.assertLogs')
def test_log_collisions(self):
app = MyApp()
app.log = logging.getLogger()
app.log.setLevel(logging.INFO)
name = 'config'
with TemporaryDirectory('_1') as td:
with open(pjoin(td, name + '.py'), 'w') as f:
f.write("get_config().Bar.b = 1")
with open(pjoin(td, name + '.json'), 'w') as f:
json.dump({
'Bar': {
'b': 2
}
}, f)
with self.assertLogs(app.log, logging.WARNING) as captured:
app.load_config_file(name, path=[td])
app.init_bar()
assert app.bar.b == 2
output = '\n'.join(captured.output)
assert 'Collision' in output
assert '1 ignored, using 2' in output
assert pjoin(td, name + '.py') in output
assert pjoin(td, name + '.json') in output
@mark.skipif(not hasattr(TestCase, 'assertLogs'), reason='requires TestCase.assertLogs')
def test_log_bad_config(self):
app = MyApp()
app.log = logging.getLogger()
name = 'config.py'
with TemporaryDirectory() as td:
with open(pjoin(td, name), 'w') as f:
f.write("syntax error()")
with self.assertLogs(app.log, logging.ERROR) as captured:
app.load_config_file(name, path=[td])
output = '\n'.join(captured.output)
self.assertIn('SyntaxError', output)
def test_raise_on_bad_config(self):
app = MyApp()
app.raise_config_file_errors = True
app.log = logging.getLogger()
name = 'config.py'
with TemporaryDirectory() as td:
with open(pjoin(td, name), 'w') as f:
f.write("syntax error()")
with self.assertRaises(SyntaxError):
app.load_config_file(name, path=[td])
class DeprecatedApp(Application):
override_called = False
parent_called = False
def _config_changed(self, name, old, new):
self.override_called = True
def _capture(*args):
self.parent_called = True
with mock.patch.object(self.log, 'debug', _capture):
super(DeprecatedApp, self)._config_changed(name, old, new)
def test_deprecated_notifier():
app = DeprecatedApp()
assert not app.override_called
assert not app.parent_called
app.config = Config({'A': {'b': 'c'}})
assert app.override_called
assert app.parent_called
def test_help_output():
check_help_output(__name__)
check_help_all_output(__name__)
if __name__ == '__main__':
# for test_help_output:
MyApp.launch_instance()
|