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]:
|
2023-10-21 15:19:06 +00:00
|
|
|
env_dir = os.getenv("TEST_TEMPORARY_DIR")
|
|
|
|
if env_dir is not None:
|
|
|
|
path = Path(env_dir).resolve()
|
2023-10-19 23:11:32 +00:00
|
|
|
log.debug("Temp HOME directory: %s", str(path))
|
2023-10-21 15:19:06 +00:00
|
|
|
monkeypatch.setenv("HOME", str(path))
|
2024-03-27 14:51:52 +00:00
|
|
|
monkeypatch.setenv("XDG_CONFIG_HOME", str(path / ".config"))
|
2024-06-05 11:34:17 +00:00
|
|
|
runtime_dir = path / "xdg-runtime-dir"
|
|
|
|
runtime_dir.mkdir()
|
|
|
|
runtime_dir.chmod(0o700)
|
|
|
|
monkeypatch.setenv("XDG_RUNTIME_DIR", str(runtime_dir))
|
2023-10-25 17:23:28 +00:00
|
|
|
monkeypatch.chdir(str(path))
|
2023-10-13 17:56:10 +00:00
|
|
|
yield path
|
|
|
|
else:
|
|
|
|
with tempfile.TemporaryDirectory(prefix="pytest-") as dirpath:
|
2023-10-19 23:11:32 +00:00
|
|
|
monkeypatch.setenv("HOME", str(dirpath))
|
2023-11-10 11:50:11 +00:00
|
|
|
monkeypatch.setenv("XDG_CONFIG_HOME", str(Path(dirpath) / ".config"))
|
2024-06-05 11:34:17 +00:00
|
|
|
runtime_dir = Path(dirpath) / "xdg-runtime-dir"
|
|
|
|
runtime_dir.mkdir()
|
|
|
|
runtime_dir.chmod(0o700)
|
|
|
|
monkeypatch.setenv("XDG_RUNTIME_DIR", str(runtime_dir))
|
2023-10-25 17:23:28 +00:00
|
|
|
monkeypatch.chdir(str(dirpath))
|
2023-10-19 23:11:32 +00:00
|
|
|
log.debug("Temp HOME directory: %s", str(dirpath))
|
2023-10-13 17:56:10 +00:00
|
|
|
yield Path(dirpath)
|