diff --git a/nibabel/externals/conftest.py b/nibabel/externals/conftest.py new file mode 100644 index 0000000000..33f88eb323 --- /dev/null +++ b/nibabel/externals/conftest.py @@ -0,0 +1,25 @@ +import pytest + +try: + from contextlib import chdir as _chdir +except ImportError: # PY310 + import os + from contextlib import contextmanager + + @contextmanager # type: ignore + def _chdir(path): + cwd = os.getcwd() + os.chdir(path) + try: + yield + finally: + os.chdir(cwd) + + +@pytest.fixture(autouse=True) +def chdir_tmpdir(request, tmp_path): + if request.node.__class__.__name__ == "DoctestItem": + with _chdir(tmp_path): + yield + else: + yield