Skip to content

Commit

Permalink
Fixes #3; adds coverage support
Browse files Browse the repository at this point in the history
  • Loading branch information
mfridman committed Oct 24, 2018
1 parent 48d8fea commit caf57f3
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 9 deletions.
29 changes: 27 additions & 2 deletions parse/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package parse

import (
"encoding/json"
"regexp"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -72,7 +74,7 @@ func (e *Event) Discard() bool {
}
}

return e.Action == ActionOutput && e.Test == "" && !e.SkipLine() && !e.IsCached()
return e.Action == ActionOutput && e.Test == ""
}

// Let's try using the Summary method to report the package result.
Expand Down Expand Up @@ -102,8 +104,31 @@ func (e *Event) SkipLine() bool {

// IsCached reports special event case for cached packages:
// ok \tgithub.com/mfridman/tparse/tests\t(cached)\n
// ok \tgithub.com/mfridman/srfax\t(cached)\tcoverage: 28.8% of statements\n
func (e *Event) IsCached() bool {
return strings.HasPrefix(e.Output, "ok \t") && strings.HasSuffix(e.Output, "\t(cached)\n")
return strings.HasPrefix(e.Output, "ok \t") && strings.Contains(e.Output, "\t(cached)")
}

// Cover reports special event case for package coverage:
// ok \tgithub.com/mfridman/srfax\t(cached)\tcoverage: 28.8% of statements\n
// ok \tgithub.com/mfridman/srfax\t0.027s\tcoverage: 28.8% of statements\n
func (e Event) Cover() (float64, bool) {
var re = regexp.MustCompile(`[0-9]{1,3}\.[0-9]{1}\%`)

var f float64
var err error

if strings.Contains(e.Output, "\tcoverage:") && strings.HasSuffix(e.Output, "statements\n") {
s := re.FindString(e.Output)
f, err = strconv.ParseFloat(strings.TrimRight(s, "%"), 64)
if err != nil {
return f, false
}

return f, true
}

return f, false
}

// Action is one of a fixed set of actions describing a single emitted test event.
Expand Down
26 changes: 19 additions & 7 deletions parse/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func (p Packages) Print(skipNoTests bool) {
"Status",
"Elapsed",
"Package",
"Cover",
"Pass",
"Fail",
"Skip",
Expand All @@ -45,20 +46,22 @@ func (p Packages) Print(skipNoTests bool) {
Yellow("SKIP"),
"0.00s",
name + "\n[no test files]",
fmt.Sprintf(" %.1f%%", pkg.Coverage),
"0", "0", "0",
})

continue
}

if pkg.Cached {
name += "\n(cached)"
name += " (cached)"
}

