2009-03-18 23:51:55 +08:00
|
|
|
import py
|
|
|
|
|
|
|
|
class ExecnetcleanupPlugin:
|
|
|
|
_gateways = None
|
|
|
|
_debug = None
|
|
|
|
|
|
|
|
def pytest_configure(self, config):
|
|
|
|
self._debug = config.option.debug
|
|
|
|
|
|
|
|
def trace(self, msg, *args):
|
|
|
|
if self._debug:
|
|
|
|
print "[execnetcleanup %0x] %s %s" %(id(self), msg, args)
|
|
|
|
|
2009-04-09 01:50:14 +08:00
|
|
|
def pyexecnet_gateway_init(self, gateway):
|
2009-03-18 23:51:55 +08:00
|
|
|
self.trace("init", gateway)
|
|
|
|
if self._gateways is not None:
|
|
|
|
self._gateways.append(gateway)
|
|
|
|
|
2009-04-09 01:50:14 +08:00
|
|
|
def pyexecnet_gateway_exit(self, gateway):
|
2009-03-18 23:51:55 +08:00
|
|
|
self.trace("exit", gateway)
|
|
|
|
if self._gateways is not None:
|
|
|
|
self._gateways.remove(gateway)
|
|
|
|
|
2009-04-09 07:41:35 +08:00
|
|
|
def pytest_testrunstart(self):
|
2009-04-03 18:57:34 +08:00
|
|
|
self.trace("testrunstart")
|
2009-03-18 23:51:55 +08:00
|
|
|
self._gateways = []
|
|
|
|
|
2009-04-09 07:41:35 +08:00
|
|
|
def pytest_testrunfinish(self, exitstatus, excrepr=None):
|
2009-04-03 20:18:25 +08:00
|
|
|
self.trace("testrunfinish", exitstatus)
|
2009-03-18 23:51:55 +08:00
|
|
|
l = []
|
|
|
|
for gw in self._gateways:
|
|
|
|
gw.exit()
|
|
|
|
l.append(gw)
|
2009-03-19 01:54:45 +08:00
|
|
|
#for gw in l:
|
|
|
|
# gw.join()
|
2009-04-03 03:47:01 +08:00
|
|
|
#
|
|
|
|
def pytest_pyfunc_call(self, __call__, pyfuncitem, args, kwargs):
|
|
|
|
if self._gateways is not None:
|
|
|
|
gateways = self._gateways[:]
|
|
|
|
res = __call__.execute(firstresult=True)
|
|
|
|
while len(self._gateways) > len(gateways):
|
|
|
|
self._gateways[-1].exit()
|
|
|
|
return res
|
2009-03-18 23:51:55 +08:00
|
|
|
|
|
|
|
def test_generic(plugintester):
|
2009-05-08 00:01:53 +08:00
|
|
|
plugintester.hookcheck(ExecnetcleanupPlugin)
|
2009-03-18 23:51:55 +08:00
|
|
|
|
|
|
|
@py.test.mark.xfail("clarify plugin registration/unregistration")
|
|
|
|
def test_execnetplugin(testdir):
|
|
|
|
p = ExecnetcleanupPlugin()
|
|
|
|
testdir.plugins.append(p)
|
|
|
|
testdir.inline_runsource("""
|
|
|
|
import py
|
|
|
|
import sys
|
|
|
|
def test_hello():
|
|
|
|
sys._gw = py.execnet.PopenGateway()
|
|
|
|
""", "-s", "--debug")
|
|
|
|
assert not p._gateways
|
|
|
|
assert py.std.sys._gw
|
|
|
|
py.test.raises(KeyError, "py.std.sys._gw.exit()") # already closed
|
|
|
|
|