Skip to content

resolve manually set basetemp to a absolute path #4425

@r2dan

Description

@r2dan

Hi Guys,

We're experiencing some issues with new tmpdir implementation using pathlib. Starting from pytest 3.9.0 our tests started to fail.

We are using test class and a pre_process fixture, which prepares some data for the other tests within the class. In the test methods we're using the pre_process fixture (to access the data) as well as the tmpdir pytest builtin fixture to store some outcomes of the tests. Minimal failing example is pasted below:

import pytest, os

class TestDummy():
    @pytest.fixture(scope="class")
    def pre_process(self, tmpdir_factory):
        path_with_data = tmpdir_factory.mktemp('data')
        return path_with_data

    @pytest.mark.parametrize("param", range(2))
    def test_dummy(self, tmpdir, pre_process, param):
        os.chdir(pre_process)
        # further processing

    def test_dummy2(self, tmpdir, pre_process):
        os.chdir(pre_process)
        # further processing

Because of the directory change in test_dummy, next invocation of the tests fail due to pathlib error, as below:


self = PosixPath('tmp')

    def iterdir(self):
        """Iterate over the files in this directory.  Does not yield any
        result for the special paths '.' and '..'.
        """
        if self._closed:
            self._raise_closed()
>       for name in self._accessor.listdir(self):

../../../.pyenv/versions/3.6.5/lib/python3.6/pathlib.py:1079: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

pathobj = PosixPath('tmp'), args = ()

    @functools.wraps(strfunc)
    def wrapped(pathobj, *args):
>       return strfunc(str(pathobj), *args)
E       FileNotFoundError: [Errno 2] No such file or directory: 'tmp'

../../../.pyenv/versions/3.6.5/lib/python3.6/pathlib.py:387: FileNotFoundError

and in fact we end up with 1 pass and 2 errors in this particular case.

We currently have workaround for this issue (store the cwd before changing it and restore it before the test end), but I'd like to ask you if this is how it should behave? I'd expect that state of one test does not influence the other test's state.

The issue is observable if no class hierarchy is used as well.

Another workaround (a little bit nasty) is to reset internal pytest variable in pre_process macro:

def pre_process(tmpdir_factory):
    path_with_data = tmpdir_factory.mktemp('data')
    tmpdir_factory._tmppath_factory._basetemp = None
    return path_with_data```

Environment details:
Python 3.6.5 (via pyenv), 4.15.0-29-generic #31-Ubuntu SMP Tue Jul 17 15:37:27 UTC 2018 i686 i686 i686 GNU/Linux

$ pip list
Package Version


atomicwrites 1.2.1
attrs 18.2.0
more-itertools 4.3.0
pip 18.1
pluggy 0.8.0
py 1.7.0
pytest 4.0.0
setuptools 40.6.2
six 1.11.0 ```

  • Include a detailed description of the bug or suggestion
  • pip list of the virtual environment you are using:
  • pytest and operating system versions:
  • Minimal example if possible - already in the text

Metadata

Metadata

Assignees

No one assigned

    Labels

    plugin: tmpdirrelated to the tmpdir builtin plugintype: bugproblem that needs to be addressed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions