mirror of
https://github.com/arcan1s/ahriman.git
synced 2025-04-24 07:17:17 +00:00
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:
parent
f42c2c57fb
commit
04dcaa93ab
@ -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:
|
||||
@ -120,12 +123,16 @@ class TriggerLoader:
|
||||
|
||||
Returns:
|
||||
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)
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user