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

Problems when use latest version (4.1.0) #4613

Closed
paulocoutinhox opened this issue Jan 7, 2019 · 12 comments
Closed

Problems when use latest version (4.1.0) #4613

paulocoutinhox opened this issue Jan 7, 2019 · 12 comments
Labels
type: question general question, might be closed after 2 weeks of inactivity

Comments

@paulocoutinhox
Copy link

Hi,

After update my tool to latest pytest, i start getting this error:

https://travis-ci.org/ezored/ezored/jobs/476372726

image

Can anyone help me?

@RonnyPfannschmidt
Copy link
Member

this indicates a broken test which changes to a temporary folder which is subsequently deleted

@RonnyPfannschmidt RonnyPfannschmidt added the type: question general question, might be closed after 2 weeks of inactivity label Jan 7, 2019
@RonnyPfannschmidt
Copy link
Member

based on https://github.com/ezored/ezored/search?q=chdir&unscoped_q=chdir this seems to be entirely self inflicted as no cleanup seems to happen (at least based on the sample of examples i took a look at

@paulocoutinhox
Copy link
Author

Its very strange, since no code was changed, only dependencies updated. Never had this problem.

@nicoddemus
Copy link
Member

You say "latest version (2.6.1)", but the latest version is 4.1.0. Was that a typo?

@paulocoutinhox
Copy link
Author

Sorry, pytest==4.1.0 and pytest-cov==2.6.1.

@paulocoutinhox paulocoutinhox changed the title Problems when use latest version (2.6.1) Problems when use latest version (4.1.0) Jan 7, 2019
@nicoddemus
Copy link
Member

Strange that this is only happening now, but @RonnyPfannschmidt's assessment is correct: your test functions change the current directory and this is making coverage to error out when calling os.getcwd.

Search your test code for os.chdir, and replace any calls you find by monkeypatch.chdir instead, which takes care of restoring the original directory. For example (https://github.com/ezored/ezored/blob/master/tests/commands/test_dependency.py#L13):

class TestDependency(TestCase):
    @tempdir()
    def test_dependency_list(self, d):
        os.chdir(d.path)

        d.write(Constants.PROJECT_FILE, Constants.PROJECT_FILE_DATA.encode('utf-8'))
        ...

Becomes:

class TestDependency(TestCase):
    @tempdir()
    def test_dependency_list(self, d, monkeypatch):
        monkeypatch.chdir(d.path)

        d.write(Constants.PROJECT_FILE, Constants.PROJECT_FILE_DATA.encode('utf-8'))
        ...

I was thinking of creating a PR fixing this, but found that there are a ton of files with this pattern (24 files, 50 matches). 😋

@RonnyPfannschmidt
Copy link
Member

@nicoddemus he cant use that due to unittest usage

@nicoddemus
Copy link
Member

Oh right. Then he will need the "autouse trick" or a custom implementation. I will post those later.

@paulocoutinhox
Copy link
Author

If you have any solution, can you post here? It don't happen on old commits:

https://travis-ci.org/ezored/ezored/builds

Very strange.

@blueyed
Copy link
Contributor

blueyed commented Jan 8, 2019

Just a sidenote: please take a little more time with reporting, i.e. provide tracebacks as text etc.

@nicoddemus
Copy link
Member

Two solutions:

  1. Use an autouse fixture to save monkeypatch in self, then use self.monkeypatch on tests:

    class TestDependency(TestCase):
        @pytest.fixture(autouse=True)
        def get_monkeypatch(self, monkeypatch):
            self.monkeypatch = monkeypatch
    
        @tempdir()
        def test_dependency_list(self, d):
            self.monkeypatch.chdir(d.path)
    
            d.write(Constants.PROJECT_FILE, Constants.PROJECT_FILE_DATA.encode('utf-8'))
            ...
  2. Implement your own "chdir-like-fixture" that makes use of TestCase.addCleanup to restore cwd to the original directory. This might be the simplest as you can simply do a quick find/replace for the os.chdir calls and add an import:

    # in some utils module somewhere
    def change_cwd(testcase, path):
        old_dir = os.getcwd()
        
        def restore():
            os.chdir(old_dir)
        testcase.addCleanup(restore)
    
        os.chdir(path)
    
    
    # usage
    from .utils import change_cwd
    
    class TestDependency(TestCase):
    
        @tempdir()
        def test_dependency_list(self, d):
            change_cwd(self, d.path)

Hope this helps

@Zac-HD Zac-HD closed this as completed Jan 15, 2019
@paulocoutinhox
Copy link
Author

Hi,

I use the first solution and the problem was solved.

Green again:
https://github.com/ezored/ezored

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

5 participants