blob: 27212d13574f5d84b3cf7d057c0bc5bb07df61d0 [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 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500472 description: "filegroup with no srcs",
473 moduleTypeUnderTest: "filegroup",
474 moduleTypeUnderTestFactory: android.FileGroupFactory,
475 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500476 bp: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500477 name: "fg_foo",
478 srcs: [],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500479 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500480}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500481 expectedBazelTargets: []string{
482 `filegroup(
483 name = "fg_foo",
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500484 srcs = [
485 ],
486)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500487 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500488 },
489 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500490 description: "filegroup with srcs",
491 moduleTypeUnderTest: "filegroup",
492 moduleTypeUnderTestFactory: android.FileGroupFactory,
493 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500494 bp: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500495 name: "fg_foo",
496 srcs: ["a", "b"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500497 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500498}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500499 expectedBazelTargets: []string{`filegroup(
500 name = "fg_foo",
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500501 srcs = [
502 "a",
503 "b",
504 ],
505)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500506 },
507 },
508 {
509 description: "filegroup with excludes srcs",
510 moduleTypeUnderTest: "filegroup",
511 moduleTypeUnderTestFactory: android.FileGroupFactory,
512 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
513 bp: `filegroup {
514 name: "fg_foo",
515 srcs: ["a", "b"],
516 exclude_srcs: ["a"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500517 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500518}`,
519 expectedBazelTargets: []string{`filegroup(
520 name = "fg_foo",
521 srcs = [
522 "b",
523 ],
524)`,
525 },
526 },
527 {
528 description: "filegroup with glob",
529 moduleTypeUnderTest: "filegroup",
530 moduleTypeUnderTestFactory: android.FileGroupFactory,
531 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
532 bp: `filegroup {
533 name: "foo",
534 srcs: ["**/*.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500535 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500536}`,
537 expectedBazelTargets: []string{`filegroup(
538 name = "foo",
539 srcs = [
540 "other/a.txt",
541 "other/b.txt",
542 "other/subdir/a.txt",
543 ],
544)`,
545 },
546 fs: map[string]string{
547 "other/a.txt": "",
548 "other/b.txt": "",
549 "other/subdir/a.txt": "",
550 "other/file": "",
551 },
552 },
553 {
554 description: "filegroup with glob in subdir",
555 moduleTypeUnderTest: "filegroup",
556 moduleTypeUnderTestFactory: android.FileGroupFactory,
557 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
558 bp: `filegroup {
559 name: "foo",
560 srcs: ["a.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500561 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500562}`,
563 dir: "other",
564 expectedBazelTargets: []string{`filegroup(
565 name = "fg_foo",
566 srcs = [
567 "a.txt",
568 "b.txt",
569 "subdir/a.txt",
570 ],
571)`,
572 },
573 fs: map[string]string{
574 "other/Android.bp": `filegroup {
575 name: "fg_foo",
576 srcs: ["**/*.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500577 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500578}`,
579 "other/a.txt": "",
580 "other/b.txt": "",
581 "other/subdir/a.txt": "",
582 "other/file": "",
583 },
584 },
585 {
586 description: "depends_on_other_dir_module",
587 moduleTypeUnderTest: "filegroup",
588 moduleTypeUnderTestFactory: android.FileGroupFactory,
589 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
590 bp: `filegroup {
591 name: "foobar",
592 srcs: [
593 ":foo",
594 "c",
595 ],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500596 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500597}`,
598 expectedBazelTargets: []string{`filegroup(
599 name = "foobar",
600 srcs = [
601 "//other:foo",
602 "c",
603 ],
604)`,
605 },
606 fs: map[string]string{
607 "other/Android.bp": `filegroup {
608 name: "foo",
609 srcs: ["a", "b"],
610}`,
611 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500612 },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500613 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500614 description: "genrule with command line variable replacements",
615 moduleTypeUnderTest: "genrule",
616 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
617 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Liz Kammer356f7d42021-01-26 09:18:53 -0500618 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
Jingwen Chen316e07c2020-12-14 09:09:52 -0500619 bp: `genrule {
Liz Kammer356f7d42021-01-26 09:18:53 -0500620 name: "foo.tool",
621 out: ["foo_tool.out"],
622 srcs: ["foo_tool.in"],
623 cmd: "cp $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500624 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500625}
626
627genrule {
Jingwen Chen316e07c2020-12-14 09:09:52 -0500628 name: "foo",
629 out: ["foo.out"],
630 srcs: ["foo.in"],
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500631 tools: [":foo.tool"],
632 cmd: "$(location :foo.tool) --genDir=$(genDir) arg $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500633 bazel_module: { bp2build_available: true },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500634}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500635 expectedBazelTargets: []string{
636 `genrule(
Jingwen Chen316e07c2020-12-14 09:09:52 -0500637 name = "foo",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500638 cmd = "$(location :foo.tool) --genDir=$(GENDIR) arg $(SRCS) $(OUTS)",
Jingwen Chen316e07c2020-12-14 09:09:52 -0500639 outs = [
640 "foo.out",
641 ],
642 srcs = [
643 "foo.in",
644 ],
645 tools = [
646 ":foo.tool",
647 ],
648)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500649 `genrule(
650 name = "foo.tool",
651 cmd = "cp $(SRCS) $(OUTS)",
652 outs = [
653 "foo_tool.out",
654 ],
655 srcs = [
656 "foo_tool.in",
657 ],
658)`,
659 },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500660 },
661 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500662 description: "genrule using $(locations :label)",
663 moduleTypeUnderTest: "genrule",
664 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
665 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Liz Kammer356f7d42021-01-26 09:18:53 -0500666 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
Jingwen Chen316e07c2020-12-14 09:09:52 -0500667 bp: `genrule {
Liz Kammer356f7d42021-01-26 09:18:53 -0500668 name: "foo.tools",
669 out: ["foo_tool.out", "foo_tool2.out"],
670 srcs: ["foo_tool.in"],
671 cmd: "cp $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500672 bazel_module: { bp2build_available: true },
673}
Liz Kammer356f7d42021-01-26 09:18:53 -0500674
675genrule {
Jingwen Chen316e07c2020-12-14 09:09:52 -0500676 name: "foo",
677 out: ["foo.out"],
678 srcs: ["foo.in"],
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500679 tools: [":foo.tools"],
680 cmd: "$(locations :foo.tools) -s $(out) $(in)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500681 bazel_module: { bp2build_available: true },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500682}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500683 expectedBazelTargets: []string{`genrule(
Jingwen Chen316e07c2020-12-14 09:09:52 -0500684 name = "foo",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500685 cmd = "$(locations :foo.tools) -s $(OUTS) $(SRCS)",
686 outs = [
687 "foo.out",
688 ],
689 srcs = [
690 "foo.in",
691 ],
692 tools = [
693 ":foo.tools",
694 ],
695)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500696 `genrule(
697 name = "foo.tools",
698 cmd = "cp $(SRCS) $(OUTS)",
699 outs = [
700 "foo_tool.out",
701 "foo_tool2.out",
702 ],
703 srcs = [
704 "foo_tool.in",
705 ],
706)`,
707 },
708 },
709 {
710 description: "genrule using $(locations //absolute:label)",
711 moduleTypeUnderTest: "genrule",
712 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
713 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
714 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
715 bp: `genrule {
716 name: "foo",
717 out: ["foo.out"],
718 srcs: ["foo.in"],
719 tool_files: [":foo.tool"],
720 cmd: "$(locations :foo.tool) -s $(out) $(in)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500721 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500722}`,
723 expectedBazelTargets: []string{`genrule(
724 name = "foo",
725 cmd = "$(locations //other:foo.tool) -s $(OUTS) $(SRCS)",
726 outs = [
727 "foo.out",
728 ],
729 srcs = [
730 "foo.in",
731 ],
732 tools = [
733 "//other:foo.tool",
734 ],
735)`,
736 },
737 fs: otherGenruleBp,
738 },
739 {
740 description: "genrule srcs using $(locations //absolute:label)",
741 moduleTypeUnderTest: "genrule",
742 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
743 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
744 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
745 bp: `genrule {
746 name: "foo",
747 out: ["foo.out"],
748 srcs: [":other.tool"],
749 tool_files: [":foo.tool"],
750 cmd: "$(locations :foo.tool) -s $(out) $(location :other.tool)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500751 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500752}`,
753 expectedBazelTargets: []string{`genrule(
754 name = "foo",
755 cmd = "$(locations //other:foo.tool) -s $(OUTS) $(location //other:other.tool)",
756 outs = [
757 "foo.out",
758 ],
759 srcs = [
760 "//other:other.tool",
761 ],
762 tools = [
763 "//other:foo.tool",
764 ],
765)`,
766 },
767 fs: otherGenruleBp,
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500768 },
769 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500770 description: "genrule using $(location) label should substitute first tool label automatically",
771 moduleTypeUnderTest: "genrule",
772 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
773 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Liz Kammer356f7d42021-01-26 09:18:53 -0500774 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500775 bp: `genrule {
776 name: "foo",
777 out: ["foo.out"],
778 srcs: ["foo.in"],
779 tool_files: [":foo.tool", ":other.tool"],
780 cmd: "$(location) -s $(out) $(in)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500781 bazel_module: { bp2build_available: true },
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500782}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500783 expectedBazelTargets: []string{`genrule(
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500784 name = "foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500785 cmd = "$(location //other:foo.tool) -s $(OUTS) $(SRCS)",
Jingwen Chen316e07c2020-12-14 09:09:52 -0500786 outs = [
787 "foo.out",
788 ],
789 srcs = [
790 "foo.in",
791 ],
792 tools = [
Liz Kammer356f7d42021-01-26 09:18:53 -0500793 "//other:foo.tool",
794 "//other:other.tool",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500795 ],
796)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500797 },
798 fs: otherGenruleBp,
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500799 },
800 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500801 description: "genrule using $(locations) label should substitute first tool label automatically",
802 moduleTypeUnderTest: "genrule",
803 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
804 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Liz Kammer356f7d42021-01-26 09:18:53 -0500805 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500806 bp: `genrule {
807 name: "foo",
808 out: ["foo.out"],
809 srcs: ["foo.in"],
810 tools: [":foo.tool", ":other.tool"],
811 cmd: "$(locations) -s $(out) $(in)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500812 bazel_module: { bp2build_available: true },
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500813}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500814 expectedBazelTargets: []string{`genrule(
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500815 name = "foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500816 cmd = "$(locations //other:foo.tool) -s $(OUTS) $(SRCS)",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500817 outs = [
818 "foo.out",
819 ],
820 srcs = [
821 "foo.in",
822 ],
823 tools = [
Liz Kammer356f7d42021-01-26 09:18:53 -0500824 "//other:foo.tool",
825 "//other:other.tool",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500826 ],
827)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500828 },
829 fs: otherGenruleBp,
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500830 },
831 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500832 description: "genrule without tools or tool_files can convert successfully",
833 moduleTypeUnderTest: "genrule",
834 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
835 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Liz Kammer356f7d42021-01-26 09:18:53 -0500836 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500837 bp: `genrule {
838 name: "foo",
839 out: ["foo.out"],
840 srcs: ["foo.in"],
841 cmd: "cp $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500842 bazel_module: { bp2build_available: true },
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500843}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500844 expectedBazelTargets: []string{`genrule(
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500845 name = "foo",
846 cmd = "cp $(SRCS) $(OUTS)",
847 outs = [
848 "foo.out",
849 ],
850 srcs = [
851 "foo.in",
Jingwen Chen316e07c2020-12-14 09:09:52 -0500852 ],
853)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500854 },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500855 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500856 }
857
858 dir := "."
859 for _, testCase := range testCases {
Liz Kammer356f7d42021-01-26 09:18:53 -0500860 fs := make(map[string][]byte)
861 toParse := []string{
862 "Android.bp",
863 }
864 for f, content := range testCase.fs {
865 if strings.HasSuffix(f, "Android.bp") {
866 toParse = append(toParse, f)
867 }
868 fs[f] = []byte(content)
869 }
870 config := android.TestConfig(buildDir, nil, testCase.bp, fs)
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500871 ctx := android.NewTestContext(config)
872 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Liz Kammer356f7d42021-01-26 09:18:53 -0500873 for _, m := range testCase.depsMutators {
874 ctx.DepsBp2BuildMutators(m)
875 }
Jingwen Chena42d6412021-01-26 21:57:27 -0500876 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500877 ctx.RegisterForBazelConversion()
878
Liz Kammer356f7d42021-01-26 09:18:53 -0500879 _, errs := ctx.ParseFileList(dir, toParse)
880 if Errored(t, testCase.description, errs) {
881 continue
882 }
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500883 _, errs = ctx.ResolveDependencies(config)
Liz Kammer356f7d42021-01-26 09:18:53 -0500884 if Errored(t, testCase.description, errs) {
885 continue
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500886 }
887
Liz Kammer356f7d42021-01-26 09:18:53 -0500888 checkDir := dir
889 if testCase.dir != "" {
890 checkDir = testCase.dir
891 }
892 bazelTargets := GenerateBazelTargets(ctx.Context.Context, Bp2Build)[checkDir]
893 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
894 t.Errorf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount)
895 } else {
896 for i, target := range bazelTargets {
897 if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
898 t.Errorf(
899 "%s: Expected generated Bazel target to be '%s', got '%s'",
900 testCase.description,
901 w,
902 g,
903 )
904 }
905 }
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500906 }
907 }
908}
Jingwen Chen041b1842021-02-01 00:23:25 -0500909
Liz Kammer356f7d42021-01-26 09:18:53 -0500910func Errored(t *testing.T, desc string, errs []error) bool {
911 t.Helper()
912 if len(errs) > 0 {
913 for _, err := range errs {
914 t.Errorf("%s: %s", desc, err)
915 }
916 return true
917 }
918 return false
919}
920
Jingwen Chen041b1842021-02-01 00:23:25 -0500921type bp2buildMutator = func(android.TopDownMutatorContext)
922
923func TestBp2BuildInlinesDefaults(t *testing.T) {
924 testCases := []struct {
925 moduleTypesUnderTest map[string]android.ModuleFactory
926 bp2buildMutatorsUnderTest map[string]bp2buildMutator
927 bp string
928 expectedBazelTarget string
929 description string
930 }{
931 {
932 moduleTypesUnderTest: map[string]android.ModuleFactory{
933 "genrule": genrule.GenRuleFactory,
934 "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
935 },
936 bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
937 "genrule": genrule.GenruleBp2Build,
938 },
939 bp: `genrule_defaults {
940 name: "gen_defaults",
941 cmd: "do-something $(in) $(out)",
942}
943genrule {
944 name: "gen",
945 out: ["out"],
946 srcs: ["in1"],
947 defaults: ["gen_defaults"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500948 bazel_module: { bp2build_available: true },
Jingwen Chen041b1842021-02-01 00:23:25 -0500949}
950`,
951 expectedBazelTarget: `genrule(
952 name = "gen",
953 cmd = "do-something $(SRCS) $(OUTS)",
954 outs = [
955 "out",
956 ],
957 srcs = [
958 "in1",
959 ],
960)`,
961 description: "genrule applies properties from a genrule_defaults dependency if not specified",
962 },
963 {
964 moduleTypesUnderTest: map[string]android.ModuleFactory{
965 "genrule": genrule.GenRuleFactory,
966 "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
967 },
968 bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
969 "genrule": genrule.GenruleBp2Build,
970 },
971 bp: `genrule_defaults {
972 name: "gen_defaults",
973 out: ["out-from-defaults"],
974 srcs: ["in-from-defaults"],
975 cmd: "cmd-from-defaults",
976}
977genrule {
978 name: "gen",
979 out: ["out"],
980 srcs: ["in1"],
981 defaults: ["gen_defaults"],
982 cmd: "do-something $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500983 bazel_module: { bp2build_available: true },
Jingwen Chen041b1842021-02-01 00:23:25 -0500984}
985`,
986 expectedBazelTarget: `genrule(
987 name = "gen",
988 cmd = "do-something $(SRCS) $(OUTS)",
989 outs = [
990 "out-from-defaults",
991 "out",
992 ],
993 srcs = [
994 "in-from-defaults",
995 "in1",
996 ],
997)`,
998 description: "genrule does merges properties from a genrule_defaults dependency, latest-first",
999 },
1000 {
1001 moduleTypesUnderTest: map[string]android.ModuleFactory{
1002 "genrule": genrule.GenRuleFactory,
1003 "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
1004 },
1005 bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
1006 "genrule": genrule.GenruleBp2Build,
1007 },
1008 bp: `genrule_defaults {
1009 name: "gen_defaults1",
1010 cmd: "cp $(in) $(out)",
1011}
1012
1013genrule_defaults {
1014 name: "gen_defaults2",
1015 srcs: ["in1"],
1016}
1017
1018genrule {
1019 name: "gen",
1020 out: ["out"],
1021 defaults: ["gen_defaults1", "gen_defaults2"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001022 bazel_module: { bp2build_available: true },
Jingwen Chen041b1842021-02-01 00:23:25 -05001023}
1024`,
1025 expectedBazelTarget: `genrule(
1026 name = "gen",
1027 cmd = "cp $(SRCS) $(OUTS)",
1028 outs = [
1029 "out",
1030 ],
1031 srcs = [
1032 "in1",
1033 ],
1034)`,
1035 description: "genrule applies properties from list of genrule_defaults",
1036 },
1037 {
1038 moduleTypesUnderTest: map[string]android.ModuleFactory{
1039 "genrule": genrule.GenRuleFactory,
1040 "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
1041 },
1042 bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
1043 "genrule": genrule.GenruleBp2Build,
1044 },
1045 bp: `genrule_defaults {
1046 name: "gen_defaults1",
1047 defaults: ["gen_defaults2"],
1048 cmd: "cmd1 $(in) $(out)", // overrides gen_defaults2's cmd property value.
1049}
1050
1051genrule_defaults {
1052 name: "gen_defaults2",
1053 defaults: ["gen_defaults3"],
1054 cmd: "cmd2 $(in) $(out)",
1055 out: ["out-from-2"],
1056 srcs: ["in1"],
1057}
1058
1059genrule_defaults {
1060 name: "gen_defaults3",
1061 out: ["out-from-3"],
1062 srcs: ["srcs-from-3"],
1063}
1064
1065genrule {
1066 name: "gen",
1067 out: ["out"],
1068 defaults: ["gen_defaults1"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001069 bazel_module: { bp2build_available: true },
Jingwen Chen041b1842021-02-01 00:23:25 -05001070}
1071`,
1072 expectedBazelTarget: `genrule(
1073 name = "gen",
1074 cmd = "cmd1 $(SRCS) $(OUTS)",
1075 outs = [
1076 "out-from-3",
1077 "out-from-2",
1078 "out",
1079 ],
1080 srcs = [
1081 "srcs-from-3",
1082 "in1",
1083 ],
1084)`,
1085 description: "genrule applies properties from genrule_defaults transitively",
1086 },
1087 }
1088
1089 dir := "."
1090 for _, testCase := range testCases {
1091 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
1092 ctx := android.NewTestContext(config)
1093 for m, factory := range testCase.moduleTypesUnderTest {
1094 ctx.RegisterModuleType(m, factory)
1095 }
1096 for mutator, f := range testCase.bp2buildMutatorsUnderTest {
1097 ctx.RegisterBp2BuildMutator(mutator, f)
1098 }
1099 ctx.RegisterForBazelConversion()
1100
1101 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
1102 android.FailIfErrored(t, errs)
1103 _, errs = ctx.ResolveDependencies(config)
1104 android.FailIfErrored(t, errs)
1105
Jingwen Chen4d2c0872021-02-02 07:06:56 -05001106 bazelTargets := GenerateBazelTargets(ctx.Context.Context, Bp2Build)[dir]
Jingwen Chen041b1842021-02-01 00:23:25 -05001107 if actualCount := len(bazelTargets); actualCount != 1 {
1108 t.Fatalf("%s: Expected 1 bazel target, got %d", testCase.description, actualCount)
1109 }
1110
1111 actualBazelTarget := bazelTargets[0]
1112 if actualBazelTarget.content != testCase.expectedBazelTarget {
1113 t.Errorf(
1114 "%s: Expected generated Bazel target to be '%s', got '%s'",
1115 testCase.description,
1116 testCase.expectedBazelTarget,
1117 actualBazelTarget.content,
1118 )
1119 }
1120 }
1121}
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001122
1123func TestAllowlistingBp2buildTargets(t *testing.T) {
1124 testCases := []struct {
1125 moduleTypeUnderTest string
1126 moduleTypeUnderTestFactory android.ModuleFactory
1127 moduleTypeUnderTestBp2BuildMutator bp2buildMutator
1128 bp string
1129 expectedCount int
1130 description string
1131 }{
1132 {
1133 description: "explicitly unavailable",
1134 moduleTypeUnderTest: "filegroup",
1135 moduleTypeUnderTestFactory: android.FileGroupFactory,
1136 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1137 bp: `filegroup {
1138 name: "foo",
1139 srcs: ["a", "b"],
1140 bazel_module: { bp2build_available: false },
1141}`,
1142 expectedCount: 0,
1143 },
1144 {
1145 description: "implicitly unavailable",
1146 moduleTypeUnderTest: "filegroup",
1147 moduleTypeUnderTestFactory: android.FileGroupFactory,
1148 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1149 bp: `filegroup {
1150 name: "foo",
1151 srcs: ["a", "b"],
1152}`,
1153 expectedCount: 0,
1154 },
1155 {
1156 description: "explicitly available",
1157 moduleTypeUnderTest: "filegroup",
1158 moduleTypeUnderTestFactory: android.FileGroupFactory,
1159 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1160 bp: `filegroup {
1161 name: "foo",
1162 srcs: ["a", "b"],
1163 bazel_module: { bp2build_available: true },
1164}`,
1165 expectedCount: 1,
1166 },
1167 {
1168 description: "generates more than 1 target if needed",
1169 moduleTypeUnderTest: "custom",
1170 moduleTypeUnderTestFactory: customModuleFactory,
1171 moduleTypeUnderTestBp2BuildMutator: customBp2BuildMutatorFromStarlark,
1172 bp: `custom {
1173 name: "foo",
1174 bazel_module: { bp2build_available: true },
1175}`,
1176 expectedCount: 3,
1177 },
1178 }
1179
1180 dir := "."
1181 for _, testCase := range testCases {
1182 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
1183 ctx := android.NewTestContext(config)
1184 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
1185 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1186 ctx.RegisterForBazelConversion()
1187
1188 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
1189 android.FailIfErrored(t, errs)
1190 _, errs = ctx.ResolveDependencies(config)
1191 android.FailIfErrored(t, errs)
1192
1193 bazelTargets := GenerateBazelTargets(ctx.Context.Context, Bp2Build)[dir]
1194 if actualCount := len(bazelTargets); actualCount != testCase.expectedCount {
1195 t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, testCase.expectedCount, actualCount)
1196 }
1197 }
1198}