Skip to content
This repository has been archived by the owner on Nov 9, 2024. It is now read-only.

Commit

Permalink
Added many more unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
nint8835 committed Mar 5, 2017
1 parent e600bbf commit d796d4a
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ Jigsaw.egg-info/
docs/_build/
.cache/
.coverage
tests/plugins/ErrorTest/error.log
23 changes: 10 additions & 13 deletions jigsaw/PluginLoader.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,16 @@ def load_manifest(self, path: str) -> None:
"""
manifest_path = os.path.join(path, "plugin.json")
self._logger.debug("Attempting to load plugin manifest from {}.".format(manifest_path))
if os.path.isfile(manifest_path):
try:
with open(manifest_path) as f:
manifest = json.load(f)
manifest["path"] = path
self._manifests.append(manifest)
self._logger.debug("Loaded plugin manifest from {}.".format(manifest_path))
except json.decoder.JSONDecodeError:
self._logger.error("Failed to decode plugin manifest at {}.".format(manifest_path))
except OSError:
self._logger.error("Failed to load plugin manifest at {}.".format(manifest_path))
else:
self._logger.debug("No plugin manifest found at {}.".format(manifest_path))
try:
with open(manifest_path) as f:
manifest = json.load(f)
manifest["path"] = path
self._manifests.append(manifest)
self._logger.debug("Loaded plugin manifest from {}.".format(manifest_path))
except json.decoder.JSONDecodeError:
self._logger.error("Failed to decode plugin manifest at {}.".format(manifest_path))
except OSError:
self._logger.error("Failed to load plugin manifest at {}.".format(manifest_path))

def get_manifest(self, plugin_name: str) -> dict:
"""
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name="Jigsaw",
version="2.1.3",
version="2.1.4",
packages=["jigsaw", ],
license="MIT",
description="A plugin framework for Python3.6+",
Expand Down
7 changes: 7 additions & 0 deletions tests/plugins/ErrorTest/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from jigsaw import JigsawPlugin


class Plugin(JigsawPlugin):
def __init__(self, manifest: dict, *args) -> None:
super(Plugin, self).__init__(manifest, *args)
x = 1/0
3 changes: 3 additions & 0 deletions tests/plugins/ErrorTest/plugin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "Error Test"
}
2 changes: 2 additions & 0 deletions tests/plugins/InvalidBaseclassTest/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Plugin(str):
pass
3 changes: 3 additions & 0 deletions tests/plugins/InvalidBaseclassTest/plugin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "Invalid Baseclass Test"
}
2 changes: 2 additions & 0 deletions tests/plugins/InvalidManifestTest/plugin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
"name": "Invalid Manifest Test",
5 changes: 5 additions & 0 deletions tests/plugins/OSErrorTest/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from jigsaw import JigsawPlugin


class Plugin(JigsawPlugin):
pass
37 changes: 36 additions & 1 deletion tests/test_jigsaw.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def test_getting_all_plugins():
j.load_manifests()
j.load_plugins()
for item in j.get_all_plugins():
if item["manifest"]["name"] == "Missing Dependency Test":
if item["manifest"]["name"] in ["Missing Dependency Test", "Invalid Baseclass Test", "Error Test"]:
assert isinstance(item["manifest"], dict)
assert not isinstance(item["plugin"], jigsaw.JigsawPlugin)
else:
Expand All @@ -132,3 +132,38 @@ def test_reload_specific_plugin():
j.load_manifests()
j.load_plugin(j.get_manifest("Basic Test"))
j.reload_plugin("Basic Test")


def test_load_invalid_plugin_manifest():
j = jigsaw.PluginLoader(os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "plugins")))
j.load_manifest(os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "plugins", "InvalidManifestTest")))
assert j.get_manifest("Invalid Manifest Test") is None


def test_loading_plugin_already_loaded():
j = jigsaw.PluginLoader(os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "plugins")))
j.load_manifests()
j.load_plugin(j.get_manifest("Basic Test"))
j.load_plugin(j.get_manifest("Basic Test"))


def test_invalid_baseclass():
j = jigsaw.PluginLoader(os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "plugins")))
j.load_manifests()
j.load_plugin(j.get_manifest("Invalid Baseclass Test"))
assert not j.get_plugin_loaded("Invalid Baseclass Test")


def test_error_on_plugin_load():
j = jigsaw.PluginLoader(os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "plugins")))
j.load_manifests()
j.load_plugin(j.get_manifest("Error Test"))
assert os.path.isfile(os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "plugins", "ErrorTest", "error.log")))


def test_oserror_on_load_plugin_manifest():
j = jigsaw.PluginLoader(os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "plugins")))
os.mkdir(os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "plugins", "OSErrorTest", "plugin.json")))
j.load_manifest(os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "plugins", "OSErrorTest")))
os.rmdir(os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "plugins", "OSErrorTest", "plugin.json")))
assert j.get_manifest("OS Error Test") is None

0 comments on commit d796d4a

Please sign in to comment.