streamline pluginmanager api and test/beautify printing of plugins with --trace
--HG-- branch : trunk
This commit is contained in:
parent
6f5918f03b
commit
eab7e039eb
|
@ -38,7 +38,7 @@ def pytest_configure(__multicall__, config):
|
||||||
__multicall__.execute()
|
__multicall__.execute()
|
||||||
if config.option.pastebin == "all":
|
if config.option.pastebin == "all":
|
||||||
config._pastebinfile = tempfile.TemporaryFile('w+')
|
config._pastebinfile = tempfile.TemporaryFile('w+')
|
||||||
tr = config.pluginmanager.impname2plugin['terminalreporter']
|
tr = config.pluginmanager.getplugin('terminalreporter')
|
||||||
oldwrite = tr._tw.write
|
oldwrite = tr._tw.write
|
||||||
def tee_write(s, **kwargs):
|
def tee_write(s, **kwargs):
|
||||||
oldwrite(s, **kwargs)
|
oldwrite(s, **kwargs)
|
||||||
|
@ -54,7 +54,7 @@ def pytest_unconfigure(config):
|
||||||
proxyid = getproxy().newPaste("python", sessionlog)
|
proxyid = getproxy().newPaste("python", sessionlog)
|
||||||
pastebinurl = "%s%s" % (url.show, proxyid)
|
pastebinurl = "%s%s" % (url.show, proxyid)
|
||||||
sys.stderr.write("pastebin session-log: %s\n" % pastebinurl)
|
sys.stderr.write("pastebin session-log: %s\n" % pastebinurl)
|
||||||
tr = config.pluginmanager.impname2plugin['terminalreporter']
|
tr = config.pluginmanager.getplugin('terminalreporter')
|
||||||
del tr._tw.__dict__['write']
|
del tr._tw.__dict__['write']
|
||||||
|
|
||||||
def getproxy():
|
def getproxy():
|
||||||
|
|
|
@ -28,8 +28,8 @@ def pytest_configure(config):
|
||||||
class PdbInvoke:
|
class PdbInvoke:
|
||||||
def pytest_runtest_makereport(self, item, call):
|
def pytest_runtest_makereport(self, item, call):
|
||||||
if call.excinfo and not call.excinfo.errisinstance(Skipped):
|
if call.excinfo and not call.excinfo.errisinstance(Skipped):
|
||||||
# XXX hack hack hack to play well with capturing
|
# play well with capturing, slightly hackish
|
||||||
capman = item.config.pluginmanager.impname2plugin['capturemanager']
|
capman = item.config.pluginmanager.getplugin('capturemanager')
|
||||||
capman.suspendcapture()
|
capman.suspendcapture()
|
||||||
|
|
||||||
tw = py.io.TerminalWriter()
|
tw = py.io.TerminalWriter()
|
||||||
|
@ -37,7 +37,6 @@ class PdbInvoke:
|
||||||
repr.toterminal(tw)
|
repr.toterminal(tw)
|
||||||
post_mortem(call.excinfo._excinfo[2])
|
post_mortem(call.excinfo._excinfo[2])
|
||||||
|
|
||||||
# XXX hack end
|
|
||||||
capman.resumecapture_item(item)
|
capman.resumecapture_item(item)
|
||||||
|
|
||||||
class Pdb(py.std.pdb.Pdb):
|
class Pdb(py.std.pdb.Pdb):
|
||||||
|
|
|
@ -189,7 +189,6 @@ class TmpTestdir:
|
||||||
plugin = PseudoPlugin(plugin)
|
plugin = PseudoPlugin(plugin)
|
||||||
if not config.pluginmanager.isregistered(plugin):
|
if not config.pluginmanager.isregistered(plugin):
|
||||||
config.pluginmanager.register(plugin)
|
config.pluginmanager.register(plugin)
|
||||||
#print "config.pluginmanager.impname2plugin", config.pluginmanager.impname2plugin
|
|
||||||
return config
|
return config
|
||||||
|
|
||||||
def parseconfig(self, *args):
|
def parseconfig(self, *args):
|
||||||
|
|
|
@ -236,14 +236,15 @@ class TerminalReporter:
|
||||||
if self.config.option.debug or self.config.option.traceconfig:
|
if self.config.option.debug or self.config.option.traceconfig:
|
||||||
self.write_line("using py lib: %s" % (py.path.local(py.__file__).dirpath()))
|
self.write_line("using py lib: %s" % (py.path.local(py.__file__).dirpath()))
|
||||||
if self.config.option.traceconfig:
|
if self.config.option.traceconfig:
|
||||||
|
self.write_line("active plugins:")
|
||||||
plugins = []
|
plugins = []
|
||||||
for plugin in self.config.pluginmanager.comregistry:
|
items = self.config.pluginmanager._name2plugin.items()
|
||||||
name = getattr(plugin, '__name__', None)
|
for name, plugin in items:
|
||||||
if name is None:
|
repr_plugin = repr(plugin)
|
||||||
name = plugin.__class__.__name__
|
fullwidth = getattr(self._tw, 'fullwidth', sys.maxint)
|
||||||
plugins.append(name)
|
if len(repr_plugin)+26 > fullwidth:
|
||||||
plugins = ", ".join(plugins)
|
repr_plugin = repr_plugin[:(fullwidth-30)] + '...'
|
||||||
self.write_line("active plugins: %s" %(plugins,))
|
self.write_line(" %-20s: %s" %(name, repr_plugin))
|
||||||
for i, testarg in enumerate(self.config.args):
|
for i, testarg in enumerate(self.config.args):
|
||||||
self.write_line("test object %d: %s" %(i+1, testarg))
|
self.write_line("test object %d: %s" %(i+1, testarg))
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,7 @@ class PluginManager(object):
|
||||||
if comregistry is None:
|
if comregistry is None:
|
||||||
comregistry = py._com.Registry()
|
comregistry = py._com.Registry()
|
||||||
self.comregistry = comregistry
|
self.comregistry = comregistry
|
||||||
self.impname2plugin = {}
|
self._name2plugin = {}
|
||||||
|
|
||||||
self.hook = py._com.HookRelay(
|
self.hook = py._com.HookRelay(
|
||||||
hookspecs=hookspec,
|
hookspecs=hookspec,
|
||||||
|
@ -33,9 +33,9 @@ class PluginManager(object):
|
||||||
def register(self, plugin, name=None):
|
def register(self, plugin, name=None):
|
||||||
assert not self.isregistered(plugin)
|
assert not self.isregistered(plugin)
|
||||||
name = self._getpluginname(plugin, name)
|
name = self._getpluginname(plugin, name)
|
||||||
if name in self.impname2plugin:
|
if name in self._name2plugin:
|
||||||
return False
|
return False
|
||||||
self.impname2plugin[name] = plugin
|
self._name2plugin[name] = plugin
|
||||||
self.hook.pytest_plugin_registered(plugin=plugin)
|
self.hook.pytest_plugin_registered(plugin=plugin)
|
||||||
self._checkplugin(plugin)
|
self._checkplugin(plugin)
|
||||||
self.comregistry.register(plugin)
|
self.comregistry.register(plugin)
|
||||||
|
@ -44,19 +44,26 @@ class PluginManager(object):
|
||||||
def unregister(self, plugin):
|
def unregister(self, plugin):
|
||||||
self.hook.pytest_plugin_unregistered(plugin=plugin)
|
self.hook.pytest_plugin_unregistered(plugin=plugin)
|
||||||
self.comregistry.unregister(plugin)
|
self.comregistry.unregister(plugin)
|
||||||
for name, value in list(self.impname2plugin.items()):
|
for name, value in list(self._name2plugin.items()):
|
||||||
if value == plugin:
|
if value == plugin:
|
||||||
del self.impname2plugin[name]
|
del self._name2plugin[name]
|
||||||
|
|
||||||
def isregistered(self, plugin, name=None):
|
def isregistered(self, plugin, name=None):
|
||||||
return self._getpluginname(plugin, name) in self.impname2plugin
|
if self._getpluginname(plugin, name) in self._name2plugin:
|
||||||
|
return True
|
||||||
|
for val in self._name2plugin.values():
|
||||||
|
if plugin == val:
|
||||||
|
return True
|
||||||
|
|
||||||
def getplugins(self):
|
def getplugins(self):
|
||||||
return list(self.comregistry)
|
return list(self.comregistry)
|
||||||
|
|
||||||
def getplugin(self, importname):
|
def getplugin(self, name):
|
||||||
impname = canonical_importname(importname)
|
try:
|
||||||
return self.impname2plugin[impname]
|
return self._name2plugin[name]
|
||||||
|
except KeyError:
|
||||||
|
impname = canonical_importname(name)
|
||||||
|
return self._name2plugin[impname]
|
||||||
|
|
||||||
# API for bootstrapping
|
# API for bootstrapping
|
||||||
#
|
#
|
||||||
|
@ -94,7 +101,7 @@ class PluginManager(object):
|
||||||
def import_plugin(self, spec):
|
def import_plugin(self, spec):
|
||||||
assert isinstance(spec, str)
|
assert isinstance(spec, str)
|
||||||
modname = canonical_importname(spec)
|
modname = canonical_importname(spec)
|
||||||
if modname in self.impname2plugin:
|
if modname in self._name2plugin:
|
||||||
return
|
return
|
||||||
try:
|
try:
|
||||||
mod = importplugin(modname)
|
mod = importplugin(modname)
|
||||||
|
|
|
@ -6,7 +6,7 @@ class TestPasting:
|
||||||
class MockProxy:
|
class MockProxy:
|
||||||
def newPaste(self, language, code):
|
def newPaste(self, language, code):
|
||||||
pastebinlist.append((language, code))
|
pastebinlist.append((language, code))
|
||||||
plugin = request.config.pluginmanager.impname2plugin['pytest_pastebin']
|
plugin = request.config.pluginmanager.getplugin('pastebin')
|
||||||
mp.setattr(plugin, 'getproxy', MockProxy)
|
mp.setattr(plugin, 'getproxy', MockProxy)
|
||||||
return pastebinlist
|
return pastebinlist
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,7 @@ class TestPDB:
|
||||||
pdblist = []
|
pdblist = []
|
||||||
def mypdb(*args):
|
def mypdb(*args):
|
||||||
pdblist.append(args)
|
pdblist.append(args)
|
||||||
plugin = request.config.pluginmanager.impname2plugin['pytest_pdb']
|
plugin = request.config.pluginmanager.getplugin('pdb')
|
||||||
monkeypatch.setattr(plugin, 'post_mortem', mypdb)
|
monkeypatch.setattr(plugin, 'post_mortem', mypdb)
|
||||||
return pdblist
|
return pdblist
|
||||||
|
|
||||||
|
|
|
@ -613,3 +613,9 @@ class TestTerminalFunctional:
|
||||||
])
|
])
|
||||||
assert result.ret == 1
|
assert result.ret == 1
|
||||||
|
|
||||||
|
def test_trace_reporting(self, testdir):
|
||||||
|
result = testdir.runpytest("--trace")
|
||||||
|
assert result.stdout.fnmatch_lines([
|
||||||
|
"*active plugins*"
|
||||||
|
])
|
||||||
|
assert result.ret == 0
|
||||||
|
|
|
@ -121,9 +121,10 @@ class TestBootstrapping:
|
||||||
a1, a2 = A(), A()
|
a1, a2 = A(), A()
|
||||||
pp.register(a1)
|
pp.register(a1)
|
||||||
assert pp.isregistered(a1)
|
assert pp.isregistered(a1)
|
||||||
pp.register(a2)
|
pp.register(a2, "hello")
|
||||||
assert pp.isregistered(a2)
|
assert pp.isregistered(a2)
|
||||||
assert pp.getplugins() == [a1, a2]
|
assert pp.getplugins() == [a1, a2]
|
||||||
|
assert pp.getplugin('hello') == a2
|
||||||
pp.unregister(a1)
|
pp.unregister(a1)
|
||||||
assert not pp.isregistered(a1)
|
assert not pp.isregistered(a1)
|
||||||
pp.unregister(a2)
|
pp.unregister(a2)
|
||||||
|
@ -142,6 +143,15 @@ class TestBootstrapping:
|
||||||
#assert not pp.isregistered(mod2)
|
#assert not pp.isregistered(mod2)
|
||||||
assert pp.getplugins() == [mod] # does not actually modify plugins
|
assert pp.getplugins() == [mod] # does not actually modify plugins
|
||||||
|
|
||||||
|
def test_canonical_import(self, monkeypatch):
|
||||||
|
mod = py.std.types.ModuleType("pytest_xyz")
|
||||||
|
monkeypatch.setitem(py.std.sys.modules, 'pytest_xyz', mod)
|
||||||
|
pp = PluginManager()
|
||||||
|
pp.import_plugin('xyz')
|
||||||
|
assert pp.getplugin('xyz') == mod
|
||||||
|
assert pp.getplugin('pytest_xyz') == mod
|
||||||
|
assert pp.isregistered(mod)
|
||||||
|
|
||||||
def test_register_mismatch_method(self):
|
def test_register_mismatch_method(self):
|
||||||
pp = PluginManager()
|
pp = PluginManager()
|
||||||
class hello:
|
class hello:
|
||||||
|
|
Loading…
Reference in New Issue