From efb631fcb59a841469b7aa7b673602c96bc7f4a4 Mon Sep 17 00:00:00 2001 From: Leo Arias Date: Fri, 29 Jan 2016 05:37:51 +0000 Subject: [PATCH] Split the logger and the reporter The logger and the reporter should be configured independently. We can do many improvements in the logger, but for now this change preserves the public interfaces, keeps all the tests passing and lets us focus on improving the reporter first. --- check.go | 29 +++++++++++++++-------------- reporter.go | 7 ------- reporter_test.go | 11 ----------- 3 files changed, 15 insertions(+), 32 deletions(-) diff --git a/check.go b/check.go index 1d4e04f..a8c8d4d 100644 --- a/check.go +++ b/check.go @@ -84,7 +84,6 @@ type C struct { testName string _status funcStatus logb *logger - logw io.Writer done chan *C reason string mustFail bool @@ -109,25 +108,30 @@ func (c *C) stopNow() { // logger is a concurrency safe byte.Buffer type logger struct { sync.Mutex - writer bytes.Buffer + buffer bytes.Buffer + output io.Writer + verbosity uint8 } func (l *logger) Write(buf []byte) (int, error) { l.Lock() defer l.Unlock() - return l.writer.Write(buf) + if l.verbosity > 1 { + l.output.Write(buf) + } + return l.buffer.Write(buf) } func (l *logger) WriteTo(w io.Writer) (int64, error) { l.Lock() defer l.Unlock() - return l.writer.WriteTo(w) + return l.buffer.WriteTo(w) } func (l *logger) String() string { l.Lock() defer l.Unlock() - return l.writer.String() + return l.buffer.String() } // ----------------------------------------------------------------------- @@ -198,9 +202,6 @@ func (c *C) logNewLine() { func (c *C) writeLog(buf []byte) { c.logb.Write(buf) - if c.logw != nil { - c.logw.Write(buf) - } } func hasStringOrError(x interface{}) (ok bool) { @@ -518,6 +519,7 @@ type suiteRunner struct { tracker *resultTracker tempDir *tempDir keepDir bool + logOutput io.Writer output *outputWriter reportedProblemLast bool benchTime time.Duration @@ -562,6 +564,7 @@ func newSuiteRunner(suite interface{}, runConf *RunConf) *suiteRunner { runner := &suiteRunner{ suite: suite, + logOutput: conf.Output, output: newOutputWriter(conf.Output, verbosity), tracker: newResultTracker(), benchTime: conf.BenchmarkTime, @@ -649,19 +652,17 @@ func (runner *suiteRunner) run() *Result { // Create a call object with the given suite method, and fork a // goroutine with the provided dispatcher for running it. func (runner *suiteRunner) forkCall(method *methodType, kind funcKind, testName string, logb *logger, dispatcher func(c *C)) *C { - var logw io.Writer - if runner.verbosity > 1 { - logw = runner.output - } if logb == nil { - logb = new(logger) + logb = &logger{ + output: runner.logOutput, + verbosity: runner.verbosity, + } } c := &C{ method: method, kind: kind, testName: testName, logb: logb, - logw: logw, tempDir: runner.tempDir, done: make(chan *C, 1), timer: timer{benchTime: runner.benchTime}, diff --git a/reporter.go b/reporter.go index 68d3c96..f963659 100644 --- a/reporter.go +++ b/reporter.go @@ -20,13 +20,6 @@ func newOutputWriter(writer io.Writer, verbosity uint8) *outputWriter { return &outputWriter{writer: writer, verbosity: verbosity} } -func (ow *outputWriter) Write(content []byte) (n int, err error) { - ow.m.Lock() - n, err = ow.writer.Write(content) - ow.m.Unlock() - return -} - func (ow *outputWriter) WriteCallStarted(label string, c *C) { if ow.verbosity > 1 { header := renderCallHeader(label, c, "", "\n") diff --git a/reporter_test.go b/reporter_test.go index 519230d..0e1056c 100644 --- a/reporter_test.go +++ b/reporter_test.go @@ -20,17 +20,6 @@ func (s *reporterS) SetUpSuite(c *C) { s.testFile = filepath.Base(fileName) } -func (s *reporterS) TestWrite(c *C) { - testString := "test string" - output := String{} - - var dummyVerbosity uint8 - o := NewOutputWriter(&output, dummyVerbosity) - - o.Write([]byte(testString)) - c.Assert(output.value, Equals, testString) -} - func (s *reporterS) TestWriteCallStartedWithStreamFlag(c *C) { testLabel := "test started label" var verbosity uint8 = 2