refine tmpdir handling and docs
- clear tmpdir specified with --basetemp - remove config.mktmp and config.getbasetemp methods
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
""" command line configuration, ini-file and conftest.py processing. """
|
||||
""" command line options, ini-file and conftest.py processing. """
|
||||
|
||||
import py
|
||||
import sys, os
|
||||
@@ -245,8 +245,6 @@ class CmdOptions(object):
|
||||
|
||||
class Config(object):
|
||||
""" access to configuration values, pluginmanager and plugin hooks. """
|
||||
basetemp = None
|
||||
|
||||
def __init__(self, pluginmanager=None):
|
||||
#: command line option values, usually added via parser.addoption(...)
|
||||
#: or parser.getgroup(...).addoption(...) calls
|
||||
@@ -330,29 +328,6 @@ class Config(object):
|
||||
args.append(py.std.os.getcwd())
|
||||
self.args = args
|
||||
|
||||
def ensuretemp(self, string, dir=True):
|
||||
return self.getbasetemp().ensure(string, dir=dir)
|
||||
|
||||
def getbasetemp(self):
|
||||
if self.basetemp is None:
|
||||
basetemp = self.option.basetemp
|
||||
if basetemp:
|
||||
basetemp = py.path.local(basetemp)
|
||||
if not basetemp.check(dir=1):
|
||||
basetemp.mkdir()
|
||||
else:
|
||||
basetemp = py.path.local.make_numbered_dir(prefix='pytest-')
|
||||
self.basetemp = basetemp
|
||||
return self.basetemp
|
||||
|
||||
def mktemp(self, basename, numbered=False):
|
||||
basetemp = self.getbasetemp()
|
||||
if not numbered:
|
||||
return basetemp.mkdir(basename)
|
||||
else:
|
||||
return py.path.local.make_numbered_dir(prefix=basename,
|
||||
keep=0, rootdir=basetemp, lock_timeout=None)
|
||||
|
||||
def getini(self, name):
|
||||
""" return configuration value from an ini file. If the
|
||||
specified name hasn't been registered through a prior ``parse.addini``
|
||||
|
||||
@@ -173,7 +173,7 @@ class TmpTestdir:
|
||||
self.Config = request.config.__class__
|
||||
self._pytest = request.getfuncargvalue("_pytest")
|
||||
# XXX remove duplication with tmpdir plugin
|
||||
basetmp = request.config.ensuretemp("testdir")
|
||||
basetmp = request.config._tmpdirhandler.ensuretemp("testdir")
|
||||
name = request.function.__name__
|
||||
for i in range(100):
|
||||
try:
|
||||
@@ -350,7 +350,12 @@ class TmpTestdir:
|
||||
if not args:
|
||||
args = (self.tmpdir,)
|
||||
config = self.config_preparse()
|
||||
args = list(args) + ["--basetemp=%s" % self.tmpdir.dirpath('basetemp')]
|
||||
args = list(args)
|
||||
for x in args:
|
||||
if str(x).startswith('--basetemp'):
|
||||
break
|
||||
else:
|
||||
args.append("--basetemp=%s" % self.tmpdir.dirpath('basetemp'))
|
||||
config.parse(args)
|
||||
return config
|
||||
|
||||
@@ -361,7 +366,8 @@ class TmpTestdir:
|
||||
oldconfig = getattr(py.test, 'config', None)
|
||||
try:
|
||||
c = py.test.config = self.Config()
|
||||
c.basetemp = oldconfig.mktemp("reparse", numbered=True)
|
||||
c.basetemp = py.path.local.make_numbered_dir(prefix="reparse",
|
||||
keep=0, rootdir=self.tmpdir, lock_timeout=None)
|
||||
c.parse(args)
|
||||
return c
|
||||
finally:
|
||||
|
||||
@@ -1,17 +1,61 @@
|
||||
""" support for providing temporary directories to test functions. """
|
||||
import pytest, py
|
||||
from _pytest.monkeypatch import monkeypatch
|
||||
|
||||
def pytest_configure(config):
|
||||
def ensuretemp(string, dir=1):
|
||||
class TempdirHandler:
|
||||
def __init__(self, config):
|
||||
self.config = config
|
||||
self.trace = config.trace.get("tmpdir")
|
||||
|
||||
def ensuretemp(self, string, dir=1):
|
||||
""" (deprecated) return temporary directory path with
|
||||
the given string as the trailing part. It is usually
|
||||
better to use the 'tmpdir' function argument which will
|
||||
take care to provide empty unique directories for each
|
||||
test call even if the test is called multiple times.
|
||||
better to use the 'tmpdir' function argument which
|
||||
provides an empty unique-per-test-invocation directory
|
||||
and is guaranteed to be empty.
|
||||
"""
|
||||
#py.log._apiwarn(">1.1", "use tmpdir function argument")
|
||||
return config.ensuretemp(string, dir=dir)
|
||||
pytest.ensuretemp = ensuretemp
|
||||
return self.getbasetemp().ensure(string, dir=dir)
|
||||
|
||||
def mktemp(self, basename, numbered=True):
|
||||
basetemp = self.getbasetemp()
|
||||
if not numbered:
|
||||
p = basetemp.mkdir(basename)
|
||||
else:
|
||||
p = py.path.local.make_numbered_dir(prefix=basename,
|
||||
keep=0, rootdir=basetemp, lock_timeout=None)
|
||||
self.trace("mktemp", p)
|
||||
return p
|
||||
|
||||
def getbasetemp(self):
|
||||
""" return base temporary directory. """
|
||||
try:
|
||||
return self._basetemp
|
||||
except AttributeError:
|
||||
basetemp = self.config.option.basetemp
|
||||
if basetemp:
|
||||
basetemp = py.path.local(basetemp)
|
||||
if basetemp.check():
|
||||
basetemp.remove()
|
||||
basetemp.mkdir()
|
||||
else:
|
||||
basetemp = py.path.local.make_numbered_dir(prefix='pytest-')
|
||||
self._basetemp = t = basetemp
|
||||
self.trace("new basetemp", t)
|
||||
return t
|
||||
|
||||
def finish(self):
|
||||
self.trace("finish")
|
||||
|
||||
def pytest_configure(config):
|
||||
config._mp = mp = monkeypatch()
|
||||
t = TempdirHandler(config)
|
||||
mp.setattr(config, '_tmpdirhandler', t, raising=False)
|
||||
mp.setattr(pytest, 'ensuretemp', t.ensuretemp, raising=False)
|
||||
|
||||
def pytest_unconfigure(config):
|
||||
config._tmpdirhandler.finish()
|
||||
config._mp.undo()
|
||||
|
||||
def pytest_funcarg__tmpdir(request):
|
||||
"""return a temporary directory path object
|
||||
@@ -22,5 +66,6 @@ def pytest_funcarg__tmpdir(request):
|
||||
"""
|
||||
name = request._pyfuncitem.name
|
||||
name = py.std.re.sub("[\W]", "_", name)
|
||||
x = request.config.mktemp(name, numbered=True)
|
||||
x = request.config._tmpdirhandler.mktemp(name, numbered=True)
|
||||
return x.realpath()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user