From 33c21d417ff27e78d2c194fc5a5bc6872756a21c Mon Sep 17 00:00:00 2001 From: Chris Markiewicz Date: Sun, 3 Dec 2023 15:21:31 -0500 Subject: [PATCH] TEST: Chdir during doctest to avoid polluting the working dir --- nibabel/externals/conftest.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 nibabel/externals/conftest.py 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