Skip to content

Commit

Permalink
cmd/go: cache coverage profile with tests
Browse files Browse the repository at this point in the history
This CL stores coverage profile data in the GOCACHE under the
'coverprofile' subkey alongside tests. This makes tests which use
coverage profiles cacheable. The values of the -coverprofile and
-outputdir flags are not included in the cache key to allow cached
profile data to be written to any output file.

Fixes golang#23565
  • Loading branch information
jproberts committed Jan 6, 2022
1 parent 6178d25 commit fcb3841
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 17 deletions.
5 changes: 3 additions & 2 deletions src/cmd/go/alldocs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 62 additions & 0 deletions src/cmd/go/go_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2320,6 +2320,68 @@ func TestCacheCoverage(t *testing.T) {
tg.run("test", "-cover", "-short", "math", "strings")
}

func TestCacheCoverageProfile(t *testing.T) {
tooSlow(t)

if godebug.Get("gocacheverify") == "1" {
t.Skip("GODEBUG gocacheverify")
}

tg := testgo(t)
defer tg.cleanup()
tg.parallel()
tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata"))
tg.makeTempdir()
tg.setenv("GOCACHE", tg.path("c1"))

// checkProfile asserts that the given profile contains the given mode
// and coverage lines for all given files.
checkProfile := func(t *testing.T, profile, mode string, files ...string) {
t.Helper()
if out, err := os.ReadFile(profile); err != nil {
t.Fatalf("failed to open coverprofile: %v", err)
} else {
if n := bytes.Count(out, []byte("mode: "+mode)); n != 1 {
if n == 0 {
t.Fatalf("missing mode: %s", mode)
} else {
t.Fatalf("too many mode: %s", mode)
}
}
for _, fname := range files {
if !bytes.Contains(out, []byte(fname)) {
t.Fatalf("missing file in coverprofile: %s", fname)
}
}
}
}

tg.run("test", "-coverprofile="+tg.path("cover.out"), "-x", "-v", "-short", "strings")
tg.grepStdout(`ok \t`, "expected strings test to succeed")
checkProfile(t, tg.path("cover.out"), "set", "strings/strings.go")
// Repeat commands should use the cache.
tg.run("test", "-coverprofile="+tg.path("cover.out"), "-x", "-v", "-short", "strings")
tg.grepStdout(`ok \tstrings\t\(cached\)`, "expected strings test results to be cached")
checkProfile(t, tg.path("cover.out"), "set", "strings/strings.go")
// Cover profiles should be cached independently. Since strings is already cached,
// only math should need to run.
tg.run("test", "-coverprofile="+tg.path("cover.out"), "-x", "-v", "-short", "strings", "math")
tg.grepStdout(`ok \tstrings\t\(cached\)`, "expected strings test results to be cached")
checkProfile(t, tg.path("cover.out"), "set", "strings/strings.go", "math/mod.go")
// A new -coverprofile file should use the cached coverage profile contents.
tg.run("test", "-coverprofile="+tg.path("cover1.out"), "-x", "-v", "-short", "strings")
tg.grepStdout(`ok \tstrings\t\(cached\)`, "expected cached strings test results to be used regardless of -coverprofile")
checkProfile(t, tg.path("cover1.out"), "set", "strings/strings.go")
// A new -covermode should not use the cached coverage profile, since the covermode changes
// the profile output.
tg.run("test", "-covermode=count", "-coverprofile="+tg.path("cover.out"), "-x", "-v", "-short", "strings")
tg.grepStdoutNot(`ok \tstrings\t\(cached\)`, "cached strings test results should not be used with different -covermode")
// A new -coverpkg should not use the cached coverage profile, since the coverpkg changes
// the profile output.
tg.run("test", "-coverpkg=math", "-coverprofile="+tg.path("cover.out"), "-x", "-v", "-short", "strings")
tg.grepStdoutNot(`ok \tstrings\t\(cached\)`, "cached strings test results should not be used with different -coverpkg")
}

