Skip to content

Commit

Permalink
Merge branch 'main' into pre-commit-ci-update-config
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruchip16 authored Aug 2, 2023
2 parents f002d88 + 9aa8325 commit 987a4b5
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
other_names: |
lint
pkg
devel
py310-devel
platforms: linux

build:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pytest \

## Inventory

Using ansible first starts with defining your inventory. This can be done
Using ansible first starts with defining your inventory. This can be done in
several ways, but to start, we'll use the `ansible_adhoc` fixture.

```python
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ disable = [
"invalid-name",
]

[tool.pytest]
[tool.pytest.ini_options]
minversion = 6.0
addopts = ["-v", "--tb=native"]
markers = [
Expand Down
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 987a4b5

Please sign in to comment.