Skip to content

Commit

Permalink
Fix capturing of coverpkg output
Browse files Browse the repository at this point in the history
The check for coverage output was a little too strict. It was only matching coverage
output by the -cover flag. The -coverpkg output is a little different.

This commit fixes the check, and adds tests for both.
  • Loading branch information
dnephin committed Apr 17, 2021
1 parent 73868ed commit 411f958
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 7 deletions.
14 changes: 7 additions & 7 deletions testjson/execution.go
Expand Up @@ -279,24 +279,24 @@ func (e *Execution) add(event TestEvent) {
e.packages[event.Package] = pkg
}
if event.PackageEvent() {
e.addPackageEvent(pkg, event)
pkg.addEvent(event)
return
}
pkg.addTestEvent(event)
}

func (e *Execution) addPackageEvent(pkg *Package, event TestEvent) {
func (p *Package) addEvent(event TestEvent) {
switch event.Action {
case ActionPass, ActionFail:
pkg.action = event.Action
p.action = event.Action
case ActionOutput:
if isCoverageOutput(event.Output) {
pkg.coverage = strings.TrimRight(event.Output, "\n")
p.coverage = strings.TrimRight(event.Output, "\n")
}
if isCachedOutput(event.Output) {
pkg.cached = true
p.cached = true
}
pkg.addOutput(0, event.Output)
p.addOutput(0, event.Output)
}
}

Expand Down Expand Up @@ -385,7 +385,7 @@ func elapsedDuration(elapsed float64) time.Duration {
func isCoverageOutput(output string) bool {
return all(
strings.HasPrefix(output, "coverage:"),
strings.HasSuffix(output, "% of statements\n"))
strings.Contains(output, "% of statements"))
}

func isCachedOutput(output string) bool {
Expand Down
69 changes: 69 additions & 0 deletions testjson/execution_test.go
Expand Up @@ -107,3 +107,72 @@ func TestParseEvent(t *testing.T) {
cmpTestEvent := cmp.AllowUnexported(TestEvent{})
assert.DeepEqual(t, event, expected, cmpTestEvent)
}

func TestPackage_AddEvent(t *testing.T) {
type testCase struct {
name string
event string
expected Package
}

var cmpPackage = cmp.Options{
cmp.AllowUnexported(Package{}),
cmpopts.EquateEmpty(),
}

run := func(t *testing.T, tc testCase) {
te, err := parseEvent([]byte(tc.event))
assert.NilError(t, err)

p := newPackage()
p.addEvent(te)
assert.DeepEqual(t, p, &tc.expected, cmpPackage)
}

var testCases = []testCase{
{
name: "coverage with -cover",
event: `{"Action":"output","Package":"gotest.tools/testing","Output":"coverage: 4.2% of statements\n"}`,
expected: Package{
coverage: "coverage: 4.2% of statements",
output: pkgOutput(0, "coverage: 4.2% of statements\n"),
},
},
{
name: "coverage with -coverpkg",
event: `{"Action":"output","Package":"gotest.tools/testing","Output":"coverage: 7.5% of statements in ./testing\n"}`,
expected: Package{
coverage: "coverage: 7.5% of statements in ./testing",
output: pkgOutput(0, "coverage: 7.5% of statements in ./testing\n"),
},
},
{
name: "package failed",
event: `{"Action":"fail","Package":"gotest.tools/testing","Elapsed":0.012}`,
expected: Package{action: ActionFail},
},
{
name: "package is cached",
event: `{"Action":"output","Package":"gotest.tools/testing","Output":"ok \tgotest.tools/testing\t(cached)\n"}`,
expected: Package{
cached: true,
output: pkgOutput(0, "ok \tgotest.tools/testing\t(cached)\n"),
},
},
{
name: "package pass",
event: `{"Action":"pass","Package":"gotest.tools/testing","Elapsed":0.012}`,
expected: Package{action: ActionPass},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
run(t, tc)
})
}
}

func pkgOutput(id int, line string) map[int][]string {
return map[int][]string{id: {line}}
}

0 comments on commit 411f958

Please sign in to comment.