| 1 |
""" support for providing temporary directories to test functions. """ |
| 2 |
import re |
| 3 |
|
| 4 |
import pytest |
| 5 |
import py |
| 6 |
from _pytest.monkeypatch import monkeypatch |
| 7 |
|
| 8 |
|
| 9 |
class TempdirFactory: |
| 10 |
"""Factory for temporary directories under the common base temp directory. |
| 11 |
|
| 12 |
The base directory can be configured using the ``--basetemp`` option. |
| 13 |
""" |
| 14 |
|
| 15 |
def __init__(self, config): |
| 16 |
self.config = config |
| 17 |
self.trace = config.trace.get("tmpdir") |
| 18 |
|
| 19 |
def ensuretemp(self, string, dir=1): |
| 20 |
""" (deprecated) return temporary directory path with |
| 21 |
the given string as the trailing part. It is usually |
| 22 |
better to use the 'tmpdir' function argument which |
| 23 |
provides an empty unique-per-test-invocation directory |
| 24 |
and is guaranteed to be empty. |
| 25 |
""" |
| 26 |
|
| 27 |
return self.getbasetemp().ensure(string, dir=dir) |
| 28 |
|
| 29 |
def mktemp(self, basename, numbered=True): |
| 30 |
"""Create a subdirectory of the base temporary directory and return it. |
| 31 |
If ``numbered``, ensure the directory is unique by adding a number |
| 32 |
prefix greater than any existing one. |
| 33 |
""" |
| 34 |
basetemp = self.getbasetemp() |
| 35 |
if not numbered: |
| 36 |
p = basetemp.mkdir(basename) |
| 37 |
else: |
| 38 |
p = py.path.local.make_numbered_dir(prefix=basename, |
| 39 |
keep=0, rootdir=basetemp, lock_timeout=None) |
| 40 |
self.trace("mktemp", p) |
| 41 |
return p |
| 42 |
|
| 43 |
def getbasetemp(self): |
| 44 |
""" return base temporary directory. """ |
| 45 |
try: |
| 46 |
return self._basetemp |
| 47 |
except AttributeError: |
| 48 |
basetemp = self.config.option.basetemp |
| 49 |
if basetemp: |
| 50 |
basetemp = py.path.local(basetemp) |
| 51 |
if basetemp.check(): |
| 52 |
basetemp.remove() |
| 53 |
basetemp.mkdir() |
| 54 |
else: |
| 55 |
temproot = py.path.local.get_temproot() |
| 56 |
user = get_user() |
| 57 |
if user: |
| 58 |
|
| 59 |
|
| 60 |
rootdir = temproot.join('pytest-of-%s' % user) |
| 61 |
else: |
| 62 |
rootdir = temproot |
| 63 |
rootdir.ensure(dir=1) |
| 64 |
basetemp = py.path.local.make_numbered_dir(prefix='pytest-', |
| 65 |
rootdir=rootdir) |
| 66 |
self._basetemp = t = basetemp.realpath() |
| 67 |
self.trace("new basetemp", t) |
| 68 |
return t |
| 69 |
|
| 70 |
def finish(self): |
| 71 |
self.trace("finish") |
| 72 |
|
| 73 |
|
| 74 |
def get_user(): |
| 75 |
"""Return the current user name, or None if getuser() does not work |
| 76 |
in the current environment (see #1010). |
| 77 |
""" |
| 78 |
import getpass |
| 79 |
try: |
| 80 |
return getpass.getuser() |
| 81 |
except (ImportError, KeyError): |
| 82 |
return None |
| 83 |
|
| 84 |
|
| 85 |
TempdirHandler = TempdirFactory |
| 86 |
|
| 87 |
|
| 88 |
def pytest_configure(config): |
| 89 |
"""Create a TempdirFactory and attach it to the config object. |
| 90 |
|
| 91 |
This is to comply with existing plugins which expect the handler to be |
| 92 |
available at pytest_configure time, but ideally should be moved entirely |
| 93 |
to the tmpdir_factory session fixture. |
| 94 |
""" |
| 95 |
mp = monkeypatch() |
| 96 |
t = TempdirFactory(config) |
| 97 |
config._cleanup.extend([mp.undo, t.finish]) |
| 98 |
mp.setattr(config, '_tmpdirhandler', t, raising=False) |
| 99 |
mp.setattr(pytest, 'ensuretemp', t.ensuretemp, raising=False) |
| 100 |
|
| 101 |
|
| 102 |
@pytest.fixture(scope='session') |
| 103 |
def tmpdir_factory(request): |
| 104 |
"""Return a TempdirFactory instance for the test session. |
| 105 |
""" |
| 106 |
return request.config._tmpdirhandler |
| 107 |
|
| 108 |
|
| 109 |
@pytest.fixture |
| 110 |
def tmpdir(request, tmpdir_factory): |
| 111 |
"""return a temporary directory path object |
| 112 |
which is unique to each test function invocation, |
| 113 |
created as a sub directory of the base temporary |
| 114 |
directory. The returned object is a `py.path.local`_ |
| 115 |
path object. |
| 116 |
""" |
| 117 |
name = request.node.name |
| 118 |
name = re.sub("[\W]", "_", name) |
| 119 |
MAXVAL = 30 |
| 120 |
if len(name) > MAXVAL: |
| 121 |
name = name[:MAXVAL] |
| 122 |
x = tmpdir_factory.mktemp(name, numbered=True) |
| 123 |
return x |