Skip to content

Commit

Permalink
Split the logger and the reporter
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
come-maiz authored and Leo Arias committed May 27, 2016
1 parent 4dee14e commit f2b55f1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 32 deletions.
29 changes: 15 additions & 14 deletions check.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ type C struct {
testName string
_status funcStatus
logb *logger
logw io.Writer
done chan *C
reason string
mustFail bool
Expand All @@ -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()
}

// -----------------------------------------------------------------------
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -518,6 +519,7 @@ type suiteRunner struct {
tracker *resultTracker
tempDir *tempDir
keepDir bool
logOutput io.Writer
output *outputWriter
reportedProblemLast bool
benchTime time.Duration
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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},
Expand Down
7 changes: 0 additions & 7 deletions reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
11 changes: 0 additions & 11 deletions reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f2b55f1

Please sign in to comment.