From dadeda185219e88653b72be1f347681359198a7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A9ri=20Le=20Bouder?= Date: Tue, 20 Dec 2022 16:22:02 -0500 Subject: [PATCH] get_module_from_config: always return a dict (#16) get_module_from_config: always return a dict Depends-On: ansible-collections/vmware.vmware_rest#377 get_module_from_config was returning an empty dict if the configuration for one module just hold an empty dict. This was also the case when a module was not present in the configuration. As a consequence it was not possible to make the difference between those that were actually present (trusted) and the others. Reviewed-by: GomathiselviS --- gouttelette/utils.py | 14 ++++++++------ tests/unit/utils/test_utils.py | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 6 deletions(-) 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