From 411f95830cf695dbffc05c771a706379ce3770fd Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Sat, 17 Apr 2021 13:33:49 -0400 Subject: [PATCH] Fix capturing of coverpkg output 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. --- testjson/execution.go | 14 ++++---- testjson/execution_test.go | 69 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 7 deletions(-) diff --git a/testjson/execution.go b/testjson/execution.go index 577c664e..24790246 100644 --- a/testjson/execution.go +++ b/testjson/execution.go @@ -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) } } @@ -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 { diff --git a/testjson/execution_test.go b/testjson/execution_test.go index 6bdd6039..f52daff5 100644 --- a/testjson/execution_test.go +++ b/testjson/execution_test.go @@ -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}} +}