2021-09-24 09:01:54 +00:00
|
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
import yaml
|
|
|
|
import re
|
2021-09-28 14:51:35 +00:00
|
|
|
from textwrap import dedent
|
2021-09-24 09:01:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _get_workflow_names():
|
|
|
|
"""Get all names of all workflows which have a test.yml in the tests directory.
|
|
|
|
|
|
|
|
To do so, recursively finds all test.yml files and parses their content.
|
|
|
|
"""
|
|
|
|
here = Path(__file__).parent.resolve()
|
2021-10-08 15:02:42 +00:00
|
|
|
pytest_workflow_files = here.glob("modules/**/test.yml")
|
2021-09-24 09:01:54 +00:00
|
|
|
for f in pytest_workflow_files:
|
2021-10-06 06:16:36 +00:00
|
|
|
# test_config = yaml.safe_load(f.read_text())
|
|
|
|
test_config = yaml.load(f.read_text(), Loader=yaml.BaseLoader)
|
2021-09-24 09:01:54 +00:00
|
|
|
for workflow in test_config:
|
2022-06-10 09:46:14 +00:00
|
|
|
# https://github.com/nf-core/modules/pull/1242 - added to cover tests
|
2022-01-28 09:32:36 +00:00
|
|
|
# that expect an error and therefore will not generate a versions.yml
|
2022-06-10 09:46:14 +00:00
|
|
|
if 'exit_code' not in workflow:
|
2022-01-28 09:32:36 +00:00
|
|
|
yield workflow["name"]
|
2021-09-24 09:01:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.workflow(*_get_workflow_names())
|
|
|
|
def test_ensure_valid_version_yml(workflow_dir):
|
|
|
|
workflow_dir = Path(workflow_dir)
|
|
|
|
software_name = workflow_dir.name.split("_")[0].lower()
|
2021-09-28 14:51:35 +00:00
|
|
|
try:
|
|
|
|
versions_yml_file = workflow_dir / f"output/{software_name}/versions.yml"
|
|
|
|
versions_yml = versions_yml_file.read_text()
|
|
|
|
except FileNotFoundError:
|
|
|
|
raise AssertionError(
|
|
|
|
dedent(
|
|
|
|
f"""\
|
|
|
|
`versions.yml` not found in the output directory.
|
|
|
|
Expected path: `{versions_yml_file}`
|
|
|
|
|
|
|
|
This can have multiple reasons:
|
|
|
|
* The test-workflow failed before a `versions.yml` could be generated.
|
|
|
|
* The workflow name in `test.yml` does not start with the tool name.
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
)
|
2021-09-24 09:01:54 +00:00
|
|
|
|
|
|
|
assert (
|
|
|
|
"END_VERSIONS" not in versions_yml
|
2021-09-27 08:40:50 +00:00
|
|
|
), "END_VERSIONS detected in versions.yml. This is a sign of an ill-formatted HEREDOC"
|
2021-09-24 09:01:54 +00:00
|
|
|
|
|
|
|
# Raises an exception if yaml is not valid
|
|
|
|
versions = yaml.safe_load(versions_yml)
|
2021-09-28 14:51:35 +00:00
|
|
|
assert (
|
|
|
|
len(versions) == 1
|
|
|
|
), "The top-level of versions.yml must contain exactly one entry: the process name as dict key"
|
2021-09-27 08:40:50 +00:00
|
|
|
software_versions = next(iter(versions.values()))
|
2021-09-24 09:01:54 +00:00
|
|
|
assert len(software_versions), "There must be at least one version emitted."
|
|
|
|
for tool, version in software_versions.items():
|
|
|
|
assert re.match(
|
2022-06-10 10:56:56 +00:00
|
|
|
r"^\d.*|^[a-f0-9]{40}$", str(version)
|
2022-06-10 11:01:40 +00:00
|
|
|
), f"Version number for {tool} must start with a number, or be a Git SHA commit id. "
|