8000 Fail if old ANSIBLE_COLLECTIONS_PATHS is detected by ssbarnea · Pull Request #449 · ansible/ansible-compat · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fail if old ANSIBLE_COLLECTIONS_PATHS is detected #449

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

Merged
merged 1 commit into from
Jan 22, 2025
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
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()
Loading
10B
0