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

Fix capturing of coverpkg output #190

Merged
merged 1 commit into from
Apr 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 7 additions & 7 deletions testjson/execution.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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}}
}