2022-02-04 22:01:20 +01:00
|
|
|
// Copyright 2021 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 (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"android/soong/android"
|
|
|
|
"android/soong/java"
|
|
|
|
)
|
|
|
|
|
2022-06-21 21:28:33 +02:00
|
|
|
func runJavaProtoTestCase(t *testing.T, tc Bp2buildTestCase) {
|
2022-02-04 22:01:20 +01:00
|
|
|
t.Helper()
|
2022-06-21 21:28:33 +02:00
|
|
|
(&tc).ModuleTypeUnderTest = "java_library_static"
|
|
|
|
(&tc).ModuleTypeUnderTestFactory = java.LibraryFactory
|
|
|
|
RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, tc)
|
2022-02-04 22:01:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestJavaProto(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
protoType string
|
|
|
|
javaLibraryType string
|
|
|
|
javaLibraryNameExtension string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
protoType: "nano",
|
|
|
|
javaLibraryType: "java_nano_proto_library",
|
|
|
|
javaLibraryNameExtension: "java_proto_nano",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
protoType: "micro",
|
|
|
|
javaLibraryType: "java_micro_proto_library",
|
|
|
|
javaLibraryNameExtension: "java_proto_micro",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
protoType: "lite",
|
|
|
|
javaLibraryType: "java_lite_proto_library",
|
|
|
|
javaLibraryNameExtension: "java_proto_lite",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
protoType: "stream",
|
|
|
|
javaLibraryType: "java_stream_proto_library",
|
|
|
|
javaLibraryNameExtension: "java_proto_stream",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
protoType: "full",
|
|
|
|
javaLibraryType: "java_proto_library",
|
|
|
|
javaLibraryNameExtension: "java_proto",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
bp := `java_library_static {
|
|
|
|
name: "java-protos",
|
|
|
|
proto: {
|
|
|
|
type: "%s",
|
|
|
|
},
|
|
|
|
srcs: ["a.proto"],
|
2023-08-07 19:38:18 +02:00
|
|
|
sdk_version: "current",
|
2022-02-04 22:01:20 +01:00
|
|
|
}`
|
|
|
|
|
2022-08-31 20:28:19 +02:00
|
|
|
protoLibrary := MakeBazelTarget("proto_library", "java-protos_proto", AttrNameToString{
|
2022-03-07 17:20:54 +01:00
|
|
|
"srcs": `["a.proto"]`,
|
2022-02-04 22:01:20 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
javaLibraryName := fmt.Sprintf("java-protos_%s", tc.javaLibraryNameExtension)
|
|
|
|
|
2022-06-21 21:28:33 +02:00
|
|
|
runJavaProtoTestCase(t, Bp2buildTestCase{
|
|
|
|
Description: fmt.Sprintf("java_proto %s", tc.protoType),
|
|
|
|
Blueprint: fmt.Sprintf(bp, tc.protoType),
|
|
|
|
ExpectedBazelTargets: []string{
|
2022-02-04 22:01:20 +01:00
|
|
|
protoLibrary,
|
2022-08-31 20:28:19 +02:00
|
|
|
MakeBazelTarget(
|
2022-02-04 22:01:20 +01:00
|
|
|
tc.javaLibraryType,
|
|
|
|
javaLibraryName,
|
2022-06-21 21:28:33 +02:00
|
|
|
AttrNameToString{
|
2023-08-07 19:38:18 +02:00
|
|
|
"deps": `[":java-protos_proto"]`,
|
|
|
|
"sdk_version": `"current"`,
|
2022-02-04 22:01:20 +01:00
|
|
|
}),
|
2022-08-31 20:28:19 +02:00
|
|
|
MakeBazelTarget("java_library", "java-protos", AttrNameToString{
|
2023-08-07 19:38:18 +02:00
|
|
|
"exports": fmt.Sprintf(`[":%s"]`, javaLibraryName),
|
|
|
|
"sdk_version": `"current"`,
|
2022-02-04 22:01:20 +01:00
|
|
|
}),
|
2022-10-31 20:08:18 +01:00
|
|
|
MakeNeverlinkDuplicateTarget("java_library", "java-protos"),
|
2022-02-04 22:01:20 +01:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJavaProtoDefault(t *testing.T) {
|
2022-06-21 21:28:33 +02:00
|
|
|
runJavaProtoTestCase(t, Bp2buildTestCase{
|
|
|
|
Description: "java_library proto default",
|
|
|
|
Blueprint: `java_library_static {
|
2022-02-04 22:01:20 +01:00
|
|
|
name: "java-protos",
|
|
|
|
srcs: ["a.proto"],
|
2022-04-23 01:09:58 +02:00
|
|
|
java_version: "7",
|
2023-08-07 19:38:18 +02:00
|
|
|
sdk_version: "current",
|
2022-02-04 22:01:20 +01:00
|
|
|
}
|
|
|
|
`,
|
2022-06-21 21:28:33 +02:00
|
|
|
ExpectedBazelTargets: []string{
|
2022-08-31 20:28:19 +02:00
|
|
|
MakeBazelTarget("proto_library", "java-protos_proto", AttrNameToString{
|
2022-03-07 17:20:54 +01:00
|
|
|
"srcs": `["a.proto"]`,
|
2022-02-04 22:01:20 +01:00
|
|
|
}),
|
2022-08-31 20:28:19 +02:00
|
|
|
MakeBazelTarget(
|
2022-02-04 22:01:20 +01:00
|
|
|
"java_lite_proto_library",
|
|
|
|
"java-protos_java_proto_lite",
|
2022-06-21 21:28:33 +02:00
|
|
|
AttrNameToString{
|
2023-02-24 18:07:08 +01:00
|
|
|
"deps": `[":java-protos_proto"]`,
|
|
|
|
"java_version": `"7"`,
|
2023-08-07 19:38:18 +02:00
|
|
|
"sdk_version": `"current"`,
|
2022-02-04 22:01:20 +01:00
|
|
|
}),
|
2022-08-31 20:28:19 +02:00
|
|
|
MakeBazelTarget("java_library", "java-protos", AttrNameToString{
|
2023-02-24 18:07:08 +01:00
|
|
|
"exports": `[":java-protos_java_proto_lite"]`,
|
|
|
|
"java_version": `"7"`,
|
2023-08-07 19:38:18 +02:00
|
|
|
"sdk_version": `"current"`,
|
2022-02-04 22:01:20 +01:00
|
|
|
}),
|
2023-02-24 18:07:08 +01:00
|
|
|
MakeNeverlinkDuplicateTargetWithAttrs(
|
|
|
|
"java_library",
|
|
|
|
"java-protos",
|
2023-08-07 19:38:18 +02:00
|
|
|
AttrNameToString{
|
|
|
|
"java_version": `"7"`,
|
|
|
|
"sdk_version": `"current"`,
|
|
|
|
}),
|
2022-02-04 22:01:20 +01:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2023-09-12 19:24:35 +02:00
|
|
|
|
|
|
|
func TestJavaLibsAndOnlyProtoSrcs(t *testing.T) {
|
|
|
|
runJavaProtoTestCase(t, Bp2buildTestCase{
|
2023-09-21 17:30:27 +02:00
|
|
|
Description: "java_library that has only proto srcs",
|
2023-09-22 01:00:45 +02:00
|
|
|
StubbedBuildDefinitions: []string{"java-lib-1", "java-lib-2"},
|
2023-09-12 19:24:35 +02:00
|
|
|
Blueprint: `java_library_static {
|
|
|
|
name: "java-protos",
|
|
|
|
srcs: ["a.proto"],
|
2023-09-22 01:00:45 +02:00
|
|
|
libs: ["java-lib-1"],
|
|
|
|
static_libs: ["java-lib-2"],
|
2023-09-12 19:24:35 +02:00
|
|
|
java_version: "7",
|
|
|
|
sdk_version: "current",
|
|
|
|
}
|
|
|
|
|
|
|
|
java_library_static {
|
2023-09-22 01:00:45 +02:00
|
|
|
name: "java-lib-1",
|
|
|
|
}
|
|
|
|
|
|
|
|
java_library_static {
|
|
|
|
name: "java-lib-2",
|
2023-09-12 19:24:35 +02:00
|
|
|
}
|
|
|
|
`,
|
|
|
|
ExpectedBazelTargets: []string{
|
|
|
|
MakeBazelTarget("proto_library", "java-protos_proto", AttrNameToString{
|
|
|
|
"srcs": `["a.proto"]`,
|
|
|
|
}),
|
|
|
|
MakeBazelTarget(
|
|
|
|
"java_lite_proto_library",
|
|
|
|
"java-protos_java_proto_lite",
|
|
|
|
AttrNameToString{
|
2023-09-22 01:00:45 +02:00
|
|
|
"deps": `[":java-protos_proto"]`,
|
|
|
|
"additional_proto_deps": `[
|
|
|
|
":java-lib-1-neverlink",
|
|
|
|
":java-lib-2",
|
|
|
|
]`,
|
2023-09-12 19:24:35 +02:00
|
|
|
"java_version": `"7"`,
|
|
|
|
"sdk_version": `"current"`,
|
|
|
|
}),
|
|
|
|
MakeBazelTarget("java_library", "java-protos", AttrNameToString{
|
2023-09-22 01:00:45 +02:00
|
|
|
"exports": `[
|
|
|
|
":java-lib-2",
|
|
|
|
":java-protos_java_proto_lite",
|
|
|
|
]`,
|
2023-09-12 19:24:35 +02:00
|
|
|
"java_version": `"7"`,
|
|
|
|
"sdk_version": `"current"`,
|
|
|
|
}),
|
|
|
|
MakeNeverlinkDuplicateTargetWithAttrs(
|
|
|
|
"java_library",
|
|
|
|
"java-protos",
|
|
|
|
AttrNameToString{
|
|
|
|
"java_version": `"7"`,
|
|
|
|
"sdk_version": `"current"`,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2023-10-03 18:58:34 +02:00
|
|
|
|
|
|
|
func TestJavaProtoPlugin(t *testing.T) {
|
|
|
|
runJavaProtoTestCase(t, Bp2buildTestCase{
|
|
|
|
Description: "java_library proto plugin",
|
|
|
|
StubbedBuildDefinitions: []string{"protoc-gen-test-plugin"},
|
|
|
|
Blueprint: `java_library_static {
|
|
|
|
name: "java-protos",
|
|
|
|
srcs: ["a.proto"],
|
|
|
|
proto: {
|
|
|
|
plugin: "test-plugin",
|
|
|
|
},
|
|
|
|
sdk_version: "current",
|
|
|
|
}
|
|
|
|
|
|
|
|
java_library_static {
|
|
|
|
name: "protoc-gen-test-plugin",
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
ExpectedBazelTargets: []string{
|
|
|
|
MakeBazelTarget("proto_library", "java-protos_proto", AttrNameToString{
|
|
|
|
"srcs": `["a.proto"]`,
|
|
|
|
}),
|
|
|
|
MakeBazelTarget(
|
|
|
|
"java_lite_proto_library",
|
|
|
|
"java-protos_java_proto_lite",
|
|
|
|
AttrNameToString{
|
|
|
|
"deps": `[":java-protos_proto"]`,
|
|
|
|
"plugin": `":protoc-gen-test-plugin"`,
|
|
|
|
"sdk_version": `"current"`,
|
|
|
|
}),
|
|
|
|
MakeBazelTarget("java_library", "java-protos", AttrNameToString{
|
|
|
|
"exports": `[":java-protos_java_proto_lite"]`,
|
|
|
|
"sdk_version": `"current"`,
|
|
|
|
}),
|
|
|
|
MakeNeverlinkDuplicateTargetWithAttrs(
|
|
|
|
"java_library",
|
|
|
|
"java-protos",
|
|
|
|
AttrNameToString{
|
|
|
|
"sdk_version": `"current"`,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|