blob: 33609af03f64fb9ee9286462384796e4e16c5773 [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
559 depsMutators []android.RegisterMutatorFunc
Jingwen Chena42d6412021-01-26 21:57:27 -0500560 bp string
Liz Kammer356f7d42021-01-26 09:18:53 -0500561 expectedBazelTargets []string
562 fs map[string]string
563 dir string
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500564 }{
565 {
Liz Kammerebfcf672021-02-16 15:00:05 -0500566 description: "filegroup with does not specify srcs",
567 moduleTypeUnderTest: "filegroup",
568 moduleTypeUnderTestFactory: android.FileGroupFactory,
569 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
570 bp: `filegroup {
571 name: "fg_foo",
572 bazel_module: { bp2build_available: true },
573}`,
574 expectedBazelTargets: []string{
575 `filegroup(
576 name = "fg_foo",
577)`,
578 },
579 },
580 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500581 description: "filegroup with no srcs",
582 moduleTypeUnderTest: "filegroup",
583 moduleTypeUnderTestFactory: android.FileGroupFactory,
584 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500585 bp: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500586 name: "fg_foo",
587 srcs: [],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500588 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500589}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500590 expectedBazelTargets: []string{
591 `filegroup(
592 name = "fg_foo",
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500593)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500594 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500595 },
596 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500597 description: "filegroup with srcs",
598 moduleTypeUnderTest: "filegroup",
599 moduleTypeUnderTestFactory: android.FileGroupFactory,
600 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500601 bp: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500602 name: "fg_foo",
603 srcs: ["a", "b"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500604 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500605}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500606 expectedBazelTargets: []string{`filegroup(
607 name = "fg_foo",
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500608 srcs = [
609 "a",
610 "b",
611 ],
612)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500613 },
614 },
615 {
616 description: "filegroup with excludes srcs",
617 moduleTypeUnderTest: "filegroup",
618 moduleTypeUnderTestFactory: android.FileGroupFactory,
619 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
620 bp: `filegroup {
621 name: "fg_foo",
622 srcs: ["a", "b"],
623 exclude_srcs: ["a"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500624 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500625}`,
626 expectedBazelTargets: []string{`filegroup(
627 name = "fg_foo",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000628 srcs = ["b"],
Liz Kammer356f7d42021-01-26 09:18:53 -0500629)`,
630 },
631 },
632 {
633 description: "filegroup with glob",
634 moduleTypeUnderTest: "filegroup",
635 moduleTypeUnderTestFactory: android.FileGroupFactory,
636 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
637 bp: `filegroup {
638 name: "foo",
639 srcs: ["**/*.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500640 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500641}`,
642 expectedBazelTargets: []string{`filegroup(
643 name = "foo",
644 srcs = [
645 "other/a.txt",
646 "other/b.txt",
647 "other/subdir/a.txt",
648 ],
649)`,
650 },
651 fs: map[string]string{
652 "other/a.txt": "",
653 "other/b.txt": "",
654 "other/subdir/a.txt": "",
655 "other/file": "",
656 },
657 },
658 {
659 description: "filegroup with glob in subdir",
660 moduleTypeUnderTest: "filegroup",
661 moduleTypeUnderTestFactory: android.FileGroupFactory,
662 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
663 bp: `filegroup {
664 name: "foo",
665 srcs: ["a.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500666 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500667}`,
668 dir: "other",
669 expectedBazelTargets: []string{`filegroup(
670 name = "fg_foo",
671 srcs = [
672 "a.txt",
673 "b.txt",
674 "subdir/a.txt",
675 ],
676)`,
677 },
678 fs: map[string]string{
679 "other/Android.bp": `filegroup {
680 name: "fg_foo",
681 srcs: ["**/*.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500682 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500683}`,
684 "other/a.txt": "",
685 "other/b.txt": "",
686 "other/subdir/a.txt": "",
687 "other/file": "",
688 },
689 },
690 {
691 description: "depends_on_other_dir_module",
692 moduleTypeUnderTest: "filegroup",
693 moduleTypeUnderTestFactory: android.FileGroupFactory,
694 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
695 bp: `filegroup {
696 name: "foobar",
697 srcs: [
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000698 ":foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500699 "c",
700 ],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500701 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500702}`,
703 expectedBazelTargets: []string{`filegroup(
704 name = "foobar",
705 srcs = [
706 "//other:foo",
707 "c",
708 ],
709)`,
710 },
711 fs: map[string]string{
712 "other/Android.bp": `filegroup {
713 name: "foo",
714 srcs: ["a", "b"],
715}`,
716 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500717 },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500718 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500719 description: "genrule with command line variable replacements",
720 moduleTypeUnderTest: "genrule",
721 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
722 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Liz Kammer356f7d42021-01-26 09:18:53 -0500723 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
Jingwen Chen316e07c2020-12-14 09:09:52 -0500724 bp: `genrule {
Liz Kammer356f7d42021-01-26 09:18:53 -0500725 name: "foo.tool",
726 out: ["foo_tool.out"],
727 srcs: ["foo_tool.in"],
728 cmd: "cp $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500729 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500730}
731
732genrule {
Jingwen Chen316e07c2020-12-14 09:09:52 -0500733 name: "foo",
734 out: ["foo.out"],
735 srcs: ["foo.in"],
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500736 tools: [":foo.tool"],
737 cmd: "$(location :foo.tool) --genDir=$(genDir) arg $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500738 bazel_module: { bp2build_available: true },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500739}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500740 expectedBazelTargets: []string{
741 `genrule(
Jingwen Chen316e07c2020-12-14 09:09:52 -0500742 name = "foo",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500743 cmd = "$(location :foo.tool) --genDir=$(GENDIR) arg $(SRCS) $(OUTS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000744 outs = ["foo.out"],
745 srcs = ["foo.in"],
746 tools = [":foo.tool"],
Jingwen Chen316e07c2020-12-14 09:09:52 -0500747)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500748 `genrule(
749 name = "foo.tool",
750 cmd = "cp $(SRCS) $(OUTS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000751 outs = ["foo_tool.out"],
752 srcs = ["foo_tool.in"],
Liz Kammer356f7d42021-01-26 09:18:53 -0500753)`,
754 },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500755 },
756 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500757 description: "genrule using $(locations :label)",
758 moduleTypeUnderTest: "genrule",
759 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
760 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Liz Kammer356f7d42021-01-26 09:18:53 -0500761 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
Jingwen Chen316e07c2020-12-14 09:09:52 -0500762 bp: `genrule {
Liz Kammer356f7d42021-01-26 09:18:53 -0500763 name: "foo.tools",
764 out: ["foo_tool.out", "foo_tool2.out"],
765 srcs: ["foo_tool.in"],
766 cmd: "cp $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500767 bazel_module: { bp2build_available: true },
768}
Liz Kammer356f7d42021-01-26 09:18:53 -0500769
770genrule {
Jingwen Chen316e07c2020-12-14 09:09:52 -0500771 name: "foo",
772 out: ["foo.out"],
773 srcs: ["foo.in"],
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500774 tools: [":foo.tools"],
775 cmd: "$(locations :foo.tools) -s $(out) $(in)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500776 bazel_module: { bp2build_available: true },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500777}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500778 expectedBazelTargets: []string{`genrule(
Jingwen Chen316e07c2020-12-14 09:09:52 -0500779 name = "foo",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500780 cmd = "$(locations :foo.tools) -s $(OUTS) $(SRCS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000781 outs = ["foo.out"],
782 srcs = ["foo.in"],
783 tools = [":foo.tools"],
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500784)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500785 `genrule(
786 name = "foo.tools",
787 cmd = "cp $(SRCS) $(OUTS)",
788 outs = [
789 "foo_tool.out",
790 "foo_tool2.out",
791 ],
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000792 srcs = ["foo_tool.in"],
Liz Kammer356f7d42021-01-26 09:18:53 -0500793)`,
794 },
795 },
796 {
797 description: "genrule using $(locations //absolute:label)",
798 moduleTypeUnderTest: "genrule",
799 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
800 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
801 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
802 bp: `genrule {
803 name: "foo",
804 out: ["foo.out"],
805 srcs: ["foo.in"],
806 tool_files: [":foo.tool"],
807 cmd: "$(locations :foo.tool) -s $(out) $(in)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500808 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500809}`,
810 expectedBazelTargets: []string{`genrule(
811 name = "foo",
812 cmd = "$(locations //other:foo.tool) -s $(OUTS) $(SRCS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000813 outs = ["foo.out"],
814 srcs = ["foo.in"],
815 tools = ["//other:foo.tool"],
Liz Kammer356f7d42021-01-26 09:18:53 -0500816)`,
817 },
818 fs: otherGenruleBp,
819 },
820 {
821 description: "genrule srcs using $(locations //absolute:label)",
822 moduleTypeUnderTest: "genrule",
823 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
824 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
825 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
826 bp: `genrule {
827 name: "foo",
828 out: ["foo.out"],
829 srcs: [":other.tool"],
830 tool_files: [":foo.tool"],
831 cmd: "$(locations :foo.tool) -s $(out) $(location :other.tool)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500832 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500833}`,
834 expectedBazelTargets: []string{`genrule(
835 name = "foo",
836 cmd = "$(locations //other:foo.tool) -s $(OUTS) $(location //other:other.tool)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000837 outs = ["foo.out"],
838 srcs = ["//other:other.tool"],
839 tools = ["//other:foo.tool"],
Liz Kammer356f7d42021-01-26 09:18:53 -0500840)`,
841 },
842 fs: otherGenruleBp,
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500843 },
844 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500845 description: "genrule using $(location) label should substitute first tool label automatically",
846 moduleTypeUnderTest: "genrule",
847 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
848 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Liz Kammer356f7d42021-01-26 09:18:53 -0500849 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500850 bp: `genrule {
851 name: "foo",
852 out: ["foo.out"],
853 srcs: ["foo.in"],
854 tool_files: [":foo.tool", ":other.tool"],
855 cmd: "$(location) -s $(out) $(in)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500856 bazel_module: { bp2build_available: true },
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500857}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500858 expectedBazelTargets: []string{`genrule(
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500859 name = "foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500860 cmd = "$(location //other:foo.tool) -s $(OUTS) $(SRCS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000861 outs = ["foo.out"],
862 srcs = ["foo.in"],
Jingwen Chen316e07c2020-12-14 09:09:52 -0500863 tools = [
Liz Kammer356f7d42021-01-26 09:18:53 -0500864 "//other:foo.tool",
865 "//other:other.tool",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500866 ],
867)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500868 },
869 fs: otherGenruleBp,
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500870 },
871 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500872 description: "genrule using $(locations) label should substitute first tool label automatically",
873 moduleTypeUnderTest: "genrule",
874 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
875 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Liz Kammer356f7d42021-01-26 09:18:53 -0500876 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500877 bp: `genrule {
878 name: "foo",
879 out: ["foo.out"],
880 srcs: ["foo.in"],
881 tools: [":foo.tool", ":other.tool"],
882 cmd: "$(locations) -s $(out) $(in)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500883 bazel_module: { bp2build_available: true },
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500884}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500885 expectedBazelTargets: []string{`genrule(
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500886 name = "foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500887 cmd = "$(locations //other:foo.tool) -s $(OUTS) $(SRCS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000888 outs = ["foo.out"],
889 srcs = ["foo.in"],
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500890 tools = [
Liz Kammer356f7d42021-01-26 09:18:53 -0500891 "//other:foo.tool",
892 "//other:other.tool",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500893 ],
894)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500895 },
896 fs: otherGenruleBp,
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500897 },
898 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500899 description: "genrule without tools or tool_files can convert successfully",
900 moduleTypeUnderTest: "genrule",
901 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
902 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Liz Kammer356f7d42021-01-26 09:18:53 -0500903 depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps},
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500904 bp: `genrule {
905 name: "foo",
906 out: ["foo.out"],
907 srcs: ["foo.in"],
908 cmd: "cp $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500909 bazel_module: { bp2build_available: true },
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500910}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500911 expectedBazelTargets: []string{`genrule(
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500912 name = "foo",
913 cmd = "cp $(SRCS) $(OUTS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000914 outs = ["foo.out"],
915 srcs = ["foo.in"],
Jingwen Chen316e07c2020-12-14 09:09:52 -0500916)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500917 },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500918 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500919 }
920
921 dir := "."
922 for _, testCase := range testCases {
Liz Kammer356f7d42021-01-26 09:18:53 -0500923 fs := make(map[string][]byte)
924 toParse := []string{
925 "Android.bp",
926 }
927 for f, content := range testCase.fs {
928 if strings.HasSuffix(f, "Android.bp") {
929 toParse = append(toParse, f)
930 }
931 fs[f] = []byte(content)
932 }
933 config := android.TestConfig(buildDir, nil, testCase.bp, fs)
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500934 ctx := android.NewTestContext(config)
935 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Liz Kammer356f7d42021-01-26 09:18:53 -0500936 for _, m := range testCase.depsMutators {
937 ctx.DepsBp2BuildMutators(m)
938 }
Jingwen Chena42d6412021-01-26 21:57:27 -0500939 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500940 ctx.RegisterForBazelConversion()
941
Liz Kammer356f7d42021-01-26 09:18:53 -0500942 _, errs := ctx.ParseFileList(dir, toParse)
Rupert Shuttleworth06559d02021-05-19 09:14:26 -0400943 if errored(t, testCase.description, errs) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500944 continue
945 }
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500946 _, errs = ctx.ResolveDependencies(config)
Rupert Shuttleworth06559d02021-05-19 09:14:26 -0400947 if errored(t, testCase.description, errs) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500948 continue
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500949 }
950
Liz Kammer356f7d42021-01-26 09:18:53 -0500951 checkDir := dir
952 if testCase.dir != "" {
953 checkDir = testCase.dir
954 }
Jingwen Chen164e0862021-02-19 00:48:40 -0500955
956 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -0500957 bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir)
Liz Kammer356f7d42021-01-26 09:18:53 -0500958 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
959 t.Errorf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount)
960 } else {
961 for i, target := range bazelTargets {
962 if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
963 t.Errorf(
964 "%s: Expected generated Bazel target to be '%s', got '%s'",
965 testCase.description,
966 w,
967 g,
968 )
969 }
970 }
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500971 }
972 }
973}
Jingwen Chen041b1842021-02-01 00:23:25 -0500974
975type bp2buildMutator = func(android.TopDownMutatorContext)
976
977func TestBp2BuildInlinesDefaults(t *testing.T) {
978 testCases := []struct {
979 moduleTypesUnderTest map[string]android.ModuleFactory
980 bp2buildMutatorsUnderTest map[string]bp2buildMutator
981 bp string
982 expectedBazelTarget string
983 description string
984 }{
985 {
986 moduleTypesUnderTest: map[string]android.ModuleFactory{
987 "genrule": genrule.GenRuleFactory,
988 "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
989 },
990 bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
991 "genrule": genrule.GenruleBp2Build,
992 },
993 bp: `genrule_defaults {
994 name: "gen_defaults",
995 cmd: "do-something $(in) $(out)",
996}
997genrule {
998 name: "gen",
999 out: ["out"],
1000 srcs: ["in1"],
1001 defaults: ["gen_defaults"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001002 bazel_module: { bp2build_available: true },
Jingwen Chen041b1842021-02-01 00:23:25 -05001003}
1004`,
1005 expectedBazelTarget: `genrule(
1006 name = "gen",
1007 cmd = "do-something $(SRCS) $(OUTS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +00001008 outs = ["out"],
1009 srcs = ["in1"],
Jingwen Chen041b1842021-02-01 00:23:25 -05001010)`,
1011 description: "genrule applies properties from a genrule_defaults dependency if not specified",
1012 },
1013 {
1014 moduleTypesUnderTest: map[string]android.ModuleFactory{
1015 "genrule": genrule.GenRuleFactory,
1016 "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
1017 },
1018 bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
1019 "genrule": genrule.GenruleBp2Build,
1020 },
1021 bp: `genrule_defaults {
1022 name: "gen_defaults",
1023 out: ["out-from-defaults"],
1024 srcs: ["in-from-defaults"],
1025 cmd: "cmd-from-defaults",
1026}
1027genrule {
1028 name: "gen",
1029 out: ["out"],
1030 srcs: ["in1"],
1031 defaults: ["gen_defaults"],
1032 cmd: "do-something $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001033 bazel_module: { bp2build_available: true },
Jingwen Chen041b1842021-02-01 00:23:25 -05001034}
1035`,
1036 expectedBazelTarget: `genrule(
1037 name = "gen",
1038 cmd = "do-something $(SRCS) $(OUTS)",
1039 outs = [
1040 "out-from-defaults",
1041 "out",
1042 ],
1043 srcs = [
1044 "in-from-defaults",
1045 "in1",
1046 ],
1047)`,
1048 description: "genrule does merges properties from a genrule_defaults dependency, latest-first",
1049 },
1050 {
1051 moduleTypesUnderTest: map[string]android.ModuleFactory{
1052 "genrule": genrule.GenRuleFactory,
1053 "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
1054 },
1055 bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
1056 "genrule": genrule.GenruleBp2Build,
1057 },
1058 bp: `genrule_defaults {
1059 name: "gen_defaults1",
1060 cmd: "cp $(in) $(out)",
1061}
1062
1063genrule_defaults {
1064 name: "gen_defaults2",
1065 srcs: ["in1"],
1066}
1067
1068genrule {
1069 name: "gen",
1070 out: ["out"],
1071 defaults: ["gen_defaults1", "gen_defaults2"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001072 bazel_module: { bp2build_available: true },
Jingwen Chen041b1842021-02-01 00:23:25 -05001073}
1074`,
1075 expectedBazelTarget: `genrule(
1076 name = "gen",
1077 cmd = "cp $(SRCS) $(OUTS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +00001078 outs = ["out"],
1079 srcs = ["in1"],
Jingwen Chen041b1842021-02-01 00:23:25 -05001080)`,
1081 description: "genrule applies properties from list of genrule_defaults",
1082 },
1083 {
1084 moduleTypesUnderTest: map[string]android.ModuleFactory{
1085 "genrule": genrule.GenRuleFactory,
1086 "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
1087 },
1088 bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
1089 "genrule": genrule.GenruleBp2Build,
1090 },
1091 bp: `genrule_defaults {
1092 name: "gen_defaults1",
1093 defaults: ["gen_defaults2"],
1094 cmd: "cmd1 $(in) $(out)", // overrides gen_defaults2's cmd property value.
1095}
1096
1097genrule_defaults {
1098 name: "gen_defaults2",
1099 defaults: ["gen_defaults3"],
1100 cmd: "cmd2 $(in) $(out)",
1101 out: ["out-from-2"],
1102 srcs: ["in1"],
1103}
1104
1105genrule_defaults {
1106 name: "gen_defaults3",
1107 out: ["out-from-3"],
1108 srcs: ["srcs-from-3"],
1109}
1110
1111genrule {
1112 name: "gen",
1113 out: ["out"],
1114 defaults: ["gen_defaults1"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001115 bazel_module: { bp2build_available: true },
Jingwen Chen041b1842021-02-01 00:23:25 -05001116}
1117`,
1118 expectedBazelTarget: `genrule(
1119 name = "gen",
1120 cmd = "cmd1 $(SRCS) $(OUTS)",
1121 outs = [
1122 "out-from-3",
1123 "out-from-2",
1124 "out",
1125 ],
1126 srcs = [
Jingwen Chen07027912021-03-15 06:02:43 -04001127 "srcs-from-3",
Liz Kammer9abd62d2021-05-21 08:37:59 -04001128 "in1",
Jingwen Chen041b1842021-02-01 00:23:25 -05001129 ],
1130)`,
1131 description: "genrule applies properties from genrule_defaults transitively",
1132 },
1133 }
1134
1135 dir := "."
1136 for _, testCase := range testCases {
1137 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
1138 ctx := android.NewTestContext(config)
1139 for m, factory := range testCase.moduleTypesUnderTest {
1140 ctx.RegisterModuleType(m, factory)
1141 }
1142 for mutator, f := range testCase.bp2buildMutatorsUnderTest {
1143 ctx.RegisterBp2BuildMutator(mutator, f)
1144 }
1145 ctx.RegisterForBazelConversion()
1146
1147 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
1148 android.FailIfErrored(t, errs)
1149 _, errs = ctx.ResolveDependencies(config)
1150 android.FailIfErrored(t, errs)
1151
Jingwen Chen164e0862021-02-19 00:48:40 -05001152 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -05001153 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
Jingwen Chen041b1842021-02-01 00:23:25 -05001154 if actualCount := len(bazelTargets); actualCount != 1 {
1155 t.Fatalf("%s: Expected 1 bazel target, got %d", testCase.description, actualCount)
1156 }
1157
1158 actualBazelTarget := bazelTargets[0]
1159 if actualBazelTarget.content != testCase.expectedBazelTarget {
1160 t.Errorf(
1161 "%s: Expected generated Bazel target to be '%s', got '%s'",
1162 testCase.description,
1163 testCase.expectedBazelTarget,
1164 actualBazelTarget.content,
1165 )
1166 }
1167 }
1168}
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001169
Jingwen Chen12b4c272021-03-10 02:05:59 -05001170func TestAllowlistingBp2buildTargetsExplicitly(t *testing.T) {
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001171 testCases := []struct {
1172 moduleTypeUnderTest string
1173 moduleTypeUnderTestFactory android.ModuleFactory
1174 moduleTypeUnderTestBp2BuildMutator bp2buildMutator
1175 bp string
1176 expectedCount int
1177 description string
1178 }{
1179 {
1180 description: "explicitly unavailable",
1181 moduleTypeUnderTest: "filegroup",
1182 moduleTypeUnderTestFactory: android.FileGroupFactory,
1183 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1184 bp: `filegroup {
1185 name: "foo",
1186 srcs: ["a", "b"],
1187 bazel_module: { bp2build_available: false },
1188}`,
1189 expectedCount: 0,
1190 },
1191 {
1192 description: "implicitly unavailable",
1193 moduleTypeUnderTest: "filegroup",
1194 moduleTypeUnderTestFactory: android.FileGroupFactory,
1195 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1196 bp: `filegroup {
1197 name: "foo",
1198 srcs: ["a", "b"],
1199}`,
1200 expectedCount: 0,
1201 },
1202 {
1203 description: "explicitly available",
1204 moduleTypeUnderTest: "filegroup",
1205 moduleTypeUnderTestFactory: android.FileGroupFactory,
1206 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1207 bp: `filegroup {
1208 name: "foo",
1209 srcs: ["a", "b"],
1210 bazel_module: { bp2build_available: true },
1211}`,
1212 expectedCount: 1,
1213 },
1214 {
1215 description: "generates more than 1 target if needed",
1216 moduleTypeUnderTest: "custom",
1217 moduleTypeUnderTestFactory: customModuleFactory,
1218 moduleTypeUnderTestBp2BuildMutator: customBp2BuildMutatorFromStarlark,
1219 bp: `custom {
1220 name: "foo",
1221 bazel_module: { bp2build_available: true },
1222}`,
1223 expectedCount: 3,
1224 },
1225 }
1226
1227 dir := "."
1228 for _, testCase := range testCases {
1229 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
1230 ctx := android.NewTestContext(config)
1231 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
1232 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1233 ctx.RegisterForBazelConversion()
1234
1235 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
1236 android.FailIfErrored(t, errs)
1237 _, errs = ctx.ResolveDependencies(config)
1238 android.FailIfErrored(t, errs)
1239
Jingwen Chen164e0862021-02-19 00:48:40 -05001240 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -05001241 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001242 if actualCount := len(bazelTargets); actualCount != testCase.expectedCount {
1243 t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, testCase.expectedCount, actualCount)
1244 }
1245 }
1246}
Liz Kammerba3ea162021-02-17 13:22:03 -05001247
Jingwen Chen12b4c272021-03-10 02:05:59 -05001248func TestAllowlistingBp2buildTargetsWithConfig(t *testing.T) {
1249 testCases := []struct {
1250 moduleTypeUnderTest string
1251 moduleTypeUnderTestFactory android.ModuleFactory
1252 moduleTypeUnderTestBp2BuildMutator bp2buildMutator
1253 expectedCount map[string]int
1254 description string
1255 bp2buildConfig android.Bp2BuildConfig
1256 checkDir string
1257 fs map[string]string
1258 }{
1259 {
1260 description: "test bp2build config package and subpackages config",
1261 moduleTypeUnderTest: "filegroup",
1262 moduleTypeUnderTestFactory: android.FileGroupFactory,
1263 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1264 expectedCount: map[string]int{
1265 "migrated": 1,
1266 "migrated/but_not_really": 0,
1267 "migrated/but_not_really/but_really": 1,
1268 "not_migrated": 0,
1269 "also_not_migrated": 0,
1270 },
1271 bp2buildConfig: android.Bp2BuildConfig{
1272 "migrated": android.Bp2BuildDefaultTrueRecursively,
1273 "migrated/but_not_really": android.Bp2BuildDefaultFalse,
1274 "not_migrated": android.Bp2BuildDefaultFalse,
1275 },
1276 fs: map[string]string{
1277 "migrated/Android.bp": `filegroup { name: "a" }`,
1278 "migrated/but_not_really/Android.bp": `filegroup { name: "b" }`,
1279 "migrated/but_not_really/but_really/Android.bp": `filegroup { name: "c" }`,
1280 "not_migrated/Android.bp": `filegroup { name: "d" }`,
1281 "also_not_migrated/Android.bp": `filegroup { name: "e" }`,
1282 },
1283 },
1284 {
1285 description: "test bp2build config opt-in and opt-out",
1286 moduleTypeUnderTest: "filegroup",
1287 moduleTypeUnderTestFactory: android.FileGroupFactory,
1288 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1289 expectedCount: map[string]int{
1290 "package-opt-in": 2,
1291 "package-opt-in/subpackage": 0,
1292 "package-opt-out": 1,
1293 "package-opt-out/subpackage": 0,
1294 },
1295 bp2buildConfig: android.Bp2BuildConfig{
1296 "package-opt-in": android.Bp2BuildDefaultFalse,
1297 "package-opt-out": android.Bp2BuildDefaultTrueRecursively,
1298 },
1299 fs: map[string]string{
1300 "package-opt-in/Android.bp": `
1301filegroup { name: "opt-in-a" }
1302filegroup { name: "opt-in-b", bazel_module: { bp2build_available: true } }
1303filegroup { name: "opt-in-c", bazel_module: { bp2build_available: true } }
1304`,
1305
1306 "package-opt-in/subpackage/Android.bp": `
1307filegroup { name: "opt-in-d" } // parent package not configured to DefaultTrueRecursively
1308`,
1309
1310 "package-opt-out/Android.bp": `
1311filegroup { name: "opt-out-a" }
1312filegroup { name: "opt-out-b", bazel_module: { bp2build_available: false } }
1313filegroup { name: "opt-out-c", bazel_module: { bp2build_available: false } }
1314`,
1315
1316 "package-opt-out/subpackage/Android.bp": `
1317filegroup { name: "opt-out-g", bazel_module: { bp2build_available: false } }
1318filegroup { name: "opt-out-h", bazel_module: { bp2build_available: false } }
1319`,
1320 },
1321 },
1322 }
1323
1324 dir := "."
1325 for _, testCase := range testCases {
1326 fs := make(map[string][]byte)
1327 toParse := []string{
1328 "Android.bp",
1329 }
1330 for f, content := range testCase.fs {
1331 if strings.HasSuffix(f, "Android.bp") {
1332 toParse = append(toParse, f)
1333 }
1334 fs[f] = []byte(content)
1335 }
1336 config := android.TestConfig(buildDir, nil, "", fs)
1337 ctx := android.NewTestContext(config)
1338 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
1339 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1340 ctx.RegisterBp2BuildConfig(testCase.bp2buildConfig)
1341 ctx.RegisterForBazelConversion()
1342
1343 _, errs := ctx.ParseFileList(dir, toParse)
1344 android.FailIfErrored(t, errs)
1345 _, errs = ctx.ResolveDependencies(config)
1346 android.FailIfErrored(t, errs)
1347
1348 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
1349
1350 // For each directory, test that the expected number of generated targets is correct.
1351 for dir, expectedCount := range testCase.expectedCount {
1352 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
1353 if actualCount := len(bazelTargets); actualCount != expectedCount {
1354 t.Fatalf(
1355 "%s: Expected %d bazel target for %s package, got %d",
1356 testCase.description,
1357 expectedCount,
1358 dir,
1359 actualCount)
1360 }
1361
1362 }
1363 }
1364}
1365
Liz Kammerba3ea162021-02-17 13:22:03 -05001366func TestCombineBuildFilesBp2buildTargets(t *testing.T) {
1367 testCases := []struct {
1368 description string
1369 moduleTypeUnderTest string
1370 moduleTypeUnderTestFactory android.ModuleFactory
1371 moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
1372 preArchMutators []android.RegisterMutatorFunc
1373 depsMutators []android.RegisterMutatorFunc
1374 bp string
1375 expectedBazelTargets []string
1376 fs map[string]string
1377 dir string
1378 }{
1379 {
1380 description: "filegroup bazel_module.label",
1381 moduleTypeUnderTest: "filegroup",
1382 moduleTypeUnderTestFactory: android.FileGroupFactory,
1383 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1384 bp: `filegroup {
1385 name: "fg_foo",
1386 bazel_module: { label: "//other:fg_foo" },
1387}`,
1388 expectedBazelTargets: []string{
1389 `// BUILD file`,
1390 },
1391 fs: map[string]string{
1392 "other/BUILD.bazel": `// BUILD file`,
1393 },
1394 },
1395 {
1396 description: "multiple bazel_module.label same BUILD",
1397 moduleTypeUnderTest: "filegroup",
1398 moduleTypeUnderTestFactory: android.FileGroupFactory,
1399 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1400 bp: `filegroup {
1401 name: "fg_foo",
1402 bazel_module: { label: "//other:fg_foo" },
1403}
1404
1405filegroup {
1406 name: "foo",
1407 bazel_module: { label: "//other:foo" },
1408}`,
1409 expectedBazelTargets: []string{
1410 `// BUILD file`,
1411 },
1412 fs: map[string]string{
1413 "other/BUILD.bazel": `// BUILD file`,
1414 },
1415 },
1416 {
1417 description: "filegroup bazel_module.label and bp2build",
1418 moduleTypeUnderTest: "filegroup",
1419 moduleTypeUnderTestFactory: android.FileGroupFactory,
1420 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1421 bp: `filegroup {
1422 name: "fg_foo",
1423 bazel_module: {
1424 label: "//other:fg_foo",
1425 bp2build_available: true,
1426 },
1427}`,
1428 expectedBazelTargets: []string{
1429 `filegroup(
1430 name = "fg_foo",
1431)`,
1432 `// BUILD file`,
1433 },
1434 fs: map[string]string{
1435 "other/BUILD.bazel": `// BUILD file`,
1436 },
1437 },
1438 {
1439 description: "filegroup bazel_module.label and filegroup bp2build",
1440 moduleTypeUnderTest: "filegroup",
1441 moduleTypeUnderTestFactory: android.FileGroupFactory,
1442 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1443 bp: `filegroup {
1444 name: "fg_foo",
1445 bazel_module: {
1446 label: "//other:fg_foo",
1447 },
1448}
1449
1450filegroup {
1451 name: "fg_bar",
1452 bazel_module: {
1453 bp2build_available: true,
1454 },
1455}`,
1456 expectedBazelTargets: []string{
1457 `filegroup(
1458 name = "fg_bar",
1459)`,
1460 `// BUILD file`,
1461 },
1462 fs: map[string]string{
1463 "other/BUILD.bazel": `// BUILD file`,
1464 },
1465 },
1466 }
1467
1468 dir := "."
1469 for _, testCase := range testCases {
Jingwen Chen49109762021-05-25 05:16:48 +00001470 t.Run(testCase.description, func(t *testing.T) {
1471 fs := make(map[string][]byte)
1472 toParse := []string{
1473 "Android.bp",
Liz Kammerba3ea162021-02-17 13:22:03 -05001474 }
Jingwen Chen49109762021-05-25 05:16:48 +00001475 for f, content := range testCase.fs {
1476 if strings.HasSuffix(f, "Android.bp") {
1477 toParse = append(toParse, f)
1478 }
1479 fs[f] = []byte(content)
1480 }
1481 config := android.TestConfig(buildDir, nil, testCase.bp, fs)
1482 ctx := android.NewTestContext(config)
1483 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
1484 for _, m := range testCase.depsMutators {
1485 ctx.DepsBp2BuildMutators(m)
1486 }
1487 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1488 ctx.RegisterForBazelConversion()
Liz Kammerba3ea162021-02-17 13:22:03 -05001489
Jingwen Chen49109762021-05-25 05:16:48 +00001490 _, errs := ctx.ParseFileList(dir, toParse)
1491 if errored(t, testCase.description, errs) {
1492 return
1493 }
1494 _, errs = ctx.ResolveDependencies(config)
1495 if errored(t, testCase.description, errs) {
1496 return
1497 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001498
Jingwen Chen49109762021-05-25 05:16:48 +00001499 checkDir := dir
1500 if testCase.dir != "" {
1501 checkDir = testCase.dir
1502 }
1503 bazelTargets := generateBazelTargetsForDir(NewCodegenContext(config, *ctx.Context, Bp2Build), checkDir)
1504 bazelTargets.sort()
1505 actualCount := len(bazelTargets)
1506 expectedCount := len(testCase.expectedBazelTargets)
1507 if actualCount != expectedCount {
1508 t.Errorf("Expected %d bazel target, got %d\n%s", expectedCount, actualCount, bazelTargets)
1509 }
1510 if !strings.Contains(bazelTargets.String(), "Section: Handcrafted targets. ") {
1511 t.Errorf("Expected string representation of bazelTargets to contain handcrafted section header.")
1512 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001513 for i, target := range bazelTargets {
Jingwen Chen49109762021-05-25 05:16:48 +00001514 actualContent := target.content
1515 expectedContent := testCase.expectedBazelTargets[i]
1516 if expectedContent != actualContent {
Liz Kammerba3ea162021-02-17 13:22:03 -05001517 t.Errorf(
Jingwen Chen49109762021-05-25 05:16:48 +00001518 "Expected generated Bazel target to be '%s', got '%s'",
1519 expectedContent,
1520 actualContent,
Liz Kammerba3ea162021-02-17 13:22:03 -05001521 )
1522 }
1523 }
Jingwen Chen49109762021-05-25 05:16:48 +00001524 })
Liz Kammerba3ea162021-02-17 13:22:03 -05001525 }
1526}
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001527
1528func TestGlobExcludeSrcs(t *testing.T) {
1529 testCases := []struct {
1530 description string
1531 moduleTypeUnderTest string
1532 moduleTypeUnderTestFactory android.ModuleFactory
1533 moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
1534 bp string
1535 expectedBazelTargets []string
1536 fs map[string]string
1537 dir string
1538 }{
1539 {
1540 description: "filegroup top level exclude_srcs",
1541 moduleTypeUnderTest: "filegroup",
1542 moduleTypeUnderTestFactory: android.FileGroupFactory,
1543 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1544 bp: `filegroup {
1545 name: "fg_foo",
1546 srcs: ["**/*.txt"],
1547 exclude_srcs: ["c.txt"],
1548 bazel_module: { bp2build_available: true },
1549}`,
1550 expectedBazelTargets: []string{`filegroup(
1551 name = "fg_foo",
1552 srcs = [
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001553 "a.txt",
1554 "b.txt",
Liz Kammer9abd62d2021-05-21 08:37:59 -04001555 "//dir:e.txt",
1556 "//dir:f.txt",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001557 ],
1558)`,
1559 },
1560 fs: map[string]string{
1561 "a.txt": "",
1562 "b.txt": "",
1563 "c.txt": "",
1564 "dir/Android.bp": "",
1565 "dir/e.txt": "",
1566 "dir/f.txt": "",
1567 },
1568 },
1569 {
1570 description: "filegroup in subdir exclude_srcs",
1571 moduleTypeUnderTest: "filegroup",
1572 moduleTypeUnderTestFactory: android.FileGroupFactory,
1573 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1574 bp: "",
1575 dir: "dir",
1576 fs: map[string]string{
1577 "dir/Android.bp": `filegroup {
1578 name: "fg_foo",
1579 srcs: ["**/*.txt"],
1580 exclude_srcs: ["b.txt"],
1581 bazel_module: { bp2build_available: true },
1582}
1583`,
1584 "dir/a.txt": "",
1585 "dir/b.txt": "",
1586 "dir/subdir/Android.bp": "",
1587 "dir/subdir/e.txt": "",
1588 "dir/subdir/f.txt": "",
1589 },
1590 expectedBazelTargets: []string{`filegroup(
1591 name = "fg_foo",
1592 srcs = [
Liz Kammer9abd62d2021-05-21 08:37:59 -04001593 "a.txt",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001594 "//dir/subdir:e.txt",
1595 "//dir/subdir:f.txt",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001596 ],
1597)`,
1598 },
1599 },
1600 }
1601
1602 dir := "."
1603 for _, testCase := range testCases {
1604 fs := make(map[string][]byte)
1605 toParse := []string{
1606 "Android.bp",
1607 }
1608 for f, content := range testCase.fs {
1609 if strings.HasSuffix(f, "Android.bp") {
1610 toParse = append(toParse, f)
1611 }
1612 fs[f] = []byte(content)
1613 }
1614 config := android.TestConfig(buildDir, nil, testCase.bp, fs)
1615 ctx := android.NewTestContext(config)
1616 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
1617 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1618 ctx.RegisterForBazelConversion()
1619
1620 _, errs := ctx.ParseFileList(dir, toParse)
Rupert Shuttleworth06559d02021-05-19 09:14:26 -04001621 if errored(t, testCase.description, errs) {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001622 continue
1623 }
1624 _, errs = ctx.ResolveDependencies(config)
Rupert Shuttleworth06559d02021-05-19 09:14:26 -04001625 if errored(t, testCase.description, errs) {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001626 continue
1627 }
1628
1629 checkDir := dir
1630 if testCase.dir != "" {
1631 checkDir = testCase.dir
1632 }
1633 bazelTargets := generateBazelTargetsForDir(NewCodegenContext(config, *ctx.Context, Bp2Build), checkDir)
1634 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
1635 t.Errorf("%s: Expected %d bazel target, got %d\n%s", testCase.description, expectedCount, actualCount, bazelTargets)
1636 } else {
1637 for i, target := range bazelTargets {
1638 if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
1639 t.Errorf(
1640 "%s: Expected generated Bazel target to be '%s', got '%s'",
1641 testCase.description,
1642 w,
1643 g,
1644 )
1645 }
1646 }
1647 }
1648 }
1649}