From a633af3c9f343eda84c7b96f1830bb441cf900ae Mon Sep 17 00:00:00 2001 From: Kate Case Date: Fri, 13 Dec 2024 13:39:14 -0500 Subject: [PATCH] Add test for broken galaxy.yml detection --- src/molecule/config.py | 4 ++-- .../resources/broken-collection/galaxy.yml | 3 +++ tests/unit/test_config.py | 24 +++++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 tests/fixtures/resources/broken-collection/galaxy.yml diff --git a/src/molecule/config.py b/src/molecule/config.py index 1e56ebdf3a..662b4ee107 100644 --- a/src/molecule/config.py +++ b/src/molecule/config.py @@ -279,11 +279,11 @@ def collection(self) -> CollectionData | None: 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", + "The detected galaxy.yml file (%s) is invalid, missing mandatory field %s", galaxy_file, util.oxford_comma(missing_keys), ) - return None + return None # pragma: no cover return galaxy_data diff --git a/tests/fixtures/resources/broken-collection/galaxy.yml b/tests/fixtures/resources/broken-collection/galaxy.yml new file mode 100644 index 0000000000..2a8c38e487 --- /dev/null +++ b/tests/fixtures/resources/broken-collection/galaxy.yml @@ -0,0 +1,3 @@ +name: baddies +name_space: acme +version: 1.0.0 diff --git a/tests/unit/test_config.py b/tests/unit/test_config.py index bddde78e12..9024e903e5 100644 --- a/tests/unit/test_config.py +++ b/tests/unit/test_config.py @@ -139,6 +139,30 @@ def test_collection_property( assert modified_instance.collection["namespace"] == "acme" +def test_collection_property_broken_collection( + caplog: pytest.LogCaptureFixture, + config_instance: config.Config, + resources_folder_path: Path, +) -> None: + """Test collection property with a malformed galaxy.yml. + + Args: + caplog: pytest log capture fixture. + config_instance: Instance of Config. + resources_folder_path: Path to resources directory holding a valid collection. + """ + modified_instance = copy.copy(config_instance) + + # Alter config_instance to start at path of a collection + collection_path = resources_folder_path / "broken-collection" + modified_instance.project_directory = str(collection_path) + + assert modified_instance.collection is None + + msg = "missing mandatory field 'namespace'" + assert msg in caplog.text + + def test_dependency_property(config_instance: config.Config) -> None: # noqa: D103 assert isinstance(config_instance.dependency, ansible_galaxy.AnsibleGalaxy)