From 00911e6a7af88038a20ecf1d31b56919a0bd77e7 Mon Sep 17 00:00:00 2001 From: Jack Heuberger Date: Fri, 3 Jun 2022 14:37:25 -0500 Subject: [PATCH] Added tests, refactored file access --- .../test-empty.megalinter-descriptor.yml | 0 megalinter/plugin_factory.py | 11 ++------ .../tests/test_megalinter/plugins_test.py | 28 ++++++++++++++++++- 3 files changed, 30 insertions(+), 9 deletions(-) create mode 100644 .automation/test/mega-linter-plugin-test/test-empty.megalinter-descriptor.yml diff --git a/.automation/test/mega-linter-plugin-test/test-empty.megalinter-descriptor.yml b/.automation/test/mega-linter-plugin-test/test-empty.megalinter-descriptor.yml new file mode 100644 index 00000000000..e69de29bb2d diff --git a/megalinter/plugin_factory.py b/megalinter/plugin_factory.py index 4f2b54d1173..e38d40d7a66 100644 --- a/megalinter/plugin_factory.py +++ b/megalinter/plugin_factory.py @@ -45,17 +45,12 @@ def load_plugin(plugin): else: # From file://, test both and /tmp/lint/ 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() diff --git a/megalinter/tests/test_megalinter/plugins_test.py b/megalinter/tests/test_megalinter/plugins_test.py index c04de6d3e7c..571fad06e73 100644 --- a/megalinter/tests/test_megalinter/plugins_test.py +++ b/megalinter/tests/test_megalinter/plugins_test.py @@ -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( @@ -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: