implement tmp_path_factory and deprecate pytest.ensuretemp as intended

This commit is contained in:
Ronny Pfannschmidt 2018-10-11 09:41:37 +02:00
parent 36c2a101cb
commit 16e2737da3
2 changed files with 31 additions and 14 deletions

View File

@ -109,3 +109,8 @@ PYTEST_PLUGINS_FROM_NON_TOP_LEVEL_CONFTEST = RemovedInPytest4Warning(
PYTEST_NAMESPACE = RemovedInPytest4Warning( PYTEST_NAMESPACE = RemovedInPytest4Warning(
"pytest_namespace is deprecated and will be removed soon" "pytest_namespace is deprecated and will be removed soon"
) )
PYTEST_ENSURETEMP = RemovedInPytest4Warning(
"pytest/tmpdir_factory.ensuretemp is deprecated, \n"
"please use the tmp_path fixture or tmp_path_factory.mktemp"
)

View File

@ -8,6 +8,8 @@ import py
from _pytest.monkeypatch import MonkeyPatch from _pytest.monkeypatch import MonkeyPatch
import attr import attr
import tempfile import tempfile
import warnings
from .pathlib import ( from .pathlib import (
Path, Path,
make_numbered_dir, make_numbered_dir,
@ -88,6 +90,9 @@ class TempdirFactory(object):
and is guaranteed to be empty. and is guaranteed to be empty.
""" """
# py.log._apiwarn(">1.1", "use tmpdir function argument") # py.log._apiwarn(">1.1", "use tmpdir function argument")
from .deprecated import PYTEST_ENSURETEMP
warnings.warn(PYTEST_ENSURETEMP, stacklevel=2)
return self.getbasetemp().ensure(string, dir=dir) return self.getbasetemp().ensure(string, dir=dir)
def mktemp(self, basename, numbered=True): def mktemp(self, basename, numbered=True):
@ -101,9 +106,6 @@ class TempdirFactory(object):
"""backward compat wrapper for ``_tmppath_factory.getbasetemp``""" """backward compat wrapper for ``_tmppath_factory.getbasetemp``"""
return py.path.local(self._tmppath_factory.getbasetemp().resolve()) return py.path.local(self._tmppath_factory.getbasetemp().resolve())
def finish(self):
self._tmppath_factory.trace("finish")
def get_user(): def get_user():
"""Return the current user name, or None if getuser() does not work """Return the current user name, or None if getuser() does not work
@ -127,18 +129,34 @@ def pytest_configure(config):
mp = MonkeyPatch() mp = MonkeyPatch()
tmppath_handler = TempPathFactory.from_config(config) tmppath_handler = TempPathFactory.from_config(config)
t = TempdirFactory(tmppath_handler) t = TempdirFactory(tmppath_handler)
config._cleanup.extend([mp.undo, t.finish]) config._cleanup.append(mp.undo)
mp.setattr(config, "_tmp_path_factory", tmppath_handler, raising=False)
mp.setattr(config, "_tmpdirhandler", t, raising=False) mp.setattr(config, "_tmpdirhandler", t, raising=False)
mp.setattr(pytest, "ensuretemp", t.ensuretemp, raising=False) mp.setattr(pytest, "ensuretemp", t.ensuretemp, raising=False)
@pytest.fixture(scope="session") @pytest.fixture(scope="session")
def tmpdir_factory(request): def tmpdir_factory(request):
"""Return a TempdirFactory instance for the test session. """Return a :class:`_pytest.tmpdir.TempdirFactory` instance for the test session.
""" """
return request.config._tmpdirhandler return request.config._tmpdirhandler
@pytest.fixture(scope="session")
def tmp_path_factory(request):
"""Return a :class:`_pytest.tmpdir.TempPathFactory` instance for the test session.
"""
return request.config._tmp_path_factory
def _mk_tmp(request, factory):
name = request.node.name
name = re.sub(r"[\W]", "_", name)
MAXVAL = 30
name = name[:MAXVAL]
return factory.mktemp(name, numbered=True)
@pytest.fixture @pytest.fixture
def tmpdir(request, tmpdir_factory): def tmpdir(request, tmpdir_factory):
"""Return a temporary directory path object """Return a temporary directory path object
@ -149,17 +167,11 @@ def tmpdir(request, tmpdir_factory):
.. _`py.path.local`: https://py.readthedocs.io/en/latest/path.html .. _`py.path.local`: https://py.readthedocs.io/en/latest/path.html
""" """
name = request.node.name return _mk_tmp(request, tmpdir_factory)
name = re.sub(r"[\W]", "_", name)
MAXVAL = 30
if len(name) > MAXVAL:
name = name[:MAXVAL]
x = tmpdir_factory.mktemp(name, numbered=True)
return x
@pytest.fixture @pytest.fixture
def tmp_path(tmpdir): def tmp_path(request, tmp_path_factory):
"""Return a temporary directory path object """Return a temporary directory path object
which is unique to each test function invocation, which is unique to each test function invocation,
created as a sub directory of the base temporary created as a sub directory of the base temporary
@ -171,4 +183,4 @@ def tmp_path(tmpdir):
in python < 3.6 this is a pathlib2.Path in python < 3.6 this is a pathlib2.Path
""" """
return Path(tmpdir) return _mk_tmp(request, tmp_path_factory)