Skip to content
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

Set XDG_CACHE_HOME #208

Merged
merged 7 commits into from
Aug 9, 2023
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
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"]