blob: 4b75e3b801343aafd462c1f9caae2436b0da05c9 [file] [log] [blame]
Wei Libafb6d62021-12-10 03:14:59 -08001// Copyright 2021 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package bp2build
16
17import (
Sam Delmericoc0161432022-02-25 21:34:51 +000018 "fmt"
Wei Libafb6d62021-12-10 03:14:59 -080019 "testing"
20
21 "android/soong/android"
22 "android/soong/java"
23)
24
Sam Delmerico77267c72022-03-18 14:11:07 +000025func runJavaLibraryTestCaseWithRegistrationCtxFunc(t *testing.T, tc bp2buildTestCase, registrationCtxFunc func(ctx android.RegistrationContext)) {
Wei Libafb6d62021-12-10 03:14:59 -080026 t.Helper()
27 (&tc).moduleTypeUnderTest = "java_library"
28 (&tc).moduleTypeUnderTestFactory = java.LibraryFactory
Sam Delmerico77267c72022-03-18 14:11:07 +000029 runBp2BuildTestCase(t, registrationCtxFunc, tc)
30}
31
32func runJavaLibraryTestCase(t *testing.T, tc bp2buildTestCase) {
Sam Delmerico58614c02022-03-15 21:02:09 +000033 t.Helper()
Sam Delmerico77267c72022-03-18 14:11:07 +000034 runJavaLibraryTestCaseWithRegistrationCtxFunc(t, tc, func(ctx android.RegistrationContext) {})
Wei Libafb6d62021-12-10 03:14:59 -080035}
36
37func TestJavaLibrary(t *testing.T) {
38 runJavaLibraryTestCase(t, bp2buildTestCase{
39 description: "java_library with srcs, exclude_srcs and libs",
40 blueprint: `java_library {
41 name: "java-lib-1",
42 srcs: ["a.java", "b.java"],
43 exclude_srcs: ["b.java"],
44 libs: ["java-lib-2"],
45 bazel_module: { bp2build_available: true },
46}
47
48java_library {
49 name: "java-lib-2",
50 srcs: ["b.java"],
51 bazel_module: { bp2build_available: true },
52}`,
53 expectedBazelTargets: []string{
54 makeBazelTarget("java_library", "java-lib-1", attrNameToString{
55 "srcs": `["a.java"]`,
56 "deps": `[":java-lib-2"]`,
57 }),
58 makeBazelTarget("java_library", "java-lib-2", attrNameToString{
59 "srcs": `["b.java"]`,
60 }),
61 },
62 })
63}
Sam Delmericoc0161432022-02-25 21:34:51 +000064
65func TestJavaLibraryConvertsStaticLibsToDepsAndExports(t *testing.T) {
66 runJavaLibraryTestCase(t, bp2buildTestCase{
67 blueprint: `java_library {
68 name: "java-lib-1",
69 srcs: ["a.java"],
70 libs: ["java-lib-2"],
71 static_libs: ["java-lib-3"],
72 bazel_module: { bp2build_available: true },
73}
74
75java_library {
76 name: "java-lib-2",
77 srcs: ["b.java"],
78 bazel_module: { bp2build_available: false },
79}
80
81java_library {
82 name: "java-lib-3",
83 srcs: ["c.java"],
84 bazel_module: { bp2build_available: false },
85}`,
86 expectedBazelTargets: []string{
87 makeBazelTarget("java_library", "java-lib-1", attrNameToString{
88 "srcs": `["a.java"]`,
89 "deps": `[
90 ":java-lib-2",
91 ":java-lib-3",
92 ]`,
93 "exports": `[":java-lib-3"]`,
94 }),
95 },
96 })
97}
98
99func TestJavaLibraryConvertsStaticLibsToExportsIfNoSrcs(t *testing.T) {
100 runJavaLibraryTestCase(t, bp2buildTestCase{
101 blueprint: `java_library {
102 name: "java-lib-1",
103 static_libs: ["java-lib-2"],
104 bazel_module: { bp2build_available: true },
105}
106
107java_library {
108 name: "java-lib-2",
109 srcs: ["a.java"],
110 bazel_module: { bp2build_available: false },
111}`,
112 expectedBazelTargets: []string{
113 makeBazelTarget("java_library", "java-lib-1", attrNameToString{
114 "exports": `[":java-lib-2"]`,
115 }),
116 },
117 })
118}
119
120func TestJavaLibraryFailsToConvertLibsWithNoSrcs(t *testing.T) {
121 runJavaLibraryTestCase(t, bp2buildTestCase{
122 expectedErr: fmt.Errorf("Module has direct dependencies but no sources. Bazel will not allow this."),
123 blueprint: `java_library {
124 name: "java-lib-1",
125 libs: ["java-lib-2"],
126 bazel_module: { bp2build_available: true },
127}
128
129java_library {
130 name: "java-lib-2",
131 srcs: ["a.java"],
132 bazel_module: { bp2build_available: false },
133}`,
134 expectedBazelTargets: []string{},
135 })
136}
Sam Delmerico77267c72022-03-18 14:11:07 +0000137
138func TestJavaLibraryPlugins(t *testing.T) {
139 runJavaLibraryTestCaseWithRegistrationCtxFunc(t, bp2buildTestCase{
140 blueprint: `java_library {
141 name: "java-lib-1",
142 plugins: ["java-plugin-1"],
143 bazel_module: { bp2build_available: true },
144}
145
146java_plugin {
147 name: "java-plugin-1",
148 srcs: ["a.java"],
149 bazel_module: { bp2build_available: false },
150}`,
151 expectedBazelTargets: []string{
152 makeBazelTarget("java_library", "java-lib-1", attrNameToString{
153 "plugins": `[":java-plugin-1"]`,
154 }),
155 },
156 }, func(ctx android.RegistrationContext) {
157 ctx.RegisterModuleType("java_plugin", java.PluginFactory)
158 })
159}
Sam Delmerico58614c02022-03-15 21:02:09 +0000160
161func TestJavaLibraryErrorproneJavacflagsEnabledManually(t *testing.T) {
162 runJavaLibraryTestCase(t, bp2buildTestCase{
163 blueprint: `java_library {
164 name: "java-lib-1",
165 srcs: ["a.java"],
166 javacflags: ["-Xsuper-fast"],
167 errorprone: {
168 enabled: true,
169 javacflags: ["-Xep:SpeedLimit:OFF"],
170 },
171}`,
172 expectedBazelTargets: []string{
173 makeBazelTarget("java_library", "java-lib-1", attrNameToString{
174 "javacopts": `[
175 "-Xsuper-fast",
176 "-Xep:SpeedLimit:OFF",
177 ]`,
178 "srcs": `["a.java"]`,
179 }),
180 },
181 })
182}
183
184func TestJavaLibraryErrorproneJavacflagsErrorproneDisabledByDefault(t *testing.T) {
185 runJavaLibraryTestCase(t, bp2buildTestCase{
186 blueprint: `java_library {
187 name: "java-lib-1",
188 srcs: ["a.java"],
189 javacflags: ["-Xsuper-fast"],
190 errorprone: {
191 javacflags: ["-Xep:SpeedLimit:OFF"],
192 },
193}`,
194 expectedBazelTargets: []string{
195 makeBazelTarget("java_library", "java-lib-1", attrNameToString{
196 "javacopts": `["-Xsuper-fast"]`,
197 "srcs": `["a.java"]`,
198 }),
199 },
200 })
201}
202
203func TestJavaLibraryErrorproneJavacflagsErrorproneDisabledManually(t *testing.T) {
204 runJavaLibraryTestCase(t, bp2buildTestCase{
205 blueprint: `java_library {
206 name: "java-lib-1",
207 srcs: ["a.java"],
208 javacflags: ["-Xsuper-fast"],
209 errorprone: {
210 enabled: false,
211 javacflags: ["-Xep:SpeedLimit:OFF"],
212 },
213}`,
214 expectedBazelTargets: []string{
215 makeBazelTarget("java_library", "java-lib-1", attrNameToString{
216 "javacopts": `["-Xsuper-fast"]`,
217 "srcs": `["a.java"]`,
218 }),
219 },
220 })
221}