blob: 0f3ca79b55ac1e35f1b10049db1e55f56269ea19 [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"
Sam Delmerico24c56032022-03-28 19:53:03 +000023 "android/soong/android/allowlists"
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +000024 "android/soong/python"
Liz Kammer2dd9ca42020-11-25 16:06:39 -080025)
26
27func TestGenerateSoongModuleTargets(t *testing.T) {
28 testCases := []struct {
Liz Kammerd366c902021-06-03 13:43:01 -040029 description string
Liz Kammer2dd9ca42020-11-25 16:06:39 -080030 bp string
31 expectedBazelTarget string
32 }{
33 {
Liz Kammerd366c902021-06-03 13:43:01 -040034 description: "only name",
Jingwen Chenb4628eb2021-04-08 14:40:57 +000035 bp: `custom { name: "foo" }
Liz Kammerd366c902021-06-03 13:43:01 -040036 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080037 expectedBazelTarget: `soong_module(
38 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050039 soong_module_name = "foo",
40 soong_module_type = "custom",
41 soong_module_variant = "",
42 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080043 ],
Liz Kammerd366c902021-06-03 13:43:01 -040044 bool_prop = False,
Liz Kammer46fb7ab2021-12-01 10:09:34 -050045 string_prop = "",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080046)`,
47 },
48 {
Liz Kammerd366c902021-06-03 13:43:01 -040049 description: "handles bool",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080050 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -040051 name: "foo",
52 bool_prop: true,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080053}
Liz Kammerd366c902021-06-03 13:43:01 -040054 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080055 expectedBazelTarget: `soong_module(
56 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050057 soong_module_name = "foo",
58 soong_module_type = "custom",
59 soong_module_variant = "",
60 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080061 ],
Liz Kammerd366c902021-06-03 13:43:01 -040062 bool_prop = True,
Liz Kammer46fb7ab2021-12-01 10:09:34 -050063 string_prop = "",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080064)`,
65 },
66 {
Liz Kammerd366c902021-06-03 13:43:01 -040067 description: "string escaping",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080068 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -040069 name: "foo",
70 owner: "a_string_with\"quotes\"_and_\\backslashes\\\\",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080071}
Liz Kammerd366c902021-06-03 13:43:01 -040072 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080073 expectedBazelTarget: `soong_module(
74 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050075 soong_module_name = "foo",
76 soong_module_type = "custom",
77 soong_module_variant = "",
78 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080079 ],
Liz Kammerd366c902021-06-03 13:43:01 -040080 bool_prop = False,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080081 owner = "a_string_with\"quotes\"_and_\\backslashes\\\\",
Liz Kammer46fb7ab2021-12-01 10:09:34 -050082 string_prop = "",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080083)`,
84 },
85 {
Liz Kammerd366c902021-06-03 13:43:01 -040086 description: "single item string list",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080087 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -040088 name: "foo",
89 required: ["bar"],
Liz Kammer2dd9ca42020-11-25 16:06:39 -080090}
Liz Kammerd366c902021-06-03 13:43:01 -040091 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080092 expectedBazelTarget: `soong_module(
93 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050094 soong_module_name = "foo",
95 soong_module_type = "custom",
96 soong_module_variant = "",
97 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080098 ],
Liz Kammerd366c902021-06-03 13:43:01 -040099 bool_prop = False,
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000100 required = ["bar"],
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500101 string_prop = "",
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800102)`,
103 },
104 {
Liz Kammerd366c902021-06-03 13:43:01 -0400105 description: "list of strings",
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800106 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -0400107 name: "foo",
108 target_required: ["qux", "bazqux"],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800109}
Liz Kammerd366c902021-06-03 13:43:01 -0400110 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800111 expectedBazelTarget: `soong_module(
112 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500113 soong_module_name = "foo",
114 soong_module_type = "custom",
115 soong_module_variant = "",
116 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800117 ],
Liz Kammerd366c902021-06-03 13:43:01 -0400118 bool_prop = False,
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500119 string_prop = "",
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800120 target_required = [
121 "qux",
122 "bazqux",
123 ],
124)`,
125 },
126 {
Liz Kammerd366c902021-06-03 13:43:01 -0400127 description: "dist/dists",
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800128 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -0400129 name: "foo",
130 dist: {
131 targets: ["goal_foo"],
132 tag: ".foo",
133 },
134 dists: [{
135 targets: ["goal_bar"],
136 tag: ".bar",
137 }],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800138}
Liz Kammerd366c902021-06-03 13:43:01 -0400139 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800140 expectedBazelTarget: `soong_module(
141 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500142 soong_module_name = "foo",
143 soong_module_type = "custom",
144 soong_module_variant = "",
145 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800146 ],
Liz Kammerd366c902021-06-03 13:43:01 -0400147 bool_prop = False,
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800148 dist = {
149 "tag": ".foo",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000150 "targets": ["goal_foo"],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800151 },
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000152 dists = [{
153 "tag": ".bar",
154 "targets": ["goal_bar"],
155 }],
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500156 string_prop = "",
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800157)`,
158 },
159 {
Liz Kammerd366c902021-06-03 13:43:01 -0400160 description: "put it together",
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800161 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -0400162 name: "foo",
163 required: ["bar"],
164 target_required: ["qux", "bazqux"],
165 bool_prop: true,
166 owner: "custom_owner",
167 dists: [
168 {
169 tag: ".tag",
170 targets: ["my_goal"],
171 },
172 ],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800173}
Liz Kammerd366c902021-06-03 13:43:01 -0400174 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800175 expectedBazelTarget: `soong_module(
176 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500177 soong_module_name = "foo",
178 soong_module_type = "custom",
179 soong_module_variant = "",
180 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800181 ],
Liz Kammerd366c902021-06-03 13:43:01 -0400182 bool_prop = True,
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000183 dists = [{
184 "tag": ".tag",
185 "targets": ["my_goal"],
186 }],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800187 owner = "custom_owner",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000188 required = ["bar"],
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500189 string_prop = "",
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800190 target_required = [
191 "qux",
192 "bazqux",
193 ],
194)`,
195 },
196 }
197
198 dir := "."
199 for _, testCase := range testCases {
Liz Kammerd366c902021-06-03 13:43:01 -0400200 t.Run(testCase.description, func(t *testing.T) {
201 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
202 ctx := android.NewTestContext(config)
Jingwen Chen164e0862021-02-19 00:48:40 -0500203
Liz Kammerd366c902021-06-03 13:43:01 -0400204 ctx.RegisterModuleType("custom", customModuleFactory)
205 ctx.Register()
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800206
Liz Kammerd366c902021-06-03 13:43:01 -0400207 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
208 android.FailIfErrored(t, errs)
209 _, errs = ctx.PrepareBuildActions(config)
210 android.FailIfErrored(t, errs)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800211
Liz Kammerd366c902021-06-03 13:43:01 -0400212 codegenCtx := NewCodegenContext(config, *ctx.Context, QueryView)
Liz Kammer6eff3232021-08-26 08:37:59 -0400213 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
214 android.FailIfErrored(t, err)
Liz Kammerd366c902021-06-03 13:43:01 -0400215 if actualCount, expectedCount := len(bazelTargets), 1; actualCount != expectedCount {
216 t.Fatalf("Expected %d bazel target, got %d", expectedCount, actualCount)
217 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800218
Liz Kammerd366c902021-06-03 13:43:01 -0400219 actualBazelTarget := bazelTargets[0]
220 if actualBazelTarget.content != testCase.expectedBazelTarget {
221 t.Errorf(
222 "Expected generated Bazel target to be '%s', got '%s'",
223 testCase.expectedBazelTarget,
224 actualBazelTarget.content,
225 )
226 }
227 })
Jingwen Chen73850672020-12-14 08:25:34 -0500228 }
229}
230
231func TestGenerateBazelTargetModules(t *testing.T) {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000232 testCases := []bp2buildTestCase{
Jingwen Chen73850672020-12-14 08:25:34 -0500233 {
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500234 description: "string ptr props",
235 blueprint: `custom {
236 name: "foo",
237 string_ptr_prop: "",
238 bazel_module: { bp2build_available: true },
239}`,
240 expectedBazelTargets: []string{
241 makeBazelTarget("custom", "foo", attrNameToString{
242 "string_ptr_prop": `""`,
243 }),
244 },
245 },
246 {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400247 description: "string props",
Jingwen Chen5146ac02021-09-02 11:44:42 +0000248 blueprint: `custom {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400249 name: "foo",
Jingwen Chen73850672020-12-14 08:25:34 -0500250 string_list_prop: ["a", "b"],
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500251 string_ptr_prop: "a",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500252 bazel_module: { bp2build_available: true },
Jingwen Chen73850672020-12-14 08:25:34 -0500253}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500254 expectedBazelTargets: []string{
255 makeBazelTarget("custom", "foo", attrNameToString{
256 "string_list_prop": `[
Jingwen Chen73850672020-12-14 08:25:34 -0500257 "a",
258 "b",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500259 ]`,
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500260 "string_ptr_prop": `"a"`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500261 }),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400262 },
Jingwen Chen73850672020-12-14 08:25:34 -0500263 },
Jingwen Chen58a12b82021-03-30 13:08:36 +0000264 {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400265 description: "control characters",
Jingwen Chen5146ac02021-09-02 11:44:42 +0000266 blueprint: `custom {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500267 name: "foo",
Jingwen Chen58a12b82021-03-30 13:08:36 +0000268 string_list_prop: ["\t", "\n"],
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500269 string_ptr_prop: "a\t\n\r",
Jingwen Chen58a12b82021-03-30 13:08:36 +0000270 bazel_module: { bp2build_available: true },
271}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500272 expectedBazelTargets: []string{
273 makeBazelTarget("custom", "foo", attrNameToString{
274 "string_list_prop": `[
Jingwen Chen58a12b82021-03-30 13:08:36 +0000275 "\t",
276 "\n",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500277 ]`,
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500278 "string_ptr_prop": `"a\t\n\r"`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500279 }),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400280 },
281 },
282 {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400283 description: "handles dep",
Jingwen Chen5146ac02021-09-02 11:44:42 +0000284 blueprint: `custom {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400285 name: "has_dep",
286 arch_paths: [":dep"],
287 bazel_module: { bp2build_available: true },
288}
289
290custom {
291 name: "dep",
292 arch_paths: ["abc"],
293 bazel_module: { bp2build_available: true },
294}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500295 expectedBazelTargets: []string{
296 makeBazelTarget("custom", "dep", attrNameToString{
297 "arch_paths": `["abc"]`,
298 }),
299 makeBazelTarget("custom", "has_dep", attrNameToString{
300 "arch_paths": `[":dep"]`,
301 }),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400302 },
303 },
304 {
Liz Kammerdaa09ef2021-12-15 15:35:38 -0500305 description: "non-existent dep",
306 blueprint: `custom {
307 name: "has_dep",
308 arch_paths: [":dep"],
309 bazel_module: { bp2build_available: true },
310}`,
311 expectedBazelTargets: []string{
312 makeBazelTarget("custom", "has_dep", attrNameToString{
313 "arch_paths": `[":dep__BP2BUILD__MISSING__DEP"]`,
314 }),
315 },
316 },
317 {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400318 description: "arch-variant srcs",
Jingwen Chen5146ac02021-09-02 11:44:42 +0000319 blueprint: `custom {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400320 name: "arch_paths",
321 arch: {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400322 x86: { arch_paths: ["x86.txt"] },
323 x86_64: { arch_paths: ["x86_64.txt"] },
324 arm: { arch_paths: ["arm.txt"] },
325 arm64: { arch_paths: ["arm64.txt"] },
326 },
327 target: {
328 linux: { arch_paths: ["linux.txt"] },
329 bionic: { arch_paths: ["bionic.txt"] },
330 host: { arch_paths: ["host.txt"] },
331 not_windows: { arch_paths: ["not_windows.txt"] },
332 android: { arch_paths: ["android.txt"] },
333 linux_musl: { arch_paths: ["linux_musl.txt"] },
334 musl: { arch_paths: ["musl.txt"] },
335 linux_glibc: { arch_paths: ["linux_glibc.txt"] },
336 glibc: { arch_paths: ["glibc.txt"] },
337 linux_bionic: { arch_paths: ["linux_bionic.txt"] },
338 darwin: { arch_paths: ["darwin.txt"] },
339 windows: { arch_paths: ["windows.txt"] },
340 },
341 multilib: {
342 lib32: { arch_paths: ["lib32.txt"] },
343 lib64: { arch_paths: ["lib64.txt"] },
Liz Kammer4562a3b2021-04-21 18:15:34 -0400344 },
345 bazel_module: { bp2build_available: true },
346}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500347 expectedBazelTargets: []string{
348 makeBazelTarget("custom", "arch_paths", attrNameToString{
349 "arch_paths": `select({
Liz Kammerfdd72e62021-10-11 15:41:03 -0400350 "//build/bazel/platforms/arch:arm": [
351 "arm.txt",
352 "lib32.txt",
353 ],
354 "//build/bazel/platforms/arch:arm64": [
355 "arm64.txt",
356 "lib64.txt",
357 ],
358 "//build/bazel/platforms/arch:x86": [
359 "x86.txt",
360 "lib32.txt",
361 ],
362 "//build/bazel/platforms/arch:x86_64": [
363 "x86_64.txt",
364 "lib64.txt",
365 ],
366 "//conditions:default": [],
367 }) + select({
368 "//build/bazel/platforms/os:android": [
369 "linux.txt",
370 "bionic.txt",
371 "android.txt",
372 ],
373 "//build/bazel/platforms/os:darwin": [
374 "host.txt",
375 "darwin.txt",
376 "not_windows.txt",
377 ],
378 "//build/bazel/platforms/os:linux": [
379 "host.txt",
380 "linux.txt",
381 "glibc.txt",
382 "linux_glibc.txt",
383 "not_windows.txt",
384 ],
385 "//build/bazel/platforms/os:linux_bionic": [
386 "host.txt",
387 "linux.txt",
388 "bionic.txt",
389 "linux_bionic.txt",
390 "not_windows.txt",
391 ],
392 "//build/bazel/platforms/os:linux_musl": [
393 "host.txt",
394 "linux.txt",
395 "musl.txt",
396 "linux_musl.txt",
397 "not_windows.txt",
398 ],
399 "//build/bazel/platforms/os:windows": [
400 "host.txt",
401 "windows.txt",
402 ],
Liz Kammer4562a3b2021-04-21 18:15:34 -0400403 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500404 })`,
405 }),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400406 },
407 },
408 {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400409 description: "arch-variant deps",
Jingwen Chen5146ac02021-09-02 11:44:42 +0000410 blueprint: `custom {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400411 name: "has_dep",
412 arch: {
413 x86: {
414 arch_paths: [":dep"],
415 },
416 },
417 bazel_module: { bp2build_available: true },
418}
419
420custom {
421 name: "dep",
422 arch_paths: ["abc"],
423 bazel_module: { bp2build_available: true },
424}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500425 expectedBazelTargets: []string{
426 makeBazelTarget("custom", "dep", attrNameToString{
427 "arch_paths": `["abc"]`,
428 }),
429 makeBazelTarget("custom", "has_dep", attrNameToString{
430 "arch_paths": `select({
Liz Kammer4562a3b2021-04-21 18:15:34 -0400431 "//build/bazel/platforms/arch:x86": [":dep"],
432 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500433 })`,
434 }),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400435 },
Jingwen Chen58a12b82021-03-30 13:08:36 +0000436 },
Liz Kammer32a03392021-09-14 11:17:21 -0400437 {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400438 description: "embedded props",
Liz Kammer32a03392021-09-14 11:17:21 -0400439 blueprint: `custom {
440 name: "embedded_props",
441 embedded_prop: "abc",
442 bazel_module: { bp2build_available: true },
443}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500444 expectedBazelTargets: []string{
445 makeBazelTarget("custom", "embedded_props", attrNameToString{
446 "embedded_attr": `"abc"`,
447 }),
Liz Kammer32a03392021-09-14 11:17:21 -0400448 },
449 },
450 {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400451 description: "ptr to embedded props",
Liz Kammer32a03392021-09-14 11:17:21 -0400452 blueprint: `custom {
453 name: "ptr_to_embedded_props",
454 other_embedded_prop: "abc",
455 bazel_module: { bp2build_available: true },
456}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500457 expectedBazelTargets: []string{
458 makeBazelTarget("custom", "ptr_to_embedded_props", attrNameToString{
459 "other_embedded_attr": `"abc"`,
460 }),
Liz Kammer32a03392021-09-14 11:17:21 -0400461 },
462 },
Jingwen Chen73850672020-12-14 08:25:34 -0500463 }
464
465 dir := "."
466 for _, testCase := range testCases {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400467 t.Run(testCase.description, func(t *testing.T) {
468 config := android.TestConfig(buildDir, nil, testCase.blueprint, nil)
469 ctx := android.NewTestContext(config)
Jingwen Chen164e0862021-02-19 00:48:40 -0500470
Liz Kammerfdd72e62021-10-11 15:41:03 -0400471 registerCustomModuleForBp2buildConversion(ctx)
Jingwen Chen73850672020-12-14 08:25:34 -0500472
Liz Kammerfdd72e62021-10-11 15:41:03 -0400473 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
474 if errored(t, testCase, errs) {
475 return
476 }
477 _, errs = ctx.ResolveDependencies(config)
478 if errored(t, testCase, errs) {
479 return
480 }
Jingwen Chen73850672020-12-14 08:25:34 -0500481
Liz Kammerfdd72e62021-10-11 15:41:03 -0400482 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
483 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
484 android.FailIfErrored(t, err)
Jingwen Chen164e0862021-02-19 00:48:40 -0500485
Liz Kammerfdd72e62021-10-11 15:41:03 -0400486 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400487 t.Errorf("Expected %d bazel target (%s),\ngot %d (%s)", expectedCount, testCase.expectedBazelTargets, actualCount, bazelTargets)
Liz Kammerfdd72e62021-10-11 15:41:03 -0400488 } else {
489 for i, expectedBazelTarget := range testCase.expectedBazelTargets {
490 actualBazelTarget := bazelTargets[i]
491 if actualBazelTarget.content != expectedBazelTarget {
492 t.Errorf(
493 "Expected generated Bazel target to be '%s', got '%s'",
494 expectedBazelTarget,
495 actualBazelTarget.content,
496 )
497 }
Liz Kammer4562a3b2021-04-21 18:15:34 -0400498 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500499 }
Liz Kammerfdd72e62021-10-11 15:41:03 -0400500 })
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800501 }
502}
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500503
Jingwen Chen40067de2021-01-26 21:58:43 -0500504func TestLoadStatements(t *testing.T) {
505 testCases := []struct {
506 bazelTargets BazelTargets
507 expectedLoadStatements string
508 }{
509 {
510 bazelTargets: BazelTargets{
511 BazelTarget{
512 name: "foo",
513 ruleClass: "cc_library",
514 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
515 },
516 },
517 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
518 },
519 {
520 bazelTargets: BazelTargets{
521 BazelTarget{
522 name: "foo",
523 ruleClass: "cc_library",
524 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
525 },
526 BazelTarget{
527 name: "bar",
528 ruleClass: "cc_library",
529 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
530 },
531 },
532 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
533 },
534 {
535 bazelTargets: BazelTargets{
536 BazelTarget{
537 name: "foo",
538 ruleClass: "cc_library",
539 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
540 },
541 BazelTarget{
542 name: "bar",
543 ruleClass: "cc_binary",
544 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
545 },
546 },
547 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")`,
548 },
549 {
550 bazelTargets: BazelTargets{
551 BazelTarget{
552 name: "foo",
553 ruleClass: "cc_library",
554 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
555 },
556 BazelTarget{
557 name: "bar",
558 ruleClass: "cc_binary",
559 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
560 },
561 BazelTarget{
562 name: "baz",
563 ruleClass: "java_binary",
564 bzlLoadLocation: "//build/bazel/rules:java.bzl",
565 },
566 },
567 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")
568load("//build/bazel/rules:java.bzl", "java_binary")`,
569 },
570 {
571 bazelTargets: BazelTargets{
572 BazelTarget{
573 name: "foo",
574 ruleClass: "cc_binary",
575 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
576 },
577 BazelTarget{
578 name: "bar",
579 ruleClass: "java_binary",
580 bzlLoadLocation: "//build/bazel/rules:java.bzl",
581 },
582 BazelTarget{
583 name: "baz",
584 ruleClass: "genrule",
585 // Note: no bzlLoadLocation for native rules
586 },
587 },
588 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary")
589load("//build/bazel/rules:java.bzl", "java_binary")`,
590 },
591 }
592
593 for _, testCase := range testCases {
594 actual := testCase.bazelTargets.LoadStatements()
595 expected := testCase.expectedLoadStatements
596 if actual != expected {
597 t.Fatalf("Expected load statements to be %s, got %s", expected, actual)
598 }
599 }
600
601}
602
603func TestGenerateBazelTargetModules_OneToMany_LoadedFromStarlark(t *testing.T) {
604 testCases := []struct {
605 bp string
606 expectedBazelTarget string
607 expectedBazelTargetCount int
608 expectedLoadStatements string
609 }{
610 {
611 bp: `custom {
612 name: "bar",
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400613 one_to_many_prop: true,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500614 bazel_module: { bp2build_available: true },
Jingwen Chen40067de2021-01-26 21:58:43 -0500615}`,
616 expectedBazelTarget: `my_library(
617 name = "bar",
618)
619
Jingwen Chen40067de2021-01-26 21:58:43 -0500620proto_library(
621 name = "bar_proto_library_deps",
Liz Kammer2ada09a2021-08-11 00:17:36 -0400622)
623
624my_proto_library(
625 name = "bar_my_proto_library_deps",
Jingwen Chen40067de2021-01-26 21:58:43 -0500626)`,
627 expectedBazelTargetCount: 3,
628 expectedLoadStatements: `load("//build/bazel/rules:proto.bzl", "my_proto_library", "proto_library")
629load("//build/bazel/rules:rules.bzl", "my_library")`,
630 },
631 }
632
633 dir := "."
634 for _, testCase := range testCases {
635 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
636 ctx := android.NewTestContext(config)
637 ctx.RegisterModuleType("custom", customModuleFactory)
Jingwen Chen40067de2021-01-26 21:58:43 -0500638 ctx.RegisterForBazelConversion()
639
640 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
641 android.FailIfErrored(t, errs)
642 _, errs = ctx.ResolveDependencies(config)
643 android.FailIfErrored(t, errs)
644
Jingwen Chen164e0862021-02-19 00:48:40 -0500645 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Liz Kammer6eff3232021-08-26 08:37:59 -0400646 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
647 android.FailIfErrored(t, err)
Jingwen Chen40067de2021-01-26 21:58:43 -0500648 if actualCount := len(bazelTargets); actualCount != testCase.expectedBazelTargetCount {
649 t.Fatalf("Expected %d bazel target, got %d", testCase.expectedBazelTargetCount, actualCount)
650 }
651
652 actualBazelTargets := bazelTargets.String()
653 if actualBazelTargets != testCase.expectedBazelTarget {
654 t.Errorf(
655 "Expected generated Bazel target to be '%s', got '%s'",
656 testCase.expectedBazelTarget,
657 actualBazelTargets,
658 )
659 }
660
661 actualLoadStatements := bazelTargets.LoadStatements()
662 if actualLoadStatements != testCase.expectedLoadStatements {
663 t.Errorf(
664 "Expected generated load statements to be '%s', got '%s'",
665 testCase.expectedLoadStatements,
666 actualLoadStatements,
667 )
668 }
669 }
670}
671
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500672func TestModuleTypeBp2Build(t *testing.T) {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000673 testCases := []bp2buildTestCase{
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500674 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400675 description: "filegroup with does not specify srcs",
676 moduleTypeUnderTest: "filegroup",
677 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000678 blueprint: `filegroup {
Liz Kammerebfcf672021-02-16 15:00:05 -0500679 name: "fg_foo",
680 bazel_module: { bp2build_available: true },
681}`,
682 expectedBazelTargets: []string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500683 makeBazelTarget("filegroup", "fg_foo", map[string]string{}),
Liz Kammerebfcf672021-02-16 15:00:05 -0500684 },
685 },
686 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400687 description: "filegroup with no srcs",
688 moduleTypeUnderTest: "filegroup",
689 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000690 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500691 name: "fg_foo",
692 srcs: [],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500693 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500694}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500695 expectedBazelTargets: []string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500696 makeBazelTarget("filegroup", "fg_foo", map[string]string{}),
Liz Kammer356f7d42021-01-26 09:18:53 -0500697 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500698 },
699 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400700 description: "filegroup with srcs",
701 moduleTypeUnderTest: "filegroup",
702 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000703 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500704 name: "fg_foo",
705 srcs: ["a", "b"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500706 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500707}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500708 expectedBazelTargets: []string{
709 makeBazelTarget("filegroup", "fg_foo", map[string]string{
710 "srcs": `[
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500711 "a",
712 "b",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500713 ]`,
714 }),
Liz Kammer356f7d42021-01-26 09:18:53 -0500715 },
716 },
717 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400718 description: "filegroup with excludes srcs",
719 moduleTypeUnderTest: "filegroup",
720 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000721 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500722 name: "fg_foo",
723 srcs: ["a", "b"],
724 exclude_srcs: ["a"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500725 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500726}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500727 expectedBazelTargets: []string{
728 makeBazelTarget("filegroup", "fg_foo", map[string]string{
729 "srcs": `["b"]`,
730 }),
Liz Kammer356f7d42021-01-26 09:18:53 -0500731 },
732 },
733 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400734 description: "filegroup with glob",
735 moduleTypeUnderTest: "filegroup",
736 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000737 blueprint: `filegroup {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500738 name: "fg_foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500739 srcs: ["**/*.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500740 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500741}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500742 expectedBazelTargets: []string{
743 makeBazelTarget("filegroup", "fg_foo", map[string]string{
744 "srcs": `[
Liz Kammer356f7d42021-01-26 09:18:53 -0500745 "other/a.txt",
746 "other/b.txt",
747 "other/subdir/a.txt",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500748 ]`,
749 }),
Liz Kammer356f7d42021-01-26 09:18:53 -0500750 },
Jingwen Chen5146ac02021-09-02 11:44:42 +0000751 filesystem: map[string]string{
Liz Kammer356f7d42021-01-26 09:18:53 -0500752 "other/a.txt": "",
753 "other/b.txt": "",
754 "other/subdir/a.txt": "",
755 "other/file": "",
756 },
757 },
758 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400759 description: "filegroup with glob in subdir",
760 moduleTypeUnderTest: "filegroup",
761 moduleTypeUnderTestFactory: android.FileGroupFactory,
762 dir: "other",
Jingwen Chen5146ac02021-09-02 11:44:42 +0000763 filesystem: map[string]string{
Liz Kammer356f7d42021-01-26 09:18:53 -0500764 "other/Android.bp": `filegroup {
765 name: "fg_foo",
766 srcs: ["**/*.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500767 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500768}`,
769 "other/a.txt": "",
770 "other/b.txt": "",
771 "other/subdir/a.txt": "",
772 "other/file": "",
773 },
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500774 expectedBazelTargets: []string{
775 makeBazelTarget("filegroup", "fg_foo", map[string]string{
776 "srcs": `[
777 "a.txt",
778 "b.txt",
779 "subdir/a.txt",
780 ]`,
781 }),
782 },
Liz Kammer356f7d42021-01-26 09:18:53 -0500783 },
784 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400785 description: "depends_on_other_dir_module",
786 moduleTypeUnderTest: "filegroup",
787 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000788 blueprint: `filegroup {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500789 name: "fg_foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500790 srcs: [
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000791 ":foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500792 "c",
793 ],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500794 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500795}`,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000796 filesystem: map[string]string{
Liz Kammer356f7d42021-01-26 09:18:53 -0500797 "other/Android.bp": `filegroup {
798 name: "foo",
799 srcs: ["a", "b"],
Liz Kammer6eff3232021-08-26 08:37:59 -0400800 bazel_module: { bp2build_available: true },
801}`,
802 },
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500803 expectedBazelTargets: []string{
804 makeBazelTarget("filegroup", "fg_foo", map[string]string{
805 "srcs": `[
806 "//other:foo",
807 "c",
808 ]`,
809 }),
810 },
Liz Kammer6eff3232021-08-26 08:37:59 -0400811 },
812 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400813 description: "depends_on_other_unconverted_module_error",
814 moduleTypeUnderTest: "filegroup",
815 moduleTypeUnderTestFactory: android.FileGroupFactory,
816 unconvertedDepsMode: errorModulesUnconvertedDeps,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500817 blueprint: `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400818 name: "foobar",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500819 srcs: [
820 ":foo",
821 "c",
822 ],
823 bazel_module: { bp2build_available: true },
824}`,
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400825 expectedErr: fmt.Errorf(`"foobar" depends on unconverted modules: foo`),
826 filesystem: map[string]string{
827 "other/Android.bp": `filegroup {
828 name: "foo",
829 srcs: ["a", "b"],
830}`,
831 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500832 },
833 }
834
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500835 for _, testCase := range testCases {
Liz Kammer6eff3232021-08-26 08:37:59 -0400836 t.Run(testCase.description, func(t *testing.T) {
837 runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, testCase)
838 })
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500839 }
840}
Jingwen Chen041b1842021-02-01 00:23:25 -0500841
842type bp2buildMutator = func(android.TopDownMutatorContext)
843
Jingwen Chen12b4c272021-03-10 02:05:59 -0500844func TestAllowlistingBp2buildTargetsExplicitly(t *testing.T) {
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500845 testCases := []struct {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400846 moduleTypeUnderTest string
847 moduleTypeUnderTestFactory android.ModuleFactory
848 bp string
849 expectedCount int
850 description string
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500851 }{
852 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400853 description: "explicitly unavailable",
854 moduleTypeUnderTest: "filegroup",
855 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500856 bp: `filegroup {
857 name: "foo",
858 srcs: ["a", "b"],
859 bazel_module: { bp2build_available: false },
860}`,
861 expectedCount: 0,
862 },
863 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400864 description: "implicitly unavailable",
865 moduleTypeUnderTest: "filegroup",
866 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500867 bp: `filegroup {
868 name: "foo",
869 srcs: ["a", "b"],
870}`,
871 expectedCount: 0,
872 },
873 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400874 description: "explicitly available",
875 moduleTypeUnderTest: "filegroup",
876 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500877 bp: `filegroup {
878 name: "foo",
879 srcs: ["a", "b"],
880 bazel_module: { bp2build_available: true },
881}`,
882 expectedCount: 1,
883 },
884 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400885 description: "generates more than 1 target if needed",
886 moduleTypeUnderTest: "custom",
887 moduleTypeUnderTestFactory: customModuleFactory,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500888 bp: `custom {
889 name: "foo",
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400890 one_to_many_prop: true,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500891 bazel_module: { bp2build_available: true },
892}`,
893 expectedCount: 3,
894 },
895 }
896
897 dir := "."
898 for _, testCase := range testCases {
Liz Kammer2ada09a2021-08-11 00:17:36 -0400899 t.Run(testCase.description, func(t *testing.T) {
900 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
901 ctx := android.NewTestContext(config)
902 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Liz Kammer2ada09a2021-08-11 00:17:36 -0400903 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 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400922 moduleTypeUnderTest string
923 moduleTypeUnderTestFactory android.ModuleFactory
924 expectedCount map[string]int
925 description string
Sam Delmerico24c56032022-03-28 19:53:03 +0000926 bp2buildConfig allowlists.Bp2BuildConfig
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400927 checkDir string
928 fs map[string]string
Jingwen Chen12b4c272021-03-10 02:05:59 -0500929 }{
930 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400931 description: "test bp2build config package and subpackages config",
932 moduleTypeUnderTest: "filegroup",
933 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen12b4c272021-03-10 02:05:59 -0500934 expectedCount: map[string]int{
935 "migrated": 1,
936 "migrated/but_not_really": 0,
937 "migrated/but_not_really/but_really": 1,
938 "not_migrated": 0,
939 "also_not_migrated": 0,
940 },
Sam Delmerico24c56032022-03-28 19:53:03 +0000941 bp2buildConfig: allowlists.Bp2BuildConfig{
942 "migrated": allowlists.Bp2BuildDefaultTrueRecursively,
943 "migrated/but_not_really": allowlists.Bp2BuildDefaultFalse,
944 "not_migrated": allowlists.Bp2BuildDefaultFalse,
Jingwen Chen12b4c272021-03-10 02:05:59 -0500945 },
946 fs: map[string]string{
947 "migrated/Android.bp": `filegroup { name: "a" }`,
948 "migrated/but_not_really/Android.bp": `filegroup { name: "b" }`,
949 "migrated/but_not_really/but_really/Android.bp": `filegroup { name: "c" }`,
950 "not_migrated/Android.bp": `filegroup { name: "d" }`,
951 "also_not_migrated/Android.bp": `filegroup { name: "e" }`,
952 },
953 },
954 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400955 description: "test bp2build config opt-in and opt-out",
956 moduleTypeUnderTest: "filegroup",
957 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen12b4c272021-03-10 02:05:59 -0500958 expectedCount: map[string]int{
959 "package-opt-in": 2,
960 "package-opt-in/subpackage": 0,
961 "package-opt-out": 1,
962 "package-opt-out/subpackage": 0,
963 },
Sam Delmerico24c56032022-03-28 19:53:03 +0000964 bp2buildConfig: allowlists.Bp2BuildConfig{
965 "package-opt-in": allowlists.Bp2BuildDefaultFalse,
966 "package-opt-out": allowlists.Bp2BuildDefaultTrueRecursively,
Jingwen Chen12b4c272021-03-10 02:05:59 -0500967 },
968 fs: map[string]string{
969 "package-opt-in/Android.bp": `
970filegroup { name: "opt-in-a" }
971filegroup { name: "opt-in-b", bazel_module: { bp2build_available: true } }
972filegroup { name: "opt-in-c", bazel_module: { bp2build_available: true } }
973`,
974
975 "package-opt-in/subpackage/Android.bp": `
976filegroup { name: "opt-in-d" } // parent package not configured to DefaultTrueRecursively
977`,
978
979 "package-opt-out/Android.bp": `
980filegroup { name: "opt-out-a" }
981filegroup { name: "opt-out-b", bazel_module: { bp2build_available: false } }
982filegroup { name: "opt-out-c", bazel_module: { bp2build_available: false } }
983`,
984
985 "package-opt-out/subpackage/Android.bp": `
986filegroup { name: "opt-out-g", bazel_module: { bp2build_available: false } }
987filegroup { name: "opt-out-h", bazel_module: { bp2build_available: false } }
988`,
989 },
990 },
991 }
992
993 dir := "."
994 for _, testCase := range testCases {
995 fs := make(map[string][]byte)
996 toParse := []string{
997 "Android.bp",
998 }
999 for f, content := range testCase.fs {
1000 if strings.HasSuffix(f, "Android.bp") {
1001 toParse = append(toParse, f)
1002 }
1003 fs[f] = []byte(content)
1004 }
1005 config := android.TestConfig(buildDir, nil, "", fs)
1006 ctx := android.NewTestContext(config)
1007 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Sam Delmerico24c56032022-03-28 19:53:03 +00001008 allowlist := android.NewBp2BuildAllowlist().SetDefaultConfig(testCase.bp2buildConfig)
1009 ctx.RegisterBp2BuildConfig(allowlist)
Jingwen Chen12b4c272021-03-10 02:05:59 -05001010 ctx.RegisterForBazelConversion()
1011
1012 _, errs := ctx.ParseFileList(dir, toParse)
1013 android.FailIfErrored(t, errs)
1014 _, errs = ctx.ResolveDependencies(config)
1015 android.FailIfErrored(t, errs)
1016
1017 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
1018
1019 // For each directory, test that the expected number of generated targets is correct.
1020 for dir, expectedCount := range testCase.expectedCount {
Liz Kammer6eff3232021-08-26 08:37:59 -04001021 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
1022 android.FailIfErrored(t, err)
Jingwen Chen12b4c272021-03-10 02:05:59 -05001023 if actualCount := len(bazelTargets); actualCount != expectedCount {
1024 t.Fatalf(
1025 "%s: Expected %d bazel target for %s package, got %d",
1026 testCase.description,
1027 expectedCount,
1028 dir,
1029 actualCount)
1030 }
1031
1032 }
1033 }
1034}
1035
Liz Kammerba3ea162021-02-17 13:22:03 -05001036func TestCombineBuildFilesBp2buildTargets(t *testing.T) {
Jingwen Chen5146ac02021-09-02 11:44:42 +00001037 testCases := []bp2buildTestCase{
Liz Kammerba3ea162021-02-17 13:22:03 -05001038 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001039 description: "filegroup bazel_module.label",
1040 moduleTypeUnderTest: "filegroup",
1041 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen5146ac02021-09-02 11:44:42 +00001042 blueprint: `filegroup {
Liz Kammerba3ea162021-02-17 13:22:03 -05001043 name: "fg_foo",
1044 bazel_module: { label: "//other:fg_foo" },
1045}`,
1046 expectedBazelTargets: []string{
1047 `// BUILD file`,
1048 },
Jingwen Chen5146ac02021-09-02 11:44:42 +00001049 filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -05001050 "other/BUILD.bazel": `// BUILD file`,
1051 },
1052 },
1053 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001054 description: "multiple bazel_module.label same BUILD",
1055 moduleTypeUnderTest: "filegroup",
1056 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen5146ac02021-09-02 11:44:42 +00001057 blueprint: `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001058 name: "fg_foo",
1059 bazel_module: { label: "//other:fg_foo" },
1060 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001061
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001062 filegroup {
1063 name: "foo",
1064 bazel_module: { label: "//other:foo" },
1065 }`,
Liz Kammerba3ea162021-02-17 13:22:03 -05001066 expectedBazelTargets: []string{
1067 `// BUILD file`,
1068 },
Jingwen Chen5146ac02021-09-02 11:44:42 +00001069 filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -05001070 "other/BUILD.bazel": `// BUILD file`,
1071 },
1072 },
1073 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001074 description: "filegroup bazel_module.label and bp2build in subdir",
1075 moduleTypeUnderTest: "filegroup",
1076 moduleTypeUnderTestFactory: android.FileGroupFactory,
1077 dir: "other",
1078 blueprint: ``,
Jingwen Chen5146ac02021-09-02 11:44:42 +00001079 filesystem: map[string]string{
Jingwen Chenc63677b2021-06-17 05:43:19 +00001080 "other/Android.bp": `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001081 name: "fg_foo",
1082 bazel_module: {
1083 bp2build_available: true,
1084 },
1085 }
1086 filegroup {
1087 name: "fg_bar",
1088 bazel_module: {
1089 label: "//other:fg_bar"
1090 },
1091 }`,
Jingwen Chenc63677b2021-06-17 05:43:19 +00001092 "other/BUILD.bazel": `// definition for fg_bar`,
1093 },
Liz Kammerba3ea162021-02-17 13:22:03 -05001094 expectedBazelTargets: []string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001095 makeBazelTarget("filegroup", "fg_foo", map[string]string{}),
1096 `// definition for fg_bar`,
Liz Kammerba3ea162021-02-17 13:22:03 -05001097 },
1098 },
1099 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001100 description: "filegroup bazel_module.label and filegroup bp2build",
1101 moduleTypeUnderTest: "filegroup",
1102 moduleTypeUnderTestFactory: android.FileGroupFactory,
1103
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001104 filesystem: map[string]string{
1105 "other/BUILD.bazel": `// BUILD file`,
1106 },
Jingwen Chen5146ac02021-09-02 11:44:42 +00001107 blueprint: `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001108 name: "fg_foo",
1109 bazel_module: {
1110 label: "//other:fg_foo",
1111 },
1112 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001113
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001114 filegroup {
1115 name: "fg_bar",
1116 bazel_module: {
1117 bp2build_available: true,
1118 },
1119 }`,
Liz Kammerba3ea162021-02-17 13:22:03 -05001120 expectedBazelTargets: []string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001121 makeBazelTarget("filegroup", "fg_bar", map[string]string{}),
Liz Kammerba3ea162021-02-17 13:22:03 -05001122 `// BUILD file`,
1123 },
Liz Kammerba3ea162021-02-17 13:22:03 -05001124 },
1125 }
1126
1127 dir := "."
1128 for _, testCase := range testCases {
Jingwen Chen49109762021-05-25 05:16:48 +00001129 t.Run(testCase.description, func(t *testing.T) {
1130 fs := make(map[string][]byte)
1131 toParse := []string{
1132 "Android.bp",
Liz Kammerba3ea162021-02-17 13:22:03 -05001133 }
Jingwen Chen5146ac02021-09-02 11:44:42 +00001134 for f, content := range testCase.filesystem {
Jingwen Chen49109762021-05-25 05:16:48 +00001135 if strings.HasSuffix(f, "Android.bp") {
1136 toParse = append(toParse, f)
1137 }
1138 fs[f] = []byte(content)
1139 }
Jingwen Chen5146ac02021-09-02 11:44:42 +00001140 config := android.TestConfig(buildDir, nil, testCase.blueprint, fs)
Jingwen Chen49109762021-05-25 05:16:48 +00001141 ctx := android.NewTestContext(config)
1142 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Jingwen Chen49109762021-05-25 05:16:48 +00001143 ctx.RegisterForBazelConversion()
Liz Kammerba3ea162021-02-17 13:22:03 -05001144
Jingwen Chen49109762021-05-25 05:16:48 +00001145 _, errs := ctx.ParseFileList(dir, toParse)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001146 if errored(t, testCase, errs) {
Jingwen Chen49109762021-05-25 05:16:48 +00001147 return
1148 }
1149 _, errs = ctx.ResolveDependencies(config)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001150 if errored(t, testCase, errs) {
Jingwen Chen49109762021-05-25 05:16:48 +00001151 return
1152 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001153
Jingwen Chen49109762021-05-25 05:16:48 +00001154 checkDir := dir
1155 if testCase.dir != "" {
1156 checkDir = testCase.dir
1157 }
Liz Kammer6eff3232021-08-26 08:37:59 -04001158 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
1159 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, checkDir)
1160 android.FailIfErrored(t, err)
Jingwen Chen49109762021-05-25 05:16:48 +00001161 bazelTargets.sort()
1162 actualCount := len(bazelTargets)
1163 expectedCount := len(testCase.expectedBazelTargets)
1164 if actualCount != expectedCount {
1165 t.Errorf("Expected %d bazel target, got %d\n%s", expectedCount, actualCount, bazelTargets)
1166 }
1167 if !strings.Contains(bazelTargets.String(), "Section: Handcrafted targets. ") {
1168 t.Errorf("Expected string representation of bazelTargets to contain handcrafted section header.")
1169 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001170 for i, target := range bazelTargets {
Jingwen Chen49109762021-05-25 05:16:48 +00001171 actualContent := target.content
1172 expectedContent := testCase.expectedBazelTargets[i]
1173 if expectedContent != actualContent {
Liz Kammerba3ea162021-02-17 13:22:03 -05001174 t.Errorf(
Jingwen Chen49109762021-05-25 05:16:48 +00001175 "Expected generated Bazel target to be '%s', got '%s'",
1176 expectedContent,
1177 actualContent,
Liz Kammerba3ea162021-02-17 13:22:03 -05001178 )
1179 }
1180 }
Jingwen Chen49109762021-05-25 05:16:48 +00001181 })
Liz Kammerba3ea162021-02-17 13:22:03 -05001182 }
1183}
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001184
1185func TestGlobExcludeSrcs(t *testing.T) {
Jingwen Chen5146ac02021-09-02 11:44:42 +00001186 testCases := []bp2buildTestCase{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001187 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001188 description: "filegroup top level exclude_srcs",
1189 moduleTypeUnderTest: "filegroup",
1190 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen5146ac02021-09-02 11:44:42 +00001191 blueprint: `filegroup {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001192 name: "fg_foo",
1193 srcs: ["**/*.txt"],
1194 exclude_srcs: ["c.txt"],
1195 bazel_module: { bp2build_available: true },
1196}`,
Jingwen Chen5146ac02021-09-02 11:44:42 +00001197 filesystem: map[string]string{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001198 "a.txt": "",
1199 "b.txt": "",
1200 "c.txt": "",
1201 "dir/Android.bp": "",
1202 "dir/e.txt": "",
1203 "dir/f.txt": "",
1204 },
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001205 expectedBazelTargets: []string{
1206 makeBazelTarget("filegroup", "fg_foo", map[string]string{
1207 "srcs": `[
1208 "a.txt",
1209 "b.txt",
1210 "//dir:e.txt",
1211 "//dir:f.txt",
1212 ]`,
1213 }),
1214 },
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001215 },
1216 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001217 description: "filegroup in subdir exclude_srcs",
1218 moduleTypeUnderTest: "filegroup",
1219 moduleTypeUnderTestFactory: android.FileGroupFactory,
1220 blueprint: "",
1221 dir: "dir",
Jingwen Chen5146ac02021-09-02 11:44:42 +00001222 filesystem: map[string]string{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001223 "dir/Android.bp": `filegroup {
1224 name: "fg_foo",
1225 srcs: ["**/*.txt"],
1226 exclude_srcs: ["b.txt"],
1227 bazel_module: { bp2build_available: true },
1228}
1229`,
1230 "dir/a.txt": "",
1231 "dir/b.txt": "",
1232 "dir/subdir/Android.bp": "",
1233 "dir/subdir/e.txt": "",
1234 "dir/subdir/f.txt": "",
1235 },
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001236 expectedBazelTargets: []string{
1237 makeBazelTarget("filegroup", "fg_foo", map[string]string{
1238 "srcs": `[
Liz Kammer9abd62d2021-05-21 08:37:59 -04001239 "a.txt",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001240 "//dir/subdir:e.txt",
1241 "//dir/subdir:f.txt",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001242 ]`,
1243 }),
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001244 },
1245 },
1246 }
1247
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001248 for _, testCase := range testCases {
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001249 t.Run(testCase.description, func(t *testing.T) {
1250 runBp2BuildTestCaseSimple(t, testCase)
1251 })
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001252 }
1253}
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001254
1255func TestCommonBp2BuildModuleAttrs(t *testing.T) {
1256 testCases := []bp2buildTestCase{
1257 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001258 description: "Required into data test",
1259 moduleTypeUnderTest: "filegroup",
1260 moduleTypeUnderTestFactory: android.FileGroupFactory,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001261 blueprint: simpleModuleDoNotConvertBp2build("filegroup", "reqd") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001262filegroup {
1263 name: "fg_foo",
1264 required: ["reqd"],
1265 bazel_module: { bp2build_available: true },
1266}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001267 expectedBazelTargets: []string{
1268 makeBazelTarget("filegroup", "fg_foo", map[string]string{
1269 "data": `[":reqd"]`,
1270 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001271 },
1272 },
1273 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001274 description: "Required via arch into data test",
1275 moduleTypeUnderTest: "python_library",
1276 moduleTypeUnderTestFactory: python.PythonLibraryFactory,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001277 blueprint: simpleModuleDoNotConvertBp2build("python_library", "reqdx86") +
1278 simpleModuleDoNotConvertBp2build("python_library", "reqdarm") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001279python_library {
1280 name: "fg_foo",
1281 arch: {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001282 arm: {
1283 required: ["reqdarm"],
1284 },
1285 x86: {
1286 required: ["reqdx86"],
1287 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001288 },
1289 bazel_module: { bp2build_available: true },
1290}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001291 expectedBazelTargets: []string{
1292 makeBazelTarget("py_library", "fg_foo", map[string]string{
1293 "data": `select({
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001294 "//build/bazel/platforms/arch:arm": [":reqdarm"],
1295 "//build/bazel/platforms/arch:x86": [":reqdx86"],
1296 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001297 })`,
1298 "srcs_version": `"PY3"`,
1299 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001300 },
1301 },
1302 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001303 description: "Required appended to data test",
1304 moduleTypeUnderTest: "python_library",
1305 moduleTypeUnderTestFactory: python.PythonLibraryFactory,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001306 filesystem: map[string]string{
1307 "data.bin": "",
1308 "src.py": "",
1309 },
1310 blueprint: simpleModuleDoNotConvertBp2build("python_library", "reqd") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001311python_library {
1312 name: "fg_foo",
1313 data: ["data.bin"],
1314 required: ["reqd"],
1315 bazel_module: { bp2build_available: true },
1316}`,
1317 expectedBazelTargets: []string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001318 makeBazelTarget("py_library", "fg_foo", map[string]string{
1319 "data": `[
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001320 "data.bin",
1321 ":reqd",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001322 ]`,
1323 "srcs_version": `"PY3"`,
1324 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001325 },
1326 },
1327 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001328 description: "All props-to-attrs at once together test",
1329 moduleTypeUnderTest: "filegroup",
1330 moduleTypeUnderTestFactory: android.FileGroupFactory,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001331 blueprint: simpleModuleDoNotConvertBp2build("filegroup", "reqd") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001332filegroup {
1333 name: "fg_foo",
1334 required: ["reqd"],
1335 bazel_module: { bp2build_available: true },
1336}`,
1337 expectedBazelTargets: []string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001338 makeBazelTarget("filegroup", "fg_foo", map[string]string{
1339 "data": `[":reqd"]`,
1340 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001341 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001342 },
1343 }
1344
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001345 for _, tc := range testCases {
1346 t.Run(tc.description, func(t *testing.T) {
1347 runBp2BuildTestCaseSimple(t, tc)
1348 })
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001349 }
1350}