add indent facility to tracing

This commit is contained in:
holger krekel 2010-11-06 09:05:17 +01:00
parent d108235095
commit f181c70d8e
2 changed files with 35 additions and 8 deletions

View File

@ -19,18 +19,21 @@ default_plugins = (
IMPORTPREFIX = "pytest_" IMPORTPREFIX = "pytest_"
class TagTracer: class TagTracer:
def __init__(self): def __init__(self, prefix="[pytest] "):
self._tag2proc = {} self._tag2proc = {}
self.writer = None self.writer = None
self.indent = 0
self.prefix = prefix
def get(self, name): def get(self, name):
return TagTracerSub(self, (name,)) return TagTracerSub(self, (name,))
def processmessage(self, tags, args): def processmessage(self, tags, args):
if self.writer is not None: if self.writer is not None:
prefix = ":".join(tags) if args:
content = " ".join(map(str, args)) indent = " " * self.indent
self.writer("[%s] %s\n" %(prefix, content)) content = " ".join(map(str, args))
self.writer("%s%s%s\n" %(self.prefix, indent, content))
try: try:
self._tag2proc[tags](tags, args) self._tag2proc[tags](tags, args)
except KeyError: except KeyError:
@ -62,7 +65,7 @@ class PluginManager(object):
self._name2plugin = {} self._name2plugin = {}
self._plugins = [] self._plugins = []
self._hints = [] self._hints = []
self.trace = TagTracer().get("pytest") self.trace = TagTracer().get("pluginmanage")
if os.environ.get('PYTEST_DEBUG'): if os.environ.get('PYTEST_DEBUG'):
self.trace.root.setwriter(sys.stderr.write) self.trace.root.setwriter(sys.stderr.write)
self.hook = HookRelay([hookspec], pm=self) self.hook = HookRelay([hookspec], pm=self)
@ -340,6 +343,7 @@ class HookRelay:
hookspecs = [hookspecs] hookspecs = [hookspecs]
self._hookspecs = [] self._hookspecs = []
self._pm = pm self._pm = pm
self.trace = pm.trace.root.get("hook")
for hookspec in hookspecs: for hookspec in hookspecs:
self._addhooks(hookspec, prefix) self._addhooks(hookspec, prefix)
@ -376,6 +380,7 @@ class HookCaller:
return mc.execute() return mc.execute()
def pcall(self, plugins, **kwargs): def pcall(self, plugins, **kwargs):
self.hookrelay.trace(self.name, kwargs)
methods = self.hookrelay._pm.listattr(self.name, plugins=plugins) methods = self.hookrelay._pm.listattr(self.name, plugins=plugins)
mc = MultiCall(methods, kwargs, firstresult=self.firstresult) mc = MultiCall(methods, kwargs, firstresult=self.firstresult)
return mc.execute() return mc.execute()

View File

@ -553,17 +553,39 @@ class TestHookRelay:
class TestTracer: class TestTracer:
def test_simple(self): def test_simple(self):
from pytest.main import TagTracer from pytest.main import TagTracer
rootlogger = TagTracer() rootlogger = TagTracer("[my] ")
log = rootlogger.get("pytest") log = rootlogger.get("pytest")
log("hello") log("hello")
l = [] l = []
rootlogger.setwriter(l.append) rootlogger.setwriter(l.append)
log("world") log("world")
assert len(l) == 1 assert len(l) == 1
assert l[0] == "[pytest] world\n" assert l[0] == "[my] world\n"
sublog = log.get("collection") sublog = log.get("collection")
sublog("hello") 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): def test_setprocessor(self):
from pytest.main import TagTracer from pytest.main import TagTracer