Skip to content

Commit

Permalink
Set XDG_CACHE_HOME (ansible#208)
Browse files Browse the repository at this point in the history
* Set XDG_CACHE_HOME

* Delete settings.json

* Fomatting fix

* Empty environ

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Fix for type env

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
cidrblock and pre-commit-ci[bot] committed Aug 9, 2023
1 parent dcc3ef5 commit 91cbc9c
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .config/dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ endgroup
envtmpdir
envtmpdir
fileh
fixturenames
metafunc
nocolor
passenv
setenv
Expand Down
8 changes: 6 additions & 2 deletions src/tox_ansible/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,7 @@ def conf_setenv(env_conf: EnvConfigSet) -> str:
ansible 2.9 did not support the ANSIBLE_COLLECTION_PATH environment variable
ansible 2.16 has it marked for deprecation in 2.19
Set the XDG_CACHE_HOME to the environment directory to isolate it
:param env_conf: The environment configuration.
:return: The set environment variables.
Expand All @@ -544,6 +545,9 @@ def conf_setenv(env_conf: EnvConfigSet) -> str:
else:
envvar_name = "ANSIBLE_COLLECTIONS_PATH"
envtmpdir = env_conf["envtmpdir"]
setenv = []
setenv.append(f"{envvar_name}={envtmpdir}/collections/")

setenv = [
f"{envvar_name}={envtmpdir}/collections/",
f"XDG_CACHE_HOME={env_conf['env_dir']}/.cache",
]
return "\n".join(setenv)
69 changes: 69 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
"""Global testing fixtures."""

from __future__ import annotations

import configparser
import os
import subprocess
import sys

from dataclasses import dataclass
from pathlib import Path
from typing import TYPE_CHECKING

import pytest


if TYPE_CHECKING:
from _pytest.python import Metafunc


@pytest.fixture(scope="module")
def module_fixture_dir(request: pytest.FixtureRequest) -> Path:
"""Provide a module specific fixture directory.
Expand All @@ -27,3 +40,59 @@ def _tox_in_tox(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.delenv("TOX_ENV_NAME", raising=False)
monkeypatch.delenv("TOX_WORK_DIR", raising=False)
monkeypatch.delenv("TOX_ENV_DIR", raising=False)


@dataclass
class BasicEnvironment:
"""An structure for an environment."""

name: str
config: dict[str, str]


def pytest_generate_tests(metafunc: Metafunc) -> None:
"""Parametrize the basic environments and there configurations.
:param metafunc: Metadata for the test
"""
if "basic_environment" in metafunc.fixturenames:
cwd = Path(__file__).parent
basic_dir = cwd / "fixtures" / "integration" / "test_basic"
try:
cmd = (
f"{sys.executable} -m tox config --ansible "
f"--root {basic_dir} --conf tox-ansible.ini"
)
env = os.environ
env.pop("TOX_ENV_DIR", None)
env.pop("TOX_ENV_NAME", None)
env.pop("TOX_WORK_DIR", None)

proc = subprocess.run(
args=cmd,
capture_output=True,
check=True,
cwd=str(basic_dir),
env=env,
shell=True,
text=True,
)
except subprocess.CalledProcessError as exc:
print(exc.stdout)
print(exc.stderr)
pytest.fail(exc.stderr)

configs = configparser.ConfigParser()
configs.read_string(proc.stdout)

assert configs.sections()

environment_configs = [
BasicEnvironment(name=name, config=dict(configs[name])) for name in configs.sections()
]

metafunc.parametrize(
"basic_environment",
environment_configs,
ids=configs.sections(),
)
34 changes: 33 additions & 1 deletion tests/integration/test_basic.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
"""Basic tests."""

from __future__ import annotations

import json
import subprocess

from pathlib import Path
from typing import TYPE_CHECKING

import pytest


if TYPE_CHECKING:
from pathlib import Path

from ..conftest import BasicEnvironment


def test_ansible_environments(module_fixture_dir: Path) -> None:
"""Test that the ansible environments are available.
Expand Down Expand Up @@ -60,3 +69,26 @@ def test_gh_matrix(
assert isinstance(entry["factors"], list)
assert isinstance(entry["name"], str)
assert isinstance(entry["python"], str)


def test_environment_config(
basic_environment: BasicEnvironment,
) -> None:
"""Test that the ansible environment configurations are generated.
Ensure the environment configurations are generated and look for information unlikely to change
as a basic smoke test.
:param basic_environment: A dict representing the environment configuration
"""
assert "py3" in basic_environment.name

config = basic_environment.config

assert config["allowlist_externals"]
assert config["commands_pre"]
assert config["commands"]
assert config["pass_env"]

assert "https://github.com/ansible/ansible/archive" in config["deps"]
assert "XDG_CACHE_HOME" in config["set_env"]

0 comments on commit 91cbc9c

Please sign in to comment.