Skip to content

Commit f2b55f1

Browse files
Leo AriasLeo Arias
authored andcommitted
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.
1 parent 4dee14e commit f2b55f1

File tree

3 files changed

+15
-32
lines changed

3 files changed

+15
-32
lines changed

check.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ type C struct {
8484
testName string
8585
_status funcStatus
8686
logb *logger
87-
logw io.Writer
8887
done chan *C
8988
reason string
9089
mustFail bool
@@ -109,25 +108,30 @@ func (c *C) stopNow() {
109108
// logger is a concurrency safe byte.Buffer
110109
type logger struct {
111110
sync.Mutex
112-
writer bytes.Buffer
111+
buffer bytes.Buffer
112+
output io.Writer
113+
verbosity uint8
113114
}
114115

115116
func (l *logger) Write(buf []byte) (int, error) {
116117
l.Lock()
117118
defer l.Unlock()
118-
return l.writer.Write(buf)
119+
if l.verbosity > 1 {
120+
l.output.Write(buf)
121+
}
122+
return l.buffer.Write(buf)
119123
}
120124

121125
func (l *logger) WriteTo(w io.Writer) (int64, error) {
122126
l.Lock()
123127
defer l.Unlock()
124-
return l.writer.WriteTo(w)
128+
return l.buffer.WriteTo(w)
125129
}
126130

127131
func (l *logger) String() string {
128132
l.Lock()
129133
defer l.Unlock()
130-
return l.writer.String()
134+
return l.buffer.String()
131135
}
132136

133137
// -----------------------------------------------------------------------
@@ -198,9 +202,6 @@ func (c *C) logNewLine() {
198202

199203
func (c *C) writeLog(buf []byte) {
200204
c.logb.Write(buf)
201-
if c.logw != nil {
202-
c.logw.Write(buf)
203-
}
204205
}
205206

206207
func hasStringOrError(x interface{}) (ok bool) {
@@ -518,6 +519,7 @@ type suiteRunner struct {
518519
tracker *resultTracker
519520
tempDir *tempDir
520521
keepDir bool
522+
logOutput io.Writer
521523
output *outputWriter
522524
reportedProblemLast bool
523525
benchTime time.Duration
@@ -562,6 +564,7 @@ func newSuiteRunner(suite interface{}, runConf *RunConf) *suiteRunner {
562564

563565
runner := &suiteRunner{
564566
suite: suite,
567+
logOutput: conf.Output,
565568
output: newOutputWriter(conf.Output, verbosity),
566569
tracker: newResultTracker(),
567570
benchTime: conf.BenchmarkTime,
@@ -649,19 +652,17 @@ func (runner *suiteRunner) run() *Result {
649652
// Create a call object with the given suite method, and fork a
650653
// goroutine with the provided dispatcher for running it.
651654
func (runner *suiteRunner) forkCall(method *methodType, kind funcKind, testName string, logb *logger, dispatcher func(c *C)) *C {
652-
var logw io.Writer
653-
if runner.verbosity > 1 {
654-
logw = runner.output
655-
}
656655
if logb == nil {
657-
logb = new(logger)
656+
logb = &logger{
657+
output: runner.logOutput,
658+
verbosity: runner.verbosity,
659+
}
658660
}
659661
c := &C{
660662
method: method,
661663
kind: kind,
662664
testName: testName,
663665
logb: logb,
664-
logw: logw,
665666
tempDir: runner.tempDir,
666667
done: make(chan *C, 1),
667668
timer: timer{benchTime: runner.benchTime},

reporter.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,6 @@ func newOutputWriter(writer io.Writer, verbosity uint8) *outputWriter {
2020
return &outputWriter{writer: writer, verbosity: verbosity}
2121
}
2222

23-
func (ow *outputWriter) Write(content []byte) (n int, err error) {
24-
ow.m.Lock()
25-
n, err = ow.writer.Write(content)
26-
ow.m.Unlock()
27-
return
28-
}
29-
3023
func (ow *outputWriter) WriteCallStarted(label string, c *C) {
3124
if ow.verbosity > 1 {
3225
header := renderCallHeader(label, c, "", "\n")

reporter_test.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,6 @@ func (s *reporterS) SetUpSuite(c *C) {
2020
s.testFile = filepath.Base(fileName)
2121
}
2222

23-
func (s *reporterS) TestWrite(c *C) {
24-
testString := "test string"
25-
output := String{}
26-
27-
var dummyVerbosity uint8
28-
o := NewOutputWriter(&output, dummyVerbosity)
29-
30-
o.Write([]byte(testString))
31-
c.Assert(output.value, Equals, testString)
32-
}
33-
3423
func (s *reporterS) TestWriteCallStartedWithStreamFlag(c *C) {
3524
testLabel := "test started label"
3625
var verbosity uint8 = 2

0 commit comments

Comments
 (0)