mirror of
https://github.com/sprockets/sprockets.cli.git
synced 2024-09-28 10:10:59 +00:00
Work in progress update
- Add stub classes for testing - Change how testing will work - Make the logger used in logging configuration based upon a class level constant/attribute
This commit is contained in:
parent
46c07bd968
commit
783f914f08
6 changed files with 72 additions and 37 deletions
|
@ -110,6 +110,7 @@ class CLI(object):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
CONTROLLERS = 'sprockets.controller'
|
CONTROLLERS = 'sprockets.controller'
|
||||||
|
LOGGER = 'sprockets'
|
||||||
PLUGINS = 'sprockets.plugin'
|
PLUGINS = 'sprockets.plugin'
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
@ -251,8 +252,7 @@ class CLI(object):
|
||||||
metavar='APP',
|
metavar='APP',
|
||||||
help='Application to run')
|
help='Application to run')
|
||||||
|
|
||||||
@staticmethod
|
def _configure_logging(self, application, verbosity=0, syslog=False):
|
||||||
def _configure_logging(application, verbosity=0, syslog=False):
|
|
||||||
"""Configure logging for the application, setting the appropriate
|
"""Configure logging for the application, setting the appropriate
|
||||||
verbosity and adding syslog if it's enabled.
|
verbosity and adding syslog if it's enabled.
|
||||||
|
|
||||||
|
@ -266,13 +266,13 @@ class CLI(object):
|
||||||
|
|
||||||
# Increase the logging verbosity
|
# Increase the logging verbosity
|
||||||
if verbosity == 1:
|
if verbosity == 1:
|
||||||
config['loggers']['sprockets']['level'] = logging.INFO
|
config['loggers'][self.LOGGER]['level'] = logging.INFO
|
||||||
elif verbosity == 2:
|
elif verbosity == 2:
|
||||||
config['loggers']['sprockets']['level'] = logging.DEBUG
|
config['loggers'][self.LOGGER]['level'] = logging.DEBUG
|
||||||
|
|
||||||
# Add syslog if it's enabled
|
# Add syslog if it's enabled
|
||||||
if syslog:
|
if syslog:
|
||||||
config['loggers']['sprockets']['handlers'].append('syslog')
|
config['loggers'][self.LOGGER]['handlers'].append('syslog')
|
||||||
|
|
||||||
# Copy the sprockets logger to the application
|
# Copy the sprockets logger to the application
|
||||||
config['loggers'][application] = dict(config['loggers']['sprockets'])
|
config['loggers'][application] = dict(config['loggers']['sprockets'])
|
||||||
|
|
1
sstubs/__init__.py
Normal file
1
sstubs/__init__.py
Normal file
|
@ -0,0 +1 @@
|
||||||
|
__author__ = 'gavinr'
|
8
sstubs/app.py
Normal file
8
sstubs/app.py
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
"""
|
||||||
|
Stub Controller for testing
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def run():
|
||||||
|
pass
|
12
sstubs/controller.py
Normal file
12
sstubs/controller.py
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
"""
|
||||||
|
Stub Controller for testing
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def add_cli_arguments(parser):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def main(app, args):
|
||||||
|
pass
|
21
sstubs/plugin.py
Normal file
21
sstubs/plugin.py
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
"""
|
||||||
|
Stub Plugin Module
|
||||||
|
|
||||||
|
"""
|
||||||
|
PRIORITY = 50
|
||||||
|
|
||||||
|
|
||||||
|
def add_cli_arguments(parser):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def initialize(controller):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def on_start(controller):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def on_shutdown(controller):
|
||||||
|
pass
|
57
tests.py
57
tests.py
|
@ -12,46 +12,34 @@ import mock
|
||||||
from sprockets import cli
|
from sprockets import cli
|
||||||
|
|
||||||
|
|
||||||
|
class Package(object):
|
||||||
|
|
||||||
|
def __init__(self, name, module_name):
|
||||||
|
self.name = name
|
||||||
|
self.module_name = module_name
|
||||||
|
|
||||||
|
|
||||||
class InitializationTests(unittest.TestCase):
|
class InitializationTests(unittest.TestCase):
|
||||||
|
|
||||||
@mock.patch('argparse.ArgumentParser.parse_args')
|
@mock.patch('argparse.ArgumentParser.parse_args')
|
||||||
@mock.patch('pkg_resources.iter_entry_points')
|
@mock.patch('pkg_resources.iter_entry_points')
|
||||||
@mock.patch('importlib.import_module')
|
def setUp(self, iter_entry_points, parse_args):
|
||||||
def setUp(self, import_module, iter_entry_points, parse_args):
|
|
||||||
self.import_module = import_module
|
|
||||||
self.iter_entry_points = iter_entry_points
|
self.iter_entry_points = iter_entry_points
|
||||||
self.parse_args = parse_args
|
self.parse_args = parse_args
|
||||||
|
|
||||||
self.app_points = [mock.Mock(name='test_app',
|
self.app_points = [Package('tapp', 'sstubs.app')]
|
||||||
module_name='mock_app')]
|
self.ctrl_points = [Package('tcontroller', 'sstubs.controller')]
|
||||||
self.ctrl_points = [mock.Mock(name='test_http',
|
self.plugin_points = [Package('tplugin', 'sstubs.plugin')]
|
||||||
module_name='mock_http')]
|
|
||||||
self.plugin_points = [mock.Mock(name='test_plugin',
|
|
||||||
module_name='mock_plugin')]
|
|
||||||
|
|
||||||
self.mock_app = mock.Mock()
|
|
||||||
self.mock_controller = mock.Mock()
|
|
||||||
self.mock_controller.add_cli_arguments = self.add_cli_arguments = \
|
|
||||||
mock.Mock()
|
|
||||||
self.mock_plugin = mock.Mock()
|
|
||||||
|
|
||||||
def entry_point_side_effect(*args, **kwargs):
|
def entry_point_side_effect(*args, **kwargs):
|
||||||
if kwargs.get('group') == 'sprockets.controller':
|
if kwargs.get('group') == 'sprockets.controller':
|
||||||
return self.ctrl_points
|
return iter(self.ctrl_points)
|
||||||
elif kwargs.get('group') == 'sprockets.plugin':
|
elif kwargs.get('group') == 'sprockets.plugin':
|
||||||
return self.plugin_points
|
return iter(self.plugin_points)
|
||||||
elif kwargs.get('group') == 'sprockets.test_http.app':
|
elif kwargs.get('group') == 'sprockets.test_http.app':
|
||||||
return self.app_points
|
return iter(self.app_points)
|
||||||
self.iter_entry_points.side_effect = entry_point_side_effect
|
|
||||||
|
|
||||||
def import_module_side_effect(*args, **kwargs):
|
self.iter_entry_points.side_effect = entry_point_side_effect
|
||||||
if args[0] == 'mock_app':
|
|
||||||
return self.mock_app
|
|
||||||
elif args[0] == 'mock_http':
|
|
||||||
return self.mock_controller
|
|
||||||
elif args[0] == 'mock_plugin':
|
|
||||||
return self.mock_plugin
|
|
||||||
self.import_module.side_effect = import_module_side_effect
|
|
||||||
self.obj = cli.CLI()
|
self.obj = cli.CLI()
|
||||||
|
|
||||||
def test_pkg_resources_iterated(self):
|
def test_pkg_resources_iterated(self):
|
||||||
|
@ -59,9 +47,14 @@ class InitializationTests(unittest.TestCase):
|
||||||
mock.call(group='sprockets.plugin')]
|
mock.call(group='sprockets.plugin')]
|
||||||
self.iter_entry_points.assert_has_calls(calls)
|
self.iter_entry_points.assert_has_calls(calls)
|
||||||
|
|
||||||
def test_controller_packages_imported(self):
|
def test_controller_imported(self):
|
||||||
self.import_module.assert_has_calls([mock.call('mock_http'),
|
for attr in ['add_cli_arguments', 'main']:
|
||||||
mock.call('mock_plugin')])
|
self.assertTrue(hasattr(self.obj._controllers.get('tcontroller'),
|
||||||
|
attr))
|
||||||
|
|
||||||
|
def test_plugin_is_imported(self):
|
||||||
|
for attr in ['initialize', 'on_start', 'on_shutdown']:
|
||||||
|
self.assertTrue(hasattr(self.obj._plugins.get('tplugin'), attr))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#def test_controller_argparse_method_invoked(self):
|
|
||||||
# self.add_cli_arguments.assert_called_once_with(self.obj.arg_parser)
|
|
||||||
|
|
Loading…
Reference in a new issue