blob: e904627c20927ba7f8ab76978b183903f55f5484 [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"
Liz Kammer6eff3232021-08-26 08:37:59 -040019 "fmt"
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)
Liz Kammer6eff3232021-08-26 08:37:59 -0400203 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
204 android.FailIfErrored(t, err)
Liz Kammerd366c902021-06-03 13:43:01 -0400205 if actualCount, expectedCount := len(bazelTargets), 1; actualCount != expectedCount {
206 t.Fatalf("Expected %d bazel target, got %d", expectedCount, actualCount)
207 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800208
Liz Kammerd366c902021-06-03 13:43:01 -0400209 actualBazelTarget := bazelTargets[0]
210 if actualBazelTarget.content != testCase.expectedBazelTarget {
211 t.Errorf(
212 "Expected generated Bazel target to be '%s', got '%s'",
213 testCase.expectedBazelTarget,
214 actualBazelTarget.content,
215 )
216 }
217 })
Jingwen Chen73850672020-12-14 08:25:34 -0500218 }
219}
220
221func TestGenerateBazelTargetModules(t *testing.T) {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000222 testCases := []bp2buildTestCase{
Jingwen Chen73850672020-12-14 08:25:34 -0500223 {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000224 blueprint: `custom {
Jingwen Chen73850672020-12-14 08:25:34 -0500225 name: "foo",
226 string_list_prop: ["a", "b"],
227 string_prop: "a",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500228 bazel_module: { bp2build_available: true },
Jingwen Chen73850672020-12-14 08:25:34 -0500229}`,
Liz Kammer4562a3b2021-04-21 18:15:34 -0400230 expectedBazelTargets: []string{`custom(
Jingwen Chen73850672020-12-14 08:25:34 -0500231 name = "foo",
232 string_list_prop = [
233 "a",
234 "b",
235 ],
236 string_prop = "a",
237)`,
Liz Kammer4562a3b2021-04-21 18:15:34 -0400238 },
Jingwen Chen73850672020-12-14 08:25:34 -0500239 },
Jingwen Chen58a12b82021-03-30 13:08:36 +0000240 {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000241 blueprint: `custom {
Jingwen Chen58a12b82021-03-30 13:08:36 +0000242 name: "control_characters",
243 string_list_prop: ["\t", "\n"],
244 string_prop: "a\t\n\r",
245 bazel_module: { bp2build_available: true },
246}`,
Liz Kammer4562a3b2021-04-21 18:15:34 -0400247 expectedBazelTargets: []string{`custom(
Jingwen Chen58a12b82021-03-30 13:08:36 +0000248 name = "control_characters",
249 string_list_prop = [
250 "\t",
251 "\n",
252 ],
253 string_prop = "a\t\n\r",
254)`,
Liz Kammer4562a3b2021-04-21 18:15:34 -0400255 },
256 },
257 {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000258 blueprint: `custom {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400259 name: "has_dep",
260 arch_paths: [":dep"],
261 bazel_module: { bp2build_available: true },
262}
263
264custom {
265 name: "dep",
266 arch_paths: ["abc"],
267 bazel_module: { bp2build_available: true },
268}`,
269 expectedBazelTargets: []string{`custom(
270 name = "dep",
271 arch_paths = ["abc"],
272)`,
273 `custom(
274 name = "has_dep",
275 arch_paths = [":dep"],
276)`,
277 },
278 },
279 {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000280 blueprint: `custom {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400281 name: "arch_paths",
282 arch: {
283 x86: {
284 arch_paths: ["abc"],
285 },
286 },
287 bazel_module: { bp2build_available: true },
288}`,
289 expectedBazelTargets: []string{`custom(
290 name = "arch_paths",
291 arch_paths = select({
292 "//build/bazel/platforms/arch:x86": ["abc"],
293 "//conditions:default": [],
294 }),
295)`,
296 },
297 },
298 {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000299 blueprint: `custom {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400300 name: "has_dep",
301 arch: {
302 x86: {
303 arch_paths: [":dep"],
304 },
305 },
306 bazel_module: { bp2build_available: true },
307}
308
309custom {
310 name: "dep",
311 arch_paths: ["abc"],
312 bazel_module: { bp2build_available: true },
313}`,
314 expectedBazelTargets: []string{`custom(
315 name = "dep",
316 arch_paths = ["abc"],
317)`,
318 `custom(
319 name = "has_dep",
320 arch_paths = select({
321 "//build/bazel/platforms/arch:x86": [":dep"],
322 "//conditions:default": [],
323 }),
324)`,
325 },
Jingwen Chen58a12b82021-03-30 13:08:36 +0000326 },
Liz Kammer32a03392021-09-14 11:17:21 -0400327 {
328 blueprint: `custom {
329 name: "embedded_props",
330 embedded_prop: "abc",
331 bazel_module: { bp2build_available: true },
332}`,
333 expectedBazelTargets: []string{`custom(
334 name = "embedded_props",
335 embedded_attr = "abc",
336)`,
337 },
338 },
339 {
340 blueprint: `custom {
341 name: "ptr_to_embedded_props",
342 other_embedded_prop: "abc",
343 bazel_module: { bp2build_available: true },
344}`,
345 expectedBazelTargets: []string{`custom(
346 name = "ptr_to_embedded_props",
347 other_embedded_attr = "abc",
348)`,
349 },
350 },
Jingwen Chen73850672020-12-14 08:25:34 -0500351 }
352
353 dir := "."
354 for _, testCase := range testCases {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000355 config := android.TestConfig(buildDir, nil, testCase.blueprint, nil)
Jingwen Chen73850672020-12-14 08:25:34 -0500356 ctx := android.NewTestContext(config)
Jingwen Chen164e0862021-02-19 00:48:40 -0500357
Liz Kammer32b77cf2021-08-04 15:17:02 -0400358 registerCustomModuleForBp2buildConversion(ctx)
Jingwen Chen73850672020-12-14 08:25:34 -0500359
360 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
Jingwen Chen5146ac02021-09-02 11:44:42 +0000361 if errored(t, testCase, errs) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500362 continue
363 }
Jingwen Chen73850672020-12-14 08:25:34 -0500364 _, errs = ctx.ResolveDependencies(config)
Jingwen Chen5146ac02021-09-02 11:44:42 +0000365 if errored(t, testCase, errs) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500366 continue
367 }
Jingwen Chen73850672020-12-14 08:25:34 -0500368
Jingwen Chen164e0862021-02-19 00:48:40 -0500369 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Liz Kammer6eff3232021-08-26 08:37:59 -0400370 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
371 android.FailIfErrored(t, err)
Jingwen Chen164e0862021-02-19 00:48:40 -0500372
Liz Kammer4562a3b2021-04-21 18:15:34 -0400373 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
Liz Kammer356f7d42021-01-26 09:18:53 -0500374 t.Errorf("Expected %d bazel target, got %d", expectedCount, actualCount)
375 } else {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400376 for i, expectedBazelTarget := range testCase.expectedBazelTargets {
377 actualBazelTarget := bazelTargets[i]
378 if actualBazelTarget.content != expectedBazelTarget {
379 t.Errorf(
380 "Expected generated Bazel target to be '%s', got '%s'",
381 expectedBazelTarget,
382 actualBazelTarget.content,
383 )
384 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500385 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800386 }
387 }
388}
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500389
Jingwen Chen40067de2021-01-26 21:58:43 -0500390func TestLoadStatements(t *testing.T) {
391 testCases := []struct {
392 bazelTargets BazelTargets
393 expectedLoadStatements string
394 }{
395 {
396 bazelTargets: BazelTargets{
397 BazelTarget{
398 name: "foo",
399 ruleClass: "cc_library",
400 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
401 },
402 },
403 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
404 },
405 {
406 bazelTargets: BazelTargets{
407 BazelTarget{
408 name: "foo",
409 ruleClass: "cc_library",
410 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
411 },
412 BazelTarget{
413 name: "bar",
414 ruleClass: "cc_library",
415 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
416 },
417 },
418 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
419 },
420 {
421 bazelTargets: BazelTargets{
422 BazelTarget{
423 name: "foo",
424 ruleClass: "cc_library",
425 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
426 },
427 BazelTarget{
428 name: "bar",
429 ruleClass: "cc_binary",
430 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
431 },
432 },
433 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")`,
434 },
435 {
436 bazelTargets: BazelTargets{
437 BazelTarget{
438 name: "foo",
439 ruleClass: "cc_library",
440 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
441 },
442 BazelTarget{
443 name: "bar",
444 ruleClass: "cc_binary",
445 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
446 },
447 BazelTarget{
448 name: "baz",
449 ruleClass: "java_binary",
450 bzlLoadLocation: "//build/bazel/rules:java.bzl",
451 },
452 },
453 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")
454load("//build/bazel/rules:java.bzl", "java_binary")`,
455 },
456 {
457 bazelTargets: BazelTargets{
458 BazelTarget{
459 name: "foo",
460 ruleClass: "cc_binary",
461 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
462 },
463 BazelTarget{
464 name: "bar",
465 ruleClass: "java_binary",
466 bzlLoadLocation: "//build/bazel/rules:java.bzl",
467 },
468 BazelTarget{
469 name: "baz",
470 ruleClass: "genrule",
471 // Note: no bzlLoadLocation for native rules
472 },
473 },
474 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary")
475load("//build/bazel/rules:java.bzl", "java_binary")`,
476 },
477 }
478
479 for _, testCase := range testCases {
480 actual := testCase.bazelTargets.LoadStatements()
481 expected := testCase.expectedLoadStatements
482 if actual != expected {
483 t.Fatalf("Expected load statements to be %s, got %s", expected, actual)
484 }
485 }
486
487}
488
489func TestGenerateBazelTargetModules_OneToMany_LoadedFromStarlark(t *testing.T) {
490 testCases := []struct {
491 bp string
492 expectedBazelTarget string
493 expectedBazelTargetCount int
494 expectedLoadStatements string
495 }{
496 {
497 bp: `custom {
498 name: "bar",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500499 bazel_module: { bp2build_available: true },
Jingwen Chen40067de2021-01-26 21:58:43 -0500500}`,
501 expectedBazelTarget: `my_library(
502 name = "bar",
503)
504
Jingwen Chen40067de2021-01-26 21:58:43 -0500505proto_library(
506 name = "bar_proto_library_deps",
Liz Kammer2ada09a2021-08-11 00:17:36 -0400507)
508
509my_proto_library(
510 name = "bar_my_proto_library_deps",
Jingwen Chen40067de2021-01-26 21:58:43 -0500511)`,
512 expectedBazelTargetCount: 3,
513 expectedLoadStatements: `load("//build/bazel/rules:proto.bzl", "my_proto_library", "proto_library")
514load("//build/bazel/rules:rules.bzl", "my_library")`,
515 },
516 }
517
518 dir := "."
519 for _, testCase := range testCases {
520 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
521 ctx := android.NewTestContext(config)
522 ctx.RegisterModuleType("custom", customModuleFactory)
Jingwen Chen12b4c272021-03-10 02:05:59 -0500523 ctx.RegisterBp2BuildMutator("custom", customBp2BuildMutatorFromStarlark)
Jingwen Chen40067de2021-01-26 21:58:43 -0500524 ctx.RegisterForBazelConversion()
525
526 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
527 android.FailIfErrored(t, errs)
528 _, errs = ctx.ResolveDependencies(config)
529 android.FailIfErrored(t, errs)
530
Jingwen Chen164e0862021-02-19 00:48:40 -0500531 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Liz Kammer6eff3232021-08-26 08:37:59 -0400532 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
533 android.FailIfErrored(t, err)
Jingwen Chen40067de2021-01-26 21:58:43 -0500534 if actualCount := len(bazelTargets); actualCount != testCase.expectedBazelTargetCount {
535 t.Fatalf("Expected %d bazel target, got %d", testCase.expectedBazelTargetCount, actualCount)
536 }
537
538 actualBazelTargets := bazelTargets.String()
539 if actualBazelTargets != testCase.expectedBazelTarget {
540 t.Errorf(
541 "Expected generated Bazel target to be '%s', got '%s'",
542 testCase.expectedBazelTarget,
543 actualBazelTargets,
544 )
545 }
546
547 actualLoadStatements := bazelTargets.LoadStatements()
548 if actualLoadStatements != testCase.expectedLoadStatements {
549 t.Errorf(
550 "Expected generated load statements to be '%s', got '%s'",
551 testCase.expectedLoadStatements,
552 actualLoadStatements,
553 )
554 }
555 }
556}
557
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500558func TestModuleTypeBp2Build(t *testing.T) {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000559 testCases := []bp2buildTestCase{
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500560 {
Liz Kammerebfcf672021-02-16 15:00:05 -0500561 description: "filegroup with does not specify srcs",
562 moduleTypeUnderTest: "filegroup",
563 moduleTypeUnderTestFactory: android.FileGroupFactory,
564 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000565 blueprint: `filegroup {
Liz Kammerebfcf672021-02-16 15:00:05 -0500566 name: "fg_foo",
567 bazel_module: { bp2build_available: true },
568}`,
569 expectedBazelTargets: []string{
570 `filegroup(
571 name = "fg_foo",
572)`,
573 },
574 },
575 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500576 description: "filegroup with no srcs",
577 moduleTypeUnderTest: "filegroup",
578 moduleTypeUnderTestFactory: android.FileGroupFactory,
579 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000580 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500581 name: "fg_foo",
582 srcs: [],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500583 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500584}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500585 expectedBazelTargets: []string{
586 `filegroup(
587 name = "fg_foo",
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500588)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500589 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500590 },
591 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500592 description: "filegroup with srcs",
593 moduleTypeUnderTest: "filegroup",
594 moduleTypeUnderTestFactory: android.FileGroupFactory,
595 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000596 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500597 name: "fg_foo",
598 srcs: ["a", "b"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500599 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500600}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500601 expectedBazelTargets: []string{`filegroup(
602 name = "fg_foo",
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500603 srcs = [
604 "a",
605 "b",
606 ],
607)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500608 },
609 },
610 {
611 description: "filegroup with excludes srcs",
612 moduleTypeUnderTest: "filegroup",
613 moduleTypeUnderTestFactory: android.FileGroupFactory,
614 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000615 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500616 name: "fg_foo",
617 srcs: ["a", "b"],
618 exclude_srcs: ["a"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500619 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500620}`,
621 expectedBazelTargets: []string{`filegroup(
622 name = "fg_foo",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000623 srcs = ["b"],
Liz Kammer356f7d42021-01-26 09:18:53 -0500624)`,
625 },
626 },
627 {
628 description: "filegroup with glob",
629 moduleTypeUnderTest: "filegroup",
630 moduleTypeUnderTestFactory: android.FileGroupFactory,
631 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000632 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500633 name: "foo",
634 srcs: ["**/*.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500635 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500636}`,
637 expectedBazelTargets: []string{`filegroup(
638 name = "foo",
639 srcs = [
640 "other/a.txt",
641 "other/b.txt",
642 "other/subdir/a.txt",
643 ],
644)`,
645 },
Jingwen Chen5146ac02021-09-02 11:44:42 +0000646 filesystem: map[string]string{
Liz Kammer356f7d42021-01-26 09:18:53 -0500647 "other/a.txt": "",
648 "other/b.txt": "",
649 "other/subdir/a.txt": "",
650 "other/file": "",
651 },
652 },
653 {
654 description: "filegroup with glob in subdir",
655 moduleTypeUnderTest: "filegroup",
656 moduleTypeUnderTestFactory: android.FileGroupFactory,
657 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000658 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500659 name: "foo",
660 srcs: ["a.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500661 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500662}`,
663 dir: "other",
664 expectedBazelTargets: []string{`filegroup(
665 name = "fg_foo",
666 srcs = [
667 "a.txt",
668 "b.txt",
669 "subdir/a.txt",
670 ],
671)`,
672 },
Jingwen Chen5146ac02021-09-02 11:44:42 +0000673 filesystem: map[string]string{
Liz Kammer356f7d42021-01-26 09:18:53 -0500674 "other/Android.bp": `filegroup {
675 name: "fg_foo",
676 srcs: ["**/*.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500677 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500678}`,
679 "other/a.txt": "",
680 "other/b.txt": "",
681 "other/subdir/a.txt": "",
682 "other/file": "",
683 },
684 },
685 {
686 description: "depends_on_other_dir_module",
687 moduleTypeUnderTest: "filegroup",
688 moduleTypeUnderTestFactory: android.FileGroupFactory,
689 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000690 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500691 name: "foobar",
692 srcs: [
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000693 ":foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500694 "c",
695 ],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500696 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500697}`,
698 expectedBazelTargets: []string{`filegroup(
699 name = "foobar",
700 srcs = [
701 "//other:foo",
702 "c",
703 ],
704)`,
705 },
Jingwen Chen5146ac02021-09-02 11:44:42 +0000706 filesystem: map[string]string{
Liz Kammer356f7d42021-01-26 09:18:53 -0500707 "other/Android.bp": `filegroup {
708 name: "foo",
709 srcs: ["a", "b"],
Liz Kammer6eff3232021-08-26 08:37:59 -0400710 bazel_module: { bp2build_available: true },
711}`,
712 },
713 },
714 {
715 description: "depends_on_other_unconverted_module_error",
716 moduleTypeUnderTest: "filegroup",
717 moduleTypeUnderTestFactory: android.FileGroupFactory,
718 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
719 unconvertedDepsMode: errorModulesUnconvertedDeps,
720 blueprint: `filegroup {
721 name: "foobar",
722 srcs: [
723 ":foo",
724 "c",
725 ],
726 bazel_module: { bp2build_available: true },
727}`,
728 expectedErr: fmt.Errorf(`"foobar" depends on unconverted modules: foo`),
729 filesystem: map[string]string{
730 "other/Android.bp": `filegroup {
731 name: "foo",
732 srcs: ["a", "b"],
Liz Kammer356f7d42021-01-26 09:18:53 -0500733}`,
734 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500735 },
736 }
737
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500738 for _, testCase := range testCases {
Liz Kammer6eff3232021-08-26 08:37:59 -0400739 t.Run(testCase.description, func(t *testing.T) {
740 runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, testCase)
741 })
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500742 }
743}
Jingwen Chen041b1842021-02-01 00:23:25 -0500744
745type bp2buildMutator = func(android.TopDownMutatorContext)
746
Jingwen Chen12b4c272021-03-10 02:05:59 -0500747func TestAllowlistingBp2buildTargetsExplicitly(t *testing.T) {
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500748 testCases := []struct {
749 moduleTypeUnderTest string
750 moduleTypeUnderTestFactory android.ModuleFactory
751 moduleTypeUnderTestBp2BuildMutator bp2buildMutator
752 bp string
753 expectedCount int
754 description string
755 }{
756 {
757 description: "explicitly unavailable",
758 moduleTypeUnderTest: "filegroup",
759 moduleTypeUnderTestFactory: android.FileGroupFactory,
760 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
761 bp: `filegroup {
762 name: "foo",
763 srcs: ["a", "b"],
764 bazel_module: { bp2build_available: false },
765}`,
766 expectedCount: 0,
767 },
768 {
769 description: "implicitly unavailable",
770 moduleTypeUnderTest: "filegroup",
771 moduleTypeUnderTestFactory: android.FileGroupFactory,
772 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
773 bp: `filegroup {
774 name: "foo",
775 srcs: ["a", "b"],
776}`,
777 expectedCount: 0,
778 },
779 {
780 description: "explicitly available",
781 moduleTypeUnderTest: "filegroup",
782 moduleTypeUnderTestFactory: android.FileGroupFactory,
783 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
784 bp: `filegroup {
785 name: "foo",
786 srcs: ["a", "b"],
787 bazel_module: { bp2build_available: true },
788}`,
789 expectedCount: 1,
790 },
791 {
792 description: "generates more than 1 target if needed",
793 moduleTypeUnderTest: "custom",
794 moduleTypeUnderTestFactory: customModuleFactory,
795 moduleTypeUnderTestBp2BuildMutator: customBp2BuildMutatorFromStarlark,
796 bp: `custom {
797 name: "foo",
798 bazel_module: { bp2build_available: true },
799}`,
800 expectedCount: 3,
801 },
802 }
803
804 dir := "."
805 for _, testCase := range testCases {
Liz Kammer2ada09a2021-08-11 00:17:36 -0400806 t.Run(testCase.description, func(t *testing.T) {
807 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
808 ctx := android.NewTestContext(config)
809 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
810 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
811 ctx.RegisterForBazelConversion()
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500812
Liz Kammer2ada09a2021-08-11 00:17:36 -0400813 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
814 android.FailIfErrored(t, errs)
815 _, errs = ctx.ResolveDependencies(config)
816 android.FailIfErrored(t, errs)
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500817
Liz Kammer2ada09a2021-08-11 00:17:36 -0400818 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Liz Kammer6eff3232021-08-26 08:37:59 -0400819 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
820 android.FailIfErrored(t, err)
Liz Kammer2ada09a2021-08-11 00:17:36 -0400821 if actualCount := len(bazelTargets); actualCount != testCase.expectedCount {
822 t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, testCase.expectedCount, actualCount)
823 }
824 })
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500825 }
826}
Liz Kammerba3ea162021-02-17 13:22:03 -0500827
Jingwen Chen12b4c272021-03-10 02:05:59 -0500828func TestAllowlistingBp2buildTargetsWithConfig(t *testing.T) {
829 testCases := []struct {
830 moduleTypeUnderTest string
831 moduleTypeUnderTestFactory android.ModuleFactory
832 moduleTypeUnderTestBp2BuildMutator bp2buildMutator
833 expectedCount map[string]int
834 description string
835 bp2buildConfig android.Bp2BuildConfig
836 checkDir string
837 fs map[string]string
838 }{
839 {
840 description: "test bp2build config package and subpackages config",
841 moduleTypeUnderTest: "filegroup",
842 moduleTypeUnderTestFactory: android.FileGroupFactory,
843 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
844 expectedCount: map[string]int{
845 "migrated": 1,
846 "migrated/but_not_really": 0,
847 "migrated/but_not_really/but_really": 1,
848 "not_migrated": 0,
849 "also_not_migrated": 0,
850 },
851 bp2buildConfig: android.Bp2BuildConfig{
852 "migrated": android.Bp2BuildDefaultTrueRecursively,
853 "migrated/but_not_really": android.Bp2BuildDefaultFalse,
854 "not_migrated": android.Bp2BuildDefaultFalse,
855 },
856 fs: map[string]string{
857 "migrated/Android.bp": `filegroup { name: "a" }`,
858 "migrated/but_not_really/Android.bp": `filegroup { name: "b" }`,
859 "migrated/but_not_really/but_really/Android.bp": `filegroup { name: "c" }`,
860 "not_migrated/Android.bp": `filegroup { name: "d" }`,
861 "also_not_migrated/Android.bp": `filegroup { name: "e" }`,
862 },
863 },
864 {
865 description: "test bp2build config opt-in and opt-out",
866 moduleTypeUnderTest: "filegroup",
867 moduleTypeUnderTestFactory: android.FileGroupFactory,
868 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
869 expectedCount: map[string]int{
870 "package-opt-in": 2,
871 "package-opt-in/subpackage": 0,
872 "package-opt-out": 1,
873 "package-opt-out/subpackage": 0,
874 },
875 bp2buildConfig: android.Bp2BuildConfig{
876 "package-opt-in": android.Bp2BuildDefaultFalse,
877 "package-opt-out": android.Bp2BuildDefaultTrueRecursively,
878 },
879 fs: map[string]string{
880 "package-opt-in/Android.bp": `
881filegroup { name: "opt-in-a" }
882filegroup { name: "opt-in-b", bazel_module: { bp2build_available: true } }
883filegroup { name: "opt-in-c", bazel_module: { bp2build_available: true } }
884`,
885
886 "package-opt-in/subpackage/Android.bp": `
887filegroup { name: "opt-in-d" } // parent package not configured to DefaultTrueRecursively
888`,
889
890 "package-opt-out/Android.bp": `
891filegroup { name: "opt-out-a" }
892filegroup { name: "opt-out-b", bazel_module: { bp2build_available: false } }
893filegroup { name: "opt-out-c", bazel_module: { bp2build_available: false } }
894`,
895
896 "package-opt-out/subpackage/Android.bp": `
897filegroup { name: "opt-out-g", bazel_module: { bp2build_available: false } }
898filegroup { name: "opt-out-h", bazel_module: { bp2build_available: false } }
899`,
900 },
901 },
902 }
903
904 dir := "."
905 for _, testCase := range testCases {
906 fs := make(map[string][]byte)
907 toParse := []string{
908 "Android.bp",
909 }
910 for f, content := range testCase.fs {
911 if strings.HasSuffix(f, "Android.bp") {
912 toParse = append(toParse, f)
913 }
914 fs[f] = []byte(content)
915 }
916 config := android.TestConfig(buildDir, nil, "", fs)
917 ctx := android.NewTestContext(config)
918 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
919 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
920 ctx.RegisterBp2BuildConfig(testCase.bp2buildConfig)
921 ctx.RegisterForBazelConversion()
922
923 _, errs := ctx.ParseFileList(dir, toParse)
924 android.FailIfErrored(t, errs)
925 _, errs = ctx.ResolveDependencies(config)
926 android.FailIfErrored(t, errs)
927
928 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
929
930 // For each directory, test that the expected number of generated targets is correct.
931 for dir, expectedCount := range testCase.expectedCount {
Liz Kammer6eff3232021-08-26 08:37:59 -0400932 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
933 android.FailIfErrored(t, err)
Jingwen Chen12b4c272021-03-10 02:05:59 -0500934 if actualCount := len(bazelTargets); actualCount != expectedCount {
935 t.Fatalf(
936 "%s: Expected %d bazel target for %s package, got %d",
937 testCase.description,
938 expectedCount,
939 dir,
940 actualCount)
941 }
942
943 }
944 }
945}
946
Liz Kammerba3ea162021-02-17 13:22:03 -0500947func TestCombineBuildFilesBp2buildTargets(t *testing.T) {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000948 testCases := []bp2buildTestCase{
Liz Kammerba3ea162021-02-17 13:22:03 -0500949 {
950 description: "filegroup bazel_module.label",
951 moduleTypeUnderTest: "filegroup",
952 moduleTypeUnderTestFactory: android.FileGroupFactory,
953 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000954 blueprint: `filegroup {
Liz Kammerba3ea162021-02-17 13:22:03 -0500955 name: "fg_foo",
956 bazel_module: { label: "//other:fg_foo" },
957}`,
958 expectedBazelTargets: []string{
959 `// BUILD file`,
960 },
Jingwen Chen5146ac02021-09-02 11:44:42 +0000961 filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -0500962 "other/BUILD.bazel": `// BUILD file`,
963 },
964 },
965 {
966 description: "multiple bazel_module.label same BUILD",
967 moduleTypeUnderTest: "filegroup",
968 moduleTypeUnderTestFactory: android.FileGroupFactory,
969 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000970 blueprint: `filegroup {
Jingwen Chenc63677b2021-06-17 05:43:19 +0000971 name: "fg_foo",
972 bazel_module: { label: "//other:fg_foo" },
973 }
Liz Kammerba3ea162021-02-17 13:22:03 -0500974
Jingwen Chenc63677b2021-06-17 05:43:19 +0000975 filegroup {
976 name: "foo",
977 bazel_module: { label: "//other:foo" },
978 }`,
Liz Kammerba3ea162021-02-17 13:22:03 -0500979 expectedBazelTargets: []string{
980 `// BUILD file`,
981 },
Jingwen Chen5146ac02021-09-02 11:44:42 +0000982 filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -0500983 "other/BUILD.bazel": `// BUILD file`,
984 },
985 },
986 {
Jingwen Chenc63677b2021-06-17 05:43:19 +0000987 description: "filegroup bazel_module.label and bp2build in subdir",
Liz Kammerba3ea162021-02-17 13:22:03 -0500988 moduleTypeUnderTest: "filegroup",
989 moduleTypeUnderTestFactory: android.FileGroupFactory,
990 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chenc63677b2021-06-17 05:43:19 +0000991 dir: "other",
Jingwen Chen5146ac02021-09-02 11:44:42 +0000992 blueprint: ``,
993 filesystem: map[string]string{
Jingwen Chenc63677b2021-06-17 05:43:19 +0000994 "other/Android.bp": `filegroup {
995 name: "fg_foo",
996 bazel_module: {
997 bp2build_available: true,
998 },
999 }
1000 filegroup {
1001 name: "fg_bar",
1002 bazel_module: {
1003 label: "//other:fg_bar"
1004 },
1005 }`,
1006 "other/BUILD.bazel": `// definition for fg_bar`,
1007 },
Liz Kammerba3ea162021-02-17 13:22:03 -05001008 expectedBazelTargets: []string{
1009 `filegroup(
1010 name = "fg_foo",
Jingwen Chenc63677b2021-06-17 05:43:19 +00001011)`, `// definition for fg_bar`,
Liz Kammerba3ea162021-02-17 13:22:03 -05001012 },
1013 },
1014 {
1015 description: "filegroup bazel_module.label and filegroup bp2build",
1016 moduleTypeUnderTest: "filegroup",
1017 moduleTypeUnderTestFactory: android.FileGroupFactory,
1018 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +00001019 blueprint: `filegroup {
Jingwen Chenc63677b2021-06-17 05:43:19 +00001020 name: "fg_foo",
1021 bazel_module: {
1022 label: "//other:fg_foo",
1023 },
1024 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001025
Jingwen Chenc63677b2021-06-17 05:43:19 +00001026 filegroup {
1027 name: "fg_bar",
1028 bazel_module: {
1029 bp2build_available: true,
1030 },
1031 }`,
Liz Kammerba3ea162021-02-17 13:22:03 -05001032 expectedBazelTargets: []string{
1033 `filegroup(
1034 name = "fg_bar",
1035)`,
1036 `// BUILD file`,
1037 },
Jingwen Chen5146ac02021-09-02 11:44:42 +00001038 filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -05001039 "other/BUILD.bazel": `// BUILD file`,
1040 },
1041 },
1042 }
1043
1044 dir := "."
1045 for _, testCase := range testCases {
Jingwen Chen49109762021-05-25 05:16:48 +00001046 t.Run(testCase.description, func(t *testing.T) {
1047 fs := make(map[string][]byte)
1048 toParse := []string{
1049 "Android.bp",
Liz Kammerba3ea162021-02-17 13:22:03 -05001050 }
Jingwen Chen5146ac02021-09-02 11:44:42 +00001051 for f, content := range testCase.filesystem {
Jingwen Chen49109762021-05-25 05:16:48 +00001052 if strings.HasSuffix(f, "Android.bp") {
1053 toParse = append(toParse, f)
1054 }
1055 fs[f] = []byte(content)
1056 }
Jingwen Chen5146ac02021-09-02 11:44:42 +00001057 config := android.TestConfig(buildDir, nil, testCase.blueprint, fs)
Jingwen Chen49109762021-05-25 05:16:48 +00001058 ctx := android.NewTestContext(config)
1059 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Jingwen Chen49109762021-05-25 05:16:48 +00001060 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1061 ctx.RegisterForBazelConversion()
Liz Kammerba3ea162021-02-17 13:22:03 -05001062
Jingwen Chen49109762021-05-25 05:16:48 +00001063 _, errs := ctx.ParseFileList(dir, toParse)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001064 if errored(t, testCase, errs) {
Jingwen Chen49109762021-05-25 05:16:48 +00001065 return
1066 }
1067 _, errs = ctx.ResolveDependencies(config)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001068 if errored(t, testCase, errs) {
Jingwen Chen49109762021-05-25 05:16:48 +00001069 return
1070 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001071
Jingwen Chen49109762021-05-25 05:16:48 +00001072 checkDir := dir
1073 if testCase.dir != "" {
1074 checkDir = testCase.dir
1075 }
Liz Kammer6eff3232021-08-26 08:37:59 -04001076 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
1077 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, checkDir)
1078 android.FailIfErrored(t, err)
Jingwen Chen49109762021-05-25 05:16:48 +00001079 bazelTargets.sort()
1080 actualCount := len(bazelTargets)
1081 expectedCount := len(testCase.expectedBazelTargets)
1082 if actualCount != expectedCount {
1083 t.Errorf("Expected %d bazel target, got %d\n%s", expectedCount, actualCount, bazelTargets)
1084 }
1085 if !strings.Contains(bazelTargets.String(), "Section: Handcrafted targets. ") {
1086 t.Errorf("Expected string representation of bazelTargets to contain handcrafted section header.")
1087 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001088 for i, target := range bazelTargets {
Jingwen Chen49109762021-05-25 05:16:48 +00001089 actualContent := target.content
1090 expectedContent := testCase.expectedBazelTargets[i]
1091 if expectedContent != actualContent {
Liz Kammerba3ea162021-02-17 13:22:03 -05001092 t.Errorf(
Jingwen Chen49109762021-05-25 05:16:48 +00001093 "Expected generated Bazel target to be '%s', got '%s'",
1094 expectedContent,
1095 actualContent,
Liz Kammerba3ea162021-02-17 13:22:03 -05001096 )
1097 }
1098 }
Jingwen Chen49109762021-05-25 05:16:48 +00001099 })
Liz Kammerba3ea162021-02-17 13:22:03 -05001100 }
1101}
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001102
1103func TestGlobExcludeSrcs(t *testing.T) {
Jingwen Chen5146ac02021-09-02 11:44:42 +00001104 testCases := []bp2buildTestCase{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001105 {
1106 description: "filegroup top level exclude_srcs",
1107 moduleTypeUnderTest: "filegroup",
1108 moduleTypeUnderTestFactory: android.FileGroupFactory,
1109 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +00001110 blueprint: `filegroup {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001111 name: "fg_foo",
1112 srcs: ["**/*.txt"],
1113 exclude_srcs: ["c.txt"],
1114 bazel_module: { bp2build_available: true },
1115}`,
1116 expectedBazelTargets: []string{`filegroup(
1117 name = "fg_foo",
1118 srcs = [
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001119 "a.txt",
1120 "b.txt",
Liz Kammer9abd62d2021-05-21 08:37:59 -04001121 "//dir:e.txt",
1122 "//dir:f.txt",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001123 ],
1124)`,
1125 },
Jingwen Chen5146ac02021-09-02 11:44:42 +00001126 filesystem: map[string]string{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001127 "a.txt": "",
1128 "b.txt": "",
1129 "c.txt": "",
1130 "dir/Android.bp": "",
1131 "dir/e.txt": "",
1132 "dir/f.txt": "",
1133 },
1134 },
1135 {
1136 description: "filegroup in subdir exclude_srcs",
1137 moduleTypeUnderTest: "filegroup",
1138 moduleTypeUnderTestFactory: android.FileGroupFactory,
1139 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +00001140 blueprint: "",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001141 dir: "dir",
Jingwen Chen5146ac02021-09-02 11:44:42 +00001142 filesystem: map[string]string{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001143 "dir/Android.bp": `filegroup {
1144 name: "fg_foo",
1145 srcs: ["**/*.txt"],
1146 exclude_srcs: ["b.txt"],
1147 bazel_module: { bp2build_available: true },
1148}
1149`,
1150 "dir/a.txt": "",
1151 "dir/b.txt": "",
1152 "dir/subdir/Android.bp": "",
1153 "dir/subdir/e.txt": "",
1154 "dir/subdir/f.txt": "",
1155 },
1156 expectedBazelTargets: []string{`filegroup(
1157 name = "fg_foo",
1158 srcs = [
Liz Kammer9abd62d2021-05-21 08:37:59 -04001159 "a.txt",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001160 "//dir/subdir:e.txt",
1161 "//dir/subdir:f.txt",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001162 ],
1163)`,
1164 },
1165 },
1166 }
1167
1168 dir := "."
1169 for _, testCase := range testCases {
1170 fs := make(map[string][]byte)
1171 toParse := []string{
1172 "Android.bp",
1173 }
Jingwen Chen5146ac02021-09-02 11:44:42 +00001174 for f, content := range testCase.filesystem {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001175 if strings.HasSuffix(f, "Android.bp") {
1176 toParse = append(toParse, f)
1177 }
1178 fs[f] = []byte(content)
1179 }
Jingwen Chen5146ac02021-09-02 11:44:42 +00001180 config := android.TestConfig(buildDir, nil, testCase.blueprint, fs)
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001181 ctx := android.NewTestContext(config)
1182 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
1183 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1184 ctx.RegisterForBazelConversion()
1185
1186 _, errs := ctx.ParseFileList(dir, toParse)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001187 if errored(t, testCase, errs) {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001188 continue
1189 }
1190 _, errs = ctx.ResolveDependencies(config)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001191 if errored(t, testCase, errs) {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001192 continue
1193 }
1194
1195 checkDir := dir
1196 if testCase.dir != "" {
1197 checkDir = testCase.dir
1198 }
Liz Kammer6eff3232021-08-26 08:37:59 -04001199 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
1200 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, checkDir)
1201 android.FailIfErrored(t, err)
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001202 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
1203 t.Errorf("%s: Expected %d bazel target, got %d\n%s", testCase.description, expectedCount, actualCount, bazelTargets)
1204 } else {
1205 for i, target := range bazelTargets {
1206 if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
1207 t.Errorf(
1208 "%s: Expected generated Bazel target to be '%s', got '%s'",
1209 testCase.description,
1210 w,
1211 g,
1212 )
1213 }
1214 }
1215 }
1216 }
1217}