func TestIssue22588(t *testing.T) {
// Don't get confused by stderr coming from tools.
tg := testgo(t)
Expand Down
18 changes: 10 additions & 8 deletions src/cmd/go/internal/test/cover.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package test

import (
"cmd/go/internal/base"
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -43,10 +44,9 @@ func initCoverProfile() {
}

// mergeCoverProfile merges file into the profile stored in testCoverProfile.
// It prints any errors it encounters to ew.
func mergeCoverProfile(ew io.Writer, file string) {
func mergeCoverProfile(file string) error {
if coverMerge.f == nil {
return
return nil
}
coverMerge.Lock()
defer coverMerge.Unlock()
Expand All @@ -56,22 +56,22 @@ func mergeCoverProfile(ew io.Writer, file string) {
r, err := os.Open(file)
if err != nil {
// Test did not create profile, which is OK.
return
return nil
}
defer r.Close()

n, err := io.ReadFull(r, buf)
if n == 0 {
return
return nil
}
if err != nil || string(buf) != expect {
fmt.Fprintf(ew, "error: test wrote malformed coverage profile.\n")
return
return errMalformedCoverProfile
}
_, err = io.Copy(coverMerge.f, r)
if err != nil {
fmt.Fprintf(ew, "error: saving coverage profile: %v\n", err)
return fmt.Errorf("saving coverage profile: %v\n", err)
}
return nil
}

func closeCoverProfile() {
Expand All @@ -82,3 +82,5 @@ func closeCoverProfile() {
base.Errorf("closing coverage profile: %v", err)
}
}

var errMalformedCoverProfile = errors.New("test wrote malformed coverage profile")
67 changes: 60 additions & 7 deletions src/cmd/go/internal/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,9 @@ elapsed time in the summary line.
The rule for a match in the cache is that the run involves the same
test binary and the flags on the command line come entirely from a
restricted set of 'cacheable' test flags, defined as -benchtime, -cpu,
-list, -parallel, -run, -short, -timeout, -failfast, and -v.
restricted set of 'cacheable' test flags, defined as -benchtime, -cover,
-covermode, -coverprofile, -cpu, -list, -parallel, -run, -short, -timeout,
-failfast, -v, -vet and -outputdir.
If a run of go test has any test or non-test flags outside this set,
the result is not cached. To disable test caching, use any test flag
or argument other than the cacheable flags. The idiomatic way to disable
Expand Down Expand Up @@ -1336,11 +1337,13 @@ func (c *runCache) builderRunTest(b *work.Builder, ctx context.Context, a *work.
}
args := str.StringList(execCmd, a.Deps[0].BuiltTarget(), testlogArg, panicArg, fuzzArg, testArgs)

var coverprofileFile string
if testCoverProfile != "" {
// Write coverage to temporary profile, for merging later.
coverprofileFile = a.Objdir + "_cover_.out"
for i, arg := range args {
if strings.HasPrefix(arg, "-test.coverprofile=") {
args[i] = "-test.coverprofile=" + a.Objdir + "_cover_.out"
args[i] = "-test.coverprofile=" + coverprofileFile
}
}
}
Expand Down Expand Up @@ -1417,7 +1420,9 @@ func (c *runCache) builderRunTest(b *work.Builder, ctx context.Context, a *work.
a.TestOutput = &buf
t := fmt.Sprintf("%.3fs", time.Since(t0).Seconds())

mergeCoverProfile(cmd.Stdout, a.Objdir+"_cover_.out")
if coverErr := mergeCoverProfile(coverprofileFile); coverErr != nil {
fmt.Fprintf(cmd.Stdout, "error: %v\n", coverErr)
}

if err == nil {
norun := ""
Expand All @@ -1439,7 +1444,7 @@ func (c *runCache) builderRunTest(b *work.Builder, ctx context.Context, a *work.
cmd.Stdout.Write([]byte("\n"))
}
fmt.Fprintf(cmd.Stdout, "ok \t%s\t%s%s%s\n", a.Package.ImportPath, t, coveragePercentage(out), norun)
c.saveOutput(a)
c.saveOutput(a, coverprofileFile)
} else {
base.SetExitStatus(1)
if len(out) == 0 {
Expand Down Expand Up @@ -1520,7 +1525,14 @@ func (c *runCache) tryCacheWithID(b *work.Builder, a *work.Action, id string) bo
// Note that this list is documented above,
// so if you add to this list, update the docs too.
cacheArgs = append(cacheArgs, arg)

case "-test.coverprofile",
"-test.outputdir":
// The -coverprofile and -outputdir arguments are cacheable but
// only change where profiles are written. They don't change the
// profile contents, so they aren't added to the cacheArgs. This
// allows cached coverage profiles to be written to different files.
// Note that this list is documented above,
// so if you add to this list, update the docs too.
default:
// nothing else is cacheable
if cache.DebugTest {
Expand Down Expand Up @@ -1640,6 +1652,26 @@ func (c *runCache) tryCacheWithID(b *work.Builder, a *work.Action, id string) bo
j++
}
c.buf.Write(data[j:])

// Write coverage data to profile.
if testCover {
// coverprofile cache expiration time should be coupled with the test data above, so
// the entry can be ignored.
f, _, err := cache.Default().GetFile(testCoverProfileKey(testID, testInputsID))
if err != nil {
if cache.DebugTest {
fmt.Fprintf(os.Stderr, "testcache: %s: test coverage profile not found: %v\n", a.Package.ImportPath, err)
}
return false
}
if err := mergeCoverProfile(f); err != nil {
if cache.DebugTest {
fmt.Fprintf(os.Stderr, "testcache: %s: test coverage profile not merged: %v\n", a.Package.ImportPath, err)
}
return false
}
}

return true
}

Expand Down Expand Up @@ -1788,7 +1820,12 @@ func testAndInputKey(testID, testInputsID cache.ActionID) cache.ActionID {
return cache.Subkey(testID, fmt.Sprintf("inputs:%x", testInputsID))
}

func (c *runCache) saveOutput(a *work.Action) {
// testCoverProfileKey returns the "coverprofile" cache key for the pair (testID, testInputsID).
func testCoverProfileKey(testID, testInputsID cache.ActionID) cache.ActionID {
return cache.Subkey(testAndInputKey(testID, testInputsID), "coverprofile")
}

func (c *runCache) saveOutput(a *work.Action, coverprofileFile string) {
if c.id1 == (cache.ActionID{}) && c.id2 == (cache.ActionID{}) {
return
}
Expand All @@ -1809,19 +1846,35 @@ func (c *runCache) saveOutput(a *work.Action) {
if err != nil {
return
}
saveCoverProfile := func(testID cache.ActionID) {
if coverprofileFile == "" {
return
}
coverprof, err := os.Open(coverprofileFile)
if err != nil {
if cache.DebugTest {
fmt.Fprintf(os.Stderr, "testcache: %s: failed to open temporary coverprofile file: %s", a.Package.ImportPath, err)
}
return
}
defer coverprof.Close()
cache.Default().Put(testCoverProfileKey(testID, testInputsID), coverprof)
}
if c.id1 != (cache.ActionID{}) {
if cache.DebugTest {
fmt.Fprintf(os.Stderr, "testcache: %s: save test ID %x => input ID %x => %x\n", a.Package.ImportPath, c.id1, testInputsID, testAndInputKey(c.id1, testInputsID))
}
cache.Default().PutNoVerify(c.id1, bytes.NewReader(testlog))
cache.Default().PutNoVerify(testAndInputKey(c.id1, testInputsID), bytes.NewReader(a.TestOutput.Bytes()))
saveCoverProfile(c.id1)
}
if c.id2 != (cache.ActionID{}) {
if cache.DebugTest {
fmt.Fprintf(os.Stderr, "testcache: %s: save test ID %x => input ID %x => %x\n", a.Package.ImportPath, c.id2, testInputsID, testAndInputKey(c.id2, testInputsID))
}
cache.Default().PutNoVerify(c.id2, bytes.NewReader(testlog))
cache.Default().PutNoVerify(testAndInputKey(c.id2, testInputsID), bytes.NewReader(a.TestOutput.Bytes()))
saveCoverProfile(c.id2)
}
}

Expand Down

0 comments on commit fcb3841

Please sign in to comment.