2010-11-06 18:38:53 +08:00
|
|
|
""" support for providing temporary directories to test functions. """
|
2014-08-01 06:13:40 +08:00
|
|
|
import re
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
import py
|
2010-11-22 00:43:18 +08:00
|
|
|
from _pytest.monkeypatch import monkeypatch
|
2010-10-10 19:48:49 +08:00
|
|
|
|
2014-08-01 06:13:40 +08:00
|
|
|
|
2015-07-16 07:03:58 +08:00
|
|
|
class TempdirFactory:
|
|
|
|
"""Factory for temporary directories under the common base temp directory.
|
|
|
|
|
|
|
|
The base directory can be configured using the ``--basetemp`` option.
|
|
|
|
"""
|
|
|
|
|
2010-11-22 00:43:18 +08:00
|
|
|
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):
|
2015-07-16 07:03:58 +08:00
|
|
|
"""Create a subdirectory of the base temporary directory and return it.
|
|
|
|
If ``numbered``, ensure the directory is unique by adding a number
|
|
|
|
prefix greater than any existing one.
|
|
|
|
"""
|
2010-11-22 00:43:18 +08:00
|
|
|
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:
|
2015-07-15 05:11:30 +08:00
|
|
|
# use a sub-directory in the temproot to speed-up
|
|
|
|
# make_numbered_dir() call
|
|
|
|
import getpass
|
|
|
|
temproot = py.path.local.get_temproot()
|
2015-07-29 07:35:13 +08:00
|
|
|
rootdir = temproot.join('pytest-of-%s' % getpass.getuser())
|
2015-07-15 05:11:30 +08:00
|
|
|
rootdir.ensure(dir=1)
|
|
|
|
basetemp = py.path.local.make_numbered_dir(prefix='pytest-',
|
|
|
|
rootdir=rootdir)
|
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
|
|
|
|
2015-07-16 07:03:58 +08:00
|
|
|
# backward compatibility
|
|
|
|
TempdirHandler = TempdirFactory
|
|
|
|
|
|
|
|
|
2010-11-22 00:43:18 +08:00
|
|
|
def pytest_configure(config):
|
2015-07-16 07:03:58 +08:00
|
|
|
"""Create a TempdirFactory and attach it to the config object.
|
|
|
|
|
|
|
|
This is to comply with existing plugins which expect the handler to be
|
|
|
|
available at pytest_configure time, but ideally should be moved entirely
|
|
|
|
to the tmpdir_factory session fixture.
|
|
|
|
"""
|
2011-04-17 18:20:13 +08:00
|
|
|
mp = monkeypatch()
|
2015-07-16 07:03:58 +08:00
|
|
|
t = TempdirFactory(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)
|
|
|
|
|
2015-07-16 07:03:58 +08:00
|
|
|
|
|
|
|
@pytest.fixture(scope='session')
|
|
|
|
def tmpdir_factory(request):
|
|
|
|
"""Return a TempdirFactory instance for the test session.
|
|
|
|
"""
|
|
|
|
return request.config._tmpdirhandler
|
|
|
|
|
|
|
|
|
2012-11-19 21:07:14 +08:00
|
|
|
@pytest.fixture
|
2015-07-16 07:03:58 +08:00
|
|
|
def tmpdir(request, tmpdir_factory):
|
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
|
2014-08-01 06:13:40 +08:00
|
|
|
name = re.sub("[\W]", "_", name)
|
2013-10-03 20:22:54 +08:00
|
|
|
MAXVAL = 30
|
|
|
|
if len(name) > MAXVAL:
|
|
|
|
name = name[:MAXVAL]
|
2015-07-16 07:03:58 +08:00
|
|
|
x = tmpdir_factory.mktemp(name, numbered=True)
|
2011-11-07 03:34:02 +08:00
|
|
|
return x
|