Skip to content

Commit

Permalink
Refactor plugin load logic to respect plugins disabled in config
Browse files Browse the repository at this point in the history
Adds unit test coverate for PHAL plugin load
  • Loading branch information
NeonDaniel committed Jun 28, 2024
1 parent d9152b0 commit c9c234c
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 13 deletions.
11 changes: 10 additions & 1 deletion ovos_PHAL/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,18 @@ 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:
# No validator, check if plugin is explicitly enabled
enabled = config.get("enabled")
if enabled:
try:
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_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 c9c234c

Please sign in to comment.