2023-10-17 21:49:16 +00:00
|
|
|
import logging
|
2023-10-13 17:56:10 +00:00
|
|
|
import os
|
2023-07-28 10:12:37 +00:00
|
|
|
import tempfile
|
2023-11-29 11:40:48 +00:00
|
|
|
from collections.abc import Iterator
|
2023-07-28 10:12:37 +00:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2023-10-17 21:49:16 +00:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2023-07-28 10:12:37 +00:00
|
|
|
|
|
|
|
@pytest.fixture
|
2023-10-19 23:11:32 +00:00
|
|
|
def temporary_home(monkeypatch: pytest.MonkeyPatch) -> Iterator[Path]:
|
2024-06-25 10:03:40 +00:00
|
|
|
with tempfile.TemporaryDirectory(prefix="pytest-") as dirpath:
|
|
|
|
xdg_runtime_dir = os.getenv("XDG_RUNTIME_DIR")
|
|
|
|
monkeypatch.setenv("HOME", str(dirpath))
|
|
|
|
monkeypatch.setenv("XDG_CONFIG_HOME", str(Path(dirpath) / ".config"))
|
|
|
|
|
|
|
|
runtime_dir = Path(dirpath) / "xdg-runtime-dir"
|
2024-06-05 11:34:17 +00:00
|
|
|
runtime_dir.mkdir()
|
|
|
|
runtime_dir.chmod(0o700)
|
2024-06-25 10:03:40 +00:00
|
|
|
|
|
|
|
gpgdir = runtime_dir / "gpgagent"
|
|
|
|
gpgdir.mkdir()
|
|
|
|
gpgdir.chmod(0o700)
|
|
|
|
monkeypatch.setenv("GPG_AGENT_INFO", str(gpgdir))
|
|
|
|
|
|
|
|
# Iterate over all environment variables
|
|
|
|
for key, value in os.environ.items():
|
|
|
|
if xdg_runtime_dir and value.startswith(xdg_runtime_dir):
|
|
|
|
monkeypatch.setenv(
|
|
|
|
key, value.replace(xdg_runtime_dir, str(runtime_dir))
|
|
|
|
)
|
|
|
|
|
2024-06-05 11:34:17 +00:00
|
|
|
monkeypatch.setenv("XDG_RUNTIME_DIR", str(runtime_dir))
|
2024-06-25 10:03:40 +00:00
|
|
|
monkeypatch.chdir(str(dirpath))
|
|
|
|
log.debug("Temp HOME directory: %s", str(dirpath))
|
|
|
|
yield Path(dirpath)
|