Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Join the Stream and Verbose flags into the verbosity value #73

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions check.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@ type suiteRunner struct {
reportedProblemLast bool
benchTime time.Duration
benchMem bool
verbosity uint8
}

type RunConf struct {
Expand Down Expand Up @@ -551,16 +552,24 @@ func newSuiteRunner(suite interface{}, runConf *RunConf) *suiteRunner {
suiteType := reflect.TypeOf(suite)
suiteNumMethods := suiteType.NumMethod()
suiteValue := reflect.ValueOf(suite)

var verbosity uint8
if conf.Verbose {
verbosity = 1

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use a type here for the enumerated constant values, take a look at https://golang.org/doc/effective_go.html#constants

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used a number instead of constants because we don't have names for the different levels.
We now could use lowVerbosity and highVerbosity. But we already need three levels of verbosity for the snappy tests, and we will probably add an extra level at some point.

}
if conf.Stream {
verbosity = 2
}

runner := &suiteRunner{
suite: suite,
output: newOutputWriter(conf.Output, conf.Stream, conf.Verbose),
output: newOutputWriter(conf.Output, verbosity),
tracker: newResultTracker(),
benchTime: conf.BenchmarkTime,
benchMem: conf.BenchmarkMem,
tempDir: &tempDir{},
keepDir: conf.KeepWorkDir,
tests: make([]*methodType, 0, suiteNumMethods),
verbosity: verbosity,
}
if runner.benchTime == 0 {
runner.benchTime = 1 * time.Second
Expand Down Expand Up @@ -641,7 +650,7 @@ func (runner *suiteRunner) run() *Result {
// 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.output.Stream {
if runner.verbosity > 1 {
logw = runner.output
}
if logb == nil {
Expand Down
4 changes: 2 additions & 2 deletions export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ func Indent(s, with string) string {
return indent(s, with)
}

func NewOutputWriter(writer io.Writer, stream, verbose bool) *outputWriter {
return newOutputWriter(writer, stream, verbose)
func NewOutputWriter(writer io.Writer, verbosity uint8) *outputWriter {
return newOutputWriter(writer, verbosity)
}

func (c *C) FakeSkip(reason string) {
Expand Down
19 changes: 9 additions & 10 deletions reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ type outputWriter struct {
m sync.Mutex
writer io.Writer
wroteCallProblemLast bool
Stream bool
Verbose bool
verbosity uint8
}

func newOutputWriter(writer io.Writer, stream, verbose bool) *outputWriter {
return &outputWriter{writer: writer, Stream: stream, Verbose: verbose}
func newOutputWriter(writer io.Writer, verbosity uint8) *outputWriter {
return &outputWriter{writer: writer, verbosity: verbosity}
}

func (ow *outputWriter) Write(content []byte) (n int, err error) {
Expand All @@ -29,7 +28,7 @@ func (ow *outputWriter) Write(content []byte) (n int, err error) {
}

func (ow *outputWriter) WriteCallStarted(label string, c *C) {
if ow.Stream {
if ow.verbosity > 1 {
header := renderCallHeader(label, c, "", "\n")
ow.m.Lock()
ow.writer.Write([]byte(header))
Expand All @@ -39,22 +38,22 @@ func (ow *outputWriter) WriteCallStarted(label string, c *C) {

func (ow *outputWriter) WriteCallProblem(label string, c *C) {
var prefix string
if !ow.Stream {
if ow.verbosity < 2 {
prefix = "\n-----------------------------------" +
"-----------------------------------\n"
}
header := renderCallHeader(label, c, prefix, "\n\n")
ow.m.Lock()
ow.wroteCallProblemLast = true
ow.writer.Write([]byte(header))
if !ow.Stream {
if ow.verbosity < 2 {
c.logb.WriteTo(ow.writer)
}
ow.m.Unlock()
}

func (ow *outputWriter) WriteCallSuccess(label string, c *C) {
if ow.Stream || (ow.Verbose && c.kind == testKd) {
if ow.verbosity > 1 || (ow.verbosity == 1 && c.kind == testKd) {
// TODO Use a buffer here.
var suffix string
if c.reason != "" {
Expand All @@ -64,13 +63,13 @@ func (ow *outputWriter) WriteCallSuccess(label string, c *C) {
suffix += "\t" + c.timerString()
}
suffix += "\n"
if ow.Stream {
if ow.verbosity > 1 {
suffix += "\n"
}
header := renderCallHeader(label, c, "", suffix)
ow.m.Lock()
// Resist temptation of using line as prefix above due to race.
if !ow.Stream && ow.wroteCallProblemLast {
if ow.verbosity < 2 && ow.wroteCallProblemLast {
header = "\n-----------------------------------" +
"-----------------------------------\n" +
header
Expand Down
50 changes: 20 additions & 30 deletions reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,46 +24,42 @@ func (s *reporterS) TestWrite(c *C) {
testString := "test string"
output := String{}

dummyStream := true
dummyVerbose := true
o := NewOutputWriter(&output, dummyStream, dummyVerbose)
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"
stream := true
var verbosity uint8 = 2
output := String{}

dummyVerbose := true
o := NewOutputWriter(&output, stream, dummyVerbose)
o := NewOutputWriter(&output, verbosity)

o.WriteCallStarted(testLabel, c)
expected := fmt.Sprintf("%s: %s:\\d+: %s\n", testLabel, s.testFile, c.TestName())
c.Assert(output.value, Matches, expected)
}

func (s *reporterS) TestWriteCallStartedWithoutStreamFlag(c *C) {
stream := false
var verbosity uint8 = 1
output := String{}

dummyLabel := "dummy"
dummyVerbose := true
o := NewOutputWriter(&output, stream, dummyVerbose)
o := NewOutputWriter(&output, verbosity)

o.WriteCallStarted(dummyLabel, c)
c.Assert(output.value, Equals, "")
}

func (s *reporterS) TestWriteCallProblemWithStreamFlag(c *C) {
testLabel := "test problem label"
stream := true
var verbosity uint8 = 2
output := String{}

dummyVerbose := true
o := NewOutputWriter(&output, stream, dummyVerbose)
o := NewOutputWriter(&output, verbosity)

o.WriteCallProblem(testLabel, c)
expected := fmt.Sprintf("%s: %s:\\d+: %s\n\n", testLabel, s.testFile, c.TestName())
Expand All @@ -72,11 +68,10 @@ func (s *reporterS) TestWriteCallProblemWithStreamFlag(c *C) {

func (s *reporterS) TestWriteCallProblemWithoutStreamFlag(c *C) {
testLabel := "test problem label"
stream := false
var verbosity uint8 = 1
output := String{}

dummyVerbose := true
o := NewOutputWriter(&output, stream, dummyVerbose)
o := NewOutputWriter(&output, verbosity)

o.WriteCallProblem(testLabel, c)
expected := fmt.Sprintf(""+
Expand All @@ -89,11 +84,10 @@ func (s *reporterS) TestWriteCallProblemWithoutStreamFlag(c *C) {
func (s *reporterS) TestWriteCallProblemWithoutStreamFlagWithLog(c *C) {
testLabel := "test problem label"
testLog := "test log"
stream := false
var verbosity uint8 = 1
output := String{}

dummyVerbose := true
o := NewOutputWriter(&output, stream, dummyVerbose)
o := NewOutputWriter(&output, verbosity)

c.Log(testLog)
o.WriteCallProblem(testLabel, c)
Expand All @@ -106,11 +100,10 @@ func (s *reporterS) TestWriteCallProblemWithoutStreamFlagWithLog(c *C) {

func (s *reporterS) TestWriteCallSuccessWithStreamFlag(c *C) {
testLabel := "test success label"
stream := true
var verbosity uint8 = 2
output := String{}

dummyVerbose := true
o := NewOutputWriter(&output, stream, dummyVerbose)
o := NewOutputWriter(&output, verbosity)

o.WriteCallSuccess(testLabel, c)
expected := fmt.Sprintf("%s: %s:\\d+: %s\t\\d\\.\\d+s\n\n", testLabel, s.testFile, c.TestName())
Expand All @@ -120,11 +113,10 @@ func (s *reporterS) TestWriteCallSuccessWithStreamFlag(c *C) {
func (s *reporterS) TestWriteCallSuccessWithStreamFlagAndReason(c *C) {
testLabel := "test success label"
testReason := "test skip reason"
stream := true
var verbosity uint8 = 2
output := String{}

dummyVerbose := true
o := NewOutputWriter(&output, stream, dummyVerbose)
o := NewOutputWriter(&output, verbosity)
c.FakeSkip(testReason)

o.WriteCallSuccess(testLabel, c)
Expand All @@ -135,11 +127,10 @@ func (s *reporterS) TestWriteCallSuccessWithStreamFlagAndReason(c *C) {

func (s *reporterS) TestWriteCallSuccessWithoutStreamFlagWithVerboseFlag(c *C) {
testLabel := "test success label"
stream := false
verbose := true
var verbosity uint8 = 1
output := String{}

o := NewOutputWriter(&output, stream, verbose)
o := NewOutputWriter(&output, verbosity)

o.WriteCallSuccess(testLabel, c)
expected := fmt.Sprintf("%s: %s:\\d+: %s\t\\d\\.\\d+s\n", testLabel, s.testFile, c.TestName())
Expand All @@ -148,11 +139,10 @@ func (s *reporterS) TestWriteCallSuccessWithoutStreamFlagWithVerboseFlag(c *C) {

func (s *reporterS) TestWriteCallSuccessWithoutStreamFlagWithoutVerboseFlag(c *C) {
testLabel := "test success label"
stream := false
verbose := false
var verbosity uint8 = 0
output := String{}

o := NewOutputWriter(&output, stream, verbose)
o := NewOutputWriter(&output, verbosity)

o.WriteCallSuccess(testLabel, c)
c.Assert(output.value, Equals, "")
Expand Down