Allow collecting coverage for go_binary executed as child process#4461
Allow collecting coverage for go_binary executed as child process#4461fmeum merged 6 commits intobazel-contrib:masterfrom
Conversation
|
Not sure about macOS yet, but I could get the Linux integration test to pass with these changes: diff --git a/tests/core/coverage/coverage_test.go b/tests/core/coverage/coverage_test.go
index 22009b3c..7cc6e54b 100644
--- a/tests/core/coverage/coverage_test.go
+++ b/tests/core/coverage/coverage_test.go
@@ -50,8 +50,14 @@ go_library(
name = "a",
srcs = ["a.go"],
importpath = "example.com/coverage/a",
- deps = [":b"],
+ deps = [
+ ":b",
+ "@io_bazel_rules_go//go/runfiles",
+ ],
data = [":a_binary"],
+ x_defs = {
+ "aBinaryRlocationPath": "$(rlocationpath :a_binary)",
+ },
)
go_binary(
@@ -107,28 +113,25 @@ func main() {
-- a_test.go --
package a
-import (
- "path/filepath"
- "os"
- "os/exec"
- "testing"
-)
+import "testing"
func TestA(t *testing.T) {
ALive()
}
func TestBinary(t *testing.T) {
- cmd := exec.Command(filepath.Join(os.Getenv("RUNFILES_DIR"), "bazel_testing", "a_binary"))
- err := cmd.Run()
- if err != nil {
- t.Fatal(err)
- }
+ RunBinary()
}
-- a.go --
package a
-import "example.com/coverage/b"
+import (
+ "os/exec"
+ "example.com/coverage/b"
+ "github.com/bazelbuild/rules_go/go/runfiles"
+)
+
+var aBinaryRlocationPath string
func ALive() int {
return b.BLive()
@@ -138,6 +141,18 @@ func ADead() int {
return b.BDead()
}
+func RunBinary() {
+ p, err := runfiles.Rlocation(aBinaryRlocationPath)
+ if err != nil {
+ panic(err)
+ }
+ cmd := exec.Command(p)
+ err = cmd.Run()
+ if err != nil {
+ panic(err)
+ }
+}
+
-- b.go --
package b
@@ -219,6 +234,8 @@ func testCoverage(t *testing.T, expectedCoverMode string, extraArgs ...string) {
extraArgs,
"--instrumentation_filter=-//:b",
"--@io_bazel_rules_go//go/config:cover_format=go_cover",
+ "--test_output=errors",
+ "--test_env=VERBOSE_COVERAGE=1",
":a_test",
)...)
|
|
For macOS, you could try adding |
thanks, i'll give it a shot. I'm confused why this wasn't an issue before when building the go_test, only with the go_binary... |
0260c1c to
13b6993
Compare
I came back this morning and couldn't reproduce the issue :( I guess this should be good to go once we resolve the last issue from golang/go@40b3c0e |
025bb18 to
f6f63a9
Compare
683b9a6 to
8cb50f5
Compare
|
I believe all feedback is addressed now and this should be good to go! |
What type of PR is this?
feature
What does this PR do? Why is it needed?
Adds a shim to go_binary when built in coverage mode to collect coverage info. Useful for integration testing.
Other notes for review
Added integration test
Manual testing:
Fixes #3513