blob: df554a09a512651b30cb893cda4c38c65952e995 [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"
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +000020 "android/soong/sh"
Liz Kammer356f7d42021-01-26 09:18:53 -050021 "strings"
Liz Kammer2dd9ca42020-11-25 16:06:39 -080022 "testing"
23)
24
25func TestGenerateSoongModuleTargets(t *testing.T) {
26 testCases := []struct {
27 bp string
28 expectedBazelTarget string
29 }{
30 {
31 bp: `custom {
32 name: "foo",
33}
34 `,
35 expectedBazelTarget: `soong_module(
36 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050037 soong_module_name = "foo",
38 soong_module_type = "custom",
39 soong_module_variant = "",
40 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080041 ],
42)`,
43 },
44 {
45 bp: `custom {
46 name: "foo",
47 ramdisk: true,
48}
49 `,
50 expectedBazelTarget: `soong_module(
51 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050052 soong_module_name = "foo",
53 soong_module_type = "custom",
54 soong_module_variant = "",
55 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080056 ],
57 ramdisk = True,
58)`,
59 },
60 {
61 bp: `custom {
62 name: "foo",
63 owner: "a_string_with\"quotes\"_and_\\backslashes\\\\",
64}
65 `,
66 expectedBazelTarget: `soong_module(
67 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050068 soong_module_name = "foo",
69 soong_module_type = "custom",
70 soong_module_variant = "",
71 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080072 ],
73 owner = "a_string_with\"quotes\"_and_\\backslashes\\\\",
74)`,
75 },
76 {
77 bp: `custom {
78 name: "foo",
79 required: ["bar"],
80}
81 `,
82 expectedBazelTarget: `soong_module(
83 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050084 soong_module_name = "foo",
85 soong_module_type = "custom",
86 soong_module_variant = "",
87 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080088 ],
89 required = [
90 "bar",
91 ],
92)`,
93 },
94 {
95 bp: `custom {
96 name: "foo",
97 target_required: ["qux", "bazqux"],
98}
99 `,
100 expectedBazelTarget: `soong_module(
101 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500102 soong_module_name = "foo",
103 soong_module_type = "custom",
104 soong_module_variant = "",
105 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800106 ],
107 target_required = [
108 "qux",
109 "bazqux",
110 ],
111)`,
112 },
113 {
114 bp: `custom {
115 name: "foo",
116 dist: {
117 targets: ["goal_foo"],
118 tag: ".foo",
119 },
120 dists: [
121 {
122 targets: ["goal_bar"],
123 tag: ".bar",
124 },
125 ],
126}
127 `,
128 expectedBazelTarget: `soong_module(
129 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500130 soong_module_name = "foo",
131 soong_module_type = "custom",
132 soong_module_variant = "",
133 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800134 ],
135 dist = {
136 "tag": ".foo",
137 "targets": [
138 "goal_foo",
139 ],
140 },
141 dists = [
142 {
143 "tag": ".bar",
144 "targets": [
145 "goal_bar",
146 ],
147 },
148 ],
149)`,
150 },
151 {
152 bp: `custom {
153 name: "foo",
154 required: ["bar"],
155 target_required: ["qux", "bazqux"],
156 ramdisk: true,
157 owner: "custom_owner",
158 dists: [
159 {
160 tag: ".tag",
161 targets: ["my_goal"],
162 },
163 ],
164}
165 `,
166 expectedBazelTarget: `soong_module(
167 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500168 soong_module_name = "foo",
169 soong_module_type = "custom",
170 soong_module_variant = "",
171 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800172 ],
173 dists = [
174 {
175 "tag": ".tag",
176 "targets": [
177 "my_goal",
178 ],
179 },
180 ],
181 owner = "custom_owner",
182 ramdisk = True,
183 required = [
184 "bar",
185 ],
186 target_required = [
187 "qux",
188 "bazqux",
189 ],
190)`,
191 },
192 }
193
194 dir := "."
195 for _, testCase := range testCases {
196 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
197 ctx := android.NewTestContext(config)
198 ctx.RegisterModuleType("custom", customModuleFactory)
199 ctx.Register()
200
201 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
202 android.FailIfErrored(t, errs)
203 _, errs = ctx.PrepareBuildActions(config)
204 android.FailIfErrored(t, errs)
205
Jingwen Chen4d2c0872021-02-02 07:06:56 -0500206 bazelTargets := GenerateBazelTargets(ctx.Context.Context, QueryView)[dir]
Jingwen Chen4e4756d2021-01-24 21:13:13 -0500207 if actualCount, expectedCount := len(bazelTargets), 1; actualCount != expectedCount {
208 t.Fatalf("Expected %d bazel target, got %d", expectedCount, actualCount)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800209 }
210
211 actualBazelTarget := bazelTargets[0]
212 if actualBazelTarget.content != testCase.expectedBazelTarget {
213 t.Errorf(
214 "Expected generated Bazel target to be '%s', got '%s'",
215 testCase.expectedBazelTarget,
Jingwen Chen73850672020-12-14 08:25:34 -0500216 actualBazelTarget.content,
217 )
218 }
219 }
220}
221
222func TestGenerateBazelTargetModules(t *testing.T) {
223 testCases := []struct {
224 bp string
225 expectedBazelTarget string
226 }{
227 {
228 bp: `custom {
229 name: "foo",
230 string_list_prop: ["a", "b"],
231 string_prop: "a",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500232 bazel_module: { bp2build_available: true },
Jingwen Chen73850672020-12-14 08:25:34 -0500233}`,
234 expectedBazelTarget: `custom(
235 name = "foo",
236 string_list_prop = [
237 "a",
238 "b",
239 ],
240 string_prop = "a",
241)`,
242 },
243 }
244
245 dir := "."
246 for _, testCase := range testCases {
247 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
248 ctx := android.NewTestContext(config)
249 ctx.RegisterModuleType("custom", customModuleFactory)
250 ctx.RegisterBp2BuildMutator("custom", customBp2BuildMutator)
251 ctx.RegisterForBazelConversion()
252
253 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
Liz Kammer356f7d42021-01-26 09:18:53 -0500254 if Errored(t, "", errs) {
255 continue
256 }
Jingwen Chen73850672020-12-14 08:25:34 -0500257 _, errs = ctx.ResolveDependencies(config)
Liz Kammer356f7d42021-01-26 09:18:53 -0500258 if Errored(t, "", errs) {
259 continue
260 }
Jingwen Chen73850672020-12-14 08:25:34 -0500261
Jingwen Chen4d2c0872021-02-02 07:06:56 -0500262 bazelTargets := GenerateBazelTargets(ctx.Context.Context, Bp2Build)[dir]
Jingwen Chen4e4756d2021-01-24 21:13:13 -0500263 if actualCount, expectedCount := len(bazelTargets), 1; actualCount != expectedCount {
Liz Kammer356f7d42021-01-26 09:18:53 -0500264 t.Errorf("Expected %d bazel target, got %d", expectedCount, actualCount)
265 } else {
266 actualBazelTarget := bazelTargets[0]
267 if actualBazelTarget.content != testCase.expectedBazelTarget {
268 t.Errorf(
269 "Expected generated Bazel target to be '%s', got '%s'",
270 testCase.expectedBazelTarget,
271 actualBazelTarget.content,
272 )
273 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800274 }
275 }
276}
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500277
Jingwen Chen40067de2021-01-26 21:58:43 -0500278func TestLoadStatements(t *testing.T) {
279 testCases := []struct {
280 bazelTargets BazelTargets
281 expectedLoadStatements string
282 }{
283 {
284 bazelTargets: BazelTargets{
285 BazelTarget{
286 name: "foo",
287 ruleClass: "cc_library",
288 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
289 },
290 },
291 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
292 },
293 {
294 bazelTargets: BazelTargets{
295 BazelTarget{
296 name: "foo",
297 ruleClass: "cc_library",
298 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
299 },
300 BazelTarget{
301 name: "bar",
302 ruleClass: "cc_library",
303 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
304 },
305 },
306 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
307 },
308 {
309 bazelTargets: BazelTargets{
310 BazelTarget{
311 name: "foo",
312 ruleClass: "cc_library",
313 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
314 },
315 BazelTarget{
316 name: "bar",
317 ruleClass: "cc_binary",
318 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
319 },
320 },
321 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")`,
322 },
323 {
324 bazelTargets: BazelTargets{
325 BazelTarget{
326 name: "foo",
327 ruleClass: "cc_library",
328 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
329 },
330 BazelTarget{
331 name: "bar",
332 ruleClass: "cc_binary",
333 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
334 },
335 BazelTarget{
336 name: "baz",
337 ruleClass: "java_binary",
338 bzlLoadLocation: "//build/bazel/rules:java.bzl",
339 },
340 },
341 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")
342load("//build/bazel/rules:java.bzl", "java_binary")`,
343 },
344 {
345 bazelTargets: BazelTargets{
346 BazelTarget{
347 name: "foo",
348 ruleClass: "cc_binary",
349 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
350 },
351 BazelTarget{
352 name: "bar",
353 ruleClass: "java_binary",
354 bzlLoadLocation: "//build/bazel/rules:java.bzl",
355 },
356 BazelTarget{
357 name: "baz",
358 ruleClass: "genrule",
359 // Note: no bzlLoadLocation for native rules
360 },
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +0000361 BazelTarget{
362 name: "sh_binary_target",
363 ruleClass: "sh_binary",
364 // Note: no bzlLoadLocation for native rules
365 // TODO(ruperts): Could open source the existing, experimental Starlark sh_ rules?
366 },
Jingwen Chen40067de2021-01-26 21:58:43 -0500367 },
368 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary")
369load("//build/bazel/rules:java.bzl", "java_binary")`,
370 },
371 }
372
373 for _, testCase := range testCases {
374 actual := testCase.bazelTargets.LoadStatements()
375 expected := testCase.expectedLoadStatements
376 if actual != expected {
377 t.Fatalf("Expected load statements to be %s, got %s", expected, actual)
378 }
379 }
380
381}
382
383func TestGenerateBazelTargetModules_OneToMany_LoadedFromStarlark(t *testing.T) {
384 testCases := []struct {
385 bp string
386 expectedBazelTarget string
387 expectedBazelTargetCount int
388 expectedLoadStatements string
389 }{
390 {
391 bp: `custom {
392 name: "bar",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500393 bazel_module: { bp2build_available: true },
Jingwen Chen40067de2021-01-26 21:58:43 -0500394}`,
395 expectedBazelTarget: `my_library(
396 name = "bar",
397)
398
399my_proto_library(
400 name = "bar_my_proto_library_deps",
401)
402
403proto_library(
404 name = "bar_proto_library_deps",
405)`,
406 expectedBazelTargetCount: 3,
407 expectedLoadStatements: `load("//build/bazel/rules:proto.bzl", "my_proto_library", "proto_library")
408load("//build/bazel/rules:rules.bzl", "my_library")`,
409 },
410 }
411
412 dir := "."
413 for _, testCase := range testCases {
414 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
415 ctx := android.NewTestContext(config)
416 ctx.RegisterModuleType("custom", customModuleFactory)
417 ctx.RegisterBp2BuildMutator("custom_starlark", customBp2BuildMutatorFromStarlark)
418 ctx.RegisterForBazelConversion()
419
420 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
421 android.FailIfErrored(t, errs)
422 _, errs = ctx.ResolveDependencies(config)
423 android.FailIfErrored(t, errs)
424
Jingwen Chen4d2c0872021-02-02 07:06:56 -0500425 bazelTargets := GenerateBazelTargets(ctx.Context.Context, Bp2Build)[dir]
Jingwen Chen40067de2021-01-26 21:58:43 -0500426 if actualCount := len(bazelTargets); actualCount != testCase.expectedBazelTargetCount {
427 t.Fatalf("Expected %d bazel target, got %d", testCase.expectedBazelTargetCount, actualCount)
428 }
429
430 actualBazelTargets := bazelTargets.String()
431 if actualBazelTargets != testCase.expectedBazelTarget {
432 t.Errorf(
433 "Expected generated Bazel target to be '%s', got '%s'",
434 testCase.expectedBazelTarget,
435 actualBazelTargets,
436 )
437 }
438
439 actualLoadStatements := bazelTargets.LoadStatements()
440 if actualLoadStatements != testCase.expectedLoadStatements {
441 t.Errorf(
442 "Expected generated load statements to be '%s', got '%s'",
443 testCase.expectedLoadStatements,
444 actualLoadStatements,
445 )
446 }
447 }
448}
449
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500450func TestModuleTypeBp2Build(t *testing.T) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500451 otherGenruleBp := map[string]string{
452 "other/Android.bp": `genrule {
453 name: "foo.tool",
454 out: ["foo_tool.out"],
455 srcs: ["foo_tool.in"],
456 cmd: "cp $(in) $(out)",
457}
458genrule {
459 name: "other.tool",
460 out: ["other_tool.out"],
461 srcs: ["other_tool.in"],
462 cmd: "cp $(in) $(out)",
463}`,
464 }
465
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500466 testCases := []struct {
Liz Kammer356f7d42021-01-26 09:18:53 -0500467 description string
Jingwen Chena42d6412021-01-26 21:57:27 -0500468 moduleTypeUnderTest string
469 moduleTypeUnderTestFactory android.ModuleFactory
470 moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
Liz Kammer356f7d42021-01-26 09:18:53 -0500471 preArchMutators []android.RegisterMutatorFunc
472 depsMutators []android.RegisterMutatorFunc
Jingwen Chena42d6412021-01-26 21:57:27 -0500473 bp string
Liz Kammer356f7d42021-01-26 09:18:53 -0500474 expectedBazelTargets []string
475 fs map[string]string
476 dir string
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500477 }{
478 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500479 description: "filegroup with no srcs",
480 moduleTypeUnderTest: "filegroup",
481 moduleTypeUnderTestFactory: android.FileGroupFactory,
482 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500483 bp: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500484 name: "fg_foo",
485 srcs: [],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500486 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500487}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500488 expectedBazelTargets: []string{
489 `filegroup(
490 name = "fg_foo",
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500491 srcs = [
492 ],
493)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500494 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500495 },
496 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500497 description: "filegroup with srcs",
498 moduleTypeUnderTest: "filegroup",
499 moduleTypeUnderTestFactory: android.FileGroupFactory,
500 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500501 bp: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500502 name: "fg_foo",
503 srcs: ["a", "b"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500504 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500505}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500506 expectedBazelTargets: []string{`filegroup(
507 name = "fg_foo",
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500508 srcs = [
509 "a",
510 "b",
511 ],
512)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500513 },
514 },
515 {
516 description: "filegroup with excludes srcs",
517 moduleTypeUnderTest: "filegroup",
518 moduleTypeUnderTestFactory: android.FileGroupFactory,
519 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
520 bp: `filegroup {
521 name: "fg_foo",
522 srcs: ["a", "b"],
523 exclude_srcs: ["a"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500524 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500525}`,
526 expectedBazelTargets: []string{`filegroup(
527 name = "fg_foo",
528 srcs = [
529 "b",
530 ],
531)`,
532 },
533 },
534 {
535 description: "filegroup with glob",
536 moduleTypeUnderTest: "filegroup",
537 moduleTypeUnderTestFactory: android.FileGroupFactory,
538 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
539 bp: `filegroup {
540 name: "foo",
541 srcs: ["**/*.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500542 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500543}`,
544 expectedBazelTargets: []string{`filegroup(
545 name = "foo",
546 srcs = [
547 "other/a.txt",
548 "other/b.txt",
549 "other/subdir/a.txt",
550 ],
551)`,
552 },
553 fs: map[string]string{
554 "other/a.txt": "",
555 "other/b.txt": "",
556 "other/subdir/a.txt": "",
557 "other/file": "",
558 },
559 },
560 {
561 description: "filegroup with glob in subdir",
562 moduleTypeUnderTest: "filegroup",
563 moduleTypeUnderTestFactory: android.FileGroupFactory,
564 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
565 bp: `filegroup {
566 name: "foo",
567 srcs: ["a.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500568 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500569}`,
570 dir: "other",
571 expectedBazelTargets: []string{`filegroup(
572 name = "fg_foo",
573 srcs = [
574 "a.txt",
575 "b.txt",
576 "subdir/a.txt",
577 ],
578)`,
579 },
580 fs: map[string]string{
581 "other/Android.bp": `filegroup {
582 name: "fg_foo",
583 srcs: ["**/*.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500584 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500585}`,
586 "other/a.txt": "",
587 "other/b.txt": "",
588 "other/subdir/a.txt": "",
589 "other/file": "",
590 },
591 },
592 {
593 description: "depends_on_other_dir_module",
594 moduleTypeUnderTest: "filegroup",
595 moduleTypeUnderTestFactory: android.FileGroupFactory,
596 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
597 bp: `filegroup {
598 name: "foobar",
599 srcs: [
600 ":foo",
601 "c",
602 ],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500603 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500604}`,
605 expectedBazelTargets: []string{`filegroup(
606 name = "foobar",
607 srcs = [
608 "//other:foo",
609 "c",
610 ],
611)`,
612 },
613 fs: map[string]string{
614 "other/Android.bp": `filegroup {
615 name: "foo",
616 srcs: ["a", "b"],
617}`,
618 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500619 },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500620 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500621 description: "genrule with command line variable replacements",
622 moduleTypeUnderTest: "genrule",
623 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
624 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Liz Kammer356f7d42021-01-26 09:18:53 -0500625 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
Jingwen Chen316e07c2020-12-14 09:09:52 -0500626 bp: `genrule {
Liz Kammer356f7d42021-01-26 09:18:53 -0500627 name: "foo.tool",
628 out: ["foo_tool.out"],
629 srcs: ["foo_tool.in"],
630 cmd: "cp $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500631 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500632}
633
634genrule {
Jingwen Chen316e07c2020-12-14 09:09:52 -0500635 name: "foo",
636 out: ["foo.out"],
637 srcs: ["foo.in"],
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500638 tools: [":foo.tool"],
639 cmd: "$(location :foo.tool) --genDir=$(genDir) arg $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500640 bazel_module: { bp2build_available: true },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500641}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500642 expectedBazelTargets: []string{
643 `genrule(
Jingwen Chen316e07c2020-12-14 09:09:52 -0500644 name = "foo",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500645 cmd = "$(location :foo.tool) --genDir=$(GENDIR) arg $(SRCS) $(OUTS)",
Jingwen Chen316e07c2020-12-14 09:09:52 -0500646 outs = [
647 "foo.out",
648 ],
649 srcs = [
650 "foo.in",
651 ],
652 tools = [
653 ":foo.tool",
654 ],
655)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500656 `genrule(
657 name = "foo.tool",
658 cmd = "cp $(SRCS) $(OUTS)",
659 outs = [
660 "foo_tool.out",
661 ],
662 srcs = [
663 "foo_tool.in",
664 ],
665)`,
666 },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500667 },
668 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500669 description: "genrule using $(locations :label)",
670 moduleTypeUnderTest: "genrule",
671 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
672 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Liz Kammer356f7d42021-01-26 09:18:53 -0500673 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
Jingwen Chen316e07c2020-12-14 09:09:52 -0500674 bp: `genrule {
Liz Kammer356f7d42021-01-26 09:18:53 -0500675 name: "foo.tools",
676 out: ["foo_tool.out", "foo_tool2.out"],
677 srcs: ["foo_tool.in"],
678 cmd: "cp $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500679 bazel_module: { bp2build_available: true },
680}
Liz Kammer356f7d42021-01-26 09:18:53 -0500681
682genrule {
Jingwen Chen316e07c2020-12-14 09:09:52 -0500683 name: "foo",
684 out: ["foo.out"],
685 srcs: ["foo.in"],
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500686 tools: [":foo.tools"],
687 cmd: "$(locations :foo.tools) -s $(out) $(in)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500688 bazel_module: { bp2build_available: true },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500689}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500690 expectedBazelTargets: []string{`genrule(
Jingwen Chen316e07c2020-12-14 09:09:52 -0500691 name = "foo",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500692 cmd = "$(locations :foo.tools) -s $(OUTS) $(SRCS)",
693 outs = [
694 "foo.out",
695 ],
696 srcs = [
697 "foo.in",
698 ],
699 tools = [
700 ":foo.tools",
701 ],
702)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500703 `genrule(
704 name = "foo.tools",
705 cmd = "cp $(SRCS) $(OUTS)",
706 outs = [
707 "foo_tool.out",
708 "foo_tool2.out",
709 ],
710 srcs = [
711 "foo_tool.in",
712 ],
713)`,
714 },
715 },
716 {
717 description: "genrule using $(locations //absolute:label)",
718 moduleTypeUnderTest: "genrule",
719 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
720 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
721 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
722 bp: `genrule {
723 name: "foo",
724 out: ["foo.out"],
725 srcs: ["foo.in"],
726 tool_files: [":foo.tool"],
727 cmd: "$(locations :foo.tool) -s $(out) $(in)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500728 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500729}`,
730 expectedBazelTargets: []string{`genrule(
731 name = "foo",
732 cmd = "$(locations //other:foo.tool) -s $(OUTS) $(SRCS)",
733 outs = [
734 "foo.out",
735 ],
736 srcs = [
737 "foo.in",
738 ],
739 tools = [
740 "//other:foo.tool",
741 ],
742)`,
743 },
744 fs: otherGenruleBp,
745 },
746 {
747 description: "genrule srcs using $(locations //absolute:label)",
748 moduleTypeUnderTest: "genrule",
749 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
750 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
751 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
752 bp: `genrule {
753 name: "foo",
754 out: ["foo.out"],
755 srcs: [":other.tool"],
756 tool_files: [":foo.tool"],
757 cmd: "$(locations :foo.tool) -s $(out) $(location :other.tool)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500758 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500759}`,
760 expectedBazelTargets: []string{`genrule(
761 name = "foo",
762 cmd = "$(locations //other:foo.tool) -s $(OUTS) $(location //other:other.tool)",
763 outs = [
764 "foo.out",
765 ],
766 srcs = [
767 "//other:other.tool",
768 ],
769 tools = [
770 "//other:foo.tool",
771 ],
772)`,
773 },
774 fs: otherGenruleBp,
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500775 },
776 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500777 description: "genrule using $(location) label should substitute first tool label automatically",
778 moduleTypeUnderTest: "genrule",
779 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
780 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Liz Kammer356f7d42021-01-26 09:18:53 -0500781 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500782 bp: `genrule {
783 name: "foo",
784 out: ["foo.out"],
785 srcs: ["foo.in"],
786 tool_files: [":foo.tool", ":other.tool"],
787 cmd: "$(location) -s $(out) $(in)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500788 bazel_module: { bp2build_available: true },
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500789}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500790 expectedBazelTargets: []string{`genrule(
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500791 name = "foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500792 cmd = "$(location //other:foo.tool) -s $(OUTS) $(SRCS)",
Jingwen Chen316e07c2020-12-14 09:09:52 -0500793 outs = [
794 "foo.out",
795 ],
796 srcs = [
797 "foo.in",
798 ],
799 tools = [
Liz Kammer356f7d42021-01-26 09:18:53 -0500800 "//other:foo.tool",
801 "//other:other.tool",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500802 ],
803)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500804 },
805 fs: otherGenruleBp,
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500806 },
807 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500808 description: "genrule using $(locations) label should substitute first tool label automatically",
809 moduleTypeUnderTest: "genrule",
810 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
811 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Liz Kammer356f7d42021-01-26 09:18:53 -0500812 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500813 bp: `genrule {
814 name: "foo",
815 out: ["foo.out"],
816 srcs: ["foo.in"],
817 tools: [":foo.tool", ":other.tool"],
818 cmd: "$(locations) -s $(out) $(in)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500819 bazel_module: { bp2build_available: true },
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500820}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500821 expectedBazelTargets: []string{`genrule(
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500822 name = "foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500823 cmd = "$(locations //other:foo.tool) -s $(OUTS) $(SRCS)",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500824 outs = [
825 "foo.out",
826 ],
827 srcs = [
828 "foo.in",
829 ],
830 tools = [
Liz Kammer356f7d42021-01-26 09:18:53 -0500831 "//other:foo.tool",
832 "//other:other.tool",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500833 ],
834)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500835 },
836 fs: otherGenruleBp,
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500837 },
838 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500839 description: "genrule without tools or tool_files can convert successfully",
840 moduleTypeUnderTest: "genrule",
841 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
842 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Liz Kammer356f7d42021-01-26 09:18:53 -0500843 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500844 bp: `genrule {
845 name: "foo",
846 out: ["foo.out"],
847 srcs: ["foo.in"],
848 cmd: "cp $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500849 bazel_module: { bp2build_available: true },
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500850}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500851 expectedBazelTargets: []string{`genrule(
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500852 name = "foo",
853 cmd = "cp $(SRCS) $(OUTS)",
854 outs = [
855 "foo.out",
856 ],
857 srcs = [
858 "foo.in",
Jingwen Chen316e07c2020-12-14 09:09:52 -0500859 ],
860)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500861 },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500862 },
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +0000863 {
864 description: "sh_binary test",
865 moduleTypeUnderTest: "sh_binary",
866 moduleTypeUnderTestFactory: sh.ShBinaryFactory,
867 moduleTypeUnderTestBp2BuildMutator: sh.ShBinaryBp2Build,
868 bp: `sh_binary {
869 name: "foo",
870 src: "foo.sh",
871 bazel_module: { bp2build_available: true },
872}`,
873 expectedBazelTargets: []string{`sh_binary(
874 name = "foo",
875 srcs = [
876 "foo.sh",
877 ],
878)`},
879 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500880 }
881
882 dir := "."
883 for _, testCase := range testCases {
Liz Kammer356f7d42021-01-26 09:18:53 -0500884 fs := make(map[string][]byte)
885 toParse := []string{
886 "Android.bp",
887 }
888 for f, content := range testCase.fs {
889 if strings.HasSuffix(f, "Android.bp") {
890 toParse = append(toParse, f)
891 }
892 fs[f] = []byte(content)
893 }
894 config := android.TestConfig(buildDir, nil, testCase.bp, fs)
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500895 ctx := android.NewTestContext(config)
896 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Liz Kammer356f7d42021-01-26 09:18:53 -0500897 for _, m := range testCase.depsMutators {
898 ctx.DepsBp2BuildMutators(m)
899 }
Jingwen Chena42d6412021-01-26 21:57:27 -0500900 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500901 ctx.RegisterForBazelConversion()
902
Liz Kammer356f7d42021-01-26 09:18:53 -0500903 _, errs := ctx.ParseFileList(dir, toParse)
904 if Errored(t, testCase.description, errs) {
905 continue
906 }
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500907 _, errs = ctx.ResolveDependencies(config)
Liz Kammer356f7d42021-01-26 09:18:53 -0500908 if Errored(t, testCase.description, errs) {
909 continue
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500910 }
911
Liz Kammer356f7d42021-01-26 09:18:53 -0500912 checkDir := dir
913 if testCase.dir != "" {
914 checkDir = testCase.dir
915 }
916 bazelTargets := GenerateBazelTargets(ctx.Context.Context, Bp2Build)[checkDir]
917 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
918 t.Errorf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount)
919 } else {
920 for i, target := range bazelTargets {
921 if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
922 t.Errorf(
923 "%s: Expected generated Bazel target to be '%s', got '%s'",
924 testCase.description,
925 w,
926 g,
927 )
928 }
929 }
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500930 }
931 }
932}
Jingwen Chen041b1842021-02-01 00:23:25 -0500933
Liz Kammer356f7d42021-01-26 09:18:53 -0500934func Errored(t *testing.T, desc string, errs []error) bool {
935 t.Helper()
936 if len(errs) > 0 {
937 for _, err := range errs {
938 t.Errorf("%s: %s", desc, err)
939 }
940 return true
941 }
942 return false
943}
944
Jingwen Chen041b1842021-02-01 00:23:25 -0500945type bp2buildMutator = func(android.TopDownMutatorContext)
946
947func TestBp2BuildInlinesDefaults(t *testing.T) {
948 testCases := []struct {
949 moduleTypesUnderTest map[string]android.ModuleFactory
950 bp2buildMutatorsUnderTest map[string]bp2buildMutator
951 bp string
952 expectedBazelTarget string
953 description string
954 }{
955 {
956 moduleTypesUnderTest: map[string]android.ModuleFactory{
957 "genrule": genrule.GenRuleFactory,
958 "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
959 },
960 bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
961 "genrule": genrule.GenruleBp2Build,
962 },
963 bp: `genrule_defaults {
964 name: "gen_defaults",
965 cmd: "do-something $(in) $(out)",
966}
967genrule {
968 name: "gen",
969 out: ["out"],
970 srcs: ["in1"],
971 defaults: ["gen_defaults"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500972 bazel_module: { bp2build_available: true },
Jingwen Chen041b1842021-02-01 00:23:25 -0500973}
974`,
975 expectedBazelTarget: `genrule(
976 name = "gen",
977 cmd = "do-something $(SRCS) $(OUTS)",
978 outs = [
979 "out",
980 ],
981 srcs = [
982 "in1",
983 ],
984)`,
985 description: "genrule applies properties from a genrule_defaults dependency if not specified",
986 },
987 {
988 moduleTypesUnderTest: map[string]android.ModuleFactory{
989 "genrule": genrule.GenRuleFactory,
990 "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
991 },
992 bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
993 "genrule": genrule.GenruleBp2Build,
994 },
995 bp: `genrule_defaults {
996 name: "gen_defaults",
997 out: ["out-from-defaults"],
998 srcs: ["in-from-defaults"],
999 cmd: "cmd-from-defaults",
1000}
1001genrule {
1002 name: "gen",
1003 out: ["out"],
1004 srcs: ["in1"],
1005 defaults: ["gen_defaults"],
1006 cmd: "do-something $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001007 bazel_module: { bp2build_available: true },
Jingwen Chen041b1842021-02-01 00:23:25 -05001008}
1009`,
1010 expectedBazelTarget: `genrule(
1011 name = "gen",
1012 cmd = "do-something $(SRCS) $(OUTS)",
1013 outs = [
1014 "out-from-defaults",
1015 "out",
1016 ],
1017 srcs = [
1018 "in-from-defaults",
1019 "in1",
1020 ],
1021)`,
1022 description: "genrule does merges properties from a genrule_defaults dependency, latest-first",
1023 },
1024 {
1025 moduleTypesUnderTest: map[string]android.ModuleFactory{
1026 "genrule": genrule.GenRuleFactory,
1027 "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
1028 },
1029 bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
1030 "genrule": genrule.GenruleBp2Build,
1031 },
1032 bp: `genrule_defaults {
1033 name: "gen_defaults1",
1034 cmd: "cp $(in) $(out)",
1035}
1036
1037genrule_defaults {
1038 name: "gen_defaults2",
1039 srcs: ["in1"],
1040}
1041
1042genrule {
1043 name: "gen",
1044 out: ["out"],
1045 defaults: ["gen_defaults1", "gen_defaults2"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001046 bazel_module: { bp2build_available: true },
Jingwen Chen041b1842021-02-01 00:23:25 -05001047}
1048`,
1049 expectedBazelTarget: `genrule(
1050 name = "gen",
1051 cmd = "cp $(SRCS) $(OUTS)",
1052 outs = [
1053 "out",
1054 ],
1055 srcs = [
1056 "in1",
1057 ],
1058)`,
1059 description: "genrule applies properties from list of genrule_defaults",
1060 },
1061 {
1062 moduleTypesUnderTest: map[string]android.ModuleFactory{
1063 "genrule": genrule.GenRuleFactory,
1064 "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
1065 },
1066 bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
1067 "genrule": genrule.GenruleBp2Build,
1068 },
1069 bp: `genrule_defaults {
1070 name: "gen_defaults1",
1071 defaults: ["gen_defaults2"],
1072 cmd: "cmd1 $(in) $(out)", // overrides gen_defaults2's cmd property value.
1073}
1074
1075genrule_defaults {
1076 name: "gen_defaults2",
1077 defaults: ["gen_defaults3"],
1078 cmd: "cmd2 $(in) $(out)",
1079 out: ["out-from-2"],
1080 srcs: ["in1"],
1081}
1082
1083genrule_defaults {
1084 name: "gen_defaults3",
1085 out: ["out-from-3"],
1086 srcs: ["srcs-from-3"],
1087}
1088
1089genrule {
1090 name: "gen",
1091 out: ["out"],
1092 defaults: ["gen_defaults1"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001093 bazel_module: { bp2build_available: true },
Jingwen Chen041b1842021-02-01 00:23:25 -05001094}
1095`,
1096 expectedBazelTarget: `genrule(
1097 name = "gen",
1098 cmd = "cmd1 $(SRCS) $(OUTS)",
1099 outs = [
1100 "out-from-3",
1101 "out-from-2",
1102 "out",
1103 ],
1104 srcs = [
1105 "srcs-from-3",
1106 "in1",
1107 ],
1108)`,
1109 description: "genrule applies properties from genrule_defaults transitively",
1110 },
1111 }
1112
1113 dir := "."
1114 for _, testCase := range testCases {
1115 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
1116 ctx := android.NewTestContext(config)
1117 for m, factory := range testCase.moduleTypesUnderTest {
1118 ctx.RegisterModuleType(m, factory)
1119 }
1120 for mutator, f := range testCase.bp2buildMutatorsUnderTest {
1121 ctx.RegisterBp2BuildMutator(mutator, f)
1122 }
1123 ctx.RegisterForBazelConversion()
1124
1125 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
1126 android.FailIfErrored(t, errs)
1127 _, errs = ctx.ResolveDependencies(config)
1128 android.FailIfErrored(t, errs)
1129
Jingwen Chen4d2c0872021-02-02 07:06:56 -05001130 bazelTargets := GenerateBazelTargets(ctx.Context.Context, Bp2Build)[dir]
Jingwen Chen041b1842021-02-01 00:23:25 -05001131 if actualCount := len(bazelTargets); actualCount != 1 {
1132 t.Fatalf("%s: Expected 1 bazel target, got %d", testCase.description, actualCount)
1133 }
1134
1135 actualBazelTarget := bazelTargets[0]
1136 if actualBazelTarget.content != testCase.expectedBazelTarget {
1137 t.Errorf(
1138 "%s: Expected generated Bazel target to be '%s', got '%s'",
1139 testCase.description,
1140 testCase.expectedBazelTarget,
1141 actualBazelTarget.content,
1142 )
1143 }
1144 }
1145}
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001146
1147func TestAllowlistingBp2buildTargets(t *testing.T) {
1148 testCases := []struct {
1149 moduleTypeUnderTest string
1150 moduleTypeUnderTestFactory android.ModuleFactory
1151 moduleTypeUnderTestBp2BuildMutator bp2buildMutator
1152 bp string
1153 expectedCount int
1154 description string
1155 }{
1156 {
1157 description: "explicitly unavailable",
1158 moduleTypeUnderTest: "filegroup",
1159 moduleTypeUnderTestFactory: android.FileGroupFactory,
1160 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1161 bp: `filegroup {
1162 name: "foo",
1163 srcs: ["a", "b"],
1164 bazel_module: { bp2build_available: false },
1165}`,
1166 expectedCount: 0,
1167 },
1168 {
1169 description: "implicitly unavailable",
1170 moduleTypeUnderTest: "filegroup",
1171 moduleTypeUnderTestFactory: android.FileGroupFactory,
1172 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1173 bp: `filegroup {
1174 name: "foo",
1175 srcs: ["a", "b"],
1176}`,
1177 expectedCount: 0,
1178 },
1179 {
1180 description: "explicitly available",
1181 moduleTypeUnderTest: "filegroup",
1182 moduleTypeUnderTestFactory: android.FileGroupFactory,
1183 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1184 bp: `filegroup {
1185 name: "foo",
1186 srcs: ["a", "b"],
1187 bazel_module: { bp2build_available: true },
1188}`,
1189 expectedCount: 1,
1190 },
1191 {
1192 description: "generates more than 1 target if needed",
1193 moduleTypeUnderTest: "custom",
1194 moduleTypeUnderTestFactory: customModuleFactory,
1195 moduleTypeUnderTestBp2BuildMutator: customBp2BuildMutatorFromStarlark,
1196 bp: `custom {
1197 name: "foo",
1198 bazel_module: { bp2build_available: true },
1199}`,
1200 expectedCount: 3,
1201 },
1202 }
1203
1204 dir := "."
1205 for _, testCase := range testCases {
1206 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
1207 ctx := android.NewTestContext(config)
1208 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
1209 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1210 ctx.RegisterForBazelConversion()
1211
1212 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
1213 android.FailIfErrored(t, errs)
1214 _, errs = ctx.ResolveDependencies(config)
1215 android.FailIfErrored(t, errs)
1216
1217 bazelTargets := GenerateBazelTargets(ctx.Context.Context, Bp2Build)[dir]
1218 if actualCount := len(bazelTargets); actualCount != testCase.expectedCount {
1219 t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, testCase.expectedCount, actualCount)
1220 }
1221 }
1222}