~ibmcharmers/charms/trusty/ibm-dsm-base/trunk

« back to all changes in this revision

Viewing changes to .tox/py35/lib/python3.5/site-packages/_pytest/tmpdir.py

  • Committer: anita nayak
  • Date: 2016-12-08 14:39:17 UTC
  • Revision ID: anitanayak@in.ibm.com-20161208143917-3pcyiz4cmbey1fol
Initial Check in for ibm-dsm-base trusty

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
        #py.log._apiwarn(">1.1", "use tmpdir function argument")
 
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
                    # use a sub-directory in the temproot to speed-up
 
59
                    # make_numbered_dir() call
 
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
# backward compatibility
 
86
TempdirHandler = TempdirFactory
 
87
 
 
88
 
 
89
def pytest_configure(config):
 
90
    """Create a TempdirFactory and attach it to the config object.
 
91
 
 
92
    This is to comply with existing plugins which expect the handler to be
 
93
    available at pytest_configure time, but ideally should be moved entirely
 
94
    to the tmpdir_factory session fixture.
 
95
    """
 
96
    mp = MonkeyPatch()
 
97
    t = TempdirFactory(config)
 
98
    config._cleanup.extend([mp.undo, t.finish])
 
99
    mp.setattr(config, '_tmpdirhandler', t, raising=False)
 
100
    mp.setattr(pytest, 'ensuretemp', t.ensuretemp, raising=False)
 
101
 
 
102
 
 
103
@pytest.fixture(scope='session')
 
104
def tmpdir_factory(request):
 
105
    """Return a TempdirFactory instance for the test session.
 
106
    """
 
107
    return request.config._tmpdirhandler
 
108
 
 
109
 
 
110
@pytest.fixture
 
111
def tmpdir(request, tmpdir_factory):
 
112
    """Return a temporary directory path object
 
113
    which is unique to each test function invocation,
 
114
    created as a sub directory of the base temporary
 
115
    directory.  The returned object is a `py.path.local`_
 
116
    path object.
 
117
    """
 
118
    name = request.node.name
 
119
    name = re.sub("[\W]", "_", name)
 
120
    MAXVAL = 30
 
121
    if len(name) > MAXVAL:
 
122
        name = name[:MAXVAL]
 
123
    x = tmpdir_factory.mktemp(name, numbered=True)
 
124
    return x