Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement module_property decorator with unit test #103

Merged
merged 2 commits into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions ovos_utils/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

108 changes: 108 additions & 0 deletions test/unittests/test_system.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
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
set_root_path("test")
from ovos_utils.system import _USER_DEFINED_ROOT
Copy link
Member

@JarbasAl JarbasAl Mar 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_USER_DEFINED_ROOT could be a module property now ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure how much this is used, but I can test that change

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't actually change behavior, only adds a wrapper method since the property is evaluated upon import, not reference. Since it's already marked as an internal variable, I'll leave it

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):
# 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)