blob: 95a26a963bc73ddaceff1f4b99968865a27bae58 [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 Kammer46fb7ab2021-12-01 10:09:34 -050044 string_prop = "",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080045)`,
46 },
47 {
Liz Kammerd366c902021-06-03 13:43:01 -040048 description: "handles bool",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080049 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -040050 name: "foo",
51 bool_prop: true,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080052}
Liz Kammerd366c902021-06-03 13:43:01 -040053 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080054 expectedBazelTarget: `soong_module(
55 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050056 soong_module_name = "foo",
57 soong_module_type = "custom",
58 soong_module_variant = "",
59 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080060 ],
Liz Kammerd366c902021-06-03 13:43:01 -040061 bool_prop = True,
Liz Kammer46fb7ab2021-12-01 10:09:34 -050062 string_prop = "",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080063)`,
64 },
65 {
Liz Kammerd366c902021-06-03 13:43:01 -040066 description: "string escaping",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080067 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -040068 name: "foo",
69 owner: "a_string_with\"quotes\"_and_\\backslashes\\\\",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080070}
Liz Kammerd366c902021-06-03 13:43:01 -040071 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080072 expectedBazelTarget: `soong_module(
73 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050074 soong_module_name = "foo",
75 soong_module_type = "custom",
76 soong_module_variant = "",
77 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080078 ],
Liz Kammerd366c902021-06-03 13:43:01 -040079 bool_prop = False,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080080 owner = "a_string_with\"quotes\"_and_\\backslashes\\\\",
Liz Kammer46fb7ab2021-12-01 10:09:34 -050081 string_prop = "",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080082)`,
83 },
84 {
Liz Kammerd366c902021-06-03 13:43:01 -040085 description: "single item string list",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080086 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -040087 name: "foo",
88 required: ["bar"],
Liz Kammer2dd9ca42020-11-25 16:06:39 -080089}
Liz Kammerd366c902021-06-03 13:43:01 -040090 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080091 expectedBazelTarget: `soong_module(
92 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050093 soong_module_name = "foo",
94 soong_module_type = "custom",
95 soong_module_variant = "",
96 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080097 ],
Liz Kammerd366c902021-06-03 13:43:01 -040098 bool_prop = False,
Jingwen Chenb4628eb2021-04-08 14:40:57 +000099 required = ["bar"],
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500100 string_prop = "",
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800101)`,
102 },
103 {
Liz Kammerd366c902021-06-03 13:43:01 -0400104 description: "list of strings",
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800105 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -0400106 name: "foo",
107 target_required: ["qux", "bazqux"],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800108}
Liz Kammerd366c902021-06-03 13:43:01 -0400109 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800110 expectedBazelTarget: `soong_module(
111 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500112 soong_module_name = "foo",
113 soong_module_type = "custom",
114 soong_module_variant = "",
115 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800116 ],
Liz Kammerd366c902021-06-03 13:43:01 -0400117 bool_prop = False,
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500118 string_prop = "",
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800119 target_required = [
120 "qux",
121 "bazqux",
122 ],
123)`,
124 },
125 {
Liz Kammerd366c902021-06-03 13:43:01 -0400126 description: "dist/dists",
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800127 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -0400128 name: "foo",
129 dist: {
130 targets: ["goal_foo"],
131 tag: ".foo",
132 },
133 dists: [{
134 targets: ["goal_bar"],
135 tag: ".bar",
136 }],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800137}
Liz Kammerd366c902021-06-03 13:43:01 -0400138 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800139 expectedBazelTarget: `soong_module(
140 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500141 soong_module_name = "foo",
142 soong_module_type = "custom",
143 soong_module_variant = "",
144 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800145 ],
Liz Kammerd366c902021-06-03 13:43:01 -0400146 bool_prop = False,
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800147 dist = {
148 "tag": ".foo",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000149 "targets": ["goal_foo"],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800150 },
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000151 dists = [{
152 "tag": ".bar",
153 "targets": ["goal_bar"],
154 }],
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500155 string_prop = "",
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800156)`,
157 },
158 {
Liz Kammerd366c902021-06-03 13:43:01 -0400159 description: "put it together",
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800160 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -0400161 name: "foo",
162 required: ["bar"],
163 target_required: ["qux", "bazqux"],
164 bool_prop: true,
165 owner: "custom_owner",
166 dists: [
167 {
168 tag: ".tag",
169 targets: ["my_goal"],
170 },
171 ],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800172}
Liz Kammerd366c902021-06-03 13:43:01 -0400173 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800174 expectedBazelTarget: `soong_module(
175 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500176 soong_module_name = "foo",
177 soong_module_type = "custom",
178 soong_module_variant = "",
179 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800180 ],
Liz Kammerd366c902021-06-03 13:43:01 -0400181 bool_prop = True,
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000182 dists = [{
183 "tag": ".tag",
184 "targets": ["my_goal"],
185 }],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800186 owner = "custom_owner",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000187 required = ["bar"],
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500188 string_prop = "",
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800189 target_required = [
190 "qux",
191 "bazqux",
192 ],
193)`,
194 },
195 }
196
197 dir := "."
198 for _, testCase := range testCases {
Liz Kammerd366c902021-06-03 13:43:01 -0400199 t.Run(testCase.description, func(t *testing.T) {
200 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
201 ctx := android.NewTestContext(config)
Jingwen Chen164e0862021-02-19 00:48:40 -0500202
Liz Kammerd366c902021-06-03 13:43:01 -0400203 ctx.RegisterModuleType("custom", customModuleFactory)
204 ctx.Register()
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800205
Liz Kammerd366c902021-06-03 13:43:01 -0400206 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
207 android.FailIfErrored(t, errs)
208 _, errs = ctx.PrepareBuildActions(config)
209 android.FailIfErrored(t, errs)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800210
Liz Kammerd366c902021-06-03 13:43:01 -0400211 codegenCtx := NewCodegenContext(config, *ctx.Context, QueryView)
Liz Kammer6eff3232021-08-26 08:37:59 -0400212 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
213 android.FailIfErrored(t, err)
Liz Kammerd366c902021-06-03 13:43:01 -0400214 if actualCount, expectedCount := len(bazelTargets), 1; actualCount != expectedCount {
215 t.Fatalf("Expected %d bazel target, got %d", expectedCount, actualCount)
216 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800217
Liz Kammerd366c902021-06-03 13:43:01 -0400218 actualBazelTarget := bazelTargets[0]
219 if actualBazelTarget.content != testCase.expectedBazelTarget {
220 t.Errorf(
221 "Expected generated Bazel target to be '%s', got '%s'",
222 testCase.expectedBazelTarget,
223 actualBazelTarget.content,
224 )
225 }
226 })
Jingwen Chen73850672020-12-14 08:25:34 -0500227 }
228}
229
230func TestGenerateBazelTargetModules(t *testing.T) {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000231 testCases := []bp2buildTestCase{
Jingwen Chen73850672020-12-14 08:25:34 -0500232 {
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500233 description: "string ptr props",
234 blueprint: `custom {
235 name: "foo",
236 string_ptr_prop: "",
237 bazel_module: { bp2build_available: true },
238}`,
239 expectedBazelTargets: []string{
240 makeBazelTarget("custom", "foo", attrNameToString{
241 "string_ptr_prop": `""`,
242 }),
243 },
244 },
245 {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400246 description: "string props",
Jingwen Chen5146ac02021-09-02 11:44:42 +0000247 blueprint: `custom {
Jingwen Chen73850672020-12-14 08:25:34 -0500248 name: "foo",
249 string_list_prop: ["a", "b"],
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500250 string_ptr_prop: "a",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500251 bazel_module: { bp2build_available: true },
Jingwen Chen73850672020-12-14 08:25:34 -0500252}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500253 expectedBazelTargets: []string{
254 makeBazelTarget("custom", "foo", attrNameToString{
255 "string_list_prop": `[
Jingwen Chen73850672020-12-14 08:25:34 -0500256 "a",
257 "b",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500258 ]`,
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500259 "string_ptr_prop": `"a"`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500260 }),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400261 },
Jingwen Chen73850672020-12-14 08:25:34 -0500262 },
Jingwen Chen58a12b82021-03-30 13:08:36 +0000263 {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400264 description: "control characters",
Jingwen Chen5146ac02021-09-02 11:44:42 +0000265 blueprint: `custom {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500266 name: "foo",
Jingwen Chen58a12b82021-03-30 13:08:36 +0000267 string_list_prop: ["\t", "\n"],
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500268 string_ptr_prop: "a\t\n\r",
Jingwen Chen58a12b82021-03-30 13:08:36 +0000269 bazel_module: { bp2build_available: true },
270}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500271 expectedBazelTargets: []string{
272 makeBazelTarget("custom", "foo", attrNameToString{
273 "string_list_prop": `[
Jingwen Chen58a12b82021-03-30 13:08:36 +0000274 "\t",
275 "\n",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500276 ]`,
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500277 "string_ptr_prop": `"a\t\n\r"`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500278 }),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400279 },
280 },
281 {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400282 description: "handles dep",
Jingwen Chen5146ac02021-09-02 11:44:42 +0000283 blueprint: `custom {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400284 name: "has_dep",
285 arch_paths: [":dep"],
286 bazel_module: { bp2build_available: true },
287}
288
289custom {
290 name: "dep",
291 arch_paths: ["abc"],
292 bazel_module: { bp2build_available: true },
293}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500294 expectedBazelTargets: []string{
295 makeBazelTarget("custom", "dep", attrNameToString{
296 "arch_paths": `["abc"]`,
297 }),
298 makeBazelTarget("custom", "has_dep", attrNameToString{
299 "arch_paths": `[":dep"]`,
300 }),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400301 },
302 },
303 {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400304 description: "arch-variant srcs",
Jingwen Chen5146ac02021-09-02 11:44:42 +0000305 blueprint: `custom {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400306 name: "arch_paths",
307 arch: {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400308 x86: { arch_paths: ["x86.txt"] },
309 x86_64: { arch_paths: ["x86_64.txt"] },
310 arm: { arch_paths: ["arm.txt"] },
311 arm64: { arch_paths: ["arm64.txt"] },
312 },
313 target: {
314 linux: { arch_paths: ["linux.txt"] },
315 bionic: { arch_paths: ["bionic.txt"] },
316 host: { arch_paths: ["host.txt"] },
317 not_windows: { arch_paths: ["not_windows.txt"] },
318 android: { arch_paths: ["android.txt"] },
319 linux_musl: { arch_paths: ["linux_musl.txt"] },
320 musl: { arch_paths: ["musl.txt"] },
321 linux_glibc: { arch_paths: ["linux_glibc.txt"] },
322 glibc: { arch_paths: ["glibc.txt"] },
323 linux_bionic: { arch_paths: ["linux_bionic.txt"] },
324 darwin: { arch_paths: ["darwin.txt"] },
325 windows: { arch_paths: ["windows.txt"] },
326 },
327 multilib: {
328 lib32: { arch_paths: ["lib32.txt"] },
329 lib64: { arch_paths: ["lib64.txt"] },
Liz Kammer4562a3b2021-04-21 18:15:34 -0400330 },
331 bazel_module: { bp2build_available: true },
332}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500333 expectedBazelTargets: []string{
334 makeBazelTarget("custom", "arch_paths", attrNameToString{
335 "arch_paths": `select({
Liz Kammerfdd72e62021-10-11 15:41:03 -0400336 "//build/bazel/platforms/arch:arm": [
337 "arm.txt",
338 "lib32.txt",
339 ],
340 "//build/bazel/platforms/arch:arm64": [
341 "arm64.txt",
342 "lib64.txt",
343 ],
344 "//build/bazel/platforms/arch:x86": [
345 "x86.txt",
346 "lib32.txt",
347 ],
348 "//build/bazel/platforms/arch:x86_64": [
349 "x86_64.txt",
350 "lib64.txt",
351 ],
352 "//conditions:default": [],
353 }) + select({
354 "//build/bazel/platforms/os:android": [
355 "linux.txt",
356 "bionic.txt",
357 "android.txt",
358 ],
359 "//build/bazel/platforms/os:darwin": [
360 "host.txt",
361 "darwin.txt",
362 "not_windows.txt",
363 ],
364 "//build/bazel/platforms/os:linux": [
365 "host.txt",
366 "linux.txt",
367 "glibc.txt",
368 "linux_glibc.txt",
369 "not_windows.txt",
370 ],
371 "//build/bazel/platforms/os:linux_bionic": [
372 "host.txt",
373 "linux.txt",
374 "bionic.txt",
375 "linux_bionic.txt",
376 "not_windows.txt",
377 ],
378 "//build/bazel/platforms/os:linux_musl": [
379 "host.txt",
380 "linux.txt",
381 "musl.txt",
382 "linux_musl.txt",
383 "not_windows.txt",
384 ],
385 "//build/bazel/platforms/os:windows": [
386 "host.txt",
387 "windows.txt",
388 ],
Liz Kammer4562a3b2021-04-21 18:15:34 -0400389 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500390 })`,
391 }),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400392 },
393 },
394 {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400395 description: "arch-variant deps",
Jingwen Chen5146ac02021-09-02 11:44:42 +0000396 blueprint: `custom {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400397 name: "has_dep",
398 arch: {
399 x86: {
400 arch_paths: [":dep"],
401 },
402 },
403 bazel_module: { bp2build_available: true },
404}
405
406custom {
407 name: "dep",
408 arch_paths: ["abc"],
409 bazel_module: { bp2build_available: true },
410}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500411 expectedBazelTargets: []string{
412 makeBazelTarget("custom", "dep", attrNameToString{
413 "arch_paths": `["abc"]`,
414 }),
415 makeBazelTarget("custom", "has_dep", attrNameToString{
416 "arch_paths": `select({
Liz Kammer4562a3b2021-04-21 18:15:34 -0400417 "//build/bazel/platforms/arch:x86": [":dep"],
418 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500419 })`,
420 }),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400421 },
Jingwen Chen58a12b82021-03-30 13:08:36 +0000422 },
Liz Kammer32a03392021-09-14 11:17:21 -0400423 {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400424 description: "embedded props",
Liz Kammer32a03392021-09-14 11:17:21 -0400425 blueprint: `custom {
426 name: "embedded_props",
427 embedded_prop: "abc",
428 bazel_module: { bp2build_available: true },
429}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500430 expectedBazelTargets: []string{
431 makeBazelTarget("custom", "embedded_props", attrNameToString{
432 "embedded_attr": `"abc"`,
433 }),
Liz Kammer32a03392021-09-14 11:17:21 -0400434 },
435 },
436 {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400437 description: "ptr to embedded props",
Liz Kammer32a03392021-09-14 11:17:21 -0400438 blueprint: `custom {
439 name: "ptr_to_embedded_props",
440 other_embedded_prop: "abc",
441 bazel_module: { bp2build_available: true },
442}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500443 expectedBazelTargets: []string{
444 makeBazelTarget("custom", "ptr_to_embedded_props", attrNameToString{
445 "other_embedded_attr": `"abc"`,
446 }),
Liz Kammer32a03392021-09-14 11:17:21 -0400447 },
448 },
Jingwen Chen73850672020-12-14 08:25:34 -0500449 }
450
451 dir := "."
452 for _, testCase := range testCases {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400453 t.Run(testCase.description, func(t *testing.T) {
454 config := android.TestConfig(buildDir, nil, testCase.blueprint, nil)
455 ctx := android.NewTestContext(config)
Jingwen Chen164e0862021-02-19 00:48:40 -0500456
Liz Kammerfdd72e62021-10-11 15:41:03 -0400457 registerCustomModuleForBp2buildConversion(ctx)
Jingwen Chen73850672020-12-14 08:25:34 -0500458
Liz Kammerfdd72e62021-10-11 15:41:03 -0400459 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
460 if errored(t, testCase, errs) {
461 return
462 }
463 _, errs = ctx.ResolveDependencies(config)
464 if errored(t, testCase, errs) {
465 return
466 }
Jingwen Chen73850672020-12-14 08:25:34 -0500467
Liz Kammerfdd72e62021-10-11 15:41:03 -0400468 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
469 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
470 android.FailIfErrored(t, err)
Jingwen Chen164e0862021-02-19 00:48:40 -0500471
Liz Kammerfdd72e62021-10-11 15:41:03 -0400472 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
473 t.Errorf("Expected %d bazel target, got %d", expectedCount, actualCount)
474 } else {
475 for i, expectedBazelTarget := range testCase.expectedBazelTargets {
476 actualBazelTarget := bazelTargets[i]
477 if actualBazelTarget.content != expectedBazelTarget {
478 t.Errorf(
479 "Expected generated Bazel target to be '%s', got '%s'",
480 expectedBazelTarget,
481 actualBazelTarget.content,
482 )
483 }
Liz Kammer4562a3b2021-04-21 18:15:34 -0400484 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500485 }
Liz Kammerfdd72e62021-10-11 15:41:03 -0400486 })
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800487 }
488}
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500489
Jingwen Chen40067de2021-01-26 21:58:43 -0500490func TestLoadStatements(t *testing.T) {
491 testCases := []struct {
492 bazelTargets BazelTargets
493 expectedLoadStatements string
494 }{
495 {
496 bazelTargets: BazelTargets{
497 BazelTarget{
498 name: "foo",
499 ruleClass: "cc_library",
500 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
501 },
502 },
503 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
504 },
505 {
506 bazelTargets: BazelTargets{
507 BazelTarget{
508 name: "foo",
509 ruleClass: "cc_library",
510 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
511 },
512 BazelTarget{
513 name: "bar",
514 ruleClass: "cc_library",
515 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
516 },
517 },
518 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
519 },
520 {
521 bazelTargets: BazelTargets{
522 BazelTarget{
523 name: "foo",
524 ruleClass: "cc_library",
525 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
526 },
527 BazelTarget{
528 name: "bar",
529 ruleClass: "cc_binary",
530 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
531 },
532 },
533 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")`,
534 },
535 {
536 bazelTargets: BazelTargets{
537 BazelTarget{
538 name: "foo",
539 ruleClass: "cc_library",
540 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
541 },
542 BazelTarget{
543 name: "bar",
544 ruleClass: "cc_binary",
545 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
546 },
547 BazelTarget{
548 name: "baz",
549 ruleClass: "java_binary",
550 bzlLoadLocation: "//build/bazel/rules:java.bzl",
551 },
552 },
553 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")
554load("//build/bazel/rules:java.bzl", "java_binary")`,
555 },
556 {
557 bazelTargets: BazelTargets{
558 BazelTarget{
559 name: "foo",
560 ruleClass: "cc_binary",
561 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
562 },
563 BazelTarget{
564 name: "bar",
565 ruleClass: "java_binary",
566 bzlLoadLocation: "//build/bazel/rules:java.bzl",
567 },
568 BazelTarget{
569 name: "baz",
570 ruleClass: "genrule",
571 // Note: no bzlLoadLocation for native rules
572 },
573 },
574 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary")
575load("//build/bazel/rules:java.bzl", "java_binary")`,
576 },
577 }
578
579 for _, testCase := range testCases {
580 actual := testCase.bazelTargets.LoadStatements()
581 expected := testCase.expectedLoadStatements
582 if actual != expected {
583 t.Fatalf("Expected load statements to be %s, got %s", expected, actual)
584 }
585 }
586
587}
588
589func TestGenerateBazelTargetModules_OneToMany_LoadedFromStarlark(t *testing.T) {
590 testCases := []struct {
591 bp string
592 expectedBazelTarget string
593 expectedBazelTargetCount int
594 expectedLoadStatements string
595 }{
596 {
597 bp: `custom {
598 name: "bar",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500599 bazel_module: { bp2build_available: true },
Jingwen Chen40067de2021-01-26 21:58:43 -0500600}`,
601 expectedBazelTarget: `my_library(
602 name = "bar",
603)
604
Jingwen Chen40067de2021-01-26 21:58:43 -0500605proto_library(
606 name = "bar_proto_library_deps",
Liz Kammer2ada09a2021-08-11 00:17:36 -0400607)
608
609my_proto_library(
610 name = "bar_my_proto_library_deps",
Jingwen Chen40067de2021-01-26 21:58:43 -0500611)`,
612 expectedBazelTargetCount: 3,
613 expectedLoadStatements: `load("//build/bazel/rules:proto.bzl", "my_proto_library", "proto_library")
614load("//build/bazel/rules:rules.bzl", "my_library")`,
615 },
616 }
617
618 dir := "."
619 for _, testCase := range testCases {
620 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
621 ctx := android.NewTestContext(config)
622 ctx.RegisterModuleType("custom", customModuleFactory)
Jingwen Chen12b4c272021-03-10 02:05:59 -0500623 ctx.RegisterBp2BuildMutator("custom", customBp2BuildMutatorFromStarlark)
Jingwen Chen40067de2021-01-26 21:58:43 -0500624 ctx.RegisterForBazelConversion()
625
626 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
627 android.FailIfErrored(t, errs)
628 _, errs = ctx.ResolveDependencies(config)
629 android.FailIfErrored(t, errs)
630
Jingwen Chen164e0862021-02-19 00:48:40 -0500631 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Liz Kammer6eff3232021-08-26 08:37:59 -0400632 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
633 android.FailIfErrored(t, err)
Jingwen Chen40067de2021-01-26 21:58:43 -0500634 if actualCount := len(bazelTargets); actualCount != testCase.expectedBazelTargetCount {
635 t.Fatalf("Expected %d bazel target, got %d", testCase.expectedBazelTargetCount, actualCount)
636 }
637
638 actualBazelTargets := bazelTargets.String()
639 if actualBazelTargets != testCase.expectedBazelTarget {
640 t.Errorf(
641 "Expected generated Bazel target to be '%s', got '%s'",
642 testCase.expectedBazelTarget,
643 actualBazelTargets,
644 )
645 }
646
647 actualLoadStatements := bazelTargets.LoadStatements()
648 if actualLoadStatements != testCase.expectedLoadStatements {
649 t.Errorf(
650 "Expected generated load statements to be '%s', got '%s'",
651 testCase.expectedLoadStatements,
652 actualLoadStatements,
653 )
654 }
655 }
656}
657
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500658func TestModuleTypeBp2Build(t *testing.T) {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000659 testCases := []bp2buildTestCase{
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500660 {
Liz Kammerebfcf672021-02-16 15:00:05 -0500661 description: "filegroup with does not specify srcs",
662 moduleTypeUnderTest: "filegroup",
663 moduleTypeUnderTestFactory: android.FileGroupFactory,
664 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000665 blueprint: `filegroup {
Liz Kammerebfcf672021-02-16 15:00:05 -0500666 name: "fg_foo",
667 bazel_module: { bp2build_available: true },
668}`,
669 expectedBazelTargets: []string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500670 makeBazelTarget("filegroup", "fg_foo", map[string]string{}),
Liz Kammerebfcf672021-02-16 15:00:05 -0500671 },
672 },
673 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500674 description: "filegroup with no 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: [],
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{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500684 makeBazelTarget("filegroup", "fg_foo", map[string]string{}),
Liz Kammer356f7d42021-01-26 09:18:53 -0500685 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500686 },
687 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500688 description: "filegroup with srcs",
689 moduleTypeUnderTest: "filegroup",
690 moduleTypeUnderTestFactory: android.FileGroupFactory,
691 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000692 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500693 name: "fg_foo",
694 srcs: ["a", "b"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500695 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500696}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500697 expectedBazelTargets: []string{
698 makeBazelTarget("filegroup", "fg_foo", map[string]string{
699 "srcs": `[
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500700 "a",
701 "b",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500702 ]`,
703 }),
Liz Kammer356f7d42021-01-26 09:18:53 -0500704 },
705 },
706 {
707 description: "filegroup with excludes srcs",
708 moduleTypeUnderTest: "filegroup",
709 moduleTypeUnderTestFactory: android.FileGroupFactory,
710 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000711 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500712 name: "fg_foo",
713 srcs: ["a", "b"],
714 exclude_srcs: ["a"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500715 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500716}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500717 expectedBazelTargets: []string{
718 makeBazelTarget("filegroup", "fg_foo", map[string]string{
719 "srcs": `["b"]`,
720 }),
Liz Kammer356f7d42021-01-26 09:18:53 -0500721 },
722 },
723 {
724 description: "filegroup with glob",
725 moduleTypeUnderTest: "filegroup",
726 moduleTypeUnderTestFactory: android.FileGroupFactory,
727 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000728 blueprint: `filegroup {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500729 name: "fg_foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500730 srcs: ["**/*.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500731 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500732}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500733 expectedBazelTargets: []string{
734 makeBazelTarget("filegroup", "fg_foo", map[string]string{
735 "srcs": `[
Liz Kammer356f7d42021-01-26 09:18:53 -0500736 "other/a.txt",
737 "other/b.txt",
738 "other/subdir/a.txt",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500739 ]`,
740 }),
Liz Kammer356f7d42021-01-26 09:18:53 -0500741 },
Jingwen Chen5146ac02021-09-02 11:44:42 +0000742 filesystem: map[string]string{
Liz Kammer356f7d42021-01-26 09:18:53 -0500743 "other/a.txt": "",
744 "other/b.txt": "",
745 "other/subdir/a.txt": "",
746 "other/file": "",
747 },
748 },
749 {
750 description: "filegroup with glob in subdir",
751 moduleTypeUnderTest: "filegroup",
752 moduleTypeUnderTestFactory: android.FileGroupFactory,
753 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500754 blueprint: ``,
755 dir: "other",
Jingwen Chen5146ac02021-09-02 11:44:42 +0000756 filesystem: map[string]string{
Liz Kammer356f7d42021-01-26 09:18:53 -0500757 "other/Android.bp": `filegroup {
758 name: "fg_foo",
759 srcs: ["**/*.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500760 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500761}`,
762 "other/a.txt": "",
763 "other/b.txt": "",
764 "other/subdir/a.txt": "",
765 "other/file": "",
766 },
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500767 expectedBazelTargets: []string{
768 makeBazelTarget("filegroup", "fg_foo", map[string]string{
769 "srcs": `[
770 "a.txt",
771 "b.txt",
772 "subdir/a.txt",
773 ]`,
774 }),
775 },
Liz Kammer356f7d42021-01-26 09:18:53 -0500776 },
777 {
778 description: "depends_on_other_dir_module",
779 moduleTypeUnderTest: "filegroup",
780 moduleTypeUnderTestFactory: android.FileGroupFactory,
781 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000782 blueprint: `filegroup {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500783 name: "fg_foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500784 srcs: [
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000785 ":foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500786 "c",
787 ],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500788 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500789}`,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000790 filesystem: map[string]string{
Liz Kammer356f7d42021-01-26 09:18:53 -0500791 "other/Android.bp": `filegroup {
792 name: "foo",
793 srcs: ["a", "b"],
Liz Kammer6eff3232021-08-26 08:37:59 -0400794 bazel_module: { bp2build_available: true },
795}`,
796 },
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500797 expectedBazelTargets: []string{
798 makeBazelTarget("filegroup", "fg_foo", map[string]string{
799 "srcs": `[
800 "//other:foo",
801 "c",
802 ]`,
803 }),
804 },
Liz Kammer6eff3232021-08-26 08:37:59 -0400805 },
806 {
807 description: "depends_on_other_unconverted_module_error",
808 moduleTypeUnderTest: "filegroup",
809 moduleTypeUnderTestFactory: android.FileGroupFactory,
810 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
811 unconvertedDepsMode: errorModulesUnconvertedDeps,
Liz Kammer6eff3232021-08-26 08:37:59 -0400812 filesystem: map[string]string{
813 "other/Android.bp": `filegroup {
814 name: "foo",
815 srcs: ["a", "b"],
Liz Kammer356f7d42021-01-26 09:18:53 -0500816}`,
817 },
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500818 blueprint: `filegroup {
819 name: "fg_foo",
820 srcs: [
821 ":foo",
822 "c",
823 ],
824 bazel_module: { bp2build_available: true },
825}`,
826 expectedErr: fmt.Errorf(`"fg_foo" depends on unconverted modules: foo`),
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500827 },
828 }
829
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500830 for _, testCase := range testCases {
Liz Kammer6eff3232021-08-26 08:37:59 -0400831 t.Run(testCase.description, func(t *testing.T) {
832 runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, testCase)
833 })
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500834 }
835}
Jingwen Chen041b1842021-02-01 00:23:25 -0500836
837type bp2buildMutator = func(android.TopDownMutatorContext)
838
Jingwen Chen12b4c272021-03-10 02:05:59 -0500839func TestAllowlistingBp2buildTargetsExplicitly(t *testing.T) {
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500840 testCases := []struct {
841 moduleTypeUnderTest string
842 moduleTypeUnderTestFactory android.ModuleFactory
843 moduleTypeUnderTestBp2BuildMutator bp2buildMutator
844 bp string
845 expectedCount int
846 description string
847 }{
848 {
849 description: "explicitly unavailable",
850 moduleTypeUnderTest: "filegroup",
851 moduleTypeUnderTestFactory: android.FileGroupFactory,
852 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
853 bp: `filegroup {
854 name: "foo",
855 srcs: ["a", "b"],
856 bazel_module: { bp2build_available: false },
857}`,
858 expectedCount: 0,
859 },
860 {
861 description: "implicitly unavailable",
862 moduleTypeUnderTest: "filegroup",
863 moduleTypeUnderTestFactory: android.FileGroupFactory,
864 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
865 bp: `filegroup {
866 name: "foo",
867 srcs: ["a", "b"],
868}`,
869 expectedCount: 0,
870 },
871 {
872 description: "explicitly available",
873 moduleTypeUnderTest: "filegroup",
874 moduleTypeUnderTestFactory: android.FileGroupFactory,
875 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
876 bp: `filegroup {
877 name: "foo",
878 srcs: ["a", "b"],
879 bazel_module: { bp2build_available: true },
880}`,
881 expectedCount: 1,
882 },
883 {
884 description: "generates more than 1 target if needed",
885 moduleTypeUnderTest: "custom",
886 moduleTypeUnderTestFactory: customModuleFactory,
887 moduleTypeUnderTestBp2BuildMutator: customBp2BuildMutatorFromStarlark,
888 bp: `custom {
889 name: "foo",
890 bazel_module: { bp2build_available: true },
891}`,
892 expectedCount: 3,
893 },
894 }
895
896 dir := "."
897 for _, testCase := range testCases {
Liz Kammer2ada09a2021-08-11 00:17:36 -0400898 t.Run(testCase.description, func(t *testing.T) {
899 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
900 ctx := android.NewTestContext(config)
901 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
902 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
903 ctx.RegisterForBazelConversion()
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500904
Liz Kammer2ada09a2021-08-11 00:17:36 -0400905 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
906 android.FailIfErrored(t, errs)
907 _, errs = ctx.ResolveDependencies(config)
908 android.FailIfErrored(t, errs)
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500909
Liz Kammer2ada09a2021-08-11 00:17:36 -0400910 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Liz Kammer6eff3232021-08-26 08:37:59 -0400911 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
912 android.FailIfErrored(t, err)
Liz Kammer2ada09a2021-08-11 00:17:36 -0400913 if actualCount := len(bazelTargets); actualCount != testCase.expectedCount {
914 t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, testCase.expectedCount, actualCount)
915 }
916 })
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500917 }
918}
Liz Kammerba3ea162021-02-17 13:22:03 -0500919
Jingwen Chen12b4c272021-03-10 02:05:59 -0500920func TestAllowlistingBp2buildTargetsWithConfig(t *testing.T) {
921 testCases := []struct {
922 moduleTypeUnderTest string
923 moduleTypeUnderTestFactory android.ModuleFactory
924 moduleTypeUnderTestBp2BuildMutator bp2buildMutator
925 expectedCount map[string]int
926 description string
927 bp2buildConfig android.Bp2BuildConfig
928 checkDir string
929 fs map[string]string
930 }{
931 {
932 description: "test bp2build config package and subpackages config",
933 moduleTypeUnderTest: "filegroup",
934 moduleTypeUnderTestFactory: android.FileGroupFactory,
935 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
936 expectedCount: map[string]int{
937 "migrated": 1,
938 "migrated/but_not_really": 0,
939 "migrated/but_not_really/but_really": 1,
940 "not_migrated": 0,
941 "also_not_migrated": 0,
942 },
943 bp2buildConfig: android.Bp2BuildConfig{
944 "migrated": android.Bp2BuildDefaultTrueRecursively,
945 "migrated/but_not_really": android.Bp2BuildDefaultFalse,
946 "not_migrated": android.Bp2BuildDefaultFalse,
947 },
948 fs: map[string]string{
949 "migrated/Android.bp": `filegroup { name: "a" }`,
950 "migrated/but_not_really/Android.bp": `filegroup { name: "b" }`,
951 "migrated/but_not_really/but_really/Android.bp": `filegroup { name: "c" }`,
952 "not_migrated/Android.bp": `filegroup { name: "d" }`,
953 "also_not_migrated/Android.bp": `filegroup { name: "e" }`,
954 },
955 },
956 {
957 description: "test bp2build config opt-in and opt-out",
958 moduleTypeUnderTest: "filegroup",
959 moduleTypeUnderTestFactory: android.FileGroupFactory,
960 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
961 expectedCount: map[string]int{
962 "package-opt-in": 2,
963 "package-opt-in/subpackage": 0,
964 "package-opt-out": 1,
965 "package-opt-out/subpackage": 0,
966 },
967 bp2buildConfig: android.Bp2BuildConfig{
968 "package-opt-in": android.Bp2BuildDefaultFalse,
969 "package-opt-out": android.Bp2BuildDefaultTrueRecursively,
970 },
971 fs: map[string]string{
972 "package-opt-in/Android.bp": `
973filegroup { name: "opt-in-a" }
974filegroup { name: "opt-in-b", bazel_module: { bp2build_available: true } }
975filegroup { name: "opt-in-c", bazel_module: { bp2build_available: true } }
976`,
977
978 "package-opt-in/subpackage/Android.bp": `
979filegroup { name: "opt-in-d" } // parent package not configured to DefaultTrueRecursively
980`,
981
982 "package-opt-out/Android.bp": `
983filegroup { name: "opt-out-a" }
984filegroup { name: "opt-out-b", bazel_module: { bp2build_available: false } }
985filegroup { name: "opt-out-c", bazel_module: { bp2build_available: false } }
986`,
987
988 "package-opt-out/subpackage/Android.bp": `
989filegroup { name: "opt-out-g", bazel_module: { bp2build_available: false } }
990filegroup { name: "opt-out-h", bazel_module: { bp2build_available: false } }
991`,
992 },
993 },
994 }
995
996 dir := "."
997 for _, testCase := range testCases {
998 fs := make(map[string][]byte)
999 toParse := []string{
1000 "Android.bp",
1001 }
1002 for f, content := range testCase.fs {
1003 if strings.HasSuffix(f, "Android.bp") {
1004 toParse = append(toParse, f)
1005 }
1006 fs[f] = []byte(content)
1007 }
1008 config := android.TestConfig(buildDir, nil, "", fs)
1009 ctx := android.NewTestContext(config)
1010 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
1011 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1012 ctx.RegisterBp2BuildConfig(testCase.bp2buildConfig)
1013 ctx.RegisterForBazelConversion()
1014
1015 _, errs := ctx.ParseFileList(dir, toParse)
1016 android.FailIfErrored(t, errs)
1017 _, errs = ctx.ResolveDependencies(config)
1018 android.FailIfErrored(t, errs)
1019
1020 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
1021
1022 // For each directory, test that the expected number of generated targets is correct.
1023 for dir, expectedCount := range testCase.expectedCount {
Liz Kammer6eff3232021-08-26 08:37:59 -04001024 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
1025 android.FailIfErrored(t, err)
Jingwen Chen12b4c272021-03-10 02:05:59 -05001026 if actualCount := len(bazelTargets); actualCount != expectedCount {
1027 t.Fatalf(
1028 "%s: Expected %d bazel target for %s package, got %d",
1029 testCase.description,
1030 expectedCount,
1031 dir,
1032 actualCount)
1033 }
1034
1035 }
1036 }
1037}
1038
Liz Kammerba3ea162021-02-17 13:22:03 -05001039func TestCombineBuildFilesBp2buildTargets(t *testing.T) {
Jingwen Chen5146ac02021-09-02 11:44:42 +00001040 testCases := []bp2buildTestCase{
Liz Kammerba3ea162021-02-17 13:22:03 -05001041 {
1042 description: "filegroup bazel_module.label",
1043 moduleTypeUnderTest: "filegroup",
1044 moduleTypeUnderTestFactory: android.FileGroupFactory,
1045 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +00001046 blueprint: `filegroup {
Liz Kammerba3ea162021-02-17 13:22:03 -05001047 name: "fg_foo",
1048 bazel_module: { label: "//other:fg_foo" },
1049}`,
1050 expectedBazelTargets: []string{
1051 `// BUILD file`,
1052 },
Jingwen Chen5146ac02021-09-02 11:44:42 +00001053 filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -05001054 "other/BUILD.bazel": `// BUILD file`,
1055 },
1056 },
1057 {
1058 description: "multiple bazel_module.label same BUILD",
1059 moduleTypeUnderTest: "filegroup",
1060 moduleTypeUnderTestFactory: android.FileGroupFactory,
1061 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +00001062 blueprint: `filegroup {
Jingwen Chenc63677b2021-06-17 05:43:19 +00001063 name: "fg_foo",
1064 bazel_module: { label: "//other:fg_foo" },
1065 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001066
Jingwen Chenc63677b2021-06-17 05:43:19 +00001067 filegroup {
1068 name: "foo",
1069 bazel_module: { label: "//other:foo" },
1070 }`,
Liz Kammerba3ea162021-02-17 13:22:03 -05001071 expectedBazelTargets: []string{
1072 `// BUILD file`,
1073 },
Jingwen Chen5146ac02021-09-02 11:44:42 +00001074 filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -05001075 "other/BUILD.bazel": `// BUILD file`,
1076 },
1077 },
1078 {
Jingwen Chenc63677b2021-06-17 05:43:19 +00001079 description: "filegroup bazel_module.label and bp2build in subdir",
Liz Kammerba3ea162021-02-17 13:22:03 -05001080 moduleTypeUnderTest: "filegroup",
1081 moduleTypeUnderTestFactory: android.FileGroupFactory,
1082 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chenc63677b2021-06-17 05:43:19 +00001083 dir: "other",
Jingwen Chen5146ac02021-09-02 11:44:42 +00001084 blueprint: ``,
1085 filesystem: map[string]string{
Jingwen Chenc63677b2021-06-17 05:43:19 +00001086 "other/Android.bp": `filegroup {
1087 name: "fg_foo",
1088 bazel_module: {
1089 bp2build_available: true,
1090 },
1091 }
1092 filegroup {
1093 name: "fg_bar",
1094 bazel_module: {
1095 label: "//other:fg_bar"
1096 },
1097 }`,
1098 "other/BUILD.bazel": `// definition for fg_bar`,
1099 },
Liz Kammerba3ea162021-02-17 13:22:03 -05001100 expectedBazelTargets: []string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001101 makeBazelTarget("filegroup", "fg_foo", map[string]string{}),
1102 `// definition for fg_bar`,
Liz Kammerba3ea162021-02-17 13:22:03 -05001103 },
1104 },
1105 {
1106 description: "filegroup bazel_module.label and filegroup bp2build",
1107 moduleTypeUnderTest: "filegroup",
1108 moduleTypeUnderTestFactory: android.FileGroupFactory,
1109 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001110 filesystem: map[string]string{
1111 "other/BUILD.bazel": `// BUILD file`,
1112 },
Jingwen Chen5146ac02021-09-02 11:44:42 +00001113 blueprint: `filegroup {
Jingwen Chenc63677b2021-06-17 05:43:19 +00001114 name: "fg_foo",
1115 bazel_module: {
1116 label: "//other:fg_foo",
1117 },
1118 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001119
Jingwen Chenc63677b2021-06-17 05:43:19 +00001120 filegroup {
1121 name: "fg_bar",
1122 bazel_module: {
1123 bp2build_available: true,
1124 },
1125 }`,
Liz Kammerba3ea162021-02-17 13:22:03 -05001126 expectedBazelTargets: []string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001127 makeBazelTarget("filegroup", "fg_bar", map[string]string{}),
Liz Kammerba3ea162021-02-17 13:22:03 -05001128 `// BUILD file`,
1129 },
Liz Kammerba3ea162021-02-17 13:22:03 -05001130 },
1131 }
1132
1133 dir := "."
1134 for _, testCase := range testCases {
Jingwen Chen49109762021-05-25 05:16:48 +00001135 t.Run(testCase.description, func(t *testing.T) {
1136 fs := make(map[string][]byte)
1137 toParse := []string{
1138 "Android.bp",
Liz Kammerba3ea162021-02-17 13:22:03 -05001139 }
Jingwen Chen5146ac02021-09-02 11:44:42 +00001140 for f, content := range testCase.filesystem {
Jingwen Chen49109762021-05-25 05:16:48 +00001141 if strings.HasSuffix(f, "Android.bp") {
1142 toParse = append(toParse, f)
1143 }
1144 fs[f] = []byte(content)
1145 }
Jingwen Chen5146ac02021-09-02 11:44:42 +00001146 config := android.TestConfig(buildDir, nil, testCase.blueprint, fs)
Jingwen Chen49109762021-05-25 05:16:48 +00001147 ctx := android.NewTestContext(config)
1148 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Jingwen Chen49109762021-05-25 05:16:48 +00001149 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1150 ctx.RegisterForBazelConversion()
Liz Kammerba3ea162021-02-17 13:22:03 -05001151
Jingwen Chen49109762021-05-25 05:16:48 +00001152 _, errs := ctx.ParseFileList(dir, toParse)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001153 if errored(t, testCase, errs) {
Jingwen Chen49109762021-05-25 05:16:48 +00001154 return
1155 }
1156 _, errs = ctx.ResolveDependencies(config)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001157 if errored(t, testCase, errs) {
Jingwen Chen49109762021-05-25 05:16:48 +00001158 return
1159 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001160
Jingwen Chen49109762021-05-25 05:16:48 +00001161 checkDir := dir
1162 if testCase.dir != "" {
1163 checkDir = testCase.dir
1164 }
Liz Kammer6eff3232021-08-26 08:37:59 -04001165 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
1166 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, checkDir)
1167 android.FailIfErrored(t, err)
Jingwen Chen49109762021-05-25 05:16:48 +00001168 bazelTargets.sort()
1169 actualCount := len(bazelTargets)
1170 expectedCount := len(testCase.expectedBazelTargets)
1171 if actualCount != expectedCount {
1172 t.Errorf("Expected %d bazel target, got %d\n%s", expectedCount, actualCount, bazelTargets)
1173 }
1174 if !strings.Contains(bazelTargets.String(), "Section: Handcrafted targets. ") {
1175 t.Errorf("Expected string representation of bazelTargets to contain handcrafted section header.")
1176 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001177 for i, target := range bazelTargets {
Jingwen Chen49109762021-05-25 05:16:48 +00001178 actualContent := target.content
1179 expectedContent := testCase.expectedBazelTargets[i]
1180 if expectedContent != actualContent {
Liz Kammerba3ea162021-02-17 13:22:03 -05001181 t.Errorf(
Jingwen Chen49109762021-05-25 05:16:48 +00001182 "Expected generated Bazel target to be '%s', got '%s'",
1183 expectedContent,
1184 actualContent,
Liz Kammerba3ea162021-02-17 13:22:03 -05001185 )
1186 }
1187 }
Jingwen Chen49109762021-05-25 05:16:48 +00001188 })
Liz Kammerba3ea162021-02-17 13:22:03 -05001189 }
1190}
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001191
1192func TestGlobExcludeSrcs(t *testing.T) {
Jingwen Chen5146ac02021-09-02 11:44:42 +00001193 testCases := []bp2buildTestCase{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001194 {
1195 description: "filegroup top level exclude_srcs",
1196 moduleTypeUnderTest: "filegroup",
1197 moduleTypeUnderTestFactory: android.FileGroupFactory,
1198 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +00001199 blueprint: `filegroup {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001200 name: "fg_foo",
1201 srcs: ["**/*.txt"],
1202 exclude_srcs: ["c.txt"],
1203 bazel_module: { bp2build_available: true },
1204}`,
Jingwen Chen5146ac02021-09-02 11:44:42 +00001205 filesystem: map[string]string{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001206 "a.txt": "",
1207 "b.txt": "",
1208 "c.txt": "",
1209 "dir/Android.bp": "",
1210 "dir/e.txt": "",
1211 "dir/f.txt": "",
1212 },
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001213 expectedBazelTargets: []string{
1214 makeBazelTarget("filegroup", "fg_foo", map[string]string{
1215 "srcs": `[
1216 "a.txt",
1217 "b.txt",
1218 "//dir:e.txt",
1219 "//dir:f.txt",
1220 ]`,
1221 }),
1222 },
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001223 },
1224 {
1225 description: "filegroup in subdir exclude_srcs",
1226 moduleTypeUnderTest: "filegroup",
1227 moduleTypeUnderTestFactory: android.FileGroupFactory,
1228 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +00001229 blueprint: "",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001230 dir: "dir",
Jingwen Chen5146ac02021-09-02 11:44:42 +00001231 filesystem: map[string]string{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001232 "dir/Android.bp": `filegroup {
1233 name: "fg_foo",
1234 srcs: ["**/*.txt"],
1235 exclude_srcs: ["b.txt"],
1236 bazel_module: { bp2build_available: true },
1237}
1238`,
1239 "dir/a.txt": "",
1240 "dir/b.txt": "",
1241 "dir/subdir/Android.bp": "",
1242 "dir/subdir/e.txt": "",
1243 "dir/subdir/f.txt": "",
1244 },
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001245 expectedBazelTargets: []string{
1246 makeBazelTarget("filegroup", "fg_foo", map[string]string{
1247 "srcs": `[
Liz Kammer9abd62d2021-05-21 08:37:59 -04001248 "a.txt",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001249 "//dir/subdir:e.txt",
1250 "//dir/subdir:f.txt",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001251 ]`,
1252 }),
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001253 },
1254 },
1255 }
1256
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001257 for _, testCase := range testCases {
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001258 t.Run(testCase.description, func(t *testing.T) {
1259 runBp2BuildTestCaseSimple(t, testCase)
1260 })
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001261 }
1262}
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001263
1264func TestCommonBp2BuildModuleAttrs(t *testing.T) {
1265 testCases := []bp2buildTestCase{
1266 {
1267 description: "Required into data test",
1268 moduleTypeUnderTest: "filegroup",
1269 moduleTypeUnderTestFactory: android.FileGroupFactory,
1270 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001271 blueprint: simpleModuleDoNotConvertBp2build("filegroup", "reqd") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001272filegroup {
1273 name: "fg_foo",
1274 required: ["reqd"],
1275 bazel_module: { bp2build_available: true },
1276}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001277 expectedBazelTargets: []string{
1278 makeBazelTarget("filegroup", "fg_foo", map[string]string{
1279 "data": `[":reqd"]`,
1280 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001281 },
1282 },
1283 {
1284 description: "Required via arch into data test",
1285 moduleTypeUnderTest: "python_library",
1286 moduleTypeUnderTestFactory: python.PythonLibraryFactory,
1287 moduleTypeUnderTestBp2BuildMutator: python.PythonLibraryBp2Build,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001288 blueprint: simpleModuleDoNotConvertBp2build("python_library", "reqdx86") +
1289 simpleModuleDoNotConvertBp2build("python_library", "reqdarm") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001290python_library {
1291 name: "fg_foo",
1292 arch: {
1293 arm: {
1294 required: ["reqdarm"],
1295 },
1296 x86: {
1297 required: ["reqdx86"],
1298 },
1299 },
1300 bazel_module: { bp2build_available: true },
1301}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001302 expectedBazelTargets: []string{
1303 makeBazelTarget("py_library", "fg_foo", map[string]string{
1304 "data": `select({
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001305 "//build/bazel/platforms/arch:arm": [":reqdarm"],
1306 "//build/bazel/platforms/arch:x86": [":reqdx86"],
1307 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001308 })`,
1309 "srcs_version": `"PY3"`,
1310 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001311 },
1312 },
1313 {
1314 description: "Required appended to data test",
1315 moduleTypeUnderTest: "python_library",
1316 moduleTypeUnderTestFactory: python.PythonLibraryFactory,
1317 moduleTypeUnderTestBp2BuildMutator: python.PythonLibraryBp2Build,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001318 filesystem: map[string]string{
1319 "data.bin": "",
1320 "src.py": "",
1321 },
1322 blueprint: simpleModuleDoNotConvertBp2build("python_library", "reqd") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001323python_library {
1324 name: "fg_foo",
1325 data: ["data.bin"],
1326 required: ["reqd"],
1327 bazel_module: { bp2build_available: true },
1328}`,
1329 expectedBazelTargets: []string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001330 makeBazelTarget("py_library", "fg_foo", map[string]string{
1331 "data": `[
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001332 "data.bin",
1333 ":reqd",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001334 ]`,
1335 "srcs_version": `"PY3"`,
1336 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001337 },
1338 },
1339 {
1340 description: "All props-to-attrs at once together test",
1341 moduleTypeUnderTest: "filegroup",
1342 moduleTypeUnderTestFactory: android.FileGroupFactory,
1343 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001344 blueprint: simpleModuleDoNotConvertBp2build("filegroup", "reqd") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001345filegroup {
1346 name: "fg_foo",
1347 required: ["reqd"],
1348 bazel_module: { bp2build_available: true },
1349}`,
1350 expectedBazelTargets: []string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001351 makeBazelTarget("filegroup", "fg_foo", map[string]string{
1352 "data": `[":reqd"]`,
1353 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001354 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001355 },
1356 }
1357
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001358 for _, tc := range testCases {
1359 t.Run(tc.description, func(t *testing.T) {
1360 runBp2BuildTestCaseSimple(t, tc)
1361 })
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001362 }
1363}