2010-11-06 18:38:53 +08:00
|
|
|
""" support for providing temporary directories to test functions. """
|
2010-10-21 22:10:37 +08:00
|
|
|
import pytest, py
|
2010-11-22 00:43:18 +08:00
|
|
|
from _pytest.monkeypatch import monkeypatch
|
2010-10-10 19:48:49 +08:00
|
|
|
|
2010-11-22 00:43:18 +08:00
|
|
|
class TempdirHandler:
|
|
|
|
def __init__(self, config):
|
|
|
|
self.config = config
|
|
|
|
self.trace = config.trace.get("tmpdir")
|
|
|
|
|
|
|
|
def ensuretemp(self, string, dir=1):
|
2010-10-10 19:48:49 +08:00
|
|
|
""" (deprecated) return temporary directory path with
|
|
|
|
the given string as the trailing part. It is usually
|
2010-11-22 00:43:18 +08:00
|
|
|
better to use the 'tmpdir' function argument which
|
|
|
|
provides an empty unique-per-test-invocation directory
|
|
|
|
and is guaranteed to be empty.
|
2010-10-10 19:48:49 +08:00
|
|
|
"""
|
|
|
|
#py.log._apiwarn(">1.1", "use tmpdir function argument")
|
2010-11-22 00:43:18 +08:00
|
|
|
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-')
|
2012-10-11 19:05:16 +08:00
|
|
|
self._basetemp = t = basetemp.realpath()
|
2010-11-22 00:43:18 +08:00
|
|
|
self.trace("new basetemp", t)
|
|
|
|
return t
|
|
|
|
|
|
|
|
def finish(self):
|
|
|
|
self.trace("finish")
|
2011-11-07 03:34:02 +08:00
|
|
|
|
2010-11-22 00:43:18 +08:00
|
|
|
def pytest_configure(config):
|
2011-04-17 18:20:13 +08:00
|
|
|
mp = monkeypatch()
|
2010-11-22 00:43:18 +08:00
|
|
|
t = TempdirHandler(config)
|
2011-04-17 18:20:13 +08:00
|
|
|
config._cleanup.extend([mp.undo, t.finish])
|
2010-11-22 00:43:18 +08:00
|
|
|
mp.setattr(config, '_tmpdirhandler', t, raising=False)
|
|
|
|
mp.setattr(pytest, 'ensuretemp', t.ensuretemp, raising=False)
|
|
|
|
|
2012-11-19 21:07:14 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def tmpdir(request):
|
2009-11-05 10:18:55 +08:00
|
|
|
"""return a temporary directory path object
|
2011-03-04 06:40:38 +08:00
|
|
|
which is unique to each test function invocation,
|
2009-11-05 10:18:55 +08:00
|
|
|
created as a sub directory of the base temporary
|
|
|
|
directory. The returned object is a `py.path.local`_
|
2010-07-27 03:15:15 +08:00
|
|
|
path object.
|
2009-11-05 10:18:55 +08:00
|
|
|
"""
|
2012-11-19 21:07:14 +08:00
|
|
|
name = request.node.name
|
2010-10-21 22:10:37 +08:00
|
|
|
name = py.std.re.sub("[\W]", "_", name)
|
2012-07-19 01:49:14 +08:00
|
|
|
x = request.config._tmpdirhandler.mktemp(name, numbered=True)
|
2011-11-07 03:34:02 +08:00
|
|
|
return x
|