diff --git a/ovos_PHAL/service.py b/ovos_PHAL/service.py index a96091d..14cdd23 100644 --- a/ovos_PHAL/service.py +++ b/ovos_PHAL/service.py @@ -73,10 +73,19 @@ def load_plugins(self): continue config = self.user_config.get(name) or {} + if config.get("enabled") is False: + # Configuration explicitly disabled plugin + LOG.debug(f"PHAL plugin {name} disabled in configuration") + continue if hasattr(plug, "validator"): - enabled = plug.validator.validate(config) + try: + enabled = plug.validator.validate(config) + except Exception: + LOG.exception(f"Validator failed for PHAL plugin: {name}") + continue else: - enabled = config.get("enabled") + # Already checked if enabled == False, default to True + enabled = True if enabled: try: self.drivers[name] = plug(bus=self.bus, config=config) diff --git a/test/unittests/test_service.py b/test/unittests/test_service.py new file mode 100644 index 0000000..7170bee --- /dev/null +++ b/test/unittests/test_service.py @@ -0,0 +1,69 @@ +import unittest +from unittest.mock import patch, Mock + +from ovos_utils.fakebus import FakeBus + +_MOCK_CONFIG = { + "admin": { + "mock_admin_plugin": {}, + "mock_admin_enabled": { + "enabled": True + }, + "mock_admin_disabled": { + "enabled": False + } + }, + "mock_enabled_plugin": { + "enabled": True + }, + "mock_disabled_plugin": { + "enabled": False + }, + "mock_plugin": {} +} + + +class MockPlugin: + def __init__(self, *args, **kwargs): + pass + + +class TestService(unittest.TestCase): + def setUp(self): + from ovos_PHAL.service import PHAL + bus = FakeBus() + self.service = PHAL(_MOCK_CONFIG, bus) + + @patch("ovos_PHAL.service.find_phal_plugins") + def test_load_plugins(self, find_plugins): + mock_plugins = {"mock_admin_plugin": Mock(), + "mock_admin_enabled": Mock(), + "mock_admin_disabled": Mock(), + "mock_disabled_plugin": Mock(), + "mock_plugin": MockPlugin, + "mock_enabled_plugin": Mock()} + + find_plugins.return_value = mock_plugins + + # Test without validators + self.service.load_plugins() + self.assertEqual(set(self.service.drivers.keys()), + {"mock_plugin", "mock_enabled_plugin"}) + self.service.drivers = {} + + # Tests with passing validators + mock_plugins["mock_plugin"] = Mock() + mock_plugins["mock_plugin"].validator.validate = Mock(return_value=True) + mock_plugins["mock_enabled_plugin"].validator.validate = Mock(return_value=True) + mock_plugins["mock_disabled_plugin"].validator.validate = Mock(return_value=True) + self.service.load_plugins() + self.assertEqual(set(self.service.drivers.keys()), + {"mock_plugin", "mock_enabled_plugin"}) + self.service.drivers = {} + + # Tests with failing validators + mock_plugins["mock_plugin"].validator.validate.return_value = False + mock_plugins["mock_enabled_plugin"].validator.validate.return_value = False + mock_plugins["mock_disabled_plugin"].validator.validate.return_value = False + self.service.load_plugins() + self.assertEqual(self.service.drivers, {}) diff --git a/test/unittests/test_something.py b/test/unittests/test_something.py deleted file mode 100644 index 9e996d0..0000000 --- a/test/unittests/test_something.py +++ /dev/null @@ -1,12 +0,0 @@ -# write your first unittest! -import unittest - - -class TestSomething(unittest.TestCase): - @classmethod - def setUpClass(self): - self.var = "hello" - - def test_something(self): - self.assertEqual("hello", self.var) -