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

23 lines
614 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_dir() -> Iterator[Path]:
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)
log.info("Keeping temporary test directory: ", path)
2023-10-13 17:56:10 +00:00
yield path
else:
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:
yield Path(dirpath)