Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

monkeypatch-style chdir #7228

Closed
4 tasks done
jtratner opened this issue May 18, 2020 · 4 comments
Closed
4 tasks done

monkeypatch-style chdir #7228

jtratner opened this issue May 18, 2020 · 4 comments
Labels
type: question general question, might be closed after 2 weeks of inactivity

Comments

@jtratner
Copy link

First - thank you so much for pytest - amazing tool and I love it!

The tmp_path and tmpdir fixtures are excellent, but can easily lead to changing current directory for all tests without realizing it (which can be confusing in error output).

It'd be useful to have a monkeypatch-style helper to change the directory built-in to pytest, equivalent to the chdir fixture below.

import pytest
import contextlib
from pathlib import Path
import os

ORIGINAL_DIR = Path.cwd()

@pytest.fixture
def chdir():
    original_path = Path.cwd()

    def do_chdir(path):
        curdir = Path.cwd()
        os.chdir(path)
    try:
        yield do_chdir
    finally:
        os.chdir(original_path)

def test_original_path():
    assert Path.cwd() == ORIGINAL_DIR


def test_something(tmpdir, chdir):
    assert Path.cwd() == ORIGINAL_DIR
    mypath = tmpdir.join("whatever")
    mypath.mkdir()
    chdir(mypath)
    assert Path.cwd() == mypath

The best-est implementation would make tmpdir.chdir() automatically restore working environment after test completed, but I could see that being complicated.

  • a detailed description of the bug or suggestion
  • output of pip list from the virtual environment you are using
pip list
Package            Version
------------------ -------
attrs              19.3.0 
importlib-metadata 1.6.0  
more-itertools     8.3.0  
packaging          20.3   
pip                18.1   
pluggy             0.13.1 
py                 1.8.1  
pyparsing          2.4.7  
pytest             5.4.2  
setuptools         40.6.2 
six                1.14.0 
wcwidth            0.1.9  
zipp               3.1.0  
  • pytest and operating system versions - pytest 5.4.2 + OSX
  • minimal example if possible
@jtratner
Copy link
Author

(happy to contribute a pull request if this is an acceptable item - might need some help figuring out how to enforce test ordering tho :) )

@nicoddemus
Copy link
Member

Hi @jtratner,

There's already monkeypatch.chdir which does exactly that. 😁

@nicoddemus nicoddemus added the type: question general question, might be closed after 2 weeks of inactivity label May 18, 2020
@jtratner
Copy link
Author

oof 😅 slightly embarrassing haha (I swear I searched for it! haha) - lemme put up a PR to update the docs to call this out with tmpdir/tmp_path examples

@nicoddemus
Copy link
Member

Hehehe no worries, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question general question, might be closed after 2 weeks of inactivity
Projects
None yet
Development

No branches or pull requests

2 participants