blob: 422422b1fc6967526d07e17d35cd47ac20a34963 [file] [log] [blame]
Liz Kammer2dd9ca42020-11-25 16:06:39 -08001// Copyright 2020 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 (
18 "android/soong/android"
Jingwen Chen316e07c2020-12-14 09:09:52 -050019 "android/soong/genrule"
Liz Kammer356f7d42021-01-26 09:18:53 -050020 "strings"
Liz Kammer2dd9ca42020-11-25 16:06:39 -080021 "testing"
22)
23
24func TestGenerateSoongModuleTargets(t *testing.T) {
25 testCases := []struct {
26 bp string
27 expectedBazelTarget string
28 }{
29 {
30 bp: `custom {
31 name: "foo",
32}
33 `,
34 expectedBazelTarget: `soong_module(
35 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050036 soong_module_name = "foo",
37 soong_module_type = "custom",
38 soong_module_variant = "",
39 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080040 ],
41)`,
42 },
43 {
44 bp: `custom {
45 name: "foo",
46 ramdisk: true,
47}
48 `,
49 expectedBazelTarget: `soong_module(
50 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050051 soong_module_name = "foo",
52 soong_module_type = "custom",
53 soong_module_variant = "",
54 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080055 ],
56 ramdisk = True,
57)`,
58 },
59 {
60 bp: `custom {
61 name: "foo",
62 owner: "a_string_with\"quotes\"_and_\\backslashes\\\\",
63}
64 `,
65 expectedBazelTarget: `soong_module(
66 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050067 soong_module_name = "foo",
68 soong_module_type = "custom",
69 soong_module_variant = "",
70 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080071 ],
72 owner = "a_string_with\"quotes\"_and_\\backslashes\\\\",
73)`,
74 },
75 {
76 bp: `custom {
77 name: "foo",
78 required: ["bar"],
79}
80 `,
81 expectedBazelTarget: `soong_module(
82 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050083 soong_module_name = "foo",
84 soong_module_type = "custom",
85 soong_module_variant = "",
86 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080087 ],
88 required = [
89 "bar",
90 ],
91)`,
92 },
93 {
94 bp: `custom {
95 name: "foo",
96 target_required: ["qux", "bazqux"],
97}
98 `,
99 expectedBazelTarget: `soong_module(
100 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500101 soong_module_name = "foo",
102 soong_module_type = "custom",
103 soong_module_variant = "",
104 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800105 ],
106 target_required = [
107 "qux",
108 "bazqux",
109 ],
110)`,
111 },
112 {
113 bp: `custom {
114 name: "foo",
115 dist: {
116 targets: ["goal_foo"],
117 tag: ".foo",
118 },
119 dists: [
120 {
121 targets: ["goal_bar"],
122 tag: ".bar",
123 },
124 ],
125}
126 `,
127 expectedBazelTarget: `soong_module(
128 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500129 soong_module_name = "foo",
130 soong_module_type = "custom",
131 soong_module_variant = "",
132 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800133 ],
134 dist = {
135 "tag": ".foo",
136 "targets": [
137 "goal_foo",
138 ],
139 },
140 dists = [
141 {
142 "tag": ".bar",
143 "targets": [
144 "goal_bar",
145 ],
146 },
147 ],
148)`,
149 },
150 {
151 bp: `custom {
152 name: "foo",
153 required: ["bar"],
154 target_required: ["qux", "bazqux"],
155 ramdisk: true,
156 owner: "custom_owner",
157 dists: [
158 {
159 tag: ".tag",
160 targets: ["my_goal"],
161 },
162 ],
163}
164 `,
165 expectedBazelTarget: `soong_module(
166 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500167 soong_module_name = "foo",
168 soong_module_type = "custom",
169 soong_module_variant = "",
170 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800171 ],
172 dists = [
173 {
174 "tag": ".tag",
175 "targets": [
176 "my_goal",
177 ],
178 },
179 ],
180 owner = "custom_owner",
181 ramdisk = True,
182 required = [
183 "bar",
184 ],
185 target_required = [
186 "qux",
187 "bazqux",
188 ],
189)`,
190 },
191 }
192
193 dir := "."
194 for _, testCase := range testCases {
195 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
196 ctx := android.NewTestContext(config)
197 ctx.RegisterModuleType("custom", customModuleFactory)
198 ctx.Register()
199
200 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
201 android.FailIfErrored(t, errs)
202 _, errs = ctx.PrepareBuildActions(config)
203 android.FailIfErrored(t, errs)
204
Jingwen Chen4d2c0872021-02-02 07:06:56 -0500205 bazelTargets := GenerateBazelTargets(ctx.Context.Context, QueryView)[dir]
Jingwen Chen4e4756d2021-01-24 21:13:13 -0500206 if actualCount, expectedCount := len(bazelTargets), 1; actualCount != expectedCount {
207 t.Fatalf("Expected %d bazel target, got %d", expectedCount, actualCount)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800208 }
209
210 actualBazelTarget := bazelTargets[0]
211 if actualBazelTarget.content != testCase.expectedBazelTarget {
212 t.Errorf(
213 "Expected generated Bazel target to be '%s', got '%s'",
214 testCase.expectedBazelTarget,
Jingwen Chen73850672020-12-14 08:25:34 -0500215 actualBazelTarget.content,
216 )
217 }
218 }
219}
220
221func TestGenerateBazelTargetModules(t *testing.T) {
222 testCases := []struct {
223 bp string
224 expectedBazelTarget string
225 }{
226 {
227 bp: `custom {
228 name: "foo",
229 string_list_prop: ["a", "b"],
230 string_prop: "a",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500231 bazel_module: { bp2build_available: true },
Jingwen Chen73850672020-12-14 08:25:34 -0500232}`,
233 expectedBazelTarget: `custom(
234 name = "foo",
235 string_list_prop = [
236 "a",
237 "b",
238 ],
239 string_prop = "a",
240)`,
241 },
242 }
243
244 dir := "."
245 for _, testCase := range testCases {
246 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
247 ctx := android.NewTestContext(config)
248 ctx.RegisterModuleType("custom", customModuleFactory)
249 ctx.RegisterBp2BuildMutator("custom", customBp2BuildMutator)
250 ctx.RegisterForBazelConversion()
251
252 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
Liz Kammer356f7d42021-01-26 09:18:53 -0500253 if Errored(t, "", errs) {
254 continue
255 }
Jingwen Chen73850672020-12-14 08:25:34 -0500256 _, errs = ctx.ResolveDependencies(config)
Liz Kammer356f7d42021-01-26 09:18:53 -0500257 if Errored(t, "", errs) {
258 continue
259 }
Jingwen Chen73850672020-12-14 08:25:34 -0500260
Jingwen Chen4d2c0872021-02-02 07:06:56 -0500261 bazelTargets := GenerateBazelTargets(ctx.Context.Context, Bp2Build)[dir]
Jingwen Chen4e4756d2021-01-24 21:13:13 -0500262 if actualCount, expectedCount := len(bazelTargets), 1; actualCount != expectedCount {
Liz Kammer356f7d42021-01-26 09:18:53 -0500263 t.Errorf("Expected %d bazel target, got %d", expectedCount, actualCount)
264 } else {
265 actualBazelTarget := bazelTargets[0]
266 if actualBazelTarget.content != testCase.expectedBazelTarget {
267 t.Errorf(
268 "Expected generated Bazel target to be '%s', got '%s'",
269 testCase.expectedBazelTarget,
270 actualBazelTarget.content,
271 )
272 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800273 }
274 }
275}
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500276
Jingwen Chen40067de2021-01-26 21:58:43 -0500277func TestLoadStatements(t *testing.T) {
278 testCases := []struct {
279 bazelTargets BazelTargets
280 expectedLoadStatements string
281 }{
282 {
283 bazelTargets: BazelTargets{
284 BazelTarget{
285 name: "foo",
286 ruleClass: "cc_library",
287 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
288 },
289 },
290 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
291 },
292 {
293 bazelTargets: BazelTargets{
294 BazelTarget{
295 name: "foo",
296 ruleClass: "cc_library",
297 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
298 },
299 BazelTarget{
300 name: "bar",
301 ruleClass: "cc_library",
302 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
303 },
304 },
305 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
306 },
307 {
308 bazelTargets: BazelTargets{
309 BazelTarget{
310 name: "foo",
311 ruleClass: "cc_library",
312 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
313 },
314 BazelTarget{
315 name: "bar",
316 ruleClass: "cc_binary",
317 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
318 },
319 },
320 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")`,
321 },
322 {
323 bazelTargets: BazelTargets{
324 BazelTarget{
325 name: "foo",
326 ruleClass: "cc_library",
327 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
328 },
329 BazelTarget{
330 name: "bar",
331 ruleClass: "cc_binary",
332 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
333 },
334 BazelTarget{
335 name: "baz",
336 ruleClass: "java_binary",
337 bzlLoadLocation: "//build/bazel/rules:java.bzl",
338 },
339 },
340 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")
341load("//build/bazel/rules:java.bzl", "java_binary")`,
342 },
343 {
344 bazelTargets: BazelTargets{
345 BazelTarget{
346 name: "foo",
347 ruleClass: "cc_binary",
348 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
349 },
350 BazelTarget{
351 name: "bar",
352 ruleClass: "java_binary",
353 bzlLoadLocation: "//build/bazel/rules:java.bzl",
354 },
355 BazelTarget{
356 name: "baz",
357 ruleClass: "genrule",
358 // Note: no bzlLoadLocation for native rules
359 },
360 },
361 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary")
362load("//build/bazel/rules:java.bzl", "java_binary")`,
363 },
364 }
365
366 for _, testCase := range testCases {
367 actual := testCase.bazelTargets.LoadStatements()
368 expected := testCase.expectedLoadStatements
369 if actual != expected {
370 t.Fatalf("Expected load statements to be %s, got %s", expected, actual)
371 }
372 }
373
374}
375
376func TestGenerateBazelTargetModules_OneToMany_LoadedFromStarlark(t *testing.T) {
377 testCases := []struct {
378 bp string
379 expectedBazelTarget string
380 expectedBazelTargetCount int
381 expectedLoadStatements string
382 }{
383 {
384 bp: `custom {
385 name: "bar",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500386 bazel_module: { bp2build_available: true },
Jingwen Chen40067de2021-01-26 21:58:43 -0500387}`,
388 expectedBazelTarget: `my_library(
389 name = "bar",
390)
391
392my_proto_library(
393 name = "bar_my_proto_library_deps",
394)
395
396proto_library(
397 name = "bar_proto_library_deps",
398)`,
399 expectedBazelTargetCount: 3,
400 expectedLoadStatements: `load("//build/bazel/rules:proto.bzl", "my_proto_library", "proto_library")
401load("//build/bazel/rules:rules.bzl", "my_library")`,
402 },
403 }
404
405 dir := "."
406 for _, testCase := range testCases {
407 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
408 ctx := android.NewTestContext(config)
409 ctx.RegisterModuleType("custom", customModuleFactory)
410 ctx.RegisterBp2BuildMutator("custom_starlark", customBp2BuildMutatorFromStarlark)
411 ctx.RegisterForBazelConversion()
412
413 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
414 android.FailIfErrored(t, errs)
415 _, errs = ctx.ResolveDependencies(config)
416 android.FailIfErrored(t, errs)
417
Jingwen Chen4d2c0872021-02-02 07:06:56 -0500418 bazelTargets := GenerateBazelTargets(ctx.Context.Context, Bp2Build)[dir]
Jingwen Chen40067de2021-01-26 21:58:43 -0500419 if actualCount := len(bazelTargets); actualCount != testCase.expectedBazelTargetCount {
420 t.Fatalf("Expected %d bazel target, got %d", testCase.expectedBazelTargetCount, actualCount)
421 }
422
423 actualBazelTargets := bazelTargets.String()
424 if actualBazelTargets != testCase.expectedBazelTarget {
425 t.Errorf(
426 "Expected generated Bazel target to be '%s', got '%s'",
427 testCase.expectedBazelTarget,
428 actualBazelTargets,
429 )
430 }
431
432 actualLoadStatements := bazelTargets.LoadStatements()
433 if actualLoadStatements != testCase.expectedLoadStatements {
434 t.Errorf(
435 "Expected generated load statements to be '%s', got '%s'",
436 testCase.expectedLoadStatements,
437 actualLoadStatements,
438 )
439 }
440 }
441}
442
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500443func TestModuleTypeBp2Build(t *testing.T) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500444 otherGenruleBp := map[string]string{
445 "other/Android.bp": `genrule {
446 name: "foo.tool",
447 out: ["foo_tool.out"],
448 srcs: ["foo_tool.in"],
449 cmd: "cp $(in) $(out)",
450}
451genrule {
452 name: "other.tool",
453 out: ["other_tool.out"],
454 srcs: ["other_tool.in"],
455 cmd: "cp $(in) $(out)",
456}`,
457 }
458
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500459 testCases := []struct {
Liz Kammer356f7d42021-01-26 09:18:53 -0500460 description string
Jingwen Chena42d6412021-01-26 21:57:27 -0500461 moduleTypeUnderTest string
462 moduleTypeUnderTestFactory android.ModuleFactory
463 moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
Liz Kammer356f7d42021-01-26 09:18:53 -0500464 preArchMutators []android.RegisterMutatorFunc
465 depsMutators []android.RegisterMutatorFunc
Jingwen Chena42d6412021-01-26 21:57:27 -0500466 bp string
Liz Kammer356f7d42021-01-26 09:18:53 -0500467 expectedBazelTargets []string
468 fs map[string]string
469 dir string
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500470 }{
471 {
Liz Kammerebfcf672021-02-16 15:00:05 -0500472 description: "filegroup with does not specify srcs",
473 moduleTypeUnderTest: "filegroup",
474 moduleTypeUnderTestFactory: android.FileGroupFactory,
475 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
476 bp: `filegroup {
477 name: "fg_foo",
478 bazel_module: { bp2build_available: true },
479}`,
480 expectedBazelTargets: []string{
481 `filegroup(
482 name = "fg_foo",
483)`,
484 },
485 },
486 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500487 description: "filegroup with no srcs",
488 moduleTypeUnderTest: "filegroup",
489 moduleTypeUnderTestFactory: android.FileGroupFactory,
490 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500491 bp: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500492 name: "fg_foo",
493 srcs: [],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500494 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500495}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500496 expectedBazelTargets: []string{
497 `filegroup(
498 name = "fg_foo",
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500499 srcs = [
500 ],
501)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500502 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500503 },
504 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500505 description: "filegroup with srcs",
506 moduleTypeUnderTest: "filegroup",
507 moduleTypeUnderTestFactory: android.FileGroupFactory,
508 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500509 bp: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500510 name: "fg_foo",
511 srcs: ["a", "b"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500512 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500513}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500514 expectedBazelTargets: []string{`filegroup(
515 name = "fg_foo",
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500516 srcs = [
517 "a",
518 "b",
519 ],
520)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500521 },
522 },
523 {
524 description: "filegroup with excludes srcs",
525 moduleTypeUnderTest: "filegroup",
526 moduleTypeUnderTestFactory: android.FileGroupFactory,
527 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
528 bp: `filegroup {
529 name: "fg_foo",
530 srcs: ["a", "b"],
531 exclude_srcs: ["a"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500532 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500533}`,
534 expectedBazelTargets: []string{`filegroup(
535 name = "fg_foo",
536 srcs = [
537 "b",
538 ],
539)`,
540 },
541 },
542 {
543 description: "filegroup with glob",
544 moduleTypeUnderTest: "filegroup",
545 moduleTypeUnderTestFactory: android.FileGroupFactory,
546 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
547 bp: `filegroup {
548 name: "foo",
549 srcs: ["**/*.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500550 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500551}`,
552 expectedBazelTargets: []string{`filegroup(
553 name = "foo",
554 srcs = [
555 "other/a.txt",
556 "other/b.txt",
557 "other/subdir/a.txt",
558 ],
559)`,
560 },
561 fs: map[string]string{
562 "other/a.txt": "",
563 "other/b.txt": "",
564 "other/subdir/a.txt": "",
565 "other/file": "",
566 },
567 },
568 {
569 description: "filegroup with glob in subdir",
570 moduleTypeUnderTest: "filegroup",
571 moduleTypeUnderTestFactory: android.FileGroupFactory,
572 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
573 bp: `filegroup {
574 name: "foo",
575 srcs: ["a.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500576 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500577}`,
578 dir: "other",
579 expectedBazelTargets: []string{`filegroup(
580 name = "fg_foo",
581 srcs = [
582 "a.txt",
583 "b.txt",
584 "subdir/a.txt",
585 ],
586)`,
587 },
588 fs: map[string]string{
589 "other/Android.bp": `filegroup {
590 name: "fg_foo",
591 srcs: ["**/*.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500592 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500593}`,
594 "other/a.txt": "",
595 "other/b.txt": "",
596 "other/subdir/a.txt": "",
597 "other/file": "",
598 },
599 },
600 {
601 description: "depends_on_other_dir_module",
602 moduleTypeUnderTest: "filegroup",
603 moduleTypeUnderTestFactory: android.FileGroupFactory,
604 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
605 bp: `filegroup {
606 name: "foobar",
607 srcs: [
608 ":foo",
609 "c",
610 ],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500611 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500612}`,
613 expectedBazelTargets: []string{`filegroup(
614 name = "foobar",
615 srcs = [
616 "//other:foo",
617 "c",
618 ],
619)`,
620 },
621 fs: map[string]string{
622 "other/Android.bp": `filegroup {
623 name: "foo",
624 srcs: ["a", "b"],
625}`,
626 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500627 },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500628 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500629 description: "genrule with command line variable replacements",
630 moduleTypeUnderTest: "genrule",
631 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
632 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Liz Kammer356f7d42021-01-26 09:18:53 -0500633 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
Jingwen Chen316e07c2020-12-14 09:09:52 -0500634 bp: `genrule {
Liz Kammer356f7d42021-01-26 09:18:53 -0500635 name: "foo.tool",
636 out: ["foo_tool.out"],
637 srcs: ["foo_tool.in"],
638 cmd: "cp $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500639 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500640}
641
642genrule {
Jingwen Chen316e07c2020-12-14 09:09:52 -0500643 name: "foo",
644 out: ["foo.out"],
645 srcs: ["foo.in"],
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500646 tools: [":foo.tool"],
647 cmd: "$(location :foo.tool) --genDir=$(genDir) arg $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500648 bazel_module: { bp2build_available: true },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500649}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500650 expectedBazelTargets: []string{
651 `genrule(
Jingwen Chen316e07c2020-12-14 09:09:52 -0500652 name = "foo",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500653 cmd = "$(location :foo.tool) --genDir=$(GENDIR) arg $(SRCS) $(OUTS)",
Jingwen Chen316e07c2020-12-14 09:09:52 -0500654 outs = [
655 "foo.out",
656 ],
657 srcs = [
658 "foo.in",
659 ],
660 tools = [
661 ":foo.tool",
662 ],
663)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500664 `genrule(
665 name = "foo.tool",
666 cmd = "cp $(SRCS) $(OUTS)",
667 outs = [
668 "foo_tool.out",
669 ],
670 srcs = [
671 "foo_tool.in",
672 ],
673)`,
674 },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500675 },
676 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500677 description: "genrule using $(locations :label)",
678 moduleTypeUnderTest: "genrule",
679 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
680 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Liz Kammer356f7d42021-01-26 09:18:53 -0500681 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
Jingwen Chen316e07c2020-12-14 09:09:52 -0500682 bp: `genrule {
Liz Kammer356f7d42021-01-26 09:18:53 -0500683 name: "foo.tools",
684 out: ["foo_tool.out", "foo_tool2.out"],
685 srcs: ["foo_tool.in"],
686 cmd: "cp $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500687 bazel_module: { bp2build_available: true },
688}
Liz Kammer356f7d42021-01-26 09:18:53 -0500689
690genrule {
Jingwen Chen316e07c2020-12-14 09:09:52 -0500691 name: "foo",
692 out: ["foo.out"],
693 srcs: ["foo.in"],
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500694 tools: [":foo.tools"],
695 cmd: "$(locations :foo.tools) -s $(out) $(in)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500696 bazel_module: { bp2build_available: true },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500697}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500698 expectedBazelTargets: []string{`genrule(
Jingwen Chen316e07c2020-12-14 09:09:52 -0500699 name = "foo",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500700 cmd = "$(locations :foo.tools) -s $(OUTS) $(SRCS)",
701 outs = [
702 "foo.out",
703 ],
704 srcs = [
705 "foo.in",
706 ],
707 tools = [
708 ":foo.tools",
709 ],
710)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500711 `genrule(
712 name = "foo.tools",
713 cmd = "cp $(SRCS) $(OUTS)",
714 outs = [
715 "foo_tool.out",
716 "foo_tool2.out",
717 ],
718 srcs = [
719 "foo_tool.in",
720 ],
721)`,
722 },
723 },
724 {
725 description: "genrule using $(locations //absolute:label)",
726 moduleTypeUnderTest: "genrule",
727 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
728 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
729 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
730 bp: `genrule {
731 name: "foo",
732 out: ["foo.out"],
733 srcs: ["foo.in"],
734 tool_files: [":foo.tool"],
735 cmd: "$(locations :foo.tool) -s $(out) $(in)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500736 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500737}`,
738 expectedBazelTargets: []string{`genrule(
739 name = "foo",
740 cmd = "$(locations //other:foo.tool) -s $(OUTS) $(SRCS)",
741 outs = [
742 "foo.out",
743 ],
744 srcs = [
745 "foo.in",
746 ],
747 tools = [
748 "//other:foo.tool",
749 ],
750)`,
751 },
752 fs: otherGenruleBp,
753 },
754 {
755 description: "genrule srcs using $(locations //absolute:label)",
756 moduleTypeUnderTest: "genrule",
757 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
758 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
759 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
760 bp: `genrule {
761 name: "foo",
762 out: ["foo.out"],
763 srcs: [":other.tool"],
764 tool_files: [":foo.tool"],
765 cmd: "$(locations :foo.tool) -s $(out) $(location :other.tool)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500766 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500767}`,
768 expectedBazelTargets: []string{`genrule(
769 name = "foo",
770 cmd = "$(locations //other:foo.tool) -s $(OUTS) $(location //other:other.tool)",
771 outs = [
772 "foo.out",
773 ],
774 srcs = [
775 "//other:other.tool",
776 ],
777 tools = [
778 "//other:foo.tool",
779 ],
780)`,
781 },
782 fs: otherGenruleBp,
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500783 },
784 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500785 description: "genrule using $(location) label should substitute first tool label automatically",
786 moduleTypeUnderTest: "genrule",
787 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
788 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Liz Kammer356f7d42021-01-26 09:18:53 -0500789 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500790 bp: `genrule {
791 name: "foo",
792 out: ["foo.out"],
793 srcs: ["foo.in"],
794 tool_files: [":foo.tool", ":other.tool"],
795 cmd: "$(location) -s $(out) $(in)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500796 bazel_module: { bp2build_available: true },
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500797}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500798 expectedBazelTargets: []string{`genrule(
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500799 name = "foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500800 cmd = "$(location //other:foo.tool) -s $(OUTS) $(SRCS)",
Jingwen Chen316e07c2020-12-14 09:09:52 -0500801 outs = [
802 "foo.out",
803 ],
804 srcs = [
805 "foo.in",
806 ],
807 tools = [
Liz Kammer356f7d42021-01-26 09:18:53 -0500808 "//other:foo.tool",
809 "//other:other.tool",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500810 ],
811)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500812 },
813 fs: otherGenruleBp,
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500814 },
815 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500816 description: "genrule using $(locations) label should substitute first tool label automatically",
817 moduleTypeUnderTest: "genrule",
818 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
819 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Liz Kammer356f7d42021-01-26 09:18:53 -0500820 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500821 bp: `genrule {
822 name: "foo",
823 out: ["foo.out"],
824 srcs: ["foo.in"],
825 tools: [":foo.tool", ":other.tool"],
826 cmd: "$(locations) -s $(out) $(in)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500827 bazel_module: { bp2build_available: true },
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500828}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500829 expectedBazelTargets: []string{`genrule(
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500830 name = "foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500831 cmd = "$(locations //other:foo.tool) -s $(OUTS) $(SRCS)",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500832 outs = [
833 "foo.out",
834 ],
835 srcs = [
836 "foo.in",
837 ],
838 tools = [
Liz Kammer356f7d42021-01-26 09:18:53 -0500839 "//other:foo.tool",
840 "//other:other.tool",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500841 ],
842)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500843 },
844 fs: otherGenruleBp,
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500845 },
846 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500847 description: "genrule without tools or tool_files can convert successfully",
848 moduleTypeUnderTest: "genrule",
849 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
850 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Liz Kammer356f7d42021-01-26 09:18:53 -0500851 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500852 bp: `genrule {
853 name: "foo",
854 out: ["foo.out"],
855 srcs: ["foo.in"],
856 cmd: "cp $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500857 bazel_module: { bp2build_available: true },
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500858}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500859 expectedBazelTargets: []string{`genrule(
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500860 name = "foo",
861 cmd = "cp $(SRCS) $(OUTS)",
862 outs = [
863 "foo.out",
864 ],
865 srcs = [
866 "foo.in",
Jingwen Chen316e07c2020-12-14 09:09:52 -0500867 ],
868)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500869 },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500870 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500871 }
872
873 dir := "."
874 for _, testCase := range testCases {
Liz Kammer356f7d42021-01-26 09:18:53 -0500875 fs := make(map[string][]byte)
876 toParse := []string{
877 "Android.bp",
878 }
879 for f, content := range testCase.fs {
880 if strings.HasSuffix(f, "Android.bp") {
881 toParse = append(toParse, f)
882 }
883 fs[f] = []byte(content)
884 }
885 config := android.TestConfig(buildDir, nil, testCase.bp, fs)
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500886 ctx := android.NewTestContext(config)
887 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Liz Kammer356f7d42021-01-26 09:18:53 -0500888 for _, m := range testCase.depsMutators {
889 ctx.DepsBp2BuildMutators(m)
890 }
Jingwen Chena42d6412021-01-26 21:57:27 -0500891 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500892 ctx.RegisterForBazelConversion()
893
Liz Kammer356f7d42021-01-26 09:18:53 -0500894 _, errs := ctx.ParseFileList(dir, toParse)
895 if Errored(t, testCase.description, errs) {
896 continue
897 }
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500898 _, errs = ctx.ResolveDependencies(config)
Liz Kammer356f7d42021-01-26 09:18:53 -0500899 if Errored(t, testCase.description, errs) {
900 continue
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500901 }
902
Liz Kammer356f7d42021-01-26 09:18:53 -0500903 checkDir := dir
904 if testCase.dir != "" {
905 checkDir = testCase.dir
906 }
907 bazelTargets := GenerateBazelTargets(ctx.Context.Context, Bp2Build)[checkDir]
908 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
909 t.Errorf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount)
910 } else {
911 for i, target := range bazelTargets {
912 if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
913 t.Errorf(
914 "%s: Expected generated Bazel target to be '%s', got '%s'",
915 testCase.description,
916 w,
917 g,
918 )
919 }
920 }
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500921 }
922 }
923}
Jingwen Chen041b1842021-02-01 00:23:25 -0500924
Liz Kammer356f7d42021-01-26 09:18:53 -0500925func Errored(t *testing.T, desc string, errs []error) bool {
926 t.Helper()
927 if len(errs) > 0 {
928 for _, err := range errs {
929 t.Errorf("%s: %s", desc, err)
930 }
931 return true
932 }
933 return false
934}
935
Jingwen Chen041b1842021-02-01 00:23:25 -0500936type bp2buildMutator = func(android.TopDownMutatorContext)
937
938func TestBp2BuildInlinesDefaults(t *testing.T) {
939 testCases := []struct {
940 moduleTypesUnderTest map[string]android.ModuleFactory
941 bp2buildMutatorsUnderTest map[string]bp2buildMutator
942 bp string
943 expectedBazelTarget string
944 description string
945 }{
946 {
947 moduleTypesUnderTest: map[string]android.ModuleFactory{
948 "genrule": genrule.GenRuleFactory,
949 "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
950 },
951 bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
952 "genrule": genrule.GenruleBp2Build,
953 },
954 bp: `genrule_defaults {
955 name: "gen_defaults",
956 cmd: "do-something $(in) $(out)",
957}
958genrule {
959 name: "gen",
960 out: ["out"],
961 srcs: ["in1"],
962 defaults: ["gen_defaults"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500963 bazel_module: { bp2build_available: true },
Jingwen Chen041b1842021-02-01 00:23:25 -0500964}
965`,
966 expectedBazelTarget: `genrule(
967 name = "gen",
968 cmd = "do-something $(SRCS) $(OUTS)",
969 outs = [
970 "out",
971 ],
972 srcs = [
973 "in1",
974 ],
975)`,
976 description: "genrule applies properties from a genrule_defaults dependency if not specified",
977 },
978 {
979 moduleTypesUnderTest: map[string]android.ModuleFactory{
980 "genrule": genrule.GenRuleFactory,
981 "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
982 },
983 bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
984 "genrule": genrule.GenruleBp2Build,
985 },
986 bp: `genrule_defaults {
987 name: "gen_defaults",
988 out: ["out-from-defaults"],
989 srcs: ["in-from-defaults"],
990 cmd: "cmd-from-defaults",
991}
992genrule {
993 name: "gen",
994 out: ["out"],
995 srcs: ["in1"],
996 defaults: ["gen_defaults"],
997 cmd: "do-something $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500998 bazel_module: { bp2build_available: true },
Jingwen Chen041b1842021-02-01 00:23:25 -0500999}
1000`,
1001 expectedBazelTarget: `genrule(
1002 name = "gen",
1003 cmd = "do-something $(SRCS) $(OUTS)",
1004 outs = [
1005 "out-from-defaults",
1006 "out",
1007 ],
1008 srcs = [
1009 "in-from-defaults",
1010 "in1",
1011 ],
1012)`,
1013 description: "genrule does merges properties from a genrule_defaults dependency, latest-first",
1014 },
1015 {
1016 moduleTypesUnderTest: map[string]android.ModuleFactory{
1017 "genrule": genrule.GenRuleFactory,
1018 "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
1019 },
1020 bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
1021 "genrule": genrule.GenruleBp2Build,
1022 },
1023 bp: `genrule_defaults {
1024 name: "gen_defaults1",
1025 cmd: "cp $(in) $(out)",
1026}
1027
1028genrule_defaults {
1029 name: "gen_defaults2",
1030 srcs: ["in1"],
1031}
1032
1033genrule {
1034 name: "gen",
1035 out: ["out"],
1036 defaults: ["gen_defaults1", "gen_defaults2"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001037 bazel_module: { bp2build_available: true },
Jingwen Chen041b1842021-02-01 00:23:25 -05001038}
1039`,
1040 expectedBazelTarget: `genrule(
1041 name = "gen",
1042 cmd = "cp $(SRCS) $(OUTS)",
1043 outs = [
1044 "out",
1045 ],
1046 srcs = [
1047 "in1",
1048 ],
1049)`,
1050 description: "genrule applies properties from list of genrule_defaults",
1051 },
1052 {
1053 moduleTypesUnderTest: map[string]android.ModuleFactory{
1054 "genrule": genrule.GenRuleFactory,
1055 "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
1056 },
1057 bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
1058 "genrule": genrule.GenruleBp2Build,
1059 },
1060 bp: `genrule_defaults {
1061 name: "gen_defaults1",
1062 defaults: ["gen_defaults2"],
1063 cmd: "cmd1 $(in) $(out)", // overrides gen_defaults2's cmd property value.
1064}
1065
1066genrule_defaults {
1067 name: "gen_defaults2",
1068 defaults: ["gen_defaults3"],
1069 cmd: "cmd2 $(in) $(out)",
1070 out: ["out-from-2"],
1071 srcs: ["in1"],
1072}
1073
1074genrule_defaults {
1075 name: "gen_defaults3",
1076 out: ["out-from-3"],
1077 srcs: ["srcs-from-3"],
1078}
1079
1080genrule {
1081 name: "gen",
1082 out: ["out"],
1083 defaults: ["gen_defaults1"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001084 bazel_module: { bp2build_available: true },
Jingwen Chen041b1842021-02-01 00:23:25 -05001085}
1086`,
1087 expectedBazelTarget: `genrule(
1088 name = "gen",
1089 cmd = "cmd1 $(SRCS) $(OUTS)",
1090 outs = [
1091 "out-from-3",
1092 "out-from-2",
1093 "out",
1094 ],
1095 srcs = [
1096 "srcs-from-3",
1097 "in1",
1098 ],
1099)`,
1100 description: "genrule applies properties from genrule_defaults transitively",
1101 },
1102 }
1103
1104 dir := "."
1105 for _, testCase := range testCases {
1106 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
1107 ctx := android.NewTestContext(config)
1108 for m, factory := range testCase.moduleTypesUnderTest {
1109 ctx.RegisterModuleType(m, factory)
1110 }
1111 for mutator, f := range testCase.bp2buildMutatorsUnderTest {
1112 ctx.RegisterBp2BuildMutator(mutator, f)
1113 }
1114 ctx.RegisterForBazelConversion()
1115
1116 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
1117 android.FailIfErrored(t, errs)
1118 _, errs = ctx.ResolveDependencies(config)
1119 android.FailIfErrored(t, errs)
1120
Jingwen Chen4d2c0872021-02-02 07:06:56 -05001121 bazelTargets := GenerateBazelTargets(ctx.Context.Context, Bp2Build)[dir]
Jingwen Chen041b1842021-02-01 00:23:25 -05001122 if actualCount := len(bazelTargets); actualCount != 1 {
1123 t.Fatalf("%s: Expected 1 bazel target, got %d", testCase.description, actualCount)
1124 }
1125
1126 actualBazelTarget := bazelTargets[0]
1127 if actualBazelTarget.content != testCase.expectedBazelTarget {
1128 t.Errorf(
1129 "%s: Expected generated Bazel target to be '%s', got '%s'",
1130 testCase.description,
1131 testCase.expectedBazelTarget,
1132 actualBazelTarget.content,
1133 )
1134 }
1135 }
1136}
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001137
1138func TestAllowlistingBp2buildTargets(t *testing.T) {
1139 testCases := []struct {
1140 moduleTypeUnderTest string
1141 moduleTypeUnderTestFactory android.ModuleFactory
1142 moduleTypeUnderTestBp2BuildMutator bp2buildMutator
1143 bp string
1144 expectedCount int
1145 description string
1146 }{
1147 {
1148 description: "explicitly unavailable",
1149 moduleTypeUnderTest: "filegroup",
1150 moduleTypeUnderTestFactory: android.FileGroupFactory,
1151 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1152 bp: `filegroup {
1153 name: "foo",
1154 srcs: ["a", "b"],
1155 bazel_module: { bp2build_available: false },
1156}`,
1157 expectedCount: 0,
1158 },
1159 {
1160 description: "implicitly unavailable",
1161 moduleTypeUnderTest: "filegroup",
1162 moduleTypeUnderTestFactory: android.FileGroupFactory,
1163 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1164 bp: `filegroup {
1165 name: "foo",
1166 srcs: ["a", "b"],
1167}`,
1168 expectedCount: 0,
1169 },
1170 {
1171 description: "explicitly available",
1172 moduleTypeUnderTest: "filegroup",
1173 moduleTypeUnderTestFactory: android.FileGroupFactory,
1174 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1175 bp: `filegroup {
1176 name: "foo",
1177 srcs: ["a", "b"],
1178 bazel_module: { bp2build_available: true },
1179}`,
1180 expectedCount: 1,
1181 },
1182 {
1183 description: "generates more than 1 target if needed",
1184 moduleTypeUnderTest: "custom",
1185 moduleTypeUnderTestFactory: customModuleFactory,
1186 moduleTypeUnderTestBp2BuildMutator: customBp2BuildMutatorFromStarlark,
1187 bp: `custom {
1188 name: "foo",
1189 bazel_module: { bp2build_available: true },
1190}`,
1191 expectedCount: 3,
1192 },
1193 }
1194
1195 dir := "."
1196 for _, testCase := range testCases {
1197 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
1198 ctx := android.NewTestContext(config)
1199 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
1200 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1201 ctx.RegisterForBazelConversion()
1202
1203 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
1204 android.FailIfErrored(t, errs)
1205 _, errs = ctx.ResolveDependencies(config)
1206 android.FailIfErrored(t, errs)
1207
1208 bazelTargets := GenerateBazelTargets(ctx.Context.Context, Bp2Build)[dir]
1209 if actualCount := len(bazelTargets); actualCount != testCase.expectedCount {
1210 t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, testCase.expectedCount, actualCount)
1211 }
1212 }
1213}