add indent facility to tracing
This commit is contained in:
parent
d108235095
commit
f181c70d8e
|
@ -19,18 +19,21 @@ default_plugins = (
|
|||
IMPORTPREFIX = "pytest_"
|
||||
|
||||
class TagTracer:
|
||||
def __init__(self):
|
||||
def __init__(self, prefix="[pytest] "):
|
||||
self._tag2proc = {}
|
||||
self.writer = None
|
||||
self.indent = 0
|
||||
self.prefix = prefix
|
||||
|
||||
def get(self, name):
|
||||
return TagTracerSub(self, (name,))
|
||||
|
||||
def processmessage(self, tags, args):
|
||||
if self.writer is not None:
|
||||
prefix = ":".join(tags)
|
||||
if args:
|
||||
indent = " " * self.indent
|
||||
content = " ".join(map(str, args))
|
||||
self.writer("[%s] %s\n" %(prefix, content))
|
||||
self.writer("%s%s%s\n" %(self.prefix, indent, content))
|
||||
try:
|
||||
self._tag2proc[tags](tags, args)
|
||||
except KeyError:
|
||||
|
@ -62,7 +65,7 @@ class PluginManager(object):
|
|||
self._name2plugin = {}
|
||||
self._plugins = []
|
||||
self._hints = []
|
||||
self.trace = TagTracer().get("pytest")
|
||||
self.trace = TagTracer().get("pluginmanage")
|
||||
if os.environ.get('PYTEST_DEBUG'):
|
||||
self.trace.root.setwriter(sys.stderr.write)
|
||||
self.hook = HookRelay([hookspec], pm=self)
|
||||
|
@ -340,6 +343,7 @@ class HookRelay:
|
|||
hookspecs = [hookspecs]
|
||||
self._hookspecs = []
|
||||
self._pm = pm
|
||||
self.trace = pm.trace.root.get("hook")
|
||||
for hookspec in hookspecs:
|
||||
self._addhooks(hookspec, prefix)
|
||||
|
||||
|
@ -376,6 +380,7 @@ class HookCaller:
|
|||
return mc.execute()
|
||||
|
||||
def pcall(self, plugins, **kwargs):
|
||||
self.hookrelay.trace(self.name, kwargs)
|
||||
methods = self.hookrelay._pm.listattr(self.name, plugins=plugins)
|
||||
mc = MultiCall(methods, kwargs, firstresult=self.firstresult)
|
||||
return mc.execute()
|
||||
|
|
|
@ -553,17 +553,39 @@ class TestHookRelay:
|
|||
class TestTracer:
|
||||
def test_simple(self):
|
||||
from pytest.main import TagTracer
|
||||
rootlogger = TagTracer()
|
||||
rootlogger = TagTracer("[my] ")
|
||||
log = rootlogger.get("pytest")
|
||||
log("hello")
|
||||
l = []
|
||||
rootlogger.setwriter(l.append)
|
||||
log("world")
|
||||
assert len(l) == 1
|
||||
assert l[0] == "[pytest] world\n"
|
||||
assert l[0] == "[my] world\n"
|
||||
sublog = log.get("collection")
|
||||
sublog("hello")
|
||||
assert l[1] == "[pytest:collection] hello\n"
|
||||
assert l[1] == "[my] hello\n"
|
||||
|
||||
def test_indent(self):
|
||||
from pytest.main import TagTracer
|
||||
rootlogger = TagTracer()
|
||||
log = rootlogger.get("1")
|
||||
l = []
|
||||
log.root.setwriter(lambda arg: l.append(arg))
|
||||
log("hello")
|
||||
log.root.indent += 1
|
||||
log("line1")
|
||||
log("line2")
|
||||
log.root.indent += 1
|
||||
log("line3")
|
||||
log("line4")
|
||||
log.root.indent -= 1
|
||||
log("line5")
|
||||
log.root.indent -= 1
|
||||
log("last")
|
||||
assert len(l) == 7
|
||||
names = [x.rstrip()[len(rootlogger.prefix):] for x in l]
|
||||
assert names == ['hello', ' line1', ' line2',
|
||||
' line3', ' line4', ' line5', 'last']
|
||||
|
||||
def test_setprocessor(self):
|
||||
from pytest.main import TagTracer
|
||||
|
|
Loading…
Reference in New Issue