Skip to content

Commit

Permalink
Support enabled: False config for plugins with validators (#35)
Browse files Browse the repository at this point in the history
* Refactor plugin load logic to respect plugins disabled in config
Adds unit test coverate for PHAL plugin load

* Update default behavior for plugins without validators with unit test updates

---------

Co-authored-by: Daniel McKnight <[email protected]>
  • Loading branch information
NeonDaniel and NeonDaniel authored Jun 28, 2024
1 parent d9152b0 commit 28a2ca3
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 14 deletions.
13 changes: 11 additions & 2 deletions ovos_PHAL/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
69 changes: 69 additions & 0 deletions test/unittests/test_service.py
Original file line number Diff line number Diff line change
@@ -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, {})
12 changes: 0 additions & 12 deletions test/unittests/test_something.py

This file was deleted.

0 comments on commit 28a2ca3

Please sign in to comment.