2010-11-06 18:38:53 +08:00
|
|
|
""" support for providing temporary directories to test functions. """
|
2017-03-17 09:21:30 +08:00
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
|
2014-08-01 06:13:40 +08:00
|
|
|
import re
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
import py
|
2016-07-12 09:03:53 +08:00
|
|
|
from _pytest.monkeypatch import MonkeyPatch
|
2010-10-10 19:48:49 +08:00
|
|
|
|
2014-08-01 06:13:40 +08:00
|
|
|
|
2017-05-18 05:16:11 +08:00
|
|
|
class TempdirFactory:
|
2015-07-16 07:03:58 +08:00
|
|
|
"""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
|
|
|
"""
|
2017-07-17 07:25:09 +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,
|
2017-07-17 07:25:07 +08:00
|
|
|
keep=0, rootdir=basetemp, lock_timeout=None)
|
2010-11-22 00:43:18 +08:00
|
|
|
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
|
|
|
temproot = py.path.local.get_temproot()
|
2015-09-17 03:42:07 +08:00
|
|
|
user = get_user()
|
|
|
|
if user:
|
|
|
|
# use a sub-directory in the temproot to speed-up
|
|
|
|
# make_numbered_dir() call
|
|
|
|
rootdir = temproot.join('pytest-of-%s' % user)
|
|
|
|
else:
|
|
|
|
rootdir = temproot
|
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-09-16 23:47:50 +08:00
|
|
|
|
|
|
|
def get_user():
|
2015-09-17 03:42:07 +08:00
|
|
|
"""Return the current user name, or None if getuser() does not work
|
|
|
|
in the current environment (see #1010).
|
2015-09-16 23:47:50 +08:00
|
|
|
"""
|
|
|
|
import getpass
|
|
|
|
try:
|
|
|
|
return getpass.getuser()
|
2015-09-19 09:06:13 +08:00
|
|
|
except (ImportError, KeyError):
|
2015-09-17 03:42:07 +08:00
|
|
|
return None
|
2015-09-16 23:47:50 +08:00
|
|
|
|
2016-11-21 04:59:15 +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.
|
|
|
|
"""
|
2016-07-12 09:03:53 +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):
|
2016-07-22 18:39:06 +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
|
2017-02-15 22:54:53 +08:00
|
|
|
name = re.sub(r"[\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
|