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
|
|
|
|
from pathlib import Path
|
|
|
|
from typing import Iterator
|
|
|
|
|
|
|
|
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-17 21:49:16 +00:00
|
|
|
if os.getenv("TEST_KEEP_TEMPORARY_DIR") is not None:
|
2023-10-13 17:56:10 +00:00
|
|
|
temp_dir = tempfile.mkdtemp(prefix="pytest-")
|
|
|
|
path = Path(temp_dir)
|
2023-10-19 23:11:32 +00:00
|
|
|
log.debug("Temp HOME directory: %s", str(path))
|
|
|
|
monkeypatch.setenv("HOME", str(temp_dir))
|
2023-10-13 17:56:10 +00:00
|
|
|
yield path
|
|
|
|
else:
|
2023-10-17 21:49:16 +00:00
|
|
|
log.debug("TEST_KEEP_TEMPORARY_DIR not set, using TemporaryDirectory")
|
2023-10-13 17:56:10 +00:00
|
|
|
with tempfile.TemporaryDirectory(prefix="pytest-") as dirpath:
|
2023-10-19 23:11:32 +00:00
|
|
|
monkeypatch.setenv("HOME", str(dirpath))
|
|
|
|
log.debug("Temp HOME directory: %s", str(dirpath))
|
2023-10-13 17:56:10 +00:00
|
|
|
yield Path(dirpath)
|