fix case when provided trigger path is directory

If trigger is directory and we have permissions to read it, the loaded
will try to load this from it and will fail with IsADirectoryError
This commit is contained in:
Evgenii Alekseev 2022-05-12 07:22:50 +03:00
parent 38e48b1571
commit f6e57a411c
2 changed files with 18 additions and 2 deletions

View File

@ -104,6 +104,9 @@ class TriggerLoader:
Returns:
ModuleType: module loaded from the imported module
Raises:
InvalidExtension: in case if module cannot be loaded from specified package
"""
self.logger.info("load module from package %s", package)
try:
@ -119,13 +122,17 @@ class TriggerLoader:
module_path(str): module import path to load
Returns:
Trigger: loaded trigger based on settings
Trigger: loaded trigger based on settings
Raises:
InvalidExtension: in case if module cannot be loaded from the specified module path or is not a trigger
"""
*package_path_parts, class_name = module_path.split(".")
package_or_path = ".".join(package_path_parts)
# it works for both missing permission and file does not exist
if os.access(Path(package_or_path), os.R_OK):
path_like = Path(package_or_path)
if os.access(path_like, os.R_OK) and path_like.is_file():
module = self._load_module_from_file(package_or_path, class_name)
else:
module = self._load_module_from_package(package_or_path)

View File

@ -58,6 +58,15 @@ def test_load_trigger_path(trigger_loader: TriggerLoader, resource_path_root: Pa
assert trigger_loader._load_trigger(f"{path}.ReportTrigger")
def test_load_trigger_path_directory(trigger_loader: TriggerLoader, resource_path_root: Path) -> None:
"""
must raise InvalidExtension if provided import path is directory
"""
path = resource_path_root.parent.parent / "src" / "ahriman" / "core" / "report"
with pytest.raises(InvalidExtension):
trigger_loader._load_trigger(f"{path}.ReportTrigger")
def test_load_trigger_path_not_found(trigger_loader: TriggerLoader) -> None:
"""
must raise InvalidExtension if file cannot be found