tbl.Append([]string{
pkg.Summary.Action.WithColor(),
strconv.FormatFloat(pkg.Summary.Elapsed, 'f', 2, 64) + "s",
name,
fmt.Sprintf(" %.1f%%", pkg.Coverage),
strconv.Itoa(len(pkg.TestsByAction(ActionPass))),
strconv.Itoa(len(pkg.TestsByAction(ActionFail))),
strconv.Itoa(len(pkg.TestsByAction(ActionSkip))),
Expand All @@ -82,6 +85,10 @@ type Package struct {

// Cached indicates whether the test result was obtained from the cache.
Cached bool

// Cover reports whether the package contains coverage (go test run with -cover)
Cover bool
Coverage float64
}

// AddTestEvent adds the event to a test based on test name.
Expand Down Expand Up @@ -115,10 +122,6 @@ func Start(r io.Reader) (Packages, error) {
return nil, err
}

if e.Discard() {
continue
}

pkg, ok := pkgs[e.Package]
if !ok {
pkg = &Package{Summary: &Event{}}
Expand All @@ -130,17 +133,26 @@ func Start(r io.Reader) (Packages, error) {
pkg.NoTest = true
}

// We don't need this line, simply record the package as cached and move on.
if e.IsCached() {
pkg.Cached = true
continue
}

cover, ok := e.Cover()
if ok {
pkg.Cover = true
pkg.Coverage = cover
}

if e.Summary() {
pkg.Summary = e
continue
}

// We don't need to save these line.
if e.Discard() {
continue
}

pkg.AddTestEvent(e)

}
Expand Down
71 changes: 71 additions & 0 deletions tests/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,74 @@ func TestNewEvent(t *testing.T) {

}
}

func TestCachedPackage(t *testing.T) {

t.Parallel()

tt := []struct {
input string
isCached bool
}{
{`{"Time":"2018-10-24T08:30:14.566611-04:00","Action":"output","Package":"github.com/mfridman/tparse/tests","Output":"ok \tgithub.com/mfridman/tparse/tests\t(cached)\n"}`, true},
{`{"Time":"2018-10-24T08:48:23.634909-04:00","Action":"output","Package":"github.com/mfridman/srfax","Output":"ok \tgithub.com/mfridman/srfax\t(cached)\tcoverage: 28.8% of statements\n"}`, true},
{`{"Time":"2018-10-24T08:48:23.634909-04:00","Action":"output","Package":"github.com/mfridman/srfax","Output":"github.com/mfridman/srfax\t(cached)"}`, false},
{`{"Time":"2018-10-24T08:48:23.634909-04:00","Action":"output","Package":"github.com/mfridman/srfax","Output":"(cached)"}`, false},
{`{"Time":"2018-10-24T08:48:23.634909-04:00","Action":"output","Package":"github.com/mfridman/srfax","Output":""}`, false},
}

for _, test := range tt {
e, err := parse.NewEvent([]byte(test.input))
if err != nil {
t.Fatal(err)
}

got := e.IsCached()
want := test.isCached

if got != want {
t.Errorf("got non-cached output (%t), want cached output (%t)", got, want)
t.Logf("log: input: %v", test.input)
}
}
}
func TestCover(t *testing.T) {

t.Parallel()

var zero float64

tt := []struct {
input string
cover bool
coverage float64
}{
{`{"Time":"2018-10-24T08:48:23.634909-04:00","Action":"output","Package":"github.com/mfridman/srfax","Output":"ok \tgithub.com/mfridman/srfax\t(cached)\tcoverage: 28.8% of statements\n"}`, true, 28.8},
{`{"Time":"2018-10-24T08:48:23.634909-04:00","Action":"output","Package":"github.com/mfridman/srfax","Output":"ok \tgithub.com/mfridman/srfax\t(cached)\tcoverage: 100.0% of statements\n"}`, true, 100.0},
{`{"Time":"2018-10-24T08:48:23.634909-04:00","Action":"output","Package":"github.com/mfridman/srfax","Output":"ok \tgithub.com/mfridman/srfax\t(cached)\tcoverage: 0.0% of statements\n"}`, true, 0.0},
{`{"Time":"2018-10-24T09:25:59.855826-04:00","Action":"output","Package":"github.com/mfridman/srfax","Output":"ok \tgithub.com/mfridman/srfax\t0.027s\tcoverage: 87.5% of statements\n"}`, true, 87.5},
{`{"Time":"2018-10-24T08:48:23.634909-04:00","Action":"output","Package":"github.com/mfridman/srfax","Output":"ok \tgithub.com/mfridman/srfax\t(cached)\tcoverage: 1000.0% of statements\n"}`, true, zero},
{`{"Time":"2018-10-24T08:48:23.634909-04:00","Action":"output","Package":"github.com/mfridman/srfax","Output":"ok \tgithub.com/mfridman/srfax\t(cached)\tcoverage: .0% of statements\n"}`, false, zero},
}

for _, test := range tt {
e, err := parse.NewEvent([]byte(test.input))
if err != nil {
t.Fatal(err)
}

f, ok := e.Cover()
if ok != test.cover {
t.Errorf("got (%t) non-coverage event, want %t", ok, test.cover)
}

if f != test.coverage {
t.Errorf("got wrong percentage for coervage %v, want %v", f, test.coverage)
}

if t.Failed() {
t.Logf("log: input: %v", test.input)
}
}

}

0 comments on commit caf57f3

Please sign in to comment.