blob: eb3a73f8932f40df831c3d51f0472a1079c3bdee [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
Liz Kammer32b77cf2021-08-04 15:17:02 -0400337 registerCustomModuleForBp2buildConversion(ctx)
Jingwen Chen73850672020-12-14 08:25:34 -0500338
339 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
Rupert Shuttleworth06559d02021-05-19 09:14:26 -0400340 if errored(t, "", errs) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500341 continue
342 }
Jingwen Chen73850672020-12-14 08:25:34 -0500343 _, errs = ctx.ResolveDependencies(config)
Rupert Shuttleworth06559d02021-05-19 09:14:26 -0400344 if errored(t, "", errs) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500345 continue
346 }
Jingwen Chen73850672020-12-14 08:25:34 -0500347
Jingwen Chen164e0862021-02-19 00:48:40 -0500348 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -0500349 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
Jingwen Chen164e0862021-02-19 00:48:40 -0500350
Liz Kammer4562a3b2021-04-21 18:15:34 -0400351 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
Liz Kammer356f7d42021-01-26 09:18:53 -0500352 t.Errorf("Expected %d bazel target, got %d", expectedCount, actualCount)
353 } else {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400354 for i, expectedBazelTarget := range testCase.expectedBazelTargets {
355 actualBazelTarget := bazelTargets[i]
356 if actualBazelTarget.content != expectedBazelTarget {
357 t.Errorf(
358 "Expected generated Bazel target to be '%s', got '%s'",
359 expectedBazelTarget,
360 actualBazelTarget.content,
361 )
362 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500363 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800364 }
365 }
366}
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500367
Jingwen Chen40067de2021-01-26 21:58:43 -0500368func TestLoadStatements(t *testing.T) {
369 testCases := []struct {
370 bazelTargets BazelTargets
371 expectedLoadStatements string
372 }{
373 {
374 bazelTargets: BazelTargets{
375 BazelTarget{
376 name: "foo",
377 ruleClass: "cc_library",
378 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
379 },
380 },
381 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
382 },
383 {
384 bazelTargets: BazelTargets{
385 BazelTarget{
386 name: "foo",
387 ruleClass: "cc_library",
388 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
389 },
390 BazelTarget{
391 name: "bar",
392 ruleClass: "cc_library",
393 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
394 },
395 },
396 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
397 },
398 {
399 bazelTargets: BazelTargets{
400 BazelTarget{
401 name: "foo",
402 ruleClass: "cc_library",
403 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
404 },
405 BazelTarget{
406 name: "bar",
407 ruleClass: "cc_binary",
408 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
409 },
410 },
411 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")`,
412 },
413 {
414 bazelTargets: BazelTargets{
415 BazelTarget{
416 name: "foo",
417 ruleClass: "cc_library",
418 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
419 },
420 BazelTarget{
421 name: "bar",
422 ruleClass: "cc_binary",
423 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
424 },
425 BazelTarget{
426 name: "baz",
427 ruleClass: "java_binary",
428 bzlLoadLocation: "//build/bazel/rules:java.bzl",
429 },
430 },
431 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")
432load("//build/bazel/rules:java.bzl", "java_binary")`,
433 },
434 {
435 bazelTargets: BazelTargets{
436 BazelTarget{
437 name: "foo",
438 ruleClass: "cc_binary",
439 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
440 },
441 BazelTarget{
442 name: "bar",
443 ruleClass: "java_binary",
444 bzlLoadLocation: "//build/bazel/rules:java.bzl",
445 },
446 BazelTarget{
447 name: "baz",
448 ruleClass: "genrule",
449 // Note: no bzlLoadLocation for native rules
450 },
451 },
452 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary")
453load("//build/bazel/rules:java.bzl", "java_binary")`,
454 },
455 }
456
457 for _, testCase := range testCases {
458 actual := testCase.bazelTargets.LoadStatements()
459 expected := testCase.expectedLoadStatements
460 if actual != expected {
461 t.Fatalf("Expected load statements to be %s, got %s", expected, actual)
462 }
463 }
464
465}
466
467func TestGenerateBazelTargetModules_OneToMany_LoadedFromStarlark(t *testing.T) {
468 testCases := []struct {
469 bp string
470 expectedBazelTarget string
471 expectedBazelTargetCount int
472 expectedLoadStatements string
473 }{
474 {
475 bp: `custom {
476 name: "bar",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500477 bazel_module: { bp2build_available: true },
Jingwen Chen40067de2021-01-26 21:58:43 -0500478}`,
479 expectedBazelTarget: `my_library(
480 name = "bar",
481)
482
483my_proto_library(
484 name = "bar_my_proto_library_deps",
485)
486
487proto_library(
488 name = "bar_proto_library_deps",
489)`,
490 expectedBazelTargetCount: 3,
491 expectedLoadStatements: `load("//build/bazel/rules:proto.bzl", "my_proto_library", "proto_library")
492load("//build/bazel/rules:rules.bzl", "my_library")`,
493 },
494 }
495
496 dir := "."
497 for _, testCase := range testCases {
498 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
499 ctx := android.NewTestContext(config)
500 ctx.RegisterModuleType("custom", customModuleFactory)
Jingwen Chen12b4c272021-03-10 02:05:59 -0500501 ctx.RegisterBp2BuildMutator("custom", customBp2BuildMutatorFromStarlark)
Jingwen Chen40067de2021-01-26 21:58:43 -0500502 ctx.RegisterForBazelConversion()
503
504 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
505 android.FailIfErrored(t, errs)
506 _, errs = ctx.ResolveDependencies(config)
507 android.FailIfErrored(t, errs)
508
Jingwen Chen164e0862021-02-19 00:48:40 -0500509 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -0500510 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
Jingwen Chen40067de2021-01-26 21:58:43 -0500511 if actualCount := len(bazelTargets); actualCount != testCase.expectedBazelTargetCount {
512 t.Fatalf("Expected %d bazel target, got %d", testCase.expectedBazelTargetCount, actualCount)
513 }
514
515 actualBazelTargets := bazelTargets.String()
516 if actualBazelTargets != testCase.expectedBazelTarget {
517 t.Errorf(
518 "Expected generated Bazel target to be '%s', got '%s'",
519 testCase.expectedBazelTarget,
520 actualBazelTargets,
521 )
522 }
523
524 actualLoadStatements := bazelTargets.LoadStatements()
525 if actualLoadStatements != testCase.expectedLoadStatements {
526 t.Errorf(
527 "Expected generated load statements to be '%s', got '%s'",
528 testCase.expectedLoadStatements,
529 actualLoadStatements,
530 )
531 }
532 }
533}
534
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500535func TestModuleTypeBp2Build(t *testing.T) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500536 otherGenruleBp := map[string]string{
537 "other/Android.bp": `genrule {
538 name: "foo.tool",
539 out: ["foo_tool.out"],
540 srcs: ["foo_tool.in"],
541 cmd: "cp $(in) $(out)",
542}
543genrule {
544 name: "other.tool",
545 out: ["other_tool.out"],
546 srcs: ["other_tool.in"],
547 cmd: "cp $(in) $(out)",
548}`,
549 }
550
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500551 testCases := []struct {
Liz Kammer356f7d42021-01-26 09:18:53 -0500552 description string
Jingwen Chena42d6412021-01-26 21:57:27 -0500553 moduleTypeUnderTest string
554 moduleTypeUnderTestFactory android.ModuleFactory
555 moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
Liz Kammer356f7d42021-01-26 09:18:53 -0500556 preArchMutators []android.RegisterMutatorFunc
Jingwen Chena42d6412021-01-26 21:57:27 -0500557 bp string
Liz Kammer356f7d42021-01-26 09:18:53 -0500558 expectedBazelTargets []string
559 fs map[string]string
560 dir string
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500561 }{
562 {
Liz Kammerebfcf672021-02-16 15:00:05 -0500563 description: "filegroup with does not specify srcs",
564 moduleTypeUnderTest: "filegroup",
565 moduleTypeUnderTestFactory: android.FileGroupFactory,
566 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
567 bp: `filegroup {
568 name: "fg_foo",
569 bazel_module: { bp2build_available: true },
570}`,
571 expectedBazelTargets: []string{
572 `filegroup(
573 name = "fg_foo",
574)`,
575 },
576 },
577 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500578 description: "filegroup with no srcs",
579 moduleTypeUnderTest: "filegroup",
580 moduleTypeUnderTestFactory: android.FileGroupFactory,
581 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500582 bp: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500583 name: "fg_foo",
584 srcs: [],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500585 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500586}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500587 expectedBazelTargets: []string{
588 `filegroup(
589 name = "fg_foo",
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500590)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500591 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500592 },
593 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500594 description: "filegroup with srcs",
595 moduleTypeUnderTest: "filegroup",
596 moduleTypeUnderTestFactory: android.FileGroupFactory,
597 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500598 bp: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500599 name: "fg_foo",
600 srcs: ["a", "b"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500601 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500602}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500603 expectedBazelTargets: []string{`filegroup(
604 name = "fg_foo",
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500605 srcs = [
606 "a",
607 "b",
608 ],
609)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500610 },
611 },
612 {
613 description: "filegroup with excludes srcs",
614 moduleTypeUnderTest: "filegroup",
615 moduleTypeUnderTestFactory: android.FileGroupFactory,
616 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
617 bp: `filegroup {
618 name: "fg_foo",
619 srcs: ["a", "b"],
620 exclude_srcs: ["a"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500621 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500622}`,
623 expectedBazelTargets: []string{`filegroup(
624 name = "fg_foo",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000625 srcs = ["b"],
Liz Kammer356f7d42021-01-26 09:18:53 -0500626)`,
627 },
628 },
629 {
630 description: "filegroup with glob",
631 moduleTypeUnderTest: "filegroup",
632 moduleTypeUnderTestFactory: android.FileGroupFactory,
633 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
634 bp: `filegroup {
635 name: "foo",
636 srcs: ["**/*.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500637 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500638}`,
639 expectedBazelTargets: []string{`filegroup(
640 name = "foo",
641 srcs = [
642 "other/a.txt",
643 "other/b.txt",
644 "other/subdir/a.txt",
645 ],
646)`,
647 },
648 fs: map[string]string{
649 "other/a.txt": "",
650 "other/b.txt": "",
651 "other/subdir/a.txt": "",
652 "other/file": "",
653 },
654 },
655 {
656 description: "filegroup with glob in subdir",
657 moduleTypeUnderTest: "filegroup",
658 moduleTypeUnderTestFactory: android.FileGroupFactory,
659 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
660 bp: `filegroup {
661 name: "foo",
662 srcs: ["a.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500663 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500664}`,
665 dir: "other",
666 expectedBazelTargets: []string{`filegroup(
667 name = "fg_foo",
668 srcs = [
669 "a.txt",
670 "b.txt",
671 "subdir/a.txt",
672 ],
673)`,
674 },
675 fs: map[string]string{
676 "other/Android.bp": `filegroup {
677 name: "fg_foo",
678 srcs: ["**/*.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500679 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500680}`,
681 "other/a.txt": "",
682 "other/b.txt": "",
683 "other/subdir/a.txt": "",
684 "other/file": "",
685 },
686 },
687 {
688 description: "depends_on_other_dir_module",
689 moduleTypeUnderTest: "filegroup",
690 moduleTypeUnderTestFactory: android.FileGroupFactory,
691 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
692 bp: `filegroup {
693 name: "foobar",
694 srcs: [
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000695 ":foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500696 "c",
697 ],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500698 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500699}`,
700 expectedBazelTargets: []string{`filegroup(
701 name = "foobar",
702 srcs = [
703 "//other:foo",
704 "c",
705 ],
706)`,
707 },
708 fs: map[string]string{
709 "other/Android.bp": `filegroup {
710 name: "foo",
711 srcs: ["a", "b"],
712}`,
713 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500714 },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500715 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500716 description: "genrule with command line variable replacements",
717 moduleTypeUnderTest: "genrule",
718 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
719 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Jingwen Chen316e07c2020-12-14 09:09:52 -0500720 bp: `genrule {
Liz Kammer356f7d42021-01-26 09:18:53 -0500721 name: "foo.tool",
722 out: ["foo_tool.out"],
723 srcs: ["foo_tool.in"],
724 cmd: "cp $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500725 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500726}
727
728genrule {
Jingwen Chen316e07c2020-12-14 09:09:52 -0500729 name: "foo",
730 out: ["foo.out"],
731 srcs: ["foo.in"],
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500732 tools: [":foo.tool"],
733 cmd: "$(location :foo.tool) --genDir=$(genDir) arg $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500734 bazel_module: { bp2build_available: true },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500735}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500736 expectedBazelTargets: []string{
737 `genrule(
Jingwen Chen316e07c2020-12-14 09:09:52 -0500738 name = "foo",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500739 cmd = "$(location :foo.tool) --genDir=$(GENDIR) arg $(SRCS) $(OUTS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000740 outs = ["foo.out"],
741 srcs = ["foo.in"],
742 tools = [":foo.tool"],
Jingwen Chen316e07c2020-12-14 09:09:52 -0500743)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500744 `genrule(
745 name = "foo.tool",
746 cmd = "cp $(SRCS) $(OUTS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000747 outs = ["foo_tool.out"],
748 srcs = ["foo_tool.in"],
Liz Kammer356f7d42021-01-26 09:18:53 -0500749)`,
750 },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500751 },
752 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500753 description: "genrule using $(locations :label)",
754 moduleTypeUnderTest: "genrule",
755 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
756 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Jingwen Chen316e07c2020-12-14 09:09:52 -0500757 bp: `genrule {
Liz Kammer356f7d42021-01-26 09:18:53 -0500758 name: "foo.tools",
759 out: ["foo_tool.out", "foo_tool2.out"],
760 srcs: ["foo_tool.in"],
761 cmd: "cp $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500762 bazel_module: { bp2build_available: true },
763}
Liz Kammer356f7d42021-01-26 09:18:53 -0500764
765genrule {
Jingwen Chen316e07c2020-12-14 09:09:52 -0500766 name: "foo",
767 out: ["foo.out"],
768 srcs: ["foo.in"],
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500769 tools: [":foo.tools"],
770 cmd: "$(locations :foo.tools) -s $(out) $(in)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500771 bazel_module: { bp2build_available: true },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500772}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500773 expectedBazelTargets: []string{`genrule(
Jingwen Chen316e07c2020-12-14 09:09:52 -0500774 name = "foo",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500775 cmd = "$(locations :foo.tools) -s $(OUTS) $(SRCS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000776 outs = ["foo.out"],
777 srcs = ["foo.in"],
778 tools = [":foo.tools"],
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500779)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500780 `genrule(
781 name = "foo.tools",
782 cmd = "cp $(SRCS) $(OUTS)",
783 outs = [
784 "foo_tool.out",
785 "foo_tool2.out",
786 ],
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000787 srcs = ["foo_tool.in"],
Liz Kammer356f7d42021-01-26 09:18:53 -0500788)`,
789 },
790 },
791 {
792 description: "genrule using $(locations //absolute:label)",
793 moduleTypeUnderTest: "genrule",
794 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
795 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Liz Kammer356f7d42021-01-26 09:18:53 -0500796 bp: `genrule {
797 name: "foo",
798 out: ["foo.out"],
799 srcs: ["foo.in"],
800 tool_files: [":foo.tool"],
801 cmd: "$(locations :foo.tool) -s $(out) $(in)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500802 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500803}`,
804 expectedBazelTargets: []string{`genrule(
805 name = "foo",
806 cmd = "$(locations //other:foo.tool) -s $(OUTS) $(SRCS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000807 outs = ["foo.out"],
808 srcs = ["foo.in"],
809 tools = ["//other:foo.tool"],
Liz Kammer356f7d42021-01-26 09:18:53 -0500810)`,
811 },
812 fs: otherGenruleBp,
813 },
814 {
815 description: "genrule srcs using $(locations //absolute:label)",
816 moduleTypeUnderTest: "genrule",
817 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
818 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Liz Kammer356f7d42021-01-26 09:18:53 -0500819 bp: `genrule {
820 name: "foo",
821 out: ["foo.out"],
822 srcs: [":other.tool"],
823 tool_files: [":foo.tool"],
824 cmd: "$(locations :foo.tool) -s $(out) $(location :other.tool)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500825 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500826}`,
827 expectedBazelTargets: []string{`genrule(
828 name = "foo",
829 cmd = "$(locations //other:foo.tool) -s $(OUTS) $(location //other:other.tool)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000830 outs = ["foo.out"],
831 srcs = ["//other:other.tool"],
832 tools = ["//other:foo.tool"],
Liz Kammer356f7d42021-01-26 09:18:53 -0500833)`,
834 },
835 fs: otherGenruleBp,
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500836 },
837 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500838 description: "genrule using $(location) label should substitute first tool label automatically",
839 moduleTypeUnderTest: "genrule",
840 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
841 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500842 bp: `genrule {
843 name: "foo",
844 out: ["foo.out"],
845 srcs: ["foo.in"],
846 tool_files: [":foo.tool", ":other.tool"],
847 cmd: "$(location) -s $(out) $(in)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500848 bazel_module: { bp2build_available: true },
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500849}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500850 expectedBazelTargets: []string{`genrule(
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500851 name = "foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500852 cmd = "$(location //other:foo.tool) -s $(OUTS) $(SRCS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000853 outs = ["foo.out"],
854 srcs = ["foo.in"],
Jingwen Chen316e07c2020-12-14 09:09:52 -0500855 tools = [
Liz Kammer356f7d42021-01-26 09:18:53 -0500856 "//other:foo.tool",
857 "//other:other.tool",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500858 ],
859)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500860 },
861 fs: otherGenruleBp,
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500862 },
863 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500864 description: "genrule using $(locations) label should substitute first tool label automatically",
865 moduleTypeUnderTest: "genrule",
866 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
867 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500868 bp: `genrule {
869 name: "foo",
870 out: ["foo.out"],
871 srcs: ["foo.in"],
872 tools: [":foo.tool", ":other.tool"],
873 cmd: "$(locations) -s $(out) $(in)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500874 bazel_module: { bp2build_available: true },
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500875}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500876 expectedBazelTargets: []string{`genrule(
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500877 name = "foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500878 cmd = "$(locations //other:foo.tool) -s $(OUTS) $(SRCS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000879 outs = ["foo.out"],
880 srcs = ["foo.in"],
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500881 tools = [
Liz Kammer356f7d42021-01-26 09:18:53 -0500882 "//other:foo.tool",
883 "//other:other.tool",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500884 ],
885)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500886 },
887 fs: otherGenruleBp,
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500888 },
889 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500890 description: "genrule without tools or tool_files can convert successfully",
891 moduleTypeUnderTest: "genrule",
892 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
893 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500894 bp: `genrule {
895 name: "foo",
896 out: ["foo.out"],
897 srcs: ["foo.in"],
898 cmd: "cp $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500899 bazel_module: { bp2build_available: true },
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500900}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500901 expectedBazelTargets: []string{`genrule(
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500902 name = "foo",
903 cmd = "cp $(SRCS) $(OUTS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000904 outs = ["foo.out"],
905 srcs = ["foo.in"],
Jingwen Chen316e07c2020-12-14 09:09:52 -0500906)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500907 },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500908 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500909 }
910
911 dir := "."
912 for _, testCase := range testCases {
Liz Kammer356f7d42021-01-26 09:18:53 -0500913 fs := make(map[string][]byte)
914 toParse := []string{
915 "Android.bp",
916 }
917 for f, content := range testCase.fs {
918 if strings.HasSuffix(f, "Android.bp") {
919 toParse = append(toParse, f)
920 }
921 fs[f] = []byte(content)
922 }
923 config := android.TestConfig(buildDir, nil, testCase.bp, fs)
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500924 ctx := android.NewTestContext(config)
925 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Jingwen Chena42d6412021-01-26 21:57:27 -0500926 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500927 ctx.RegisterForBazelConversion()
928
Liz Kammer356f7d42021-01-26 09:18:53 -0500929 _, errs := ctx.ParseFileList(dir, toParse)
Rupert Shuttleworth06559d02021-05-19 09:14:26 -0400930 if errored(t, testCase.description, errs) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500931 continue
932 }
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500933 _, errs = ctx.ResolveDependencies(config)
Rupert Shuttleworth06559d02021-05-19 09:14:26 -0400934 if errored(t, testCase.description, errs) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500935 continue
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500936 }
937
Liz Kammer356f7d42021-01-26 09:18:53 -0500938 checkDir := dir
939 if testCase.dir != "" {
940 checkDir = testCase.dir
941 }
Jingwen Chen164e0862021-02-19 00:48:40 -0500942
943 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -0500944 bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir)
Liz Kammer356f7d42021-01-26 09:18:53 -0500945 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
946 t.Errorf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount)
947 } else {
948 for i, target := range bazelTargets {
949 if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
950 t.Errorf(
951 "%s: Expected generated Bazel target to be '%s', got '%s'",
952 testCase.description,
953 w,
954 g,
955 )
956 }
957 }
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500958 }
959 }
960}
Jingwen Chen041b1842021-02-01 00:23:25 -0500961
962type bp2buildMutator = func(android.TopDownMutatorContext)
963
964func TestBp2BuildInlinesDefaults(t *testing.T) {
965 testCases := []struct {
966 moduleTypesUnderTest map[string]android.ModuleFactory
967 bp2buildMutatorsUnderTest map[string]bp2buildMutator
968 bp string
969 expectedBazelTarget string
970 description string
971 }{
972 {
973 moduleTypesUnderTest: map[string]android.ModuleFactory{
974 "genrule": genrule.GenRuleFactory,
975 "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
976 },
977 bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
978 "genrule": genrule.GenruleBp2Build,
979 },
980 bp: `genrule_defaults {
981 name: "gen_defaults",
982 cmd: "do-something $(in) $(out)",
983}
984genrule {
985 name: "gen",
986 out: ["out"],
987 srcs: ["in1"],
988 defaults: ["gen_defaults"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500989 bazel_module: { bp2build_available: true },
Jingwen Chen041b1842021-02-01 00:23:25 -0500990}
991`,
992 expectedBazelTarget: `genrule(
993 name = "gen",
994 cmd = "do-something $(SRCS) $(OUTS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000995 outs = ["out"],
996 srcs = ["in1"],
Jingwen Chen041b1842021-02-01 00:23:25 -0500997)`,
998 description: "genrule applies properties from a genrule_defaults dependency if not specified",
999 },
1000 {
1001 moduleTypesUnderTest: map[string]android.ModuleFactory{
1002 "genrule": genrule.GenRuleFactory,
1003 "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
1004 },
1005 bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
1006 "genrule": genrule.GenruleBp2Build,
1007 },
1008 bp: `genrule_defaults {
1009 name: "gen_defaults",
1010 out: ["out-from-defaults"],
1011 srcs: ["in-from-defaults"],
1012 cmd: "cmd-from-defaults",
1013}
1014genrule {
1015 name: "gen",
1016 out: ["out"],
1017 srcs: ["in1"],
1018 defaults: ["gen_defaults"],
1019 cmd: "do-something $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001020 bazel_module: { bp2build_available: true },
Jingwen Chen041b1842021-02-01 00:23:25 -05001021}
1022`,
1023 expectedBazelTarget: `genrule(
1024 name = "gen",
1025 cmd = "do-something $(SRCS) $(OUTS)",
1026 outs = [
1027 "out-from-defaults",
1028 "out",
1029 ],
1030 srcs = [
1031 "in-from-defaults",
1032 "in1",
1033 ],
1034)`,
1035 description: "genrule does merges properties from a genrule_defaults dependency, latest-first",
1036 },
1037 {
1038 moduleTypesUnderTest: map[string]android.ModuleFactory{
1039 "genrule": genrule.GenRuleFactory,
1040 "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
1041 },
1042 bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
1043 "genrule": genrule.GenruleBp2Build,
1044 },
1045 bp: `genrule_defaults {
1046 name: "gen_defaults1",
1047 cmd: "cp $(in) $(out)",
1048}
1049
1050genrule_defaults {
1051 name: "gen_defaults2",
1052 srcs: ["in1"],
1053}
1054
1055genrule {
1056 name: "gen",
1057 out: ["out"],
1058 defaults: ["gen_defaults1", "gen_defaults2"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001059 bazel_module: { bp2build_available: true },
Jingwen Chen041b1842021-02-01 00:23:25 -05001060}
1061`,
1062 expectedBazelTarget: `genrule(
1063 name = "gen",
1064 cmd = "cp $(SRCS) $(OUTS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +00001065 outs = ["out"],
1066 srcs = ["in1"],
Jingwen Chen041b1842021-02-01 00:23:25 -05001067)`,
1068 description: "genrule applies properties from list of genrule_defaults",
1069 },
1070 {
1071 moduleTypesUnderTest: map[string]android.ModuleFactory{
1072 "genrule": genrule.GenRuleFactory,
1073 "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
1074 },
1075 bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
1076 "genrule": genrule.GenruleBp2Build,
1077 },
1078 bp: `genrule_defaults {
1079 name: "gen_defaults1",
1080 defaults: ["gen_defaults2"],
1081 cmd: "cmd1 $(in) $(out)", // overrides gen_defaults2's cmd property value.
1082}
1083
1084genrule_defaults {
1085 name: "gen_defaults2",
1086 defaults: ["gen_defaults3"],
1087 cmd: "cmd2 $(in) $(out)",
1088 out: ["out-from-2"],
1089 srcs: ["in1"],
1090}
1091
1092genrule_defaults {
1093 name: "gen_defaults3",
1094 out: ["out-from-3"],
1095 srcs: ["srcs-from-3"],
1096}
1097
1098genrule {
1099 name: "gen",
1100 out: ["out"],
1101 defaults: ["gen_defaults1"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001102 bazel_module: { bp2build_available: true },
Jingwen Chen041b1842021-02-01 00:23:25 -05001103}
1104`,
1105 expectedBazelTarget: `genrule(
1106 name = "gen",
1107 cmd = "cmd1 $(SRCS) $(OUTS)",
1108 outs = [
1109 "out-from-3",
1110 "out-from-2",
1111 "out",
1112 ],
1113 srcs = [
Jingwen Chen07027912021-03-15 06:02:43 -04001114 "srcs-from-3",
Liz Kammer9abd62d2021-05-21 08:37:59 -04001115 "in1",
Jingwen Chen041b1842021-02-01 00:23:25 -05001116 ],
1117)`,
1118 description: "genrule applies properties from genrule_defaults transitively",
1119 },
1120 }
1121
1122 dir := "."
1123 for _, testCase := range testCases {
1124 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
1125 ctx := android.NewTestContext(config)
1126 for m, factory := range testCase.moduleTypesUnderTest {
1127 ctx.RegisterModuleType(m, factory)
1128 }
1129 for mutator, f := range testCase.bp2buildMutatorsUnderTest {
1130 ctx.RegisterBp2BuildMutator(mutator, f)
1131 }
1132 ctx.RegisterForBazelConversion()
1133
1134 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
1135 android.FailIfErrored(t, errs)
1136 _, errs = ctx.ResolveDependencies(config)
1137 android.FailIfErrored(t, errs)
1138
Jingwen Chen164e0862021-02-19 00:48:40 -05001139 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -05001140 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
Jingwen Chen041b1842021-02-01 00:23:25 -05001141 if actualCount := len(bazelTargets); actualCount != 1 {
1142 t.Fatalf("%s: Expected 1 bazel target, got %d", testCase.description, actualCount)
1143 }
1144
1145 actualBazelTarget := bazelTargets[0]
1146 if actualBazelTarget.content != testCase.expectedBazelTarget {
1147 t.Errorf(
1148 "%s: Expected generated Bazel target to be '%s', got '%s'",
1149 testCase.description,
1150 testCase.expectedBazelTarget,
1151 actualBazelTarget.content,
1152 )
1153 }
1154 }
1155}
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001156
Jingwen Chen12b4c272021-03-10 02:05:59 -05001157func TestAllowlistingBp2buildTargetsExplicitly(t *testing.T) {
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001158 testCases := []struct {
1159 moduleTypeUnderTest string
1160 moduleTypeUnderTestFactory android.ModuleFactory
1161 moduleTypeUnderTestBp2BuildMutator bp2buildMutator
1162 bp string
1163 expectedCount int
1164 description string
1165 }{
1166 {
1167 description: "explicitly unavailable",
1168 moduleTypeUnderTest: "filegroup",
1169 moduleTypeUnderTestFactory: android.FileGroupFactory,
1170 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1171 bp: `filegroup {
1172 name: "foo",
1173 srcs: ["a", "b"],
1174 bazel_module: { bp2build_available: false },
1175}`,
1176 expectedCount: 0,
1177 },
1178 {
1179 description: "implicitly unavailable",
1180 moduleTypeUnderTest: "filegroup",
1181 moduleTypeUnderTestFactory: android.FileGroupFactory,
1182 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1183 bp: `filegroup {
1184 name: "foo",
1185 srcs: ["a", "b"],
1186}`,
1187 expectedCount: 0,
1188 },
1189 {
1190 description: "explicitly available",
1191 moduleTypeUnderTest: "filegroup",
1192 moduleTypeUnderTestFactory: android.FileGroupFactory,
1193 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1194 bp: `filegroup {
1195 name: "foo",
1196 srcs: ["a", "b"],
1197 bazel_module: { bp2build_available: true },
1198}`,
1199 expectedCount: 1,
1200 },
1201 {
1202 description: "generates more than 1 target if needed",
1203 moduleTypeUnderTest: "custom",
1204 moduleTypeUnderTestFactory: customModuleFactory,
1205 moduleTypeUnderTestBp2BuildMutator: customBp2BuildMutatorFromStarlark,
1206 bp: `custom {
1207 name: "foo",
1208 bazel_module: { bp2build_available: true },
1209}`,
1210 expectedCount: 3,
1211 },
1212 }
1213
1214 dir := "."
1215 for _, testCase := range testCases {
1216 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
1217 ctx := android.NewTestContext(config)
1218 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
1219 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1220 ctx.RegisterForBazelConversion()
1221
1222 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
1223 android.FailIfErrored(t, errs)
1224 _, errs = ctx.ResolveDependencies(config)
1225 android.FailIfErrored(t, errs)
1226
Jingwen Chen164e0862021-02-19 00:48:40 -05001227 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -05001228 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001229 if actualCount := len(bazelTargets); actualCount != testCase.expectedCount {
1230 t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, testCase.expectedCount, actualCount)
1231 }
1232 }
1233}
Liz Kammerba3ea162021-02-17 13:22:03 -05001234
Jingwen Chen12b4c272021-03-10 02:05:59 -05001235func TestAllowlistingBp2buildTargetsWithConfig(t *testing.T) {
1236 testCases := []struct {
1237 moduleTypeUnderTest string
1238 moduleTypeUnderTestFactory android.ModuleFactory
1239 moduleTypeUnderTestBp2BuildMutator bp2buildMutator
1240 expectedCount map[string]int
1241 description string
1242 bp2buildConfig android.Bp2BuildConfig
1243 checkDir string
1244 fs map[string]string
1245 }{
1246 {
1247 description: "test bp2build config package and subpackages config",
1248 moduleTypeUnderTest: "filegroup",
1249 moduleTypeUnderTestFactory: android.FileGroupFactory,
1250 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1251 expectedCount: map[string]int{
1252 "migrated": 1,
1253 "migrated/but_not_really": 0,
1254 "migrated/but_not_really/but_really": 1,
1255 "not_migrated": 0,
1256 "also_not_migrated": 0,
1257 },
1258 bp2buildConfig: android.Bp2BuildConfig{
1259 "migrated": android.Bp2BuildDefaultTrueRecursively,
1260 "migrated/but_not_really": android.Bp2BuildDefaultFalse,
1261 "not_migrated": android.Bp2BuildDefaultFalse,
1262 },
1263 fs: map[string]string{
1264 "migrated/Android.bp": `filegroup { name: "a" }`,
1265 "migrated/but_not_really/Android.bp": `filegroup { name: "b" }`,
1266 "migrated/but_not_really/but_really/Android.bp": `filegroup { name: "c" }`,
1267 "not_migrated/Android.bp": `filegroup { name: "d" }`,
1268 "also_not_migrated/Android.bp": `filegroup { name: "e" }`,
1269 },
1270 },
1271 {
1272 description: "test bp2build config opt-in and opt-out",
1273 moduleTypeUnderTest: "filegroup",
1274 moduleTypeUnderTestFactory: android.FileGroupFactory,
1275 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1276 expectedCount: map[string]int{
1277 "package-opt-in": 2,
1278 "package-opt-in/subpackage": 0,
1279 "package-opt-out": 1,
1280 "package-opt-out/subpackage": 0,
1281 },
1282 bp2buildConfig: android.Bp2BuildConfig{
1283 "package-opt-in": android.Bp2BuildDefaultFalse,
1284 "package-opt-out": android.Bp2BuildDefaultTrueRecursively,
1285 },
1286 fs: map[string]string{
1287 "package-opt-in/Android.bp": `
1288filegroup { name: "opt-in-a" }
1289filegroup { name: "opt-in-b", bazel_module: { bp2build_available: true } }
1290filegroup { name: "opt-in-c", bazel_module: { bp2build_available: true } }
1291`,
1292
1293 "package-opt-in/subpackage/Android.bp": `
1294filegroup { name: "opt-in-d" } // parent package not configured to DefaultTrueRecursively
1295`,
1296
1297 "package-opt-out/Android.bp": `
1298filegroup { name: "opt-out-a" }
1299filegroup { name: "opt-out-b", bazel_module: { bp2build_available: false } }
1300filegroup { name: "opt-out-c", bazel_module: { bp2build_available: false } }
1301`,
1302
1303 "package-opt-out/subpackage/Android.bp": `
1304filegroup { name: "opt-out-g", bazel_module: { bp2build_available: false } }
1305filegroup { name: "opt-out-h", bazel_module: { bp2build_available: false } }
1306`,
1307 },
1308 },
1309 }
1310
1311 dir := "."
1312 for _, testCase := range testCases {
1313 fs := make(map[string][]byte)
1314 toParse := []string{
1315 "Android.bp",
1316 }
1317 for f, content := range testCase.fs {
1318 if strings.HasSuffix(f, "Android.bp") {
1319 toParse = append(toParse, f)
1320 }
1321 fs[f] = []byte(content)
1322 }
1323 config := android.TestConfig(buildDir, nil, "", fs)
1324 ctx := android.NewTestContext(config)
1325 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
1326 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1327 ctx.RegisterBp2BuildConfig(testCase.bp2buildConfig)
1328 ctx.RegisterForBazelConversion()
1329
1330 _, errs := ctx.ParseFileList(dir, toParse)
1331 android.FailIfErrored(t, errs)
1332 _, errs = ctx.ResolveDependencies(config)
1333 android.FailIfErrored(t, errs)
1334
1335 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
1336
1337 // For each directory, test that the expected number of generated targets is correct.
1338 for dir, expectedCount := range testCase.expectedCount {
1339 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
1340 if actualCount := len(bazelTargets); actualCount != expectedCount {
1341 t.Fatalf(
1342 "%s: Expected %d bazel target for %s package, got %d",
1343 testCase.description,
1344 expectedCount,
1345 dir,
1346 actualCount)
1347 }
1348
1349 }
1350 }
1351}
1352
Liz Kammerba3ea162021-02-17 13:22:03 -05001353func TestCombineBuildFilesBp2buildTargets(t *testing.T) {
1354 testCases := []struct {
1355 description string
1356 moduleTypeUnderTest string
1357 moduleTypeUnderTestFactory android.ModuleFactory
1358 moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
1359 preArchMutators []android.RegisterMutatorFunc
Liz Kammerba3ea162021-02-17 13:22:03 -05001360 bp string
1361 expectedBazelTargets []string
1362 fs map[string]string
1363 dir string
1364 }{
1365 {
1366 description: "filegroup bazel_module.label",
1367 moduleTypeUnderTest: "filegroup",
1368 moduleTypeUnderTestFactory: android.FileGroupFactory,
1369 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1370 bp: `filegroup {
1371 name: "fg_foo",
1372 bazel_module: { label: "//other:fg_foo" },
1373}`,
1374 expectedBazelTargets: []string{
1375 `// BUILD file`,
1376 },
1377 fs: map[string]string{
1378 "other/BUILD.bazel": `// BUILD file`,
1379 },
1380 },
1381 {
1382 description: "multiple bazel_module.label same BUILD",
1383 moduleTypeUnderTest: "filegroup",
1384 moduleTypeUnderTestFactory: android.FileGroupFactory,
1385 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1386 bp: `filegroup {
Jingwen Chenc63677b2021-06-17 05:43:19 +00001387 name: "fg_foo",
1388 bazel_module: { label: "//other:fg_foo" },
1389 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001390
Jingwen Chenc63677b2021-06-17 05:43:19 +00001391 filegroup {
1392 name: "foo",
1393 bazel_module: { label: "//other:foo" },
1394 }`,
Liz Kammerba3ea162021-02-17 13:22:03 -05001395 expectedBazelTargets: []string{
1396 `// BUILD file`,
1397 },
1398 fs: map[string]string{
1399 "other/BUILD.bazel": `// BUILD file`,
1400 },
1401 },
1402 {
Jingwen Chenc63677b2021-06-17 05:43:19 +00001403 description: "filegroup bazel_module.label and bp2build in subdir",
Liz Kammerba3ea162021-02-17 13:22:03 -05001404 moduleTypeUnderTest: "filegroup",
1405 moduleTypeUnderTestFactory: android.FileGroupFactory,
1406 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chenc63677b2021-06-17 05:43:19 +00001407 dir: "other",
1408 bp: ``,
1409 fs: map[string]string{
1410 "other/Android.bp": `filegroup {
1411 name: "fg_foo",
1412 bazel_module: {
1413 bp2build_available: true,
1414 },
1415 }
1416 filegroup {
1417 name: "fg_bar",
1418 bazel_module: {
1419 label: "//other:fg_bar"
1420 },
1421 }`,
1422 "other/BUILD.bazel": `// definition for fg_bar`,
1423 },
Liz Kammerba3ea162021-02-17 13:22:03 -05001424 expectedBazelTargets: []string{
1425 `filegroup(
1426 name = "fg_foo",
Jingwen Chenc63677b2021-06-17 05:43:19 +00001427)`, `// definition for fg_bar`,
Liz Kammerba3ea162021-02-17 13:22:03 -05001428 },
1429 },
1430 {
1431 description: "filegroup bazel_module.label and filegroup bp2build",
1432 moduleTypeUnderTest: "filegroup",
1433 moduleTypeUnderTestFactory: android.FileGroupFactory,
1434 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1435 bp: `filegroup {
Jingwen Chenc63677b2021-06-17 05:43:19 +00001436 name: "fg_foo",
1437 bazel_module: {
1438 label: "//other:fg_foo",
1439 },
1440 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001441
Jingwen Chenc63677b2021-06-17 05:43:19 +00001442 filegroup {
1443 name: "fg_bar",
1444 bazel_module: {
1445 bp2build_available: true,
1446 },
1447 }`,
Liz Kammerba3ea162021-02-17 13:22:03 -05001448 expectedBazelTargets: []string{
1449 `filegroup(
1450 name = "fg_bar",
1451)`,
1452 `// BUILD file`,
1453 },
1454 fs: map[string]string{
1455 "other/BUILD.bazel": `// BUILD file`,
1456 },
1457 },
1458 }
1459
1460 dir := "."
1461 for _, testCase := range testCases {
Jingwen Chen49109762021-05-25 05:16:48 +00001462 t.Run(testCase.description, func(t *testing.T) {
1463 fs := make(map[string][]byte)
1464 toParse := []string{
1465 "Android.bp",
Liz Kammerba3ea162021-02-17 13:22:03 -05001466 }
Jingwen Chen49109762021-05-25 05:16:48 +00001467 for f, content := range testCase.fs {
1468 if strings.HasSuffix(f, "Android.bp") {
1469 toParse = append(toParse, f)
1470 }
1471 fs[f] = []byte(content)
1472 }
1473 config := android.TestConfig(buildDir, nil, testCase.bp, fs)
1474 ctx := android.NewTestContext(config)
1475 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Jingwen Chen49109762021-05-25 05:16:48 +00001476 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1477 ctx.RegisterForBazelConversion()
Liz Kammerba3ea162021-02-17 13:22:03 -05001478
Jingwen Chen49109762021-05-25 05:16:48 +00001479 _, errs := ctx.ParseFileList(dir, toParse)
1480 if errored(t, testCase.description, errs) {
1481 return
1482 }
1483 _, errs = ctx.ResolveDependencies(config)
1484 if errored(t, testCase.description, errs) {
1485 return
1486 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001487
Jingwen Chen49109762021-05-25 05:16:48 +00001488 checkDir := dir
1489 if testCase.dir != "" {
1490 checkDir = testCase.dir
1491 }
1492 bazelTargets := generateBazelTargetsForDir(NewCodegenContext(config, *ctx.Context, Bp2Build), checkDir)
1493 bazelTargets.sort()
1494 actualCount := len(bazelTargets)
1495 expectedCount := len(testCase.expectedBazelTargets)
1496 if actualCount != expectedCount {
1497 t.Errorf("Expected %d bazel target, got %d\n%s", expectedCount, actualCount, bazelTargets)
1498 }
1499 if !strings.Contains(bazelTargets.String(), "Section: Handcrafted targets. ") {
1500 t.Errorf("Expected string representation of bazelTargets to contain handcrafted section header.")
1501 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001502 for i, target := range bazelTargets {
Jingwen Chen49109762021-05-25 05:16:48 +00001503 actualContent := target.content
1504 expectedContent := testCase.expectedBazelTargets[i]
1505 if expectedContent != actualContent {
Liz Kammerba3ea162021-02-17 13:22:03 -05001506 t.Errorf(
Jingwen Chen49109762021-05-25 05:16:48 +00001507 "Expected generated Bazel target to be '%s', got '%s'",
1508 expectedContent,
1509 actualContent,
Liz Kammerba3ea162021-02-17 13:22:03 -05001510 )
1511 }
1512 }
Jingwen Chen49109762021-05-25 05:16:48 +00001513 })
Liz Kammerba3ea162021-02-17 13:22:03 -05001514 }
1515}
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001516
1517func TestGlobExcludeSrcs(t *testing.T) {
1518 testCases := []struct {
1519 description string
1520 moduleTypeUnderTest string
1521 moduleTypeUnderTestFactory android.ModuleFactory
1522 moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
1523 bp string
1524 expectedBazelTargets []string
1525 fs map[string]string
1526 dir string
1527 }{
1528 {
1529 description: "filegroup top level exclude_srcs",
1530 moduleTypeUnderTest: "filegroup",
1531 moduleTypeUnderTestFactory: android.FileGroupFactory,
1532 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1533 bp: `filegroup {
1534 name: "fg_foo",
1535 srcs: ["**/*.txt"],
1536 exclude_srcs: ["c.txt"],
1537 bazel_module: { bp2build_available: true },
1538}`,
1539 expectedBazelTargets: []string{`filegroup(
1540 name = "fg_foo",
1541 srcs = [
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001542 "a.txt",
1543 "b.txt",
Liz Kammer9abd62d2021-05-21 08:37:59 -04001544 "//dir:e.txt",
1545 "//dir:f.txt",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001546 ],
1547)`,
1548 },
1549 fs: map[string]string{
1550 "a.txt": "",
1551 "b.txt": "",
1552 "c.txt": "",
1553 "dir/Android.bp": "",
1554 "dir/e.txt": "",
1555 "dir/f.txt": "",
1556 },
1557 },
1558 {
1559 description: "filegroup in subdir exclude_srcs",
1560 moduleTypeUnderTest: "filegroup",
1561 moduleTypeUnderTestFactory: android.FileGroupFactory,
1562 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1563 bp: "",
1564 dir: "dir",
1565 fs: map[string]string{
1566 "dir/Android.bp": `filegroup {
1567 name: "fg_foo",
1568 srcs: ["**/*.txt"],
1569 exclude_srcs: ["b.txt"],
1570 bazel_module: { bp2build_available: true },
1571}
1572`,
1573 "dir/a.txt": "",
1574 "dir/b.txt": "",
1575 "dir/subdir/Android.bp": "",
1576 "dir/subdir/e.txt": "",
1577 "dir/subdir/f.txt": "",
1578 },
1579 expectedBazelTargets: []string{`filegroup(
1580 name = "fg_foo",
1581 srcs = [
Liz Kammer9abd62d2021-05-21 08:37:59 -04001582 "a.txt",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001583 "//dir/subdir:e.txt",
1584 "//dir/subdir:f.txt",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001585 ],
1586)`,
1587 },
1588 },
1589 }
1590
1591 dir := "."
1592 for _, testCase := range testCases {
1593 fs := make(map[string][]byte)
1594 toParse := []string{
1595 "Android.bp",
1596 }
1597 for f, content := range testCase.fs {
1598 if strings.HasSuffix(f, "Android.bp") {
1599 toParse = append(toParse, f)
1600 }
1601 fs[f] = []byte(content)
1602 }
1603 config := android.TestConfig(buildDir, nil, testCase.bp, fs)
1604 ctx := android.NewTestContext(config)
1605 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
1606 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1607 ctx.RegisterForBazelConversion()
1608
1609 _, errs := ctx.ParseFileList(dir, toParse)
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 _, errs = ctx.ResolveDependencies(config)
Rupert Shuttleworth06559d02021-05-19 09:14:26 -04001614 if errored(t, testCase.description, errs) {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001615 continue
1616 }
1617
1618 checkDir := dir
1619 if testCase.dir != "" {
1620 checkDir = testCase.dir
1621 }
1622 bazelTargets := generateBazelTargetsForDir(NewCodegenContext(config, *ctx.Context, Bp2Build), checkDir)
1623 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
1624 t.Errorf("%s: Expected %d bazel target, got %d\n%s", testCase.description, expectedCount, actualCount, bazelTargets)
1625 } else {
1626 for i, target := range bazelTargets {
1627 if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
1628 t.Errorf(
1629 "%s: Expected generated Bazel target to be '%s', got '%s'",
1630 testCase.description,
1631 w,
1632 g,
1633 )
1634 }
1635 }
1636 }
1637 }
1638}