[svn r57565] also introduce config.maketrace(name, flush=False) which

returns either a nulltracer or opens a log in the tracedir
and returns an object that you can call with args to print
into the file.

--HG--
branch : trunk
This commit is contained in:
hpk 2008-08-21 19:39:34 +02:00
parent bf42c88e48
commit 7adfbfa166
2 changed files with 43 additions and 1 deletions

View File

@ -226,6 +226,33 @@ class Config(object):
if self.option.tracedir is not None:
return py.path.local(self.option.tracedir)
def maketrace(self, name, flush=False):
""" return a tracedirectory or None, depending on --tracedir. """
tracedir = self.gettracedir()
if tracedir is None:
return NullTracer()
return Tracer(tracedir.join(name), flush=flush)
class Tracer(object):
file = None
def __init__(self, path, flush=False):
self.file = path.open(mode='w')
self.flush = flush
def __call__(self, *args):
print >>self.file, " ".join(map(str, args))
if self.flush:
self.file.flush()
def close(self):
self.file.close()
class NullTracer:
def __call__(self, *args):
pass
def close(self):
pass
# this is the one per-process instance of py.test configuration
config_per_process = Config()

View File

@ -201,13 +201,28 @@ class TestSessionAndOptions:
s = eventlog.read()
assert s.find("TestrunStart") != -1
def test_tracedir(self):
def test_tracedir_tracer(self):
tracedir = self.tmpdir.mkdir("tracedir")
config = py.test.config._reparse([self.tmpdir,
'--tracedir=%s' % tracedir])
assert config.gettracedir() == tracedir
trace = config.maketrace("trace1.log", flush=True)
trace("hello", "world")
class A: pass
trace(A())
p = tracedir.join("trace1.log")
lines = p.readlines(cr=0)
assert lines[0] == "hello world"
assert lines[1].find("A") != -1
trace.close()
def test_trace_null(self):
config = py.test.config._reparse([self.tmpdir])
assert config.gettracedir() is None
trace = config.maketrace("hello", flush=True)
trace("hello", "world")
trace.close()
def test_implied_dsession(self):
for x in 'startserver runbrowser rest'.split():