blob: 0d9106c968aa7b2c7556218154aff499e549d939 [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
Jingwen Chen40067de2021-01-26 21:58:43 -0500483proto_library(
484 name = "bar_proto_library_deps",
Liz Kammer2ada09a2021-08-11 00:17:36 -0400485)
486
487my_proto_library(
488 name = "bar_my_proto_library_deps",
Jingwen Chen40067de2021-01-26 21:58:43 -0500489)`,
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 {
Liz Kammer2ada09a2021-08-11 00:17:36 -04001216 t.Run(testCase.description, func(t *testing.T) {
1217 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
1218 ctx := android.NewTestContext(config)
1219 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
1220 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1221 ctx.RegisterForBazelConversion()
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001222
Liz Kammer2ada09a2021-08-11 00:17:36 -04001223 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
1224 android.FailIfErrored(t, errs)
1225 _, errs = ctx.ResolveDependencies(config)
1226 android.FailIfErrored(t, errs)
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001227
Liz Kammer2ada09a2021-08-11 00:17:36 -04001228 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
1229 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
1230 if actualCount := len(bazelTargets); actualCount != testCase.expectedCount {
1231 t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, testCase.expectedCount, actualCount)
1232 }
1233 })
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001234 }
1235}
Liz Kammerba3ea162021-02-17 13:22:03 -05001236
Jingwen Chen12b4c272021-03-10 02:05:59 -05001237func TestAllowlistingBp2buildTargetsWithConfig(t *testing.T) {
1238 testCases := []struct {
1239 moduleTypeUnderTest string
1240 moduleTypeUnderTestFactory android.ModuleFactory
1241 moduleTypeUnderTestBp2BuildMutator bp2buildMutator
1242 expectedCount map[string]int
1243 description string
1244 bp2buildConfig android.Bp2BuildConfig
1245 checkDir string
1246 fs map[string]string
1247 }{
1248 {
1249 description: "test bp2build config package and subpackages config",
1250 moduleTypeUnderTest: "filegroup",
1251 moduleTypeUnderTestFactory: android.FileGroupFactory,
1252 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1253 expectedCount: map[string]int{
1254 "migrated": 1,
1255 "migrated/but_not_really": 0,
1256 "migrated/but_not_really/but_really": 1,
1257 "not_migrated": 0,
1258 "also_not_migrated": 0,
1259 },
1260 bp2buildConfig: android.Bp2BuildConfig{
1261 "migrated": android.Bp2BuildDefaultTrueRecursively,
1262 "migrated/but_not_really": android.Bp2BuildDefaultFalse,
1263 "not_migrated": android.Bp2BuildDefaultFalse,
1264 },
1265 fs: map[string]string{
1266 "migrated/Android.bp": `filegroup { name: "a" }`,
1267 "migrated/but_not_really/Android.bp": `filegroup { name: "b" }`,
1268 "migrated/but_not_really/but_really/Android.bp": `filegroup { name: "c" }`,
1269 "not_migrated/Android.bp": `filegroup { name: "d" }`,
1270 "also_not_migrated/Android.bp": `filegroup { name: "e" }`,
1271 },
1272 },
1273 {
1274 description: "test bp2build config opt-in and opt-out",
1275 moduleTypeUnderTest: "filegroup",
1276 moduleTypeUnderTestFactory: android.FileGroupFactory,
1277 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1278 expectedCount: map[string]int{
1279 "package-opt-in": 2,
1280 "package-opt-in/subpackage": 0,
1281 "package-opt-out": 1,
1282 "package-opt-out/subpackage": 0,
1283 },
1284 bp2buildConfig: android.Bp2BuildConfig{
1285 "package-opt-in": android.Bp2BuildDefaultFalse,
1286 "package-opt-out": android.Bp2BuildDefaultTrueRecursively,
1287 },
1288 fs: map[string]string{
1289 "package-opt-in/Android.bp": `
1290filegroup { name: "opt-in-a" }
1291filegroup { name: "opt-in-b", bazel_module: { bp2build_available: true } }
1292filegroup { name: "opt-in-c", bazel_module: { bp2build_available: true } }
1293`,
1294
1295 "package-opt-in/subpackage/Android.bp": `
1296filegroup { name: "opt-in-d" } // parent package not configured to DefaultTrueRecursively
1297`,
1298
1299 "package-opt-out/Android.bp": `
1300filegroup { name: "opt-out-a" }
1301filegroup { name: "opt-out-b", bazel_module: { bp2build_available: false } }
1302filegroup { name: "opt-out-c", bazel_module: { bp2build_available: false } }
1303`,
1304
1305 "package-opt-out/subpackage/Android.bp": `
1306filegroup { name: "opt-out-g", bazel_module: { bp2build_available: false } }
1307filegroup { name: "opt-out-h", bazel_module: { bp2build_available: false } }
1308`,
1309 },
1310 },
1311 }
1312
1313 dir := "."
1314 for _, testCase := range testCases {
1315 fs := make(map[string][]byte)
1316 toParse := []string{
1317 "Android.bp",
1318 }
1319 for f, content := range testCase.fs {
1320 if strings.HasSuffix(f, "Android.bp") {
1321 toParse = append(toParse, f)
1322 }
1323 fs[f] = []byte(content)
1324 }
1325 config := android.TestConfig(buildDir, nil, "", fs)
1326 ctx := android.NewTestContext(config)
1327 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
1328 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1329 ctx.RegisterBp2BuildConfig(testCase.bp2buildConfig)
1330 ctx.RegisterForBazelConversion()
1331
1332 _, errs := ctx.ParseFileList(dir, toParse)
1333 android.FailIfErrored(t, errs)
1334 _, errs = ctx.ResolveDependencies(config)
1335 android.FailIfErrored(t, errs)
1336
1337 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
1338
1339 // For each directory, test that the expected number of generated targets is correct.
1340 for dir, expectedCount := range testCase.expectedCount {
1341 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
1342 if actualCount := len(bazelTargets); actualCount != expectedCount {
1343 t.Fatalf(
1344 "%s: Expected %d bazel target for %s package, got %d",
1345 testCase.description,
1346 expectedCount,
1347 dir,
1348 actualCount)
1349 }
1350
1351 }
1352 }
1353}
1354
Liz Kammerba3ea162021-02-17 13:22:03 -05001355func TestCombineBuildFilesBp2buildTargets(t *testing.T) {
1356 testCases := []struct {
1357 description string
1358 moduleTypeUnderTest string
1359 moduleTypeUnderTestFactory android.ModuleFactory
1360 moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
1361 preArchMutators []android.RegisterMutatorFunc
Liz Kammerba3ea162021-02-17 13:22:03 -05001362 bp string
1363 expectedBazelTargets []string
1364 fs map[string]string
1365 dir string
1366 }{
1367 {
1368 description: "filegroup bazel_module.label",
1369 moduleTypeUnderTest: "filegroup",
1370 moduleTypeUnderTestFactory: android.FileGroupFactory,
1371 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1372 bp: `filegroup {
1373 name: "fg_foo",
1374 bazel_module: { label: "//other:fg_foo" },
1375}`,
1376 expectedBazelTargets: []string{
1377 `// BUILD file`,
1378 },
1379 fs: map[string]string{
1380 "other/BUILD.bazel": `// BUILD file`,
1381 },
1382 },
1383 {
1384 description: "multiple bazel_module.label same BUILD",
1385 moduleTypeUnderTest: "filegroup",
1386 moduleTypeUnderTestFactory: android.FileGroupFactory,
1387 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1388 bp: `filegroup {
Jingwen Chenc63677b2021-06-17 05:43:19 +00001389 name: "fg_foo",
1390 bazel_module: { label: "//other:fg_foo" },
1391 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001392
Jingwen Chenc63677b2021-06-17 05:43:19 +00001393 filegroup {
1394 name: "foo",
1395 bazel_module: { label: "//other:foo" },
1396 }`,
Liz Kammerba3ea162021-02-17 13:22:03 -05001397 expectedBazelTargets: []string{
1398 `// BUILD file`,
1399 },
1400 fs: map[string]string{
1401 "other/BUILD.bazel": `// BUILD file`,
1402 },
1403 },
1404 {
Jingwen Chenc63677b2021-06-17 05:43:19 +00001405 description: "filegroup bazel_module.label and bp2build in subdir",
Liz Kammerba3ea162021-02-17 13:22:03 -05001406 moduleTypeUnderTest: "filegroup",
1407 moduleTypeUnderTestFactory: android.FileGroupFactory,
1408 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chenc63677b2021-06-17 05:43:19 +00001409 dir: "other",
1410 bp: ``,
1411 fs: map[string]string{
1412 "other/Android.bp": `filegroup {
1413 name: "fg_foo",
1414 bazel_module: {
1415 bp2build_available: true,
1416 },
1417 }
1418 filegroup {
1419 name: "fg_bar",
1420 bazel_module: {
1421 label: "//other:fg_bar"
1422 },
1423 }`,
1424 "other/BUILD.bazel": `// definition for fg_bar`,
1425 },
Liz Kammerba3ea162021-02-17 13:22:03 -05001426 expectedBazelTargets: []string{
1427 `filegroup(
1428 name = "fg_foo",
Jingwen Chenc63677b2021-06-17 05:43:19 +00001429)`, `// definition for fg_bar`,
Liz Kammerba3ea162021-02-17 13:22:03 -05001430 },
1431 },
1432 {
1433 description: "filegroup bazel_module.label and filegroup bp2build",
1434 moduleTypeUnderTest: "filegroup",
1435 moduleTypeUnderTestFactory: android.FileGroupFactory,
1436 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1437 bp: `filegroup {
Jingwen Chenc63677b2021-06-17 05:43:19 +00001438 name: "fg_foo",
1439 bazel_module: {
1440 label: "//other:fg_foo",
1441 },
1442 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001443
Jingwen Chenc63677b2021-06-17 05:43:19 +00001444 filegroup {
1445 name: "fg_bar",
1446 bazel_module: {
1447 bp2build_available: true,
1448 },
1449 }`,
Liz Kammerba3ea162021-02-17 13:22:03 -05001450 expectedBazelTargets: []string{
1451 `filegroup(
1452 name = "fg_bar",
1453)`,
1454 `// BUILD file`,
1455 },
1456 fs: map[string]string{
1457 "other/BUILD.bazel": `// BUILD file`,
1458 },
1459 },
1460 }
1461
1462 dir := "."
1463 for _, testCase := range testCases {
Jingwen Chen49109762021-05-25 05:16:48 +00001464 t.Run(testCase.description, func(t *testing.T) {
1465 fs := make(map[string][]byte)
1466 toParse := []string{
1467 "Android.bp",
Liz Kammerba3ea162021-02-17 13:22:03 -05001468 }
Jingwen Chen49109762021-05-25 05:16:48 +00001469 for f, content := range testCase.fs {
1470 if strings.HasSuffix(f, "Android.bp") {
1471 toParse = append(toParse, f)
1472 }
1473 fs[f] = []byte(content)
1474 }
1475 config := android.TestConfig(buildDir, nil, testCase.bp, fs)
1476 ctx := android.NewTestContext(config)
1477 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Jingwen Chen49109762021-05-25 05:16:48 +00001478 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1479 ctx.RegisterForBazelConversion()
Liz Kammerba3ea162021-02-17 13:22:03 -05001480
Jingwen Chen49109762021-05-25 05:16:48 +00001481 _, errs := ctx.ParseFileList(dir, toParse)
1482 if errored(t, testCase.description, errs) {
1483 return
1484 }
1485 _, errs = ctx.ResolveDependencies(config)
1486 if errored(t, testCase.description, errs) {
1487 return
1488 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001489
Jingwen Chen49109762021-05-25 05:16:48 +00001490 checkDir := dir
1491 if testCase.dir != "" {
1492 checkDir = testCase.dir
1493 }
1494 bazelTargets := generateBazelTargetsForDir(NewCodegenContext(config, *ctx.Context, Bp2Build), checkDir)
1495 bazelTargets.sort()
1496 actualCount := len(bazelTargets)
1497 expectedCount := len(testCase.expectedBazelTargets)
1498 if actualCount != expectedCount {
1499 t.Errorf("Expected %d bazel target, got %d\n%s", expectedCount, actualCount, bazelTargets)
1500 }
1501 if !strings.Contains(bazelTargets.String(), "Section: Handcrafted targets. ") {
1502 t.Errorf("Expected string representation of bazelTargets to contain handcrafted section header.")
1503 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001504 for i, target := range bazelTargets {
Jingwen Chen49109762021-05-25 05:16:48 +00001505 actualContent := target.content
1506 expectedContent := testCase.expectedBazelTargets[i]
1507 if expectedContent != actualContent {
Liz Kammerba3ea162021-02-17 13:22:03 -05001508 t.Errorf(
Jingwen Chen49109762021-05-25 05:16:48 +00001509 "Expected generated Bazel target to be '%s', got '%s'",
1510 expectedContent,
1511 actualContent,
Liz Kammerba3ea162021-02-17 13:22:03 -05001512 )
1513 }
1514 }
Jingwen Chen49109762021-05-25 05:16:48 +00001515 })
Liz Kammerba3ea162021-02-17 13:22:03 -05001516 }
1517}
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001518
1519func TestGlobExcludeSrcs(t *testing.T) {
1520 testCases := []struct {
1521 description string
1522 moduleTypeUnderTest string
1523 moduleTypeUnderTestFactory android.ModuleFactory
1524 moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
1525 bp string
1526 expectedBazelTargets []string
1527 fs map[string]string
1528 dir string
1529 }{
1530 {
1531 description: "filegroup top level exclude_srcs",
1532 moduleTypeUnderTest: "filegroup",
1533 moduleTypeUnderTestFactory: android.FileGroupFactory,
1534 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1535 bp: `filegroup {
1536 name: "fg_foo",
1537 srcs: ["**/*.txt"],
1538 exclude_srcs: ["c.txt"],
1539 bazel_module: { bp2build_available: true },
1540}`,
1541 expectedBazelTargets: []string{`filegroup(
1542 name = "fg_foo",
1543 srcs = [
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001544 "a.txt",
1545 "b.txt",
Liz Kammer9abd62d2021-05-21 08:37:59 -04001546 "//dir:e.txt",
1547 "//dir:f.txt",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001548 ],
1549)`,
1550 },
1551 fs: map[string]string{
1552 "a.txt": "",
1553 "b.txt": "",
1554 "c.txt": "",
1555 "dir/Android.bp": "",
1556 "dir/e.txt": "",
1557 "dir/f.txt": "",
1558 },
1559 },
1560 {
1561 description: "filegroup in subdir exclude_srcs",
1562 moduleTypeUnderTest: "filegroup",
1563 moduleTypeUnderTestFactory: android.FileGroupFactory,
1564 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1565 bp: "",
1566 dir: "dir",
1567 fs: map[string]string{
1568 "dir/Android.bp": `filegroup {
1569 name: "fg_foo",
1570 srcs: ["**/*.txt"],
1571 exclude_srcs: ["b.txt"],
1572 bazel_module: { bp2build_available: true },
1573}
1574`,
1575 "dir/a.txt": "",
1576 "dir/b.txt": "",
1577 "dir/subdir/Android.bp": "",
1578 "dir/subdir/e.txt": "",
1579 "dir/subdir/f.txt": "",
1580 },
1581 expectedBazelTargets: []string{`filegroup(
1582 name = "fg_foo",
1583 srcs = [
Liz Kammer9abd62d2021-05-21 08:37:59 -04001584 "a.txt",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001585 "//dir/subdir:e.txt",
1586 "//dir/subdir:f.txt",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001587 ],
1588)`,
1589 },
1590 },
1591 }
1592
1593 dir := "."
1594 for _, testCase := range testCases {
1595 fs := make(map[string][]byte)
1596 toParse := []string{
1597 "Android.bp",
1598 }
1599 for f, content := range testCase.fs {
1600 if strings.HasSuffix(f, "Android.bp") {
1601 toParse = append(toParse, f)
1602 }
1603 fs[f] = []byte(content)
1604 }
1605 config := android.TestConfig(buildDir, nil, testCase.bp, fs)
1606 ctx := android.NewTestContext(config)
1607 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
1608 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1609 ctx.RegisterForBazelConversion()
1610
1611 _, errs := ctx.ParseFileList(dir, toParse)
Rupert Shuttleworth06559d02021-05-19 09:14:26 -04001612 if errored(t, testCase.description, errs) {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001613 continue
1614 }
1615 _, errs = ctx.ResolveDependencies(config)
Rupert Shuttleworth06559d02021-05-19 09:14:26 -04001616 if errored(t, testCase.description, errs) {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001617 continue
1618 }
1619
1620 checkDir := dir
1621 if testCase.dir != "" {
1622 checkDir = testCase.dir
1623 }
1624 bazelTargets := generateBazelTargetsForDir(NewCodegenContext(config, *ctx.Context, Bp2Build), checkDir)
1625 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
1626 t.Errorf("%s: Expected %d bazel target, got %d\n%s", testCase.description, expectedCount, actualCount, bazelTargets)
1627 } else {
1628 for i, target := range bazelTargets {
1629 if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
1630 t.Errorf(
1631 "%s: Expected generated Bazel target to be '%s', got '%s'",
1632 testCase.description,
1633 w,
1634 g,
1635 )
1636 }
1637 }
1638 }
1639 }
1640}