Skip to content

Commit

Permalink
Test Discovery considers global molecule config (#258)
Browse files Browse the repository at this point in the history
* fix: discovery considers global molecule config

* chore: auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix: linting

* chore: auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Fixes

* fix: change the way the global molecule config is loaded

* chore: auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix: avoid catching generic Exceptions

* chore: auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* test: fix path concat

* chore: auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* test: make ruff happy

* chore: auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* test: ruff again

* chore: auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* test: ruff

---------

Co-authored-by: Martin Chmielewski <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Sorin Sbarnea <[email protected]>
Co-authored-by: Sorin Sbarnea <[email protected]>
  • Loading branch information
5 people authored Apr 22, 2024
1 parent 320a7ff commit d357f58
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 38 deletions.
85 changes: 48 additions & 37 deletions src/pytest_ansible/molecule.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,44 +135,55 @@ def __init__(self, name, parent) -> None:
"""Construct MoleculeItem."""
self.funcargs = {}
super().__init__(name, parent)
molecule_yml = self.path
with Path(molecule_yml).open(encoding="utf-8") as stream:
# If the molecule.yml file is empty, YAML loader returns None. To
# simplify things down the road, we replace None with an empty
# dict.
data = yaml.load(stream, Loader=yaml.SafeLoader) or {}

# we add the driver as mark
self.molecule_driver = data.get("driver", {}).get("name", "no_driver")
self.add_marker(self.molecule_driver)

# check for known markers and add them
markers = data.get("markers", [])
if "xfail" in markers:
self.add_marker(
pytest.mark.xfail(
reason="Marked as broken by scenario configuration.",
),
)
if "skip" in markers:
self.add_marker(
pytest.mark.skip(reason="Disabled by scenario configuration."),
)

# we also add platforms as marks
for platform in data.get("platforms", []):
platform_name = platform["name"]
self.config.addinivalue_line(
"markers",
f"{platform_name}: molecule platform name is {platform_name}",
)
self.add_marker(platform_name)
self.add_marker("molecule")
if (
self.config.option.molecule_unavailable_driver
and not self.config.option.molecule[self.molecule_driver]["available"]
):
self.add_marker(self.config.option.molecule_unavailable_driver)
# Determine molecule scenario
scenario_molecule_yml = self.path
data_scenario = self.yaml_loader(scenario_molecule_yml)
# check if there is a global molecule config
try:
data_global = self.yaml_loader(
Path(Path.cwd()) / ".config/molecule/config.yml",
)
data = data_global | data_scenario
except FileNotFoundError:
data = data_scenario

# we add the driver as mark
self.molecule_driver = data.get("driver", {}).get("name", "no_driver")
self.add_marker(self.molecule_driver)

# check for known markers and add them
markers = data.get("markers", [])
if "xfail" in markers:
self.add_marker(
pytest.mark.xfail(
reason="Marked as broken by scenario configuration.",
),
)
if "skip" in markers:
self.add_marker(
pytest.mark.skip(reason="Disabled by scenario configuration."),
)

# we also add platforms as marks
for platform in data.get("platforms", []):
platform_name = platform["name"]
self.config.addinivalue_line(
"markers",
f"{platform_name}: molecule platform name is {platform_name}",
)
self.add_marker(platform_name)
self.add_marker("molecule")
if (
self.config.option.molecule_unavailable_driver
and not self.config.option.molecule[self.molecule_driver]["available"]
):
self.add_marker(self.config.option.molecule_unavailable_driver)

def yaml_loader(self, filepath: str) -> dict:
"""Load a yaml file at a given filepath."""
with Path.open(filepath, encoding="utf-8") as file_descriptor:
return yaml.safe_load(file_descriptor) or {}

def runtest(self):
"""Perform effective test run."""
Expand Down
1 change: 0 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ skip_install = true
deps =
build >= 0.9.0
twine >= 4.0.2 # pyup: ignore
setenv =
commands =
rm -rfv {toxinidir}/dist/
python3 -m build \
Expand Down

0 comments on commit d357f58

Please sign in to comment.