Skip to content

Commit

Permalink
Correctly identify the installed collections when duplicates exist (#379
Browse files Browse the repository at this point in the history
)

This fixes a bug where where load_collections would load the latest
collection found instead of the first one.
  • Loading branch information
ssbarnea authored May 28, 2024
1 parent a0808b6 commit 29cfc62
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ changelog = "https://github.com/ansible/ansible-compat/releases"

[tool.coverage.report]
exclude_lines = ["pragma: no cover", "if TYPE_CHECKING:"]
fail_under = 92
fail_under = 100
skip_covered = true
show_missing = true

Expand Down Expand Up @@ -346,6 +346,7 @@ filterwarnings = [
testpaths = ["test"]

[tool.ruff]
extend-include = ["src/ansible_compat/_version.py"]
target-version = "py39"
lint.select = ["ALL"]
lint.ignore = [
Expand Down
26 changes: 20 additions & 6 deletions src/ansible_compat/runtime.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Ansible runtime environment manager."""

# pylint: disable=too-many-lines

from __future__ import annotations

import contextlib
Expand Down Expand Up @@ -271,7 +273,15 @@ def load_collections(self) -> None:
self.collections = OrderedDict()
no_collections_msg = "None of the provided paths were usable"

proc = self.run(["ansible-galaxy", "collection", "list", "--format=json"])
proc = self.run(
[
"ansible-galaxy",
"collection",
"list",
"--format=json",
f"-p={':'.join(self.config.collections_paths)}",
],
)
if proc.returncode == RC_ANSIBLE_OPTIONS_ERROR and (
no_collections_msg in proc.stdout or no_collections_msg in proc.stderr
): # pragma: no cover
Expand All @@ -298,11 +308,15 @@ def load_collections(self) -> None:
msg = f"Unexpected collection data, {collection_info}"
raise TypeError(msg)

self.collections[collection] = Collection(
name=collection,
version=collection_info["version"],
path=path,
)
if collection in self.collections:
msg = f"Multiple versions of '{collection}' were found installed, only the first one will be used, {self.collections[collection].version} ({self.collections[collection].path})."
logging.warning(msg)
else:
self.collections[collection] = Collection(
name=collection,
version=collection_info["version"],
path=path,
)

def _ensure_module_available(self) -> None:
"""Assure that Ansible Python module is installed and matching CLI version."""
Expand Down
13 changes: 10 additions & 3 deletions test/test_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,9 +449,16 @@ def test_require_collection_preexisting_broken(runtime_tmp: Runtime) -> None:
runtime_tmp.require_collection("foo.bar")


def test_require_collection(runtime_tmp: Runtime) -> None:
"""Check that require collection successful install case."""
runtime_tmp.require_collection("community.molecule", "0.1.0")
def test_require_collection_install(runtime_tmp: Runtime) -> None:
"""Check that require collection successful install case, including upgrade path."""
runtime_tmp.install_collection("ansible.posix:==1.5.2")
runtime_tmp.load_collections()
collection = runtime_tmp.collections["ansible.posix"]
assert collection.version == "1.5.2"
runtime_tmp.require_collection(name="ansible.posix", version="1.5.4", install=True)
runtime_tmp.load_collections()
collection = runtime_tmp.collections["ansible.posix"]
assert collection.version == "1.5.4"


@pytest.mark.parametrize(
Expand Down

0 comments on commit 29cfc62

Please sign in to comment.