Skip to content

Commit

Permalink
Fail if old ANSIBLE_COLLECTIONS_PATHS is detected (#449)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea authored Jan 22, 2025
1 parent b5c3067 commit 709cea0
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 58 deletions.
45 changes: 22 additions & 23 deletions .config/pydoclint-baseline.txt

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ exclude: |
)$
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: "v0.8.6"
rev: "v0.9.2"
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
Expand Down Expand Up @@ -54,13 +54,16 @@ repos:
- id: debug-statements
language_version: python3
- repo: https://github.com/codespell-project/codespell
rev: v2.3.0
rev: v2.4.0
hooks:
- id: codespell
- repo: https://github.com/jsh9/pydoclint
rev: 0.5.9
rev: 0.6.0
hooks:
- id: pydoclint
# This allows automatic reduction of the baseline file when needed.
entry: sh -ec "pydoclint . && pydoclint --generate-baseline=1 ."
pass_filenames: false
- repo: https://github.com/adrienverge/yamllint.git
rev: v1.35.1
hooks:
Expand Down
13 changes: 0 additions & 13 deletions src/ansible_compat/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,6 @@
from pathlib import Path


# do not use lru_cache here, as environment can change between calls
def ansible_collections_path() -> str:
"""Return collection path variable for current version of Ansible."""
for env_var in [
"ANSIBLE_COLLECTIONS_PATH",
"ANSIBLE_COLLECTIONS_PATHS",
]:
if env_var in os.environ:
return env_var
return "ANSIBLE_COLLECTIONS_PATH"


def parse_ansible_version(stdout: str) -> Version:
"""Parse output of 'ansible --version'."""
# Ansible can produce extra output before displaying version in debug mode.
Expand Down Expand Up @@ -477,7 +465,6 @@ def __deepcopy__(self, memo: object) -> AnsibleConfig:

__all__ = [
"AnsibleConfig",
"ansible_collections_path",
"ansible_version",
"parse_ansible_version",
]
10 changes: 6 additions & 4 deletions src/ansible_compat/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

from ansible_compat.config import (
AnsibleConfig,
ansible_collections_path,
parse_ansible_version,
)
from ansible_compat.constants import (
Expand Down Expand Up @@ -195,6 +194,9 @@ def __init__(
self.isolated = isolated
self.max_retries = max_retries
self.environ = environ or os.environ.copy()
if "ANSIBLE_COLLECTIONS_PATHS" in self.environ:
msg = "ANSIBLE_COLLECTIONS_PATHS was detected, replace it with ANSIBLE_COLLECTION_PATH to continue."
raise RuntimeError(msg)
self.plugins = Plugins(runtime=self)
self.verbosity = verbosity

Expand Down Expand Up @@ -505,7 +507,7 @@ def install_collection(
cpaths: list[str] = self.config.collections_paths
if destination and str(destination) not in cpaths:
# we cannot use '-p' because it breaks galaxy ability to ignore already installed collections, so
# we hack ansible_collections_path instead and inject our own path there.
# we hack ANSIBLE_COLLECTION_PATH instead and inject our own path there.
# pylint: disable=no-member
cpaths.insert(0, str(destination))
cmd.append(f"{collection}")
Expand All @@ -514,7 +516,7 @@ def install_collection(
process = self.run(
cmd,
retry=True,
env={**self.environ, ansible_collections_path(): ":".join(cpaths)},
env={**self.environ, "ANSIBLE_COLLECTION_PATH": ":".join(cpaths)},
)
if process.returncode != 0:
msg = f"Command {' '.join(cmd)}, returned {process.returncode} code:\n{process.stdout}\n{process.stderr}"
Expand Down Expand Up @@ -822,7 +824,7 @@ def _prepare_ansible_paths(self) -> None:
if library_paths != self.config.DEFAULT_MODULE_PATH:
self._update_env("ANSIBLE_LIBRARY", library_paths)
if collections_path != self.config.default_collections_path:
self._update_env(ansible_collections_path(), collections_path)
self._update_env("ANSIBLE_COLLECTION_PATH", collections_path)
if roles_path != self.config.default_roles_path:
self._update_env("ANSIBLE_ROLES_PATH", roles_path)

Expand Down
15 changes: 0 additions & 15 deletions test/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

from ansible_compat.config import (
AnsibleConfig,
ansible_collections_path,
ansible_version,
parse_ansible_version,
)
Expand Down Expand Up @@ -90,17 +89,3 @@ def test_ansible_version() -> None:
def test_ansible_version_arg() -> None:
"""Validate ansible_version behavior."""
assert ansible_version("2.0") >= Version("1.0")


@pytest.mark.parametrize(
"var",
("", "ANSIBLE_COLLECTIONS_PATH", "ANSIBLE_COLLECTIONS_PATHS"),
ids=["blank", "singular", "plural"],
)
def test_ansible_collections_path_env(var: str, monkeypatch: MonkeyPatch) -> None:
"""Test that ansible_collections_path returns the appropriate env var."""
# Set the variable
if var:
monkeypatch.setenv(var, "")

assert ansible_collections_path() == (var or "ANSIBLE_COLLECTIONS_PATH")
10 changes: 10 additions & 0 deletions test/test_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -1030,3 +1030,13 @@ def test_runtime_has_playbook() -> None:
assert not runtime.has_playbook("this-does-not-exist.yml", basedir=Path())
# this is part of community.molecule collection
assert runtime.has_playbook("community.molecule.validate.yml")


def test_runtime_exception(monkeypatch: pytest.MonkeyPatch) -> None:
"""Asserts that we raise a runtime exception if unsupported environment variable is detected."""
monkeypatch.setenv("ANSIBLE_COLLECTIONS_PATHS", "foo")
with pytest.raises(
RuntimeError,
match=r"ANSIBLE_COLLECTIONS_PATHS was detected, replace it with ANSIBLE_COLLECTION_PATH to continue.",
):
Runtime()

0 comments on commit 709cea0

Please sign in to comment.