2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
2010-10-07 17:59:00 +08:00
|
|
|
import sys
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2010-10-07 17:59:00 +08:00
|
|
|
pytest_plugins = "pytester",
|
|
|
|
|
|
|
|
import os, py
|
2010-10-26 15:11:53 +08:00
|
|
|
pid = os.getpid()
|
|
|
|
|
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addoption('--lsof',
|
|
|
|
action="store_true", dest="lsof", default=False,
|
|
|
|
help=("run FD checks if lsof is available"))
|
|
|
|
|
|
|
|
def pytest_configure(config):
|
2011-11-12 06:56:11 +08:00
|
|
|
config.addinivalue_line("markers",
|
|
|
|
"multi(arg=[value1,value2, ...]): call the test function "
|
|
|
|
"multiple times with arg=value1, then with arg=value2, ... "
|
|
|
|
)
|
2010-10-26 15:11:53 +08:00
|
|
|
if config.getvalue("lsof"):
|
|
|
|
try:
|
|
|
|
out = py.process.cmdexec("lsof -p %d" % pid)
|
|
|
|
except py.process.cmdexec.Error:
|
|
|
|
pass
|
|
|
|
else:
|
2011-11-08 02:08:41 +08:00
|
|
|
config._numfiles = len(getopenfiles(out))
|
2010-10-26 15:11:53 +08:00
|
|
|
|
|
|
|
#def pytest_report_header():
|
|
|
|
# return "pid: %s" % os.getpid()
|
|
|
|
|
|
|
|
def getopenfiles(out):
|
|
|
|
def isopen(line):
|
|
|
|
return ("REG" in line or "CHR" in line) and (
|
2011-11-08 02:08:41 +08:00
|
|
|
"deleted" not in line and 'mem' not in line and "txt" not in line)
|
|
|
|
return [x for x in out.split("\n") if isopen(x)]
|
2010-10-26 15:11:53 +08:00
|
|
|
|
2011-11-08 02:08:41 +08:00
|
|
|
def check_open_files(config):
|
2010-10-26 15:11:53 +08:00
|
|
|
out2 = py.process.cmdexec("lsof -p %d" % pid)
|
2011-11-08 02:08:41 +08:00
|
|
|
lines2 = getopenfiles(out2)
|
|
|
|
if len(lines2) > config._numfiles + 1:
|
|
|
|
error = []
|
|
|
|
error.append("***** %s FD leackage detected" %
|
|
|
|
(len(lines2)-config._numfiles))
|
|
|
|
error.extend(lines2)
|
|
|
|
error.append(error[0])
|
|
|
|
# update numfile so that the overall test run continuess
|
|
|
|
config._numfiles = len(lines2)
|
|
|
|
raise AssertionError("\n".join(error))
|
2010-10-26 05:08:41 +08:00
|
|
|
|
2012-06-21 17:30:10 +08:00
|
|
|
@pytest.mark.tryfirst # XXX rather do item.addfinalizer
|
2010-10-28 01:35:27 +08:00
|
|
|
def pytest_runtest_setup(item):
|
|
|
|
item._oldir = py.path.local()
|
|
|
|
|
2011-11-08 02:08:41 +08:00
|
|
|
def pytest_runtest_teardown(item, __multicall__):
|
2010-10-28 01:35:27 +08:00
|
|
|
item._oldir.chdir()
|
2011-11-08 02:08:41 +08:00
|
|
|
if hasattr(item.config, '_numfiles'):
|
|
|
|
x = __multicall__.execute()
|
|
|
|
check_open_files(item.config)
|
|
|
|
return x
|
2010-10-28 01:35:27 +08:00
|
|
|
|
2010-10-07 17:59:00 +08:00
|
|
|
def pytest_generate_tests(metafunc):
|
|
|
|
multi = getattr(metafunc.function, 'multi', None)
|
|
|
|
if multi is not None:
|
|
|
|
assert len(multi.kwargs) == 1
|
|
|
|
for name, l in multi.kwargs.items():
|
|
|
|
for val in l:
|
|
|
|
metafunc.addcall(funcargs={name: val})
|
2012-10-05 20:24:44 +08:00
|
|
|
elif 'anypython' in metafunc.fixturenames:
|
2010-10-07 17:59:00 +08:00
|
|
|
for name in ('python2.4', 'python2.5', 'python2.6',
|
2013-02-13 05:59:29 +08:00
|
|
|
'python2.7', 'python3.2', "python3.3",
|
|
|
|
'pypy', 'jython'):
|
2010-10-07 17:59:00 +08:00
|
|
|
metafunc.addcall(id=name, param=name)
|
|
|
|
|
|
|
|
# XXX copied from execnet's conftest.py - needs to be merged
|
|
|
|
winpymap = {
|
|
|
|
'python2.7': r'C:\Python27\python.exe',
|
|
|
|
'python2.6': r'C:\Python26\python.exe',
|
|
|
|
'python2.5': r'C:\Python25\python.exe',
|
|
|
|
'python2.4': r'C:\Python24\python.exe',
|
|
|
|
'python3.1': r'C:\Python31\python.exe',
|
|
|
|
}
|
|
|
|
|
|
|
|
def getexecutable(name, cache={}):
|
|
|
|
try:
|
|
|
|
return cache[name]
|
|
|
|
except KeyError:
|
|
|
|
executable = py.path.local.sysfind(name)
|
|
|
|
if executable:
|
|
|
|
if name == "jython":
|
|
|
|
import subprocess
|
|
|
|
popen = subprocess.Popen([str(executable), "--version"],
|
|
|
|
universal_newlines=True, stderr=subprocess.PIPE)
|
|
|
|
out, err = popen.communicate()
|
|
|
|
if not err or "2.5" not in err:
|
|
|
|
executable = None
|
2011-08-20 01:25:52 +08:00
|
|
|
if "2.5.2" in err:
|
|
|
|
executable = None # http://bugs.jython.org/issue1790
|
2010-10-07 17:59:00 +08:00
|
|
|
cache[name] = executable
|
|
|
|
return executable
|
|
|
|
|
|
|
|
def pytest_funcarg__anypython(request):
|
|
|
|
name = request.param
|
|
|
|
executable = getexecutable(name)
|
|
|
|
if executable is None:
|
|
|
|
if sys.platform == "win32":
|
|
|
|
executable = winpymap.get(name, None)
|
|
|
|
if executable:
|
|
|
|
executable = py.path.local(executable)
|
|
|
|
if executable.check():
|
|
|
|
return executable
|
2011-08-20 01:25:52 +08:00
|
|
|
pytest.skip("no suitable %s found" % (name,))
|
2010-10-07 17:59:00 +08:00
|
|
|
return executable
|