Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/pip/dot-config/ansible-core-2.15.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruchip16 committed Aug 2, 2023
2 parents fc8b589 + 9aa8325 commit 2917c9d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/pytest_ansible/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def pytest_addoption(parser):
"--ansible-unit-inject-only",
action="store_true",
default=False,
help="Enable support for ansible collection unit tests by only injecting exisiting ANSIBLE_COLLECTIONS_PATHS.",
help="Enable support for ansible collection unit tests by only injecting exisiting ANSIBLE_COLLECTIONS_PATH.",
)
# Add github marker to --help
parser.addini("ansible", "Ansible integration", "args")
Expand Down
27 changes: 22 additions & 5 deletions src/pytest_ansible/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def inject(start_path: Path) -> None:

logger.info("Collections dir: %s", collections_dir)

# TODO: Make this a configuration option, check COLLECTIONS_PATHS
# TODO: Make this a configuration option, check COLLECTIONS_PATH
# Add the user location for any dependencies
paths = [str(collections_dir), "~/.ansible/collections"]
logger.info("Paths: %s", paths)
Expand All @@ -119,14 +119,17 @@ def inject(start_path: Path) -> None:
# e.g. ansible-galaxy collection install etc

# Set the environment variable as courtesy for integration tests

envvar_name = determine_envvar()
env_paths = os.pathsep.join(paths)
logger.info("Setting ANSIBLE_COLLECTIONS_PATH to %s", env_paths)
os.environ["ANSIBLE_COLLECTIONS_PATHS"] = env_paths
logger.info("Setting %s to %s", envvar_name, env_paths)
os.environ[envvar_name] = env_paths


def inject_only() -> None:
"""Inject the current ANSIBLE_COLLECTIONS_PATHS."""
env_paths = os.environ.get("ANSIBLE_COLLECTIONS_PATHS", "")
"""Inject the current ANSIBLE_COLLECTIONS_PATH(S)."""
envvar_name = determine_envvar()
env_paths = os.environ.get(envvar_name, "")
path_list = env_paths.split(os.pathsep)
for path in path_list:
if path:
Expand All @@ -148,3 +151,17 @@ def acf_inject(paths: list[str]) -> None:
logger.debug("_ACF configured paths: %s", acf._n_configured_paths)
else:
logger.debug("_ACF not available")


def determine_envvar() -> str:
"""Use the existence of the AnsibleCollectionFinder to determine
the ansible version.
ansible 2.9 did not have AnsibleCollectionFinder and did not support ANSIBLE_COLLECTIONS_PATH
later versions do.
:returns: The appropriate environment variable to use
"""
if not HAS_COLLECTION_FINDER:
return "ANSIBLE_COLLECTIONS_PATHS"
return "ANSIBLE_COLLECTIONS_PATH"
2 changes: 1 addition & 1 deletion tests/unit/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def test_inject_only(
:param caplog: The pytest caplog fixture
"""
caplog.set_level(logging.DEBUG)
monkeypatch.setenv("ANSIBLE_COLLECTIONS_PATHS", str(tmp_path / "collections"))
monkeypatch.setenv("ANSIBLE_COLLECTIONS_PATH", str(tmp_path / "collections"))

(tmp_path / "collections" / "ansible_collections").mkdir(parents=True)

Expand Down

0 comments on commit 2917c9d

Please sign in to comment.