aboutsummaryrefslogtreecommitdiff
path: root/src/testing
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2024-05-15 16:06:23 -0400
committerGopher Robot <gobot@golang.org>2024-05-21 19:41:02 +0000
commit180ea45566d19e60aa2d660f6139b7f6e18ff56b (patch)
treee14f155a336e4f5888334d2e0c8076ac8353f49a /src/testing
parent647870becc230b022b431a4ef8b7c9b31382db6c (diff)
downloadgo-180ea45566d19e60aa2d660f6139b7f6e18ff56b.tar.xz
runtime/coverage: remove uses of //go:linkname
Move code to internal/coverage/cfile, making it possible to access directly from testing/internal/testdeps, so that we can avoid needing //go:linkname hacks. For #67401. Change-Id: I10b23a9970164afd2165e718ef3b2d9e86783883 Reviewed-on: https://go-review.googlesource.com/c/go/+/585820 Auto-Submit: Russ Cox <rsc@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Than McIntosh <thanm@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com>
Diffstat (limited to 'src/testing')
-rw-r--r--src/testing/internal/testdeps/deps.go31
-rw-r--r--src/testing/newcover.go10
-rw-r--r--src/testing/testing.go6
3 files changed, 42 insertions, 5 deletions
diff --git a/src/testing/internal/testdeps/deps.go b/src/testing/internal/testdeps/deps.go
index 868307550e..88c1b253ee 100644
--- a/src/testing/internal/testdeps/deps.go
+++ b/src/testing/internal/testdeps/deps.go
@@ -13,6 +13,7 @@ package testdeps
import (
"bufio"
"context"
+ "internal/coverage/cfile"
"internal/fuzz"
"internal/testlog"
"io"
@@ -26,6 +27,9 @@ import (
"time"
)
+// Cover indicates whether coverage is enabled.
+var Cover bool
+
// TestDeps is an implementation of the testing.testDeps interface,
// suitable for passing to [testing.MainStart].
type TestDeps struct{}
@@ -197,3 +201,30 @@ func (TestDeps) ResetCoverage() {
func (TestDeps) SnapshotCoverage() {
fuzz.SnapshotCoverage()
}
+
+var CoverMode string
+var Covered string
+
+func (TestDeps) InitRuntimeCoverage() (mode string, tearDown func(string, string) (string, error), snapcov func() float64) {
+ if CoverMode == "" {
+ return
+ }
+ return CoverMode, coverTearDown, cfile.Snapshot
+}
+
+func coverTearDown(coverprofile string, gocoverdir string) (string, error) {
+ var err error
+ if gocoverdir == "" {
+ gocoverdir, err = os.MkdirTemp("", "gocoverdir")
+ if err != nil {
+ return "error setting GOCOVERDIR: bad os.MkdirTemp return", err
+ }
+ defer os.RemoveAll(gocoverdir)
+ }
+ cfile.MarkProfileEmitted(true)
+ cmode := CoverMode
+ if err := cfile.ProcessCoverTestDir(gocoverdir, coverprofile, cmode, Covered, os.Stdout); err != nil {
+ return "error generating coverage report", err
+ }
+ return "", nil
+}
diff --git a/src/testing/newcover.go b/src/testing/newcover.go
index 7a70dcfffa..a7dbcfc65a 100644
--- a/src/testing/newcover.go
+++ b/src/testing/newcover.go
@@ -21,13 +21,13 @@ var cover2 struct {
snapshotcov func() float64
}
-// registerCover2 is injected in testmain.
-//go:linkname registerCover2
-
-// registerCover2 is invoked during "go test -cover" runs by the test harness
-// code in _testmain.go; it is used to record a 'tear down' function
+// registerCover2 is invoked during "go test -cover" runs.
+// It is used to record a 'tear down' function
// (to be called when the test is complete) and the coverage mode.
func registerCover2(mode string, tearDown func(coverprofile string, gocoverdir string) (string, error), snapcov func() float64) {
+ if mode == "" {
+ return
+ }
cover2.mode = mode
cover2.tearDown = tearDown
cover2.snapshotcov = snapcov
diff --git a/src/testing/testing.go b/src/testing/testing.go
index 60f0c23137..78ad8dbaac 100644
--- a/src/testing/testing.go
+++ b/src/testing/testing.go
@@ -1855,6 +1855,10 @@ func (f matchStringOnly) CheckCorpus([]any, []reflect.Type) error { return nil }
func (f matchStringOnly) ResetCoverage() {}
func (f matchStringOnly) SnapshotCoverage() {}
+func (f matchStringOnly) InitRuntimeCoverage() (mode string, tearDown func(string, string) (string, error), snapcov func() float64) {
+ return
+}
+
// Main is an internal function, part of the implementation of the "go test" command.
// It was exported because it is cross-package and predates "internal" packages.
// It is no longer used by "go test" but preserved, as much as possible, for other
@@ -1902,12 +1906,14 @@ type testDeps interface {
CheckCorpus([]any, []reflect.Type) error
ResetCoverage()
SnapshotCoverage()
+ InitRuntimeCoverage() (mode string, tearDown func(coverprofile string, gocoverdir string) (string, error), snapcov func() float64)
}
// MainStart is meant for use by tests generated by 'go test'.
// It is not meant to be called directly and is not subject to the Go 1 compatibility document.
// It may change signature from release to release.
func MainStart(deps testDeps, tests []InternalTest, benchmarks []InternalBenchmark, fuzzTargets []InternalFuzzTarget, examples []InternalExample) *M {
+ registerCover2(deps.InitRuntimeCoverage())
Init()
return &M{
deps: deps,