blob: e5dbda605d12a52f52a87ea79ce2859542b9d53e [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 {
Liz Kammerd366c902021-06-03 13:43:01 -040026 description string
Liz Kammer2dd9ca42020-11-25 16:06:39 -080027 bp string
28 expectedBazelTarget string
29 }{
30 {
Liz Kammerd366c902021-06-03 13:43:01 -040031 description: "only name",
Jingwen Chenb4628eb2021-04-08 14:40:57 +000032 bp: `custom { name: "foo" }
Liz Kammerd366c902021-06-03 13:43:01 -040033 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080034 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 ],
Liz Kammerd366c902021-06-03 13:43:01 -040041 bool_prop = False,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080042)`,
43 },
44 {
Liz Kammerd366c902021-06-03 13:43:01 -040045 description: "handles bool",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080046 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -040047 name: "foo",
48 bool_prop: true,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080049}
Liz Kammerd366c902021-06-03 13:43:01 -040050 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080051 expectedBazelTarget: `soong_module(
52 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050053 soong_module_name = "foo",
54 soong_module_type = "custom",
55 soong_module_variant = "",
56 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080057 ],
Liz Kammerd366c902021-06-03 13:43:01 -040058 bool_prop = True,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080059)`,
60 },
61 {
Liz Kammerd366c902021-06-03 13:43:01 -040062 description: "string escaping",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080063 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -040064 name: "foo",
65 owner: "a_string_with\"quotes\"_and_\\backslashes\\\\",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080066}
Liz Kammerd366c902021-06-03 13:43:01 -040067 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080068 expectedBazelTarget: `soong_module(
69 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050070 soong_module_name = "foo",
71 soong_module_type = "custom",
72 soong_module_variant = "",
73 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080074 ],
Liz Kammerd366c902021-06-03 13:43:01 -040075 bool_prop = False,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080076 owner = "a_string_with\"quotes\"_and_\\backslashes\\\\",
77)`,
78 },
79 {
Liz Kammerd366c902021-06-03 13:43:01 -040080 description: "single item string list",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080081 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -040082 name: "foo",
83 required: ["bar"],
Liz Kammer2dd9ca42020-11-25 16:06:39 -080084}
Liz Kammerd366c902021-06-03 13:43:01 -040085 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080086 expectedBazelTarget: `soong_module(
87 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050088 soong_module_name = "foo",
89 soong_module_type = "custom",
90 soong_module_variant = "",
91 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080092 ],
Liz Kammerd366c902021-06-03 13:43:01 -040093 bool_prop = False,
Jingwen Chenb4628eb2021-04-08 14:40:57 +000094 required = ["bar"],
Liz Kammer2dd9ca42020-11-25 16:06:39 -080095)`,
96 },
97 {
Liz Kammerd366c902021-06-03 13:43:01 -040098 description: "list of strings",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080099 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -0400100 name: "foo",
101 target_required: ["qux", "bazqux"],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800102}
Liz Kammerd366c902021-06-03 13:43:01 -0400103 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800104 expectedBazelTarget: `soong_module(
105 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500106 soong_module_name = "foo",
107 soong_module_type = "custom",
108 soong_module_variant = "",
109 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800110 ],
Liz Kammerd366c902021-06-03 13:43:01 -0400111 bool_prop = False,
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800112 target_required = [
113 "qux",
114 "bazqux",
115 ],
116)`,
117 },
118 {
Liz Kammerd366c902021-06-03 13:43:01 -0400119 description: "dist/dists",
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800120 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -0400121 name: "foo",
122 dist: {
123 targets: ["goal_foo"],
124 tag: ".foo",
125 },
126 dists: [{
127 targets: ["goal_bar"],
128 tag: ".bar",
129 }],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800130}
Liz Kammerd366c902021-06-03 13:43:01 -0400131 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800132 expectedBazelTarget: `soong_module(
133 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500134 soong_module_name = "foo",
135 soong_module_type = "custom",
136 soong_module_variant = "",
137 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800138 ],
Liz Kammerd366c902021-06-03 13:43:01 -0400139 bool_prop = False,
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800140 dist = {
141 "tag": ".foo",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000142 "targets": ["goal_foo"],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800143 },
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000144 dists = [{
145 "tag": ".bar",
146 "targets": ["goal_bar"],
147 }],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800148)`,
149 },
150 {
Liz Kammerd366c902021-06-03 13:43:01 -0400151 description: "put it together",
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800152 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -0400153 name: "foo",
154 required: ["bar"],
155 target_required: ["qux", "bazqux"],
156 bool_prop: true,
157 owner: "custom_owner",
158 dists: [
159 {
160 tag: ".tag",
161 targets: ["my_goal"],
162 },
163 ],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800164}
Liz Kammerd366c902021-06-03 13:43:01 -0400165 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800166 expectedBazelTarget: `soong_module(
167 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500168 soong_module_name = "foo",
169 soong_module_type = "custom",
170 soong_module_variant = "",
171 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800172 ],
Liz Kammerd366c902021-06-03 13:43:01 -0400173 bool_prop = True,
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000174 dists = [{
175 "tag": ".tag",
176 "targets": ["my_goal"],
177 }],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800178 owner = "custom_owner",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000179 required = ["bar"],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800180 target_required = [
181 "qux",
182 "bazqux",
183 ],
184)`,
185 },
186 }
187
188 dir := "."
189 for _, testCase := range testCases {
Liz Kammerd366c902021-06-03 13:43:01 -0400190 t.Run(testCase.description, func(t *testing.T) {
191 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
192 ctx := android.NewTestContext(config)
Jingwen Chen164e0862021-02-19 00:48:40 -0500193
Liz Kammerd366c902021-06-03 13:43:01 -0400194 ctx.RegisterModuleType("custom", customModuleFactory)
195 ctx.Register()
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800196
Liz Kammerd366c902021-06-03 13:43:01 -0400197 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
198 android.FailIfErrored(t, errs)
199 _, errs = ctx.PrepareBuildActions(config)
200 android.FailIfErrored(t, errs)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800201
Liz Kammerd366c902021-06-03 13:43:01 -0400202 codegenCtx := NewCodegenContext(config, *ctx.Context, QueryView)
203 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
204 if actualCount, expectedCount := len(bazelTargets), 1; actualCount != expectedCount {
205 t.Fatalf("Expected %d bazel target, got %d", expectedCount, actualCount)
206 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800207
Liz Kammerd366c902021-06-03 13:43:01 -0400208 actualBazelTarget := bazelTargets[0]
209 if actualBazelTarget.content != testCase.expectedBazelTarget {
210 t.Errorf(
211 "Expected generated Bazel target to be '%s', got '%s'",
212 testCase.expectedBazelTarget,
213 actualBazelTarget.content,
214 )
215 }
216 })
Jingwen Chen73850672020-12-14 08:25:34 -0500217 }
218}
219
220func TestGenerateBazelTargetModules(t *testing.T) {
221 testCases := []struct {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400222 name string
223 bp string
224 expectedBazelTargets []string
Jingwen Chen73850672020-12-14 08:25:34 -0500225 }{
226 {
227 bp: `custom {
228 name: "foo",
229 string_list_prop: ["a", "b"],
230 string_prop: "a",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500231 bazel_module: { bp2build_available: true },
Jingwen Chen73850672020-12-14 08:25:34 -0500232}`,
Liz Kammer4562a3b2021-04-21 18:15:34 -0400233 expectedBazelTargets: []string{`custom(
Jingwen Chen73850672020-12-14 08:25:34 -0500234 name = "foo",
235 string_list_prop = [
236 "a",
237 "b",
238 ],
239 string_prop = "a",
240)`,
Liz Kammer4562a3b2021-04-21 18:15:34 -0400241 },
Jingwen Chen73850672020-12-14 08:25:34 -0500242 },
Jingwen Chen58a12b82021-03-30 13:08:36 +0000243 {
244 bp: `custom {
245 name: "control_characters",
246 string_list_prop: ["\t", "\n"],
247 string_prop: "a\t\n\r",
248 bazel_module: { bp2build_available: true },
249}`,
Liz Kammer4562a3b2021-04-21 18:15:34 -0400250 expectedBazelTargets: []string{`custom(
Jingwen Chen58a12b82021-03-30 13:08:36 +0000251 name = "control_characters",
252 string_list_prop = [
253 "\t",
254 "\n",
255 ],
256 string_prop = "a\t\n\r",
257)`,
Liz Kammer4562a3b2021-04-21 18:15:34 -0400258 },
259 },
260 {
261 bp: `custom {
262 name: "has_dep",
263 arch_paths: [":dep"],
264 bazel_module: { bp2build_available: true },
265}
266
267custom {
268 name: "dep",
269 arch_paths: ["abc"],
270 bazel_module: { bp2build_available: true },
271}`,
272 expectedBazelTargets: []string{`custom(
273 name = "dep",
274 arch_paths = ["abc"],
275)`,
276 `custom(
277 name = "has_dep",
278 arch_paths = [":dep"],
279)`,
280 },
281 },
282 {
283 bp: `custom {
284 name: "arch_paths",
285 arch: {
286 x86: {
287 arch_paths: ["abc"],
288 },
289 },
290 bazel_module: { bp2build_available: true },
291}`,
292 expectedBazelTargets: []string{`custom(
293 name = "arch_paths",
294 arch_paths = select({
295 "//build/bazel/platforms/arch:x86": ["abc"],
296 "//conditions:default": [],
297 }),
298)`,
299 },
300 },
301 {
302 bp: `custom {
303 name: "has_dep",
304 arch: {
305 x86: {
306 arch_paths: [":dep"],
307 },
308 },
309 bazel_module: { bp2build_available: true },
310}
311
312custom {
313 name: "dep",
314 arch_paths: ["abc"],
315 bazel_module: { bp2build_available: true },
316}`,
317 expectedBazelTargets: []string{`custom(
318 name = "dep",
319 arch_paths = ["abc"],
320)`,
321 `custom(
322 name = "has_dep",
323 arch_paths = select({
324 "//build/bazel/platforms/arch:x86": [":dep"],
325 "//conditions:default": [],
326 }),
327)`,
328 },
Jingwen Chen58a12b82021-03-30 13:08:36 +0000329 },
Jingwen Chen73850672020-12-14 08:25:34 -0500330 }
331
332 dir := "."
333 for _, testCase := range testCases {
334 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
335 ctx := android.NewTestContext(config)
Jingwen Chen164e0862021-02-19 00:48:40 -0500336
Jingwen Chen73850672020-12-14 08:25:34 -0500337 ctx.RegisterModuleType("custom", customModuleFactory)
338 ctx.RegisterBp2BuildMutator("custom", customBp2BuildMutator)
339 ctx.RegisterForBazelConversion()
340
341 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
Rupert Shuttleworth06559d02021-05-19 09:14:26 -0400342 if errored(t, "", errs) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500343 continue
344 }
Jingwen Chen73850672020-12-14 08:25:34 -0500345 _, errs = ctx.ResolveDependencies(config)
Rupert Shuttleworth06559d02021-05-19 09:14:26 -0400346 if errored(t, "", errs) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500347 continue
348 }
Jingwen Chen73850672020-12-14 08:25:34 -0500349
Jingwen Chen164e0862021-02-19 00:48:40 -0500350 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -0500351 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
Jingwen Chen164e0862021-02-19 00:48:40 -0500352
Liz Kammer4562a3b2021-04-21 18:15:34 -0400353 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
Liz Kammer356f7d42021-01-26 09:18:53 -0500354 t.Errorf("Expected %d bazel target, got %d", expectedCount, actualCount)
355 } else {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400356 for i, expectedBazelTarget := range testCase.expectedBazelTargets {
357 actualBazelTarget := bazelTargets[i]
358 if actualBazelTarget.content != expectedBazelTarget {
359 t.Errorf(
360 "Expected generated Bazel target to be '%s', got '%s'",
361 expectedBazelTarget,
362 actualBazelTarget.content,
363 )
364 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500365 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800366 }
367 }
368}
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500369
Jingwen Chen40067de2021-01-26 21:58:43 -0500370func TestLoadStatements(t *testing.T) {
371 testCases := []struct {
372 bazelTargets BazelTargets
373 expectedLoadStatements string
374 }{
375 {
376 bazelTargets: BazelTargets{
377 BazelTarget{
378 name: "foo",
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_library",
395 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
396 },
397 },
398 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "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 },
413 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")`,
414 },
415 {
416 bazelTargets: BazelTargets{
417 BazelTarget{
418 name: "foo",
419 ruleClass: "cc_library",
420 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
421 },
422 BazelTarget{
423 name: "bar",
424 ruleClass: "cc_binary",
425 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
426 },
427 BazelTarget{
428 name: "baz",
429 ruleClass: "java_binary",
430 bzlLoadLocation: "//build/bazel/rules:java.bzl",
431 },
432 },
433 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")
434load("//build/bazel/rules:java.bzl", "java_binary")`,
435 },
436 {
437 bazelTargets: BazelTargets{
438 BazelTarget{
439 name: "foo",
440 ruleClass: "cc_binary",
441 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
442 },
443 BazelTarget{
444 name: "bar",
445 ruleClass: "java_binary",
446 bzlLoadLocation: "//build/bazel/rules:java.bzl",
447 },
448 BazelTarget{
449 name: "baz",
450 ruleClass: "genrule",
451 // Note: no bzlLoadLocation for native rules
452 },
453 },
454 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary")
455load("//build/bazel/rules:java.bzl", "java_binary")`,
456 },
457 }
458
459 for _, testCase := range testCases {
460 actual := testCase.bazelTargets.LoadStatements()
461 expected := testCase.expectedLoadStatements
462 if actual != expected {
463 t.Fatalf("Expected load statements to be %s, got %s", expected, actual)
464 }
465 }
466
467}
468
469func TestGenerateBazelTargetModules_OneToMany_LoadedFromStarlark(t *testing.T) {
470 testCases := []struct {
471 bp string
472 expectedBazelTarget string
473 expectedBazelTargetCount int
474 expectedLoadStatements string
475 }{
476 {
477 bp: `custom {
478 name: "bar",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500479 bazel_module: { bp2build_available: true },
Jingwen Chen40067de2021-01-26 21:58:43 -0500480}`,
481 expectedBazelTarget: `my_library(
482 name = "bar",
483)
484
485my_proto_library(
486 name = "bar_my_proto_library_deps",
487)
488
489proto_library(
490 name = "bar_proto_library_deps",
491)`,
492 expectedBazelTargetCount: 3,
493 expectedLoadStatements: `load("//build/bazel/rules:proto.bzl", "my_proto_library", "proto_library")
494load("//build/bazel/rules:rules.bzl", "my_library")`,
495 },
496 }
497
498 dir := "."
499 for _, testCase := range testCases {
500 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
501 ctx := android.NewTestContext(config)
502 ctx.RegisterModuleType("custom", customModuleFactory)
Jingwen Chen12b4c272021-03-10 02:05:59 -0500503 ctx.RegisterBp2BuildMutator("custom", customBp2BuildMutatorFromStarlark)
Jingwen Chen40067de2021-01-26 21:58:43 -0500504 ctx.RegisterForBazelConversion()
505
506 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
507 android.FailIfErrored(t, errs)
508 _, errs = ctx.ResolveDependencies(config)
509 android.FailIfErrored(t, errs)
510
Jingwen Chen164e0862021-02-19 00:48:40 -0500511 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -0500512 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
Jingwen Chen40067de2021-01-26 21:58:43 -0500513 if actualCount := len(bazelTargets); actualCount != testCase.expectedBazelTargetCount {
514 t.Fatalf("Expected %d bazel target, got %d", testCase.expectedBazelTargetCount, actualCount)
515 }
516
517 actualBazelTargets := bazelTargets.String()
518 if actualBazelTargets != testCase.expectedBazelTarget {
519 t.Errorf(
520 "Expected generated Bazel target to be '%s', got '%s'",
521 testCase.expectedBazelTarget,
522 actualBazelTargets,
523 )
524 }
525
526 actualLoadStatements := bazelTargets.LoadStatements()
527 if actualLoadStatements != testCase.expectedLoadStatements {
528 t.Errorf(
529 "Expected generated load statements to be '%s', got '%s'",
530 testCase.expectedLoadStatements,
531 actualLoadStatements,
532 )
533 }
534 }
535}
536
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500537func TestModuleTypeBp2Build(t *testing.T) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500538 otherGenruleBp := map[string]string{
539 "other/Android.bp": `genrule {
540 name: "foo.tool",
541 out: ["foo_tool.out"],
542 srcs: ["foo_tool.in"],
543 cmd: "cp $(in) $(out)",
544}
545genrule {
546 name: "other.tool",
547 out: ["other_tool.out"],
548 srcs: ["other_tool.in"],
549 cmd: "cp $(in) $(out)",
550}`,
551 }
552
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500553 testCases := []struct {
Liz Kammer356f7d42021-01-26 09:18:53 -0500554 description string
Jingwen Chena42d6412021-01-26 21:57:27 -0500555 moduleTypeUnderTest string
556 moduleTypeUnderTestFactory android.ModuleFactory
557 moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
Liz Kammer356f7d42021-01-26 09:18:53 -0500558 preArchMutators []android.RegisterMutatorFunc
Jingwen Chena42d6412021-01-26 21:57:27 -0500559 bp string
Liz Kammer356f7d42021-01-26 09:18:53 -0500560 expectedBazelTargets []string
561 fs map[string]string
562 dir string
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500563 }{
564 {
Liz Kammerebfcf672021-02-16 15:00:05 -0500565 description: "filegroup with does not specify srcs",
566 moduleTypeUnderTest: "filegroup",
567 moduleTypeUnderTestFactory: android.FileGroupFactory,
568 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
569 bp: `filegroup {
570 name: "fg_foo",
571 bazel_module: { bp2build_available: true },
572}`,
573 expectedBazelTargets: []string{
574 `filegroup(
575 name = "fg_foo",
576)`,
577 },
578 },
579 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500580 description: "filegroup with no srcs",
581 moduleTypeUnderTest: "filegroup",
582 moduleTypeUnderTestFactory: android.FileGroupFactory,
583 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500584 bp: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500585 name: "fg_foo",
586 srcs: [],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500587 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500588}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500589 expectedBazelTargets: []string{
590 `filegroup(
591 name = "fg_foo",
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500592)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500593 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500594 },
595 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500596 description: "filegroup with srcs",
597 moduleTypeUnderTest: "filegroup",
598 moduleTypeUnderTestFactory: android.FileGroupFactory,
599 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500600 bp: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500601 name: "fg_foo",
602 srcs: ["a", "b"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500603 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500604}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500605 expectedBazelTargets: []string{`filegroup(
606 name = "fg_foo",
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500607 srcs = [
608 "a",
609 "b",
610 ],
611)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500612 },
613 },
614 {
615 description: "filegroup with excludes srcs",
616 moduleTypeUnderTest: "filegroup",
617 moduleTypeUnderTestFactory: android.FileGroupFactory,
618 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
619 bp: `filegroup {
620 name: "fg_foo",
621 srcs: ["a", "b"],
622 exclude_srcs: ["a"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500623 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500624}`,
625 expectedBazelTargets: []string{`filegroup(
626 name = "fg_foo",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000627 srcs = ["b"],
Liz Kammer356f7d42021-01-26 09:18:53 -0500628)`,
629 },
630 },
631 {
632 description: "filegroup with glob",
633 moduleTypeUnderTest: "filegroup",
634 moduleTypeUnderTestFactory: android.FileGroupFactory,
635 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
636 bp: `filegroup {
637 name: "foo",
638 srcs: ["**/*.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500639 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500640}`,
641 expectedBazelTargets: []string{`filegroup(
642 name = "foo",
643 srcs = [
644 "other/a.txt",
645 "other/b.txt",
646 "other/subdir/a.txt",
647 ],
648)`,
649 },
650 fs: map[string]string{
651 "other/a.txt": "",
652 "other/b.txt": "",
653 "other/subdir/a.txt": "",
654 "other/file": "",
655 },
656 },
657 {
658 description: "filegroup with glob in subdir",
659 moduleTypeUnderTest: "filegroup",
660 moduleTypeUnderTestFactory: android.FileGroupFactory,
661 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
662 bp: `filegroup {
663 name: "foo",
664 srcs: ["a.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500665 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500666}`,
667 dir: "other",
668 expectedBazelTargets: []string{`filegroup(
669 name = "fg_foo",
670 srcs = [
671 "a.txt",
672 "b.txt",
673 "subdir/a.txt",
674 ],
675)`,
676 },
677 fs: map[string]string{
678 "other/Android.bp": `filegroup {
679 name: "fg_foo",
680 srcs: ["**/*.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500681 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500682}`,
683 "other/a.txt": "",
684 "other/b.txt": "",
685 "other/subdir/a.txt": "",
686 "other/file": "",
687 },
688 },
689 {
690 description: "depends_on_other_dir_module",
691 moduleTypeUnderTest: "filegroup",
692 moduleTypeUnderTestFactory: android.FileGroupFactory,
693 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
694 bp: `filegroup {
695 name: "foobar",
696 srcs: [
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000697 ":foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500698 "c",
699 ],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500700 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500701}`,
702 expectedBazelTargets: []string{`filegroup(
703 name = "foobar",
704 srcs = [
705 "//other:foo",
706 "c",
707 ],
708)`,
709 },
710 fs: map[string]string{
711 "other/Android.bp": `filegroup {
712 name: "foo",
713 srcs: ["a", "b"],
714}`,
715 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500716 },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500717 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500718 description: "genrule with command line variable replacements",
719 moduleTypeUnderTest: "genrule",
720 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
721 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Jingwen Chen316e07c2020-12-14 09:09:52 -0500722 bp: `genrule {
Liz Kammer356f7d42021-01-26 09:18:53 -0500723 name: "foo.tool",
724 out: ["foo_tool.out"],
725 srcs: ["foo_tool.in"],
726 cmd: "cp $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500727 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500728}
729
730genrule {
Jingwen Chen316e07c2020-12-14 09:09:52 -0500731 name: "foo",
732 out: ["foo.out"],
733 srcs: ["foo.in"],
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500734 tools: [":foo.tool"],
735 cmd: "$(location :foo.tool) --genDir=$(genDir) arg $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500736 bazel_module: { bp2build_available: true },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500737}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500738 expectedBazelTargets: []string{
739 `genrule(
Jingwen Chen316e07c2020-12-14 09:09:52 -0500740 name = "foo",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500741 cmd = "$(location :foo.tool) --genDir=$(GENDIR) arg $(SRCS) $(OUTS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000742 outs = ["foo.out"],
743 srcs = ["foo.in"],
744 tools = [":foo.tool"],
Jingwen Chen316e07c2020-12-14 09:09:52 -0500745)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500746 `genrule(
747 name = "foo.tool",
748 cmd = "cp $(SRCS) $(OUTS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000749 outs = ["foo_tool.out"],
750 srcs = ["foo_tool.in"],
Liz Kammer356f7d42021-01-26 09:18:53 -0500751)`,
752 },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500753 },
754 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500755 description: "genrule using $(locations :label)",
756 moduleTypeUnderTest: "genrule",
757 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
758 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Jingwen Chen316e07c2020-12-14 09:09:52 -0500759 bp: `genrule {
Liz Kammer356f7d42021-01-26 09:18:53 -0500760 name: "foo.tools",
761 out: ["foo_tool.out", "foo_tool2.out"],
762 srcs: ["foo_tool.in"],
763 cmd: "cp $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500764 bazel_module: { bp2build_available: true },
765}
Liz Kammer356f7d42021-01-26 09:18:53 -0500766
767genrule {
Jingwen Chen316e07c2020-12-14 09:09:52 -0500768 name: "foo",
769 out: ["foo.out"],
770 srcs: ["foo.in"],
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500771 tools: [":foo.tools"],
772 cmd: "$(locations :foo.tools) -s $(out) $(in)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500773 bazel_module: { bp2build_available: true },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500774}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500775 expectedBazelTargets: []string{`genrule(
Jingwen Chen316e07c2020-12-14 09:09:52 -0500776 name = "foo",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500777 cmd = "$(locations :foo.tools) -s $(OUTS) $(SRCS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000778 outs = ["foo.out"],
779 srcs = ["foo.in"],
780 tools = [":foo.tools"],
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500781)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500782 `genrule(
783 name = "foo.tools",
784 cmd = "cp $(SRCS) $(OUTS)",
785 outs = [
786 "foo_tool.out",
787 "foo_tool2.out",
788 ],
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000789 srcs = ["foo_tool.in"],
Liz Kammer356f7d42021-01-26 09:18:53 -0500790)`,
791 },
792 },
793 {
794 description: "genrule using $(locations //absolute:label)",
795 moduleTypeUnderTest: "genrule",
796 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
797 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Liz Kammer356f7d42021-01-26 09:18:53 -0500798 bp: `genrule {
799 name: "foo",
800 out: ["foo.out"],
801 srcs: ["foo.in"],
802 tool_files: [":foo.tool"],
803 cmd: "$(locations :foo.tool) -s $(out) $(in)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500804 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500805}`,
806 expectedBazelTargets: []string{`genrule(
807 name = "foo",
808 cmd = "$(locations //other:foo.tool) -s $(OUTS) $(SRCS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000809 outs = ["foo.out"],
810 srcs = ["foo.in"],
811 tools = ["//other:foo.tool"],
Liz Kammer356f7d42021-01-26 09:18:53 -0500812)`,
813 },
814 fs: otherGenruleBp,
815 },
816 {
817 description: "genrule srcs using $(locations //absolute:label)",
818 moduleTypeUnderTest: "genrule",
819 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
820 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Liz Kammer356f7d42021-01-26 09:18:53 -0500821 bp: `genrule {
822 name: "foo",
823 out: ["foo.out"],
824 srcs: [":other.tool"],
825 tool_files: [":foo.tool"],
826 cmd: "$(locations :foo.tool) -s $(out) $(location :other.tool)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500827 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500828}`,
829 expectedBazelTargets: []string{`genrule(
830 name = "foo",
831 cmd = "$(locations //other:foo.tool) -s $(OUTS) $(location //other:other.tool)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000832 outs = ["foo.out"],
833 srcs = ["//other:other.tool"],
834 tools = ["//other:foo.tool"],
Liz Kammer356f7d42021-01-26 09:18:53 -0500835)`,
836 },
837 fs: otherGenruleBp,
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500838 },
839 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500840 description: "genrule using $(location) label should substitute first tool label automatically",
841 moduleTypeUnderTest: "genrule",
842 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
843 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500844 bp: `genrule {
845 name: "foo",
846 out: ["foo.out"],
847 srcs: ["foo.in"],
848 tool_files: [":foo.tool", ":other.tool"],
849 cmd: "$(location) -s $(out) $(in)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500850 bazel_module: { bp2build_available: true },
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500851}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500852 expectedBazelTargets: []string{`genrule(
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500853 name = "foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500854 cmd = "$(location //other:foo.tool) -s $(OUTS) $(SRCS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000855 outs = ["foo.out"],
856 srcs = ["foo.in"],
Jingwen Chen316e07c2020-12-14 09:09:52 -0500857 tools = [
Liz Kammer356f7d42021-01-26 09:18:53 -0500858 "//other:foo.tool",
859 "//other:other.tool",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500860 ],
861)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500862 },
863 fs: otherGenruleBp,
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500864 },
865 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500866 description: "genrule using $(locations) label should substitute first tool label automatically",
867 moduleTypeUnderTest: "genrule",
868 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
869 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500870 bp: `genrule {
871 name: "foo",
872 out: ["foo.out"],
873 srcs: ["foo.in"],
874 tools: [":foo.tool", ":other.tool"],
875 cmd: "$(locations) -s $(out) $(in)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500876 bazel_module: { bp2build_available: true },
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500877}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500878 expectedBazelTargets: []string{`genrule(
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500879 name = "foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500880 cmd = "$(locations //other:foo.tool) -s $(OUTS) $(SRCS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000881 outs = ["foo.out"],
882 srcs = ["foo.in"],
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500883 tools = [
Liz Kammer356f7d42021-01-26 09:18:53 -0500884 "//other:foo.tool",
885 "//other:other.tool",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500886 ],
887)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500888 },
889 fs: otherGenruleBp,
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500890 },
891 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500892 description: "genrule without tools or tool_files can convert successfully",
893 moduleTypeUnderTest: "genrule",
894 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
895 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500896 bp: `genrule {
897 name: "foo",
898 out: ["foo.out"],
899 srcs: ["foo.in"],
900 cmd: "cp $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500901 bazel_module: { bp2build_available: true },
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500902}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500903 expectedBazelTargets: []string{`genrule(
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500904 name = "foo",
905 cmd = "cp $(SRCS) $(OUTS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000906 outs = ["foo.out"],
907 srcs = ["foo.in"],
Jingwen Chen316e07c2020-12-14 09:09:52 -0500908)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500909 },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500910 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500911 }
912
913 dir := "."
914 for _, testCase := range testCases {
Liz Kammer356f7d42021-01-26 09:18:53 -0500915 fs := make(map[string][]byte)
916 toParse := []string{
917 "Android.bp",
918 }
919 for f, content := range testCase.fs {
920 if strings.HasSuffix(f, "Android.bp") {
921 toParse = append(toParse, f)
922 }
923 fs[f] = []byte(content)
924 }
925 config := android.TestConfig(buildDir, nil, testCase.bp, fs)
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500926 ctx := android.NewTestContext(config)
927 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Jingwen Chena42d6412021-01-26 21:57:27 -0500928 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500929 ctx.RegisterForBazelConversion()
930
Liz Kammer356f7d42021-01-26 09:18:53 -0500931 _, errs := ctx.ParseFileList(dir, toParse)
Rupert Shuttleworth06559d02021-05-19 09:14:26 -0400932 if errored(t, testCase.description, errs) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500933 continue
934 }
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500935 _, errs = ctx.ResolveDependencies(config)
Rupert Shuttleworth06559d02021-05-19 09:14:26 -0400936 if errored(t, testCase.description, errs) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500937 continue
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500938 }
939
Liz Kammer356f7d42021-01-26 09:18:53 -0500940 checkDir := dir
941 if testCase.dir != "" {
942 checkDir = testCase.dir
943 }
Jingwen Chen164e0862021-02-19 00:48:40 -0500944
945 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -0500946 bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir)
Liz Kammer356f7d42021-01-26 09:18:53 -0500947 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
948 t.Errorf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount)
949 } else {
950 for i, target := range bazelTargets {
951 if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
952 t.Errorf(
953 "%s: Expected generated Bazel target to be '%s', got '%s'",
954 testCase.description,
955 w,
956 g,
957 )
958 }
959 }
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500960 }
961 }
962}
Jingwen Chen041b1842021-02-01 00:23:25 -0500963
964type bp2buildMutator = func(android.TopDownMutatorContext)
965
966func TestBp2BuildInlinesDefaults(t *testing.T) {
967 testCases := []struct {
968 moduleTypesUnderTest map[string]android.ModuleFactory
969 bp2buildMutatorsUnderTest map[string]bp2buildMutator
970 bp string
971 expectedBazelTarget string
972 description string
973 }{
974 {
975 moduleTypesUnderTest: map[string]android.ModuleFactory{
976 "genrule": genrule.GenRuleFactory,
977 "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
978 },
979 bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
980 "genrule": genrule.GenruleBp2Build,
981 },
982 bp: `genrule_defaults {
983 name: "gen_defaults",
984 cmd: "do-something $(in) $(out)",
985}
986genrule {
987 name: "gen",
988 out: ["out"],
989 srcs: ["in1"],
990 defaults: ["gen_defaults"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500991 bazel_module: { bp2build_available: true },
Jingwen Chen041b1842021-02-01 00:23:25 -0500992}
993`,
994 expectedBazelTarget: `genrule(
995 name = "gen",
996 cmd = "do-something $(SRCS) $(OUTS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000997 outs = ["out"],
998 srcs = ["in1"],
Jingwen Chen041b1842021-02-01 00:23:25 -0500999)`,
1000 description: "genrule applies properties from a genrule_defaults dependency if not specified",
1001 },
1002 {
1003 moduleTypesUnderTest: map[string]android.ModuleFactory{
1004 "genrule": genrule.GenRuleFactory,
1005 "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
1006 },
1007 bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
1008 "genrule": genrule.GenruleBp2Build,
1009 },
1010 bp: `genrule_defaults {
1011 name: "gen_defaults",
1012 out: ["out-from-defaults"],
1013 srcs: ["in-from-defaults"],
1014 cmd: "cmd-from-defaults",
1015}
1016genrule {
1017 name: "gen",
1018 out: ["out"],
1019 srcs: ["in1"],
1020 defaults: ["gen_defaults"],
1021 cmd: "do-something $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001022 bazel_module: { bp2build_available: true },
Jingwen Chen041b1842021-02-01 00:23:25 -05001023}
1024`,
1025 expectedBazelTarget: `genrule(
1026 name = "gen",
1027 cmd = "do-something $(SRCS) $(OUTS)",
1028 outs = [
1029 "out-from-defaults",
1030 "out",
1031 ],
1032 srcs = [
1033 "in-from-defaults",
1034 "in1",
1035 ],
1036)`,
1037 description: "genrule does merges properties from a genrule_defaults dependency, latest-first",
1038 },
1039 {
1040 moduleTypesUnderTest: map[string]android.ModuleFactory{
1041 "genrule": genrule.GenRuleFactory,
1042 "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
1043 },
1044 bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
1045 "genrule": genrule.GenruleBp2Build,
1046 },
1047 bp: `genrule_defaults {
1048 name: "gen_defaults1",
1049 cmd: "cp $(in) $(out)",
1050}
1051
1052genrule_defaults {
1053 name: "gen_defaults2",
1054 srcs: ["in1"],
1055}
1056
1057genrule {
1058 name: "gen",
1059 out: ["out"],
1060 defaults: ["gen_defaults1", "gen_defaults2"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001061 bazel_module: { bp2build_available: true },
Jingwen Chen041b1842021-02-01 00:23:25 -05001062}
1063`,
1064 expectedBazelTarget: `genrule(
1065 name = "gen",
1066 cmd = "cp $(SRCS) $(OUTS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +00001067 outs = ["out"],
1068 srcs = ["in1"],
Jingwen Chen041b1842021-02-01 00:23:25 -05001069)`,
1070 description: "genrule applies properties from list of genrule_defaults",
1071 },
1072 {
1073 moduleTypesUnderTest: map[string]android.ModuleFactory{
1074 "genrule": genrule.GenRuleFactory,
1075 "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
1076 },
1077 bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
1078 "genrule": genrule.GenruleBp2Build,
1079 },
1080 bp: `genrule_defaults {
1081 name: "gen_defaults1",
1082 defaults: ["gen_defaults2"],
1083 cmd: "cmd1 $(in) $(out)", // overrides gen_defaults2's cmd property value.
1084}
1085
1086genrule_defaults {
1087 name: "gen_defaults2",
1088 defaults: ["gen_defaults3"],
1089 cmd: "cmd2 $(in) $(out)",
1090 out: ["out-from-2"],
1091 srcs: ["in1"],
1092}
1093
1094genrule_defaults {
1095 name: "gen_defaults3",
1096 out: ["out-from-3"],
1097 srcs: ["srcs-from-3"],
1098}
1099
1100genrule {
1101 name: "gen",
1102 out: ["out"],
1103 defaults: ["gen_defaults1"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001104 bazel_module: { bp2build_available: true },
Jingwen Chen041b1842021-02-01 00:23:25 -05001105}
1106`,
1107 expectedBazelTarget: `genrule(
1108 name = "gen",
1109 cmd = "cmd1 $(SRCS) $(OUTS)",
1110 outs = [
1111 "out-from-3",
1112 "out-from-2",
1113 "out",
1114 ],
1115 srcs = [
Jingwen Chen07027912021-03-15 06:02:43 -04001116 "srcs-from-3",
Liz Kammer9abd62d2021-05-21 08:37:59 -04001117 "in1",
Jingwen Chen041b1842021-02-01 00:23:25 -05001118 ],
1119)`,
1120 description: "genrule applies properties from genrule_defaults transitively",
1121 },
1122 }
1123
1124 dir := "."
1125 for _, testCase := range testCases {
1126 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
1127 ctx := android.NewTestContext(config)
1128 for m, factory := range testCase.moduleTypesUnderTest {
1129 ctx.RegisterModuleType(m, factory)
1130 }
1131 for mutator, f := range testCase.bp2buildMutatorsUnderTest {
1132 ctx.RegisterBp2BuildMutator(mutator, f)
1133 }
1134 ctx.RegisterForBazelConversion()
1135
1136 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
1137 android.FailIfErrored(t, errs)
1138 _, errs = ctx.ResolveDependencies(config)
1139 android.FailIfErrored(t, errs)
1140
Jingwen Chen164e0862021-02-19 00:48:40 -05001141 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -05001142 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
Jingwen Chen041b1842021-02-01 00:23:25 -05001143 if actualCount := len(bazelTargets); actualCount != 1 {
1144 t.Fatalf("%s: Expected 1 bazel target, got %d", testCase.description, actualCount)
1145 }
1146
1147 actualBazelTarget := bazelTargets[0]
1148 if actualBazelTarget.content != testCase.expectedBazelTarget {
1149 t.Errorf(
1150 "%s: Expected generated Bazel target to be '%s', got '%s'",
1151 testCase.description,
1152 testCase.expectedBazelTarget,
1153 actualBazelTarget.content,
1154 )
1155 }
1156 }
1157}
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001158
Jingwen Chen12b4c272021-03-10 02:05:59 -05001159func TestAllowlistingBp2buildTargetsExplicitly(t *testing.T) {
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001160 testCases := []struct {
1161 moduleTypeUnderTest string
1162 moduleTypeUnderTestFactory android.ModuleFactory
1163 moduleTypeUnderTestBp2BuildMutator bp2buildMutator
1164 bp string
1165 expectedCount int
1166 description string
1167 }{
1168 {
1169 description: "explicitly unavailable",
1170 moduleTypeUnderTest: "filegroup",
1171 moduleTypeUnderTestFactory: android.FileGroupFactory,
1172 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1173 bp: `filegroup {
1174 name: "foo",
1175 srcs: ["a", "b"],
1176 bazel_module: { bp2build_available: false },
1177}`,
1178 expectedCount: 0,
1179 },
1180 {
1181 description: "implicitly unavailable",
1182 moduleTypeUnderTest: "filegroup",
1183 moduleTypeUnderTestFactory: android.FileGroupFactory,
1184 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1185 bp: `filegroup {
1186 name: "foo",
1187 srcs: ["a", "b"],
1188}`,
1189 expectedCount: 0,
1190 },
1191 {
1192 description: "explicitly available",
1193 moduleTypeUnderTest: "filegroup",
1194 moduleTypeUnderTestFactory: android.FileGroupFactory,
1195 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1196 bp: `filegroup {
1197 name: "foo",
1198 srcs: ["a", "b"],
1199 bazel_module: { bp2build_available: true },
1200}`,
1201 expectedCount: 1,
1202 },
1203 {
1204 description: "generates more than 1 target if needed",
1205 moduleTypeUnderTest: "custom",
1206 moduleTypeUnderTestFactory: customModuleFactory,
1207 moduleTypeUnderTestBp2BuildMutator: customBp2BuildMutatorFromStarlark,
1208 bp: `custom {
1209 name: "foo",
1210 bazel_module: { bp2build_available: true },
1211}`,
1212 expectedCount: 3,
1213 },
1214 }
1215
1216 dir := "."
1217 for _, testCase := range testCases {
1218 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
1219 ctx := android.NewTestContext(config)
1220 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
1221 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1222 ctx.RegisterForBazelConversion()
1223
1224 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
1225 android.FailIfErrored(t, errs)
1226 _, errs = ctx.ResolveDependencies(config)
1227 android.FailIfErrored(t, errs)
1228
Jingwen Chen164e0862021-02-19 00:48:40 -05001229 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -05001230 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001231 if actualCount := len(bazelTargets); actualCount != testCase.expectedCount {
1232 t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, testCase.expectedCount, actualCount)
1233 }
1234 }
1235}
Liz Kammerba3ea162021-02-17 13:22:03 -05001236
Jingwen Chen12b4c272021-03-10 02:05:59 -05001237func TestAllowlistingBp2buildTargetsWithConfig(t *testing.T) {
1238 testCases := []struct {
1239 moduleTypeUnderTest string
1240 moduleTypeUnderTestFactory android.ModuleFactory
1241 moduleTypeUnderTestBp2BuildMutator bp2buildMutator
1242 expectedCount map[string]int
1243 description string
1244 bp2buildConfig android.Bp2BuildConfig
1245 checkDir string
1246 fs map[string]string
1247 }{
1248 {
1249 description: "test bp2build config package and subpackages config",
1250 moduleTypeUnderTest: "filegroup",
1251 moduleTypeUnderTestFactory: android.FileGroupFactory,
1252 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1253 expectedCount: map[string]int{
1254 "migrated": 1,
1255 "migrated/but_not_really": 0,
1256 "migrated/but_not_really/but_really": 1,
1257 "not_migrated": 0,
1258 "also_not_migrated": 0,
1259 },
1260 bp2buildConfig: android.Bp2BuildConfig{
1261 "migrated": android.Bp2BuildDefaultTrueRecursively,
1262 "migrated/but_not_really": android.Bp2BuildDefaultFalse,
1263 "not_migrated": android.Bp2BuildDefaultFalse,
1264 },
1265 fs: map[string]string{
1266 "migrated/Android.bp": `filegroup { name: "a" }`,
1267 "migrated/but_not_really/Android.bp": `filegroup { name: "b" }`,
1268 "migrated/but_not_really/but_really/Android.bp": `filegroup { name: "c" }`,
1269 "not_migrated/Android.bp": `filegroup { name: "d" }`,
1270 "also_not_migrated/Android.bp": `filegroup { name: "e" }`,
1271 },
1272 },
1273 {
1274 description: "test bp2build config opt-in and opt-out",
1275 moduleTypeUnderTest: "filegroup",
1276 moduleTypeUnderTestFactory: android.FileGroupFactory,
1277 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1278 expectedCount: map[string]int{
1279 "package-opt-in": 2,
1280 "package-opt-in/subpackage": 0,
1281 "package-opt-out": 1,
1282 "package-opt-out/subpackage": 0,
1283 },
1284 bp2buildConfig: android.Bp2BuildConfig{
1285 "package-opt-in": android.Bp2BuildDefaultFalse,
1286 "package-opt-out": android.Bp2BuildDefaultTrueRecursively,
1287 },
1288 fs: map[string]string{
1289 "package-opt-in/Android.bp": `
1290filegroup { name: "opt-in-a" }
1291filegroup { name: "opt-in-b", bazel_module: { bp2build_available: true } }
1292filegroup { name: "opt-in-c", bazel_module: { bp2build_available: true } }
1293`,
1294
1295 "package-opt-in/subpackage/Android.bp": `
1296filegroup { name: "opt-in-d" } // parent package not configured to DefaultTrueRecursively
1297`,
1298
1299 "package-opt-out/Android.bp": `
1300filegroup { name: "opt-out-a" }
1301filegroup { name: "opt-out-b", bazel_module: { bp2build_available: false } }
1302filegroup { name: "opt-out-c", bazel_module: { bp2build_available: false } }
1303`,
1304
1305 "package-opt-out/subpackage/Android.bp": `
1306filegroup { name: "opt-out-g", bazel_module: { bp2build_available: false } }
1307filegroup { name: "opt-out-h", bazel_module: { bp2build_available: false } }
1308`,
1309 },
1310 },
1311 }
1312
1313 dir := "."
1314 for _, testCase := range testCases {
1315 fs := make(map[string][]byte)
1316 toParse := []string{
1317 "Android.bp",
1318 }
1319 for f, content := range testCase.fs {
1320 if strings.HasSuffix(f, "Android.bp") {
1321 toParse = append(toParse, f)
1322 }
1323 fs[f] = []byte(content)
1324 }
1325 config := android.TestConfig(buildDir, nil, "", fs)
1326 ctx := android.NewTestContext(config)
1327 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
1328 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1329 ctx.RegisterBp2BuildConfig(testCase.bp2buildConfig)
1330 ctx.RegisterForBazelConversion()
1331
1332 _, errs := ctx.ParseFileList(dir, toParse)
1333 android.FailIfErrored(t, errs)
1334 _, errs = ctx.ResolveDependencies(config)
1335 android.FailIfErrored(t, errs)
1336
1337 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
1338
1339 // For each directory, test that the expected number of generated targets is correct.
1340 for dir, expectedCount := range testCase.expectedCount {
1341 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
1342 if actualCount := len(bazelTargets); actualCount != expectedCount {
1343 t.Fatalf(
1344 "%s: Expected %d bazel target for %s package, got %d",
1345 testCase.description,
1346 expectedCount,
1347 dir,
1348 actualCount)
1349 }
1350
1351 }
1352 }
1353}
1354
Liz Kammerba3ea162021-02-17 13:22:03 -05001355func TestCombineBuildFilesBp2buildTargets(t *testing.T) {
1356 testCases := []struct {
1357 description string
1358 moduleTypeUnderTest string
1359 moduleTypeUnderTestFactory android.ModuleFactory
1360 moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
1361 preArchMutators []android.RegisterMutatorFunc
Liz Kammerba3ea162021-02-17 13:22:03 -05001362 bp string
1363 expectedBazelTargets []string
1364 fs map[string]string
1365 dir string
1366 }{
1367 {
1368 description: "filegroup bazel_module.label",
1369 moduleTypeUnderTest: "filegroup",
1370 moduleTypeUnderTestFactory: android.FileGroupFactory,
1371 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1372 bp: `filegroup {
1373 name: "fg_foo",
1374 bazel_module: { label: "//other:fg_foo" },
1375}`,
1376 expectedBazelTargets: []string{
1377 `// BUILD file`,
1378 },
1379 fs: map[string]string{
1380 "other/BUILD.bazel": `// BUILD file`,
1381 },
1382 },
1383 {
1384 description: "multiple bazel_module.label same BUILD",
1385 moduleTypeUnderTest: "filegroup",
1386 moduleTypeUnderTestFactory: android.FileGroupFactory,
1387 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1388 bp: `filegroup {
Jingwen Chenc63677b2021-06-17 05:43:19 +00001389 name: "fg_foo",
1390 bazel_module: { label: "//other:fg_foo" },
1391 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001392
Jingwen Chenc63677b2021-06-17 05:43:19 +00001393 filegroup {
1394 name: "foo",
1395 bazel_module: { label: "//other:foo" },
1396 }`,
Liz Kammerba3ea162021-02-17 13:22:03 -05001397 expectedBazelTargets: []string{
1398 `// BUILD file`,
1399 },
1400 fs: map[string]string{
1401 "other/BUILD.bazel": `// BUILD file`,
1402 },
1403 },
1404 {
Jingwen Chenc63677b2021-06-17 05:43:19 +00001405 description: "filegroup bazel_module.label and bp2build in subdir",
Liz Kammerba3ea162021-02-17 13:22:03 -05001406 moduleTypeUnderTest: "filegroup",
1407 moduleTypeUnderTestFactory: android.FileGroupFactory,
1408 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chenc63677b2021-06-17 05:43:19 +00001409 dir: "other",
1410 bp: ``,
1411 fs: map[string]string{
1412 "other/Android.bp": `filegroup {
1413 name: "fg_foo",
1414 bazel_module: {
1415 bp2build_available: true,
1416 },
1417 }
1418 filegroup {
1419 name: "fg_bar",
1420 bazel_module: {
1421 label: "//other:fg_bar"
1422 },
1423 }`,
1424 "other/BUILD.bazel": `// definition for fg_bar`,
1425 },
Liz Kammerba3ea162021-02-17 13:22:03 -05001426 expectedBazelTargets: []string{
1427 `filegroup(
1428 name = "fg_foo",
Jingwen Chenc63677b2021-06-17 05:43:19 +00001429)`, `// definition for fg_bar`,
Liz Kammerba3ea162021-02-17 13:22:03 -05001430 },
1431 },
1432 {
1433 description: "filegroup bazel_module.label and filegroup bp2build",
1434 moduleTypeUnderTest: "filegroup",
1435 moduleTypeUnderTestFactory: android.FileGroupFactory,
1436 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1437 bp: `filegroup {
Jingwen Chenc63677b2021-06-17 05:43:19 +00001438 name: "fg_foo",
1439 bazel_module: {
1440 label: "//other:fg_foo",
1441 },
1442 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001443
Jingwen Chenc63677b2021-06-17 05:43:19 +00001444 filegroup {
1445 name: "fg_bar",
1446 bazel_module: {
1447 bp2build_available: true,
1448 },
1449 }`,
Liz Kammerba3ea162021-02-17 13:22:03 -05001450 expectedBazelTargets: []string{
1451 `filegroup(
1452 name = "fg_bar",
1453)`,
1454 `// BUILD file`,
1455 },
1456 fs: map[string]string{
1457 "other/BUILD.bazel": `// BUILD file`,
1458 },
1459 },
1460 }
1461
1462 dir := "."
1463 for _, testCase := range testCases {
Jingwen Chen49109762021-05-25 05:16:48 +00001464 t.Run(testCase.description, func(t *testing.T) {
1465 fs := make(map[string][]byte)
1466 toParse := []string{
1467 "Android.bp",
Liz Kammerba3ea162021-02-17 13:22:03 -05001468 }
Jingwen Chen49109762021-05-25 05:16:48 +00001469 for f, content := range testCase.fs {
1470 if strings.HasSuffix(f, "Android.bp") {
1471 toParse = append(toParse, f)
1472 }
1473 fs[f] = []byte(content)
1474 }
1475 config := android.TestConfig(buildDir, nil, testCase.bp, fs)
1476 ctx := android.NewTestContext(config)
1477 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Jingwen Chen49109762021-05-25 05:16:48 +00001478 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1479 ctx.RegisterForBazelConversion()
Liz Kammerba3ea162021-02-17 13:22:03 -05001480
Jingwen Chen49109762021-05-25 05:16:48 +00001481 _, errs := ctx.ParseFileList(dir, toParse)
1482 if errored(t, testCase.description, errs) {
1483 return
1484 }
1485 _, errs = ctx.ResolveDependencies(config)
1486 if errored(t, testCase.description, errs) {
1487 return
1488 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001489
Jingwen Chen49109762021-05-25 05:16:48 +00001490 checkDir := dir
1491 if testCase.dir != "" {
1492 checkDir = testCase.dir
1493 }
1494 bazelTargets := generateBazelTargetsForDir(NewCodegenContext(config, *ctx.Context, Bp2Build), checkDir)
1495 bazelTargets.sort()
1496 actualCount := len(bazelTargets)
1497 expectedCount := len(testCase.expectedBazelTargets)
1498 if actualCount != expectedCount {
1499 t.Errorf("Expected %d bazel target, got %d\n%s", expectedCount, actualCount, bazelTargets)
1500 }
1501 if !strings.Contains(bazelTargets.String(), "Section: Handcrafted targets. ") {
1502 t.Errorf("Expected string representation of bazelTargets to contain handcrafted section header.")
1503 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001504 for i, target := range bazelTargets {
Jingwen Chen49109762021-05-25 05:16:48 +00001505 actualContent := target.content
1506 expectedContent := testCase.expectedBazelTargets[i]
1507 if expectedContent != actualContent {
Liz Kammerba3ea162021-02-17 13:22:03 -05001508 t.Errorf(
Jingwen Chen49109762021-05-25 05:16:48 +00001509 "Expected generated Bazel target to be '%s', got '%s'",
1510 expectedContent,
1511 actualContent,
Liz Kammerba3ea162021-02-17 13:22:03 -05001512 )
1513 }
1514 }
Jingwen Chen49109762021-05-25 05:16:48 +00001515 })
Liz Kammerba3ea162021-02-17 13:22:03 -05001516 }
1517}
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001518
1519func TestGlobExcludeSrcs(t *testing.T) {
1520 testCases := []struct {
1521 description string
1522 moduleTypeUnderTest string
1523 moduleTypeUnderTestFactory android.ModuleFactory
1524 moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
1525 bp string
1526 expectedBazelTargets []string
1527 fs map[string]string
1528 dir string
1529 }{
1530 {
1531 description: "filegroup top level exclude_srcs",
1532 moduleTypeUnderTest: "filegroup",
1533 moduleTypeUnderTestFactory: android.FileGroupFactory,
1534 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1535 bp: `filegroup {
1536 name: "fg_foo",
1537 srcs: ["**/*.txt"],
1538 exclude_srcs: ["c.txt"],
1539 bazel_module: { bp2build_available: true },
1540}`,
1541 expectedBazelTargets: []string{`filegroup(
1542 name = "fg_foo",
1543 srcs = [
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001544 "a.txt",
1545 "b.txt",
Liz Kammer9abd62d2021-05-21 08:37:59 -04001546 "//dir:e.txt",
1547 "//dir:f.txt",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001548 ],
1549)`,
1550 },
1551 fs: map[string]string{
1552 "a.txt": "",
1553 "b.txt": "",
1554 "c.txt": "",
1555 "dir/Android.bp": "",
1556 "dir/e.txt": "",
1557 "dir/f.txt": "",
1558 },
1559 },
1560 {
1561 description: "filegroup in subdir exclude_srcs",
1562 moduleTypeUnderTest: "filegroup",
1563 moduleTypeUnderTestFactory: android.FileGroupFactory,
1564 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1565 bp: "",
1566 dir: "dir",
1567 fs: map[string]string{
1568 "dir/Android.bp": `filegroup {
1569 name: "fg_foo",
1570 srcs: ["**/*.txt"],
1571 exclude_srcs: ["b.txt"],
1572 bazel_module: { bp2build_available: true },
1573}
1574`,
1575 "dir/a.txt": "",
1576 "dir/b.txt": "",
1577 "dir/subdir/Android.bp": "",
1578 "dir/subdir/e.txt": "",
1579 "dir/subdir/f.txt": "",
1580 },
1581 expectedBazelTargets: []string{`filegroup(
1582 name = "fg_foo",
1583 srcs = [
Liz Kammer9abd62d2021-05-21 08:37:59 -04001584 "a.txt",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001585 "//dir/subdir:e.txt",
1586 "//dir/subdir:f.txt",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001587 ],
1588)`,
1589 },
1590 },
1591 }
1592
1593 dir := "."
1594 for _, testCase := range testCases {
1595 fs := make(map[string][]byte)
1596 toParse := []string{
1597 "Android.bp",
1598 }
1599 for f, content := range testCase.fs {
1600 if strings.HasSuffix(f, "Android.bp") {
1601 toParse = append(toParse, f)
1602 }
1603 fs[f] = []byte(content)
1604 }
1605 config := android.TestConfig(buildDir, nil, testCase.bp, fs)
1606 ctx := android.NewTestContext(config)
1607 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
1608 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1609 ctx.RegisterForBazelConversion()
1610
1611 _, errs := ctx.ParseFileList(dir, toParse)
Rupert Shuttleworth06559d02021-05-19 09:14:26 -04001612 if errored(t, testCase.description, errs) {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001613 continue
1614 }
1615 _, errs = ctx.ResolveDependencies(config)
Rupert Shuttleworth06559d02021-05-19 09:14:26 -04001616 if errored(t, testCase.description, errs) {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001617 continue
1618 }
1619
1620 checkDir := dir
1621 if testCase.dir != "" {
1622 checkDir = testCase.dir
1623 }
1624 bazelTargets := generateBazelTargetsForDir(NewCodegenContext(config, *ctx.Context, Bp2Build), checkDir)
1625 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
1626 t.Errorf("%s: Expected %d bazel target, got %d\n%s", testCase.description, expectedCount, actualCount, bazelTargets)
1627 } else {
1628 for i, target := range bazelTargets {
1629 if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
1630 t.Errorf(
1631 "%s: Expected generated Bazel target to be '%s', got '%s'",
1632 testCase.description,
1633 w,
1634 g,
1635 )
1636 }
1637 }
1638 }
1639 }
1640}