Skip to content

Commit

Permalink
add indent facility to tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
hpk42 committed Nov 6, 2010
1 parent d108235 commit f181c70
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
15 changes: 10 additions & 5 deletions pytest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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()
Expand Down
28 changes: 25 additions & 3 deletions testing/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f181c70

Please sign in to comment.