2011-11-01 19:28:00 +00:00
|
|
|
|
|
|
|
|
import os
|
2012-07-13 07:49:46 +00:00
|
|
|
import sys
|
2017-12-27 21:42:38 +00:00
|
|
|
from pytest import raises
|
2014-08-28 22:31:51 +00:00
|
|
|
|
2017-12-27 21:42:38 +00:00
|
|
|
from cement import init_defaults
|
|
|
|
|
from cement.core.foundation import TestApp
|
|
|
|
|
from cement.core.exc import FrameworkError
|
2018-07-17 09:28:59 +00:00
|
|
|
from cement.core.plugin import PluginInterface, PluginHandler
|
2011-11-01 19:28:00 +00:00
|
|
|
|
2017-12-27 21:42:38 +00:00
|
|
|
|
2018-03-02 03:23:38 +00:00
|
|
|
# module tests
|
2017-12-27 21:42:38 +00:00
|
|
|
|
2018-07-17 09:28:59 +00:00
|
|
|
class TestPluginInterface(object):
|
2017-12-27 21:42:38 +00:00
|
|
|
def test_interface(self):
|
2018-07-17 09:28:59 +00:00
|
|
|
assert PluginInterface.Meta.interface == 'plugin'
|
2017-12-27 21:42:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestPluginHandler(object):
|
|
|
|
|
def test_subclassing(self):
|
|
|
|
|
|
|
|
|
|
class MyPluginHandler(PluginHandler):
|
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
label = 'my_plugin_handler'
|
|
|
|
|
|
|
|
|
|
def load_plugin(plugin_name):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def load_plugins(self, plugins):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def get_loaded_plugins(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def get_enabled_plugins(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def get_disabled_plugins(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
h = MyPluginHandler()
|
|
|
|
|
assert h._meta.interface == 'plugin'
|
|
|
|
|
assert h._meta.label == 'my_plugin_handler'
|
|
|
|
|
|
|
|
|
|
|
2018-03-02 03:23:38 +00:00
|
|
|
# app functionality and coverage tests
|
2017-12-27 21:42:38 +00:00
|
|
|
|
|
|
|
|
CONF1 = """
|
2018-03-02 04:31:24 +00:00
|
|
|
[plugin.myplugin]
|
2018-07-30 02:28:18 +00:00
|
|
|
enabled = true
|
2011-11-01 19:28:00 +00:00
|
|
|
foo = bar
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
CONF2 = """
|
2018-03-02 04:31:24 +00:00
|
|
|
[plugin.myplugin]
|
2018-07-30 02:28:18 +00:00
|
|
|
enabled = false
|
2011-11-01 19:28:00 +00:00
|
|
|
foo = bar
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
CONF3 = """
|
2018-03-02 04:31:24 +00:00
|
|
|
[plugin.bogus_plugin]
|
2011-11-01 19:28:00 +00:00
|
|
|
foo = bar
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
CONF4 = """
|
2018-03-02 04:31:24 +00:00
|
|
|
[plugin.ext_json]
|
2018-07-30 02:28:18 +00:00
|
|
|
enabled = true
|
2011-11-01 19:28:00 +00:00
|
|
|
foo = bar
|
|
|
|
|
"""
|
|
|
|
|
|
2012-04-17 12:20:22 +00:00
|
|
|
CONF5 = ''
|
|
|
|
|
|
2011-11-01 19:28:00 +00:00
|
|
|
PLUGIN = """
|
|
|
|
|
|
2017-12-27 21:42:38 +00:00
|
|
|
from cement.core.output import OutputHandler
|
2011-11-01 19:28:00 +00:00
|
|
|
|
2017-12-27 21:42:38 +00:00
|
|
|
class MyOutputHandler(OutputHandler):
|
2011-12-19 23:35:10 +00:00
|
|
|
class Meta:
|
2017-12-27 21:42:38 +00:00
|
|
|
label = 'my_output_handler'
|
2014-04-16 16:58:45 +00:00
|
|
|
|
2011-11-01 19:28:00 +00:00
|
|
|
def render(self, data_dict, template=None):
|
|
|
|
|
pass
|
2014-04-16 16:58:45 +00:00
|
|
|
|
2014-07-09 16:41:12 +00:00
|
|
|
def load(app):
|
2017-12-27 21:42:38 +00:00
|
|
|
app.handler.register(MyOutputHandler)
|
2011-11-01 19:28:00 +00:00
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
2015-06-12 19:14:39 +00:00
|
|
|
|
2017-12-27 21:42:38 +00:00
|
|
|
def test_load_plugins_from_files(tmp):
|
|
|
|
|
|
|
|
|
|
f = open(os.path.join(tmp.dir, 'myplugin.conf'), 'w')
|
|
|
|
|
f.write(CONF1)
|
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
|
|
f = open(os.path.join(tmp.dir, 'myplugin.py'), 'w')
|
|
|
|
|
f.write(PLUGIN)
|
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
|
|
class MyApp(TestApp):
|
|
|
|
|
class Meta:
|
2018-08-10 06:33:16 +00:00
|
|
|
config_dirs = [tmp.dir]
|
2017-12-27 21:42:38 +00:00
|
|
|
plugin_dir = tmp.dir
|
2018-08-11 20:42:45 +00:00
|
|
|
plugin_module = None
|
2017-12-27 21:42:38 +00:00
|
|
|
|
|
|
|
|
with MyApp() as app:
|
|
|
|
|
han = app.handler.get('output', 'my_output_handler')()
|
|
|
|
|
assert han._meta.label, 'my_output_handler'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_load_order_presedence_one(tmp):
|
|
|
|
|
# multiple plugin configs, first plugin conf defines it as disabled,
|
|
|
|
|
# but last read should make it enabled.
|
2018-03-02 04:31:24 +00:00
|
|
|
defaults = init_defaults('plugin.myplugin')
|
2017-12-27 21:42:38 +00:00
|
|
|
|
|
|
|
|
f = open(os.path.join(tmp.dir, 'a.conf'), 'w')
|
|
|
|
|
f.write(CONF2) # disabled config
|
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
|
|
f = open(os.path.join(tmp.dir, 'b.conf'), 'w')
|
|
|
|
|
f.write(CONF1) # enabled config
|
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
|
|
f = open(os.path.join(tmp.dir, 'myplugin.py'), 'w')
|
|
|
|
|
f.write(PLUGIN)
|
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
|
|
class MyApp(TestApp):
|
|
|
|
|
class Meta:
|
|
|
|
|
config_defaults = defaults
|
2018-08-10 06:33:16 +00:00
|
|
|
config_dirs = [tmp.dir]
|
2017-12-27 21:42:38 +00:00
|
|
|
plugin_dir = tmp.dir
|
2018-08-11 20:42:45 +00:00
|
|
|
plugin_module = None
|
2017-12-27 21:42:38 +00:00
|
|
|
|
|
|
|
|
with MyApp() as app:
|
2018-08-10 06:33:16 +00:00
|
|
|
app.run()
|
2017-12-27 21:42:38 +00:00
|
|
|
assert 'myplugin' in app.plugin._enabled_plugins
|
|
|
|
|
assert 'myplugin' not in app.plugin._disabled_plugins
|
|
|
|
|
|
|
|
|
|
|
2018-08-10 06:33:16 +00:00
|
|
|
def test_load_order_presedence_two(tmp):
|
2017-12-27 21:42:38 +00:00
|
|
|
# multiple plugin configs, first plugin conf defines it as enabled,
|
|
|
|
|
# but last read should make it disabled.
|
2018-03-02 04:31:24 +00:00
|
|
|
defaults = init_defaults('plugin.myplugin')
|
2017-12-27 21:42:38 +00:00
|
|
|
|
|
|
|
|
f = open(os.path.join(tmp.dir, 'a.conf'), 'w')
|
|
|
|
|
f.write(CONF1) # enabled config
|
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
|
|
f = open(os.path.join(tmp.dir, 'b.conf'), 'w')
|
|
|
|
|
f.write(CONF2) # disabled config
|
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
|
|
f = open(os.path.join(tmp.dir, 'myplugin.py'), 'w')
|
|
|
|
|
f.write(PLUGIN)
|
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
|
|
class MyApp(TestApp):
|
|
|
|
|
class Meta:
|
|
|
|
|
config_defaults = defaults
|
2018-08-10 06:33:16 +00:00
|
|
|
config_dirs = [tmp.dir]
|
2017-12-27 21:42:38 +00:00
|
|
|
plugin_dir = tmp.dir
|
2018-08-11 20:42:45 +00:00
|
|
|
plugin_module = None
|
2018-08-10 06:33:16 +00:00
|
|
|
debug = True
|
2017-12-27 21:42:38 +00:00
|
|
|
|
|
|
|
|
with MyApp() as app:
|
2018-08-10 06:33:16 +00:00
|
|
|
app.run()
|
|
|
|
|
print(tmp.dir)
|
|
|
|
|
tmp.cleanup = False
|
2017-12-27 21:42:38 +00:00
|
|
|
assert 'myplugin' in app.plugin._disabled_plugins
|
|
|
|
|
assert 'myplugin' not in app.plugin._enabled_plugins
|
|
|
|
|
|
|
|
|
|
|
2018-08-10 06:33:16 +00:00
|
|
|
def test_load_order_presedence_three(tmp):
|
2017-12-27 21:42:38 +00:00
|
|
|
# Multiple plugin configs, enable -> disabled -> enable
|
2018-03-02 04:31:24 +00:00
|
|
|
defaults = init_defaults('plugin.myplugin')
|
2017-12-27 21:42:38 +00:00
|
|
|
|
|
|
|
|
f = open(os.path.join(tmp.dir, 'a.conf'), 'w')
|
|
|
|
|
f.write(CONF1) # enabled config
|
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
|
|
f = open(os.path.join(tmp.dir, 'b.conf'), 'w')
|
|
|
|
|
f.write(CONF2) # disabled config
|
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
|
|
f = open(os.path.join(tmp.dir, 'c.conf'), 'w')
|
|
|
|
|
f.write(CONF1) # enabled config
|
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
|
|
f = open(os.path.join(tmp.dir, 'd.conf'), 'w')
|
|
|
|
|
f.write(CONF2) # disabled config
|
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
|
|
f = open(os.path.join(tmp.dir, 'e.conf'), 'w')
|
|
|
|
|
f.write(CONF1) # enabled config
|
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
|
|
f = open(os.path.join(tmp.dir, 'myplugin.py'), 'w')
|
|
|
|
|
f.write(PLUGIN)
|
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
|
|
class MyApp(TestApp):
|
|
|
|
|
class Meta:
|
|
|
|
|
config_defaults = defaults
|
2018-08-10 06:33:16 +00:00
|
|
|
config_dirs = [tmp.dir]
|
2017-12-27 21:42:38 +00:00
|
|
|
plugin_dir = tmp.dir
|
2018-08-11 20:42:45 +00:00
|
|
|
plugin_module = None
|
2017-12-27 21:42:38 +00:00
|
|
|
|
|
|
|
|
with MyApp() as app:
|
|
|
|
|
assert 'myplugin' in app.plugin._enabled_plugins
|
|
|
|
|
assert 'myplugin' not in app.plugin._disabled_plugins
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_load_plugins_from_config(tmp):
|
|
|
|
|
f = open(os.path.join(tmp.dir, 'myplugin.py'), 'w')
|
|
|
|
|
f.write(PLUGIN)
|
|
|
|
|
f.close()
|
|
|
|
|
|
2018-03-02 04:31:24 +00:00
|
|
|
defaults = init_defaults('plugin.myplugin', 'plugin.myplugin2')
|
2018-07-30 02:28:18 +00:00
|
|
|
defaults['plugin.myplugin']['enabled'] = True
|
|
|
|
|
defaults['plugin.myplugin2']['enabled'] = False
|
2017-12-27 21:42:38 +00:00
|
|
|
|
|
|
|
|
class MyApp(TestApp):
|
|
|
|
|
class Meta:
|
|
|
|
|
config_defaults = defaults
|
2018-08-10 06:33:16 +00:00
|
|
|
config_dirs = [tmp.dir]
|
2017-12-27 21:42:38 +00:00
|
|
|
plugin_dir = tmp.dir
|
2018-08-11 20:42:45 +00:00
|
|
|
plugin_module = None
|
2017-12-27 21:42:38 +00:00
|
|
|
|
|
|
|
|
with MyApp() as app:
|
|
|
|
|
han = app.handler.get('output', 'my_output_handler')()
|
|
|
|
|
assert han._meta.label == 'my_output_handler'
|
|
|
|
|
|
|
|
|
|
# some more checks for coverage
|
|
|
|
|
|
|
|
|
|
assert 'myplugin' in app.plugin.get_enabled_plugins()
|
|
|
|
|
assert 'myplugin' in app.plugin.get_loaded_plugins()
|
|
|
|
|
assert 'myplugin2' in app.plugin.get_disabled_plugins()
|
|
|
|
|
assert 'myplugin2' not in app.plugin.get_enabled_plugins()
|
|
|
|
|
assert 'myplugin2' not in app.plugin.get_loaded_plugins()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_disabled_plugins_from_files(tmp):
|
|
|
|
|
f = open(os.path.join(tmp.dir, 'myplugin.conf'), 'w')
|
|
|
|
|
f.write(CONF2)
|
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
|
|
f = open(os.path.join(tmp.dir, 'myplugin.py'), 'w')
|
|
|
|
|
f.write(PLUGIN)
|
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
|
|
class MyApp(TestApp):
|
|
|
|
|
class Meta:
|
2018-08-10 06:33:16 +00:00
|
|
|
config_dirs = [tmp.dir]
|
2017-12-27 21:42:38 +00:00
|
|
|
plugin_dir = tmp.dir
|
2018-08-11 20:42:45 +00:00
|
|
|
plugin_module = None
|
2017-12-27 21:42:38 +00:00
|
|
|
|
|
|
|
|
with MyApp() as app:
|
|
|
|
|
assert 'my_output_handler' not in app.handler.__handlers__['output']
|
|
|
|
|
assert 'myplugin2' not in app.plugin.get_enabled_plugins()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_bogus_plugin_from_files(tmp):
|
|
|
|
|
f = open(os.path.join(tmp.dir, 'myplugin.conf'), 'w')
|
|
|
|
|
f.write(CONF3)
|
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
|
|
# do this for coverage... empty config file
|
|
|
|
|
f = open(os.path.join(tmp.dir, 'bogus.conf'), 'w')
|
|
|
|
|
f.write(CONF5)
|
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
|
|
class MyApp(TestApp):
|
|
|
|
|
class Meta:
|
2018-08-10 06:33:16 +00:00
|
|
|
config_dirs = [tmp.dir]
|
2017-12-27 21:42:38 +00:00
|
|
|
plugin_dir = tmp.dir
|
2018-08-11 20:42:45 +00:00
|
|
|
plugin_module = None
|
2017-12-27 21:42:38 +00:00
|
|
|
|
|
|
|
|
with MyApp() as app:
|
|
|
|
|
assert 'bogus_plugin' not in app.plugin.get_enabled_plugins()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_bad_plugin_dir(tmp):
|
|
|
|
|
f = open(os.path.join(tmp.dir, 'myplugin.conf'), 'w')
|
|
|
|
|
f.write(CONF1)
|
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
|
|
class MyApp(TestApp):
|
|
|
|
|
class Meta:
|
2018-08-10 06:33:16 +00:00
|
|
|
config_dirs = [tmp.dir]
|
2017-12-27 21:42:38 +00:00
|
|
|
plugin_dir = './some/bogus/path'
|
2018-08-11 20:42:45 +00:00
|
|
|
plugin_module = None
|
2017-12-27 21:42:38 +00:00
|
|
|
|
|
|
|
|
with raises(FrameworkError, match="Unable to load plugin 'myplugin'."):
|
2018-03-02 03:23:38 +00:00
|
|
|
with MyApp():
|
2017-12-27 21:42:38 +00:00
|
|
|
pass
|
|
|
|
|
|
2018-03-02 03:23:38 +00:00
|
|
|
|
2017-12-27 21:42:38 +00:00
|
|
|
def test_load_plugin_from_module(tmp):
|
|
|
|
|
# We mock this out by loading a cement ext, but it is essentially the
|
|
|
|
|
# same type of code.
|
|
|
|
|
if 'cement.ext.ext_json' in sys.modules:
|
2012-07-13 07:49:46 +00:00
|
|
|
del sys.modules['cement.ext.ext_json']
|
2017-12-27 21:42:38 +00:00
|
|
|
|
|
|
|
|
f = open(os.path.join(tmp.dir, 'ext_json.conf'), 'w')
|
|
|
|
|
f.write(CONF4)
|
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
|
|
class MyApp(TestApp):
|
|
|
|
|
class Meta:
|
2018-08-10 06:33:16 +00:00
|
|
|
config_dirs = [tmp.dir]
|
2017-12-27 21:42:38 +00:00
|
|
|
plugin_dir = tmp.dir
|
2018-08-11 20:42:45 +00:00
|
|
|
plugin_module = 'cement.ext'
|
2017-12-27 21:42:38 +00:00
|
|
|
|
|
|
|
|
with MyApp() as app:
|
|
|
|
|
assert 'ext_json' in app.plugin.get_enabled_plugins()
|