blob: 983604b93c50d014492f0245badc6499bd47026a [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 {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400226 description: "string props",
Jingwen Chen5146ac02021-09-02 11:44:42 +0000227 blueprint: `custom {
Jingwen Chen73850672020-12-14 08:25:34 -0500228 name: "foo",
229 string_list_prop: ["a", "b"],
230 string_prop: "a",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500231 bazel_module: { bp2build_available: true },
Jingwen Chen73850672020-12-14 08:25:34 -0500232}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500233 expectedBazelTargets: []string{
234 makeBazelTarget("custom", "foo", attrNameToString{
235 "string_list_prop": `[
Jingwen Chen73850672020-12-14 08:25:34 -0500236 "a",
237 "b",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500238 ]`,
239 "string_prop": `"a"`,
240 }),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400241 },
Jingwen Chen73850672020-12-14 08:25:34 -0500242 },
Jingwen Chen58a12b82021-03-30 13:08:36 +0000243 {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400244 description: "control characters",
Jingwen Chen5146ac02021-09-02 11:44:42 +0000245 blueprint: `custom {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500246 name: "foo",
Jingwen Chen58a12b82021-03-30 13:08:36 +0000247 string_list_prop: ["\t", "\n"],
248 string_prop: "a\t\n\r",
249 bazel_module: { bp2build_available: true },
250}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500251 expectedBazelTargets: []string{
252 makeBazelTarget("custom", "foo", attrNameToString{
253 "string_list_prop": `[
Jingwen Chen58a12b82021-03-30 13:08:36 +0000254 "\t",
255 "\n",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500256 ]`,
257 "string_prop": `"a\t\n\r"`,
258 }),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400259 },
260 },
261 {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400262 description: "handles dep",
Jingwen Chen5146ac02021-09-02 11:44:42 +0000263 blueprint: `custom {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400264 name: "has_dep",
265 arch_paths: [":dep"],
266 bazel_module: { bp2build_available: true },
267}
268
269custom {
270 name: "dep",
271 arch_paths: ["abc"],
272 bazel_module: { bp2build_available: true },
273}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500274 expectedBazelTargets: []string{
275 makeBazelTarget("custom", "dep", attrNameToString{
276 "arch_paths": `["abc"]`,
277 }),
278 makeBazelTarget("custom", "has_dep", attrNameToString{
279 "arch_paths": `[":dep"]`,
280 }),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400281 },
282 },
283 {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400284 description: "arch-variant srcs",
Jingwen Chen5146ac02021-09-02 11:44:42 +0000285 blueprint: `custom {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400286 name: "arch_paths",
287 arch: {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400288 x86: { arch_paths: ["x86.txt"] },
289 x86_64: { arch_paths: ["x86_64.txt"] },
290 arm: { arch_paths: ["arm.txt"] },
291 arm64: { arch_paths: ["arm64.txt"] },
292 },
293 target: {
294 linux: { arch_paths: ["linux.txt"] },
295 bionic: { arch_paths: ["bionic.txt"] },
296 host: { arch_paths: ["host.txt"] },
297 not_windows: { arch_paths: ["not_windows.txt"] },
298 android: { arch_paths: ["android.txt"] },
299 linux_musl: { arch_paths: ["linux_musl.txt"] },
300 musl: { arch_paths: ["musl.txt"] },
301 linux_glibc: { arch_paths: ["linux_glibc.txt"] },
302 glibc: { arch_paths: ["glibc.txt"] },
303 linux_bionic: { arch_paths: ["linux_bionic.txt"] },
304 darwin: { arch_paths: ["darwin.txt"] },
305 windows: { arch_paths: ["windows.txt"] },
306 },
307 multilib: {
308 lib32: { arch_paths: ["lib32.txt"] },
309 lib64: { arch_paths: ["lib64.txt"] },
Liz Kammer4562a3b2021-04-21 18:15:34 -0400310 },
311 bazel_module: { bp2build_available: true },
312}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500313 expectedBazelTargets: []string{
314 makeBazelTarget("custom", "arch_paths", attrNameToString{
315 "arch_paths": `select({
Liz Kammerfdd72e62021-10-11 15:41:03 -0400316 "//build/bazel/platforms/arch:arm": [
317 "arm.txt",
318 "lib32.txt",
319 ],
320 "//build/bazel/platforms/arch:arm64": [
321 "arm64.txt",
322 "lib64.txt",
323 ],
324 "//build/bazel/platforms/arch:x86": [
325 "x86.txt",
326 "lib32.txt",
327 ],
328 "//build/bazel/platforms/arch:x86_64": [
329 "x86_64.txt",
330 "lib64.txt",
331 ],
332 "//conditions:default": [],
333 }) + select({
334 "//build/bazel/platforms/os:android": [
335 "linux.txt",
336 "bionic.txt",
337 "android.txt",
338 ],
339 "//build/bazel/platforms/os:darwin": [
340 "host.txt",
341 "darwin.txt",
342 "not_windows.txt",
343 ],
344 "//build/bazel/platforms/os:linux": [
345 "host.txt",
346 "linux.txt",
347 "glibc.txt",
348 "linux_glibc.txt",
349 "not_windows.txt",
350 ],
351 "//build/bazel/platforms/os:linux_bionic": [
352 "host.txt",
353 "linux.txt",
354 "bionic.txt",
355 "linux_bionic.txt",
356 "not_windows.txt",
357 ],
358 "//build/bazel/platforms/os:linux_musl": [
359 "host.txt",
360 "linux.txt",
361 "musl.txt",
362 "linux_musl.txt",
363 "not_windows.txt",
364 ],
365 "//build/bazel/platforms/os:windows": [
366 "host.txt",
367 "windows.txt",
368 ],
Liz Kammer4562a3b2021-04-21 18:15:34 -0400369 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500370 })`,
371 }),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400372 },
373 },
374 {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400375 description: "arch-variant deps",
Jingwen Chen5146ac02021-09-02 11:44:42 +0000376 blueprint: `custom {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400377 name: "has_dep",
378 arch: {
379 x86: {
380 arch_paths: [":dep"],
381 },
382 },
383 bazel_module: { bp2build_available: true },
384}
385
386custom {
387 name: "dep",
388 arch_paths: ["abc"],
389 bazel_module: { bp2build_available: true },
390}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500391 expectedBazelTargets: []string{
392 makeBazelTarget("custom", "dep", attrNameToString{
393 "arch_paths": `["abc"]`,
394 }),
395 makeBazelTarget("custom", "has_dep", attrNameToString{
396 "arch_paths": `select({
Liz Kammer4562a3b2021-04-21 18:15:34 -0400397 "//build/bazel/platforms/arch:x86": [":dep"],
398 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500399 })`,
400 }),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400401 },
Jingwen Chen58a12b82021-03-30 13:08:36 +0000402 },
Liz Kammer32a03392021-09-14 11:17:21 -0400403 {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400404 description: "embedded props",
Liz Kammer32a03392021-09-14 11:17:21 -0400405 blueprint: `custom {
406 name: "embedded_props",
407 embedded_prop: "abc",
408 bazel_module: { bp2build_available: true },
409}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500410 expectedBazelTargets: []string{
411 makeBazelTarget("custom", "embedded_props", attrNameToString{
412 "embedded_attr": `"abc"`,
413 }),
Liz Kammer32a03392021-09-14 11:17:21 -0400414 },
415 },
416 {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400417 description: "ptr to embedded props",
Liz Kammer32a03392021-09-14 11:17:21 -0400418 blueprint: `custom {
419 name: "ptr_to_embedded_props",
420 other_embedded_prop: "abc",
421 bazel_module: { bp2build_available: true },
422}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500423 expectedBazelTargets: []string{
424 makeBazelTarget("custom", "ptr_to_embedded_props", attrNameToString{
425 "other_embedded_attr": `"abc"`,
426 }),
Liz Kammer32a03392021-09-14 11:17:21 -0400427 },
428 },
Jingwen Chen73850672020-12-14 08:25:34 -0500429 }
430
431 dir := "."
432 for _, testCase := range testCases {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400433 t.Run(testCase.description, func(t *testing.T) {
434 config := android.TestConfig(buildDir, nil, testCase.blueprint, nil)
435 ctx := android.NewTestContext(config)
Jingwen Chen164e0862021-02-19 00:48:40 -0500436
Liz Kammerfdd72e62021-10-11 15:41:03 -0400437 registerCustomModuleForBp2buildConversion(ctx)
Jingwen Chen73850672020-12-14 08:25:34 -0500438
Liz Kammerfdd72e62021-10-11 15:41:03 -0400439 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
440 if errored(t, testCase, errs) {
441 return
442 }
443 _, errs = ctx.ResolveDependencies(config)
444 if errored(t, testCase, errs) {
445 return
446 }
Jingwen Chen73850672020-12-14 08:25:34 -0500447
Liz Kammerfdd72e62021-10-11 15:41:03 -0400448 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
449 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
450 android.FailIfErrored(t, err)
Jingwen Chen164e0862021-02-19 00:48:40 -0500451
Liz Kammerfdd72e62021-10-11 15:41:03 -0400452 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
453 t.Errorf("Expected %d bazel target, got %d", expectedCount, actualCount)
454 } else {
455 for i, expectedBazelTarget := range testCase.expectedBazelTargets {
456 actualBazelTarget := bazelTargets[i]
457 if actualBazelTarget.content != expectedBazelTarget {
458 t.Errorf(
459 "Expected generated Bazel target to be '%s', got '%s'",
460 expectedBazelTarget,
461 actualBazelTarget.content,
462 )
463 }
Liz Kammer4562a3b2021-04-21 18:15:34 -0400464 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500465 }
Liz Kammerfdd72e62021-10-11 15:41:03 -0400466 })
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800467 }
468}
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500469
Jingwen Chen40067de2021-01-26 21:58:43 -0500470func TestLoadStatements(t *testing.T) {
471 testCases := []struct {
472 bazelTargets BazelTargets
473 expectedLoadStatements string
474 }{
475 {
476 bazelTargets: BazelTargets{
477 BazelTarget{
478 name: "foo",
479 ruleClass: "cc_library",
480 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
481 },
482 },
483 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
484 },
485 {
486 bazelTargets: BazelTargets{
487 BazelTarget{
488 name: "foo",
489 ruleClass: "cc_library",
490 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
491 },
492 BazelTarget{
493 name: "bar",
494 ruleClass: "cc_library",
495 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
496 },
497 },
498 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
499 },
500 {
501 bazelTargets: BazelTargets{
502 BazelTarget{
503 name: "foo",
504 ruleClass: "cc_library",
505 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
506 },
507 BazelTarget{
508 name: "bar",
509 ruleClass: "cc_binary",
510 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
511 },
512 },
513 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")`,
514 },
515 {
516 bazelTargets: BazelTargets{
517 BazelTarget{
518 name: "foo",
519 ruleClass: "cc_library",
520 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
521 },
522 BazelTarget{
523 name: "bar",
524 ruleClass: "cc_binary",
525 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
526 },
527 BazelTarget{
528 name: "baz",
529 ruleClass: "java_binary",
530 bzlLoadLocation: "//build/bazel/rules:java.bzl",
531 },
532 },
533 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")
534load("//build/bazel/rules:java.bzl", "java_binary")`,
535 },
536 {
537 bazelTargets: BazelTargets{
538 BazelTarget{
539 name: "foo",
540 ruleClass: "cc_binary",
541 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
542 },
543 BazelTarget{
544 name: "bar",
545 ruleClass: "java_binary",
546 bzlLoadLocation: "//build/bazel/rules:java.bzl",
547 },
548 BazelTarget{
549 name: "baz",
550 ruleClass: "genrule",
551 // Note: no bzlLoadLocation for native rules
552 },
553 },
554 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary")
555load("//build/bazel/rules:java.bzl", "java_binary")`,
556 },
557 }
558
559 for _, testCase := range testCases {
560 actual := testCase.bazelTargets.LoadStatements()
561 expected := testCase.expectedLoadStatements
562 if actual != expected {
563 t.Fatalf("Expected load statements to be %s, got %s", expected, actual)
564 }
565 }
566
567}
568
569func TestGenerateBazelTargetModules_OneToMany_LoadedFromStarlark(t *testing.T) {
570 testCases := []struct {
571 bp string
572 expectedBazelTarget string
573 expectedBazelTargetCount int
574 expectedLoadStatements string
575 }{
576 {
577 bp: `custom {
578 name: "bar",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500579 bazel_module: { bp2build_available: true },
Jingwen Chen40067de2021-01-26 21:58:43 -0500580}`,
581 expectedBazelTarget: `my_library(
582 name = "bar",
583)
584
Jingwen Chen40067de2021-01-26 21:58:43 -0500585proto_library(
586 name = "bar_proto_library_deps",
Liz Kammer2ada09a2021-08-11 00:17:36 -0400587)
588
589my_proto_library(
590 name = "bar_my_proto_library_deps",
Jingwen Chen40067de2021-01-26 21:58:43 -0500591)`,
592 expectedBazelTargetCount: 3,
593 expectedLoadStatements: `load("//build/bazel/rules:proto.bzl", "my_proto_library", "proto_library")
594load("//build/bazel/rules:rules.bzl", "my_library")`,
595 },
596 }
597
598 dir := "."
599 for _, testCase := range testCases {
600 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
601 ctx := android.NewTestContext(config)
602 ctx.RegisterModuleType("custom", customModuleFactory)
Jingwen Chen12b4c272021-03-10 02:05:59 -0500603 ctx.RegisterBp2BuildMutator("custom", customBp2BuildMutatorFromStarlark)
Jingwen Chen40067de2021-01-26 21:58:43 -0500604 ctx.RegisterForBazelConversion()
605
606 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
607 android.FailIfErrored(t, errs)
608 _, errs = ctx.ResolveDependencies(config)
609 android.FailIfErrored(t, errs)
610
Jingwen Chen164e0862021-02-19 00:48:40 -0500611 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Liz Kammer6eff3232021-08-26 08:37:59 -0400612 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
613 android.FailIfErrored(t, err)
Jingwen Chen40067de2021-01-26 21:58:43 -0500614 if actualCount := len(bazelTargets); actualCount != testCase.expectedBazelTargetCount {
615 t.Fatalf("Expected %d bazel target, got %d", testCase.expectedBazelTargetCount, actualCount)
616 }
617
618 actualBazelTargets := bazelTargets.String()
619 if actualBazelTargets != testCase.expectedBazelTarget {
620 t.Errorf(
621 "Expected generated Bazel target to be '%s', got '%s'",
622 testCase.expectedBazelTarget,
623 actualBazelTargets,
624 )
625 }
626
627 actualLoadStatements := bazelTargets.LoadStatements()
628 if actualLoadStatements != testCase.expectedLoadStatements {
629 t.Errorf(
630 "Expected generated load statements to be '%s', got '%s'",
631 testCase.expectedLoadStatements,
632 actualLoadStatements,
633 )
634 }
635 }
636}
637
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500638func TestModuleTypeBp2Build(t *testing.T) {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000639 testCases := []bp2buildTestCase{
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500640 {
Liz Kammerebfcf672021-02-16 15:00:05 -0500641 description: "filegroup with does not specify srcs",
642 moduleTypeUnderTest: "filegroup",
643 moduleTypeUnderTestFactory: android.FileGroupFactory,
644 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000645 blueprint: `filegroup {
Liz Kammerebfcf672021-02-16 15:00:05 -0500646 name: "fg_foo",
647 bazel_module: { bp2build_available: true },
648}`,
649 expectedBazelTargets: []string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500650 makeBazelTarget("filegroup", "fg_foo", map[string]string{}),
Liz Kammerebfcf672021-02-16 15:00:05 -0500651 },
652 },
653 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500654 description: "filegroup with no srcs",
655 moduleTypeUnderTest: "filegroup",
656 moduleTypeUnderTestFactory: android.FileGroupFactory,
657 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000658 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500659 name: "fg_foo",
660 srcs: [],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500661 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500662}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500663 expectedBazelTargets: []string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500664 makeBazelTarget("filegroup", "fg_foo", map[string]string{}),
Liz Kammer356f7d42021-01-26 09:18:53 -0500665 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500666 },
667 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500668 description: "filegroup with srcs",
669 moduleTypeUnderTest: "filegroup",
670 moduleTypeUnderTestFactory: android.FileGroupFactory,
671 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000672 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500673 name: "fg_foo",
674 srcs: ["a", "b"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500675 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500676}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500677 expectedBazelTargets: []string{
678 makeBazelTarget("filegroup", "fg_foo", map[string]string{
679 "srcs": `[
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500680 "a",
681 "b",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500682 ]`,
683 }),
Liz Kammer356f7d42021-01-26 09:18:53 -0500684 },
685 },
686 {
687 description: "filegroup with excludes srcs",
688 moduleTypeUnderTest: "filegroup",
689 moduleTypeUnderTestFactory: android.FileGroupFactory,
690 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000691 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500692 name: "fg_foo",
693 srcs: ["a", "b"],
694 exclude_srcs: ["a"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500695 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500696}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500697 expectedBazelTargets: []string{
698 makeBazelTarget("filegroup", "fg_foo", map[string]string{
699 "srcs": `["b"]`,
700 }),
Liz Kammer356f7d42021-01-26 09:18:53 -0500701 },
702 },
703 {
704 description: "filegroup with glob",
705 moduleTypeUnderTest: "filegroup",
706 moduleTypeUnderTestFactory: android.FileGroupFactory,
707 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000708 blueprint: `filegroup {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500709 name: "fg_foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500710 srcs: ["**/*.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500711 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500712}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500713 expectedBazelTargets: []string{
714 makeBazelTarget("filegroup", "fg_foo", map[string]string{
715 "srcs": `[
Liz Kammer356f7d42021-01-26 09:18:53 -0500716 "other/a.txt",
717 "other/b.txt",
718 "other/subdir/a.txt",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500719 ]`,
720 }),
Liz Kammer356f7d42021-01-26 09:18:53 -0500721 },
Jingwen Chen5146ac02021-09-02 11:44:42 +0000722 filesystem: map[string]string{
Liz Kammer356f7d42021-01-26 09:18:53 -0500723 "other/a.txt": "",
724 "other/b.txt": "",
725 "other/subdir/a.txt": "",
726 "other/file": "",
727 },
728 },
729 {
730 description: "filegroup with glob in subdir",
731 moduleTypeUnderTest: "filegroup",
732 moduleTypeUnderTestFactory: android.FileGroupFactory,
733 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500734 blueprint: ``,
735 dir: "other",
Jingwen Chen5146ac02021-09-02 11:44:42 +0000736 filesystem: map[string]string{
Liz Kammer356f7d42021-01-26 09:18:53 -0500737 "other/Android.bp": `filegroup {
738 name: "fg_foo",
739 srcs: ["**/*.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500740 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500741}`,
742 "other/a.txt": "",
743 "other/b.txt": "",
744 "other/subdir/a.txt": "",
745 "other/file": "",
746 },
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500747 expectedBazelTargets: []string{
748 makeBazelTarget("filegroup", "fg_foo", map[string]string{
749 "srcs": `[
750 "a.txt",
751 "b.txt",
752 "subdir/a.txt",
753 ]`,
754 }),
755 },
Liz Kammer356f7d42021-01-26 09:18:53 -0500756 },
757 {
758 description: "depends_on_other_dir_module",
759 moduleTypeUnderTest: "filegroup",
760 moduleTypeUnderTestFactory: android.FileGroupFactory,
761 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000762 blueprint: `filegroup {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500763 name: "fg_foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500764 srcs: [
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000765 ":foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500766 "c",
767 ],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500768 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500769}`,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000770 filesystem: map[string]string{
Liz Kammer356f7d42021-01-26 09:18:53 -0500771 "other/Android.bp": `filegroup {
772 name: "foo",
773 srcs: ["a", "b"],
Liz Kammer6eff3232021-08-26 08:37:59 -0400774 bazel_module: { bp2build_available: true },
775}`,
776 },
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500777 expectedBazelTargets: []string{
778 makeBazelTarget("filegroup", "fg_foo", map[string]string{
779 "srcs": `[
780 "//other:foo",
781 "c",
782 ]`,
783 }),
784 },
Liz Kammer6eff3232021-08-26 08:37:59 -0400785 },
786 {
787 description: "depends_on_other_unconverted_module_error",
788 moduleTypeUnderTest: "filegroup",
789 moduleTypeUnderTestFactory: android.FileGroupFactory,
790 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
791 unconvertedDepsMode: errorModulesUnconvertedDeps,
Liz Kammer6eff3232021-08-26 08:37:59 -0400792 filesystem: map[string]string{
793 "other/Android.bp": `filegroup {
794 name: "foo",
795 srcs: ["a", "b"],
Liz Kammer356f7d42021-01-26 09:18:53 -0500796}`,
797 },
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500798 blueprint: `filegroup {
799 name: "fg_foo",
800 srcs: [
801 ":foo",
802 "c",
803 ],
804 bazel_module: { bp2build_available: true },
805}`,
806 expectedErr: fmt.Errorf(`"fg_foo" depends on unconverted modules: foo`),
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500807 },
808 }
809
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500810 for _, testCase := range testCases {
Liz Kammer6eff3232021-08-26 08:37:59 -0400811 t.Run(testCase.description, func(t *testing.T) {
812 runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, testCase)
813 })
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500814 }
815}
Jingwen Chen041b1842021-02-01 00:23:25 -0500816
817type bp2buildMutator = func(android.TopDownMutatorContext)
818
Jingwen Chen12b4c272021-03-10 02:05:59 -0500819func TestAllowlistingBp2buildTargetsExplicitly(t *testing.T) {
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500820 testCases := []struct {
821 moduleTypeUnderTest string
822 moduleTypeUnderTestFactory android.ModuleFactory
823 moduleTypeUnderTestBp2BuildMutator bp2buildMutator
824 bp string
825 expectedCount int
826 description string
827 }{
828 {
829 description: "explicitly unavailable",
830 moduleTypeUnderTest: "filegroup",
831 moduleTypeUnderTestFactory: android.FileGroupFactory,
832 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
833 bp: `filegroup {
834 name: "foo",
835 srcs: ["a", "b"],
836 bazel_module: { bp2build_available: false },
837}`,
838 expectedCount: 0,
839 },
840 {
841 description: "implicitly unavailable",
842 moduleTypeUnderTest: "filegroup",
843 moduleTypeUnderTestFactory: android.FileGroupFactory,
844 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
845 bp: `filegroup {
846 name: "foo",
847 srcs: ["a", "b"],
848}`,
849 expectedCount: 0,
850 },
851 {
852 description: "explicitly available",
853 moduleTypeUnderTest: "filegroup",
854 moduleTypeUnderTestFactory: android.FileGroupFactory,
855 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
856 bp: `filegroup {
857 name: "foo",
858 srcs: ["a", "b"],
859 bazel_module: { bp2build_available: true },
860}`,
861 expectedCount: 1,
862 },
863 {
864 description: "generates more than 1 target if needed",
865 moduleTypeUnderTest: "custom",
866 moduleTypeUnderTestFactory: customModuleFactory,
867 moduleTypeUnderTestBp2BuildMutator: customBp2BuildMutatorFromStarlark,
868 bp: `custom {
869 name: "foo",
870 bazel_module: { bp2build_available: true },
871}`,
872 expectedCount: 3,
873 },
874 }
875
876 dir := "."
877 for _, testCase := range testCases {
Liz Kammer2ada09a2021-08-11 00:17:36 -0400878 t.Run(testCase.description, func(t *testing.T) {
879 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
880 ctx := android.NewTestContext(config)
881 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
882 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
883 ctx.RegisterForBazelConversion()
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500884
Liz Kammer2ada09a2021-08-11 00:17:36 -0400885 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
886 android.FailIfErrored(t, errs)
887 _, errs = ctx.ResolveDependencies(config)
888 android.FailIfErrored(t, errs)
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500889
Liz Kammer2ada09a2021-08-11 00:17:36 -0400890 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Liz Kammer6eff3232021-08-26 08:37:59 -0400891 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
892 android.FailIfErrored(t, err)
Liz Kammer2ada09a2021-08-11 00:17:36 -0400893 if actualCount := len(bazelTargets); actualCount != testCase.expectedCount {
894 t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, testCase.expectedCount, actualCount)
895 }
896 })
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500897 }
898}
Liz Kammerba3ea162021-02-17 13:22:03 -0500899
Jingwen Chen12b4c272021-03-10 02:05:59 -0500900func TestAllowlistingBp2buildTargetsWithConfig(t *testing.T) {
901 testCases := []struct {
902 moduleTypeUnderTest string
903 moduleTypeUnderTestFactory android.ModuleFactory
904 moduleTypeUnderTestBp2BuildMutator bp2buildMutator
905 expectedCount map[string]int
906 description string
907 bp2buildConfig android.Bp2BuildConfig
908 checkDir string
909 fs map[string]string
910 }{
911 {
912 description: "test bp2build config package and subpackages config",
913 moduleTypeUnderTest: "filegroup",
914 moduleTypeUnderTestFactory: android.FileGroupFactory,
915 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
916 expectedCount: map[string]int{
917 "migrated": 1,
918 "migrated/but_not_really": 0,
919 "migrated/but_not_really/but_really": 1,
920 "not_migrated": 0,
921 "also_not_migrated": 0,
922 },
923 bp2buildConfig: android.Bp2BuildConfig{
924 "migrated": android.Bp2BuildDefaultTrueRecursively,
925 "migrated/but_not_really": android.Bp2BuildDefaultFalse,
926 "not_migrated": android.Bp2BuildDefaultFalse,
927 },
928 fs: map[string]string{
929 "migrated/Android.bp": `filegroup { name: "a" }`,
930 "migrated/but_not_really/Android.bp": `filegroup { name: "b" }`,
931 "migrated/but_not_really/but_really/Android.bp": `filegroup { name: "c" }`,
932 "not_migrated/Android.bp": `filegroup { name: "d" }`,
933 "also_not_migrated/Android.bp": `filegroup { name: "e" }`,
934 },
935 },
936 {
937 description: "test bp2build config opt-in and opt-out",
938 moduleTypeUnderTest: "filegroup",
939 moduleTypeUnderTestFactory: android.FileGroupFactory,
940 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
941 expectedCount: map[string]int{
942 "package-opt-in": 2,
943 "package-opt-in/subpackage": 0,
944 "package-opt-out": 1,
945 "package-opt-out/subpackage": 0,
946 },
947 bp2buildConfig: android.Bp2BuildConfig{
948 "package-opt-in": android.Bp2BuildDefaultFalse,
949 "package-opt-out": android.Bp2BuildDefaultTrueRecursively,
950 },
951 fs: map[string]string{
952 "package-opt-in/Android.bp": `
953filegroup { name: "opt-in-a" }
954filegroup { name: "opt-in-b", bazel_module: { bp2build_available: true } }
955filegroup { name: "opt-in-c", bazel_module: { bp2build_available: true } }
956`,
957
958 "package-opt-in/subpackage/Android.bp": `
959filegroup { name: "opt-in-d" } // parent package not configured to DefaultTrueRecursively
960`,
961
962 "package-opt-out/Android.bp": `
963filegroup { name: "opt-out-a" }
964filegroup { name: "opt-out-b", bazel_module: { bp2build_available: false } }
965filegroup { name: "opt-out-c", bazel_module: { bp2build_available: false } }
966`,
967
968 "package-opt-out/subpackage/Android.bp": `
969filegroup { name: "opt-out-g", bazel_module: { bp2build_available: false } }
970filegroup { name: "opt-out-h", bazel_module: { bp2build_available: false } }
971`,
972 },
973 },
974 }
975
976 dir := "."
977 for _, testCase := range testCases {
978 fs := make(map[string][]byte)
979 toParse := []string{
980 "Android.bp",
981 }
982 for f, content := range testCase.fs {
983 if strings.HasSuffix(f, "Android.bp") {
984 toParse = append(toParse, f)
985 }
986 fs[f] = []byte(content)
987 }
988 config := android.TestConfig(buildDir, nil, "", fs)
989 ctx := android.NewTestContext(config)
990 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
991 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
992 ctx.RegisterBp2BuildConfig(testCase.bp2buildConfig)
993 ctx.RegisterForBazelConversion()
994
995 _, errs := ctx.ParseFileList(dir, toParse)
996 android.FailIfErrored(t, errs)
997 _, errs = ctx.ResolveDependencies(config)
998 android.FailIfErrored(t, errs)
999
1000 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
1001
1002 // For each directory, test that the expected number of generated targets is correct.
1003 for dir, expectedCount := range testCase.expectedCount {
Liz Kammer6eff3232021-08-26 08:37:59 -04001004 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
1005 android.FailIfErrored(t, err)
Jingwen Chen12b4c272021-03-10 02:05:59 -05001006 if actualCount := len(bazelTargets); actualCount != expectedCount {
1007 t.Fatalf(
1008 "%s: Expected %d bazel target for %s package, got %d",
1009 testCase.description,
1010 expectedCount,
1011 dir,
1012 actualCount)
1013 }
1014
1015 }
1016 }
1017}
1018
Liz Kammerba3ea162021-02-17 13:22:03 -05001019func TestCombineBuildFilesBp2buildTargets(t *testing.T) {
Jingwen Chen5146ac02021-09-02 11:44:42 +00001020 testCases := []bp2buildTestCase{
Liz Kammerba3ea162021-02-17 13:22:03 -05001021 {
1022 description: "filegroup bazel_module.label",
1023 moduleTypeUnderTest: "filegroup",
1024 moduleTypeUnderTestFactory: android.FileGroupFactory,
1025 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +00001026 blueprint: `filegroup {
Liz Kammerba3ea162021-02-17 13:22:03 -05001027 name: "fg_foo",
1028 bazel_module: { label: "//other:fg_foo" },
1029}`,
1030 expectedBazelTargets: []string{
1031 `// BUILD file`,
1032 },
Jingwen Chen5146ac02021-09-02 11:44:42 +00001033 filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -05001034 "other/BUILD.bazel": `// BUILD file`,
1035 },
1036 },
1037 {
1038 description: "multiple bazel_module.label same BUILD",
1039 moduleTypeUnderTest: "filegroup",
1040 moduleTypeUnderTestFactory: android.FileGroupFactory,
1041 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +00001042 blueprint: `filegroup {
Jingwen Chenc63677b2021-06-17 05:43:19 +00001043 name: "fg_foo",
1044 bazel_module: { label: "//other:fg_foo" },
1045 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001046
Jingwen Chenc63677b2021-06-17 05:43:19 +00001047 filegroup {
1048 name: "foo",
1049 bazel_module: { label: "//other:foo" },
1050 }`,
Liz Kammerba3ea162021-02-17 13:22:03 -05001051 expectedBazelTargets: []string{
1052 `// BUILD file`,
1053 },
Jingwen Chen5146ac02021-09-02 11:44:42 +00001054 filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -05001055 "other/BUILD.bazel": `// BUILD file`,
1056 },
1057 },
1058 {
Jingwen Chenc63677b2021-06-17 05:43:19 +00001059 description: "filegroup bazel_module.label and bp2build in subdir",
Liz Kammerba3ea162021-02-17 13:22:03 -05001060 moduleTypeUnderTest: "filegroup",
1061 moduleTypeUnderTestFactory: android.FileGroupFactory,
1062 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chenc63677b2021-06-17 05:43:19 +00001063 dir: "other",
Jingwen Chen5146ac02021-09-02 11:44:42 +00001064 blueprint: ``,
1065 filesystem: map[string]string{
Jingwen Chenc63677b2021-06-17 05:43:19 +00001066 "other/Android.bp": `filegroup {
1067 name: "fg_foo",
1068 bazel_module: {
1069 bp2build_available: true,
1070 },
1071 }
1072 filegroup {
1073 name: "fg_bar",
1074 bazel_module: {
1075 label: "//other:fg_bar"
1076 },
1077 }`,
1078 "other/BUILD.bazel": `// definition for fg_bar`,
1079 },
Liz Kammerba3ea162021-02-17 13:22:03 -05001080 expectedBazelTargets: []string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001081 makeBazelTarget("filegroup", "fg_foo", map[string]string{}),
1082 `// definition for fg_bar`,
Liz Kammerba3ea162021-02-17 13:22:03 -05001083 },
1084 },
1085 {
1086 description: "filegroup bazel_module.label and filegroup bp2build",
1087 moduleTypeUnderTest: "filegroup",
1088 moduleTypeUnderTestFactory: android.FileGroupFactory,
1089 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001090 filesystem: map[string]string{
1091 "other/BUILD.bazel": `// BUILD file`,
1092 },
Jingwen Chen5146ac02021-09-02 11:44:42 +00001093 blueprint: `filegroup {
Jingwen Chenc63677b2021-06-17 05:43:19 +00001094 name: "fg_foo",
1095 bazel_module: {
1096 label: "//other:fg_foo",
1097 },
1098 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001099
Jingwen Chenc63677b2021-06-17 05:43:19 +00001100 filegroup {
1101 name: "fg_bar",
1102 bazel_module: {
1103 bp2build_available: true,
1104 },
1105 }`,
Liz Kammerba3ea162021-02-17 13:22:03 -05001106 expectedBazelTargets: []string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001107 makeBazelTarget("filegroup", "fg_bar", map[string]string{}),
Liz Kammerba3ea162021-02-17 13:22:03 -05001108 `// BUILD file`,
1109 },
Liz Kammerba3ea162021-02-17 13:22:03 -05001110 },
1111 }
1112
1113 dir := "."
1114 for _, testCase := range testCases {
Jingwen Chen49109762021-05-25 05:16:48 +00001115 t.Run(testCase.description, func(t *testing.T) {
1116 fs := make(map[string][]byte)
1117 toParse := []string{
1118 "Android.bp",
Liz Kammerba3ea162021-02-17 13:22:03 -05001119 }
Jingwen Chen5146ac02021-09-02 11:44:42 +00001120 for f, content := range testCase.filesystem {
Jingwen Chen49109762021-05-25 05:16:48 +00001121 if strings.HasSuffix(f, "Android.bp") {
1122 toParse = append(toParse, f)
1123 }
1124 fs[f] = []byte(content)
1125 }
Jingwen Chen5146ac02021-09-02 11:44:42 +00001126 config := android.TestConfig(buildDir, nil, testCase.blueprint, fs)
Jingwen Chen49109762021-05-25 05:16:48 +00001127 ctx := android.NewTestContext(config)
1128 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Jingwen Chen49109762021-05-25 05:16:48 +00001129 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1130 ctx.RegisterForBazelConversion()
Liz Kammerba3ea162021-02-17 13:22:03 -05001131
Jingwen Chen49109762021-05-25 05:16:48 +00001132 _, errs := ctx.ParseFileList(dir, toParse)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001133 if errored(t, testCase, errs) {
Jingwen Chen49109762021-05-25 05:16:48 +00001134 return
1135 }
1136 _, errs = ctx.ResolveDependencies(config)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001137 if errored(t, testCase, errs) {
Jingwen Chen49109762021-05-25 05:16:48 +00001138 return
1139 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001140
Jingwen Chen49109762021-05-25 05:16:48 +00001141 checkDir := dir
1142 if testCase.dir != "" {
1143 checkDir = testCase.dir
1144 }
Liz Kammer6eff3232021-08-26 08:37:59 -04001145 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
1146 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, checkDir)
1147 android.FailIfErrored(t, err)
Jingwen Chen49109762021-05-25 05:16:48 +00001148 bazelTargets.sort()
1149 actualCount := len(bazelTargets)
1150 expectedCount := len(testCase.expectedBazelTargets)
1151 if actualCount != expectedCount {
1152 t.Errorf("Expected %d bazel target, got %d\n%s", expectedCount, actualCount, bazelTargets)
1153 }
1154 if !strings.Contains(bazelTargets.String(), "Section: Handcrafted targets. ") {
1155 t.Errorf("Expected string representation of bazelTargets to contain handcrafted section header.")
1156 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001157 for i, target := range bazelTargets {
Jingwen Chen49109762021-05-25 05:16:48 +00001158 actualContent := target.content
1159 expectedContent := testCase.expectedBazelTargets[i]
1160 if expectedContent != actualContent {
Liz Kammerba3ea162021-02-17 13:22:03 -05001161 t.Errorf(
Jingwen Chen49109762021-05-25 05:16:48 +00001162 "Expected generated Bazel target to be '%s', got '%s'",
1163 expectedContent,
1164 actualContent,
Liz Kammerba3ea162021-02-17 13:22:03 -05001165 )
1166 }
1167 }
Jingwen Chen49109762021-05-25 05:16:48 +00001168 })
Liz Kammerba3ea162021-02-17 13:22:03 -05001169 }
1170}
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001171
1172func TestGlobExcludeSrcs(t *testing.T) {
Jingwen Chen5146ac02021-09-02 11:44:42 +00001173 testCases := []bp2buildTestCase{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001174 {
1175 description: "filegroup top level exclude_srcs",
1176 moduleTypeUnderTest: "filegroup",
1177 moduleTypeUnderTestFactory: android.FileGroupFactory,
1178 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +00001179 blueprint: `filegroup {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001180 name: "fg_foo",
1181 srcs: ["**/*.txt"],
1182 exclude_srcs: ["c.txt"],
1183 bazel_module: { bp2build_available: true },
1184}`,
Jingwen Chen5146ac02021-09-02 11:44:42 +00001185 filesystem: map[string]string{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001186 "a.txt": "",
1187 "b.txt": "",
1188 "c.txt": "",
1189 "dir/Android.bp": "",
1190 "dir/e.txt": "",
1191 "dir/f.txt": "",
1192 },
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001193 expectedBazelTargets: []string{
1194 makeBazelTarget("filegroup", "fg_foo", map[string]string{
1195 "srcs": `[
1196 "a.txt",
1197 "b.txt",
1198 "//dir:e.txt",
1199 "//dir:f.txt",
1200 ]`,
1201 }),
1202 },
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001203 },
1204 {
1205 description: "filegroup in subdir exclude_srcs",
1206 moduleTypeUnderTest: "filegroup",
1207 moduleTypeUnderTestFactory: android.FileGroupFactory,
1208 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +00001209 blueprint: "",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001210 dir: "dir",
Jingwen Chen5146ac02021-09-02 11:44:42 +00001211 filesystem: map[string]string{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001212 "dir/Android.bp": `filegroup {
1213 name: "fg_foo",
1214 srcs: ["**/*.txt"],
1215 exclude_srcs: ["b.txt"],
1216 bazel_module: { bp2build_available: true },
1217}
1218`,
1219 "dir/a.txt": "",
1220 "dir/b.txt": "",
1221 "dir/subdir/Android.bp": "",
1222 "dir/subdir/e.txt": "",
1223 "dir/subdir/f.txt": "",
1224 },
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001225 expectedBazelTargets: []string{
1226 makeBazelTarget("filegroup", "fg_foo", map[string]string{
1227 "srcs": `[
Liz Kammer9abd62d2021-05-21 08:37:59 -04001228 "a.txt",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001229 "//dir/subdir:e.txt",
1230 "//dir/subdir:f.txt",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001231 ]`,
1232 }),
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001233 },
1234 },
1235 }
1236
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001237 for _, testCase := range testCases {
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001238 t.Run(testCase.description, func(t *testing.T) {
1239 runBp2BuildTestCaseSimple(t, testCase)
1240 })
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001241 }
1242}
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001243
1244func TestCommonBp2BuildModuleAttrs(t *testing.T) {
1245 testCases := []bp2buildTestCase{
1246 {
1247 description: "Required into data test",
1248 moduleTypeUnderTest: "filegroup",
1249 moduleTypeUnderTestFactory: android.FileGroupFactory,
1250 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001251 blueprint: simpleModuleDoNotConvertBp2build("filegroup", "reqd") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001252filegroup {
1253 name: "fg_foo",
1254 required: ["reqd"],
1255 bazel_module: { bp2build_available: true },
1256}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001257 expectedBazelTargets: []string{
1258 makeBazelTarget("filegroup", "fg_foo", map[string]string{
1259 "data": `[":reqd"]`,
1260 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001261 },
1262 },
1263 {
1264 description: "Required via arch into data test",
1265 moduleTypeUnderTest: "python_library",
1266 moduleTypeUnderTestFactory: python.PythonLibraryFactory,
1267 moduleTypeUnderTestBp2BuildMutator: python.PythonLibraryBp2Build,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001268 blueprint: simpleModuleDoNotConvertBp2build("python_library", "reqdx86") +
1269 simpleModuleDoNotConvertBp2build("python_library", "reqdarm") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001270python_library {
1271 name: "fg_foo",
1272 arch: {
1273 arm: {
1274 required: ["reqdarm"],
1275 },
1276 x86: {
1277 required: ["reqdx86"],
1278 },
1279 },
1280 bazel_module: { bp2build_available: true },
1281}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001282 expectedBazelTargets: []string{
1283 makeBazelTarget("py_library", "fg_foo", map[string]string{
1284 "data": `select({
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001285 "//build/bazel/platforms/arch:arm": [":reqdarm"],
1286 "//build/bazel/platforms/arch:x86": [":reqdx86"],
1287 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001288 })`,
1289 "srcs_version": `"PY3"`,
1290 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001291 },
1292 },
1293 {
1294 description: "Required appended to data test",
1295 moduleTypeUnderTest: "python_library",
1296 moduleTypeUnderTestFactory: python.PythonLibraryFactory,
1297 moduleTypeUnderTestBp2BuildMutator: python.PythonLibraryBp2Build,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001298 filesystem: map[string]string{
1299 "data.bin": "",
1300 "src.py": "",
1301 },
1302 blueprint: simpleModuleDoNotConvertBp2build("python_library", "reqd") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001303python_library {
1304 name: "fg_foo",
1305 data: ["data.bin"],
1306 required: ["reqd"],
1307 bazel_module: { bp2build_available: true },
1308}`,
1309 expectedBazelTargets: []string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001310 makeBazelTarget("py_library", "fg_foo", map[string]string{
1311 "data": `[
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001312 "data.bin",
1313 ":reqd",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001314 ]`,
1315 "srcs_version": `"PY3"`,
1316 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001317 },
1318 },
1319 {
1320 description: "All props-to-attrs at once together test",
1321 moduleTypeUnderTest: "filegroup",
1322 moduleTypeUnderTestFactory: android.FileGroupFactory,
1323 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001324 blueprint: simpleModuleDoNotConvertBp2build("filegroup", "reqd") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001325filegroup {
1326 name: "fg_foo",
1327 required: ["reqd"],
1328 bazel_module: { bp2build_available: true },
1329}`,
1330 expectedBazelTargets: []string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001331 makeBazelTarget("filegroup", "fg_foo", map[string]string{
1332 "data": `[":reqd"]`,
1333 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001334 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001335 },
1336 }
1337
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001338 for _, tc := range testCases {
1339 t.Run(tc.description, func(t *testing.T) {
1340 runBp2BuildTestCaseSimple(t, tc)
1341 })
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001342 }
1343}