1 // Copyright 2022 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Support for test coverage with redesigned coverage implementation.
11 "internal/goexperiment"
15 // cover2 variable stores the current coverage mode and a
16 // tear-down function to be called at the end of the testing run.
19 tearDown func(coverprofile string, gocoverdir string) (string, error)
20 snapshotcov func() float64
23 // registerCover2 is invoked during "go test -cover" runs by the test harness
24 // code in _testmain.go; it is used to record a 'tear down' function
25 // (to be called when the test is complete) and the coverage mode.
26 func registerCover2(mode string, tearDown func(coverprofile string, gocoverdir string) (string, error), snapcov func() float64) {
28 cover2.tearDown = tearDown
29 cover2.snapshotcov = snapcov
32 // coverReport2 invokes a callback in _testmain.go that will
33 // emit coverage data at the point where test execution is complete,
34 // for "go test -cover" runs.
36 if !goexperiment.CoverageRedesign {
39 if errmsg, err := cover2.tearDown(*coverProfile, *gocoverdir); err != nil {
40 fmt.Fprintf(os.Stderr, "%s: %v\n", errmsg, err)
45 // testGoCoverDir returns the value passed to the -test.gocoverdir
46 // flag by the Go command, if goexperiment.CoverageRedesign is
48 func testGoCoverDir() string {
52 // coverage2 returns a rough "coverage percentage so far"
53 // number to support the testing.Coverage() function.
54 func coverage2() float64 {
55 if cover2.mode == "" {
58 return cover2.snapshotcov()