Files
clan-core/pkgs/clan-cli/tests/temporary_dir.py
Jörg Thalheim 93cbe62765 always resolve symlinks for TemporaryDirectory
On macOS mktemp returns a temporary directory in a symlink.
Nix has a bug where it won't accept path:// located in a symlink.
This avoid this issue by always resolving symlinks as returned by
TemporaryDirectory.
2025-03-19 16:47:18 +01:00

45 lines
1.3 KiB
Python

import logging
import os
import tempfile
from collections.abc import Iterator
from pathlib import Path
import pytest
log = logging.getLogger(__name__)
@pytest.fixture
def temporary_home(monkeypatch: pytest.MonkeyPatch) -> Iterator[Path]:
with tempfile.TemporaryDirectory(prefix="pytest-home-") as _dirpath:
dirpath = Path(_dirpath).resolve()
xdg_runtime_dir = os.getenv("XDG_RUNTIME_DIR")
monkeypatch.setenv("HOME", str(dirpath))
monkeypatch.setenv("XDG_CONFIG_HOME", str(dirpath / ".config"))
runtime_dir = dirpath / "xdg-runtime-dir"
runtime_dir.mkdir()
runtime_dir.chmod(0o700)
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))
)
monkeypatch.setenv("XDG_RUNTIME_DIR", str(runtime_dir))
monkeypatch.chdir(str(dirpath))
yield dirpath
@pytest.fixture
def temp_dir() -> Iterator[Path]:
with tempfile.TemporaryDirectory(prefix="pytest-") as _dirpath:
yield Path(_dirpath).resolve()