Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly identify the installed collections when duplicates exist #379

Merged
merged 3 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading