Skip to content

Commit

Permalink
Added tests, refactored file access
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack Heuberger committed Jun 3, 2022
1 parent 13fa1e7 commit 00911e6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
Empty file.
11 changes: 3 additions & 8 deletions megalinter/plugin_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,12 @@ def load_plugin(plugin):
else:
# From file://<path>, test both <path> and /tmp/lint/<path>
plugin_path = plugin.split("file://")[1]
if not os.path.isfile(plugin_path):
if not os.access(plugin_path, os.R_OK):
plugin_path = "/tmp/lint/" + plugin_path
if not os.path.isfile(plugin_path):
if not os.access(plugin_path, os.R_OK):
raise Exception(
f"[Plugins] Local plugin descriptor {plugin} not found"
f"[Plugins] Local plugin descriptor not found or not readable {plugin}"
)
# Make sure plugin file is readable and not empty
if not os.access(plugin_path, os.R_OK):
raise Exception(
f"[Plugins] Local plugin descriptor {plugin} not readable"
)
if os.stat(plugin_path).st_size == 0:
raise Exception(f"[Plugins] Plugin descriptor {plugin} is empty")
r = open(plugin_path, "r").read()
Expand Down
28 changes: 27 additions & 1 deletion megalinter/tests/test_megalinter/plugins_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,32 @@ def test_load_local_plugin_success(self):
self.assertIn("[Plugins] Loaded plugin descriptor", output)
self.assertIn("[Plugins] Successful initialization of TEST", output)

def test_load_local_plugin_fail(self):
try:
utilstest.call_mega_linter(
{
"PLUGINS": ".automation/test/mega-linter-plugin-test/test-fake.megalinter-descriptor.yml",
"LOG_LEVEL": "DEBUG",
"MULTI_STATUS": "false",
"GITHUB_COMMENT_REPORTER": "false",
}
)
except Exception as e:
self.assertIn("[Plugins] Local plugin descriptor not found or not readable", str(e))

def test_load_local_plugin_read_fail(self):
try:
utilstest.call_mega_linter(
{
"PLUGINS": ".automation/test/mega-linter-plugin-test/test-empty.megalinter-descriptor.yml",
"LOG_LEVEL": "DEBUG",
"MULTI_STATUS": "false",
"GITHUB_COMMENT_REPORTER": "false",
}
)
except Exception as e:
self.assertIn("[Plugins] Local plugin descriptor not found or not readable", str(e))

def test_load_plugin_http_error(self):
try:
utilstest.call_mega_linter(
Expand All @@ -70,7 +96,7 @@ def test_load_plugin_http_error(self):
}
)
except Exception as e:
self.assertIn("[Plugins] Unable to load plugin", str(e))
self.assertIn("[Plugins] Unable to load remote plugin", str(e))

def test_load_plugin_host_url_error_1(self):
try:
Expand Down

0 comments on commit 00911e6

Please sign in to comment.