blob: 1440b6fce51e251ba0724a02daa9f0fa1f3ee8c1 [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 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400248 name: "foo",
Jingwen Chen73850672020-12-14 08:25:34 -0500249 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 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400473 t.Errorf("Expected %d bazel target (%s),\ngot %d (%s)", expectedCount, testCase.expectedBazelTargets, actualCount, bazelTargets)
Liz Kammerfdd72e62021-10-11 15:41:03 -0400474 } 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",
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400599 one_to_many_prop: true,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500600 bazel_module: { bp2build_available: true },
Jingwen Chen40067de2021-01-26 21:58:43 -0500601}`,
602 expectedBazelTarget: `my_library(
603 name = "bar",
604)
605
Jingwen Chen40067de2021-01-26 21:58:43 -0500606proto_library(
607 name = "bar_proto_library_deps",
Liz Kammer2ada09a2021-08-11 00:17:36 -0400608)
609
610my_proto_library(
611 name = "bar_my_proto_library_deps",
Jingwen Chen40067de2021-01-26 21:58:43 -0500612)`,
613 expectedBazelTargetCount: 3,
614 expectedLoadStatements: `load("//build/bazel/rules:proto.bzl", "my_proto_library", "proto_library")
615load("//build/bazel/rules:rules.bzl", "my_library")`,
616 },
617 }
618
619 dir := "."
620 for _, testCase := range testCases {
621 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
622 ctx := android.NewTestContext(config)
623 ctx.RegisterModuleType("custom", customModuleFactory)
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 Kammerbe46fcc2021-11-01 15:32:43 -0400661 description: "filegroup with does not specify srcs",
662 moduleTypeUnderTest: "filegroup",
663 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000664 blueprint: `filegroup {
Liz Kammerebfcf672021-02-16 15:00:05 -0500665 name: "fg_foo",
666 bazel_module: { bp2build_available: true },
667}`,
668 expectedBazelTargets: []string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500669 makeBazelTarget("filegroup", "fg_foo", map[string]string{}),
Liz Kammerebfcf672021-02-16 15:00:05 -0500670 },
671 },
672 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400673 description: "filegroup with no srcs",
674 moduleTypeUnderTest: "filegroup",
675 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000676 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500677 name: "fg_foo",
678 srcs: [],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500679 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500680}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500681 expectedBazelTargets: []string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500682 makeBazelTarget("filegroup", "fg_foo", map[string]string{}),
Liz Kammer356f7d42021-01-26 09:18:53 -0500683 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500684 },
685 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400686 description: "filegroup with srcs",
687 moduleTypeUnderTest: "filegroup",
688 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000689 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500690 name: "fg_foo",
691 srcs: ["a", "b"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500692 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500693}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500694 expectedBazelTargets: []string{
695 makeBazelTarget("filegroup", "fg_foo", map[string]string{
696 "srcs": `[
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500697 "a",
698 "b",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500699 ]`,
700 }),
Liz Kammer356f7d42021-01-26 09:18:53 -0500701 },
702 },
703 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400704 description: "filegroup with excludes srcs",
705 moduleTypeUnderTest: "filegroup",
706 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000707 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500708 name: "fg_foo",
709 srcs: ["a", "b"],
710 exclude_srcs: ["a"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500711 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500712}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500713 expectedBazelTargets: []string{
714 makeBazelTarget("filegroup", "fg_foo", map[string]string{
715 "srcs": `["b"]`,
716 }),
Liz Kammer356f7d42021-01-26 09:18:53 -0500717 },
718 },
719 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400720 description: "filegroup with glob",
721 moduleTypeUnderTest: "filegroup",
722 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000723 blueprint: `filegroup {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500724 name: "fg_foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500725 srcs: ["**/*.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500726 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500727}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500728 expectedBazelTargets: []string{
729 makeBazelTarget("filegroup", "fg_foo", map[string]string{
730 "srcs": `[
Liz Kammer356f7d42021-01-26 09:18:53 -0500731 "other/a.txt",
732 "other/b.txt",
733 "other/subdir/a.txt",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500734 ]`,
735 }),
Liz Kammer356f7d42021-01-26 09:18:53 -0500736 },
Jingwen Chen5146ac02021-09-02 11:44:42 +0000737 filesystem: map[string]string{
Liz Kammer356f7d42021-01-26 09:18:53 -0500738 "other/a.txt": "",
739 "other/b.txt": "",
740 "other/subdir/a.txt": "",
741 "other/file": "",
742 },
743 },
744 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400745 description: "filegroup with glob in subdir",
746 moduleTypeUnderTest: "filegroup",
747 moduleTypeUnderTestFactory: android.FileGroupFactory,
748 dir: "other",
Jingwen Chen5146ac02021-09-02 11:44:42 +0000749 filesystem: map[string]string{
Liz Kammer356f7d42021-01-26 09:18:53 -0500750 "other/Android.bp": `filegroup {
751 name: "fg_foo",
752 srcs: ["**/*.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500753 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500754}`,
755 "other/a.txt": "",
756 "other/b.txt": "",
757 "other/subdir/a.txt": "",
758 "other/file": "",
759 },
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500760 expectedBazelTargets: []string{
761 makeBazelTarget("filegroup", "fg_foo", map[string]string{
762 "srcs": `[
763 "a.txt",
764 "b.txt",
765 "subdir/a.txt",
766 ]`,
767 }),
768 },
Liz Kammer356f7d42021-01-26 09:18:53 -0500769 },
770 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400771 description: "depends_on_other_dir_module",
772 moduleTypeUnderTest: "filegroup",
773 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000774 blueprint: `filegroup {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500775 name: "fg_foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500776 srcs: [
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000777 ":foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500778 "c",
779 ],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500780 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500781}`,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000782 filesystem: map[string]string{
Liz Kammer356f7d42021-01-26 09:18:53 -0500783 "other/Android.bp": `filegroup {
784 name: "foo",
785 srcs: ["a", "b"],
Liz Kammer6eff3232021-08-26 08:37:59 -0400786 bazel_module: { bp2build_available: true },
787}`,
788 },
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500789 expectedBazelTargets: []string{
790 makeBazelTarget("filegroup", "fg_foo", map[string]string{
791 "srcs": `[
792 "//other:foo",
793 "c",
794 ]`,
795 }),
796 },
Liz Kammer6eff3232021-08-26 08:37:59 -0400797 },
798 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400799 description: "depends_on_other_unconverted_module_error",
800 moduleTypeUnderTest: "filegroup",
801 moduleTypeUnderTestFactory: android.FileGroupFactory,
802 unconvertedDepsMode: errorModulesUnconvertedDeps,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500803 blueprint: `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400804 name: "foobar",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500805 srcs: [
806 ":foo",
807 "c",
808 ],
809 bazel_module: { bp2build_available: true },
810}`,
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400811 expectedErr: fmt.Errorf(`"foobar" depends on unconverted modules: foo`),
812 filesystem: map[string]string{
813 "other/Android.bp": `filegroup {
814 name: "foo",
815 srcs: ["a", "b"],
816}`,
817 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500818 },
819 }
820
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500821 for _, testCase := range testCases {
Liz Kammer6eff3232021-08-26 08:37:59 -0400822 t.Run(testCase.description, func(t *testing.T) {
823 runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, testCase)
824 })
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500825 }
826}
Jingwen Chen041b1842021-02-01 00:23:25 -0500827
828type bp2buildMutator = func(android.TopDownMutatorContext)
829
Jingwen Chen12b4c272021-03-10 02:05:59 -0500830func TestAllowlistingBp2buildTargetsExplicitly(t *testing.T) {
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500831 testCases := []struct {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400832 moduleTypeUnderTest string
833 moduleTypeUnderTestFactory android.ModuleFactory
834 bp string
835 expectedCount int
836 description string
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500837 }{
838 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400839 description: "explicitly unavailable",
840 moduleTypeUnderTest: "filegroup",
841 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500842 bp: `filegroup {
843 name: "foo",
844 srcs: ["a", "b"],
845 bazel_module: { bp2build_available: false },
846}`,
847 expectedCount: 0,
848 },
849 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400850 description: "implicitly unavailable",
851 moduleTypeUnderTest: "filegroup",
852 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500853 bp: `filegroup {
854 name: "foo",
855 srcs: ["a", "b"],
856}`,
857 expectedCount: 0,
858 },
859 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400860 description: "explicitly available",
861 moduleTypeUnderTest: "filegroup",
862 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500863 bp: `filegroup {
864 name: "foo",
865 srcs: ["a", "b"],
866 bazel_module: { bp2build_available: true },
867}`,
868 expectedCount: 1,
869 },
870 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400871 description: "generates more than 1 target if needed",
872 moduleTypeUnderTest: "custom",
873 moduleTypeUnderTestFactory: customModuleFactory,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500874 bp: `custom {
875 name: "foo",
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400876 one_to_many_prop: true,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500877 bazel_module: { bp2build_available: true },
878}`,
879 expectedCount: 3,
880 },
881 }
882
883 dir := "."
884 for _, testCase := range testCases {
Liz Kammer2ada09a2021-08-11 00:17:36 -0400885 t.Run(testCase.description, func(t *testing.T) {
886 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
887 ctx := android.NewTestContext(config)
888 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Liz Kammer2ada09a2021-08-11 00:17:36 -0400889 ctx.RegisterForBazelConversion()
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500890
Liz Kammer2ada09a2021-08-11 00:17:36 -0400891 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
892 android.FailIfErrored(t, errs)
893 _, errs = ctx.ResolveDependencies(config)
894 android.FailIfErrored(t, errs)
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500895
Liz Kammer2ada09a2021-08-11 00:17:36 -0400896 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Liz Kammer6eff3232021-08-26 08:37:59 -0400897 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
898 android.FailIfErrored(t, err)
Liz Kammer2ada09a2021-08-11 00:17:36 -0400899 if actualCount := len(bazelTargets); actualCount != testCase.expectedCount {
900 t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, testCase.expectedCount, actualCount)
901 }
902 })
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500903 }
904}
Liz Kammerba3ea162021-02-17 13:22:03 -0500905
Jingwen Chen12b4c272021-03-10 02:05:59 -0500906func TestAllowlistingBp2buildTargetsWithConfig(t *testing.T) {
907 testCases := []struct {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400908 moduleTypeUnderTest string
909 moduleTypeUnderTestFactory android.ModuleFactory
910 expectedCount map[string]int
911 description string
912 bp2buildConfig android.Bp2BuildConfig
913 checkDir string
914 fs map[string]string
Jingwen Chen12b4c272021-03-10 02:05:59 -0500915 }{
916 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400917 description: "test bp2build config package and subpackages config",
918 moduleTypeUnderTest: "filegroup",
919 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen12b4c272021-03-10 02:05:59 -0500920 expectedCount: map[string]int{
921 "migrated": 1,
922 "migrated/but_not_really": 0,
923 "migrated/but_not_really/but_really": 1,
924 "not_migrated": 0,
925 "also_not_migrated": 0,
926 },
927 bp2buildConfig: android.Bp2BuildConfig{
928 "migrated": android.Bp2BuildDefaultTrueRecursively,
929 "migrated/but_not_really": android.Bp2BuildDefaultFalse,
930 "not_migrated": android.Bp2BuildDefaultFalse,
931 },
932 fs: map[string]string{
933 "migrated/Android.bp": `filegroup { name: "a" }`,
934 "migrated/but_not_really/Android.bp": `filegroup { name: "b" }`,
935 "migrated/but_not_really/but_really/Android.bp": `filegroup { name: "c" }`,
936 "not_migrated/Android.bp": `filegroup { name: "d" }`,
937 "also_not_migrated/Android.bp": `filegroup { name: "e" }`,
938 },
939 },
940 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400941 description: "test bp2build config opt-in and opt-out",
942 moduleTypeUnderTest: "filegroup",
943 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen12b4c272021-03-10 02:05:59 -0500944 expectedCount: map[string]int{
945 "package-opt-in": 2,
946 "package-opt-in/subpackage": 0,
947 "package-opt-out": 1,
948 "package-opt-out/subpackage": 0,
949 },
950 bp2buildConfig: android.Bp2BuildConfig{
951 "package-opt-in": android.Bp2BuildDefaultFalse,
952 "package-opt-out": android.Bp2BuildDefaultTrueRecursively,
953 },
954 fs: map[string]string{
955 "package-opt-in/Android.bp": `
956filegroup { name: "opt-in-a" }
957filegroup { name: "opt-in-b", bazel_module: { bp2build_available: true } }
958filegroup { name: "opt-in-c", bazel_module: { bp2build_available: true } }
959`,
960
961 "package-opt-in/subpackage/Android.bp": `
962filegroup { name: "opt-in-d" } // parent package not configured to DefaultTrueRecursively
963`,
964
965 "package-opt-out/Android.bp": `
966filegroup { name: "opt-out-a" }
967filegroup { name: "opt-out-b", bazel_module: { bp2build_available: false } }
968filegroup { name: "opt-out-c", bazel_module: { bp2build_available: false } }
969`,
970
971 "package-opt-out/subpackage/Android.bp": `
972filegroup { name: "opt-out-g", bazel_module: { bp2build_available: false } }
973filegroup { name: "opt-out-h", bazel_module: { bp2build_available: false } }
974`,
975 },
976 },
977 }
978
979 dir := "."
980 for _, testCase := range testCases {
981 fs := make(map[string][]byte)
982 toParse := []string{
983 "Android.bp",
984 }
985 for f, content := range testCase.fs {
986 if strings.HasSuffix(f, "Android.bp") {
987 toParse = append(toParse, f)
988 }
989 fs[f] = []byte(content)
990 }
991 config := android.TestConfig(buildDir, nil, "", fs)
992 ctx := android.NewTestContext(config)
993 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Jingwen Chen12b4c272021-03-10 02:05:59 -0500994 ctx.RegisterBp2BuildConfig(testCase.bp2buildConfig)
995 ctx.RegisterForBazelConversion()
996
997 _, errs := ctx.ParseFileList(dir, toParse)
998 android.FailIfErrored(t, errs)
999 _, errs = ctx.ResolveDependencies(config)
1000 android.FailIfErrored(t, errs)
1001
1002 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
1003
1004 // For each directory, test that the expected number of generated targets is correct.
1005 for dir, expectedCount := range testCase.expectedCount {
Liz Kammer6eff3232021-08-26 08:37:59 -04001006 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
1007 android.FailIfErrored(t, err)
Jingwen Chen12b4c272021-03-10 02:05:59 -05001008 if actualCount := len(bazelTargets); actualCount != expectedCount {
1009 t.Fatalf(
1010 "%s: Expected %d bazel target for %s package, got %d",
1011 testCase.description,
1012 expectedCount,
1013 dir,
1014 actualCount)
1015 }
1016
1017 }
1018 }
1019}
1020
Liz Kammerba3ea162021-02-17 13:22:03 -05001021func TestCombineBuildFilesBp2buildTargets(t *testing.T) {
Jingwen Chen5146ac02021-09-02 11:44:42 +00001022 testCases := []bp2buildTestCase{
Liz Kammerba3ea162021-02-17 13:22:03 -05001023 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001024 description: "filegroup bazel_module.label",
1025 moduleTypeUnderTest: "filegroup",
1026 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen5146ac02021-09-02 11:44:42 +00001027 blueprint: `filegroup {
Liz Kammerba3ea162021-02-17 13:22:03 -05001028 name: "fg_foo",
1029 bazel_module: { label: "//other:fg_foo" },
1030}`,
1031 expectedBazelTargets: []string{
1032 `// BUILD file`,
1033 },
Jingwen Chen5146ac02021-09-02 11:44:42 +00001034 filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -05001035 "other/BUILD.bazel": `// BUILD file`,
1036 },
1037 },
1038 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001039 description: "multiple bazel_module.label same BUILD",
1040 moduleTypeUnderTest: "filegroup",
1041 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen5146ac02021-09-02 11:44:42 +00001042 blueprint: `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001043 name: "fg_foo",
1044 bazel_module: { label: "//other:fg_foo" },
1045 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001046
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001047 filegroup {
1048 name: "foo",
1049 bazel_module: { label: "//other:foo" },
1050 }`,
Liz Kammerba3ea162021-02-17 13:22:03 -05001051 expectedBazelTargets: []string{
1052 `// BUILD file`,
1053 },
Jingwen Chen5146ac02021-09-02 11:44:42 +00001054 filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -05001055 "other/BUILD.bazel": `// BUILD file`,
1056 },
1057 },
1058 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001059 description: "filegroup bazel_module.label and bp2build in subdir",
1060 moduleTypeUnderTest: "filegroup",
1061 moduleTypeUnderTestFactory: android.FileGroupFactory,
1062 dir: "other",
1063 blueprint: ``,
Jingwen Chen5146ac02021-09-02 11:44:42 +00001064 filesystem: map[string]string{
Jingwen Chenc63677b2021-06-17 05:43:19 +00001065 "other/Android.bp": `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001066 name: "fg_foo",
1067 bazel_module: {
1068 bp2build_available: true,
1069 },
1070 }
1071 filegroup {
1072 name: "fg_bar",
1073 bazel_module: {
1074 label: "//other:fg_bar"
1075 },
1076 }`,
Jingwen Chenc63677b2021-06-17 05:43:19 +00001077 "other/BUILD.bazel": `// definition for fg_bar`,
1078 },
Liz Kammerba3ea162021-02-17 13:22:03 -05001079 expectedBazelTargets: []string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001080 makeBazelTarget("filegroup", "fg_foo", map[string]string{}),
1081 `// definition for fg_bar`,
Liz Kammerba3ea162021-02-17 13:22:03 -05001082 },
1083 },
1084 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001085 description: "filegroup bazel_module.label and filegroup bp2build",
1086 moduleTypeUnderTest: "filegroup",
1087 moduleTypeUnderTestFactory: android.FileGroupFactory,
1088
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001089 filesystem: map[string]string{
1090 "other/BUILD.bazel": `// BUILD file`,
1091 },
Jingwen Chen5146ac02021-09-02 11:44:42 +00001092 blueprint: `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001093 name: "fg_foo",
1094 bazel_module: {
1095 label: "//other:fg_foo",
1096 },
1097 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001098
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001099 filegroup {
1100 name: "fg_bar",
1101 bazel_module: {
1102 bp2build_available: true,
1103 },
1104 }`,
Liz Kammerba3ea162021-02-17 13:22:03 -05001105 expectedBazelTargets: []string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001106 makeBazelTarget("filegroup", "fg_bar", map[string]string{}),
Liz Kammerba3ea162021-02-17 13:22:03 -05001107 `// BUILD file`,
1108 },
Liz Kammerba3ea162021-02-17 13:22:03 -05001109 },
1110 }
1111
1112 dir := "."
1113 for _, testCase := range testCases {
Jingwen Chen49109762021-05-25 05:16:48 +00001114 t.Run(testCase.description, func(t *testing.T) {
1115 fs := make(map[string][]byte)
1116 toParse := []string{
1117 "Android.bp",
Liz Kammerba3ea162021-02-17 13:22:03 -05001118 }
Jingwen Chen5146ac02021-09-02 11:44:42 +00001119 for f, content := range testCase.filesystem {
Jingwen Chen49109762021-05-25 05:16:48 +00001120 if strings.HasSuffix(f, "Android.bp") {
1121 toParse = append(toParse, f)
1122 }
1123 fs[f] = []byte(content)
1124 }
Jingwen Chen5146ac02021-09-02 11:44:42 +00001125 config := android.TestConfig(buildDir, nil, testCase.blueprint, fs)
Jingwen Chen49109762021-05-25 05:16:48 +00001126 ctx := android.NewTestContext(config)
1127 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Jingwen Chen49109762021-05-25 05:16:48 +00001128 ctx.RegisterForBazelConversion()
Liz Kammerba3ea162021-02-17 13:22:03 -05001129
Jingwen Chen49109762021-05-25 05:16:48 +00001130 _, errs := ctx.ParseFileList(dir, toParse)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001131 if errored(t, testCase, errs) {
Jingwen Chen49109762021-05-25 05:16:48 +00001132 return
1133 }
1134 _, errs = ctx.ResolveDependencies(config)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001135 if errored(t, testCase, errs) {
Jingwen Chen49109762021-05-25 05:16:48 +00001136 return
1137 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001138
Jingwen Chen49109762021-05-25 05:16:48 +00001139 checkDir := dir
1140 if testCase.dir != "" {
1141 checkDir = testCase.dir
1142 }
Liz Kammer6eff3232021-08-26 08:37:59 -04001143 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
1144 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, checkDir)
1145 android.FailIfErrored(t, err)
Jingwen Chen49109762021-05-25 05:16:48 +00001146 bazelTargets.sort()
1147 actualCount := len(bazelTargets)
1148 expectedCount := len(testCase.expectedBazelTargets)
1149 if actualCount != expectedCount {
1150 t.Errorf("Expected %d bazel target, got %d\n%s", expectedCount, actualCount, bazelTargets)
1151 }
1152 if !strings.Contains(bazelTargets.String(), "Section: Handcrafted targets. ") {
1153 t.Errorf("Expected string representation of bazelTargets to contain handcrafted section header.")
1154 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001155 for i, target := range bazelTargets {
Jingwen Chen49109762021-05-25 05:16:48 +00001156 actualContent := target.content
1157 expectedContent := testCase.expectedBazelTargets[i]
1158 if expectedContent != actualContent {
Liz Kammerba3ea162021-02-17 13:22:03 -05001159 t.Errorf(
Jingwen Chen49109762021-05-25 05:16:48 +00001160 "Expected generated Bazel target to be '%s', got '%s'",
1161 expectedContent,
1162 actualContent,
Liz Kammerba3ea162021-02-17 13:22:03 -05001163 )
1164 }
1165 }
Jingwen Chen49109762021-05-25 05:16:48 +00001166 })
Liz Kammerba3ea162021-02-17 13:22:03 -05001167 }
1168}
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001169
1170func TestGlobExcludeSrcs(t *testing.T) {
Jingwen Chen5146ac02021-09-02 11:44:42 +00001171 testCases := []bp2buildTestCase{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001172 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001173 description: "filegroup top level exclude_srcs",
1174 moduleTypeUnderTest: "filegroup",
1175 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen5146ac02021-09-02 11:44:42 +00001176 blueprint: `filegroup {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001177 name: "fg_foo",
1178 srcs: ["**/*.txt"],
1179 exclude_srcs: ["c.txt"],
1180 bazel_module: { bp2build_available: true },
1181}`,
Jingwen Chen5146ac02021-09-02 11:44:42 +00001182 filesystem: map[string]string{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001183 "a.txt": "",
1184 "b.txt": "",
1185 "c.txt": "",
1186 "dir/Android.bp": "",
1187 "dir/e.txt": "",
1188 "dir/f.txt": "",
1189 },
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001190 expectedBazelTargets: []string{
1191 makeBazelTarget("filegroup", "fg_foo", map[string]string{
1192 "srcs": `[
1193 "a.txt",
1194 "b.txt",
1195 "//dir:e.txt",
1196 "//dir:f.txt",
1197 ]`,
1198 }),
1199 },
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001200 },
1201 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001202 description: "filegroup in subdir exclude_srcs",
1203 moduleTypeUnderTest: "filegroup",
1204 moduleTypeUnderTestFactory: android.FileGroupFactory,
1205 blueprint: "",
1206 dir: "dir",
Jingwen Chen5146ac02021-09-02 11:44:42 +00001207 filesystem: map[string]string{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001208 "dir/Android.bp": `filegroup {
1209 name: "fg_foo",
1210 srcs: ["**/*.txt"],
1211 exclude_srcs: ["b.txt"],
1212 bazel_module: { bp2build_available: true },
1213}
1214`,
1215 "dir/a.txt": "",
1216 "dir/b.txt": "",
1217 "dir/subdir/Android.bp": "",
1218 "dir/subdir/e.txt": "",
1219 "dir/subdir/f.txt": "",
1220 },
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001221 expectedBazelTargets: []string{
1222 makeBazelTarget("filegroup", "fg_foo", map[string]string{
1223 "srcs": `[
Liz Kammer9abd62d2021-05-21 08:37:59 -04001224 "a.txt",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001225 "//dir/subdir:e.txt",
1226 "//dir/subdir:f.txt",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001227 ]`,
1228 }),
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001229 },
1230 },
1231 }
1232
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001233 for _, testCase := range testCases {
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001234 t.Run(testCase.description, func(t *testing.T) {
1235 runBp2BuildTestCaseSimple(t, testCase)
1236 })
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001237 }
1238}
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001239
1240func TestCommonBp2BuildModuleAttrs(t *testing.T) {
1241 testCases := []bp2buildTestCase{
1242 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001243 description: "Required into data test",
1244 moduleTypeUnderTest: "filegroup",
1245 moduleTypeUnderTestFactory: android.FileGroupFactory,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001246 blueprint: simpleModuleDoNotConvertBp2build("filegroup", "reqd") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001247filegroup {
1248 name: "fg_foo",
1249 required: ["reqd"],
1250 bazel_module: { bp2build_available: true },
1251}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001252 expectedBazelTargets: []string{
1253 makeBazelTarget("filegroup", "fg_foo", map[string]string{
1254 "data": `[":reqd"]`,
1255 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001256 },
1257 },
1258 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001259 description: "Required via arch into data test",
1260 moduleTypeUnderTest: "python_library",
1261 moduleTypeUnderTestFactory: python.PythonLibraryFactory,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001262 blueprint: simpleModuleDoNotConvertBp2build("python_library", "reqdx86") +
1263 simpleModuleDoNotConvertBp2build("python_library", "reqdarm") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001264python_library {
1265 name: "fg_foo",
1266 arch: {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001267 arm: {
1268 required: ["reqdarm"],
1269 },
1270 x86: {
1271 required: ["reqdx86"],
1272 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001273 },
1274 bazel_module: { bp2build_available: true },
1275}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001276 expectedBazelTargets: []string{
1277 makeBazelTarget("py_library", "fg_foo", map[string]string{
1278 "data": `select({
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001279 "//build/bazel/platforms/arch:arm": [":reqdarm"],
1280 "//build/bazel/platforms/arch:x86": [":reqdx86"],
1281 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001282 })`,
1283 "srcs_version": `"PY3"`,
1284 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001285 },
1286 },
1287 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001288 description: "Required appended to data test",
1289 moduleTypeUnderTest: "python_library",
1290 moduleTypeUnderTestFactory: python.PythonLibraryFactory,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001291 filesystem: map[string]string{
1292 "data.bin": "",
1293 "src.py": "",
1294 },
1295 blueprint: simpleModuleDoNotConvertBp2build("python_library", "reqd") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001296python_library {
1297 name: "fg_foo",
1298 data: ["data.bin"],
1299 required: ["reqd"],
1300 bazel_module: { bp2build_available: true },
1301}`,
1302 expectedBazelTargets: []string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001303 makeBazelTarget("py_library", "fg_foo", map[string]string{
1304 "data": `[
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001305 "data.bin",
1306 ":reqd",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001307 ]`,
1308 "srcs_version": `"PY3"`,
1309 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001310 },
1311 },
1312 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001313 description: "All props-to-attrs at once together test",
1314 moduleTypeUnderTest: "filegroup",
1315 moduleTypeUnderTestFactory: android.FileGroupFactory,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001316 blueprint: simpleModuleDoNotConvertBp2build("filegroup", "reqd") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001317filegroup {
1318 name: "fg_foo",
1319 required: ["reqd"],
1320 bazel_module: { bp2build_available: true },
1321}`,
1322 expectedBazelTargets: []string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001323 makeBazelTarget("filegroup", "fg_foo", map[string]string{
1324 "data": `[":reqd"]`,
1325 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001326 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001327 },
1328 }
1329
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001330 for _, tc := range testCases {
1331 t.Run(tc.description, func(t *testing.T) {
1332 runBp2BuildTestCaseSimple(t, tc)
1333 })
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001334 }
1335}