blob: aa4fc1d978ee9ebbeb7f507f8e538ca7480902e9 [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)
Jingwen Chen164e0862021-02-19 00:48:40 -0500197
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800198 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 Chen164e0862021-02-19 00:48:40 -0500206 codegenCtx := NewCodegenContext(config, *ctx.Context, QueryView)
Jingwen Chenba369ad2021-02-22 10:19:34 -0500207 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
Jingwen Chen4e4756d2021-01-24 21:13:13 -0500208 if actualCount, expectedCount := len(bazelTargets), 1; actualCount != expectedCount {
209 t.Fatalf("Expected %d bazel target, got %d", expectedCount, actualCount)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800210 }
211
212 actualBazelTarget := bazelTargets[0]
213 if actualBazelTarget.content != testCase.expectedBazelTarget {
214 t.Errorf(
215 "Expected generated Bazel target to be '%s', got '%s'",
216 testCase.expectedBazelTarget,
Jingwen Chen73850672020-12-14 08:25:34 -0500217 actualBazelTarget.content,
218 )
219 }
220 }
221}
222
223func TestGenerateBazelTargetModules(t *testing.T) {
224 testCases := []struct {
225 bp string
226 expectedBazelTarget string
227 }{
228 {
229 bp: `custom {
230 name: "foo",
231 string_list_prop: ["a", "b"],
232 string_prop: "a",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500233 bazel_module: { bp2build_available: true },
Jingwen Chen73850672020-12-14 08:25:34 -0500234}`,
235 expectedBazelTarget: `custom(
236 name = "foo",
237 string_list_prop = [
238 "a",
239 "b",
240 ],
241 string_prop = "a",
242)`,
243 },
244 }
245
246 dir := "."
247 for _, testCase := range testCases {
248 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
249 ctx := android.NewTestContext(config)
Jingwen Chen164e0862021-02-19 00:48:40 -0500250
Jingwen Chen73850672020-12-14 08:25:34 -0500251 ctx.RegisterModuleType("custom", customModuleFactory)
252 ctx.RegisterBp2BuildMutator("custom", customBp2BuildMutator)
253 ctx.RegisterForBazelConversion()
254
255 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
Liz Kammer356f7d42021-01-26 09:18:53 -0500256 if Errored(t, "", errs) {
257 continue
258 }
Jingwen Chen73850672020-12-14 08:25:34 -0500259 _, errs = ctx.ResolveDependencies(config)
Liz Kammer356f7d42021-01-26 09:18:53 -0500260 if Errored(t, "", errs) {
261 continue
262 }
Jingwen Chen73850672020-12-14 08:25:34 -0500263
Jingwen Chen164e0862021-02-19 00:48:40 -0500264 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -0500265 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
Jingwen Chen164e0862021-02-19 00:48:40 -0500266
Jingwen Chen4e4756d2021-01-24 21:13:13 -0500267 if actualCount, expectedCount := len(bazelTargets), 1; actualCount != expectedCount {
Liz Kammer356f7d42021-01-26 09:18:53 -0500268 t.Errorf("Expected %d bazel target, got %d", expectedCount, actualCount)
269 } else {
270 actualBazelTarget := bazelTargets[0]
271 if actualBazelTarget.content != testCase.expectedBazelTarget {
272 t.Errorf(
273 "Expected generated Bazel target to be '%s', got '%s'",
274 testCase.expectedBazelTarget,
275 actualBazelTarget.content,
276 )
277 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800278 }
279 }
280}
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500281
Jingwen Chen40067de2021-01-26 21:58:43 -0500282func TestLoadStatements(t *testing.T) {
283 testCases := []struct {
284 bazelTargets BazelTargets
285 expectedLoadStatements string
286 }{
287 {
288 bazelTargets: BazelTargets{
289 BazelTarget{
290 name: "foo",
291 ruleClass: "cc_library",
292 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
293 },
294 },
295 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
296 },
297 {
298 bazelTargets: BazelTargets{
299 BazelTarget{
300 name: "foo",
301 ruleClass: "cc_library",
302 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
303 },
304 BazelTarget{
305 name: "bar",
306 ruleClass: "cc_library",
307 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
308 },
309 },
310 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
311 },
312 {
313 bazelTargets: BazelTargets{
314 BazelTarget{
315 name: "foo",
316 ruleClass: "cc_library",
317 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
318 },
319 BazelTarget{
320 name: "bar",
321 ruleClass: "cc_binary",
322 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
323 },
324 },
325 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")`,
326 },
327 {
328 bazelTargets: BazelTargets{
329 BazelTarget{
330 name: "foo",
331 ruleClass: "cc_library",
332 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
333 },
334 BazelTarget{
335 name: "bar",
336 ruleClass: "cc_binary",
337 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
338 },
339 BazelTarget{
340 name: "baz",
341 ruleClass: "java_binary",
342 bzlLoadLocation: "//build/bazel/rules:java.bzl",
343 },
344 },
345 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")
346load("//build/bazel/rules:java.bzl", "java_binary")`,
347 },
348 {
349 bazelTargets: BazelTargets{
350 BazelTarget{
351 name: "foo",
352 ruleClass: "cc_binary",
353 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
354 },
355 BazelTarget{
356 name: "bar",
357 ruleClass: "java_binary",
358 bzlLoadLocation: "//build/bazel/rules:java.bzl",
359 },
360 BazelTarget{
361 name: "baz",
362 ruleClass: "genrule",
363 // Note: no bzlLoadLocation for native rules
364 },
365 },
366 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary")
367load("//build/bazel/rules:java.bzl", "java_binary")`,
368 },
369 }
370
371 for _, testCase := range testCases {
372 actual := testCase.bazelTargets.LoadStatements()
373 expected := testCase.expectedLoadStatements
374 if actual != expected {
375 t.Fatalf("Expected load statements to be %s, got %s", expected, actual)
376 }
377 }
378
379}
380
381func TestGenerateBazelTargetModules_OneToMany_LoadedFromStarlark(t *testing.T) {
382 testCases := []struct {
383 bp string
384 expectedBazelTarget string
385 expectedBazelTargetCount int
386 expectedLoadStatements string
387 }{
388 {
389 bp: `custom {
390 name: "bar",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500391 bazel_module: { bp2build_available: true },
Jingwen Chen40067de2021-01-26 21:58:43 -0500392}`,
393 expectedBazelTarget: `my_library(
394 name = "bar",
395)
396
397my_proto_library(
398 name = "bar_my_proto_library_deps",
399)
400
401proto_library(
402 name = "bar_proto_library_deps",
403)`,
404 expectedBazelTargetCount: 3,
405 expectedLoadStatements: `load("//build/bazel/rules:proto.bzl", "my_proto_library", "proto_library")
406load("//build/bazel/rules:rules.bzl", "my_library")`,
407 },
408 }
409
410 dir := "."
411 for _, testCase := range testCases {
412 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
413 ctx := android.NewTestContext(config)
414 ctx.RegisterModuleType("custom", customModuleFactory)
415 ctx.RegisterBp2BuildMutator("custom_starlark", customBp2BuildMutatorFromStarlark)
416 ctx.RegisterForBazelConversion()
417
418 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
419 android.FailIfErrored(t, errs)
420 _, errs = ctx.ResolveDependencies(config)
421 android.FailIfErrored(t, errs)
422
Jingwen Chen164e0862021-02-19 00:48:40 -0500423 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -0500424 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
Jingwen Chen40067de2021-01-26 21:58:43 -0500425 if actualCount := len(bazelTargets); actualCount != testCase.expectedBazelTargetCount {
426 t.Fatalf("Expected %d bazel target, got %d", testCase.expectedBazelTargetCount, actualCount)
427 }
428
429 actualBazelTargets := bazelTargets.String()
430 if actualBazelTargets != testCase.expectedBazelTarget {
431 t.Errorf(
432 "Expected generated Bazel target to be '%s', got '%s'",
433 testCase.expectedBazelTarget,
434 actualBazelTargets,
435 )
436 }
437
438 actualLoadStatements := bazelTargets.LoadStatements()
439 if actualLoadStatements != testCase.expectedLoadStatements {
440 t.Errorf(
441 "Expected generated load statements to be '%s', got '%s'",
442 testCase.expectedLoadStatements,
443 actualLoadStatements,
444 )
445 }
446 }
447}
448
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500449func TestModuleTypeBp2Build(t *testing.T) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500450 otherGenruleBp := map[string]string{
451 "other/Android.bp": `genrule {
452 name: "foo.tool",
453 out: ["foo_tool.out"],
454 srcs: ["foo_tool.in"],
455 cmd: "cp $(in) $(out)",
456}
457genrule {
458 name: "other.tool",
459 out: ["other_tool.out"],
460 srcs: ["other_tool.in"],
461 cmd: "cp $(in) $(out)",
462}`,
463 }
464
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500465 testCases := []struct {
Liz Kammer356f7d42021-01-26 09:18:53 -0500466 description string
Jingwen Chena42d6412021-01-26 21:57:27 -0500467 moduleTypeUnderTest string
468 moduleTypeUnderTestFactory android.ModuleFactory
469 moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
Liz Kammer356f7d42021-01-26 09:18:53 -0500470 preArchMutators []android.RegisterMutatorFunc
471 depsMutators []android.RegisterMutatorFunc
Jingwen Chena42d6412021-01-26 21:57:27 -0500472 bp string
Liz Kammer356f7d42021-01-26 09:18:53 -0500473 expectedBazelTargets []string
474 fs map[string]string
475 dir string
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500476 }{
477 {
Liz Kammerebfcf672021-02-16 15:00:05 -0500478 description: "filegroup with does not specify srcs",
479 moduleTypeUnderTest: "filegroup",
480 moduleTypeUnderTestFactory: android.FileGroupFactory,
481 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
482 bp: `filegroup {
483 name: "fg_foo",
484 bazel_module: { bp2build_available: true },
485}`,
486 expectedBazelTargets: []string{
487 `filegroup(
488 name = "fg_foo",
489)`,
490 },
491 },
492 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500493 description: "filegroup with no srcs",
494 moduleTypeUnderTest: "filegroup",
495 moduleTypeUnderTestFactory: android.FileGroupFactory,
496 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500497 bp: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500498 name: "fg_foo",
499 srcs: [],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500500 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500501}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500502 expectedBazelTargets: []string{
503 `filegroup(
504 name = "fg_foo",
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500505 srcs = [
506 ],
507)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500508 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500509 },
510 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500511 description: "filegroup with srcs",
512 moduleTypeUnderTest: "filegroup",
513 moduleTypeUnderTestFactory: android.FileGroupFactory,
514 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500515 bp: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500516 name: "fg_foo",
517 srcs: ["a", "b"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500518 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500519}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500520 expectedBazelTargets: []string{`filegroup(
521 name = "fg_foo",
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500522 srcs = [
523 "a",
524 "b",
525 ],
526)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500527 },
528 },
529 {
530 description: "filegroup with excludes srcs",
531 moduleTypeUnderTest: "filegroup",
532 moduleTypeUnderTestFactory: android.FileGroupFactory,
533 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
534 bp: `filegroup {
535 name: "fg_foo",
536 srcs: ["a", "b"],
537 exclude_srcs: ["a"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500538 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500539}`,
540 expectedBazelTargets: []string{`filegroup(
541 name = "fg_foo",
542 srcs = [
543 "b",
544 ],
545)`,
546 },
547 },
548 {
549 description: "filegroup with glob",
550 moduleTypeUnderTest: "filegroup",
551 moduleTypeUnderTestFactory: android.FileGroupFactory,
552 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
553 bp: `filegroup {
554 name: "foo",
555 srcs: ["**/*.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500556 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500557}`,
558 expectedBazelTargets: []string{`filegroup(
559 name = "foo",
560 srcs = [
561 "other/a.txt",
562 "other/b.txt",
563 "other/subdir/a.txt",
564 ],
565)`,
566 },
567 fs: map[string]string{
568 "other/a.txt": "",
569 "other/b.txt": "",
570 "other/subdir/a.txt": "",
571 "other/file": "",
572 },
573 },
574 {
575 description: "filegroup with glob in subdir",
576 moduleTypeUnderTest: "filegroup",
577 moduleTypeUnderTestFactory: android.FileGroupFactory,
578 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
579 bp: `filegroup {
580 name: "foo",
581 srcs: ["a.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500582 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500583}`,
584 dir: "other",
585 expectedBazelTargets: []string{`filegroup(
586 name = "fg_foo",
587 srcs = [
588 "a.txt",
589 "b.txt",
590 "subdir/a.txt",
591 ],
592)`,
593 },
594 fs: map[string]string{
595 "other/Android.bp": `filegroup {
596 name: "fg_foo",
597 srcs: ["**/*.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500598 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500599}`,
600 "other/a.txt": "",
601 "other/b.txt": "",
602 "other/subdir/a.txt": "",
603 "other/file": "",
604 },
605 },
606 {
607 description: "depends_on_other_dir_module",
608 moduleTypeUnderTest: "filegroup",
609 moduleTypeUnderTestFactory: android.FileGroupFactory,
610 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
611 bp: `filegroup {
612 name: "foobar",
613 srcs: [
614 ":foo",
615 "c",
616 ],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500617 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500618}`,
619 expectedBazelTargets: []string{`filegroup(
620 name = "foobar",
621 srcs = [
622 "//other:foo",
623 "c",
624 ],
625)`,
626 },
627 fs: map[string]string{
628 "other/Android.bp": `filegroup {
629 name: "foo",
630 srcs: ["a", "b"],
631}`,
632 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500633 },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500634 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500635 description: "genrule with command line variable replacements",
636 moduleTypeUnderTest: "genrule",
637 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
638 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Liz Kammer356f7d42021-01-26 09:18:53 -0500639 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
Jingwen Chen316e07c2020-12-14 09:09:52 -0500640 bp: `genrule {
Liz Kammer356f7d42021-01-26 09:18:53 -0500641 name: "foo.tool",
642 out: ["foo_tool.out"],
643 srcs: ["foo_tool.in"],
644 cmd: "cp $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500645 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500646}
647
648genrule {
Jingwen Chen316e07c2020-12-14 09:09:52 -0500649 name: "foo",
650 out: ["foo.out"],
651 srcs: ["foo.in"],
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500652 tools: [":foo.tool"],
653 cmd: "$(location :foo.tool) --genDir=$(genDir) arg $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500654 bazel_module: { bp2build_available: true },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500655}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500656 expectedBazelTargets: []string{
657 `genrule(
Jingwen Chen316e07c2020-12-14 09:09:52 -0500658 name = "foo",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500659 cmd = "$(location :foo.tool) --genDir=$(GENDIR) arg $(SRCS) $(OUTS)",
Jingwen Chen316e07c2020-12-14 09:09:52 -0500660 outs = [
661 "foo.out",
662 ],
663 srcs = [
664 "foo.in",
665 ],
666 tools = [
667 ":foo.tool",
668 ],
669)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500670 `genrule(
671 name = "foo.tool",
672 cmd = "cp $(SRCS) $(OUTS)",
673 outs = [
674 "foo_tool.out",
675 ],
676 srcs = [
677 "foo_tool.in",
678 ],
679)`,
680 },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500681 },
682 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500683 description: "genrule using $(locations :label)",
684 moduleTypeUnderTest: "genrule",
685 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
686 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Liz Kammer356f7d42021-01-26 09:18:53 -0500687 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
Jingwen Chen316e07c2020-12-14 09:09:52 -0500688 bp: `genrule {
Liz Kammer356f7d42021-01-26 09:18:53 -0500689 name: "foo.tools",
690 out: ["foo_tool.out", "foo_tool2.out"],
691 srcs: ["foo_tool.in"],
692 cmd: "cp $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500693 bazel_module: { bp2build_available: true },
694}
Liz Kammer356f7d42021-01-26 09:18:53 -0500695
696genrule {
Jingwen Chen316e07c2020-12-14 09:09:52 -0500697 name: "foo",
698 out: ["foo.out"],
699 srcs: ["foo.in"],
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500700 tools: [":foo.tools"],
701 cmd: "$(locations :foo.tools) -s $(out) $(in)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500702 bazel_module: { bp2build_available: true },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500703}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500704 expectedBazelTargets: []string{`genrule(
Jingwen Chen316e07c2020-12-14 09:09:52 -0500705 name = "foo",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500706 cmd = "$(locations :foo.tools) -s $(OUTS) $(SRCS)",
707 outs = [
708 "foo.out",
709 ],
710 srcs = [
711 "foo.in",
712 ],
713 tools = [
714 ":foo.tools",
715 ],
716)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500717 `genrule(
718 name = "foo.tools",
719 cmd = "cp $(SRCS) $(OUTS)",
720 outs = [
721 "foo_tool.out",
722 "foo_tool2.out",
723 ],
724 srcs = [
725 "foo_tool.in",
726 ],
727)`,
728 },
729 },
730 {
731 description: "genrule using $(locations //absolute:label)",
732 moduleTypeUnderTest: "genrule",
733 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
734 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
735 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
736 bp: `genrule {
737 name: "foo",
738 out: ["foo.out"],
739 srcs: ["foo.in"],
740 tool_files: [":foo.tool"],
741 cmd: "$(locations :foo.tool) -s $(out) $(in)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500742 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500743}`,
744 expectedBazelTargets: []string{`genrule(
745 name = "foo",
746 cmd = "$(locations //other:foo.tool) -s $(OUTS) $(SRCS)",
747 outs = [
748 "foo.out",
749 ],
750 srcs = [
751 "foo.in",
752 ],
753 tools = [
754 "//other:foo.tool",
755 ],
756)`,
757 },
758 fs: otherGenruleBp,
759 },
760 {
761 description: "genrule srcs using $(locations //absolute:label)",
762 moduleTypeUnderTest: "genrule",
763 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
764 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
765 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
766 bp: `genrule {
767 name: "foo",
768 out: ["foo.out"],
769 srcs: [":other.tool"],
770 tool_files: [":foo.tool"],
771 cmd: "$(locations :foo.tool) -s $(out) $(location :other.tool)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500772 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500773}`,
774 expectedBazelTargets: []string{`genrule(
775 name = "foo",
776 cmd = "$(locations //other:foo.tool) -s $(OUTS) $(location //other:other.tool)",
777 outs = [
778 "foo.out",
779 ],
780 srcs = [
781 "//other:other.tool",
782 ],
783 tools = [
784 "//other:foo.tool",
785 ],
786)`,
787 },
788 fs: otherGenruleBp,
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500789 },
790 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500791 description: "genrule using $(location) label should substitute first tool label automatically",
792 moduleTypeUnderTest: "genrule",
793 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
794 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Liz Kammer356f7d42021-01-26 09:18:53 -0500795 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500796 bp: `genrule {
797 name: "foo",
798 out: ["foo.out"],
799 srcs: ["foo.in"],
800 tool_files: [":foo.tool", ":other.tool"],
801 cmd: "$(location) -s $(out) $(in)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500802 bazel_module: { bp2build_available: true },
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500803}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500804 expectedBazelTargets: []string{`genrule(
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500805 name = "foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500806 cmd = "$(location //other:foo.tool) -s $(OUTS) $(SRCS)",
Jingwen Chen316e07c2020-12-14 09:09:52 -0500807 outs = [
808 "foo.out",
809 ],
810 srcs = [
811 "foo.in",
812 ],
813 tools = [
Liz Kammer356f7d42021-01-26 09:18:53 -0500814 "//other:foo.tool",
815 "//other:other.tool",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500816 ],
817)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500818 },
819 fs: otherGenruleBp,
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500820 },
821 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500822 description: "genrule using $(locations) label should substitute first tool label automatically",
823 moduleTypeUnderTest: "genrule",
824 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
825 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Liz Kammer356f7d42021-01-26 09:18:53 -0500826 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500827 bp: `genrule {
828 name: "foo",
829 out: ["foo.out"],
830 srcs: ["foo.in"],
831 tools: [":foo.tool", ":other.tool"],
832 cmd: "$(locations) -s $(out) $(in)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500833 bazel_module: { bp2build_available: true },
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500834}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500835 expectedBazelTargets: []string{`genrule(
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500836 name = "foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500837 cmd = "$(locations //other:foo.tool) -s $(OUTS) $(SRCS)",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500838 outs = [
839 "foo.out",
840 ],
841 srcs = [
842 "foo.in",
843 ],
844 tools = [
Liz Kammer356f7d42021-01-26 09:18:53 -0500845 "//other:foo.tool",
846 "//other:other.tool",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500847 ],
848)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500849 },
850 fs: otherGenruleBp,
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500851 },
852 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500853 description: "genrule without tools or tool_files can convert successfully",
854 moduleTypeUnderTest: "genrule",
855 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
856 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Liz Kammer356f7d42021-01-26 09:18:53 -0500857 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500858 bp: `genrule {
859 name: "foo",
860 out: ["foo.out"],
861 srcs: ["foo.in"],
862 cmd: "cp $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500863 bazel_module: { bp2build_available: true },
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500864}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500865 expectedBazelTargets: []string{`genrule(
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500866 name = "foo",
867 cmd = "cp $(SRCS) $(OUTS)",
868 outs = [
869 "foo.out",
870 ],
871 srcs = [
872 "foo.in",
Jingwen Chen316e07c2020-12-14 09:09:52 -0500873 ],
874)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500875 },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500876 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500877 }
878
879 dir := "."
880 for _, testCase := range testCases {
Liz Kammer356f7d42021-01-26 09:18:53 -0500881 fs := make(map[string][]byte)
882 toParse := []string{
883 "Android.bp",
884 }
885 for f, content := range testCase.fs {
886 if strings.HasSuffix(f, "Android.bp") {
887 toParse = append(toParse, f)
888 }
889 fs[f] = []byte(content)
890 }
891 config := android.TestConfig(buildDir, nil, testCase.bp, fs)
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500892 ctx := android.NewTestContext(config)
893 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Liz Kammer356f7d42021-01-26 09:18:53 -0500894 for _, m := range testCase.depsMutators {
895 ctx.DepsBp2BuildMutators(m)
896 }
Jingwen Chena42d6412021-01-26 21:57:27 -0500897 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500898 ctx.RegisterForBazelConversion()
899
Liz Kammer356f7d42021-01-26 09:18:53 -0500900 _, errs := ctx.ParseFileList(dir, toParse)
901 if Errored(t, testCase.description, errs) {
902 continue
903 }
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500904 _, errs = ctx.ResolveDependencies(config)
Liz Kammer356f7d42021-01-26 09:18:53 -0500905 if Errored(t, testCase.description, errs) {
906 continue
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500907 }
908
Liz Kammer356f7d42021-01-26 09:18:53 -0500909 checkDir := dir
910 if testCase.dir != "" {
911 checkDir = testCase.dir
912 }
Jingwen Chen164e0862021-02-19 00:48:40 -0500913
914 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -0500915 bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir)
Liz Kammer356f7d42021-01-26 09:18:53 -0500916 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
917 t.Errorf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount)
918 } else {
919 for i, target := range bazelTargets {
920 if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
921 t.Errorf(
922 "%s: Expected generated Bazel target to be '%s', got '%s'",
923 testCase.description,
924 w,
925 g,
926 )
927 }
928 }
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500929 }
930 }
931}
Jingwen Chen041b1842021-02-01 00:23:25 -0500932
Liz Kammer356f7d42021-01-26 09:18:53 -0500933func Errored(t *testing.T, desc string, errs []error) bool {
934 t.Helper()
935 if len(errs) > 0 {
936 for _, err := range errs {
937 t.Errorf("%s: %s", desc, err)
938 }
939 return true
940 }
941 return false
942}
943
Jingwen Chen041b1842021-02-01 00:23:25 -0500944type bp2buildMutator = func(android.TopDownMutatorContext)
945
946func TestBp2BuildInlinesDefaults(t *testing.T) {
947 testCases := []struct {
948 moduleTypesUnderTest map[string]android.ModuleFactory
949 bp2buildMutatorsUnderTest map[string]bp2buildMutator
950 bp string
951 expectedBazelTarget string
952 description string
953 }{
954 {
955 moduleTypesUnderTest: map[string]android.ModuleFactory{
956 "genrule": genrule.GenRuleFactory,
957 "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
958 },
959 bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
960 "genrule": genrule.GenruleBp2Build,
961 },
962 bp: `genrule_defaults {
963 name: "gen_defaults",
964 cmd: "do-something $(in) $(out)",
965}
966genrule {
967 name: "gen",
968 out: ["out"],
969 srcs: ["in1"],
970 defaults: ["gen_defaults"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500971 bazel_module: { bp2build_available: true },
Jingwen Chen041b1842021-02-01 00:23:25 -0500972}
973`,
974 expectedBazelTarget: `genrule(
975 name = "gen",
976 cmd = "do-something $(SRCS) $(OUTS)",
977 outs = [
978 "out",
979 ],
980 srcs = [
981 "in1",
982 ],
983)`,
984 description: "genrule applies properties from a genrule_defaults dependency if not specified",
985 },
986 {
987 moduleTypesUnderTest: map[string]android.ModuleFactory{
988 "genrule": genrule.GenRuleFactory,
989 "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
990 },
991 bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
992 "genrule": genrule.GenruleBp2Build,
993 },
994 bp: `genrule_defaults {
995 name: "gen_defaults",
996 out: ["out-from-defaults"],
997 srcs: ["in-from-defaults"],
998 cmd: "cmd-from-defaults",
999}
1000genrule {
1001 name: "gen",
1002 out: ["out"],
1003 srcs: ["in1"],
1004 defaults: ["gen_defaults"],
1005 cmd: "do-something $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001006 bazel_module: { bp2build_available: true },
Jingwen Chen041b1842021-02-01 00:23:25 -05001007}
1008`,
1009 expectedBazelTarget: `genrule(
1010 name = "gen",
1011 cmd = "do-something $(SRCS) $(OUTS)",
1012 outs = [
1013 "out-from-defaults",
1014 "out",
1015 ],
1016 srcs = [
1017 "in-from-defaults",
1018 "in1",
1019 ],
1020)`,
1021 description: "genrule does merges properties from a genrule_defaults dependency, latest-first",
1022 },
1023 {
1024 moduleTypesUnderTest: map[string]android.ModuleFactory{
1025 "genrule": genrule.GenRuleFactory,
1026 "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
1027 },
1028 bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
1029 "genrule": genrule.GenruleBp2Build,
1030 },
1031 bp: `genrule_defaults {
1032 name: "gen_defaults1",
1033 cmd: "cp $(in) $(out)",
1034}
1035
1036genrule_defaults {
1037 name: "gen_defaults2",
1038 srcs: ["in1"],
1039}
1040
1041genrule {
1042 name: "gen",
1043 out: ["out"],
1044 defaults: ["gen_defaults1", "gen_defaults2"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001045 bazel_module: { bp2build_available: true },
Jingwen Chen041b1842021-02-01 00:23:25 -05001046}
1047`,
1048 expectedBazelTarget: `genrule(
1049 name = "gen",
1050 cmd = "cp $(SRCS) $(OUTS)",
1051 outs = [
1052 "out",
1053 ],
1054 srcs = [
1055 "in1",
1056 ],
1057)`,
1058 description: "genrule applies properties from list of genrule_defaults",
1059 },
1060 {
1061 moduleTypesUnderTest: map[string]android.ModuleFactory{
1062 "genrule": genrule.GenRuleFactory,
1063 "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
1064 },
1065 bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
1066 "genrule": genrule.GenruleBp2Build,
1067 },
1068 bp: `genrule_defaults {
1069 name: "gen_defaults1",
1070 defaults: ["gen_defaults2"],
1071 cmd: "cmd1 $(in) $(out)", // overrides gen_defaults2's cmd property value.
1072}
1073
1074genrule_defaults {
1075 name: "gen_defaults2",
1076 defaults: ["gen_defaults3"],
1077 cmd: "cmd2 $(in) $(out)",
1078 out: ["out-from-2"],
1079 srcs: ["in1"],
1080}
1081
1082genrule_defaults {
1083 name: "gen_defaults3",
1084 out: ["out-from-3"],
1085 srcs: ["srcs-from-3"],
1086}
1087
1088genrule {
1089 name: "gen",
1090 out: ["out"],
1091 defaults: ["gen_defaults1"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001092 bazel_module: { bp2build_available: true },
Jingwen Chen041b1842021-02-01 00:23:25 -05001093}
1094`,
1095 expectedBazelTarget: `genrule(
1096 name = "gen",
1097 cmd = "cmd1 $(SRCS) $(OUTS)",
1098 outs = [
1099 "out-from-3",
1100 "out-from-2",
1101 "out",
1102 ],
1103 srcs = [
1104 "srcs-from-3",
1105 "in1",
1106 ],
1107)`,
1108 description: "genrule applies properties from genrule_defaults transitively",
1109 },
1110 }
1111
1112 dir := "."
1113 for _, testCase := range testCases {
1114 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
1115 ctx := android.NewTestContext(config)
1116 for m, factory := range testCase.moduleTypesUnderTest {
1117 ctx.RegisterModuleType(m, factory)
1118 }
1119 for mutator, f := range testCase.bp2buildMutatorsUnderTest {
1120 ctx.RegisterBp2BuildMutator(mutator, f)
1121 }
1122 ctx.RegisterForBazelConversion()
1123
1124 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
1125 android.FailIfErrored(t, errs)
1126 _, errs = ctx.ResolveDependencies(config)
1127 android.FailIfErrored(t, errs)
1128
Jingwen Chen164e0862021-02-19 00:48:40 -05001129 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -05001130 bazelTargets := generateBazelTargetsForDir(codegenCtx, 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
Jingwen Chen164e0862021-02-19 00:48:40 -05001217 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -05001218 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001219 if actualCount := len(bazelTargets); actualCount != testCase.expectedCount {
1220 t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, testCase.expectedCount, actualCount)
1221 }
1222 }
1223}