What is tmp_path equivalent for tmpdir.as_cwd() ? #10437
-
In astropy infrastructure for doctest, I saw a tmpdir usage that I am not sure how to modernize with tmp_path . What is the equivalent of this call if I were to replace tmpdir with tmp_path , as you recommend in your docs? Thanks! @pytest.fixture(autouse=True)
def _docdir(request):
"""Run doctests in isolated tmpdir so outputs do not end up in repo"""
# Trigger ONLY for doctestplus
doctest_plugin = request.config.pluginmanager.getplugin("doctestplus")
if isinstance(request.node.parent, doctest_plugin._doctest_textfile_item_cls):
# Don't apply this fixture to io.rst. It reads files and doesn't write
if "io.rst" not in request.node.name:
tmpdir = request.getfixturevalue('tmpdir')
with tmpdir.as_cwd():
yield
else:
yield
else:
yield |
Beta Was this translation helpful? Give feedback.
Answered by
The-Compiler
Oct 26, 2022
Replies: 1 comment
-
No pre-made pathlib equivalent exists. You could use pytest's monkeypatch fixture: monkeypatch.chdir(tmp_path) or alternatively, something like this: old_cwd = pathlib.Path.cwd()
os.chdir(path)
yield
os.chdir(old_cwd) If you really need a context manager (doesn't look like you do here), it'd look something like this: @contextlib.contextmanager
def change_cwd(path: pathlib.Path) -> Iterator[None]:
old_cwd = pathlib.Path.cwd()
os.chdir(path)
try:
yield
finally:
os.chdir(old_cwd) |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
The-Compiler
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No pre-made pathlib equivalent exists. You could use pytest's monkeypatch fixture:
or alternatively, something like this:
If you really need a context manager (doesn't look like you do here), it'd look something like this: