From 7adfbfa1660749c279ad673c28ae795f3217d4cc Mon Sep 17 00:00:00 2001 From: hpk Date: Thu, 21 Aug 2008 19:39:34 +0200 Subject: [PATCH] [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 --- py/test/config.py | 27 +++++++++++++++++++++++++++ py/test/testing/test_config.py | 17 ++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/py/test/config.py b/py/test/config.py index 6488bde30..24c281d87 100644 --- a/py/test/config.py +++ b/py/test/config.py @@ -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() diff --git a/py/test/testing/test_config.py b/py/test/testing/test_config.py index 2ca255db0..9ee16f849 100644 --- a/py/test/testing/test_config.py +++ b/py/test/testing/test_config.py @@ -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():