From f181c70d8ee2c5f876489e36071b47aa77ac3e13 Mon Sep 17 00:00:00 2001 From: holger krekel Date: Sat, 6 Nov 2010 09:05:17 +0100 Subject: [PATCH] add indent facility to tracing --- pytest/main.py | 15 ++++++++++----- testing/test_main.py | 28 +++++++++++++++++++++++++--- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/pytest/main.py b/pytest/main.py index 2b08a240f71..9f4825cd361 100644 --- a/pytest/main.py +++ b/pytest/main.py @@ -19,18 +19,21 @@ 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) - content = " ".join(map(str, args)) - self.writer("[%s] %s\n" %(prefix, content)) + if args: + indent = " " * self.indent + content = " ".join(map(str, args)) + self.writer("%s%s%s\n" %(self.prefix, indent, content)) try: self._tag2proc[tags](tags, args) except KeyError: @@ -62,7 +65,7 @@ def __init__(self, load=False): 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 @@ def __init__(self, hookspecs, pm, prefix="pytest_"): 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 @@ def __call__(self, **kwargs): 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() diff --git a/testing/test_main.py b/testing/test_main.py index 4f8a754d7f7..b2532104c7c 100644 --- a/testing/test_main.py +++ b/testing/test_main.py @@ -553,17 +553,39 @@ def hello(self, arg): 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