diff options
author | Armin Ronacher <armin.ronacher@active-4.com> | 2014-06-30 22:50:56 +0200 |
---|---|---|
committer | Armin Ronacher <armin.ronacher@active-4.com> | 2014-06-30 22:50:56 +0200 |
commit | 1e6456bf54bb37743d22fe564b475f10bc13281e (patch) | |
tree | 3cf0c8cb8dd3688785ff73100f8bd8af98eb3105 /tests | |
download | pluginbase-1e6456bf54bb37743d22fe564b475f10bc13281e.tar.gz |
Initial commit.0.1
Diffstat (limited to 'tests')
-rw-r--r-- | tests/conftest.py | 19 | ||||
-rw-r--r-- | tests/dummy.py | 1 | ||||
-rw-r--r-- | tests/plugins/hello.py | 12 | ||||
-rw-r--r-- | tests/plugins/hello2.py | 2 | ||||
-rw-r--r-- | tests/test_basics.py | 91 |
5 files changed, 125 insertions, 0 deletions
diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..9226228 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,19 @@ +import pytest + +from pluginbase import PluginBase + + +@pytest.fixture(scope='function') +def base(): + return PluginBase(package='dummy.plugins') + + +@pytest.fixture(scope='function') +def dummy_internal_name(): + return 'pluginbase._internalspace._sp7bb7d8da1d24ae5a5205609c951b8be4' + + +@pytest.fixture(scope='function') +def source(base): + return base.make_plugin_source(searchpath=['./plugins'], + identifier='demo') diff --git a/tests/dummy.py b/tests/dummy.py new file mode 100644 index 0000000..4ef4ca0 --- /dev/null +++ b/tests/dummy.py @@ -0,0 +1 @@ +# make sure there is a module we can base our modules against. diff --git a/tests/plugins/hello.py b/tests/plugins/hello.py new file mode 100644 index 0000000..a113f41 --- /dev/null +++ b/tests/plugins/hello.py @@ -0,0 +1,12 @@ +def import_self(): + from dummy.plugins import hello + return hello + + +def get_plugin_source(): + from pluginbase import get_plugin_source + return get_plugin_source() + + +def demo_func(): + return 42 diff --git a/tests/plugins/hello2.py b/tests/plugins/hello2.py new file mode 100644 index 0000000..58335c3 --- /dev/null +++ b/tests/plugins/hello2.py @@ -0,0 +1,2 @@ +def awesome_stuff(): + pass diff --git a/tests/test_basics.py b/tests/test_basics.py new file mode 100644 index 0000000..3130d49 --- /dev/null +++ b/tests/test_basics.py @@ -0,0 +1,91 @@ +import sys + +from pluginbase import get_plugin_source + + +def test_basic_plugin(source, dummy_internal_name): + # When the source is active the import gives us a module + with source: + from dummy.plugins import hello + + # Which because of a stable identifier has a predictable name. + assert hello.__name__ == dummy_internal_name + '.hello' + + # And can continue to import from itself. + assert hello.import_self() is hello + + # On the other hand without a source will fall flat on the floor. + try: + from dummy.plugins import hello + except RuntimeError: + pass + else: + assert False, 'Expected a runtime error but managed to ' \ + 'import hello (%s)' % hello + + +def test_fetching_plugin_source(source): + # Finding the plugin source outside of a plugin and without a with + # block of a plugin source returns None. + assert get_plugin_source() is None + + # Inside a source block we can find the source through mere calling. + with source: + assert get_plugin_source() is source + + # A module can always find its own source as well (the hello module + # calls get_plugin_source() itself). + with source: + from dummy.plugins import hello + assert hello.get_plugin_source() is source + + # Last but not least the plugin source can be found by module names + # (in a plugin source block by the import name and in any case by + # the internal name) + with source: + assert get_plugin_source('dummy.plugins.hello') is source + assert get_plugin_source(hello.__name__) is source + + # As well as by module object. + assert get_plugin_source(hello) is source + + +def test_cleanup(base): + new_source = base.make_plugin_source(searchpath=['./plugins']) + mod_name = new_source.mod.__name__ + assert sys.modules.get(mod_name) is new_source.mod + + with new_source: + from dummy.plugins import hello + + new_source = None + assert sys.modules.get(mod_name) is None + assert hello.import_self is None + + +def test_persist(base): + new_source = base.make_plugin_source(searchpath=['./plugins'], + persist=True) + mod_name = new_source.mod.__name__ + assert sys.modules.get(mod_name) is new_source.mod + + with new_source: + from dummy.plugins import hello + + new_source = None + assert sys.modules.get(mod_name) is not None + assert hello.import_self is not None + sys.modules[mod_name].__pluginbase_state__.source.cleanup() + assert sys.modules.get(mod_name) is None + assert hello.import_self is None + + +def test_list_plugins(source): + plugins = source.list_plugins() + hello_plugins = [x for x in plugins if x.startswith('hello')] + assert hello_plugins == ['hello', 'hello2'] + + +def test_load_plugin(source): + hello = source.load_plugin('hello') + assert hello.demo_func() == 42 |