Files
build_soong/bp2build/go_conversion_test.go
Spandan Das de623294fe Partial bp2build conversion of blueprint_go_binary
This module type does not implement android.Module, and therefore we
cannot write a conventional bp2build converter for this module type.
Instead this has been special-cased inside bp2build/build_conversion.go.

There is one major deviation between Soong and Bazel's
go_binary/go_library. Soong
collects the deps in the transitve closure and puts them on compile/link
paths. Bazel OTOH, requires the direct imports to be listed in deps of the binary
explicitly (AFAIK). Since bp2build cannot determine the list of direct
imports from the list of transitive deps, put all the transitive deps in
`deps`

Test: unit tests
Test: TH
Bug: 284483729
Change-Id: I004aaf8607fef1697a0d9e7d018ad657b67778ac
2023-07-11 22:03:48 +00:00

127 lines
2.9 KiB
Go

// Copyright 2023 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bp2build
import (
"testing"
"github.com/google/blueprint/bootstrap"
"android/soong/android"
)
func runGoTests(t *testing.T, tc Bp2buildTestCase) {
RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {
tCtx := ctx.(*android.TestContext)
bootstrap.RegisterGoModuleTypes(tCtx.Context.Context) // android.TestContext --> android.Context --> blueprint.Context
}, tc)
}
func TestConvertGoPackage(t *testing.T) {
bp := `
bootstrap_go_package {
name: "foo",
pkgPath: "android/foo",
deps: [
"bar",
],
srcs: [
"foo1.go",
"foo2.go",
],
linux: {
srcs: [
"foo_linux.go",
],
},
darwin: {
srcs: [
"foo_darwin.go",
],
},
testSrcs: [
"foo1_test.go",
"foo2_test.go",
],
}
`
depBp := `
bootstrap_go_package {
name: "bar",
}
`
t.Parallel()
runGoTests(t, Bp2buildTestCase{
Description: "Convert bootstrap_go_package to go_library",
ModuleTypeUnderTest: "bootrstap_go_package",
Blueprint: bp,
Filesystem: map[string]string{
"bar/Android.bp": depBp, // Put dep in Android.bp to reduce boilerplate in ExpectedBazelTargets
},
ExpectedBazelTargets: []string{makeBazelTargetHostOrDevice("go_library", "foo",
AttrNameToString{
"deps": `["//bar:bar"]`,
"importpath": `"android/foo"`,
"srcs": `[
":foo1.go",
":foo2.go",
] + select({
"//build/bazel/platforms/os:darwin": [":foo_darwin.go"],
"//build/bazel/platforms/os:linux_glibc": [":foo_linux.go"],
"//conditions:default": [],
})`,
},
android.HostSupported,
)},
})
}
func TestConvertGoBinaryWithTransitiveDeps(t *testing.T) {
bp := `
blueprint_go_binary {
name: "foo",
srcs: ["main.go"],
deps: ["bar"],
}
`
depBp := `
bootstrap_go_package {
name: "bar",
deps: ["baz"],
}
bootstrap_go_package {
name: "baz",
}
`
t.Parallel()
runGoTests(t, Bp2buildTestCase{
Description: "Convert blueprint_go_binary to go_binary",
Blueprint: bp,
Filesystem: map[string]string{
"bar/Android.bp": depBp, // Put dep in Android.bp to reduce boilerplate in ExpectedBazelTargets
},
ExpectedBazelTargets: []string{makeBazelTargetHostOrDevice("go_binary", "foo",
AttrNameToString{
"deps": `[
"//bar:bar",
"//bar:baz",
]`,
"srcs": `[":main.go"]`,
},
android.HostSupported,
)},
})
}