From 236e4b4d21f590b991cc615400dcbd3a07e7dacf Mon Sep 17 00:00:00 2001 From: Kate Case Date: Fri, 13 Dec 2024 11:53:19 -0500 Subject: [PATCH] Add some sanity checking to the detected galaxy.yml file --- src/molecule/config.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/molecule/config.py b/src/molecule/config.py index 40c54251a6..35aa5dd68c 100644 --- a/src/molecule/config.py +++ b/src/molecule/config.py @@ -26,7 +26,7 @@ import warnings from pathlib import Path -from typing import TYPE_CHECKING, cast +from typing import TYPE_CHECKING from uuid import uuid4 from ansible_compat.ports import cache, cached_property @@ -38,7 +38,6 @@ from molecule.dependency import ansible_galaxy, shell from molecule.model import schema_v3 from molecule.provisioner import ansible -from molecule.types import CollectionData from molecule.util import boolean @@ -49,7 +48,7 @@ from molecule.dependency.base import Base as Dependency from molecule.driver.base import Driver from molecule.state import State - from molecule.types import CommandArgs, ConfigData, MoleculeArgs + from molecule.types import CollectionData, CommandArgs, ConfigData, MoleculeArgs from molecule.verifier.base import Verifier @@ -270,12 +269,23 @@ def collection(self) -> CollectionData | None: Returns: A dictionary of information about the collection molecule is running inside, if any. """ - if not self.collection_directory: + collection_directory = self.collection_directory + if not collection_directory: return None - galaxy_file = self.collection_directory / "galaxy.yml" - galaxy_data = util.safe_load_file(galaxy_file) - return cast(CollectionData, galaxy_data) + galaxy_file = collection_directory / "galaxy.yml" + galaxy_data: CollectionData = util.safe_load_file(galaxy_file) + + important_keys = {"name", "namespace"} + if missing_keys := important_keys.difference(galaxy_data.keys()): + LOG.warning( + "The detected galaxy.yml file (%s) is incomplete, missing %s", + galaxy_file, + missing_keys, + ) + return None + + return galaxy_data @cached_property def dependency(self) -> Dependency | None: