Skip to content

Commit

Permalink
Allow use of * version on dependencies (#268)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea committed May 15, 2023
1 parent f93bf7c commit 291d775
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
21 changes: 16 additions & 5 deletions src/ansible_compat/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion test/collections/acme.goodies/galaxy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion test/test_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 291d775

Please sign in to comment.