From 405b545ccb3af78a6a47856fb0bb75718526dbbf Mon Sep 17 00:00:00 2001 From: Daniel McKnight Date: Tue, 7 Mar 2023 11:53:22 -0800 Subject: [PATCH 1/2] Implement module_property decorator with unit test --- ovos_utils/system.py | 25 ++++++++ test/unittests/test_system.py | 105 ++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 test/unittests/test_system.py diff --git a/ovos_utils/system.py b/ovos_utils/system.py index b1b2c8e9..2696048f 100644 --- a/ovos_utils/system.py +++ b/ovos_utils/system.py @@ -318,3 +318,28 @@ def has_screen(): except ImportError: pass return have_display + + +def module_property(func): + """ + Decorator to turn module functions into properties. + Function names must be prefixed with an underscore. + :param func: function to decorate + """ + import sys + module = sys.modules[func.__module__] + + def fallback_getattr(name): + raise AttributeError( + f"module '{module.__name__}' has no attribute '{name}'") + + default_getattr = getattr(module, '__getattr__', fallback_getattr) + + def patched_getattr(name): + if f'_{name}' == func.__name__: + return func() + return default_getattr(name) + + module.__getattr__ = patched_getattr + return func + diff --git a/test/unittests/test_system.py b/test/unittests/test_system.py new file mode 100644 index 00000000..818322d3 --- /dev/null +++ b/test/unittests/test_system.py @@ -0,0 +1,105 @@ +import unittest + + +class TestSystem(unittest.TestCase): + def test_is_running_from_module(self): + from ovos_utils.system import is_running_from_module + self.assertFalse(is_running_from_module("mycroft")) + self.assertTrue(is_running_from_module("unittest")) + + def test_ntp_sync(self): + # TODO + pass + + def test_system_shutdown(self): + # TODO + pass + + def test_system_reboot(self): + # TODO + pass + + def test_ssh_enable(self): + # TODO + pass + + def test_ssh_disable(self): + # TODO + pass + + def test_restart_mycroft_service(self): + # TODO + pass + + def test_restart_service(self): + # TODO + pass + + def test_enable_service(self): + # TODO + pass + + def test_disable_service(self): + # TODO + pass + + def test_check_service_active(self): + # TODO + pass + + def test_set_root_path(self): + from ovos_utils.system import set_root_path, _USER_DEFINED_ROOT + set_root_path("test") + self.assertEqual(_USER_DEFINED_ROOT, "test") + set_root_path("mycroft") + self.assertEqual(_USER_DEFINED_ROOT, "mycroft") + set_root_path(None) + self.assertIsNone(_USER_DEFINED_ROOT) + + def test_find_root_from_sys_path(self): + # TODO + pass + + def test_find_root_from_sitepackages(self): + # TODO + pass + + def test_search_mycroft_core_location(self): + # TODO + pass + + def test_get_desktop_environment(self): + # TODO + pass + + def test_is_process_running(self): + # TODO + pass + + def test_find_executable(self): + # TODO + pass + + def test_is_installed(self): + # TODO + pass + + def test_has_screen(self): + # TODO + pass + + def test_module_property(self): + import sys + from ovos_utils.system import module_property + + test_val = True + + @module_property + def _mock_property(): + return test_val + + test_module = sys.modules[self.__module__] + self.assertTrue(test_module.mock_property) + + test_val = False + self.assertFalse(test_module.mock_property) From dd3749ca6ea85a19de33627d6d0034fe5ebe46e7 Mon Sep 17 00:00:00 2001 From: Daniel McKnight Date: Tue, 7 Mar 2023 11:57:36 -0800 Subject: [PATCH 2/2] Resolve bug in added test --- test/unittests/test_system.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/unittests/test_system.py b/test/unittests/test_system.py index 818322d3..1f518446 100644 --- a/test/unittests/test_system.py +++ b/test/unittests/test_system.py @@ -48,12 +48,15 @@ def test_check_service_active(self): pass def test_set_root_path(self): - from ovos_utils.system import set_root_path, _USER_DEFINED_ROOT + from ovos_utils.system import set_root_path set_root_path("test") + from ovos_utils.system import _USER_DEFINED_ROOT self.assertEqual(_USER_DEFINED_ROOT, "test") set_root_path("mycroft") + from ovos_utils.system import _USER_DEFINED_ROOT self.assertEqual(_USER_DEFINED_ROOT, "mycroft") set_root_path(None) + from ovos_utils.system import _USER_DEFINED_ROOT self.assertIsNone(_USER_DEFINED_ROOT) def test_find_root_from_sys_path(self):