diff --git a/src/ansible_compat/runtime.py b/src/ansible_compat/runtime.py index f53191fb..6188a396 100644 --- a/src/ansible_compat/runtime.py +++ b/src/ansible_compat/runtime.py @@ -16,7 +16,6 @@ import subprocess_tee from packaging.version import Version -from packaging.version import parse as parse_version from ansible_compat.config import ( AnsibleConfig, @@ -58,6 +57,18 @@ class Collection: path: Path +class CollectionVersion(Version): + """Collection version.""" + + def __init__(self, version: str) -> None: + """Initialize collection version.""" + # As packaging Version class does not support wildcard, we convert it + # to "0", as this being the smallest version possible. + if version == "*": + version = "0" + super().__init__(version) + + # pylint: disable=too-many-instance-attributes class Runtime: """Ansible Runtime manager.""" @@ -191,7 +202,7 @@ def _ensure_module_available(self) -> None: msg = "Unable to find Ansible python module." raise RuntimeError(msg) - ansible_module_version = parse_version( + ansible_module_version = Version( ansible_release_module.__version__, ) if ansible_module_version != self.version: @@ -326,7 +337,7 @@ def install_collection( # if the range requires a pre-release, we need to manuall add the --pre # flag when needed. matches = version_re.search(str(collection)) - if matches and Version(matches[1]).is_prerelease: + if matches and CollectionVersion(matches[1]).is_prerelease: cmd.append("--pre") cpaths: list[str] = self.config.collections_paths @@ -592,10 +603,10 @@ def require_collection( with mpath.open(encoding="utf-8") as f: manifest = json.loads(f.read()) - found_version = parse_version( + found_version = CollectionVersion( manifest["collection_info"]["version"], ) - if version and found_version < parse_version(version): + if version and found_version < CollectionVersion(version): if install: self.install_collection(f"{name}:>={version}") self.require_collection(name, version, install=False) diff --git a/test/collections/acme.goodies/galaxy.yml b/test/collections/acme.goodies/galaxy.yml index 477f40a3..901429b2 100644 --- a/test/collections/acme.goodies/galaxy.yml +++ b/test/collections/acme.goodies/galaxy.yml @@ -6,7 +6,8 @@ authors: - Red Hat description: Sample collection to use with molecule dependencies: - community.molecule: ">=0.1.0" + community.molecule: ">=0.1.0" # used to also test '=>' condition + ansible.utils: "*" # used to also test '*' build_ignore: - "*.egg-info" - .DS_Store diff --git a/test/test_runtime.py b/test/test_runtime.py index d9f76a52..c45b4660 100644 --- a/test/test_runtime.py +++ b/test/test_runtime.py @@ -640,7 +640,11 @@ def test_runtime_version_in_range( pytest.param( "test/collections/acme.goodies", "default", - ["ansible.posix"], + [ + "ansible.posix", # from tests/requirements.yml + "ansible.utils", # from galaxy.yml + "community.molecule", # from galaxy.yml + ], id="normal", ), pytest.param(