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

builders/gentestmain: register correct cover mode in the coverage report file when enabling race mode #3019

Merged
merged 1 commit into from
Nov 30, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions go/private/rules/test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,11 @@ def _go_test_impl(ctx):
main_go = go.declare_file(go, path = "testmain.go")
arguments = go.builder_args(go, "gentestmain")
arguments.add("-output", main_go)
if ctx.configuration.coverage_enabled:
arguments.add("-coverage")
if go.coverage_enabled:
if go.mode.race:
arguments.add("-cover_mode", "atomic")
else:
arguments.add("-cover_mode", "set")
arguments.add(
# the l is the alias for the package under test, the l_test must be the
# same with the test suffix
Expand Down
25 changes: 14 additions & 11 deletions go/tools/builders/generate_test_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ type Cases struct {
FuzzTargets []TestCase
Examples []Example
TestMain string
Coverage bool
CoverMode string
Pkgname string
}

Expand Down Expand Up @@ -105,7 +105,7 @@ import (
"testing"
"testing/internal/testdeps"

{{if .Coverage}}
{{if ne .CoverMode ""}}
"github.com/bazelbuild/rules_go/go/tools/coverdata"
{{end}}

Expand Down Expand Up @@ -181,12 +181,15 @@ func main() {
flag.Lookup("test.run").Value.Set(filter)
}

{{if .Coverage}}
if len(coverdata.Cover.Counters) > 0 {
testing.RegisterCover(coverdata.Cover)
}
if coverageDat, ok := os.LookupEnv("COVERAGE_OUTPUT_FILE"); ok {
if testing.CoverMode() != "" {
{{if ne .CoverMode ""}}
if len(coverdata.Counters) > 0 {
testing.RegisterCover(testing.Cover{
Mode: "{{ .CoverMode }}",
lbcjbb marked this conversation as resolved.
Show resolved Hide resolved
Counters: coverdata.Counters,
Blocks: coverdata.Blocks,
})

if coverageDat, ok := os.LookupEnv("COVERAGE_OUTPUT_FILE"); ok {
flag.Lookup("test.coverprofile").Value.Set(coverageDat)
}
}
Expand All @@ -213,7 +216,7 @@ func genTestMain(args []string) error {
flags := flag.NewFlagSet("GoTestGenTest", flag.ExitOnError)
goenv := envFlags(flags)
out := flags.String("output", "", "output file to write. Defaults to stdout.")
coverage := flags.Bool("coverage", false, "whether coverage is supported")
coverMode := flags.String("cover_mode", "", "the coverage mode to use")
pkgname := flags.String("pkgname", "", "package name of test")
flags.Var(&imports, "import", "Packages to import")
flags.Var(&sources, "src", "Sources to process for tests")
Expand Down Expand Up @@ -263,8 +266,8 @@ func genTestMain(args []string) error {
}

cases := Cases{
Coverage: *coverage,
Pkgname: *pkgname,
CoverMode: *coverMode,
Pkgname: *pkgname,
}

testFileSet := token.NewFileSet()
Expand Down
18 changes: 8 additions & 10 deletions go/tools/coverdata/coverdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,11 @@ import (
"testing"
)

// Cover contains all coverage data for the program.
var Cover = testing.Cover{
Mode: "set",
CoveredPackages: "",
Counters: map[string][]uint32{},
Blocks: map[string][]testing.CoverBlock{},
}
// Contains all coverage data for the program.
var (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if this change is necessary. Can you keep Cover as testing.Cover without setting Mode and CoveredPackages? You can set coverdata.Cover.Mode in generate_test_main.go

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not necessary, but preferred IMHO, to avoid to manipulate an incomplete object which could be a source of mistake, even more when the object is exported in another package.

Counters = make(map[string][]uint32)
Blocks = make(map[string][]testing.CoverBlock)
)

// RegisterFile causes the coverage data recorded for a file to be included
// in program-wide coverage reports. This should be called from init functions
Expand All @@ -40,12 +38,12 @@ func RegisterFile(fileName string, counter []uint32, pos []uint32, numStmts []ui
if 3*len(counter) != len(pos) || len(counter) != len(numStmts) {
panic("coverage: mismatched sizes")
}
if Cover.Counters[fileName] != nil {
if Counters[fileName] != nil {
// Already registered.
fmt.Printf("Already covered %s\n", fileName)
return
}
Cover.Counters[fileName] = counter
Counters[fileName] = counter
block := make([]testing.CoverBlock, len(counter))
for i := range counter {
block[i] = testing.CoverBlock{
Expand All @@ -56,5 +54,5 @@ func RegisterFile(fileName string, counter []uint32, pos []uint32, numStmts []ui
Stmts: numStmts[i],
}
}
Cover.Blocks[fileName] = block
Blocks[fileName] = block
}
18 changes: 17 additions & 1 deletion tests/core/coverage/coverage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package coverage_test

import (
"bytes"
"fmt"
"io/ioutil"
"path/filepath"
"testing"
Expand Down Expand Up @@ -171,7 +172,21 @@ func TestPanic(t *testing.T) {
}

func TestCoverage(t *testing.T) {
if err := bazel_testing.RunBazel("coverage", "--instrumentation_filter=-//:b", ":a_test"); err != nil {
t.Run("without-race", func(t *testing.T) {
testCoverage(t, "set")
})

t.Run("with-race", func(t *testing.T) {
testCoverage(t, "atomic", "--@io_bazel_rules_go//go/config:race")
})
}

func testCoverage(t *testing.T, expectedCoverMode string, extraArgs ...string) {
args := append([]string{"coverage"}, append(
extraArgs, "--instrumentation_filter=-//:b", ":a_test",
)...)

if err := bazel_testing.RunBazel(args...); err != nil {
t.Fatal(err)
}

Expand All @@ -181,6 +196,7 @@ func TestCoverage(t *testing.T) {
t.Fatal(err)
}
for _, include := range []string{
fmt.Sprintf("mode: %s", expectedCoverMode),
"example.com/coverage/a/a.go:",
"example.com/coverage/c/c.go:",
} {
Expand Down
7 changes: 4 additions & 3 deletions tests/core/race/race_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,10 @@ func Test(t *testing.T) {
target: "//:pure_race_bin",
wantBuildFail: true,
}, {
desc: "cover_race",
cmd: "coverage",
target: "//:coverrace_test",
desc: "cover_race",
cmd: "coverage",
target: "//:coverrace_test",
featureFlag: true,
},
} {
t.Run(test.desc, func(t *testing.T) {
Expand Down