Create additional test target for go modules in bp2build
For go modules with non-empty testSrcs, we will create an additional
go_test target. To build a standalone test executable, we need to
include the source files in the compilation unit. This will be done
using the `embed` attribute
1. For tests of go_library, this is straightforward. It will embed the
go_library and "inherit" its .go files and deps
2. For tetss of go_binary, we need to create an additional go_source
target since go_binary cannot be embedded inside a go_test
Using `b test` for these tests revealed that certain tests are not
hermitic and rely on `testdata` files checked into the tree. This CL
introduces an allowlist to skip generating go_test targets for them.
Test: bp2build unit test
Test: TH
Bug: 284483729
Bug: 288491147
Change-Id: Ic736d655babc2f6067e4da75384900b7b8bdc2ed
diff --git a/bp2build/go_conversion_test.go b/bp2build/go_conversion_test.go
index 507fbf0..2387641 100644
--- a/bp2build/go_conversion_test.go
+++ b/bp2build/go_conversion_test.go
@@ -45,11 +45,17 @@
srcs: [
"foo_linux.go",
],
+ testSrcs: [
+ "foo_linux_test.go",
+ ],
},
darwin: {
srcs: [
"foo_darwin.go",
],
+ testSrcs: [
+ "foo_darwin_test.go",
+ ],
},
testSrcs: [
"foo1_test.go",
@@ -84,7 +90,21 @@
})`,
},
android.HostSupported,
- )},
+ ),
+ makeBazelTargetHostOrDevice("go_test", "foo-test",
+ AttrNameToString{
+ "embed": `[":foo"]`,
+ "srcs": `[
+ "foo1_test.go",
+ "foo2_test.go",
+ ] + select({
+ "//build/bazel/platforms/os:darwin": ["foo_darwin_test.go"],
+ "//build/bazel/platforms/os:linux_glibc": ["foo_linux_test.go"],
+ "//conditions:default": [],
+ })`,
+ },
+ android.HostSupported,
+ )},
})
}
@@ -125,6 +145,44 @@
})
}
+func TestConvertGoBinaryWithTestSrcs(t *testing.T) {
+ bp := `
+blueprint_go_binary {
+ name: "foo",
+ srcs: ["main.go"],
+ testSrcs: ["main_test.go"],
+}
+`
+ t.Parallel()
+ runGoTests(t, Bp2buildTestCase{
+ Description: "Convert blueprint_go_binary with testSrcs",
+ Blueprint: bp,
+ ExpectedBazelTargets: []string{
+ makeBazelTargetHostOrDevice("go_binary", "foo",
+ AttrNameToString{
+ "deps": `[]`,
+ "embed": `[":foo-source"]`,
+ },
+ android.HostSupported,
+ ),
+ makeBazelTargetHostOrDevice("go_source", "foo-source",
+ AttrNameToString{
+ "deps": `[]`,
+ "srcs": `["main.go"]`,
+ },
+ android.HostSupported,
+ ),
+ makeBazelTargetHostOrDevice("go_test", "foo-test",
+ AttrNameToString{
+ "embed": `[":foo-source"]`,
+ "srcs": `["main_test.go"]`,
+ },
+ android.HostSupported,
+ ),
+ },
+ })
+}
+
func TestConvertGoBinaryWithSrcInDifferentPackage(t *testing.T) {
bp := `
blueprint_go_binary {