blob: b1c342c69786908e9c151fbe9cd2a7717374d13f [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 {
Jingwen Chenb4628eb2021-04-08 14:40:57 +000030 bp: `custom { name: "foo" }
Liz Kammer2dd9ca42020-11-25 16:06:39 -080031 `,
32 expectedBazelTarget: `soong_module(
33 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050034 soong_module_name = "foo",
35 soong_module_type = "custom",
36 soong_module_variant = "",
37 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080038 ],
39)`,
40 },
41 {
42 bp: `custom {
43 name: "foo",
44 ramdisk: true,
45}
46 `,
47 expectedBazelTarget: `soong_module(
48 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050049 soong_module_name = "foo",
50 soong_module_type = "custom",
51 soong_module_variant = "",
52 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080053 ],
54 ramdisk = True,
55)`,
56 },
57 {
58 bp: `custom {
59 name: "foo",
60 owner: "a_string_with\"quotes\"_and_\\backslashes\\\\",
61}
62 `,
63 expectedBazelTarget: `soong_module(
64 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050065 soong_module_name = "foo",
66 soong_module_type = "custom",
67 soong_module_variant = "",
68 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080069 ],
70 owner = "a_string_with\"quotes\"_and_\\backslashes\\\\",
71)`,
72 },
73 {
74 bp: `custom {
75 name: "foo",
76 required: ["bar"],
77}
78 `,
79 expectedBazelTarget: `soong_module(
80 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050081 soong_module_name = "foo",
82 soong_module_type = "custom",
83 soong_module_variant = "",
84 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080085 ],
Jingwen Chenb4628eb2021-04-08 14:40:57 +000086 required = ["bar"],
Liz Kammer2dd9ca42020-11-25 16:06:39 -080087)`,
88 },
89 {
90 bp: `custom {
91 name: "foo",
92 target_required: ["qux", "bazqux"],
93}
94 `,
95 expectedBazelTarget: `soong_module(
96 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050097 soong_module_name = "foo",
98 soong_module_type = "custom",
99 soong_module_variant = "",
100 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800101 ],
102 target_required = [
103 "qux",
104 "bazqux",
105 ],
106)`,
107 },
108 {
109 bp: `custom {
110 name: "foo",
111 dist: {
112 targets: ["goal_foo"],
113 tag: ".foo",
114 },
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000115 dists: [{
116 targets: ["goal_bar"],
117 tag: ".bar",
118 }],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800119}
120 `,
121 expectedBazelTarget: `soong_module(
122 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500123 soong_module_name = "foo",
124 soong_module_type = "custom",
125 soong_module_variant = "",
126 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800127 ],
128 dist = {
129 "tag": ".foo",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000130 "targets": ["goal_foo"],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800131 },
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000132 dists = [{
133 "tag": ".bar",
134 "targets": ["goal_bar"],
135 }],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800136)`,
137 },
138 {
139 bp: `custom {
140 name: "foo",
141 required: ["bar"],
142 target_required: ["qux", "bazqux"],
143 ramdisk: true,
144 owner: "custom_owner",
145 dists: [
146 {
147 tag: ".tag",
148 targets: ["my_goal"],
149 },
150 ],
151}
152 `,
153 expectedBazelTarget: `soong_module(
154 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500155 soong_module_name = "foo",
156 soong_module_type = "custom",
157 soong_module_variant = "",
158 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800159 ],
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000160 dists = [{
161 "tag": ".tag",
162 "targets": ["my_goal"],
163 }],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800164 owner = "custom_owner",
165 ramdisk = True,
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000166 required = ["bar"],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800167 target_required = [
168 "qux",
169 "bazqux",
170 ],
171)`,
172 },
173 }
174
175 dir := "."
176 for _, testCase := range testCases {
177 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
178 ctx := android.NewTestContext(config)
Jingwen Chen164e0862021-02-19 00:48:40 -0500179
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800180 ctx.RegisterModuleType("custom", customModuleFactory)
181 ctx.Register()
182
183 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
184 android.FailIfErrored(t, errs)
185 _, errs = ctx.PrepareBuildActions(config)
186 android.FailIfErrored(t, errs)
187
Jingwen Chen164e0862021-02-19 00:48:40 -0500188 codegenCtx := NewCodegenContext(config, *ctx.Context, QueryView)
Jingwen Chenba369ad2021-02-22 10:19:34 -0500189 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
Jingwen Chen4e4756d2021-01-24 21:13:13 -0500190 if actualCount, expectedCount := len(bazelTargets), 1; actualCount != expectedCount {
191 t.Fatalf("Expected %d bazel target, got %d", expectedCount, actualCount)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800192 }
193
194 actualBazelTarget := bazelTargets[0]
195 if actualBazelTarget.content != testCase.expectedBazelTarget {
196 t.Errorf(
197 "Expected generated Bazel target to be '%s', got '%s'",
198 testCase.expectedBazelTarget,
Jingwen Chen73850672020-12-14 08:25:34 -0500199 actualBazelTarget.content,
200 )
201 }
202 }
203}
204
205func TestGenerateBazelTargetModules(t *testing.T) {
206 testCases := []struct {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400207 name string
208 bp string
209 expectedBazelTargets []string
Jingwen Chen73850672020-12-14 08:25:34 -0500210 }{
211 {
212 bp: `custom {
213 name: "foo",
214 string_list_prop: ["a", "b"],
215 string_prop: "a",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500216 bazel_module: { bp2build_available: true },
Jingwen Chen73850672020-12-14 08:25:34 -0500217}`,
Liz Kammer4562a3b2021-04-21 18:15:34 -0400218 expectedBazelTargets: []string{`custom(
Jingwen Chen73850672020-12-14 08:25:34 -0500219 name = "foo",
220 string_list_prop = [
221 "a",
222 "b",
223 ],
224 string_prop = "a",
225)`,
Liz Kammer4562a3b2021-04-21 18:15:34 -0400226 },
Jingwen Chen73850672020-12-14 08:25:34 -0500227 },
Jingwen Chen58a12b82021-03-30 13:08:36 +0000228 {
229 bp: `custom {
230 name: "control_characters",
231 string_list_prop: ["\t", "\n"],
232 string_prop: "a\t\n\r",
233 bazel_module: { bp2build_available: true },
234}`,
Liz Kammer4562a3b2021-04-21 18:15:34 -0400235 expectedBazelTargets: []string{`custom(
Jingwen Chen58a12b82021-03-30 13:08:36 +0000236 name = "control_characters",
237 string_list_prop = [
238 "\t",
239 "\n",
240 ],
241 string_prop = "a\t\n\r",
242)`,
Liz Kammer4562a3b2021-04-21 18:15:34 -0400243 },
244 },
245 {
246 bp: `custom {
247 name: "has_dep",
248 arch_paths: [":dep"],
249 bazel_module: { bp2build_available: true },
250}
251
252custom {
253 name: "dep",
254 arch_paths: ["abc"],
255 bazel_module: { bp2build_available: true },
256}`,
257 expectedBazelTargets: []string{`custom(
258 name = "dep",
259 arch_paths = ["abc"],
260)`,
261 `custom(
262 name = "has_dep",
263 arch_paths = [":dep"],
264)`,
265 },
266 },
267 {
268 bp: `custom {
269 name: "arch_paths",
270 arch: {
271 x86: {
272 arch_paths: ["abc"],
273 },
274 },
275 bazel_module: { bp2build_available: true },
276}`,
277 expectedBazelTargets: []string{`custom(
278 name = "arch_paths",
279 arch_paths = select({
280 "//build/bazel/platforms/arch:x86": ["abc"],
281 "//conditions:default": [],
282 }),
283)`,
284 },
285 },
286 {
287 bp: `custom {
288 name: "has_dep",
289 arch: {
290 x86: {
291 arch_paths: [":dep"],
292 },
293 },
294 bazel_module: { bp2build_available: true },
295}
296
297custom {
298 name: "dep",
299 arch_paths: ["abc"],
300 bazel_module: { bp2build_available: true },
301}`,
302 expectedBazelTargets: []string{`custom(
303 name = "dep",
304 arch_paths = ["abc"],
305)`,
306 `custom(
307 name = "has_dep",
308 arch_paths = select({
309 "//build/bazel/platforms/arch:x86": [":dep"],
310 "//conditions:default": [],
311 }),
312)`,
313 },
Jingwen Chen58a12b82021-03-30 13:08:36 +0000314 },
Jingwen Chen73850672020-12-14 08:25:34 -0500315 }
316
317 dir := "."
318 for _, testCase := range testCases {
319 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
320 ctx := android.NewTestContext(config)
Jingwen Chen164e0862021-02-19 00:48:40 -0500321
Jingwen Chen73850672020-12-14 08:25:34 -0500322 ctx.RegisterModuleType("custom", customModuleFactory)
323 ctx.RegisterBp2BuildMutator("custom", customBp2BuildMutator)
324 ctx.RegisterForBazelConversion()
325
326 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
Rupert Shuttleworth06559d02021-05-19 09:14:26 -0400327 if errored(t, "", errs) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500328 continue
329 }
Jingwen Chen73850672020-12-14 08:25:34 -0500330 _, errs = ctx.ResolveDependencies(config)
Rupert Shuttleworth06559d02021-05-19 09:14:26 -0400331 if errored(t, "", errs) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500332 continue
333 }
Jingwen Chen73850672020-12-14 08:25:34 -0500334
Jingwen Chen164e0862021-02-19 00:48:40 -0500335 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -0500336 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
Jingwen Chen164e0862021-02-19 00:48:40 -0500337
Liz Kammer4562a3b2021-04-21 18:15:34 -0400338 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
Liz Kammer356f7d42021-01-26 09:18:53 -0500339 t.Errorf("Expected %d bazel target, got %d", expectedCount, actualCount)
340 } else {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400341 for i, expectedBazelTarget := range testCase.expectedBazelTargets {
342 actualBazelTarget := bazelTargets[i]
343 if actualBazelTarget.content != expectedBazelTarget {
344 t.Errorf(
345 "Expected generated Bazel target to be '%s', got '%s'",
346 expectedBazelTarget,
347 actualBazelTarget.content,
348 )
349 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500350 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800351 }
352 }
353}
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500354
Jingwen Chen40067de2021-01-26 21:58:43 -0500355func TestLoadStatements(t *testing.T) {
356 testCases := []struct {
357 bazelTargets BazelTargets
358 expectedLoadStatements string
359 }{
360 {
361 bazelTargets: BazelTargets{
362 BazelTarget{
363 name: "foo",
364 ruleClass: "cc_library",
365 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
366 },
367 },
368 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
369 },
370 {
371 bazelTargets: BazelTargets{
372 BazelTarget{
373 name: "foo",
374 ruleClass: "cc_library",
375 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
376 },
377 BazelTarget{
378 name: "bar",
379 ruleClass: "cc_library",
380 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
381 },
382 },
383 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
384 },
385 {
386 bazelTargets: BazelTargets{
387 BazelTarget{
388 name: "foo",
389 ruleClass: "cc_library",
390 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
391 },
392 BazelTarget{
393 name: "bar",
394 ruleClass: "cc_binary",
395 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
396 },
397 },
398 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")`,
399 },
400 {
401 bazelTargets: BazelTargets{
402 BazelTarget{
403 name: "foo",
404 ruleClass: "cc_library",
405 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
406 },
407 BazelTarget{
408 name: "bar",
409 ruleClass: "cc_binary",
410 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
411 },
412 BazelTarget{
413 name: "baz",
414 ruleClass: "java_binary",
415 bzlLoadLocation: "//build/bazel/rules:java.bzl",
416 },
417 },
418 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")
419load("//build/bazel/rules:java.bzl", "java_binary")`,
420 },
421 {
422 bazelTargets: BazelTargets{
423 BazelTarget{
424 name: "foo",
425 ruleClass: "cc_binary",
426 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
427 },
428 BazelTarget{
429 name: "bar",
430 ruleClass: "java_binary",
431 bzlLoadLocation: "//build/bazel/rules:java.bzl",
432 },
433 BazelTarget{
434 name: "baz",
435 ruleClass: "genrule",
436 // Note: no bzlLoadLocation for native rules
437 },
438 },
439 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary")
440load("//build/bazel/rules:java.bzl", "java_binary")`,
441 },
442 }
443
444 for _, testCase := range testCases {
445 actual := testCase.bazelTargets.LoadStatements()
446 expected := testCase.expectedLoadStatements
447 if actual != expected {
448 t.Fatalf("Expected load statements to be %s, got %s", expected, actual)
449 }
450 }
451
452}
453
454func TestGenerateBazelTargetModules_OneToMany_LoadedFromStarlark(t *testing.T) {
455 testCases := []struct {
456 bp string
457 expectedBazelTarget string
458 expectedBazelTargetCount int
459 expectedLoadStatements string
460 }{
461 {
462 bp: `custom {
463 name: "bar",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500464 bazel_module: { bp2build_available: true },
Jingwen Chen40067de2021-01-26 21:58:43 -0500465}`,
466 expectedBazelTarget: `my_library(
467 name = "bar",
468)
469
470my_proto_library(
471 name = "bar_my_proto_library_deps",
472)
473
474proto_library(
475 name = "bar_proto_library_deps",
476)`,
477 expectedBazelTargetCount: 3,
478 expectedLoadStatements: `load("//build/bazel/rules:proto.bzl", "my_proto_library", "proto_library")
479load("//build/bazel/rules:rules.bzl", "my_library")`,
480 },
481 }
482
483 dir := "."
484 for _, testCase := range testCases {
485 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
486 ctx := android.NewTestContext(config)
487 ctx.RegisterModuleType("custom", customModuleFactory)
Jingwen Chen12b4c272021-03-10 02:05:59 -0500488 ctx.RegisterBp2BuildMutator("custom", customBp2BuildMutatorFromStarlark)
Jingwen Chen40067de2021-01-26 21:58:43 -0500489 ctx.RegisterForBazelConversion()
490
491 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
492 android.FailIfErrored(t, errs)
493 _, errs = ctx.ResolveDependencies(config)
494 android.FailIfErrored(t, errs)
495
Jingwen Chen164e0862021-02-19 00:48:40 -0500496 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -0500497 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
Jingwen Chen40067de2021-01-26 21:58:43 -0500498 if actualCount := len(bazelTargets); actualCount != testCase.expectedBazelTargetCount {
499 t.Fatalf("Expected %d bazel target, got %d", testCase.expectedBazelTargetCount, actualCount)
500 }
501
502 actualBazelTargets := bazelTargets.String()
503 if actualBazelTargets != testCase.expectedBazelTarget {
504 t.Errorf(
505 "Expected generated Bazel target to be '%s', got '%s'",
506 testCase.expectedBazelTarget,
507 actualBazelTargets,
508 )
509 }
510
511 actualLoadStatements := bazelTargets.LoadStatements()
512 if actualLoadStatements != testCase.expectedLoadStatements {
513 t.Errorf(
514 "Expected generated load statements to be '%s', got '%s'",
515 testCase.expectedLoadStatements,
516 actualLoadStatements,
517 )
518 }
519 }
520}
521
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500522func TestModuleTypeBp2Build(t *testing.T) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500523 otherGenruleBp := map[string]string{
524 "other/Android.bp": `genrule {
525 name: "foo.tool",
526 out: ["foo_tool.out"],
527 srcs: ["foo_tool.in"],
528 cmd: "cp $(in) $(out)",
529}
530genrule {
531 name: "other.tool",
532 out: ["other_tool.out"],
533 srcs: ["other_tool.in"],
534 cmd: "cp $(in) $(out)",
535}`,
536 }
537
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500538 testCases := []struct {
Liz Kammer356f7d42021-01-26 09:18:53 -0500539 description string
Jingwen Chena42d6412021-01-26 21:57:27 -0500540 moduleTypeUnderTest string
541 moduleTypeUnderTestFactory android.ModuleFactory
542 moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
Liz Kammer356f7d42021-01-26 09:18:53 -0500543 preArchMutators []android.RegisterMutatorFunc
544 depsMutators []android.RegisterMutatorFunc
Jingwen Chena42d6412021-01-26 21:57:27 -0500545 bp string
Liz Kammer356f7d42021-01-26 09:18:53 -0500546 expectedBazelTargets []string
547 fs map[string]string
548 dir string
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500549 }{
550 {
Liz Kammerebfcf672021-02-16 15:00:05 -0500551 description: "filegroup with does not specify srcs",
552 moduleTypeUnderTest: "filegroup",
553 moduleTypeUnderTestFactory: android.FileGroupFactory,
554 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
555 bp: `filegroup {
556 name: "fg_foo",
557 bazel_module: { bp2build_available: true },
558}`,
559 expectedBazelTargets: []string{
560 `filegroup(
561 name = "fg_foo",
562)`,
563 },
564 },
565 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500566 description: "filegroup with no srcs",
567 moduleTypeUnderTest: "filegroup",
568 moduleTypeUnderTestFactory: android.FileGroupFactory,
569 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500570 bp: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500571 name: "fg_foo",
572 srcs: [],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500573 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500574}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500575 expectedBazelTargets: []string{
576 `filegroup(
577 name = "fg_foo",
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500578)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500579 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500580 },
581 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500582 description: "filegroup with srcs",
583 moduleTypeUnderTest: "filegroup",
584 moduleTypeUnderTestFactory: android.FileGroupFactory,
585 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500586 bp: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500587 name: "fg_foo",
588 srcs: ["a", "b"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500589 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500590}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500591 expectedBazelTargets: []string{`filegroup(
592 name = "fg_foo",
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500593 srcs = [
594 "a",
595 "b",
596 ],
597)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500598 },
599 },
600 {
601 description: "filegroup with excludes srcs",
602 moduleTypeUnderTest: "filegroup",
603 moduleTypeUnderTestFactory: android.FileGroupFactory,
604 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
605 bp: `filegroup {
606 name: "fg_foo",
607 srcs: ["a", "b"],
608 exclude_srcs: ["a"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500609 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500610}`,
611 expectedBazelTargets: []string{`filegroup(
612 name = "fg_foo",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000613 srcs = ["b"],
Liz Kammer356f7d42021-01-26 09:18:53 -0500614)`,
615 },
616 },
617 {
618 description: "filegroup with glob",
619 moduleTypeUnderTest: "filegroup",
620 moduleTypeUnderTestFactory: android.FileGroupFactory,
621 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
622 bp: `filegroup {
623 name: "foo",
624 srcs: ["**/*.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500625 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500626}`,
627 expectedBazelTargets: []string{`filegroup(
628 name = "foo",
629 srcs = [
630 "other/a.txt",
631 "other/b.txt",
632 "other/subdir/a.txt",
633 ],
634)`,
635 },
636 fs: map[string]string{
637 "other/a.txt": "",
638 "other/b.txt": "",
639 "other/subdir/a.txt": "",
640 "other/file": "",
641 },
642 },
643 {
644 description: "filegroup with glob in subdir",
645 moduleTypeUnderTest: "filegroup",
646 moduleTypeUnderTestFactory: android.FileGroupFactory,
647 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
648 bp: `filegroup {
649 name: "foo",
650 srcs: ["a.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500651 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500652}`,
653 dir: "other",
654 expectedBazelTargets: []string{`filegroup(
655 name = "fg_foo",
656 srcs = [
657 "a.txt",
658 "b.txt",
659 "subdir/a.txt",
660 ],
661)`,
662 },
663 fs: map[string]string{
664 "other/Android.bp": `filegroup {
665 name: "fg_foo",
666 srcs: ["**/*.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500667 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500668}`,
669 "other/a.txt": "",
670 "other/b.txt": "",
671 "other/subdir/a.txt": "",
672 "other/file": "",
673 },
674 },
675 {
676 description: "depends_on_other_dir_module",
677 moduleTypeUnderTest: "filegroup",
678 moduleTypeUnderTestFactory: android.FileGroupFactory,
679 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
680 bp: `filegroup {
681 name: "foobar",
682 srcs: [
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000683 ":foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500684 "c",
685 ],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500686 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500687}`,
688 expectedBazelTargets: []string{`filegroup(
689 name = "foobar",
690 srcs = [
691 "//other:foo",
692 "c",
693 ],
694)`,
695 },
696 fs: map[string]string{
697 "other/Android.bp": `filegroup {
698 name: "foo",
699 srcs: ["a", "b"],
700}`,
701 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500702 },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500703 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500704 description: "genrule with command line variable replacements",
705 moduleTypeUnderTest: "genrule",
706 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
707 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Liz Kammer356f7d42021-01-26 09:18:53 -0500708 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
Jingwen Chen316e07c2020-12-14 09:09:52 -0500709 bp: `genrule {
Liz Kammer356f7d42021-01-26 09:18:53 -0500710 name: "foo.tool",
711 out: ["foo_tool.out"],
712 srcs: ["foo_tool.in"],
713 cmd: "cp $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500714 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500715}
716
717genrule {
Jingwen Chen316e07c2020-12-14 09:09:52 -0500718 name: "foo",
719 out: ["foo.out"],
720 srcs: ["foo.in"],
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500721 tools: [":foo.tool"],
722 cmd: "$(location :foo.tool) --genDir=$(genDir) arg $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500723 bazel_module: { bp2build_available: true },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500724}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500725 expectedBazelTargets: []string{
726 `genrule(
Jingwen Chen316e07c2020-12-14 09:09:52 -0500727 name = "foo",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500728 cmd = "$(location :foo.tool) --genDir=$(GENDIR) arg $(SRCS) $(OUTS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000729 outs = ["foo.out"],
730 srcs = ["foo.in"],
731 tools = [":foo.tool"],
Jingwen Chen316e07c2020-12-14 09:09:52 -0500732)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500733 `genrule(
734 name = "foo.tool",
735 cmd = "cp $(SRCS) $(OUTS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000736 outs = ["foo_tool.out"],
737 srcs = ["foo_tool.in"],
Liz Kammer356f7d42021-01-26 09:18:53 -0500738)`,
739 },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500740 },
741 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500742 description: "genrule using $(locations :label)",
743 moduleTypeUnderTest: "genrule",
744 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
745 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Liz Kammer356f7d42021-01-26 09:18:53 -0500746 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
Jingwen Chen316e07c2020-12-14 09:09:52 -0500747 bp: `genrule {
Liz Kammer356f7d42021-01-26 09:18:53 -0500748 name: "foo.tools",
749 out: ["foo_tool.out", "foo_tool2.out"],
750 srcs: ["foo_tool.in"],
751 cmd: "cp $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500752 bazel_module: { bp2build_available: true },
753}
Liz Kammer356f7d42021-01-26 09:18:53 -0500754
755genrule {
Jingwen Chen316e07c2020-12-14 09:09:52 -0500756 name: "foo",
757 out: ["foo.out"],
758 srcs: ["foo.in"],
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500759 tools: [":foo.tools"],
760 cmd: "$(locations :foo.tools) -s $(out) $(in)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500761 bazel_module: { bp2build_available: true },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500762}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500763 expectedBazelTargets: []string{`genrule(
Jingwen Chen316e07c2020-12-14 09:09:52 -0500764 name = "foo",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500765 cmd = "$(locations :foo.tools) -s $(OUTS) $(SRCS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000766 outs = ["foo.out"],
767 srcs = ["foo.in"],
768 tools = [":foo.tools"],
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500769)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500770 `genrule(
771 name = "foo.tools",
772 cmd = "cp $(SRCS) $(OUTS)",
773 outs = [
774 "foo_tool.out",
775 "foo_tool2.out",
776 ],
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000777 srcs = ["foo_tool.in"],
Liz Kammer356f7d42021-01-26 09:18:53 -0500778)`,
779 },
780 },
781 {
782 description: "genrule using $(locations //absolute:label)",
783 moduleTypeUnderTest: "genrule",
784 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
785 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
786 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
787 bp: `genrule {
788 name: "foo",
789 out: ["foo.out"],
790 srcs: ["foo.in"],
791 tool_files: [":foo.tool"],
792 cmd: "$(locations :foo.tool) -s $(out) $(in)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500793 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500794}`,
795 expectedBazelTargets: []string{`genrule(
796 name = "foo",
797 cmd = "$(locations //other:foo.tool) -s $(OUTS) $(SRCS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000798 outs = ["foo.out"],
799 srcs = ["foo.in"],
800 tools = ["//other:foo.tool"],
Liz Kammer356f7d42021-01-26 09:18:53 -0500801)`,
802 },
803 fs: otherGenruleBp,
804 },
805 {
806 description: "genrule srcs using $(locations //absolute:label)",
807 moduleTypeUnderTest: "genrule",
808 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
809 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
810 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
811 bp: `genrule {
812 name: "foo",
813 out: ["foo.out"],
814 srcs: [":other.tool"],
815 tool_files: [":foo.tool"],
816 cmd: "$(locations :foo.tool) -s $(out) $(location :other.tool)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500817 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500818}`,
819 expectedBazelTargets: []string{`genrule(
820 name = "foo",
821 cmd = "$(locations //other:foo.tool) -s $(OUTS) $(location //other:other.tool)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000822 outs = ["foo.out"],
823 srcs = ["//other:other.tool"],
824 tools = ["//other:foo.tool"],
Liz Kammer356f7d42021-01-26 09:18:53 -0500825)`,
826 },
827 fs: otherGenruleBp,
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500828 },
829 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500830 description: "genrule using $(location) label should substitute first tool label automatically",
831 moduleTypeUnderTest: "genrule",
832 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
833 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Liz Kammer356f7d42021-01-26 09:18:53 -0500834 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500835 bp: `genrule {
836 name: "foo",
837 out: ["foo.out"],
838 srcs: ["foo.in"],
839 tool_files: [":foo.tool", ":other.tool"],
840 cmd: "$(location) -s $(out) $(in)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500841 bazel_module: { bp2build_available: true },
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500842}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500843 expectedBazelTargets: []string{`genrule(
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500844 name = "foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500845 cmd = "$(location //other:foo.tool) -s $(OUTS) $(SRCS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000846 outs = ["foo.out"],
847 srcs = ["foo.in"],
Jingwen Chen316e07c2020-12-14 09:09:52 -0500848 tools = [
Liz Kammer356f7d42021-01-26 09:18:53 -0500849 "//other:foo.tool",
850 "//other:other.tool",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500851 ],
852)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500853 },
854 fs: otherGenruleBp,
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500855 },
856 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500857 description: "genrule using $(locations) label should substitute first tool label automatically",
858 moduleTypeUnderTest: "genrule",
859 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
860 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Liz Kammer356f7d42021-01-26 09:18:53 -0500861 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500862 bp: `genrule {
863 name: "foo",
864 out: ["foo.out"],
865 srcs: ["foo.in"],
866 tools: [":foo.tool", ":other.tool"],
867 cmd: "$(locations) -s $(out) $(in)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500868 bazel_module: { bp2build_available: true },
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500869}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500870 expectedBazelTargets: []string{`genrule(
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500871 name = "foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500872 cmd = "$(locations //other:foo.tool) -s $(OUTS) $(SRCS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000873 outs = ["foo.out"],
874 srcs = ["foo.in"],
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500875 tools = [
Liz Kammer356f7d42021-01-26 09:18:53 -0500876 "//other:foo.tool",
877 "//other:other.tool",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500878 ],
879)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500880 },
881 fs: otherGenruleBp,
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500882 },
883 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500884 description: "genrule without tools or tool_files can convert successfully",
885 moduleTypeUnderTest: "genrule",
886 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
887 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Liz Kammer356f7d42021-01-26 09:18:53 -0500888 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500889 bp: `genrule {
890 name: "foo",
891 out: ["foo.out"],
892 srcs: ["foo.in"],
893 cmd: "cp $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500894 bazel_module: { bp2build_available: true },
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500895}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500896 expectedBazelTargets: []string{`genrule(
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500897 name = "foo",
898 cmd = "cp $(SRCS) $(OUTS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000899 outs = ["foo.out"],
900 srcs = ["foo.in"],
Jingwen Chen316e07c2020-12-14 09:09:52 -0500901)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500902 },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500903 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500904 }
905
906 dir := "."
907 for _, testCase := range testCases {
Liz Kammer356f7d42021-01-26 09:18:53 -0500908 fs := make(map[string][]byte)
909 toParse := []string{
910 "Android.bp",
911 }
912 for f, content := range testCase.fs {
913 if strings.HasSuffix(f, "Android.bp") {
914 toParse = append(toParse, f)
915 }
916 fs[f] = []byte(content)
917 }
918 config := android.TestConfig(buildDir, nil, testCase.bp, fs)
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500919 ctx := android.NewTestContext(config)
920 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Liz Kammer356f7d42021-01-26 09:18:53 -0500921 for _, m := range testCase.depsMutators {
922 ctx.DepsBp2BuildMutators(m)
923 }
Jingwen Chena42d6412021-01-26 21:57:27 -0500924 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500925 ctx.RegisterForBazelConversion()
926
Liz Kammer356f7d42021-01-26 09:18:53 -0500927 _, errs := ctx.ParseFileList(dir, toParse)
Rupert Shuttleworth06559d02021-05-19 09:14:26 -0400928 if errored(t, testCase.description, errs) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500929 continue
930 }
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500931 _, errs = ctx.ResolveDependencies(config)
Rupert Shuttleworth06559d02021-05-19 09:14:26 -0400932 if errored(t, testCase.description, errs) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500933 continue
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500934 }
935
Liz Kammer356f7d42021-01-26 09:18:53 -0500936 checkDir := dir
937 if testCase.dir != "" {
938 checkDir = testCase.dir
939 }
Jingwen Chen164e0862021-02-19 00:48:40 -0500940
941 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -0500942 bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir)
Liz Kammer356f7d42021-01-26 09:18:53 -0500943 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
944 t.Errorf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount)
945 } else {
946 for i, target := range bazelTargets {
947 if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
948 t.Errorf(
949 "%s: Expected generated Bazel target to be '%s', got '%s'",
950 testCase.description,
951 w,
952 g,
953 )
954 }
955 }
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500956 }
957 }
958}
Jingwen Chen041b1842021-02-01 00:23:25 -0500959
960type bp2buildMutator = func(android.TopDownMutatorContext)
961
962func TestBp2BuildInlinesDefaults(t *testing.T) {
963 testCases := []struct {
964 moduleTypesUnderTest map[string]android.ModuleFactory
965 bp2buildMutatorsUnderTest map[string]bp2buildMutator
966 bp string
967 expectedBazelTarget string
968 description string
969 }{
970 {
971 moduleTypesUnderTest: map[string]android.ModuleFactory{
972 "genrule": genrule.GenRuleFactory,
973 "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
974 },
975 bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
976 "genrule": genrule.GenruleBp2Build,
977 },
978 bp: `genrule_defaults {
979 name: "gen_defaults",
980 cmd: "do-something $(in) $(out)",
981}
982genrule {
983 name: "gen",
984 out: ["out"],
985 srcs: ["in1"],
986 defaults: ["gen_defaults"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500987 bazel_module: { bp2build_available: true },
Jingwen Chen041b1842021-02-01 00:23:25 -0500988}
989`,
990 expectedBazelTarget: `genrule(
991 name = "gen",
992 cmd = "do-something $(SRCS) $(OUTS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000993 outs = ["out"],
994 srcs = ["in1"],
Jingwen Chen041b1842021-02-01 00:23:25 -0500995)`,
996 description: "genrule applies properties from a genrule_defaults dependency if not specified",
997 },
998 {
999 moduleTypesUnderTest: map[string]android.ModuleFactory{
1000 "genrule": genrule.GenRuleFactory,
1001 "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
1002 },
1003 bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
1004 "genrule": genrule.GenruleBp2Build,
1005 },
1006 bp: `genrule_defaults {
1007 name: "gen_defaults",
1008 out: ["out-from-defaults"],
1009 srcs: ["in-from-defaults"],
1010 cmd: "cmd-from-defaults",
1011}
1012genrule {
1013 name: "gen",
1014 out: ["out"],
1015 srcs: ["in1"],
1016 defaults: ["gen_defaults"],
1017 cmd: "do-something $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001018 bazel_module: { bp2build_available: true },
Jingwen Chen041b1842021-02-01 00:23:25 -05001019}
1020`,
1021 expectedBazelTarget: `genrule(
1022 name = "gen",
1023 cmd = "do-something $(SRCS) $(OUTS)",
1024 outs = [
1025 "out-from-defaults",
1026 "out",
1027 ],
1028 srcs = [
1029 "in-from-defaults",
1030 "in1",
1031 ],
1032)`,
1033 description: "genrule does merges properties from a genrule_defaults dependency, latest-first",
1034 },
1035 {
1036 moduleTypesUnderTest: map[string]android.ModuleFactory{
1037 "genrule": genrule.GenRuleFactory,
1038 "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
1039 },
1040 bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
1041 "genrule": genrule.GenruleBp2Build,
1042 },
1043 bp: `genrule_defaults {
1044 name: "gen_defaults1",
1045 cmd: "cp $(in) $(out)",
1046}
1047
1048genrule_defaults {
1049 name: "gen_defaults2",
1050 srcs: ["in1"],
1051}
1052
1053genrule {
1054 name: "gen",
1055 out: ["out"],
1056 defaults: ["gen_defaults1", "gen_defaults2"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001057 bazel_module: { bp2build_available: true },
Jingwen Chen041b1842021-02-01 00:23:25 -05001058}
1059`,
1060 expectedBazelTarget: `genrule(
1061 name = "gen",
1062 cmd = "cp $(SRCS) $(OUTS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +00001063 outs = ["out"],
1064 srcs = ["in1"],
Jingwen Chen041b1842021-02-01 00:23:25 -05001065)`,
1066 description: "genrule applies properties from list of genrule_defaults",
1067 },
1068 {
1069 moduleTypesUnderTest: map[string]android.ModuleFactory{
1070 "genrule": genrule.GenRuleFactory,
1071 "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
1072 },
1073 bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
1074 "genrule": genrule.GenruleBp2Build,
1075 },
1076 bp: `genrule_defaults {
1077 name: "gen_defaults1",
1078 defaults: ["gen_defaults2"],
1079 cmd: "cmd1 $(in) $(out)", // overrides gen_defaults2's cmd property value.
1080}
1081
1082genrule_defaults {
1083 name: "gen_defaults2",
1084 defaults: ["gen_defaults3"],
1085 cmd: "cmd2 $(in) $(out)",
1086 out: ["out-from-2"],
1087 srcs: ["in1"],
1088}
1089
1090genrule_defaults {
1091 name: "gen_defaults3",
1092 out: ["out-from-3"],
1093 srcs: ["srcs-from-3"],
1094}
1095
1096genrule {
1097 name: "gen",
1098 out: ["out"],
1099 defaults: ["gen_defaults1"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001100 bazel_module: { bp2build_available: true },
Jingwen Chen041b1842021-02-01 00:23:25 -05001101}
1102`,
1103 expectedBazelTarget: `genrule(
1104 name = "gen",
1105 cmd = "cmd1 $(SRCS) $(OUTS)",
1106 outs = [
1107 "out-from-3",
1108 "out-from-2",
1109 "out",
1110 ],
1111 srcs = [
Jingwen Chen041b1842021-02-01 00:23:25 -05001112 "in1",
Jingwen Chen07027912021-03-15 06:02:43 -04001113 "srcs-from-3",
Jingwen Chen041b1842021-02-01 00:23:25 -05001114 ],
1115)`,
1116 description: "genrule applies properties from genrule_defaults transitively",
1117 },
1118 }
1119
1120 dir := "."
1121 for _, testCase := range testCases {
1122 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
1123 ctx := android.NewTestContext(config)
1124 for m, factory := range testCase.moduleTypesUnderTest {
1125 ctx.RegisterModuleType(m, factory)
1126 }
1127 for mutator, f := range testCase.bp2buildMutatorsUnderTest {
1128 ctx.RegisterBp2BuildMutator(mutator, f)
1129 }
1130 ctx.RegisterForBazelConversion()
1131
1132 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
1133 android.FailIfErrored(t, errs)
1134 _, errs = ctx.ResolveDependencies(config)
1135 android.FailIfErrored(t, errs)
1136
Jingwen Chen164e0862021-02-19 00:48:40 -05001137 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -05001138 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
Jingwen Chen041b1842021-02-01 00:23:25 -05001139 if actualCount := len(bazelTargets); actualCount != 1 {
1140 t.Fatalf("%s: Expected 1 bazel target, got %d", testCase.description, actualCount)
1141 }
1142
1143 actualBazelTarget := bazelTargets[0]
1144 if actualBazelTarget.content != testCase.expectedBazelTarget {
1145 t.Errorf(
1146 "%s: Expected generated Bazel target to be '%s', got '%s'",
1147 testCase.description,
1148 testCase.expectedBazelTarget,
1149 actualBazelTarget.content,
1150 )
1151 }
1152 }
1153}
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001154
Jingwen Chen12b4c272021-03-10 02:05:59 -05001155func TestAllowlistingBp2buildTargetsExplicitly(t *testing.T) {
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001156 testCases := []struct {
1157 moduleTypeUnderTest string
1158 moduleTypeUnderTestFactory android.ModuleFactory
1159 moduleTypeUnderTestBp2BuildMutator bp2buildMutator
1160 bp string
1161 expectedCount int
1162 description string
1163 }{
1164 {
1165 description: "explicitly unavailable",
1166 moduleTypeUnderTest: "filegroup",
1167 moduleTypeUnderTestFactory: android.FileGroupFactory,
1168 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1169 bp: `filegroup {
1170 name: "foo",
1171 srcs: ["a", "b"],
1172 bazel_module: { bp2build_available: false },
1173}`,
1174 expectedCount: 0,
1175 },
1176 {
1177 description: "implicitly unavailable",
1178 moduleTypeUnderTest: "filegroup",
1179 moduleTypeUnderTestFactory: android.FileGroupFactory,
1180 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1181 bp: `filegroup {
1182 name: "foo",
1183 srcs: ["a", "b"],
1184}`,
1185 expectedCount: 0,
1186 },
1187 {
1188 description: "explicitly available",
1189 moduleTypeUnderTest: "filegroup",
1190 moduleTypeUnderTestFactory: android.FileGroupFactory,
1191 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1192 bp: `filegroup {
1193 name: "foo",
1194 srcs: ["a", "b"],
1195 bazel_module: { bp2build_available: true },
1196}`,
1197 expectedCount: 1,
1198 },
1199 {
1200 description: "generates more than 1 target if needed",
1201 moduleTypeUnderTest: "custom",
1202 moduleTypeUnderTestFactory: customModuleFactory,
1203 moduleTypeUnderTestBp2BuildMutator: customBp2BuildMutatorFromStarlark,
1204 bp: `custom {
1205 name: "foo",
1206 bazel_module: { bp2build_available: true },
1207}`,
1208 expectedCount: 3,
1209 },
1210 }
1211
1212 dir := "."
1213 for _, testCase := range testCases {
1214 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
1215 ctx := android.NewTestContext(config)
1216 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
1217 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1218 ctx.RegisterForBazelConversion()
1219
1220 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
1221 android.FailIfErrored(t, errs)
1222 _, errs = ctx.ResolveDependencies(config)
1223 android.FailIfErrored(t, errs)
1224
Jingwen Chen164e0862021-02-19 00:48:40 -05001225 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -05001226 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001227 if actualCount := len(bazelTargets); actualCount != testCase.expectedCount {
1228 t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, testCase.expectedCount, actualCount)
1229 }
1230 }
1231}
Liz Kammerba3ea162021-02-17 13:22:03 -05001232
Jingwen Chen12b4c272021-03-10 02:05:59 -05001233func TestAllowlistingBp2buildTargetsWithConfig(t *testing.T) {
1234 testCases := []struct {
1235 moduleTypeUnderTest string
1236 moduleTypeUnderTestFactory android.ModuleFactory
1237 moduleTypeUnderTestBp2BuildMutator bp2buildMutator
1238 expectedCount map[string]int
1239 description string
1240 bp2buildConfig android.Bp2BuildConfig
1241 checkDir string
1242 fs map[string]string
1243 }{
1244 {
1245 description: "test bp2build config package and subpackages config",
1246 moduleTypeUnderTest: "filegroup",
1247 moduleTypeUnderTestFactory: android.FileGroupFactory,
1248 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1249 expectedCount: map[string]int{
1250 "migrated": 1,
1251 "migrated/but_not_really": 0,
1252 "migrated/but_not_really/but_really": 1,
1253 "not_migrated": 0,
1254 "also_not_migrated": 0,
1255 },
1256 bp2buildConfig: android.Bp2BuildConfig{
1257 "migrated": android.Bp2BuildDefaultTrueRecursively,
1258 "migrated/but_not_really": android.Bp2BuildDefaultFalse,
1259 "not_migrated": android.Bp2BuildDefaultFalse,
1260 },
1261 fs: map[string]string{
1262 "migrated/Android.bp": `filegroup { name: "a" }`,
1263 "migrated/but_not_really/Android.bp": `filegroup { name: "b" }`,
1264 "migrated/but_not_really/but_really/Android.bp": `filegroup { name: "c" }`,
1265 "not_migrated/Android.bp": `filegroup { name: "d" }`,
1266 "also_not_migrated/Android.bp": `filegroup { name: "e" }`,
1267 },
1268 },
1269 {
1270 description: "test bp2build config opt-in and opt-out",
1271 moduleTypeUnderTest: "filegroup",
1272 moduleTypeUnderTestFactory: android.FileGroupFactory,
1273 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1274 expectedCount: map[string]int{
1275 "package-opt-in": 2,
1276 "package-opt-in/subpackage": 0,
1277 "package-opt-out": 1,
1278 "package-opt-out/subpackage": 0,
1279 },
1280 bp2buildConfig: android.Bp2BuildConfig{
1281 "package-opt-in": android.Bp2BuildDefaultFalse,
1282 "package-opt-out": android.Bp2BuildDefaultTrueRecursively,
1283 },
1284 fs: map[string]string{
1285 "package-opt-in/Android.bp": `
1286filegroup { name: "opt-in-a" }
1287filegroup { name: "opt-in-b", bazel_module: { bp2build_available: true } }
1288filegroup { name: "opt-in-c", bazel_module: { bp2build_available: true } }
1289`,
1290
1291 "package-opt-in/subpackage/Android.bp": `
1292filegroup { name: "opt-in-d" } // parent package not configured to DefaultTrueRecursively
1293`,
1294
1295 "package-opt-out/Android.bp": `
1296filegroup { name: "opt-out-a" }
1297filegroup { name: "opt-out-b", bazel_module: { bp2build_available: false } }
1298filegroup { name: "opt-out-c", bazel_module: { bp2build_available: false } }
1299`,
1300
1301 "package-opt-out/subpackage/Android.bp": `
1302filegroup { name: "opt-out-g", bazel_module: { bp2build_available: false } }
1303filegroup { name: "opt-out-h", bazel_module: { bp2build_available: false } }
1304`,
1305 },
1306 },
1307 }
1308
1309 dir := "."
1310 for _, testCase := range testCases {
1311 fs := make(map[string][]byte)
1312 toParse := []string{
1313 "Android.bp",
1314 }
1315 for f, content := range testCase.fs {
1316 if strings.HasSuffix(f, "Android.bp") {
1317 toParse = append(toParse, f)
1318 }
1319 fs[f] = []byte(content)
1320 }
1321 config := android.TestConfig(buildDir, nil, "", fs)
1322 ctx := android.NewTestContext(config)
1323 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
1324 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1325 ctx.RegisterBp2BuildConfig(testCase.bp2buildConfig)
1326 ctx.RegisterForBazelConversion()
1327
1328 _, errs := ctx.ParseFileList(dir, toParse)
1329 android.FailIfErrored(t, errs)
1330 _, errs = ctx.ResolveDependencies(config)
1331 android.FailIfErrored(t, errs)
1332
1333 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
1334
1335 // For each directory, test that the expected number of generated targets is correct.
1336 for dir, expectedCount := range testCase.expectedCount {
1337 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
1338 if actualCount := len(bazelTargets); actualCount != expectedCount {
1339 t.Fatalf(
1340 "%s: Expected %d bazel target for %s package, got %d",
1341 testCase.description,
1342 expectedCount,
1343 dir,
1344 actualCount)
1345 }
1346
1347 }
1348 }
1349}
1350
Liz Kammerba3ea162021-02-17 13:22:03 -05001351func TestCombineBuildFilesBp2buildTargets(t *testing.T) {
1352 testCases := []struct {
1353 description string
1354 moduleTypeUnderTest string
1355 moduleTypeUnderTestFactory android.ModuleFactory
1356 moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
1357 preArchMutators []android.RegisterMutatorFunc
1358 depsMutators []android.RegisterMutatorFunc
1359 bp string
1360 expectedBazelTargets []string
1361 fs map[string]string
1362 dir string
1363 }{
1364 {
1365 description: "filegroup bazel_module.label",
1366 moduleTypeUnderTest: "filegroup",
1367 moduleTypeUnderTestFactory: android.FileGroupFactory,
1368 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1369 bp: `filegroup {
1370 name: "fg_foo",
1371 bazel_module: { label: "//other:fg_foo" },
1372}`,
1373 expectedBazelTargets: []string{
1374 `// BUILD file`,
1375 },
1376 fs: map[string]string{
1377 "other/BUILD.bazel": `// BUILD file`,
1378 },
1379 },
1380 {
1381 description: "multiple bazel_module.label same BUILD",
1382 moduleTypeUnderTest: "filegroup",
1383 moduleTypeUnderTestFactory: android.FileGroupFactory,
1384 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1385 bp: `filegroup {
1386 name: "fg_foo",
1387 bazel_module: { label: "//other:fg_foo" },
1388}
1389
1390filegroup {
1391 name: "foo",
1392 bazel_module: { label: "//other:foo" },
1393}`,
1394 expectedBazelTargets: []string{
1395 `// BUILD file`,
1396 },
1397 fs: map[string]string{
1398 "other/BUILD.bazel": `// BUILD file`,
1399 },
1400 },
1401 {
1402 description: "filegroup bazel_module.label and bp2build",
1403 moduleTypeUnderTest: "filegroup",
1404 moduleTypeUnderTestFactory: android.FileGroupFactory,
1405 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1406 bp: `filegroup {
1407 name: "fg_foo",
1408 bazel_module: {
1409 label: "//other:fg_foo",
1410 bp2build_available: true,
1411 },
1412}`,
1413 expectedBazelTargets: []string{
1414 `filegroup(
1415 name = "fg_foo",
1416)`,
1417 `// BUILD file`,
1418 },
1419 fs: map[string]string{
1420 "other/BUILD.bazel": `// BUILD file`,
1421 },
1422 },
1423 {
1424 description: "filegroup bazel_module.label and filegroup bp2build",
1425 moduleTypeUnderTest: "filegroup",
1426 moduleTypeUnderTestFactory: android.FileGroupFactory,
1427 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1428 bp: `filegroup {
1429 name: "fg_foo",
1430 bazel_module: {
1431 label: "//other:fg_foo",
1432 },
1433}
1434
1435filegroup {
1436 name: "fg_bar",
1437 bazel_module: {
1438 bp2build_available: true,
1439 },
1440}`,
1441 expectedBazelTargets: []string{
1442 `filegroup(
1443 name = "fg_bar",
1444)`,
1445 `// BUILD file`,
1446 },
1447 fs: map[string]string{
1448 "other/BUILD.bazel": `// BUILD file`,
1449 },
1450 },
1451 }
1452
1453 dir := "."
1454 for _, testCase := range testCases {
Jingwen Chen49109762021-05-25 05:16:48 +00001455 t.Run(testCase.description, func(t *testing.T) {
1456 fs := make(map[string][]byte)
1457 toParse := []string{
1458 "Android.bp",
Liz Kammerba3ea162021-02-17 13:22:03 -05001459 }
Jingwen Chen49109762021-05-25 05:16:48 +00001460 for f, content := range testCase.fs {
1461 if strings.HasSuffix(f, "Android.bp") {
1462 toParse = append(toParse, f)
1463 }
1464 fs[f] = []byte(content)
1465 }
1466 config := android.TestConfig(buildDir, nil, testCase.bp, fs)
1467 ctx := android.NewTestContext(config)
1468 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
1469 for _, m := range testCase.depsMutators {
1470 ctx.DepsBp2BuildMutators(m)
1471 }
1472 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1473 ctx.RegisterForBazelConversion()
Liz Kammerba3ea162021-02-17 13:22:03 -05001474
Jingwen Chen49109762021-05-25 05:16:48 +00001475 _, errs := ctx.ParseFileList(dir, toParse)
1476 if errored(t, testCase.description, errs) {
1477 return
1478 }
1479 _, errs = ctx.ResolveDependencies(config)
1480 if errored(t, testCase.description, errs) {
1481 return
1482 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001483
Jingwen Chen49109762021-05-25 05:16:48 +00001484 checkDir := dir
1485 if testCase.dir != "" {
1486 checkDir = testCase.dir
1487 }
1488 bazelTargets := generateBazelTargetsForDir(NewCodegenContext(config, *ctx.Context, Bp2Build), checkDir)
1489 bazelTargets.sort()
1490 actualCount := len(bazelTargets)
1491 expectedCount := len(testCase.expectedBazelTargets)
1492 if actualCount != expectedCount {
1493 t.Errorf("Expected %d bazel target, got %d\n%s", expectedCount, actualCount, bazelTargets)
1494 }
1495 if !strings.Contains(bazelTargets.String(), "Section: Handcrafted targets. ") {
1496 t.Errorf("Expected string representation of bazelTargets to contain handcrafted section header.")
1497 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001498 for i, target := range bazelTargets {
Jingwen Chen49109762021-05-25 05:16:48 +00001499 actualContent := target.content
1500 expectedContent := testCase.expectedBazelTargets[i]
1501 if expectedContent != actualContent {
Liz Kammerba3ea162021-02-17 13:22:03 -05001502 t.Errorf(
Jingwen Chen49109762021-05-25 05:16:48 +00001503 "Expected generated Bazel target to be '%s', got '%s'",
1504 expectedContent,
1505 actualContent,
Liz Kammerba3ea162021-02-17 13:22:03 -05001506 )
1507 }
1508 }
Jingwen Chen49109762021-05-25 05:16:48 +00001509 })
Liz Kammerba3ea162021-02-17 13:22:03 -05001510 }
1511}
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001512
1513func TestGlobExcludeSrcs(t *testing.T) {
1514 testCases := []struct {
1515 description string
1516 moduleTypeUnderTest string
1517 moduleTypeUnderTestFactory android.ModuleFactory
1518 moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
1519 bp string
1520 expectedBazelTargets []string
1521 fs map[string]string
1522 dir string
1523 }{
1524 {
1525 description: "filegroup top level exclude_srcs",
1526 moduleTypeUnderTest: "filegroup",
1527 moduleTypeUnderTestFactory: android.FileGroupFactory,
1528 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1529 bp: `filegroup {
1530 name: "fg_foo",
1531 srcs: ["**/*.txt"],
1532 exclude_srcs: ["c.txt"],
1533 bazel_module: { bp2build_available: true },
1534}`,
1535 expectedBazelTargets: []string{`filegroup(
1536 name = "fg_foo",
1537 srcs = [
1538 "//dir:e.txt",
1539 "//dir:f.txt",
1540 "a.txt",
1541 "b.txt",
1542 ],
1543)`,
1544 },
1545 fs: map[string]string{
1546 "a.txt": "",
1547 "b.txt": "",
1548 "c.txt": "",
1549 "dir/Android.bp": "",
1550 "dir/e.txt": "",
1551 "dir/f.txt": "",
1552 },
1553 },
1554 {
1555 description: "filegroup in subdir exclude_srcs",
1556 moduleTypeUnderTest: "filegroup",
1557 moduleTypeUnderTestFactory: android.FileGroupFactory,
1558 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1559 bp: "",
1560 dir: "dir",
1561 fs: map[string]string{
1562 "dir/Android.bp": `filegroup {
1563 name: "fg_foo",
1564 srcs: ["**/*.txt"],
1565 exclude_srcs: ["b.txt"],
1566 bazel_module: { bp2build_available: true },
1567}
1568`,
1569 "dir/a.txt": "",
1570 "dir/b.txt": "",
1571 "dir/subdir/Android.bp": "",
1572 "dir/subdir/e.txt": "",
1573 "dir/subdir/f.txt": "",
1574 },
1575 expectedBazelTargets: []string{`filegroup(
1576 name = "fg_foo",
1577 srcs = [
1578 "//dir/subdir:e.txt",
1579 "//dir/subdir:f.txt",
1580 "a.txt",
1581 ],
1582)`,
1583 },
1584 },
1585 }
1586
1587 dir := "."
1588 for _, testCase := range testCases {
1589 fs := make(map[string][]byte)
1590 toParse := []string{
1591 "Android.bp",
1592 }
1593 for f, content := range testCase.fs {
1594 if strings.HasSuffix(f, "Android.bp") {
1595 toParse = append(toParse, f)
1596 }
1597 fs[f] = []byte(content)
1598 }
1599 config := android.TestConfig(buildDir, nil, testCase.bp, fs)
1600 ctx := android.NewTestContext(config)
1601 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
1602 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1603 ctx.RegisterForBazelConversion()
1604
1605 _, errs := ctx.ParseFileList(dir, toParse)
Rupert Shuttleworth06559d02021-05-19 09:14:26 -04001606 if errored(t, testCase.description, errs) {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001607 continue
1608 }
1609 _, errs = ctx.ResolveDependencies(config)
Rupert Shuttleworth06559d02021-05-19 09:14:26 -04001610 if errored(t, testCase.description, errs) {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001611 continue
1612 }
1613
1614 checkDir := dir
1615 if testCase.dir != "" {
1616 checkDir = testCase.dir
1617 }
1618 bazelTargets := generateBazelTargetsForDir(NewCodegenContext(config, *ctx.Context, Bp2Build), checkDir)
1619 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
1620 t.Errorf("%s: Expected %d bazel target, got %d\n%s", testCase.description, expectedCount, actualCount, bazelTargets)
1621 } else {
1622 for i, target := range bazelTargets {
1623 if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
1624 t.Errorf(
1625 "%s: Expected generated Bazel target to be '%s', got '%s'",
1626 testCase.description,
1627 w,
1628 g,
1629 )
1630 }
1631 }
1632 }
1633 }
1634}