
import tempfile
import os
from pathlib import Path
from typing import Optional


def get_test_fn(test_name: Optional[str] = None, extension: str = "txt") -> Path:
    """ PYTEST compatible temporary test file creator """

    # Get current test name if we can..
    if test_name is None:
        test_name = os.environ.get('PYTEST_CURRENT_TEST')
        if test_name is not None:
            test_name = test_name.split(':')[-1].split(' ')[0]
        else:
            test_name = __name__

    file_path = Path(tempfile.gettempdir())
    file_path = file_path / Path(f"{test_name}.{extension}")

    # Create the file
    with open(file_path, "w"):
        pass

    return file_path
