blob: ee1d862cd31874cd21d0f9b530c18a161257849a [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 Kammer4562a3b2021-04-21 18:15:34 -0400233 expectedBazelTargets: []string{`custom(
Jingwen Chen73850672020-12-14 08:25:34 -0500234 name = "foo",
235 string_list_prop = [
236 "a",
237 "b",
238 ],
239 string_prop = "a",
240)`,
Liz Kammer4562a3b2021-04-21 18:15:34 -0400241 },
Jingwen Chen73850672020-12-14 08:25:34 -0500242 },
Jingwen Chen58a12b82021-03-30 13:08:36 +0000243 {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400244 description: "control characters",
Jingwen Chen5146ac02021-09-02 11:44:42 +0000245 blueprint: `custom {
Jingwen Chen58a12b82021-03-30 13:08:36 +0000246 name: "control_characters",
247 string_list_prop: ["\t", "\n"],
248 string_prop: "a\t\n\r",
249 bazel_module: { bp2build_available: true },
250}`,
Liz Kammer4562a3b2021-04-21 18:15:34 -0400251 expectedBazelTargets: []string{`custom(
Jingwen Chen58a12b82021-03-30 13:08:36 +0000252 name = "control_characters",
253 string_list_prop = [
254 "\t",
255 "\n",
256 ],
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}`,
274 expectedBazelTargets: []string{`custom(
275 name = "dep",
276 arch_paths = ["abc"],
277)`,
278 `custom(
279 name = "has_dep",
280 arch_paths = [":dep"],
281)`,
282 },
283 },
284 {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400285 description: "arch-variant srcs",
Jingwen Chen5146ac02021-09-02 11:44:42 +0000286 blueprint: `custom {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400287 name: "arch_paths",
288 arch: {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400289 x86: { arch_paths: ["x86.txt"] },
290 x86_64: { arch_paths: ["x86_64.txt"] },
291 arm: { arch_paths: ["arm.txt"] },
292 arm64: { arch_paths: ["arm64.txt"] },
293 },
294 target: {
295 linux: { arch_paths: ["linux.txt"] },
296 bionic: { arch_paths: ["bionic.txt"] },
297 host: { arch_paths: ["host.txt"] },
298 not_windows: { arch_paths: ["not_windows.txt"] },
299 android: { arch_paths: ["android.txt"] },
300 linux_musl: { arch_paths: ["linux_musl.txt"] },
301 musl: { arch_paths: ["musl.txt"] },
302 linux_glibc: { arch_paths: ["linux_glibc.txt"] },
303 glibc: { arch_paths: ["glibc.txt"] },
304 linux_bionic: { arch_paths: ["linux_bionic.txt"] },
305 darwin: { arch_paths: ["darwin.txt"] },
306 windows: { arch_paths: ["windows.txt"] },
307 },
308 multilib: {
309 lib32: { arch_paths: ["lib32.txt"] },
310 lib64: { arch_paths: ["lib64.txt"] },
Liz Kammer4562a3b2021-04-21 18:15:34 -0400311 },
312 bazel_module: { bp2build_available: true },
313}`,
314 expectedBazelTargets: []string{`custom(
315 name = "arch_paths",
316 arch_paths = select({
Liz Kammerfdd72e62021-10-11 15:41:03 -0400317 "//build/bazel/platforms/arch:arm": [
318 "arm.txt",
319 "lib32.txt",
320 ],
321 "//build/bazel/platforms/arch:arm64": [
322 "arm64.txt",
323 "lib64.txt",
324 ],
325 "//build/bazel/platforms/arch:x86": [
326 "x86.txt",
327 "lib32.txt",
328 ],
329 "//build/bazel/platforms/arch:x86_64": [
330 "x86_64.txt",
331 "lib64.txt",
332 ],
333 "//conditions:default": [],
334 }) + select({
335 "//build/bazel/platforms/os:android": [
336 "linux.txt",
337 "bionic.txt",
338 "android.txt",
339 ],
340 "//build/bazel/platforms/os:darwin": [
341 "host.txt",
342 "darwin.txt",
343 "not_windows.txt",
344 ],
345 "//build/bazel/platforms/os:linux": [
346 "host.txt",
347 "linux.txt",
348 "glibc.txt",
349 "linux_glibc.txt",
350 "not_windows.txt",
351 ],
352 "//build/bazel/platforms/os:linux_bionic": [
353 "host.txt",
354 "linux.txt",
355 "bionic.txt",
356 "linux_bionic.txt",
357 "not_windows.txt",
358 ],
359 "//build/bazel/platforms/os:linux_musl": [
360 "host.txt",
361 "linux.txt",
362 "musl.txt",
363 "linux_musl.txt",
364 "not_windows.txt",
365 ],
366 "//build/bazel/platforms/os:windows": [
367 "host.txt",
368 "windows.txt",
369 ],
Liz Kammer4562a3b2021-04-21 18:15:34 -0400370 "//conditions:default": [],
371 }),
372)`,
373 },
374 },
375 {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400376 description: "arch-variant deps",
Jingwen Chen5146ac02021-09-02 11:44:42 +0000377 blueprint: `custom {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400378 name: "has_dep",
379 arch: {
380 x86: {
381 arch_paths: [":dep"],
382 },
383 },
384 bazel_module: { bp2build_available: true },
385}
386
387custom {
388 name: "dep",
389 arch_paths: ["abc"],
390 bazel_module: { bp2build_available: true },
391}`,
392 expectedBazelTargets: []string{`custom(
393 name = "dep",
394 arch_paths = ["abc"],
395)`,
396 `custom(
397 name = "has_dep",
398 arch_paths = select({
399 "//build/bazel/platforms/arch:x86": [":dep"],
400 "//conditions:default": [],
401 }),
402)`,
403 },
Jingwen Chen58a12b82021-03-30 13:08:36 +0000404 },
Liz Kammer32a03392021-09-14 11:17:21 -0400405 {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400406 description: "embedded props",
Liz Kammer32a03392021-09-14 11:17:21 -0400407 blueprint: `custom {
408 name: "embedded_props",
409 embedded_prop: "abc",
410 bazel_module: { bp2build_available: true },
411}`,
412 expectedBazelTargets: []string{`custom(
413 name = "embedded_props",
414 embedded_attr = "abc",
415)`,
416 },
417 },
418 {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400419 description: "ptr to embedded props",
Liz Kammer32a03392021-09-14 11:17:21 -0400420 blueprint: `custom {
421 name: "ptr_to_embedded_props",
422 other_embedded_prop: "abc",
423 bazel_module: { bp2build_available: true },
424}`,
425 expectedBazelTargets: []string{`custom(
426 name = "ptr_to_embedded_props",
427 other_embedded_attr = "abc",
428)`,
429 },
430 },
Jingwen Chen73850672020-12-14 08:25:34 -0500431 }
432
433 dir := "."
434 for _, testCase := range testCases {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400435 t.Run(testCase.description, func(t *testing.T) {
436 config := android.TestConfig(buildDir, nil, testCase.blueprint, nil)
437 ctx := android.NewTestContext(config)
Jingwen Chen164e0862021-02-19 00:48:40 -0500438
Liz Kammerfdd72e62021-10-11 15:41:03 -0400439 registerCustomModuleForBp2buildConversion(ctx)
Jingwen Chen73850672020-12-14 08:25:34 -0500440
Liz Kammerfdd72e62021-10-11 15:41:03 -0400441 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
442 if errored(t, testCase, errs) {
443 return
444 }
445 _, errs = ctx.ResolveDependencies(config)
446 if errored(t, testCase, errs) {
447 return
448 }
Jingwen Chen73850672020-12-14 08:25:34 -0500449
Liz Kammerfdd72e62021-10-11 15:41:03 -0400450 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
451 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
452 android.FailIfErrored(t, err)
Jingwen Chen164e0862021-02-19 00:48:40 -0500453
Liz Kammerfdd72e62021-10-11 15:41:03 -0400454 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
455 t.Errorf("Expected %d bazel target, got %d", expectedCount, actualCount)
456 } else {
457 for i, expectedBazelTarget := range testCase.expectedBazelTargets {
458 actualBazelTarget := bazelTargets[i]
459 if actualBazelTarget.content != expectedBazelTarget {
460 t.Errorf(
461 "Expected generated Bazel target to be '%s', got '%s'",
462 expectedBazelTarget,
463 actualBazelTarget.content,
464 )
465 }
Liz Kammer4562a3b2021-04-21 18:15:34 -0400466 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500467 }
Liz Kammerfdd72e62021-10-11 15:41:03 -0400468 })
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800469 }
470}
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500471
Jingwen Chen40067de2021-01-26 21:58:43 -0500472func TestLoadStatements(t *testing.T) {
473 testCases := []struct {
474 bazelTargets BazelTargets
475 expectedLoadStatements string
476 }{
477 {
478 bazelTargets: BazelTargets{
479 BazelTarget{
480 name: "foo",
481 ruleClass: "cc_library",
482 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
483 },
484 },
485 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
486 },
487 {
488 bazelTargets: BazelTargets{
489 BazelTarget{
490 name: "foo",
491 ruleClass: "cc_library",
492 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
493 },
494 BazelTarget{
495 name: "bar",
496 ruleClass: "cc_library",
497 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
498 },
499 },
500 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
501 },
502 {
503 bazelTargets: BazelTargets{
504 BazelTarget{
505 name: "foo",
506 ruleClass: "cc_library",
507 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
508 },
509 BazelTarget{
510 name: "bar",
511 ruleClass: "cc_binary",
512 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
513 },
514 },
515 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")`,
516 },
517 {
518 bazelTargets: BazelTargets{
519 BazelTarget{
520 name: "foo",
521 ruleClass: "cc_library",
522 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
523 },
524 BazelTarget{
525 name: "bar",
526 ruleClass: "cc_binary",
527 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
528 },
529 BazelTarget{
530 name: "baz",
531 ruleClass: "java_binary",
532 bzlLoadLocation: "//build/bazel/rules:java.bzl",
533 },
534 },
535 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")
536load("//build/bazel/rules:java.bzl", "java_binary")`,
537 },
538 {
539 bazelTargets: BazelTargets{
540 BazelTarget{
541 name: "foo",
542 ruleClass: "cc_binary",
543 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
544 },
545 BazelTarget{
546 name: "bar",
547 ruleClass: "java_binary",
548 bzlLoadLocation: "//build/bazel/rules:java.bzl",
549 },
550 BazelTarget{
551 name: "baz",
552 ruleClass: "genrule",
553 // Note: no bzlLoadLocation for native rules
554 },
555 },
556 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary")
557load("//build/bazel/rules:java.bzl", "java_binary")`,
558 },
559 }
560
561 for _, testCase := range testCases {
562 actual := testCase.bazelTargets.LoadStatements()
563 expected := testCase.expectedLoadStatements
564 if actual != expected {
565 t.Fatalf("Expected load statements to be %s, got %s", expected, actual)
566 }
567 }
568
569}
570
571func TestGenerateBazelTargetModules_OneToMany_LoadedFromStarlark(t *testing.T) {
572 testCases := []struct {
573 bp string
574 expectedBazelTarget string
575 expectedBazelTargetCount int
576 expectedLoadStatements string
577 }{
578 {
579 bp: `custom {
580 name: "bar",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500581 bazel_module: { bp2build_available: true },
Jingwen Chen40067de2021-01-26 21:58:43 -0500582}`,
583 expectedBazelTarget: `my_library(
584 name = "bar",
585)
586
Jingwen Chen40067de2021-01-26 21:58:43 -0500587proto_library(
588 name = "bar_proto_library_deps",
Liz Kammer2ada09a2021-08-11 00:17:36 -0400589)
590
591my_proto_library(
592 name = "bar_my_proto_library_deps",
Jingwen Chen40067de2021-01-26 21:58:43 -0500593)`,
594 expectedBazelTargetCount: 3,
595 expectedLoadStatements: `load("//build/bazel/rules:proto.bzl", "my_proto_library", "proto_library")
596load("//build/bazel/rules:rules.bzl", "my_library")`,
597 },
598 }
599
600 dir := "."
601 for _, testCase := range testCases {
602 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
603 ctx := android.NewTestContext(config)
604 ctx.RegisterModuleType("custom", customModuleFactory)
Jingwen Chen12b4c272021-03-10 02:05:59 -0500605 ctx.RegisterBp2BuildMutator("custom", customBp2BuildMutatorFromStarlark)
Jingwen Chen40067de2021-01-26 21:58:43 -0500606 ctx.RegisterForBazelConversion()
607
608 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
609 android.FailIfErrored(t, errs)
610 _, errs = ctx.ResolveDependencies(config)
611 android.FailIfErrored(t, errs)
612
Jingwen Chen164e0862021-02-19 00:48:40 -0500613 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Liz Kammer6eff3232021-08-26 08:37:59 -0400614 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
615 android.FailIfErrored(t, err)
Jingwen Chen40067de2021-01-26 21:58:43 -0500616 if actualCount := len(bazelTargets); actualCount != testCase.expectedBazelTargetCount {
617 t.Fatalf("Expected %d bazel target, got %d", testCase.expectedBazelTargetCount, actualCount)
618 }
619
620 actualBazelTargets := bazelTargets.String()
621 if actualBazelTargets != testCase.expectedBazelTarget {
622 t.Errorf(
623 "Expected generated Bazel target to be '%s', got '%s'",
624 testCase.expectedBazelTarget,
625 actualBazelTargets,
626 )
627 }
628
629 actualLoadStatements := bazelTargets.LoadStatements()
630 if actualLoadStatements != testCase.expectedLoadStatements {
631 t.Errorf(
632 "Expected generated load statements to be '%s', got '%s'",
633 testCase.expectedLoadStatements,
634 actualLoadStatements,
635 )
636 }
637 }
638}
639
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500640func TestModuleTypeBp2Build(t *testing.T) {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000641 testCases := []bp2buildTestCase{
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500642 {
Liz Kammerebfcf672021-02-16 15:00:05 -0500643 description: "filegroup with does not specify srcs",
644 moduleTypeUnderTest: "filegroup",
645 moduleTypeUnderTestFactory: android.FileGroupFactory,
646 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000647 blueprint: `filegroup {
Liz Kammerebfcf672021-02-16 15:00:05 -0500648 name: "fg_foo",
649 bazel_module: { bp2build_available: true },
650}`,
651 expectedBazelTargets: []string{
652 `filegroup(
653 name = "fg_foo",
654)`,
655 },
656 },
657 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500658 description: "filegroup with no srcs",
659 moduleTypeUnderTest: "filegroup",
660 moduleTypeUnderTestFactory: android.FileGroupFactory,
661 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000662 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500663 name: "fg_foo",
664 srcs: [],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500665 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500666}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500667 expectedBazelTargets: []string{
668 `filegroup(
669 name = "fg_foo",
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500670)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500671 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500672 },
673 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500674 description: "filegroup with srcs",
675 moduleTypeUnderTest: "filegroup",
676 moduleTypeUnderTestFactory: android.FileGroupFactory,
677 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000678 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500679 name: "fg_foo",
680 srcs: ["a", "b"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500681 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500682}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500683 expectedBazelTargets: []string{`filegroup(
684 name = "fg_foo",
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500685 srcs = [
686 "a",
687 "b",
688 ],
689)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500690 },
691 },
692 {
693 description: "filegroup with excludes srcs",
694 moduleTypeUnderTest: "filegroup",
695 moduleTypeUnderTestFactory: android.FileGroupFactory,
696 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000697 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500698 name: "fg_foo",
699 srcs: ["a", "b"],
700 exclude_srcs: ["a"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500701 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500702}`,
703 expectedBazelTargets: []string{`filegroup(
704 name = "fg_foo",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000705 srcs = ["b"],
Liz Kammer356f7d42021-01-26 09:18:53 -0500706)`,
707 },
708 },
709 {
710 description: "filegroup with glob",
711 moduleTypeUnderTest: "filegroup",
712 moduleTypeUnderTestFactory: android.FileGroupFactory,
713 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000714 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500715 name: "foo",
716 srcs: ["**/*.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500717 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500718}`,
719 expectedBazelTargets: []string{`filegroup(
720 name = "foo",
721 srcs = [
722 "other/a.txt",
723 "other/b.txt",
724 "other/subdir/a.txt",
725 ],
726)`,
727 },
Jingwen Chen5146ac02021-09-02 11:44:42 +0000728 filesystem: map[string]string{
Liz Kammer356f7d42021-01-26 09:18:53 -0500729 "other/a.txt": "",
730 "other/b.txt": "",
731 "other/subdir/a.txt": "",
732 "other/file": "",
733 },
734 },
735 {
736 description: "filegroup with glob in subdir",
737 moduleTypeUnderTest: "filegroup",
738 moduleTypeUnderTestFactory: android.FileGroupFactory,
739 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000740 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500741 name: "foo",
742 srcs: ["a.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500743 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500744}`,
745 dir: "other",
746 expectedBazelTargets: []string{`filegroup(
747 name = "fg_foo",
748 srcs = [
749 "a.txt",
750 "b.txt",
751 "subdir/a.txt",
752 ],
753)`,
754 },
Jingwen Chen5146ac02021-09-02 11:44:42 +0000755 filesystem: map[string]string{
Liz Kammer356f7d42021-01-26 09:18:53 -0500756 "other/Android.bp": `filegroup {
757 name: "fg_foo",
758 srcs: ["**/*.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500759 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500760}`,
761 "other/a.txt": "",
762 "other/b.txt": "",
763 "other/subdir/a.txt": "",
764 "other/file": "",
765 },
766 },
767 {
768 description: "depends_on_other_dir_module",
769 moduleTypeUnderTest: "filegroup",
770 moduleTypeUnderTestFactory: android.FileGroupFactory,
771 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000772 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500773 name: "foobar",
774 srcs: [
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000775 ":foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500776 "c",
777 ],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500778 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500779}`,
780 expectedBazelTargets: []string{`filegroup(
781 name = "foobar",
782 srcs = [
783 "//other:foo",
784 "c",
785 ],
786)`,
787 },
Jingwen Chen5146ac02021-09-02 11:44:42 +0000788 filesystem: map[string]string{
Liz Kammer356f7d42021-01-26 09:18:53 -0500789 "other/Android.bp": `filegroup {
790 name: "foo",
791 srcs: ["a", "b"],
Liz Kammer6eff3232021-08-26 08:37:59 -0400792 bazel_module: { bp2build_available: true },
793}`,
794 },
795 },
796 {
797 description: "depends_on_other_unconverted_module_error",
798 moduleTypeUnderTest: "filegroup",
799 moduleTypeUnderTestFactory: android.FileGroupFactory,
800 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
801 unconvertedDepsMode: errorModulesUnconvertedDeps,
802 blueprint: `filegroup {
803 name: "foobar",
804 srcs: [
805 ":foo",
806 "c",
807 ],
808 bazel_module: { bp2build_available: true },
809}`,
810 expectedErr: fmt.Errorf(`"foobar" depends on unconverted modules: foo`),
811 filesystem: map[string]string{
812 "other/Android.bp": `filegroup {
813 name: "foo",
814 srcs: ["a", "b"],
Liz Kammer356f7d42021-01-26 09:18:53 -0500815}`,
816 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500817 },
818 }
819
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500820 for _, testCase := range testCases {
Liz Kammer6eff3232021-08-26 08:37:59 -0400821 t.Run(testCase.description, func(t *testing.T) {
822 runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, testCase)
823 })
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500824 }
825}
Jingwen Chen041b1842021-02-01 00:23:25 -0500826
827type bp2buildMutator = func(android.TopDownMutatorContext)
828
Jingwen Chen12b4c272021-03-10 02:05:59 -0500829func TestAllowlistingBp2buildTargetsExplicitly(t *testing.T) {
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500830 testCases := []struct {
831 moduleTypeUnderTest string
832 moduleTypeUnderTestFactory android.ModuleFactory
833 moduleTypeUnderTestBp2BuildMutator bp2buildMutator
834 bp string
835 expectedCount int
836 description string
837 }{
838 {
839 description: "explicitly unavailable",
840 moduleTypeUnderTest: "filegroup",
841 moduleTypeUnderTestFactory: android.FileGroupFactory,
842 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
843 bp: `filegroup {
844 name: "foo",
845 srcs: ["a", "b"],
846 bazel_module: { bp2build_available: false },
847}`,
848 expectedCount: 0,
849 },
850 {
851 description: "implicitly unavailable",
852 moduleTypeUnderTest: "filegroup",
853 moduleTypeUnderTestFactory: android.FileGroupFactory,
854 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
855 bp: `filegroup {
856 name: "foo",
857 srcs: ["a", "b"],
858}`,
859 expectedCount: 0,
860 },
861 {
862 description: "explicitly available",
863 moduleTypeUnderTest: "filegroup",
864 moduleTypeUnderTestFactory: android.FileGroupFactory,
865 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
866 bp: `filegroup {
867 name: "foo",
868 srcs: ["a", "b"],
869 bazel_module: { bp2build_available: true },
870}`,
871 expectedCount: 1,
872 },
873 {
874 description: "generates more than 1 target if needed",
875 moduleTypeUnderTest: "custom",
876 moduleTypeUnderTestFactory: customModuleFactory,
877 moduleTypeUnderTestBp2BuildMutator: customBp2BuildMutatorFromStarlark,
878 bp: `custom {
879 name: "foo",
880 bazel_module: { bp2build_available: true },
881}`,
882 expectedCount: 3,
883 },
884 }
885
886 dir := "."
887 for _, testCase := range testCases {
Liz Kammer2ada09a2021-08-11 00:17:36 -0400888 t.Run(testCase.description, func(t *testing.T) {
889 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
890 ctx := android.NewTestContext(config)
891 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
892 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
893 ctx.RegisterForBazelConversion()
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500894
Liz Kammer2ada09a2021-08-11 00:17:36 -0400895 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
896 android.FailIfErrored(t, errs)
897 _, errs = ctx.ResolveDependencies(config)
898 android.FailIfErrored(t, errs)
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500899
Liz Kammer2ada09a2021-08-11 00:17:36 -0400900 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Liz Kammer6eff3232021-08-26 08:37:59 -0400901 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
902 android.FailIfErrored(t, err)
Liz Kammer2ada09a2021-08-11 00:17:36 -0400903 if actualCount := len(bazelTargets); actualCount != testCase.expectedCount {
904 t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, testCase.expectedCount, actualCount)
905 }
906 })
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500907 }
908}
Liz Kammerba3ea162021-02-17 13:22:03 -0500909
Jingwen Chen12b4c272021-03-10 02:05:59 -0500910func TestAllowlistingBp2buildTargetsWithConfig(t *testing.T) {
911 testCases := []struct {
912 moduleTypeUnderTest string
913 moduleTypeUnderTestFactory android.ModuleFactory
914 moduleTypeUnderTestBp2BuildMutator bp2buildMutator
915 expectedCount map[string]int
916 description string
917 bp2buildConfig android.Bp2BuildConfig
918 checkDir string
919 fs map[string]string
920 }{
921 {
922 description: "test bp2build config package and subpackages config",
923 moduleTypeUnderTest: "filegroup",
924 moduleTypeUnderTestFactory: android.FileGroupFactory,
925 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
926 expectedCount: map[string]int{
927 "migrated": 1,
928 "migrated/but_not_really": 0,
929 "migrated/but_not_really/but_really": 1,
930 "not_migrated": 0,
931 "also_not_migrated": 0,
932 },
933 bp2buildConfig: android.Bp2BuildConfig{
934 "migrated": android.Bp2BuildDefaultTrueRecursively,
935 "migrated/but_not_really": android.Bp2BuildDefaultFalse,
936 "not_migrated": android.Bp2BuildDefaultFalse,
937 },
938 fs: map[string]string{
939 "migrated/Android.bp": `filegroup { name: "a" }`,
940 "migrated/but_not_really/Android.bp": `filegroup { name: "b" }`,
941 "migrated/but_not_really/but_really/Android.bp": `filegroup { name: "c" }`,
942 "not_migrated/Android.bp": `filegroup { name: "d" }`,
943 "also_not_migrated/Android.bp": `filegroup { name: "e" }`,
944 },
945 },
946 {
947 description: "test bp2build config opt-in and opt-out",
948 moduleTypeUnderTest: "filegroup",
949 moduleTypeUnderTestFactory: android.FileGroupFactory,
950 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
951 expectedCount: map[string]int{
952 "package-opt-in": 2,
953 "package-opt-in/subpackage": 0,
954 "package-opt-out": 1,
955 "package-opt-out/subpackage": 0,
956 },
957 bp2buildConfig: android.Bp2BuildConfig{
958 "package-opt-in": android.Bp2BuildDefaultFalse,
959 "package-opt-out": android.Bp2BuildDefaultTrueRecursively,
960 },
961 fs: map[string]string{
962 "package-opt-in/Android.bp": `
963filegroup { name: "opt-in-a" }
964filegroup { name: "opt-in-b", bazel_module: { bp2build_available: true } }
965filegroup { name: "opt-in-c", bazel_module: { bp2build_available: true } }
966`,
967
968 "package-opt-in/subpackage/Android.bp": `
969filegroup { name: "opt-in-d" } // parent package not configured to DefaultTrueRecursively
970`,
971
972 "package-opt-out/Android.bp": `
973filegroup { name: "opt-out-a" }
974filegroup { name: "opt-out-b", bazel_module: { bp2build_available: false } }
975filegroup { name: "opt-out-c", bazel_module: { bp2build_available: false } }
976`,
977
978 "package-opt-out/subpackage/Android.bp": `
979filegroup { name: "opt-out-g", bazel_module: { bp2build_available: false } }
980filegroup { name: "opt-out-h", bazel_module: { bp2build_available: false } }
981`,
982 },
983 },
984 }
985
986 dir := "."
987 for _, testCase := range testCases {
988 fs := make(map[string][]byte)
989 toParse := []string{
990 "Android.bp",
991 }
992 for f, content := range testCase.fs {
993 if strings.HasSuffix(f, "Android.bp") {
994 toParse = append(toParse, f)
995 }
996 fs[f] = []byte(content)
997 }
998 config := android.TestConfig(buildDir, nil, "", fs)
999 ctx := android.NewTestContext(config)
1000 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
1001 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1002 ctx.RegisterBp2BuildConfig(testCase.bp2buildConfig)
1003 ctx.RegisterForBazelConversion()
1004
1005 _, errs := ctx.ParseFileList(dir, toParse)
1006 android.FailIfErrored(t, errs)
1007 _, errs = ctx.ResolveDependencies(config)
1008 android.FailIfErrored(t, errs)
1009
1010 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
1011
1012 // For each directory, test that the expected number of generated targets is correct.
1013 for dir, expectedCount := range testCase.expectedCount {
Liz Kammer6eff3232021-08-26 08:37:59 -04001014 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
1015 android.FailIfErrored(t, err)
Jingwen Chen12b4c272021-03-10 02:05:59 -05001016 if actualCount := len(bazelTargets); actualCount != expectedCount {
1017 t.Fatalf(
1018 "%s: Expected %d bazel target for %s package, got %d",
1019 testCase.description,
1020 expectedCount,
1021 dir,
1022 actualCount)
1023 }
1024
1025 }
1026 }
1027}
1028
Liz Kammerba3ea162021-02-17 13:22:03 -05001029func TestCombineBuildFilesBp2buildTargets(t *testing.T) {
Jingwen Chen5146ac02021-09-02 11:44:42 +00001030 testCases := []bp2buildTestCase{
Liz Kammerba3ea162021-02-17 13:22:03 -05001031 {
1032 description: "filegroup bazel_module.label",
1033 moduleTypeUnderTest: "filegroup",
1034 moduleTypeUnderTestFactory: android.FileGroupFactory,
1035 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +00001036 blueprint: `filegroup {
Liz Kammerba3ea162021-02-17 13:22:03 -05001037 name: "fg_foo",
1038 bazel_module: { label: "//other:fg_foo" },
1039}`,
1040 expectedBazelTargets: []string{
1041 `// BUILD file`,
1042 },
Jingwen Chen5146ac02021-09-02 11:44:42 +00001043 filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -05001044 "other/BUILD.bazel": `// BUILD file`,
1045 },
1046 },
1047 {
1048 description: "multiple bazel_module.label same BUILD",
1049 moduleTypeUnderTest: "filegroup",
1050 moduleTypeUnderTestFactory: android.FileGroupFactory,
1051 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +00001052 blueprint: `filegroup {
Jingwen Chenc63677b2021-06-17 05:43:19 +00001053 name: "fg_foo",
1054 bazel_module: { label: "//other:fg_foo" },
1055 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001056
Jingwen Chenc63677b2021-06-17 05:43:19 +00001057 filegroup {
1058 name: "foo",
1059 bazel_module: { label: "//other:foo" },
1060 }`,
Liz Kammerba3ea162021-02-17 13:22:03 -05001061 expectedBazelTargets: []string{
1062 `// BUILD file`,
1063 },
Jingwen Chen5146ac02021-09-02 11:44:42 +00001064 filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -05001065 "other/BUILD.bazel": `// BUILD file`,
1066 },
1067 },
1068 {
Jingwen Chenc63677b2021-06-17 05:43:19 +00001069 description: "filegroup bazel_module.label and bp2build in subdir",
Liz Kammerba3ea162021-02-17 13:22:03 -05001070 moduleTypeUnderTest: "filegroup",
1071 moduleTypeUnderTestFactory: android.FileGroupFactory,
1072 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chenc63677b2021-06-17 05:43:19 +00001073 dir: "other",
Jingwen Chen5146ac02021-09-02 11:44:42 +00001074 blueprint: ``,
1075 filesystem: map[string]string{
Jingwen Chenc63677b2021-06-17 05:43:19 +00001076 "other/Android.bp": `filegroup {
1077 name: "fg_foo",
1078 bazel_module: {
1079 bp2build_available: true,
1080 },
1081 }
1082 filegroup {
1083 name: "fg_bar",
1084 bazel_module: {
1085 label: "//other:fg_bar"
1086 },
1087 }`,
1088 "other/BUILD.bazel": `// definition for fg_bar`,
1089 },
Liz Kammerba3ea162021-02-17 13:22:03 -05001090 expectedBazelTargets: []string{
1091 `filegroup(
1092 name = "fg_foo",
Jingwen Chenc63677b2021-06-17 05:43:19 +00001093)`, `// definition for fg_bar`,
Liz Kammerba3ea162021-02-17 13:22:03 -05001094 },
1095 },
1096 {
1097 description: "filegroup bazel_module.label and filegroup bp2build",
1098 moduleTypeUnderTest: "filegroup",
1099 moduleTypeUnderTestFactory: android.FileGroupFactory,
1100 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +00001101 blueprint: `filegroup {
Jingwen Chenc63677b2021-06-17 05:43:19 +00001102 name: "fg_foo",
1103 bazel_module: {
1104 label: "//other:fg_foo",
1105 },
1106 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001107
Jingwen Chenc63677b2021-06-17 05:43:19 +00001108 filegroup {
1109 name: "fg_bar",
1110 bazel_module: {
1111 bp2build_available: true,
1112 },
1113 }`,
Liz Kammerba3ea162021-02-17 13:22:03 -05001114 expectedBazelTargets: []string{
1115 `filegroup(
1116 name = "fg_bar",
1117)`,
1118 `// BUILD file`,
1119 },
Jingwen Chen5146ac02021-09-02 11:44:42 +00001120 filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -05001121 "other/BUILD.bazel": `// BUILD file`,
1122 },
1123 },
1124 }
1125
1126 dir := "."
1127 for _, testCase := range testCases {
Jingwen Chen49109762021-05-25 05:16:48 +00001128 t.Run(testCase.description, func(t *testing.T) {
1129 fs := make(map[string][]byte)
1130 toParse := []string{
1131 "Android.bp",
Liz Kammerba3ea162021-02-17 13:22:03 -05001132 }
Jingwen Chen5146ac02021-09-02 11:44:42 +00001133 for f, content := range testCase.filesystem {
Jingwen Chen49109762021-05-25 05:16:48 +00001134 if strings.HasSuffix(f, "Android.bp") {
1135 toParse = append(toParse, f)
1136 }
1137 fs[f] = []byte(content)
1138 }
Jingwen Chen5146ac02021-09-02 11:44:42 +00001139 config := android.TestConfig(buildDir, nil, testCase.blueprint, fs)
Jingwen Chen49109762021-05-25 05:16:48 +00001140 ctx := android.NewTestContext(config)
1141 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Jingwen Chen49109762021-05-25 05:16:48 +00001142 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1143 ctx.RegisterForBazelConversion()
Liz Kammerba3ea162021-02-17 13:22:03 -05001144
Jingwen Chen49109762021-05-25 05:16:48 +00001145 _, errs := ctx.ParseFileList(dir, toParse)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001146 if errored(t, testCase, errs) {
Jingwen Chen49109762021-05-25 05:16:48 +00001147 return
1148 }
1149 _, errs = ctx.ResolveDependencies(config)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001150 if errored(t, testCase, errs) {
Jingwen Chen49109762021-05-25 05:16:48 +00001151 return
1152 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001153
Jingwen Chen49109762021-05-25 05:16:48 +00001154 checkDir := dir
1155 if testCase.dir != "" {
1156 checkDir = testCase.dir
1157 }
Liz Kammer6eff3232021-08-26 08:37:59 -04001158 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
1159 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, checkDir)
1160 android.FailIfErrored(t, err)
Jingwen Chen49109762021-05-25 05:16:48 +00001161 bazelTargets.sort()
1162 actualCount := len(bazelTargets)
1163 expectedCount := len(testCase.expectedBazelTargets)
1164 if actualCount != expectedCount {
1165 t.Errorf("Expected %d bazel target, got %d\n%s", expectedCount, actualCount, bazelTargets)
1166 }
1167 if !strings.Contains(bazelTargets.String(), "Section: Handcrafted targets. ") {
1168 t.Errorf("Expected string representation of bazelTargets to contain handcrafted section header.")
1169 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001170 for i, target := range bazelTargets {
Jingwen Chen49109762021-05-25 05:16:48 +00001171 actualContent := target.content
1172 expectedContent := testCase.expectedBazelTargets[i]
1173 if expectedContent != actualContent {
Liz Kammerba3ea162021-02-17 13:22:03 -05001174 t.Errorf(
Jingwen Chen49109762021-05-25 05:16:48 +00001175 "Expected generated Bazel target to be '%s', got '%s'",
1176 expectedContent,
1177 actualContent,
Liz Kammerba3ea162021-02-17 13:22:03 -05001178 )
1179 }
1180 }
Jingwen Chen49109762021-05-25 05:16:48 +00001181 })
Liz Kammerba3ea162021-02-17 13:22:03 -05001182 }
1183}
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001184
1185func TestGlobExcludeSrcs(t *testing.T) {
Jingwen Chen5146ac02021-09-02 11:44:42 +00001186 testCases := []bp2buildTestCase{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001187 {
1188 description: "filegroup top level exclude_srcs",
1189 moduleTypeUnderTest: "filegroup",
1190 moduleTypeUnderTestFactory: android.FileGroupFactory,
1191 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +00001192 blueprint: `filegroup {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001193 name: "fg_foo",
1194 srcs: ["**/*.txt"],
1195 exclude_srcs: ["c.txt"],
1196 bazel_module: { bp2build_available: true },
1197}`,
1198 expectedBazelTargets: []string{`filegroup(
1199 name = "fg_foo",
1200 srcs = [
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001201 "a.txt",
1202 "b.txt",
Liz Kammer9abd62d2021-05-21 08:37:59 -04001203 "//dir:e.txt",
1204 "//dir:f.txt",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001205 ],
1206)`,
1207 },
Jingwen Chen5146ac02021-09-02 11:44:42 +00001208 filesystem: map[string]string{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001209 "a.txt": "",
1210 "b.txt": "",
1211 "c.txt": "",
1212 "dir/Android.bp": "",
1213 "dir/e.txt": "",
1214 "dir/f.txt": "",
1215 },
1216 },
1217 {
1218 description: "filegroup in subdir exclude_srcs",
1219 moduleTypeUnderTest: "filegroup",
1220 moduleTypeUnderTestFactory: android.FileGroupFactory,
1221 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +00001222 blueprint: "",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001223 dir: "dir",
Jingwen Chen5146ac02021-09-02 11:44:42 +00001224 filesystem: map[string]string{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001225 "dir/Android.bp": `filegroup {
1226 name: "fg_foo",
1227 srcs: ["**/*.txt"],
1228 exclude_srcs: ["b.txt"],
1229 bazel_module: { bp2build_available: true },
1230}
1231`,
1232 "dir/a.txt": "",
1233 "dir/b.txt": "",
1234 "dir/subdir/Android.bp": "",
1235 "dir/subdir/e.txt": "",
1236 "dir/subdir/f.txt": "",
1237 },
1238 expectedBazelTargets: []string{`filegroup(
1239 name = "fg_foo",
1240 srcs = [
Liz Kammer9abd62d2021-05-21 08:37:59 -04001241 "a.txt",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001242 "//dir/subdir:e.txt",
1243 "//dir/subdir:f.txt",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001244 ],
1245)`,
1246 },
1247 },
1248 }
1249
1250 dir := "."
1251 for _, testCase := range testCases {
1252 fs := make(map[string][]byte)
1253 toParse := []string{
1254 "Android.bp",
1255 }
Jingwen Chen5146ac02021-09-02 11:44:42 +00001256 for f, content := range testCase.filesystem {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001257 if strings.HasSuffix(f, "Android.bp") {
1258 toParse = append(toParse, f)
1259 }
1260 fs[f] = []byte(content)
1261 }
Jingwen Chen5146ac02021-09-02 11:44:42 +00001262 config := android.TestConfig(buildDir, nil, testCase.blueprint, fs)
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001263 ctx := android.NewTestContext(config)
1264 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
1265 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1266 ctx.RegisterForBazelConversion()
1267
1268 _, errs := ctx.ParseFileList(dir, toParse)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001269 if errored(t, testCase, errs) {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001270 continue
1271 }
1272 _, errs = ctx.ResolveDependencies(config)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001273 if errored(t, testCase, errs) {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001274 continue
1275 }
1276
1277 checkDir := dir
1278 if testCase.dir != "" {
1279 checkDir = testCase.dir
1280 }
Liz Kammer6eff3232021-08-26 08:37:59 -04001281 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
1282 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, checkDir)
1283 android.FailIfErrored(t, err)
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001284 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
1285 t.Errorf("%s: Expected %d bazel target, got %d\n%s", testCase.description, expectedCount, actualCount, bazelTargets)
1286 } else {
1287 for i, target := range bazelTargets {
1288 if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
1289 t.Errorf(
1290 "%s: Expected generated Bazel target to be '%s', got '%s'",
1291 testCase.description,
1292 w,
1293 g,
1294 )
1295 }
1296 }
1297 }
1298 }
1299}
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001300
1301func TestCommonBp2BuildModuleAttrs(t *testing.T) {
1302 testCases := []bp2buildTestCase{
1303 {
1304 description: "Required into data test",
1305 moduleTypeUnderTest: "filegroup",
1306 moduleTypeUnderTestFactory: android.FileGroupFactory,
1307 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1308 blueprint: `filegroup {
1309 name: "reqd",
1310}
1311
1312filegroup {
1313 name: "fg_foo",
1314 required: ["reqd"],
1315 bazel_module: { bp2build_available: true },
1316}`,
1317 expectedBazelTargets: []string{`filegroup(
1318 name = "fg_foo",
1319 data = [":reqd"],
1320)`,
1321 `filegroup(
1322 name = "reqd",
1323)`,
1324 },
1325 },
1326 {
1327 description: "Required via arch into data test",
1328 moduleTypeUnderTest: "python_library",
1329 moduleTypeUnderTestFactory: python.PythonLibraryFactory,
1330 moduleTypeUnderTestBp2BuildMutator: python.PythonLibraryBp2Build,
1331 blueprint: `python_library {
1332 name: "reqdx86",
1333 bazel_module: { bp2build_available: false, },
1334}
1335
1336python_library {
1337 name: "reqdarm",
1338 bazel_module: { bp2build_available: false, },
1339}
1340
1341python_library {
1342 name: "fg_foo",
1343 arch: {
1344 arm: {
1345 required: ["reqdarm"],
1346 },
1347 x86: {
1348 required: ["reqdx86"],
1349 },
1350 },
1351 bazel_module: { bp2build_available: true },
1352}`,
1353 expectedBazelTargets: []string{`py_library(
1354 name = "fg_foo",
1355 data = select({
1356 "//build/bazel/platforms/arch:arm": [":reqdarm"],
1357 "//build/bazel/platforms/arch:x86": [":reqdx86"],
1358 "//conditions:default": [],
1359 }),
1360 srcs_version = "PY3",
1361)`,
1362 },
1363 },
1364 {
1365 description: "Required appended to data test",
1366 moduleTypeUnderTest: "python_library",
1367 moduleTypeUnderTestFactory: python.PythonLibraryFactory,
1368 moduleTypeUnderTestBp2BuildMutator: python.PythonLibraryBp2Build,
1369 blueprint: `python_library {
1370 name: "reqd",
1371 srcs: ["src.py"],
1372}
1373
1374python_library {
1375 name: "fg_foo",
1376 data: ["data.bin"],
1377 required: ["reqd"],
1378 bazel_module: { bp2build_available: true },
1379}`,
1380 expectedBazelTargets: []string{
1381 `py_library(
1382 name = "fg_foo",
1383 data = [
1384 "data.bin",
1385 ":reqd",
1386 ],
1387 srcs_version = "PY3",
1388)`,
1389 `py_library(
1390 name = "reqd",
1391 srcs = ["src.py"],
1392 srcs_version = "PY3",
1393)`,
1394 },
1395 filesystem: map[string]string{
1396 "data.bin": "",
1397 "src.py": "",
1398 },
1399 },
1400 {
1401 description: "All props-to-attrs at once together test",
1402 moduleTypeUnderTest: "filegroup",
1403 moduleTypeUnderTestFactory: android.FileGroupFactory,
1404 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1405 blueprint: `filegroup {
1406 name: "reqd"
1407}
1408filegroup {
1409 name: "fg_foo",
1410 required: ["reqd"],
1411 bazel_module: { bp2build_available: true },
1412}`,
1413 expectedBazelTargets: []string{
1414 `filegroup(
1415 name = "fg_foo",
1416 data = [":reqd"],
1417)`,
1418 `filegroup(
1419 name = "reqd",
1420)`,
1421 },
1422 filesystem: map[string]string{},
1423 },
1424 }
1425
1426 for _, test := range testCases {
1427 runBp2BuildTestCaseSimple(t, test)
1428 }
1429}