testing/conftest.py: Reintialize config._openfiles for each test

And no longer need getopenfiles or config._numfiles

--HG--
branch : refactor_LsofFdLeakChecker
This commit is contained in:
Marc Abramowitz 2014-04-01 14:13:11 -07:00
parent 064e79761c
commit e45a33f029
1 changed files with 3 additions and 12 deletions

View File

@ -38,7 +38,8 @@ def pytest_addoption(parser):
action="store_true", dest="lsof", default=False, action="store_true", dest="lsof", default=False,
help=("run FD checks if lsof is available")) help=("run FD checks if lsof is available"))
def pytest_configure(config): def pytest_runtest_setup(item):
config = item.config
config._basedir = py.path.local() config._basedir = py.path.local()
if config.getvalue("lsof"): if config.getvalue("lsof"):
try: try:
@ -46,18 +47,10 @@ def pytest_configure(config):
config._openfiles = config._fd_leak_checker.get_open_files() config._openfiles = config._fd_leak_checker.get_open_files()
except py.process.cmdexec.Error: except py.process.cmdexec.Error:
pass pass
else:
config._numfiles = len(config._openfiles)
#def pytest_report_header(): #def pytest_report_header():
# return "pid: %s" % os.getpid() # return "pid: %s" % os.getpid()
def getopenfiles(out):
def isopen(line):
return ("REG" in line or "CHR" in line) and (
"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)]
def check_open_files(config): def check_open_files(config):
lines2 = config._fd_leak_checker.get_open_files() lines2 = config._fd_leak_checker.get_open_files()
new_fds = sorted(set([t[0] for t in lines2]) - set([t[0] for t in config._openfiles])) new_fds = sorted(set([t[0] for t in lines2]) - set([t[0] for t in config._openfiles]))
@ -71,13 +64,11 @@ def check_open_files(config):
error.append("*** After:") error.append("*** After:")
error.extend([str(f) for f in lines2]) error.extend([str(f) for f in lines2])
error.append(error[0]) error.append(error[0])
# update numfile so that the overall test run continuess
config._numfiles = len(lines2)
raise AssertionError("\n".join(error)) raise AssertionError("\n".join(error))
def pytest_runtest_teardown(item, __multicall__): def pytest_runtest_teardown(item, __multicall__):
item.config._basedir.chdir() item.config._basedir.chdir()
if hasattr(item.config, '_numfiles'): if hasattr(item.config, '_openfiles'):
x = __multicall__.execute() x = __multicall__.execute()
check_open_files(item.config) check_open_files(item.config)
return x return x