clan-core/pkgs/clan-cli/tests/temporary_dir.py

28 lines
859 B
Python
Raw Normal View History

import logging
2023-10-13 17:56:10 +00:00
import os
2023-07-28 10:12:37 +00:00
import tempfile
from pathlib import Path
from typing import Iterator
import pytest
log = logging.getLogger(__name__)
2023-07-28 10:12:37 +00:00
@pytest.fixture
def temporary_home(monkeypatch: pytest.MonkeyPatch) -> Iterator[Path]:
env_dir = os.getenv("TEST_TEMPORARY_DIR")
if env_dir is not None:
path = Path(env_dir).resolve()
log.debug("Temp HOME directory: %s", str(path))
monkeypatch.setenv("HOME", str(path))
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:
monkeypatch.setenv("HOME", str(dirpath))
monkeypatch.setenv("XDG_CONFIG_HOME", str(Path(dirpath) / ".config"))
2023-10-25 17:23:28 +00:00
monkeypatch.chdir(str(dirpath))
log.debug("Temp HOME directory: %s", str(dirpath))
2023-10-13 17:56:10 +00:00
yield Path(dirpath)