Skip to content

Commit f181c70

Browse files
committed
add indent facility to tracing
1 parent d108235 commit f181c70

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

pytest/main.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,21 @@
1919
IMPORTPREFIX = "pytest_"
2020

2121
class TagTracer:
22-
def __init__(self):
22+
def __init__(self, prefix="[pytest] "):
2323
self._tag2proc = {}
2424
self.writer = None
25+
self.indent = 0
26+
self.prefix = prefix
2527

2628
def get(self, name):
2729
return TagTracerSub(self, (name,))
2830

2931
def processmessage(self, tags, args):
3032
if self.writer is not None:
31-
prefix = ":".join(tags)
32-
content = " ".join(map(str, args))
33-
self.writer("[%s] %s\n" %(prefix, content))
33+
if args:
34+
indent = " " * self.indent
35+
content = " ".join(map(str, args))
36+
self.writer("%s%s%s\n" %(self.prefix, indent, content))
3437
try:
3538
self._tag2proc[tags](tags, args)
3639
except KeyError:
@@ -62,7 +65,7 @@ def __init__(self, load=False):
6265
self._name2plugin = {}
6366
self._plugins = []
6467
self._hints = []
65-
self.trace = TagTracer().get("pytest")
68+
self.trace = TagTracer().get("pluginmanage")
6669
if os.environ.get('PYTEST_DEBUG'):
6770
self.trace.root.setwriter(sys.stderr.write)
6871
self.hook = HookRelay([hookspec], pm=self)
@@ -340,6 +343,7 @@ def __init__(self, hookspecs, pm, prefix="pytest_"):
340343
hookspecs = [hookspecs]
341344
self._hookspecs = []
342345
self._pm = pm
346+
self.trace = pm.trace.root.get("hook")
343347
for hookspec in hookspecs:
344348
self._addhooks(hookspec, prefix)
345349

@@ -376,6 +380,7 @@ def __call__(self, **kwargs):
376380
return mc.execute()
377381

378382
def pcall(self, plugins, **kwargs):
383+
self.hookrelay.trace(self.name, kwargs)
379384
methods = self.hookrelay._pm.listattr(self.name, plugins=plugins)
380385
mc = MultiCall(methods, kwargs, firstresult=self.firstresult)
381386
return mc.execute()

testing/test_main.py

+25-3
Original file line numberDiff line numberDiff line change
@@ -553,17 +553,39 @@ def hello(self, arg):
553553
class TestTracer:
554554
def test_simple(self):
555555
from pytest.main import TagTracer
556-
rootlogger = TagTracer()
556+
rootlogger = TagTracer("[my] ")
557557
log = rootlogger.get("pytest")
558558
log("hello")
559559
l = []
560560
rootlogger.setwriter(l.append)
561561
log("world")
562562
assert len(l) == 1
563-
assert l[0] == "[pytest] world\n"
563+
assert l[0] == "[my] world\n"
564564
sublog = log.get("collection")
565565
sublog("hello")
566-
assert l[1] == "[pytest:collection] hello\n"
566+
assert l[1] == "[my] hello\n"
567+
568+
def test_indent(self):
569+
from pytest.main import TagTracer
570+
rootlogger = TagTracer()
571+
log = rootlogger.get("1")
572+
l = []
573+
log.root.setwriter(lambda arg: l.append(arg))
574+
log("hello")
575+
log.root.indent += 1
576+
log("line1")
577+
log("line2")
578+
log.root.indent += 1
579+
log("line3")
580+
log("line4")
581+
log.root.indent -= 1
582+
log("line5")
583+
log.root.indent -= 1
584+
log("last")
585+
assert len(l) == 7
586+
names = [x.rstrip()[len(rootlogger.prefix):] for x in l]
587+
assert names == ['hello', ' line1', ' line2',
588+
' line3', ' line4', ' line5', 'last']
567589

568590
def test_setprocessor(self):
569591
from pytest.main import TagTracer

0 commit comments

Comments
 (0)