def test_create_parser(): from sampleproject.toolkit.app import BaseApplication app = BaseApplication() parser = app.create_parser(add_help=False) assert "temboard" == parser.prog parser.add_argument("--help", action="help")
def test_application_specs(): from sampleproject.toolkit.app import BaseApplication app = BaseApplication() assert 'temboard_plugins' in app.config_specs app = BaseApplication(with_plugins=None) assert 'temboard_plugins' not in app.config_specs
def test_fetch_missing(mocker): iter_ep = mocker.patch( 'sampleproject.toolkit.app.pkg_resources.iter_entry_points') from sampleproject.toolkit.app import BaseApplication, UserError app = BaseApplication() iter_ep.return_value = [] with pytest.raises(UserError): app.fetch_plugin('myplugin')
def test_apply_config_bad_logging(mocker): mod = 'sampleproject.toolkit.app.' sl = mocker.patch(mod + 'setup_logging', autospec=True) from sampleproject.toolkit.app import BaseApplication, UserError app = BaseApplication() app.config.logging = dict() sl.side_effect = Exception('pouet') with pytest.raises(UserError): app.apply_config()
def test_reload(mocker): m = 'sampleproject.toolkit.app.BaseApplication' mocker.patch(m + '.read_file', autospec=True) mocker.patch(m + '.read_dir', autospec=True) mocker.patch(m + '.apply_config', autospec=True) from sampleproject.toolkit.app import BaseApplication app = BaseApplication() app.config = mocker.Mock(name='config') app.config.temboard.configfile = 'pouet.conf' app.reload()
def test_fetch_failing(mocker): iter_ep = mocker.patch( 'sampleproject.toolkit.app.pkg_resources.iter_entry_points') from sampleproject.toolkit.app import BaseApplication, UserError app = BaseApplication() ep = mocker.Mock(name='ep') ep.load.side_effect = Exception('Pouet') iter_ep.return_value = [ep] with pytest.raises(UserError): app.fetch_plugin('myplugin')
def test_fetch_plugin(mocker): iter_ep = mocker.patch( 'sampleproject.toolkit.app.pkg_resources.iter_entry_points') from sampleproject.toolkit.app import BaseApplication app = BaseApplication() ep = mocker.Mock(name='found') ep.name = 'found' ep.load.return_value = 'PLUGIN OBJECT' iter_ep.return_value = [ep] assert 'PLUGIN OBJECT' == app.fetch_plugin(['found'])
def test_create_plugins(mocker): mod = 'sampleproject.toolkit.app' mocker.patch(mod + '.refresh_distributions') mocker.patch(mod + '.refresh_pythonpath') mocker.patch(mod + '.BaseApplication.fetch_plugin', autospec=True) from sampleproject.toolkit.app import BaseApplication app = BaseApplication() app.config = mocker.Mock(name='config') app.config.temboard.plugins = ['ng'] app.create_plugins() assert 'ng' in app.plugins
def test_bootstrap(mocker): mod = 'sampleproject.toolkit.app' mocker.patch(mod + '.BaseApplication.read_file', autospec=True) mocker.patch(mod + '.BaseApplication.read_dir', autospec=True) mocker.patch(mod + '.BaseApplication.apply_config', autospec=True) mocker.patch(mod + '.MergedConfiguration') from sampleproject.toolkit.app import BaseApplication app = BaseApplication() app.config.temboard.configfile = 'pouet' app.bootstrap(args=None, environ={}) assert repr(app)
def test_update_plugins(mocker): from sampleproject.toolkit.app import BaseApplication app = BaseApplication() unloadme = mocker.Mock(name='unloadme') old_plugins = dict(unloadme=unloadme) loadme = mocker.Mock(name='loadme') app.plugins = dict(loadme=loadme) app.update_plugins(old_plugins=old_plugins) assert loadme.load.called is True assert unloadme.unload.called is True
def test_app_pickle(): from pickle import dumps as pickle, loads as unpickle from sampleproject.toolkit.app import BaseApplication empty_generator = (x for x in []) orig = BaseApplication(specs=empty_generator) orig.config.update(dict(a=1)) copy = unpickle(pickle(orig)) assert copy.config
def test_application_specs(): from sampleproject.toolkit.app import BaseApplication app = BaseApplication() list(app.bootstrap_specs()) list(app.core_specs()) app = BaseApplication(with_plugins=None) specs = [str(s) for s in app.core_specs()] assert 'temboard_plugins' not in specs
def test_find_config_file(mocker): mod = 'sampleproject.toolkit.app' exists = mocker.patch(mod + '.os.path.exists', autospec=True) from sampleproject.toolkit.app import BaseApplication app = BaseApplication() app.config.add_specs(app.list_stage1_specs()) app.config.load() app.DEFAULT_CONFIGFILES = [ 'notfound', '/abs/notfound', 'found', '/notsearched', ] exists.side_effect = [ False, False, True, Exception("Not reached"), ] assert "/found" in app.find_config_file() # No file found. exists.reset_mock() exists.side_effect = None exists.return_value = False assert app.find_config_file() is None
def test_purge_plugins(): from sampleproject.toolkit.app import BaseApplication, MergedConfiguration app = BaseApplication() app.plugins = dict(destroyme=1, keepme=1) app.config = MergedConfiguration() app.config.update(dict(temboard=dict(plugins=['keepme']))) app.purge_plugins() assert 'destroyme' not in app.plugins
def test_read_file(mocker): from sampleproject.toolkit.app import BaseApplication, UserError app = BaseApplication() open_ = mocker.patch('sampleproject.toolkit.app.open', create=True) app.read_file(mocker.Mock(name='parser'), 'pouet.conf') open_.side_effect = IOError() with pytest.raises(UserError): app.read_file(mocker.Mock(name='parser'), 'pouet.conf')
def test_apply_config_without_plugins(mocker): mod = 'sampleproject.toolkit.app.' mocker.patch(mod + 'BaseApplication.setup_logging', autospec=True) from sampleproject.toolkit.app import BaseApplication app = BaseApplication(with_plugins=False) app.config_sources = dict() app.config = mocker.Mock(name='config') app.apply_config() assert app.setup_logging.called is True
def test_read_dir(mocker): mod = 'sampleproject.toolkit.app' rf = mocker.patch(mod + '.BaseApplication.read_file', autospec=True) isdir = mocker.patch(mod + '.os.path.isdir', autospec=True) glob = mocker.patch(mod + '.glob', autospec=True) from sampleproject.toolkit.app import BaseApplication app = BaseApplication() isdir.return_value = False app.read_dir(mocker.Mock(name='parser'), '/dev/null') isdir.return_value = True glob.return_value = ['pouet.conf.d/toto.conf'] app.read_dir(mocker.Mock(name='parser'), 'pouet.conf.d') assert rf.called is True
def test_apply_config_with_plugins(mocker): mod = 'sampleproject.toolkit.app.' mocker.patch(mod + 'BaseApplication.setup_logging', autospec=True) cp = mocker.patch(mod + 'BaseApplication.create_plugins', autospec=True) mocker.patch(mod + 'BaseApplication.update_plugins', autospec=True) mocker.patch(mod + 'BaseApplication.purge_plugins', autospec=True) from sampleproject.toolkit.app import BaseApplication app = BaseApplication() app.config_sources = dict() app.config = mocker.Mock(name='config') app.services.append(mocker.Mock(name='service')) cp.return_value = ['plugin'] app.apply_config() assert app.setup_logging.called is True assert app.update_plugins.called is True assert app.purge_plugins.called is True
def test_bootstrap(caplog, mocker): mod = 'sampleproject.toolkit.app' fc = mocker.patch(mod + '.BaseApplication.find_config_file', autospec=True) mocker.patch(mod + '.BaseApplication.read_file', autospec=True) mocker.patch(mod + '.BaseApplication.read_dir', autospec=True) mocker.patch(mod + '.BaseApplication.apply_config', autospec=True) mocker.patch(mod + '.MergedConfiguration') from sampleproject.toolkit.app import BaseApplication app = BaseApplication() fc.return_value = 'pouet' app.bootstrap(args=None, environ={}) assert not caplog.records assert repr(app) # No config file is ok. caplog.clear() app.config.temboard.configfile = None fc.return_value = None app.bootstrap(args=None, environ={}) assert caplog.records assert "No config file" in caplog.records[0].message
def cli(main): return BaseApplication(main=main)
def test_missing_main(): with pytest.raises(SystemExit): BaseApplication()()