blob: c32711298e6e4687e6e5950a2d15360efb584e7f [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 Kammer356f7d42021-01-26 09:18:53 -050019 "strings"
Liz Kammer2dd9ca42020-11-25 16:06:39 -080020 "testing"
21)
22
23func TestGenerateSoongModuleTargets(t *testing.T) {
24 testCases := []struct {
Liz Kammerd366c902021-06-03 13:43:01 -040025 description string
Liz Kammer2dd9ca42020-11-25 16:06:39 -080026 bp string
27 expectedBazelTarget string
28 }{
29 {
Liz Kammerd366c902021-06-03 13:43:01 -040030 description: "only name",
Jingwen Chenb4628eb2021-04-08 14:40:57 +000031 bp: `custom { name: "foo" }
Liz Kammerd366c902021-06-03 13:43:01 -040032 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080033 expectedBazelTarget: `soong_module(
34 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050035 soong_module_name = "foo",
36 soong_module_type = "custom",
37 soong_module_variant = "",
38 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080039 ],
Liz Kammerd366c902021-06-03 13:43:01 -040040 bool_prop = False,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080041)`,
42 },
43 {
Liz Kammerd366c902021-06-03 13:43:01 -040044 description: "handles bool",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080045 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -040046 name: "foo",
47 bool_prop: true,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080048}
Liz Kammerd366c902021-06-03 13:43:01 -040049 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080050 expectedBazelTarget: `soong_module(
51 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050052 soong_module_name = "foo",
53 soong_module_type = "custom",
54 soong_module_variant = "",
55 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080056 ],
Liz Kammerd366c902021-06-03 13:43:01 -040057 bool_prop = True,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080058)`,
59 },
60 {
Liz Kammerd366c902021-06-03 13:43:01 -040061 description: "string escaping",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080062 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -040063 name: "foo",
64 owner: "a_string_with\"quotes\"_and_\\backslashes\\\\",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080065}
Liz Kammerd366c902021-06-03 13:43:01 -040066 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080067 expectedBazelTarget: `soong_module(
68 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050069 soong_module_name = "foo",
70 soong_module_type = "custom",
71 soong_module_variant = "",
72 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080073 ],
Liz Kammerd366c902021-06-03 13:43:01 -040074 bool_prop = False,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080075 owner = "a_string_with\"quotes\"_and_\\backslashes\\\\",
76)`,
77 },
78 {
Liz Kammerd366c902021-06-03 13:43:01 -040079 description: "single item string list",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080080 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -040081 name: "foo",
82 required: ["bar"],
Liz Kammer2dd9ca42020-11-25 16:06:39 -080083}
Liz Kammerd366c902021-06-03 13:43:01 -040084 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080085 expectedBazelTarget: `soong_module(
86 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050087 soong_module_name = "foo",
88 soong_module_type = "custom",
89 soong_module_variant = "",
90 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080091 ],
Liz Kammerd366c902021-06-03 13:43:01 -040092 bool_prop = False,
Jingwen Chenb4628eb2021-04-08 14:40:57 +000093 required = ["bar"],
Liz Kammer2dd9ca42020-11-25 16:06:39 -080094)`,
95 },
96 {
Liz Kammerd366c902021-06-03 13:43:01 -040097 description: "list of strings",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080098 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -040099 name: "foo",
100 target_required: ["qux", "bazqux"],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800101}
Liz Kammerd366c902021-06-03 13:43:01 -0400102 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800103 expectedBazelTarget: `soong_module(
104 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500105 soong_module_name = "foo",
106 soong_module_type = "custom",
107 soong_module_variant = "",
108 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800109 ],
Liz Kammerd366c902021-06-03 13:43:01 -0400110 bool_prop = False,
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800111 target_required = [
112 "qux",
113 "bazqux",
114 ],
115)`,
116 },
117 {
Liz Kammerd366c902021-06-03 13:43:01 -0400118 description: "dist/dists",
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800119 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -0400120 name: "foo",
121 dist: {
122 targets: ["goal_foo"],
123 tag: ".foo",
124 },
125 dists: [{
126 targets: ["goal_bar"],
127 tag: ".bar",
128 }],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800129}
Liz Kammerd366c902021-06-03 13:43:01 -0400130 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800131 expectedBazelTarget: `soong_module(
132 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500133 soong_module_name = "foo",
134 soong_module_type = "custom",
135 soong_module_variant = "",
136 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800137 ],
Liz Kammerd366c902021-06-03 13:43:01 -0400138 bool_prop = False,
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800139 dist = {
140 "tag": ".foo",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000141 "targets": ["goal_foo"],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800142 },
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000143 dists = [{
144 "tag": ".bar",
145 "targets": ["goal_bar"],
146 }],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800147)`,
148 },
149 {
Liz Kammerd366c902021-06-03 13:43:01 -0400150 description: "put it together",
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800151 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -0400152 name: "foo",
153 required: ["bar"],
154 target_required: ["qux", "bazqux"],
155 bool_prop: true,
156 owner: "custom_owner",
157 dists: [
158 {
159 tag: ".tag",
160 targets: ["my_goal"],
161 },
162 ],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800163}
Liz Kammerd366c902021-06-03 13:43:01 -0400164 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800165 expectedBazelTarget: `soong_module(
166 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500167 soong_module_name = "foo",
168 soong_module_type = "custom",
169 soong_module_variant = "",
170 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800171 ],
Liz Kammerd366c902021-06-03 13:43:01 -0400172 bool_prop = True,
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000173 dists = [{
174 "tag": ".tag",
175 "targets": ["my_goal"],
176 }],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800177 owner = "custom_owner",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000178 required = ["bar"],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800179 target_required = [
180 "qux",
181 "bazqux",
182 ],
183)`,
184 },
185 }
186
187 dir := "."
188 for _, testCase := range testCases {
Liz Kammerd366c902021-06-03 13:43:01 -0400189 t.Run(testCase.description, func(t *testing.T) {
190 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
191 ctx := android.NewTestContext(config)
Jingwen Chen164e0862021-02-19 00:48:40 -0500192
Liz Kammerd366c902021-06-03 13:43:01 -0400193 ctx.RegisterModuleType("custom", customModuleFactory)
194 ctx.Register()
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800195
Liz Kammerd366c902021-06-03 13:43:01 -0400196 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
197 android.FailIfErrored(t, errs)
198 _, errs = ctx.PrepareBuildActions(config)
199 android.FailIfErrored(t, errs)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800200
Liz Kammerd366c902021-06-03 13:43:01 -0400201 codegenCtx := NewCodegenContext(config, *ctx.Context, QueryView)
202 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
203 if actualCount, expectedCount := len(bazelTargets), 1; actualCount != expectedCount {
204 t.Fatalf("Expected %d bazel target, got %d", expectedCount, actualCount)
205 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800206
Liz Kammerd366c902021-06-03 13:43:01 -0400207 actualBazelTarget := bazelTargets[0]
208 if actualBazelTarget.content != testCase.expectedBazelTarget {
209 t.Errorf(
210 "Expected generated Bazel target to be '%s', got '%s'",
211 testCase.expectedBazelTarget,
212 actualBazelTarget.content,
213 )
214 }
215 })
Jingwen Chen73850672020-12-14 08:25:34 -0500216 }
217}
218
219func TestGenerateBazelTargetModules(t *testing.T) {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000220 testCases := []bp2buildTestCase{
Jingwen Chen73850672020-12-14 08:25:34 -0500221 {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000222 blueprint: `custom {
Jingwen Chen73850672020-12-14 08:25:34 -0500223 name: "foo",
224 string_list_prop: ["a", "b"],
225 string_prop: "a",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500226 bazel_module: { bp2build_available: true },
Jingwen Chen73850672020-12-14 08:25:34 -0500227}`,
Liz Kammer4562a3b2021-04-21 18:15:34 -0400228 expectedBazelTargets: []string{`custom(
Jingwen Chen73850672020-12-14 08:25:34 -0500229 name = "foo",
230 string_list_prop = [
231 "a",
232 "b",
233 ],
234 string_prop = "a",
235)`,
Liz Kammer4562a3b2021-04-21 18:15:34 -0400236 },
Jingwen Chen73850672020-12-14 08:25:34 -0500237 },
Jingwen Chen58a12b82021-03-30 13:08:36 +0000238 {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000239 blueprint: `custom {
Jingwen Chen58a12b82021-03-30 13:08:36 +0000240 name: "control_characters",
241 string_list_prop: ["\t", "\n"],
242 string_prop: "a\t\n\r",
243 bazel_module: { bp2build_available: true },
244}`,
Liz Kammer4562a3b2021-04-21 18:15:34 -0400245 expectedBazelTargets: []string{`custom(
Jingwen Chen58a12b82021-03-30 13:08:36 +0000246 name = "control_characters",
247 string_list_prop = [
248 "\t",
249 "\n",
250 ],
251 string_prop = "a\t\n\r",
252)`,
Liz Kammer4562a3b2021-04-21 18:15:34 -0400253 },
254 },
255 {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000256 blueprint: `custom {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400257 name: "has_dep",
258 arch_paths: [":dep"],
259 bazel_module: { bp2build_available: true },
260}
261
262custom {
263 name: "dep",
264 arch_paths: ["abc"],
265 bazel_module: { bp2build_available: true },
266}`,
267 expectedBazelTargets: []string{`custom(
268 name = "dep",
269 arch_paths = ["abc"],
270)`,
271 `custom(
272 name = "has_dep",
273 arch_paths = [":dep"],
274)`,
275 },
276 },
277 {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000278 blueprint: `custom {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400279 name: "arch_paths",
280 arch: {
281 x86: {
282 arch_paths: ["abc"],
283 },
284 },
285 bazel_module: { bp2build_available: true },
286}`,
287 expectedBazelTargets: []string{`custom(
288 name = "arch_paths",
289 arch_paths = select({
290 "//build/bazel/platforms/arch:x86": ["abc"],
291 "//conditions:default": [],
292 }),
293)`,
294 },
295 },
296 {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000297 blueprint: `custom {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400298 name: "has_dep",
299 arch: {
300 x86: {
301 arch_paths: [":dep"],
302 },
303 },
304 bazel_module: { bp2build_available: true },
305}
306
307custom {
308 name: "dep",
309 arch_paths: ["abc"],
310 bazel_module: { bp2build_available: true },
311}`,
312 expectedBazelTargets: []string{`custom(
313 name = "dep",
314 arch_paths = ["abc"],
315)`,
316 `custom(
317 name = "has_dep",
318 arch_paths = select({
319 "//build/bazel/platforms/arch:x86": [":dep"],
320 "//conditions:default": [],
321 }),
322)`,
323 },
Jingwen Chen58a12b82021-03-30 13:08:36 +0000324 },
Liz Kammer32a03392021-09-14 11:17:21 -0400325 {
326 blueprint: `custom {
327 name: "embedded_props",
328 embedded_prop: "abc",
329 bazel_module: { bp2build_available: true },
330}`,
331 expectedBazelTargets: []string{`custom(
332 name = "embedded_props",
333 embedded_attr = "abc",
334)`,
335 },
336 },
337 {
338 blueprint: `custom {
339 name: "ptr_to_embedded_props",
340 other_embedded_prop: "abc",
341 bazel_module: { bp2build_available: true },
342}`,
343 expectedBazelTargets: []string{`custom(
344 name = "ptr_to_embedded_props",
345 other_embedded_attr = "abc",
346)`,
347 },
348 },
Jingwen Chen73850672020-12-14 08:25:34 -0500349 }
350
351 dir := "."
352 for _, testCase := range testCases {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000353 config := android.TestConfig(buildDir, nil, testCase.blueprint, nil)
Jingwen Chen73850672020-12-14 08:25:34 -0500354 ctx := android.NewTestContext(config)
Jingwen Chen164e0862021-02-19 00:48:40 -0500355
Liz Kammer32b77cf2021-08-04 15:17:02 -0400356 registerCustomModuleForBp2buildConversion(ctx)
Jingwen Chen73850672020-12-14 08:25:34 -0500357
358 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
Jingwen Chen5146ac02021-09-02 11:44:42 +0000359 if errored(t, testCase, errs) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500360 continue
361 }
Jingwen Chen73850672020-12-14 08:25:34 -0500362 _, errs = ctx.ResolveDependencies(config)
Jingwen Chen5146ac02021-09-02 11:44:42 +0000363 if errored(t, testCase, errs) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500364 continue
365 }
Jingwen Chen73850672020-12-14 08:25:34 -0500366
Jingwen Chen164e0862021-02-19 00:48:40 -0500367 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -0500368 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
Jingwen Chen164e0862021-02-19 00:48:40 -0500369
Liz Kammer4562a3b2021-04-21 18:15:34 -0400370 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
Liz Kammer356f7d42021-01-26 09:18:53 -0500371 t.Errorf("Expected %d bazel target, got %d", expectedCount, actualCount)
372 } else {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400373 for i, expectedBazelTarget := range testCase.expectedBazelTargets {
374 actualBazelTarget := bazelTargets[i]
375 if actualBazelTarget.content != expectedBazelTarget {
376 t.Errorf(
377 "Expected generated Bazel target to be '%s', got '%s'",
378 expectedBazelTarget,
379 actualBazelTarget.content,
380 )
381 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500382 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800383 }
384 }
385}
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500386
Jingwen Chen40067de2021-01-26 21:58:43 -0500387func TestLoadStatements(t *testing.T) {
388 testCases := []struct {
389 bazelTargets BazelTargets
390 expectedLoadStatements string
391 }{
392 {
393 bazelTargets: BazelTargets{
394 BazelTarget{
395 name: "foo",
396 ruleClass: "cc_library",
397 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
398 },
399 },
400 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
401 },
402 {
403 bazelTargets: BazelTargets{
404 BazelTarget{
405 name: "foo",
406 ruleClass: "cc_library",
407 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
408 },
409 BazelTarget{
410 name: "bar",
411 ruleClass: "cc_library",
412 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
413 },
414 },
415 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
416 },
417 {
418 bazelTargets: BazelTargets{
419 BazelTarget{
420 name: "foo",
421 ruleClass: "cc_library",
422 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
423 },
424 BazelTarget{
425 name: "bar",
426 ruleClass: "cc_binary",
427 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
428 },
429 },
430 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")`,
431 },
432 {
433 bazelTargets: BazelTargets{
434 BazelTarget{
435 name: "foo",
436 ruleClass: "cc_library",
437 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
438 },
439 BazelTarget{
440 name: "bar",
441 ruleClass: "cc_binary",
442 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
443 },
444 BazelTarget{
445 name: "baz",
446 ruleClass: "java_binary",
447 bzlLoadLocation: "//build/bazel/rules:java.bzl",
448 },
449 },
450 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")
451load("//build/bazel/rules:java.bzl", "java_binary")`,
452 },
453 {
454 bazelTargets: BazelTargets{
455 BazelTarget{
456 name: "foo",
457 ruleClass: "cc_binary",
458 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
459 },
460 BazelTarget{
461 name: "bar",
462 ruleClass: "java_binary",
463 bzlLoadLocation: "//build/bazel/rules:java.bzl",
464 },
465 BazelTarget{
466 name: "baz",
467 ruleClass: "genrule",
468 // Note: no bzlLoadLocation for native rules
469 },
470 },
471 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary")
472load("//build/bazel/rules:java.bzl", "java_binary")`,
473 },
474 }
475
476 for _, testCase := range testCases {
477 actual := testCase.bazelTargets.LoadStatements()
478 expected := testCase.expectedLoadStatements
479 if actual != expected {
480 t.Fatalf("Expected load statements to be %s, got %s", expected, actual)
481 }
482 }
483
484}
485
486func TestGenerateBazelTargetModules_OneToMany_LoadedFromStarlark(t *testing.T) {
487 testCases := []struct {
488 bp string
489 expectedBazelTarget string
490 expectedBazelTargetCount int
491 expectedLoadStatements string
492 }{
493 {
494 bp: `custom {
495 name: "bar",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500496 bazel_module: { bp2build_available: true },
Jingwen Chen40067de2021-01-26 21:58:43 -0500497}`,
498 expectedBazelTarget: `my_library(
499 name = "bar",
500)
501
Jingwen Chen40067de2021-01-26 21:58:43 -0500502proto_library(
503 name = "bar_proto_library_deps",
Liz Kammer2ada09a2021-08-11 00:17:36 -0400504)
505
506my_proto_library(
507 name = "bar_my_proto_library_deps",
Jingwen Chen40067de2021-01-26 21:58:43 -0500508)`,
509 expectedBazelTargetCount: 3,
510 expectedLoadStatements: `load("//build/bazel/rules:proto.bzl", "my_proto_library", "proto_library")
511load("//build/bazel/rules:rules.bzl", "my_library")`,
512 },
513 }
514
515 dir := "."
516 for _, testCase := range testCases {
517 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
518 ctx := android.NewTestContext(config)
519 ctx.RegisterModuleType("custom", customModuleFactory)
Jingwen Chen12b4c272021-03-10 02:05:59 -0500520 ctx.RegisterBp2BuildMutator("custom", customBp2BuildMutatorFromStarlark)
Jingwen Chen40067de2021-01-26 21:58:43 -0500521 ctx.RegisterForBazelConversion()
522
523 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
524 android.FailIfErrored(t, errs)
525 _, errs = ctx.ResolveDependencies(config)
526 android.FailIfErrored(t, errs)
527
Jingwen Chen164e0862021-02-19 00:48:40 -0500528 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -0500529 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
Jingwen Chen40067de2021-01-26 21:58:43 -0500530 if actualCount := len(bazelTargets); actualCount != testCase.expectedBazelTargetCount {
531 t.Fatalf("Expected %d bazel target, got %d", testCase.expectedBazelTargetCount, actualCount)
532 }
533
534 actualBazelTargets := bazelTargets.String()
535 if actualBazelTargets != testCase.expectedBazelTarget {
536 t.Errorf(
537 "Expected generated Bazel target to be '%s', got '%s'",
538 testCase.expectedBazelTarget,
539 actualBazelTargets,
540 )
541 }
542
543 actualLoadStatements := bazelTargets.LoadStatements()
544 if actualLoadStatements != testCase.expectedLoadStatements {
545 t.Errorf(
546 "Expected generated load statements to be '%s', got '%s'",
547 testCase.expectedLoadStatements,
548 actualLoadStatements,
549 )
550 }
551 }
552}
553
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500554func TestModuleTypeBp2Build(t *testing.T) {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000555 testCases := []bp2buildTestCase{
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500556 {
Liz Kammerebfcf672021-02-16 15:00:05 -0500557 description: "filegroup with does not specify srcs",
558 moduleTypeUnderTest: "filegroup",
559 moduleTypeUnderTestFactory: android.FileGroupFactory,
560 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000561 blueprint: `filegroup {
Liz Kammerebfcf672021-02-16 15:00:05 -0500562 name: "fg_foo",
563 bazel_module: { bp2build_available: true },
564}`,
565 expectedBazelTargets: []string{
566 `filegroup(
567 name = "fg_foo",
568)`,
569 },
570 },
571 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500572 description: "filegroup with no srcs",
573 moduleTypeUnderTest: "filegroup",
574 moduleTypeUnderTestFactory: android.FileGroupFactory,
575 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000576 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500577 name: "fg_foo",
578 srcs: [],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500579 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500580}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500581 expectedBazelTargets: []string{
582 `filegroup(
583 name = "fg_foo",
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500584)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500585 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500586 },
587 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500588 description: "filegroup with srcs",
589 moduleTypeUnderTest: "filegroup",
590 moduleTypeUnderTestFactory: android.FileGroupFactory,
591 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000592 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500593 name: "fg_foo",
594 srcs: ["a", "b"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500595 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500596}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500597 expectedBazelTargets: []string{`filegroup(
598 name = "fg_foo",
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500599 srcs = [
600 "a",
601 "b",
602 ],
603)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500604 },
605 },
606 {
607 description: "filegroup with excludes srcs",
608 moduleTypeUnderTest: "filegroup",
609 moduleTypeUnderTestFactory: android.FileGroupFactory,
610 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000611 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500612 name: "fg_foo",
613 srcs: ["a", "b"],
614 exclude_srcs: ["a"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500615 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500616}`,
617 expectedBazelTargets: []string{`filegroup(
618 name = "fg_foo",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000619 srcs = ["b"],
Liz Kammer356f7d42021-01-26 09:18:53 -0500620)`,
621 },
622 },
623 {
624 description: "filegroup with glob",
625 moduleTypeUnderTest: "filegroup",
626 moduleTypeUnderTestFactory: android.FileGroupFactory,
627 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000628 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500629 name: "foo",
630 srcs: ["**/*.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500631 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500632}`,
633 expectedBazelTargets: []string{`filegroup(
634 name = "foo",
635 srcs = [
636 "other/a.txt",
637 "other/b.txt",
638 "other/subdir/a.txt",
639 ],
640)`,
641 },
Jingwen Chen5146ac02021-09-02 11:44:42 +0000642 filesystem: map[string]string{
Liz Kammer356f7d42021-01-26 09:18:53 -0500643 "other/a.txt": "",
644 "other/b.txt": "",
645 "other/subdir/a.txt": "",
646 "other/file": "",
647 },
648 },
649 {
650 description: "filegroup with glob in subdir",
651 moduleTypeUnderTest: "filegroup",
652 moduleTypeUnderTestFactory: android.FileGroupFactory,
653 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000654 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500655 name: "foo",
656 srcs: ["a.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500657 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500658}`,
659 dir: "other",
660 expectedBazelTargets: []string{`filegroup(
661 name = "fg_foo",
662 srcs = [
663 "a.txt",
664 "b.txt",
665 "subdir/a.txt",
666 ],
667)`,
668 },
Jingwen Chen5146ac02021-09-02 11:44:42 +0000669 filesystem: map[string]string{
Liz Kammer356f7d42021-01-26 09:18:53 -0500670 "other/Android.bp": `filegroup {
671 name: "fg_foo",
672 srcs: ["**/*.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500673 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500674}`,
675 "other/a.txt": "",
676 "other/b.txt": "",
677 "other/subdir/a.txt": "",
678 "other/file": "",
679 },
680 },
681 {
682 description: "depends_on_other_dir_module",
683 moduleTypeUnderTest: "filegroup",
684 moduleTypeUnderTestFactory: android.FileGroupFactory,
685 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000686 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500687 name: "foobar",
688 srcs: [
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000689 ":foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500690 "c",
691 ],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500692 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500693}`,
694 expectedBazelTargets: []string{`filegroup(
695 name = "foobar",
696 srcs = [
697 "//other:foo",
698 "c",
699 ],
700)`,
701 },
Jingwen Chen5146ac02021-09-02 11:44:42 +0000702 filesystem: map[string]string{
Liz Kammer356f7d42021-01-26 09:18:53 -0500703 "other/Android.bp": `filegroup {
704 name: "foo",
705 srcs: ["a", "b"],
706}`,
707 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500708 },
709 }
710
711 dir := "."
712 for _, testCase := range testCases {
Liz Kammer356f7d42021-01-26 09:18:53 -0500713 fs := make(map[string][]byte)
714 toParse := []string{
715 "Android.bp",
716 }
Jingwen Chen5146ac02021-09-02 11:44:42 +0000717 for f, content := range testCase.filesystem {
Liz Kammer356f7d42021-01-26 09:18:53 -0500718 if strings.HasSuffix(f, "Android.bp") {
719 toParse = append(toParse, f)
720 }
721 fs[f] = []byte(content)
722 }
Jingwen Chen5146ac02021-09-02 11:44:42 +0000723 config := android.TestConfig(buildDir, nil, testCase.blueprint, fs)
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500724 ctx := android.NewTestContext(config)
725 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Jingwen Chena42d6412021-01-26 21:57:27 -0500726 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500727 ctx.RegisterForBazelConversion()
728
Liz Kammer356f7d42021-01-26 09:18:53 -0500729 _, errs := ctx.ParseFileList(dir, toParse)
Jingwen Chen5146ac02021-09-02 11:44:42 +0000730 if errored(t, testCase, errs) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500731 continue
732 }
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500733 _, errs = ctx.ResolveDependencies(config)
Jingwen Chen5146ac02021-09-02 11:44:42 +0000734 if errored(t, testCase, errs) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500735 continue
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500736 }
737
Liz Kammer356f7d42021-01-26 09:18:53 -0500738 checkDir := dir
739 if testCase.dir != "" {
740 checkDir = testCase.dir
741 }
Jingwen Chen164e0862021-02-19 00:48:40 -0500742
743 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -0500744 bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir)
Liz Kammer356f7d42021-01-26 09:18:53 -0500745 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
746 t.Errorf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount)
747 } else {
748 for i, target := range bazelTargets {
749 if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
750 t.Errorf(
751 "%s: Expected generated Bazel target to be '%s', got '%s'",
752 testCase.description,
753 w,
754 g,
755 )
756 }
757 }
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500758 }
759 }
760}
Jingwen Chen041b1842021-02-01 00:23:25 -0500761
762type bp2buildMutator = func(android.TopDownMutatorContext)
763
Jingwen Chen12b4c272021-03-10 02:05:59 -0500764func TestAllowlistingBp2buildTargetsExplicitly(t *testing.T) {
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500765 testCases := []struct {
766 moduleTypeUnderTest string
767 moduleTypeUnderTestFactory android.ModuleFactory
768 moduleTypeUnderTestBp2BuildMutator bp2buildMutator
769 bp string
770 expectedCount int
771 description string
772 }{
773 {
774 description: "explicitly unavailable",
775 moduleTypeUnderTest: "filegroup",
776 moduleTypeUnderTestFactory: android.FileGroupFactory,
777 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
778 bp: `filegroup {
779 name: "foo",
780 srcs: ["a", "b"],
781 bazel_module: { bp2build_available: false },
782}`,
783 expectedCount: 0,
784 },
785 {
786 description: "implicitly unavailable",
787 moduleTypeUnderTest: "filegroup",
788 moduleTypeUnderTestFactory: android.FileGroupFactory,
789 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
790 bp: `filegroup {
791 name: "foo",
792 srcs: ["a", "b"],
793}`,
794 expectedCount: 0,
795 },
796 {
797 description: "explicitly available",
798 moduleTypeUnderTest: "filegroup",
799 moduleTypeUnderTestFactory: android.FileGroupFactory,
800 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
801 bp: `filegroup {
802 name: "foo",
803 srcs: ["a", "b"],
804 bazel_module: { bp2build_available: true },
805}`,
806 expectedCount: 1,
807 },
808 {
809 description: "generates more than 1 target if needed",
810 moduleTypeUnderTest: "custom",
811 moduleTypeUnderTestFactory: customModuleFactory,
812 moduleTypeUnderTestBp2BuildMutator: customBp2BuildMutatorFromStarlark,
813 bp: `custom {
814 name: "foo",
815 bazel_module: { bp2build_available: true },
816}`,
817 expectedCount: 3,
818 },
819 }
820
821 dir := "."
822 for _, testCase := range testCases {
Liz Kammer2ada09a2021-08-11 00:17:36 -0400823 t.Run(testCase.description, func(t *testing.T) {
824 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
825 ctx := android.NewTestContext(config)
826 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
827 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
828 ctx.RegisterForBazelConversion()
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500829
Liz Kammer2ada09a2021-08-11 00:17:36 -0400830 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
831 android.FailIfErrored(t, errs)
832 _, errs = ctx.ResolveDependencies(config)
833 android.FailIfErrored(t, errs)
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500834
Liz Kammer2ada09a2021-08-11 00:17:36 -0400835 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
836 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
837 if actualCount := len(bazelTargets); actualCount != testCase.expectedCount {
838 t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, testCase.expectedCount, actualCount)
839 }
840 })
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500841 }
842}
Liz Kammerba3ea162021-02-17 13:22:03 -0500843
Jingwen Chen12b4c272021-03-10 02:05:59 -0500844func TestAllowlistingBp2buildTargetsWithConfig(t *testing.T) {
845 testCases := []struct {
846 moduleTypeUnderTest string
847 moduleTypeUnderTestFactory android.ModuleFactory
848 moduleTypeUnderTestBp2BuildMutator bp2buildMutator
849 expectedCount map[string]int
850 description string
851 bp2buildConfig android.Bp2BuildConfig
852 checkDir string
853 fs map[string]string
854 }{
855 {
856 description: "test bp2build config package and subpackages config",
857 moduleTypeUnderTest: "filegroup",
858 moduleTypeUnderTestFactory: android.FileGroupFactory,
859 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
860 expectedCount: map[string]int{
861 "migrated": 1,
862 "migrated/but_not_really": 0,
863 "migrated/but_not_really/but_really": 1,
864 "not_migrated": 0,
865 "also_not_migrated": 0,
866 },
867 bp2buildConfig: android.Bp2BuildConfig{
868 "migrated": android.Bp2BuildDefaultTrueRecursively,
869 "migrated/but_not_really": android.Bp2BuildDefaultFalse,
870 "not_migrated": android.Bp2BuildDefaultFalse,
871 },
872 fs: map[string]string{
873 "migrated/Android.bp": `filegroup { name: "a" }`,
874 "migrated/but_not_really/Android.bp": `filegroup { name: "b" }`,
875 "migrated/but_not_really/but_really/Android.bp": `filegroup { name: "c" }`,
876 "not_migrated/Android.bp": `filegroup { name: "d" }`,
877 "also_not_migrated/Android.bp": `filegroup { name: "e" }`,
878 },
879 },
880 {
881 description: "test bp2build config opt-in and opt-out",
882 moduleTypeUnderTest: "filegroup",
883 moduleTypeUnderTestFactory: android.FileGroupFactory,
884 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
885 expectedCount: map[string]int{
886 "package-opt-in": 2,
887 "package-opt-in/subpackage": 0,
888 "package-opt-out": 1,
889 "package-opt-out/subpackage": 0,
890 },
891 bp2buildConfig: android.Bp2BuildConfig{
892 "package-opt-in": android.Bp2BuildDefaultFalse,
893 "package-opt-out": android.Bp2BuildDefaultTrueRecursively,
894 },
895 fs: map[string]string{
896 "package-opt-in/Android.bp": `
897filegroup { name: "opt-in-a" }
898filegroup { name: "opt-in-b", bazel_module: { bp2build_available: true } }
899filegroup { name: "opt-in-c", bazel_module: { bp2build_available: true } }
900`,
901
902 "package-opt-in/subpackage/Android.bp": `
903filegroup { name: "opt-in-d" } // parent package not configured to DefaultTrueRecursively
904`,
905
906 "package-opt-out/Android.bp": `
907filegroup { name: "opt-out-a" }
908filegroup { name: "opt-out-b", bazel_module: { bp2build_available: false } }
909filegroup { name: "opt-out-c", bazel_module: { bp2build_available: false } }
910`,
911
912 "package-opt-out/subpackage/Android.bp": `
913filegroup { name: "opt-out-g", bazel_module: { bp2build_available: false } }
914filegroup { name: "opt-out-h", bazel_module: { bp2build_available: false } }
915`,
916 },
917 },
918 }
919
920 dir := "."
921 for _, testCase := range testCases {
922 fs := make(map[string][]byte)
923 toParse := []string{
924 "Android.bp",
925 }
926 for f, content := range testCase.fs {
927 if strings.HasSuffix(f, "Android.bp") {
928 toParse = append(toParse, f)
929 }
930 fs[f] = []byte(content)
931 }
932 config := android.TestConfig(buildDir, nil, "", fs)
933 ctx := android.NewTestContext(config)
934 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
935 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
936 ctx.RegisterBp2BuildConfig(testCase.bp2buildConfig)
937 ctx.RegisterForBazelConversion()
938
939 _, errs := ctx.ParseFileList(dir, toParse)
940 android.FailIfErrored(t, errs)
941 _, errs = ctx.ResolveDependencies(config)
942 android.FailIfErrored(t, errs)
943
944 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
945
946 // For each directory, test that the expected number of generated targets is correct.
947 for dir, expectedCount := range testCase.expectedCount {
948 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
949 if actualCount := len(bazelTargets); actualCount != expectedCount {
950 t.Fatalf(
951 "%s: Expected %d bazel target for %s package, got %d",
952 testCase.description,
953 expectedCount,
954 dir,
955 actualCount)
956 }
957
958 }
959 }
960}
961
Liz Kammerba3ea162021-02-17 13:22:03 -0500962func TestCombineBuildFilesBp2buildTargets(t *testing.T) {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000963 testCases := []bp2buildTestCase{
Liz Kammerba3ea162021-02-17 13:22:03 -0500964 {
965 description: "filegroup bazel_module.label",
966 moduleTypeUnderTest: "filegroup",
967 moduleTypeUnderTestFactory: android.FileGroupFactory,
968 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000969 blueprint: `filegroup {
Liz Kammerba3ea162021-02-17 13:22:03 -0500970 name: "fg_foo",
971 bazel_module: { label: "//other:fg_foo" },
972}`,
973 expectedBazelTargets: []string{
974 `// BUILD file`,
975 },
Jingwen Chen5146ac02021-09-02 11:44:42 +0000976 filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -0500977 "other/BUILD.bazel": `// BUILD file`,
978 },
979 },
980 {
981 description: "multiple bazel_module.label same BUILD",
982 moduleTypeUnderTest: "filegroup",
983 moduleTypeUnderTestFactory: android.FileGroupFactory,
984 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000985 blueprint: `filegroup {
Jingwen Chenc63677b2021-06-17 05:43:19 +0000986 name: "fg_foo",
987 bazel_module: { label: "//other:fg_foo" },
988 }
Liz Kammerba3ea162021-02-17 13:22:03 -0500989
Jingwen Chenc63677b2021-06-17 05:43:19 +0000990 filegroup {
991 name: "foo",
992 bazel_module: { label: "//other:foo" },
993 }`,
Liz Kammerba3ea162021-02-17 13:22:03 -0500994 expectedBazelTargets: []string{
995 `// BUILD file`,
996 },
Jingwen Chen5146ac02021-09-02 11:44:42 +0000997 filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -0500998 "other/BUILD.bazel": `// BUILD file`,
999 },
1000 },
1001 {
Jingwen Chenc63677b2021-06-17 05:43:19 +00001002 description: "filegroup bazel_module.label and bp2build in subdir",
Liz Kammerba3ea162021-02-17 13:22:03 -05001003 moduleTypeUnderTest: "filegroup",
1004 moduleTypeUnderTestFactory: android.FileGroupFactory,
1005 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chenc63677b2021-06-17 05:43:19 +00001006 dir: "other",
Jingwen Chen5146ac02021-09-02 11:44:42 +00001007 blueprint: ``,
1008 filesystem: map[string]string{
Jingwen Chenc63677b2021-06-17 05:43:19 +00001009 "other/Android.bp": `filegroup {
1010 name: "fg_foo",
1011 bazel_module: {
1012 bp2build_available: true,
1013 },
1014 }
1015 filegroup {
1016 name: "fg_bar",
1017 bazel_module: {
1018 label: "//other:fg_bar"
1019 },
1020 }`,
1021 "other/BUILD.bazel": `// definition for fg_bar`,
1022 },
Liz Kammerba3ea162021-02-17 13:22:03 -05001023 expectedBazelTargets: []string{
1024 `filegroup(
1025 name = "fg_foo",
Jingwen Chenc63677b2021-06-17 05:43:19 +00001026)`, `// definition for fg_bar`,
Liz Kammerba3ea162021-02-17 13:22:03 -05001027 },
1028 },
1029 {
1030 description: "filegroup bazel_module.label and filegroup bp2build",
1031 moduleTypeUnderTest: "filegroup",
1032 moduleTypeUnderTestFactory: android.FileGroupFactory,
1033 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +00001034 blueprint: `filegroup {
Jingwen Chenc63677b2021-06-17 05:43:19 +00001035 name: "fg_foo",
1036 bazel_module: {
1037 label: "//other:fg_foo",
1038 },
1039 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001040
Jingwen Chenc63677b2021-06-17 05:43:19 +00001041 filegroup {
1042 name: "fg_bar",
1043 bazel_module: {
1044 bp2build_available: true,
1045 },
1046 }`,
Liz Kammerba3ea162021-02-17 13:22:03 -05001047 expectedBazelTargets: []string{
1048 `filegroup(
1049 name = "fg_bar",
1050)`,
1051 `// BUILD file`,
1052 },
Jingwen Chen5146ac02021-09-02 11:44:42 +00001053 filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -05001054 "other/BUILD.bazel": `// BUILD file`,
1055 },
1056 },
1057 }
1058
1059 dir := "."
1060 for _, testCase := range testCases {
Jingwen Chen49109762021-05-25 05:16:48 +00001061 t.Run(testCase.description, func(t *testing.T) {
1062 fs := make(map[string][]byte)
1063 toParse := []string{
1064 "Android.bp",
Liz Kammerba3ea162021-02-17 13:22:03 -05001065 }
Jingwen Chen5146ac02021-09-02 11:44:42 +00001066 for f, content := range testCase.filesystem {
Jingwen Chen49109762021-05-25 05:16:48 +00001067 if strings.HasSuffix(f, "Android.bp") {
1068 toParse = append(toParse, f)
1069 }
1070 fs[f] = []byte(content)
1071 }
Jingwen Chen5146ac02021-09-02 11:44:42 +00001072 config := android.TestConfig(buildDir, nil, testCase.blueprint, fs)
Jingwen Chen49109762021-05-25 05:16:48 +00001073 ctx := android.NewTestContext(config)
1074 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Jingwen Chen49109762021-05-25 05:16:48 +00001075 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1076 ctx.RegisterForBazelConversion()
Liz Kammerba3ea162021-02-17 13:22:03 -05001077
Jingwen Chen49109762021-05-25 05:16:48 +00001078 _, errs := ctx.ParseFileList(dir, toParse)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001079 if errored(t, testCase, errs) {
Jingwen Chen49109762021-05-25 05:16:48 +00001080 return
1081 }
1082 _, errs = ctx.ResolveDependencies(config)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001083 if errored(t, testCase, errs) {
Jingwen Chen49109762021-05-25 05:16:48 +00001084 return
1085 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001086
Jingwen Chen49109762021-05-25 05:16:48 +00001087 checkDir := dir
1088 if testCase.dir != "" {
1089 checkDir = testCase.dir
1090 }
1091 bazelTargets := generateBazelTargetsForDir(NewCodegenContext(config, *ctx.Context, Bp2Build), checkDir)
1092 bazelTargets.sort()
1093 actualCount := len(bazelTargets)
1094 expectedCount := len(testCase.expectedBazelTargets)
1095 if actualCount != expectedCount {
1096 t.Errorf("Expected %d bazel target, got %d\n%s", expectedCount, actualCount, bazelTargets)
1097 }
1098 if !strings.Contains(bazelTargets.String(), "Section: Handcrafted targets. ") {
1099 t.Errorf("Expected string representation of bazelTargets to contain handcrafted section header.")
1100 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001101 for i, target := range bazelTargets {
Jingwen Chen49109762021-05-25 05:16:48 +00001102 actualContent := target.content
1103 expectedContent := testCase.expectedBazelTargets[i]
1104 if expectedContent != actualContent {
Liz Kammerba3ea162021-02-17 13:22:03 -05001105 t.Errorf(
Jingwen Chen49109762021-05-25 05:16:48 +00001106 "Expected generated Bazel target to be '%s', got '%s'",
1107 expectedContent,
1108 actualContent,
Liz Kammerba3ea162021-02-17 13:22:03 -05001109 )
1110 }
1111 }
Jingwen Chen49109762021-05-25 05:16:48 +00001112 })
Liz Kammerba3ea162021-02-17 13:22:03 -05001113 }
1114}
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001115
1116func TestGlobExcludeSrcs(t *testing.T) {
Jingwen Chen5146ac02021-09-02 11:44:42 +00001117 testCases := []bp2buildTestCase{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001118 {
1119 description: "filegroup top level exclude_srcs",
1120 moduleTypeUnderTest: "filegroup",
1121 moduleTypeUnderTestFactory: android.FileGroupFactory,
1122 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +00001123 blueprint: `filegroup {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001124 name: "fg_foo",
1125 srcs: ["**/*.txt"],
1126 exclude_srcs: ["c.txt"],
1127 bazel_module: { bp2build_available: true },
1128}`,
1129 expectedBazelTargets: []string{`filegroup(
1130 name = "fg_foo",
1131 srcs = [
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001132 "a.txt",
1133 "b.txt",
Liz Kammer9abd62d2021-05-21 08:37:59 -04001134 "//dir:e.txt",
1135 "//dir:f.txt",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001136 ],
1137)`,
1138 },
Jingwen Chen5146ac02021-09-02 11:44:42 +00001139 filesystem: map[string]string{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001140 "a.txt": "",
1141 "b.txt": "",
1142 "c.txt": "",
1143 "dir/Android.bp": "",
1144 "dir/e.txt": "",
1145 "dir/f.txt": "",
1146 },
1147 },
1148 {
1149 description: "filegroup in subdir exclude_srcs",
1150 moduleTypeUnderTest: "filegroup",
1151 moduleTypeUnderTestFactory: android.FileGroupFactory,
1152 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +00001153 blueprint: "",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001154 dir: "dir",
Jingwen Chen5146ac02021-09-02 11:44:42 +00001155 filesystem: map[string]string{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001156 "dir/Android.bp": `filegroup {
1157 name: "fg_foo",
1158 srcs: ["**/*.txt"],
1159 exclude_srcs: ["b.txt"],
1160 bazel_module: { bp2build_available: true },
1161}
1162`,
1163 "dir/a.txt": "",
1164 "dir/b.txt": "",
1165 "dir/subdir/Android.bp": "",
1166 "dir/subdir/e.txt": "",
1167 "dir/subdir/f.txt": "",
1168 },
1169 expectedBazelTargets: []string{`filegroup(
1170 name = "fg_foo",
1171 srcs = [
Liz Kammer9abd62d2021-05-21 08:37:59 -04001172 "a.txt",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001173 "//dir/subdir:e.txt",
1174 "//dir/subdir:f.txt",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001175 ],
1176)`,
1177 },
1178 },
1179 }
1180
1181 dir := "."
1182 for _, testCase := range testCases {
1183 fs := make(map[string][]byte)
1184 toParse := []string{
1185 "Android.bp",
1186 }
Jingwen Chen5146ac02021-09-02 11:44:42 +00001187 for f, content := range testCase.filesystem {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001188 if strings.HasSuffix(f, "Android.bp") {
1189 toParse = append(toParse, f)
1190 }
1191 fs[f] = []byte(content)
1192 }
Jingwen Chen5146ac02021-09-02 11:44:42 +00001193 config := android.TestConfig(buildDir, nil, testCase.blueprint, fs)
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001194 ctx := android.NewTestContext(config)
1195 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
1196 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1197 ctx.RegisterForBazelConversion()
1198
1199 _, errs := ctx.ParseFileList(dir, toParse)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001200 if errored(t, testCase, errs) {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001201 continue
1202 }
1203 _, errs = ctx.ResolveDependencies(config)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001204 if errored(t, testCase, errs) {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001205 continue
1206 }
1207
1208 checkDir := dir
1209 if testCase.dir != "" {
1210 checkDir = testCase.dir
1211 }
1212 bazelTargets := generateBazelTargetsForDir(NewCodegenContext(config, *ctx.Context, Bp2Build), checkDir)
1213 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
1214 t.Errorf("%s: Expected %d bazel target, got %d\n%s", testCase.description, expectedCount, actualCount, bazelTargets)
1215 } else {
1216 for i, target := range bazelTargets {
1217 if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
1218 t.Errorf(
1219 "%s: Expected generated Bazel target to be '%s', got '%s'",
1220 testCase.description,
1221 w,
1222 g,
1223 )
1224 }
1225 }
1226 }
1227 }
1228}