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

Commit d796d4a

Browse files
committed
Added many more unit tests.
1 parent e600bbf commit d796d4a

File tree

10 files changed

+70
-15
lines changed

10 files changed

+70
-15
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ Jigsaw.egg-info/
99
docs/_build/
1010
.cache/
1111
.coverage
12+
tests/plugins/ErrorTest/error.log

jigsaw/PluginLoader.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,16 @@ def load_manifest(self, path: str) -> None:
5757
"""
5858
manifest_path = os.path.join(path, "plugin.json")
5959
self._logger.debug("Attempting to load plugin manifest from {}.".format(manifest_path))
60-
if os.path.isfile(manifest_path):
61-
try:
62-
with open(manifest_path) as f:
63-
manifest = json.load(f)
64-
manifest["path"] = path
65-
self._manifests.append(manifest)
66-
self._logger.debug("Loaded plugin manifest from {}.".format(manifest_path))
67-
except json.decoder.JSONDecodeError:
68-
self._logger.error("Failed to decode plugin manifest at {}.".format(manifest_path))
69-
except OSError:
70-
self._logger.error("Failed to load plugin manifest at {}.".format(manifest_path))
71-
else:
72-
self._logger.debug("No plugin manifest found at {}.".format(manifest_path))
60+
try:
61+
with open(manifest_path) as f:
62+
manifest = json.load(f)
63+
manifest["path"] = path
64+
self._manifests.append(manifest)
65+
self._logger.debug("Loaded plugin manifest from {}.".format(manifest_path))
66+
except json.decoder.JSONDecodeError:
67+
self._logger.error("Failed to decode plugin manifest at {}.".format(manifest_path))
68+
except OSError:
69+
self._logger.error("Failed to load plugin manifest at {}.".format(manifest_path))
7370

7471
def get_manifest(self, plugin_name: str) -> dict:
7572
"""

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name="Jigsaw",
5-
version="2.1.3",
5+
version="2.1.4",
66
packages=["jigsaw", ],
77
license="MIT",
88
description="A plugin framework for Python3.6+",
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from jigsaw import JigsawPlugin
2+
3+
4+
class Plugin(JigsawPlugin):
5+
def __init__(self, manifest: dict, *args) -> None:
6+
super(Plugin, self).__init__(manifest, *args)
7+
x = 1/0
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "Error Test"
3+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class Plugin(str):
2+
pass
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "Invalid Baseclass Test"
3+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{
2+
"name": "Invalid Manifest Test",
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from jigsaw import JigsawPlugin
2+
3+
4+
class Plugin(JigsawPlugin):
5+
pass

tests/test_jigsaw.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def test_getting_all_plugins():
105105
j.load_manifests()
106106
j.load_plugins()
107107
for item in j.get_all_plugins():
108-
if item["manifest"]["name"] == "Missing Dependency Test":
108+
if item["manifest"]["name"] in ["Missing Dependency Test", "Invalid Baseclass Test", "Error Test"]:
109109
assert isinstance(item["manifest"], dict)
110110
assert not isinstance(item["plugin"], jigsaw.JigsawPlugin)
111111
else:
@@ -132,3 +132,38 @@ def test_reload_specific_plugin():
132132
j.load_manifests()
133133
j.load_plugin(j.get_manifest("Basic Test"))
134134
j.reload_plugin("Basic Test")
135+
136+
137+
def test_load_invalid_plugin_manifest():
138+
j = jigsaw.PluginLoader(os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "plugins")))
139+
j.load_manifest(os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "plugins", "InvalidManifestTest")))
140+
assert j.get_manifest("Invalid Manifest Test") is None
141+
142+
143+
def test_loading_plugin_already_loaded():
144+
j = jigsaw.PluginLoader(os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "plugins")))
145+
j.load_manifests()
146+
j.load_plugin(j.get_manifest("Basic Test"))
147+
j.load_plugin(j.get_manifest("Basic Test"))
148+
149+
150+
def test_invalid_baseclass():
151+
j = jigsaw.PluginLoader(os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "plugins")))
152+
j.load_manifests()
153+
j.load_plugin(j.get_manifest("Invalid Baseclass Test"))
154+
assert not j.get_plugin_loaded("Invalid Baseclass Test")
155+
156+
157+
def test_error_on_plugin_load():
158+
j = jigsaw.PluginLoader(os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "plugins")))
159+
j.load_manifests()
160+
j.load_plugin(j.get_manifest("Error Test"))
161+
assert os.path.isfile(os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "plugins", "ErrorTest", "error.log")))
162+
163+
164+
def test_oserror_on_load_plugin_manifest():
165+
j = jigsaw.PluginLoader(os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "plugins")))
166+
os.mkdir(os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "plugins", "OSErrorTest", "plugin.json")))
167+
j.load_manifest(os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "plugins", "OSErrorTest")))
168+
os.rmdir(os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "plugins", "OSErrorTest", "plugin.json")))
169+
assert j.get_manifest("OS Error Test") is None

0 commit comments

Comments
 (0)