blob: f14574c064598fb1fbd3277477489db1b0f8defa [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 (
Liz Kammer6eff3232021-08-26 08:37:59 -040018 "fmt"
Liz Kammer356f7d42021-01-26 09:18:53 -050019 "strings"
Liz Kammer2dd9ca42020-11-25 16:06:39 -080020 "testing"
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +000021
22 "android/soong/android"
23 "android/soong/python"
Liz Kammer2dd9ca42020-11-25 16:06:39 -080024)
25
26func TestGenerateSoongModuleTargets(t *testing.T) {
27 testCases := []struct {
Liz Kammerd366c902021-06-03 13:43:01 -040028 description string
Liz Kammer2dd9ca42020-11-25 16:06:39 -080029 bp string
30 expectedBazelTarget string
31 }{
32 {
Liz Kammerd366c902021-06-03 13:43:01 -040033 description: "only name",
Jingwen Chenb4628eb2021-04-08 14:40:57 +000034 bp: `custom { name: "foo" }
Liz Kammerd366c902021-06-03 13:43:01 -040035 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080036 expectedBazelTarget: `soong_module(
37 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050038 soong_module_name = "foo",
39 soong_module_type = "custom",
40 soong_module_variant = "",
41 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080042 ],
Liz Kammerd366c902021-06-03 13:43:01 -040043 bool_prop = False,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080044)`,
45 },
46 {
Liz Kammerd366c902021-06-03 13:43:01 -040047 description: "handles bool",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080048 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -040049 name: "foo",
50 bool_prop: true,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080051}
Liz Kammerd366c902021-06-03 13:43:01 -040052 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080053 expectedBazelTarget: `soong_module(
54 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050055 soong_module_name = "foo",
56 soong_module_type = "custom",
57 soong_module_variant = "",
58 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080059 ],
Liz Kammerd366c902021-06-03 13:43:01 -040060 bool_prop = True,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080061)`,
62 },
63 {
Liz Kammerd366c902021-06-03 13:43:01 -040064 description: "string escaping",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080065 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -040066 name: "foo",
67 owner: "a_string_with\"quotes\"_and_\\backslashes\\\\",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080068}
Liz Kammerd366c902021-06-03 13:43:01 -040069 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080070 expectedBazelTarget: `soong_module(
71 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050072 soong_module_name = "foo",
73 soong_module_type = "custom",
74 soong_module_variant = "",
75 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080076 ],
Liz Kammerd366c902021-06-03 13:43:01 -040077 bool_prop = False,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080078 owner = "a_string_with\"quotes\"_and_\\backslashes\\\\",
79)`,
80 },
81 {
Liz Kammerd366c902021-06-03 13:43:01 -040082 description: "single item string list",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080083 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -040084 name: "foo",
85 required: ["bar"],
Liz Kammer2dd9ca42020-11-25 16:06:39 -080086}
Liz Kammerd366c902021-06-03 13:43:01 -040087 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080088 expectedBazelTarget: `soong_module(
89 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050090 soong_module_name = "foo",
91 soong_module_type = "custom",
92 soong_module_variant = "",
93 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080094 ],
Liz Kammerd366c902021-06-03 13:43:01 -040095 bool_prop = False,
Jingwen Chenb4628eb2021-04-08 14:40:57 +000096 required = ["bar"],
Liz Kammer2dd9ca42020-11-25 16:06:39 -080097)`,
98 },
99 {
Liz Kammerd366c902021-06-03 13:43:01 -0400100 description: "list of strings",
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800101 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -0400102 name: "foo",
103 target_required: ["qux", "bazqux"],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800104}
Liz Kammerd366c902021-06-03 13:43:01 -0400105 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800106 expectedBazelTarget: `soong_module(
107 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500108 soong_module_name = "foo",
109 soong_module_type = "custom",
110 soong_module_variant = "",
111 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800112 ],
Liz Kammerd366c902021-06-03 13:43:01 -0400113 bool_prop = False,
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800114 target_required = [
115 "qux",
116 "bazqux",
117 ],
118)`,
119 },
120 {
Liz Kammerd366c902021-06-03 13:43:01 -0400121 description: "dist/dists",
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800122 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -0400123 name: "foo",
124 dist: {
125 targets: ["goal_foo"],
126 tag: ".foo",
127 },
128 dists: [{
129 targets: ["goal_bar"],
130 tag: ".bar",
131 }],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800132}
Liz Kammerd366c902021-06-03 13:43:01 -0400133 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800134 expectedBazelTarget: `soong_module(
135 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500136 soong_module_name = "foo",
137 soong_module_type = "custom",
138 soong_module_variant = "",
139 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800140 ],
Liz Kammerd366c902021-06-03 13:43:01 -0400141 bool_prop = False,
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800142 dist = {
143 "tag": ".foo",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000144 "targets": ["goal_foo"],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800145 },
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000146 dists = [{
147 "tag": ".bar",
148 "targets": ["goal_bar"],
149 }],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800150)`,
151 },
152 {
Liz Kammerd366c902021-06-03 13:43:01 -0400153 description: "put it together",
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800154 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -0400155 name: "foo",
156 required: ["bar"],
157 target_required: ["qux", "bazqux"],
158 bool_prop: true,
159 owner: "custom_owner",
160 dists: [
161 {
162 tag: ".tag",
163 targets: ["my_goal"],
164 },
165 ],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800166}
Liz Kammerd366c902021-06-03 13:43:01 -0400167 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800168 expectedBazelTarget: `soong_module(
169 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500170 soong_module_name = "foo",
171 soong_module_type = "custom",
172 soong_module_variant = "",
173 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800174 ],
Liz Kammerd366c902021-06-03 13:43:01 -0400175 bool_prop = True,
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000176 dists = [{
177 "tag": ".tag",
178 "targets": ["my_goal"],
179 }],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800180 owner = "custom_owner",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000181 required = ["bar"],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800182 target_required = [
183 "qux",
184 "bazqux",
185 ],
186)`,
187 },
188 }
189
190 dir := "."
191 for _, testCase := range testCases {
Liz Kammerd366c902021-06-03 13:43:01 -0400192 t.Run(testCase.description, func(t *testing.T) {
193 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
194 ctx := android.NewTestContext(config)
Jingwen Chen164e0862021-02-19 00:48:40 -0500195
Liz Kammerd366c902021-06-03 13:43:01 -0400196 ctx.RegisterModuleType("custom", customModuleFactory)
197 ctx.Register()
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800198
Liz Kammerd366c902021-06-03 13:43:01 -0400199 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
200 android.FailIfErrored(t, errs)
201 _, errs = ctx.PrepareBuildActions(config)
202 android.FailIfErrored(t, errs)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800203
Liz Kammerd366c902021-06-03 13:43:01 -0400204 codegenCtx := NewCodegenContext(config, *ctx.Context, QueryView)
Liz Kammer6eff3232021-08-26 08:37:59 -0400205 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
206 android.FailIfErrored(t, err)
Liz Kammerd366c902021-06-03 13:43:01 -0400207 if actualCount, expectedCount := len(bazelTargets), 1; actualCount != expectedCount {
208 t.Fatalf("Expected %d bazel target, got %d", expectedCount, actualCount)
209 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800210
Liz Kammerd366c902021-06-03 13:43:01 -0400211 actualBazelTarget := bazelTargets[0]
212 if actualBazelTarget.content != testCase.expectedBazelTarget {
213 t.Errorf(
214 "Expected generated Bazel target to be '%s', got '%s'",
215 testCase.expectedBazelTarget,
216 actualBazelTarget.content,
217 )
218 }
219 })
Jingwen Chen73850672020-12-14 08:25:34 -0500220 }
221}
222
223func TestGenerateBazelTargetModules(t *testing.T) {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000224 testCases := []bp2buildTestCase{
Jingwen Chen73850672020-12-14 08:25:34 -0500225 {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000226 blueprint: `custom {
Jingwen Chen73850672020-12-14 08:25:34 -0500227 name: "foo",
228 string_list_prop: ["a", "b"],
229 string_prop: "a",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500230 bazel_module: { bp2build_available: true },
Jingwen Chen73850672020-12-14 08:25:34 -0500231}`,
Liz Kammer4562a3b2021-04-21 18:15:34 -0400232 expectedBazelTargets: []string{`custom(
Jingwen Chen73850672020-12-14 08:25:34 -0500233 name = "foo",
234 string_list_prop = [
235 "a",
236 "b",
237 ],
238 string_prop = "a",
239)`,
Liz Kammer4562a3b2021-04-21 18:15:34 -0400240 },
Jingwen Chen73850672020-12-14 08:25:34 -0500241 },
Jingwen Chen58a12b82021-03-30 13:08:36 +0000242 {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000243 blueprint: `custom {
Jingwen Chen58a12b82021-03-30 13:08:36 +0000244 name: "control_characters",
245 string_list_prop: ["\t", "\n"],
246 string_prop: "a\t\n\r",
247 bazel_module: { bp2build_available: true },
248}`,
Liz Kammer4562a3b2021-04-21 18:15:34 -0400249 expectedBazelTargets: []string{`custom(
Jingwen Chen58a12b82021-03-30 13:08:36 +0000250 name = "control_characters",
251 string_list_prop = [
252 "\t",
253 "\n",
254 ],
255 string_prop = "a\t\n\r",
256)`,
Liz Kammer4562a3b2021-04-21 18:15:34 -0400257 },
258 },
259 {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000260 blueprint: `custom {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400261 name: "has_dep",
262 arch_paths: [":dep"],
263 bazel_module: { bp2build_available: true },
264}
265
266custom {
267 name: "dep",
268 arch_paths: ["abc"],
269 bazel_module: { bp2build_available: true },
270}`,
271 expectedBazelTargets: []string{`custom(
272 name = "dep",
273 arch_paths = ["abc"],
274)`,
275 `custom(
276 name = "has_dep",
277 arch_paths = [":dep"],
278)`,
279 },
280 },
281 {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000282 blueprint: `custom {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400283 name: "arch_paths",
284 arch: {
285 x86: {
286 arch_paths: ["abc"],
287 },
288 },
289 bazel_module: { bp2build_available: true },
290}`,
291 expectedBazelTargets: []string{`custom(
292 name = "arch_paths",
293 arch_paths = select({
294 "//build/bazel/platforms/arch:x86": ["abc"],
295 "//conditions:default": [],
296 }),
297)`,
298 },
299 },
300 {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000301 blueprint: `custom {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400302 name: "has_dep",
303 arch: {
304 x86: {
305 arch_paths: [":dep"],
306 },
307 },
308 bazel_module: { bp2build_available: true },
309}
310
311custom {
312 name: "dep",
313 arch_paths: ["abc"],
314 bazel_module: { bp2build_available: true },
315}`,
316 expectedBazelTargets: []string{`custom(
317 name = "dep",
318 arch_paths = ["abc"],
319)`,
320 `custom(
321 name = "has_dep",
322 arch_paths = select({
323 "//build/bazel/platforms/arch:x86": [":dep"],
324 "//conditions:default": [],
325 }),
326)`,
327 },
Jingwen Chen58a12b82021-03-30 13:08:36 +0000328 },
Liz Kammer32a03392021-09-14 11:17:21 -0400329 {
330 blueprint: `custom {
331 name: "embedded_props",
332 embedded_prop: "abc",
333 bazel_module: { bp2build_available: true },
334}`,
335 expectedBazelTargets: []string{`custom(
336 name = "embedded_props",
337 embedded_attr = "abc",
338)`,
339 },
340 },
341 {
342 blueprint: `custom {
343 name: "ptr_to_embedded_props",
344 other_embedded_prop: "abc",
345 bazel_module: { bp2build_available: true },
346}`,
347 expectedBazelTargets: []string{`custom(
348 name = "ptr_to_embedded_props",
349 other_embedded_attr = "abc",
350)`,
351 },
352 },
Jingwen Chen73850672020-12-14 08:25:34 -0500353 }
354
355 dir := "."
356 for _, testCase := range testCases {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000357 config := android.TestConfig(buildDir, nil, testCase.blueprint, nil)
Jingwen Chen73850672020-12-14 08:25:34 -0500358 ctx := android.NewTestContext(config)
Jingwen Chen164e0862021-02-19 00:48:40 -0500359
Liz Kammer32b77cf2021-08-04 15:17:02 -0400360 registerCustomModuleForBp2buildConversion(ctx)
Jingwen Chen73850672020-12-14 08:25:34 -0500361
362 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
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 _, errs = ctx.ResolveDependencies(config)
Jingwen Chen5146ac02021-09-02 11:44:42 +0000367 if errored(t, testCase, errs) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500368 continue
369 }
Jingwen Chen73850672020-12-14 08:25:34 -0500370
Jingwen Chen164e0862021-02-19 00:48:40 -0500371 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Liz Kammer6eff3232021-08-26 08:37:59 -0400372 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
373 android.FailIfErrored(t, err)
Jingwen Chen164e0862021-02-19 00:48:40 -0500374
Liz Kammer4562a3b2021-04-21 18:15:34 -0400375 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
Liz Kammer356f7d42021-01-26 09:18:53 -0500376 t.Errorf("Expected %d bazel target, got %d", expectedCount, actualCount)
377 } else {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400378 for i, expectedBazelTarget := range testCase.expectedBazelTargets {
379 actualBazelTarget := bazelTargets[i]
380 if actualBazelTarget.content != expectedBazelTarget {
381 t.Errorf(
382 "Expected generated Bazel target to be '%s', got '%s'",
383 expectedBazelTarget,
384 actualBazelTarget.content,
385 )
386 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500387 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800388 }
389 }
390}
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500391
Jingwen Chen40067de2021-01-26 21:58:43 -0500392func TestLoadStatements(t *testing.T) {
393 testCases := []struct {
394 bazelTargets BazelTargets
395 expectedLoadStatements string
396 }{
397 {
398 bazelTargets: BazelTargets{
399 BazelTarget{
400 name: "foo",
401 ruleClass: "cc_library",
402 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
403 },
404 },
405 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
406 },
407 {
408 bazelTargets: BazelTargets{
409 BazelTarget{
410 name: "foo",
411 ruleClass: "cc_library",
412 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
413 },
414 BazelTarget{
415 name: "bar",
416 ruleClass: "cc_library",
417 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
418 },
419 },
420 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
421 },
422 {
423 bazelTargets: BazelTargets{
424 BazelTarget{
425 name: "foo",
426 ruleClass: "cc_library",
427 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
428 },
429 BazelTarget{
430 name: "bar",
431 ruleClass: "cc_binary",
432 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
433 },
434 },
435 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")`,
436 },
437 {
438 bazelTargets: BazelTargets{
439 BazelTarget{
440 name: "foo",
441 ruleClass: "cc_library",
442 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
443 },
444 BazelTarget{
445 name: "bar",
446 ruleClass: "cc_binary",
447 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
448 },
449 BazelTarget{
450 name: "baz",
451 ruleClass: "java_binary",
452 bzlLoadLocation: "//build/bazel/rules:java.bzl",
453 },
454 },
455 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")
456load("//build/bazel/rules:java.bzl", "java_binary")`,
457 },
458 {
459 bazelTargets: BazelTargets{
460 BazelTarget{
461 name: "foo",
462 ruleClass: "cc_binary",
463 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
464 },
465 BazelTarget{
466 name: "bar",
467 ruleClass: "java_binary",
468 bzlLoadLocation: "//build/bazel/rules:java.bzl",
469 },
470 BazelTarget{
471 name: "baz",
472 ruleClass: "genrule",
473 // Note: no bzlLoadLocation for native rules
474 },
475 },
476 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary")
477load("//build/bazel/rules:java.bzl", "java_binary")`,
478 },
479 }
480
481 for _, testCase := range testCases {
482 actual := testCase.bazelTargets.LoadStatements()
483 expected := testCase.expectedLoadStatements
484 if actual != expected {
485 t.Fatalf("Expected load statements to be %s, got %s", expected, actual)
486 }
487 }
488
489}
490
491func TestGenerateBazelTargetModules_OneToMany_LoadedFromStarlark(t *testing.T) {
492 testCases := []struct {
493 bp string
494 expectedBazelTarget string
495 expectedBazelTargetCount int
496 expectedLoadStatements string
497 }{
498 {
499 bp: `custom {
500 name: "bar",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500501 bazel_module: { bp2build_available: true },
Jingwen Chen40067de2021-01-26 21:58:43 -0500502}`,
503 expectedBazelTarget: `my_library(
504 name = "bar",
505)
506
Jingwen Chen40067de2021-01-26 21:58:43 -0500507proto_library(
508 name = "bar_proto_library_deps",
Liz Kammer2ada09a2021-08-11 00:17:36 -0400509)
510
511my_proto_library(
512 name = "bar_my_proto_library_deps",
Jingwen Chen40067de2021-01-26 21:58:43 -0500513)`,
514 expectedBazelTargetCount: 3,
515 expectedLoadStatements: `load("//build/bazel/rules:proto.bzl", "my_proto_library", "proto_library")
516load("//build/bazel/rules:rules.bzl", "my_library")`,
517 },
518 }
519
520 dir := "."
521 for _, testCase := range testCases {
522 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
523 ctx := android.NewTestContext(config)
524 ctx.RegisterModuleType("custom", customModuleFactory)
Jingwen Chen12b4c272021-03-10 02:05:59 -0500525 ctx.RegisterBp2BuildMutator("custom", customBp2BuildMutatorFromStarlark)
Jingwen Chen40067de2021-01-26 21:58:43 -0500526 ctx.RegisterForBazelConversion()
527
528 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
529 android.FailIfErrored(t, errs)
530 _, errs = ctx.ResolveDependencies(config)
531 android.FailIfErrored(t, errs)
532
Jingwen Chen164e0862021-02-19 00:48:40 -0500533 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Liz Kammer6eff3232021-08-26 08:37:59 -0400534 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
535 android.FailIfErrored(t, err)
Jingwen Chen40067de2021-01-26 21:58:43 -0500536 if actualCount := len(bazelTargets); actualCount != testCase.expectedBazelTargetCount {
537 t.Fatalf("Expected %d bazel target, got %d", testCase.expectedBazelTargetCount, actualCount)
538 }
539
540 actualBazelTargets := bazelTargets.String()
541 if actualBazelTargets != testCase.expectedBazelTarget {
542 t.Errorf(
543 "Expected generated Bazel target to be '%s', got '%s'",
544 testCase.expectedBazelTarget,
545 actualBazelTargets,
546 )
547 }
548
549 actualLoadStatements := bazelTargets.LoadStatements()
550 if actualLoadStatements != testCase.expectedLoadStatements {
551 t.Errorf(
552 "Expected generated load statements to be '%s', got '%s'",
553 testCase.expectedLoadStatements,
554 actualLoadStatements,
555 )
556 }
557 }
558}
559
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500560func TestModuleTypeBp2Build(t *testing.T) {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000561 testCases := []bp2buildTestCase{
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500562 {
Liz Kammerebfcf672021-02-16 15:00:05 -0500563 description: "filegroup with does not specify srcs",
564 moduleTypeUnderTest: "filegroup",
565 moduleTypeUnderTestFactory: android.FileGroupFactory,
566 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000567 blueprint: `filegroup {
Liz Kammerebfcf672021-02-16 15:00:05 -0500568 name: "fg_foo",
569 bazel_module: { bp2build_available: true },
570}`,
571 expectedBazelTargets: []string{
572 `filegroup(
573 name = "fg_foo",
574)`,
575 },
576 },
577 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500578 description: "filegroup with no srcs",
579 moduleTypeUnderTest: "filegroup",
580 moduleTypeUnderTestFactory: android.FileGroupFactory,
581 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000582 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500583 name: "fg_foo",
584 srcs: [],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500585 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500586}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500587 expectedBazelTargets: []string{
588 `filegroup(
589 name = "fg_foo",
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500590)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500591 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500592 },
593 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500594 description: "filegroup with srcs",
595 moduleTypeUnderTest: "filegroup",
596 moduleTypeUnderTestFactory: android.FileGroupFactory,
597 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000598 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500599 name: "fg_foo",
600 srcs: ["a", "b"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500601 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500602}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500603 expectedBazelTargets: []string{`filegroup(
604 name = "fg_foo",
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500605 srcs = [
606 "a",
607 "b",
608 ],
609)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500610 },
611 },
612 {
613 description: "filegroup with excludes srcs",
614 moduleTypeUnderTest: "filegroup",
615 moduleTypeUnderTestFactory: android.FileGroupFactory,
616 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000617 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500618 name: "fg_foo",
619 srcs: ["a", "b"],
620 exclude_srcs: ["a"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500621 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500622}`,
623 expectedBazelTargets: []string{`filegroup(
624 name = "fg_foo",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000625 srcs = ["b"],
Liz Kammer356f7d42021-01-26 09:18:53 -0500626)`,
627 },
628 },
629 {
630 description: "filegroup with glob",
631 moduleTypeUnderTest: "filegroup",
632 moduleTypeUnderTestFactory: android.FileGroupFactory,
633 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000634 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500635 name: "foo",
636 srcs: ["**/*.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500637 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500638}`,
639 expectedBazelTargets: []string{`filegroup(
640 name = "foo",
641 srcs = [
642 "other/a.txt",
643 "other/b.txt",
644 "other/subdir/a.txt",
645 ],
646)`,
647 },
Jingwen Chen5146ac02021-09-02 11:44:42 +0000648 filesystem: map[string]string{
Liz Kammer356f7d42021-01-26 09:18:53 -0500649 "other/a.txt": "",
650 "other/b.txt": "",
651 "other/subdir/a.txt": "",
652 "other/file": "",
653 },
654 },
655 {
656 description: "filegroup with glob in subdir",
657 moduleTypeUnderTest: "filegroup",
658 moduleTypeUnderTestFactory: android.FileGroupFactory,
659 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000660 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500661 name: "foo",
662 srcs: ["a.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500663 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500664}`,
665 dir: "other",
666 expectedBazelTargets: []string{`filegroup(
667 name = "fg_foo",
668 srcs = [
669 "a.txt",
670 "b.txt",
671 "subdir/a.txt",
672 ],
673)`,
674 },
Jingwen Chen5146ac02021-09-02 11:44:42 +0000675 filesystem: map[string]string{
Liz Kammer356f7d42021-01-26 09:18:53 -0500676 "other/Android.bp": `filegroup {
677 name: "fg_foo",
678 srcs: ["**/*.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500679 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500680}`,
681 "other/a.txt": "",
682 "other/b.txt": "",
683 "other/subdir/a.txt": "",
684 "other/file": "",
685 },
686 },
687 {
688 description: "depends_on_other_dir_module",
689 moduleTypeUnderTest: "filegroup",
690 moduleTypeUnderTestFactory: android.FileGroupFactory,
691 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000692 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500693 name: "foobar",
694 srcs: [
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000695 ":foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500696 "c",
697 ],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500698 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500699}`,
700 expectedBazelTargets: []string{`filegroup(
701 name = "foobar",
702 srcs = [
703 "//other:foo",
704 "c",
705 ],
706)`,
707 },
Jingwen Chen5146ac02021-09-02 11:44:42 +0000708 filesystem: map[string]string{
Liz Kammer356f7d42021-01-26 09:18:53 -0500709 "other/Android.bp": `filegroup {
710 name: "foo",
711 srcs: ["a", "b"],
Liz Kammer6eff3232021-08-26 08:37:59 -0400712 bazel_module: { bp2build_available: true },
713}`,
714 },
715 },
716 {
717 description: "depends_on_other_unconverted_module_error",
718 moduleTypeUnderTest: "filegroup",
719 moduleTypeUnderTestFactory: android.FileGroupFactory,
720 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
721 unconvertedDepsMode: errorModulesUnconvertedDeps,
722 blueprint: `filegroup {
723 name: "foobar",
724 srcs: [
725 ":foo",
726 "c",
727 ],
728 bazel_module: { bp2build_available: true },
729}`,
730 expectedErr: fmt.Errorf(`"foobar" depends on unconverted modules: foo`),
731 filesystem: map[string]string{
732 "other/Android.bp": `filegroup {
733 name: "foo",
734 srcs: ["a", "b"],
Liz Kammer356f7d42021-01-26 09:18:53 -0500735}`,
736 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500737 },
738 }
739
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500740 for _, testCase := range testCases {
Liz Kammer6eff3232021-08-26 08:37:59 -0400741 t.Run(testCase.description, func(t *testing.T) {
742 runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, testCase)
743 })
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500744 }
745}
Jingwen Chen041b1842021-02-01 00:23:25 -0500746
747type bp2buildMutator = func(android.TopDownMutatorContext)
748
Jingwen Chen12b4c272021-03-10 02:05:59 -0500749func TestAllowlistingBp2buildTargetsExplicitly(t *testing.T) {
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500750 testCases := []struct {
751 moduleTypeUnderTest string
752 moduleTypeUnderTestFactory android.ModuleFactory
753 moduleTypeUnderTestBp2BuildMutator bp2buildMutator
754 bp string
755 expectedCount int
756 description string
757 }{
758 {
759 description: "explicitly unavailable",
760 moduleTypeUnderTest: "filegroup",
761 moduleTypeUnderTestFactory: android.FileGroupFactory,
762 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
763 bp: `filegroup {
764 name: "foo",
765 srcs: ["a", "b"],
766 bazel_module: { bp2build_available: false },
767}`,
768 expectedCount: 0,
769 },
770 {
771 description: "implicitly unavailable",
772 moduleTypeUnderTest: "filegroup",
773 moduleTypeUnderTestFactory: android.FileGroupFactory,
774 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
775 bp: `filegroup {
776 name: "foo",
777 srcs: ["a", "b"],
778}`,
779 expectedCount: 0,
780 },
781 {
782 description: "explicitly available",
783 moduleTypeUnderTest: "filegroup",
784 moduleTypeUnderTestFactory: android.FileGroupFactory,
785 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
786 bp: `filegroup {
787 name: "foo",
788 srcs: ["a", "b"],
789 bazel_module: { bp2build_available: true },
790}`,
791 expectedCount: 1,
792 },
793 {
794 description: "generates more than 1 target if needed",
795 moduleTypeUnderTest: "custom",
796 moduleTypeUnderTestFactory: customModuleFactory,
797 moduleTypeUnderTestBp2BuildMutator: customBp2BuildMutatorFromStarlark,
798 bp: `custom {
799 name: "foo",
800 bazel_module: { bp2build_available: true },
801}`,
802 expectedCount: 3,
803 },
804 }
805
806 dir := "."
807 for _, testCase := range testCases {
Liz Kammer2ada09a2021-08-11 00:17:36 -0400808 t.Run(testCase.description, func(t *testing.T) {
809 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
810 ctx := android.NewTestContext(config)
811 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
812 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
813 ctx.RegisterForBazelConversion()
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500814
Liz Kammer2ada09a2021-08-11 00:17:36 -0400815 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
816 android.FailIfErrored(t, errs)
817 _, errs = ctx.ResolveDependencies(config)
818 android.FailIfErrored(t, errs)
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500819
Liz Kammer2ada09a2021-08-11 00:17:36 -0400820 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Liz Kammer6eff3232021-08-26 08:37:59 -0400821 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
822 android.FailIfErrored(t, err)
Liz Kammer2ada09a2021-08-11 00:17:36 -0400823 if actualCount := len(bazelTargets); actualCount != testCase.expectedCount {
824 t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, testCase.expectedCount, actualCount)
825 }
826 })
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500827 }
828}
Liz Kammerba3ea162021-02-17 13:22:03 -0500829
Jingwen Chen12b4c272021-03-10 02:05:59 -0500830func TestAllowlistingBp2buildTargetsWithConfig(t *testing.T) {
831 testCases := []struct {
832 moduleTypeUnderTest string
833 moduleTypeUnderTestFactory android.ModuleFactory
834 moduleTypeUnderTestBp2BuildMutator bp2buildMutator
835 expectedCount map[string]int
836 description string
837 bp2buildConfig android.Bp2BuildConfig
838 checkDir string
839 fs map[string]string
840 }{
841 {
842 description: "test bp2build config package and subpackages config",
843 moduleTypeUnderTest: "filegroup",
844 moduleTypeUnderTestFactory: android.FileGroupFactory,
845 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
846 expectedCount: map[string]int{
847 "migrated": 1,
848 "migrated/but_not_really": 0,
849 "migrated/but_not_really/but_really": 1,
850 "not_migrated": 0,
851 "also_not_migrated": 0,
852 },
853 bp2buildConfig: android.Bp2BuildConfig{
854 "migrated": android.Bp2BuildDefaultTrueRecursively,
855 "migrated/but_not_really": android.Bp2BuildDefaultFalse,
856 "not_migrated": android.Bp2BuildDefaultFalse,
857 },
858 fs: map[string]string{
859 "migrated/Android.bp": `filegroup { name: "a" }`,
860 "migrated/but_not_really/Android.bp": `filegroup { name: "b" }`,
861 "migrated/but_not_really/but_really/Android.bp": `filegroup { name: "c" }`,
862 "not_migrated/Android.bp": `filegroup { name: "d" }`,
863 "also_not_migrated/Android.bp": `filegroup { name: "e" }`,
864 },
865 },
866 {
867 description: "test bp2build config opt-in and opt-out",
868 moduleTypeUnderTest: "filegroup",
869 moduleTypeUnderTestFactory: android.FileGroupFactory,
870 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
871 expectedCount: map[string]int{
872 "package-opt-in": 2,
873 "package-opt-in/subpackage": 0,
874 "package-opt-out": 1,
875 "package-opt-out/subpackage": 0,
876 },
877 bp2buildConfig: android.Bp2BuildConfig{
878 "package-opt-in": android.Bp2BuildDefaultFalse,
879 "package-opt-out": android.Bp2BuildDefaultTrueRecursively,
880 },
881 fs: map[string]string{
882 "package-opt-in/Android.bp": `
883filegroup { name: "opt-in-a" }
884filegroup { name: "opt-in-b", bazel_module: { bp2build_available: true } }
885filegroup { name: "opt-in-c", bazel_module: { bp2build_available: true } }
886`,
887
888 "package-opt-in/subpackage/Android.bp": `
889filegroup { name: "opt-in-d" } // parent package not configured to DefaultTrueRecursively
890`,
891
892 "package-opt-out/Android.bp": `
893filegroup { name: "opt-out-a" }
894filegroup { name: "opt-out-b", bazel_module: { bp2build_available: false } }
895filegroup { name: "opt-out-c", bazel_module: { bp2build_available: false } }
896`,
897
898 "package-opt-out/subpackage/Android.bp": `
899filegroup { name: "opt-out-g", bazel_module: { bp2build_available: false } }
900filegroup { name: "opt-out-h", bazel_module: { bp2build_available: false } }
901`,
902 },
903 },
904 }
905
906 dir := "."
907 for _, testCase := range testCases {
908 fs := make(map[string][]byte)
909 toParse := []string{
910 "Android.bp",
911 }
912 for f, content := range testCase.fs {
913 if strings.HasSuffix(f, "Android.bp") {
914 toParse = append(toParse, f)
915 }
916 fs[f] = []byte(content)
917 }
918 config := android.TestConfig(buildDir, nil, "", fs)
919 ctx := android.NewTestContext(config)
920 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
921 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
922 ctx.RegisterBp2BuildConfig(testCase.bp2buildConfig)
923 ctx.RegisterForBazelConversion()
924
925 _, errs := ctx.ParseFileList(dir, toParse)
926 android.FailIfErrored(t, errs)
927 _, errs = ctx.ResolveDependencies(config)
928 android.FailIfErrored(t, errs)
929
930 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
931
932 // For each directory, test that the expected number of generated targets is correct.
933 for dir, expectedCount := range testCase.expectedCount {
Liz Kammer6eff3232021-08-26 08:37:59 -0400934 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
935 android.FailIfErrored(t, err)
Jingwen Chen12b4c272021-03-10 02:05:59 -0500936 if actualCount := len(bazelTargets); actualCount != expectedCount {
937 t.Fatalf(
938 "%s: Expected %d bazel target for %s package, got %d",
939 testCase.description,
940 expectedCount,
941 dir,
942 actualCount)
943 }
944
945 }
946 }
947}
948
Liz Kammerba3ea162021-02-17 13:22:03 -0500949func TestCombineBuildFilesBp2buildTargets(t *testing.T) {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000950 testCases := []bp2buildTestCase{
Liz Kammerba3ea162021-02-17 13:22:03 -0500951 {
952 description: "filegroup bazel_module.label",
953 moduleTypeUnderTest: "filegroup",
954 moduleTypeUnderTestFactory: android.FileGroupFactory,
955 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000956 blueprint: `filegroup {
Liz Kammerba3ea162021-02-17 13:22:03 -0500957 name: "fg_foo",
958 bazel_module: { label: "//other:fg_foo" },
959}`,
960 expectedBazelTargets: []string{
961 `// BUILD file`,
962 },
Jingwen Chen5146ac02021-09-02 11:44:42 +0000963 filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -0500964 "other/BUILD.bazel": `// BUILD file`,
965 },
966 },
967 {
968 description: "multiple bazel_module.label same BUILD",
969 moduleTypeUnderTest: "filegroup",
970 moduleTypeUnderTestFactory: android.FileGroupFactory,
971 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000972 blueprint: `filegroup {
Jingwen Chenc63677b2021-06-17 05:43:19 +0000973 name: "fg_foo",
974 bazel_module: { label: "//other:fg_foo" },
975 }
Liz Kammerba3ea162021-02-17 13:22:03 -0500976
Jingwen Chenc63677b2021-06-17 05:43:19 +0000977 filegroup {
978 name: "foo",
979 bazel_module: { label: "//other:foo" },
980 }`,
Liz Kammerba3ea162021-02-17 13:22:03 -0500981 expectedBazelTargets: []string{
982 `// BUILD file`,
983 },
Jingwen Chen5146ac02021-09-02 11:44:42 +0000984 filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -0500985 "other/BUILD.bazel": `// BUILD file`,
986 },
987 },
988 {
Jingwen Chenc63677b2021-06-17 05:43:19 +0000989 description: "filegroup bazel_module.label and bp2build in subdir",
Liz Kammerba3ea162021-02-17 13:22:03 -0500990 moduleTypeUnderTest: "filegroup",
991 moduleTypeUnderTestFactory: android.FileGroupFactory,
992 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chenc63677b2021-06-17 05:43:19 +0000993 dir: "other",
Jingwen Chen5146ac02021-09-02 11:44:42 +0000994 blueprint: ``,
995 filesystem: map[string]string{
Jingwen Chenc63677b2021-06-17 05:43:19 +0000996 "other/Android.bp": `filegroup {
997 name: "fg_foo",
998 bazel_module: {
999 bp2build_available: true,
1000 },
1001 }
1002 filegroup {
1003 name: "fg_bar",
1004 bazel_module: {
1005 label: "//other:fg_bar"
1006 },
1007 }`,
1008 "other/BUILD.bazel": `// definition for fg_bar`,
1009 },
Liz Kammerba3ea162021-02-17 13:22:03 -05001010 expectedBazelTargets: []string{
1011 `filegroup(
1012 name = "fg_foo",
Jingwen Chenc63677b2021-06-17 05:43:19 +00001013)`, `// definition for fg_bar`,
Liz Kammerba3ea162021-02-17 13:22:03 -05001014 },
1015 },
1016 {
1017 description: "filegroup bazel_module.label and filegroup bp2build",
1018 moduleTypeUnderTest: "filegroup",
1019 moduleTypeUnderTestFactory: android.FileGroupFactory,
1020 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +00001021 blueprint: `filegroup {
Jingwen Chenc63677b2021-06-17 05:43:19 +00001022 name: "fg_foo",
1023 bazel_module: {
1024 label: "//other:fg_foo",
1025 },
1026 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001027
Jingwen Chenc63677b2021-06-17 05:43:19 +00001028 filegroup {
1029 name: "fg_bar",
1030 bazel_module: {
1031 bp2build_available: true,
1032 },
1033 }`,
Liz Kammerba3ea162021-02-17 13:22:03 -05001034 expectedBazelTargets: []string{
1035 `filegroup(
1036 name = "fg_bar",
1037)`,
1038 `// BUILD file`,
1039 },
Jingwen Chen5146ac02021-09-02 11:44:42 +00001040 filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -05001041 "other/BUILD.bazel": `// BUILD file`,
1042 },
1043 },
1044 }
1045
1046 dir := "."
1047 for _, testCase := range testCases {
Jingwen Chen49109762021-05-25 05:16:48 +00001048 t.Run(testCase.description, func(t *testing.T) {
1049 fs := make(map[string][]byte)
1050 toParse := []string{
1051 "Android.bp",
Liz Kammerba3ea162021-02-17 13:22:03 -05001052 }
Jingwen Chen5146ac02021-09-02 11:44:42 +00001053 for f, content := range testCase.filesystem {
Jingwen Chen49109762021-05-25 05:16:48 +00001054 if strings.HasSuffix(f, "Android.bp") {
1055 toParse = append(toParse, f)
1056 }
1057 fs[f] = []byte(content)
1058 }
Jingwen Chen5146ac02021-09-02 11:44:42 +00001059 config := android.TestConfig(buildDir, nil, testCase.blueprint, fs)
Jingwen Chen49109762021-05-25 05:16:48 +00001060 ctx := android.NewTestContext(config)
1061 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Jingwen Chen49109762021-05-25 05:16:48 +00001062 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1063 ctx.RegisterForBazelConversion()
Liz Kammerba3ea162021-02-17 13:22:03 -05001064
Jingwen Chen49109762021-05-25 05:16:48 +00001065 _, errs := ctx.ParseFileList(dir, toParse)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001066 if errored(t, testCase, errs) {
Jingwen Chen49109762021-05-25 05:16:48 +00001067 return
1068 }
1069 _, errs = ctx.ResolveDependencies(config)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001070 if errored(t, testCase, errs) {
Jingwen Chen49109762021-05-25 05:16:48 +00001071 return
1072 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001073
Jingwen Chen49109762021-05-25 05:16:48 +00001074 checkDir := dir
1075 if testCase.dir != "" {
1076 checkDir = testCase.dir
1077 }
Liz Kammer6eff3232021-08-26 08:37:59 -04001078 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
1079 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, checkDir)
1080 android.FailIfErrored(t, err)
Jingwen Chen49109762021-05-25 05:16:48 +00001081 bazelTargets.sort()
1082 actualCount := len(bazelTargets)
1083 expectedCount := len(testCase.expectedBazelTargets)
1084 if actualCount != expectedCount {
1085 t.Errorf("Expected %d bazel target, got %d\n%s", expectedCount, actualCount, bazelTargets)
1086 }
1087 if !strings.Contains(bazelTargets.String(), "Section: Handcrafted targets. ") {
1088 t.Errorf("Expected string representation of bazelTargets to contain handcrafted section header.")
1089 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001090 for i, target := range bazelTargets {
Jingwen Chen49109762021-05-25 05:16:48 +00001091 actualContent := target.content
1092 expectedContent := testCase.expectedBazelTargets[i]
1093 if expectedContent != actualContent {
Liz Kammerba3ea162021-02-17 13:22:03 -05001094 t.Errorf(
Jingwen Chen49109762021-05-25 05:16:48 +00001095 "Expected generated Bazel target to be '%s', got '%s'",
1096 expectedContent,
1097 actualContent,
Liz Kammerba3ea162021-02-17 13:22:03 -05001098 )
1099 }
1100 }
Jingwen Chen49109762021-05-25 05:16:48 +00001101 })
Liz Kammerba3ea162021-02-17 13:22:03 -05001102 }
1103}
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001104
1105func TestGlobExcludeSrcs(t *testing.T) {
Jingwen Chen5146ac02021-09-02 11:44:42 +00001106 testCases := []bp2buildTestCase{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001107 {
1108 description: "filegroup top level exclude_srcs",
1109 moduleTypeUnderTest: "filegroup",
1110 moduleTypeUnderTestFactory: android.FileGroupFactory,
1111 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +00001112 blueprint: `filegroup {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001113 name: "fg_foo",
1114 srcs: ["**/*.txt"],
1115 exclude_srcs: ["c.txt"],
1116 bazel_module: { bp2build_available: true },
1117}`,
1118 expectedBazelTargets: []string{`filegroup(
1119 name = "fg_foo",
1120 srcs = [
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001121 "a.txt",
1122 "b.txt",
Liz Kammer9abd62d2021-05-21 08:37:59 -04001123 "//dir:e.txt",
1124 "//dir:f.txt",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001125 ],
1126)`,
1127 },
Jingwen Chen5146ac02021-09-02 11:44:42 +00001128 filesystem: map[string]string{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001129 "a.txt": "",
1130 "b.txt": "",
1131 "c.txt": "",
1132 "dir/Android.bp": "",
1133 "dir/e.txt": "",
1134 "dir/f.txt": "",
1135 },
1136 },
1137 {
1138 description: "filegroup in subdir exclude_srcs",
1139 moduleTypeUnderTest: "filegroup",
1140 moduleTypeUnderTestFactory: android.FileGroupFactory,
1141 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +00001142 blueprint: "",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001143 dir: "dir",
Jingwen Chen5146ac02021-09-02 11:44:42 +00001144 filesystem: map[string]string{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001145 "dir/Android.bp": `filegroup {
1146 name: "fg_foo",
1147 srcs: ["**/*.txt"],
1148 exclude_srcs: ["b.txt"],
1149 bazel_module: { bp2build_available: true },
1150}
1151`,
1152 "dir/a.txt": "",
1153 "dir/b.txt": "",
1154 "dir/subdir/Android.bp": "",
1155 "dir/subdir/e.txt": "",
1156 "dir/subdir/f.txt": "",
1157 },
1158 expectedBazelTargets: []string{`filegroup(
1159 name = "fg_foo",
1160 srcs = [
Liz Kammer9abd62d2021-05-21 08:37:59 -04001161 "a.txt",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001162 "//dir/subdir:e.txt",
1163 "//dir/subdir:f.txt",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001164 ],
1165)`,
1166 },
1167 },
1168 }
1169
1170 dir := "."
1171 for _, testCase := range testCases {
1172 fs := make(map[string][]byte)
1173 toParse := []string{
1174 "Android.bp",
1175 }
Jingwen Chen5146ac02021-09-02 11:44:42 +00001176 for f, content := range testCase.filesystem {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001177 if strings.HasSuffix(f, "Android.bp") {
1178 toParse = append(toParse, f)
1179 }
1180 fs[f] = []byte(content)
1181 }
Jingwen Chen5146ac02021-09-02 11:44:42 +00001182 config := android.TestConfig(buildDir, nil, testCase.blueprint, fs)
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001183 ctx := android.NewTestContext(config)
1184 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
1185 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1186 ctx.RegisterForBazelConversion()
1187
1188 _, errs := ctx.ParseFileList(dir, toParse)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001189 if errored(t, testCase, errs) {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001190 continue
1191 }
1192 _, errs = ctx.ResolveDependencies(config)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001193 if errored(t, testCase, errs) {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001194 continue
1195 }
1196
1197 checkDir := dir
1198 if testCase.dir != "" {
1199 checkDir = testCase.dir
1200 }
Liz Kammer6eff3232021-08-26 08:37:59 -04001201 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
1202 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, checkDir)
1203 android.FailIfErrored(t, err)
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001204 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
1205 t.Errorf("%s: Expected %d bazel target, got %d\n%s", testCase.description, expectedCount, actualCount, bazelTargets)
1206 } else {
1207 for i, target := range bazelTargets {
1208 if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
1209 t.Errorf(
1210 "%s: Expected generated Bazel target to be '%s', got '%s'",
1211 testCase.description,
1212 w,
1213 g,
1214 )
1215 }
1216 }
1217 }
1218 }
1219}
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001220
1221func TestCommonBp2BuildModuleAttrs(t *testing.T) {
1222 testCases := []bp2buildTestCase{
1223 {
1224 description: "Required into data test",
1225 moduleTypeUnderTest: "filegroup",
1226 moduleTypeUnderTestFactory: android.FileGroupFactory,
1227 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1228 blueprint: `filegroup {
1229 name: "reqd",
1230}
1231
1232filegroup {
1233 name: "fg_foo",
1234 required: ["reqd"],
1235 bazel_module: { bp2build_available: true },
1236}`,
1237 expectedBazelTargets: []string{`filegroup(
1238 name = "fg_foo",
1239 data = [":reqd"],
1240)`,
1241 `filegroup(
1242 name = "reqd",
1243)`,
1244 },
1245 },
1246 {
1247 description: "Required via arch into data test",
1248 moduleTypeUnderTest: "python_library",
1249 moduleTypeUnderTestFactory: python.PythonLibraryFactory,
1250 moduleTypeUnderTestBp2BuildMutator: python.PythonLibraryBp2Build,
1251 blueprint: `python_library {
1252 name: "reqdx86",
1253 bazel_module: { bp2build_available: false, },
1254}
1255
1256python_library {
1257 name: "reqdarm",
1258 bazel_module: { bp2build_available: false, },
1259}
1260
1261python_library {
1262 name: "fg_foo",
1263 arch: {
1264 arm: {
1265 required: ["reqdarm"],
1266 },
1267 x86: {
1268 required: ["reqdx86"],
1269 },
1270 },
1271 bazel_module: { bp2build_available: true },
1272}`,
1273 expectedBazelTargets: []string{`py_library(
1274 name = "fg_foo",
1275 data = select({
1276 "//build/bazel/platforms/arch:arm": [":reqdarm"],
1277 "//build/bazel/platforms/arch:x86": [":reqdx86"],
1278 "//conditions:default": [],
1279 }),
1280 srcs_version = "PY3",
1281)`,
1282 },
1283 },
1284 {
1285 description: "Required appended to data test",
1286 moduleTypeUnderTest: "python_library",
1287 moduleTypeUnderTestFactory: python.PythonLibraryFactory,
1288 moduleTypeUnderTestBp2BuildMutator: python.PythonLibraryBp2Build,
1289 blueprint: `python_library {
1290 name: "reqd",
1291 srcs: ["src.py"],
1292}
1293
1294python_library {
1295 name: "fg_foo",
1296 data: ["data.bin"],
1297 required: ["reqd"],
1298 bazel_module: { bp2build_available: true },
1299}`,
1300 expectedBazelTargets: []string{
1301 `py_library(
1302 name = "fg_foo",
1303 data = [
1304 "data.bin",
1305 ":reqd",
1306 ],
1307 srcs_version = "PY3",
1308)`,
1309 `py_library(
1310 name = "reqd",
1311 srcs = ["src.py"],
1312 srcs_version = "PY3",
1313)`,
1314 },
1315 filesystem: map[string]string{
1316 "data.bin": "",
1317 "src.py": "",
1318 },
1319 },
1320 {
1321 description: "All props-to-attrs at once together test",
1322 moduleTypeUnderTest: "filegroup",
1323 moduleTypeUnderTestFactory: android.FileGroupFactory,
1324 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1325 blueprint: `filegroup {
1326 name: "reqd"
1327}
1328filegroup {
1329 name: "fg_foo",
1330 required: ["reqd"],
1331 bazel_module: { bp2build_available: true },
1332}`,
1333 expectedBazelTargets: []string{
1334 `filegroup(
1335 name = "fg_foo",
1336 data = [":reqd"],
1337)`,
1338 `filegroup(
1339 name = "reqd",
1340)`,
1341 },
1342 filesystem: map[string]string{},
1343 },
1344 }
1345
1346 for _, test := range testCases {
1347 runBp2BuildTestCaseSimple(t, test)
1348 }
1349}