Skip to content

Commit

Permalink
Identify and, filter or display coverage output
Browse files Browse the repository at this point in the history
Identify coverage output events with string comparison. There does not
appear to be any way.

Add coverage output to the short format, and filter out the duplicate
line from the standard-quiet output.
  • Loading branch information
dnephin committed Jun 20, 2019
1 parent 6b99016 commit 1d5430f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,4 @@ gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gotest.tools v2.1.0+incompatible h1:5USw7CrJBYKqjg9R7QlA6jzqZKEAtvW82aNmsxxGPxw=
gotest.tools v2.1.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
12 changes: 12 additions & 0 deletions testjson/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ type Package struct {
Skipped []TestCase
Passed []TestCase
output map[string][]string
// coverage stores the code coverage output for the package without the
// trailing newline (ex: coverage: 91.1% of statements).
coverage string
// action identifies if the package passed or failed. A package may fail
// with no test failures if an init() or TestMain exits non-zero.
// skip indicates there were no tests.
Expand Down Expand Up @@ -135,6 +138,9 @@ func (e *Execution) add(event TestEvent) {
case ActionPass, ActionFail:
pkg.action = event.Action
case ActionOutput:
if isCoverageOutput(event.Output) {
pkg.coverage = strings.TrimRight(event.Output, "\n")
}
pkg.output[""] = append(pkg.output[""], event.Output)
}
return
Expand Down Expand Up @@ -173,6 +179,12 @@ func elapsedDuration(elapsed float64) time.Duration {
return time.Duration(elapsed*1000) * time.Millisecond
}

func isCoverageOutput(output string) bool {
return all(
strings.HasPrefix(output, "coverage:"),
strings.HasSuffix(output, "% of statements\n"))
}

// Output returns the full test output for a test.
func (e *Execution) Output(pkg, test string) string {
return strings.Join(e.packages[pkg].output[test], "")
Expand Down
22 changes: 18 additions & 4 deletions testjson/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ func standardVerboseFormat(event TestEvent, _ *Execution) (string, error) {

// go test
func standardQuietFormat(event TestEvent, _ *Execution) (string, error) {
if event.PackageEvent() && event.Output != "PASS\n" {
if !event.PackageEvent() {
return "", nil
}
if event.Output != "PASS\n" && !isCoverageOutput(event.Output) {
return event.Output, nil
}
return "", nil
Expand Down Expand Up @@ -96,7 +99,7 @@ func all(cond ...bool) bool {
return true
}

func shortFormat(event TestEvent, _ *Execution) (string, error) {
func shortFormat(event TestEvent, exec *Execution) (string, error) {
if !event.PackageEvent() {
return "", nil
}
Expand All @@ -107,9 +110,20 @@ func shortFormat(event TestEvent, _ *Execution) (string, error) {
}
return fmt.Sprintf(" (%s)", d)
}
fmtCoverage := func() string {
pkg := exec.Package(event.Package)
if pkg.coverage == "" {
return ""
}
return " (" + pkg.coverage + ")"
}
fmtEvent := func(action string) (string, error) {
return fmt.Sprintf("%s %s%s\n",
action, relativePackagePath(event.Package), fmtElapsed()), nil
return fmt.Sprintf("%s %s%s%s\n",
action,
relativePackagePath(event.Package),
fmtElapsed(),
fmtCoverage(),
), nil
}
withColor := colorEvent(event)
switch event.Action {
Expand Down

0 comments on commit 1d5430f

Please sign in to comment.