Skip to content

Commit 291d775

Browse files
authored
Allow use of * version on dependencies (#268)
Related: ansible/ansible-lint#2924
1 parent f93bf7c commit 291d775

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

src/ansible_compat/runtime.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import subprocess_tee
1818
from packaging.version import Version
19-
from packaging.version import parse as parse_version
2019

2120
from ansible_compat.config import (
2221
AnsibleConfig,
@@ -58,6 +57,18 @@ class Collection:
5857
path: Path
5958

6059

60+
class CollectionVersion(Version):
61+
"""Collection version."""
62+
63+
def __init__(self, version: str) -> None:
64+
"""Initialize collection version."""
65+
# As packaging Version class does not support wildcard, we convert it
66+
# to "0", as this being the smallest version possible.
67+
if version == "*":
68+
version = "0"
69+
super().__init__(version)
70+
71+
6172
# pylint: disable=too-many-instance-attributes
6273
class Runtime:
6374
"""Ansible Runtime manager."""
@@ -191,7 +202,7 @@ def _ensure_module_available(self) -> None:
191202
msg = "Unable to find Ansible python module."
192203
raise RuntimeError(msg)
193204

194-
ansible_module_version = parse_version(
205+
ansible_module_version = Version(
195206
ansible_release_module.__version__,
196207
)
197208
if ansible_module_version != self.version:
@@ -326,7 +337,7 @@ def install_collection(
326337
# if the range requires a pre-release, we need to manuall add the --pre
327338
# flag when needed.
328339
matches = version_re.search(str(collection))
329-
if matches and Version(matches[1]).is_prerelease:
340+
if matches and CollectionVersion(matches[1]).is_prerelease:
330341
cmd.append("--pre")
331342

332343
cpaths: list[str] = self.config.collections_paths
@@ -592,10 +603,10 @@ def require_collection(
592603

593604
with mpath.open(encoding="utf-8") as f:
594605
manifest = json.loads(f.read())
595-
found_version = parse_version(
606+
found_version = CollectionVersion(
596607
manifest["collection_info"]["version"],
597608
)
598-
if version and found_version < parse_version(version):
609+
if version and found_version < CollectionVersion(version):
599610
if install:
600611
self.install_collection(f"{name}:>={version}")
601612
self.require_collection(name, version, install=False)

test/collections/acme.goodies/galaxy.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ authors:
66
- Red Hat
77
description: Sample collection to use with molecule
88
dependencies:
9-
community.molecule: ">=0.1.0"
9+
community.molecule: ">=0.1.0" # used to also test '=>' condition
10+
ansible.utils: "*" # used to also test '*'
1011
build_ignore:
1112
- "*.egg-info"
1213
- .DS_Store

test/test_runtime.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,11 @@ def test_runtime_version_in_range(
640640
pytest.param(
641641
"test/collections/acme.goodies",
642642
"default",
643-
["ansible.posix"],
643+
[
644+
"ansible.posix", # from tests/requirements.yml
645+
"ansible.utils", # from galaxy.yml
646+
"community.molecule", # from galaxy.yml
647+
],
644648
id="normal",
645649
),
646650
pytest.param(

0 commit comments

Comments
 (0)