diff --git a/gouttelette/utils.py b/gouttelette/utils.py index f04bb34..3990363 100644 --- a/gouttelette/utils.py +++ b/gouttelette/utils.py @@ -67,13 +67,13 @@ def indent(text_block: str, indent: int = 0) -> str: return result -def get_module_from_config(module: str, generator: str) -> Any: +def get_module_from_config(module: str, generator: str) -> dict[str, Any]: raw_content = pkg_resources.resource_string(generator, "config/modules.yaml") for i in yaml.safe_load(raw_content): if module in i: - return i[module] - return {} + return i[module] or {} + raise KeyError def python_type(value: str) -> str: @@ -95,10 +95,12 @@ class UtilsBase: name: str def is_trusted(self, generator: str) -> bool: - if get_module_from_config(self.name, generator): + try: + get_module_from_config(self.name, generator) return True - print(f"- do not build: {self.name}") - return False + except KeyError: + print(f"- do not build: {self.name}") + return False def write_module(self, target_dir: Path, content: str) -> None: module_dir = target_dir / "plugins" / "modules" diff --git a/tests/unit/utils/test_utils.py b/tests/unit/utils/test_utils.py index 4950224..f8c036b 100644 --- a/tests/unit/utils/test_utils.py +++ b/tests/unit/utils/test_utils.py @@ -1,7 +1,10 @@ #!/usr/bin/env python3 +import pkg_resources from gouttelette import utils +from unittest.mock import patch + def test_format_documentaion(): input_arg = { @@ -47,3 +50,14 @@ def test_python_type(): assert utils.python_type("boolean") == "bool" assert utils.python_type(["object", "string"]) == "dict" assert utils.python_type(["string", "object"]) == "str" + + +@patch("pkg_resources.resource_string") +def test_UtilsBase_is_trusted(m_resource_string): + m_resource_string.return_value = "---\n- foo:\n- bar:\n some: key\n" + module = utils.UtilsBase("no-trusted-pantoute") + assert module.is_trusted("a-generator") is False + module = utils.UtilsBase("foo") + assert module.is_trusted("a-generator") is True + module = utils.UtilsBase("bar") + assert module.is_trusted("a-generator") is True