blob: f5a59382c7771d5d873aa1fad000e941757ed3ca [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 Delmerico3177a6e2022-06-21 19:28:33 +000025func runJavaLibraryTestCaseWithRegistrationCtxFunc(t *testing.T, tc Bp2buildTestCase, registrationCtxFunc func(ctx android.RegistrationContext)) {
Wei Libafb6d62021-12-10 03:14:59 -080026 t.Helper()
Sam Delmerico3177a6e2022-06-21 19:28:33 +000027 (&tc).ModuleTypeUnderTest = "java_library"
28 (&tc).ModuleTypeUnderTestFactory = java.LibraryFactory
29 RunBp2BuildTestCase(t, registrationCtxFunc, tc)
Sam Delmerico77267c72022-03-18 14:11:07 +000030}
31
Sam Delmerico3177a6e2022-06-21 19:28:33 +000032func 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) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +000038 runJavaLibraryTestCase(t, Bp2buildTestCase{
39 Description: "java_library with srcs, exclude_srcs and libs",
40 Blueprint: `java_library {
Wei Libafb6d62021-12-10 03:14:59 -080041 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}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +000053 ExpectedBazelTargets: []string{
54 makeBazelTarget("java_library", "java-lib-1", AttrNameToString{
Wei Libafb6d62021-12-10 03:14:59 -080055 "srcs": `["a.java"]`,
56 "deps": `[":java-lib-2"]`,
57 }),
Sam Delmerico3177a6e2022-06-21 19:28:33 +000058 makeBazelTarget("java_library", "java-lib-2", AttrNameToString{
Wei Libafb6d62021-12-10 03:14:59 -080059 "srcs": `["b.java"]`,
60 }),
61 },
62 })
63}
Sam Delmericoc0161432022-02-25 21:34:51 +000064
65func TestJavaLibraryConvertsStaticLibsToDepsAndExports(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +000066 runJavaLibraryTestCase(t, Bp2buildTestCase{
67 Blueprint: `java_library {
Sam Delmericoc0161432022-02-25 21:34:51 +000068 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}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +000086 ExpectedBazelTargets: []string{
87 makeBazelTarget("java_library", "java-lib-1", AttrNameToString{
Sam Delmericoc0161432022-02-25 21:34:51 +000088 "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) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000100 runJavaLibraryTestCase(t, Bp2buildTestCase{
101 Blueprint: `java_library {
Sam Delmericoc0161432022-02-25 21:34:51 +0000102 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}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000112 ExpectedBazelTargets: []string{
113 makeBazelTarget("java_library", "java-lib-1", AttrNameToString{
Sam Delmericoc0161432022-02-25 21:34:51 +0000114 "exports": `[":java-lib-2"]`,
115 }),
116 },
117 })
118}
119
120func TestJavaLibraryFailsToConvertLibsWithNoSrcs(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000121 runJavaLibraryTestCase(t, Bp2buildTestCase{
122 ExpectedErr: fmt.Errorf("Module has direct dependencies but no sources. Bazel will not allow this."),
123 Blueprint: `java_library {
Sam Delmericoc0161432022-02-25 21:34:51 +0000124 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}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000134 ExpectedBazelTargets: []string{},
Sam Delmericoc0161432022-02-25 21:34:51 +0000135 })
136}
Sam Delmerico77267c72022-03-18 14:11:07 +0000137
138func TestJavaLibraryPlugins(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000139 runJavaLibraryTestCaseWithRegistrationCtxFunc(t, Bp2buildTestCase{
140 Blueprint: `java_library {
Sam Delmerico77267c72022-03-18 14:11:07 +0000141 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}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000151 ExpectedBazelTargets: []string{
152 makeBazelTarget("java_library", "java-lib-1", AttrNameToString{
Sam Delmerico77267c72022-03-18 14:11:07 +0000153 "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
Vinh Tran3ac6daf2022-04-22 19:09:58 -0400161func TestJavaLibraryJavaVersion(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000162 runJavaLibraryTestCase(t, Bp2buildTestCase{
163 Blueprint: `java_library {
Vinh Tran3ac6daf2022-04-22 19:09:58 -0400164 name: "java-lib-1",
165 srcs: ["a.java"],
166 java_version: "11",
167}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000168 ExpectedBazelTargets: []string{
169 makeBazelTarget("java_library", "java-lib-1", AttrNameToString{
Vinh Tran3ac6daf2022-04-22 19:09:58 -0400170 "srcs": `["a.java"]`,
171 "javacopts": `["-source 11 -target 11"]`,
172 }),
173 },
174 })
175}
176
Sam Delmerico58614c02022-03-15 21:02:09 +0000177func TestJavaLibraryErrorproneJavacflagsEnabledManually(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000178 runJavaLibraryTestCase(t, Bp2buildTestCase{
179 Blueprint: `java_library {
Sam Delmerico58614c02022-03-15 21:02:09 +0000180 name: "java-lib-1",
181 srcs: ["a.java"],
182 javacflags: ["-Xsuper-fast"],
183 errorprone: {
184 enabled: true,
185 javacflags: ["-Xep:SpeedLimit:OFF"],
186 },
187}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000188 ExpectedBazelTargets: []string{
189 makeBazelTarget("java_library", "java-lib-1", AttrNameToString{
Sam Delmerico58614c02022-03-15 21:02:09 +0000190 "javacopts": `[
191 "-Xsuper-fast",
192 "-Xep:SpeedLimit:OFF",
193 ]`,
194 "srcs": `["a.java"]`,
195 }),
196 },
197 })
198}
199
200func TestJavaLibraryErrorproneJavacflagsErrorproneDisabledByDefault(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000201 runJavaLibraryTestCase(t, Bp2buildTestCase{
202 Blueprint: `java_library {
Sam Delmerico58614c02022-03-15 21:02:09 +0000203 name: "java-lib-1",
204 srcs: ["a.java"],
205 javacflags: ["-Xsuper-fast"],
206 errorprone: {
207 javacflags: ["-Xep:SpeedLimit:OFF"],
208 },
209}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000210 ExpectedBazelTargets: []string{
211 makeBazelTarget("java_library", "java-lib-1", AttrNameToString{
Sam Delmerico58614c02022-03-15 21:02:09 +0000212 "javacopts": `["-Xsuper-fast"]`,
213 "srcs": `["a.java"]`,
214 }),
215 },
216 })
217}
218
219func TestJavaLibraryErrorproneJavacflagsErrorproneDisabledManually(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000220 runJavaLibraryTestCase(t, Bp2buildTestCase{
221 Blueprint: `java_library {
Sam Delmerico58614c02022-03-15 21:02:09 +0000222 name: "java-lib-1",
223 srcs: ["a.java"],
224 javacflags: ["-Xsuper-fast"],
225 errorprone: {
226 enabled: false,
227 javacflags: ["-Xep:SpeedLimit:OFF"],
228 },
229}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000230 ExpectedBazelTargets: []string{
231 makeBazelTarget("java_library", "java-lib-1", AttrNameToString{
Sam Delmerico58614c02022-03-15 21:02:09 +0000232 "javacopts": `["-Xsuper-fast"]`,
233 "srcs": `["a.java"]`,
234 }),
235 },
236 })
237}
Sam Delmerico24da73c2022-03-16 20:36:54 +0000238
239func TestJavaLibraryLogTags(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000240 runJavaLibraryTestCase(t, Bp2buildTestCase{
241 Description: "Java library - logtags creates separate dependency",
242 ModuleTypeUnderTest: "java_library",
243 ModuleTypeUnderTestFactory: java.LibraryFactory,
244 Blueprint: `java_library {
Sam Delmerico24da73c2022-03-16 20:36:54 +0000245 name: "example_lib",
246 srcs: [
247 "a.java",
248 "b.java",
249 "a.logtag",
250 "b.logtag",
251 ],
252 bazel_module: { bp2build_available: true },
253}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000254 ExpectedBazelTargets: []string{
255 makeBazelTarget("event_log_tags", "example_lib_logtags", AttrNameToString{
Sam Delmerico24da73c2022-03-16 20:36:54 +0000256 "srcs": `[
257 "a.logtag",
258 "b.logtag",
259 ]`,
260 }),
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000261 makeBazelTarget("java_library", "example_lib", AttrNameToString{
Sam Delmerico24da73c2022-03-16 20:36:54 +0000262 "srcs": `[
263 "a.java",
264 "b.java",
265 ":example_lib_logtags",
266 ]`,
267 }),
268 }})
269}
Sam Delmerico79985812022-03-23 20:20:42 +0000270
271func TestJavaLibraryResources(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000272 runJavaLibraryTestCase(t, Bp2buildTestCase{
273 Filesystem: map[string]string{
Sam Delmerico79985812022-03-23 20:20:42 +0000274 "res/a.res": "",
275 "res/b.res": "",
276 "res/dir1/b.res": "",
277 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000278 Blueprint: `java_library {
Sam Delmerico79985812022-03-23 20:20:42 +0000279 name: "java-lib-1",
280 java_resources: ["res/a.res", "res/b.res"],
281}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000282 ExpectedBazelTargets: []string{
283 makeBazelTarget("java_library", "java-lib-1", AttrNameToString{
Sam Delmerico79985812022-03-23 20:20:42 +0000284 "resources": `[
285 "res/a.res",
286 "res/b.res",
287 ]`,
288 }),
289 },
290 })
291}
292
293func TestJavaLibraryResourceDirs(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000294 runJavaLibraryTestCase(t, Bp2buildTestCase{
295 Filesystem: map[string]string{
Sam Delmerico79985812022-03-23 20:20:42 +0000296 "res/a.res": "",
297 "res/b.res": "",
298 "res/dir1/b.res": "",
299 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000300 Blueprint: `java_library {
Sam Delmerico79985812022-03-23 20:20:42 +0000301 name: "java-lib-1",
302 java_resource_dirs: ["res"],
303}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000304 ExpectedBazelTargets: []string{
305 makeBazelTarget("java_library", "java-lib-1", AttrNameToString{
Sam Delmerico79985812022-03-23 20:20:42 +0000306 "resource_strip_prefix": `"res"`,
307 "resources": `[
308 "res/a.res",
309 "res/b.res",
310 "res/dir1/b.res",
311 ]`,
312 }),
313 },
314 })
315}
316
317func TestJavaLibraryResourcesExcludeDir(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000318 runJavaLibraryTestCase(t, Bp2buildTestCase{
319 Filesystem: map[string]string{
Sam Delmerico79985812022-03-23 20:20:42 +0000320 "res/a.res": "",
321 "res/exclude/b.res": "",
322 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000323 Blueprint: `java_library {
Sam Delmerico79985812022-03-23 20:20:42 +0000324 name: "java-lib-1",
325 java_resource_dirs: ["res"],
326 exclude_java_resource_dirs: ["res/exclude"],
327}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000328 ExpectedBazelTargets: []string{
329 makeBazelTarget("java_library", "java-lib-1", AttrNameToString{
Sam Delmerico79985812022-03-23 20:20:42 +0000330 "resource_strip_prefix": `"res"`,
331 "resources": `["res/a.res"]`,
332 }),
333 },
334 })
335}
336
337func TestJavaLibraryResourcesExcludeFile(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000338 runJavaLibraryTestCase(t, Bp2buildTestCase{
339 Filesystem: map[string]string{
Sam Delmerico79985812022-03-23 20:20:42 +0000340 "res/a.res": "",
341 "res/dir1/b.res": "",
342 "res/dir1/exclude.res": "",
343 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000344 Blueprint: `java_library {
Sam Delmerico79985812022-03-23 20:20:42 +0000345 name: "java-lib-1",
346 java_resource_dirs: ["res"],
347 exclude_java_resources: ["res/dir1/exclude.res"],
348}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000349 ExpectedBazelTargets: []string{
350 makeBazelTarget("java_library", "java-lib-1", AttrNameToString{
Sam Delmerico79985812022-03-23 20:20:42 +0000351 "resource_strip_prefix": `"res"`,
352 "resources": `[
353 "res/a.res",
354 "res/dir1/b.res",
355 ]`,
356 }),
357 },
358 })
359}
360
361func TestJavaLibraryResourcesFailsWithMultipleDirs(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000362 runJavaLibraryTestCase(t, Bp2buildTestCase{
363 Filesystem: map[string]string{
Sam Delmerico79985812022-03-23 20:20:42 +0000364 "res/a.res": "",
365 "res1/a.res": "",
366 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000367 Blueprint: `java_library {
Sam Delmerico79985812022-03-23 20:20:42 +0000368 name: "java-lib-1",
369 java_resource_dirs: ["res", "res1"],
370}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000371 ExpectedErr: fmt.Errorf("bp2build does not support more than one directory in java_resource_dirs (b/226423379)"),
372 ExpectedBazelTargets: []string{},
Sam Delmerico79985812022-03-23 20:20:42 +0000373 })
374}
Sam Delmerico97bd1272022-08-25 14:45:31 -0400375
376func TestJavaLibraryAidl(t *testing.T) {
377 runJavaLibraryTestCase(t, Bp2buildTestCase{
378 Description: "Java library - aidl creates separate dependency",
379 ModuleTypeUnderTest: "java_library",
380 ModuleTypeUnderTestFactory: java.LibraryFactory,
381 Blueprint: `java_library {
382 name: "example_lib",
383 srcs: [
384 "a.java",
385 "b.java",
386 "a.aidl",
387 "b.aidl",
388 ],
389 bazel_module: { bp2build_available: true },
390}`,
391 ExpectedBazelTargets: []string{
392 makeBazelTarget("aidl_library", "example_lib_aidl_library", AttrNameToString{
393 "srcs": `[
394 "a.aidl",
395 "b.aidl",
396 ]`,
397 }),
398 makeBazelTarget("java_aidl_library", "example_lib_java_aidl_library", AttrNameToString{
399 "deps": `[":example_lib_aidl_library"]`,
400 }),
401 makeBazelTarget("java_library", "example_lib", AttrNameToString{
402 "deps": `[":example_lib_java_aidl_library"]`,
403 "exports": `[":example_lib_java_aidl_library"]`,
404 "srcs": `[
405 "a.java",
406 "b.java",
407 ]`,
408 }),
409 }})
410}
411
412func TestJavaLibraryAidlSrcsNoFileGroup(t *testing.T) {
413 runJavaLibraryTestCaseWithRegistrationCtxFunc(t, Bp2buildTestCase{
414 Description: "Java library - aidl filegroup is parsed",
415 ModuleTypeUnderTest: "java_library",
416 ModuleTypeUnderTestFactory: java.LibraryFactory,
417 Blueprint: `
418java_library {
419 name: "example_lib",
420 srcs: [
421 "a.java",
422 "b.aidl",
423 ],
424 bazel_module: { bp2build_available: true },
425}`,
426 ExpectedBazelTargets: []string{
427 makeBazelTarget("aidl_library", "example_lib_aidl_library", AttrNameToString{
428 "srcs": `["b.aidl"]`,
429 }),
430 makeBazelTarget("java_aidl_library", "example_lib_java_aidl_library", AttrNameToString{
431 "deps": `[":example_lib_aidl_library"]`,
432 }),
433 makeBazelTarget("java_library", "example_lib", AttrNameToString{
434 "deps": `[":example_lib_java_aidl_library"]`,
435 "exports": `[":example_lib_java_aidl_library"]`,
436 "srcs": `["a.java"]`,
437 }),
438 },
439 }, func(ctx android.RegistrationContext) {
440 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
441 })
442}
443
444func TestJavaLibraryAidlFilegroup(t *testing.T) {
445 runJavaLibraryTestCaseWithRegistrationCtxFunc(t, Bp2buildTestCase{
446 Description: "Java library - aidl filegroup is parsed",
447 ModuleTypeUnderTest: "java_library",
448 ModuleTypeUnderTestFactory: java.LibraryFactory,
449 Blueprint: `
450filegroup {
451 name: "random_other_files",
452 srcs: [
453 "a.java",
454 "b.java",
455 ],
456}
457filegroup {
458 name: "aidl_files",
459 srcs: [
460 "a.aidl",
461 "b.aidl",
462 ],
463}
464java_library {
465 name: "example_lib",
466 srcs: [
467 "a.java",
468 "b.java",
469 ":aidl_files",
470 ":random_other_files",
471 ],
472 bazel_module: { bp2build_available: true },
473}`,
474 ExpectedBazelTargets: []string{
475 MakeBazelTargetNoRestrictions("aidl_library", "aidl_files", AttrNameToString{
476 "srcs": `[
477 "a.aidl",
478 "b.aidl",
479 ]`,
480 }),
481 makeBazelTarget("java_aidl_library", "example_lib_java_aidl_library", AttrNameToString{
482 "deps": `[":aidl_files"]`,
483 }),
484 makeBazelTarget("java_library", "example_lib", AttrNameToString{
485 "deps": `[":example_lib_java_aidl_library"]`,
486 "exports": `[":example_lib_java_aidl_library"]`,
487 "srcs": `[
488 "a.java",
489 "b.java",
490 ":random_other_files",
491 ]`,
492 }),
493 MakeBazelTargetNoRestrictions("filegroup", "random_other_files", AttrNameToString{
494 "srcs": `[
495 "a.java",
496 "b.java",
497 ]`,
498 }),
499 },
500 }, func(ctx android.RegistrationContext) {
501 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
502 })
503}