blob: b21a47713d929d8c1afd2b80c5f8e79a0db87482 [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 Kammerdaa09ef2021-12-15 15:35:38 -0500304 description: "non-existent dep",
305 blueprint: `custom {
306 name: "has_dep",
307 arch_paths: [":dep"],
308 bazel_module: { bp2build_available: true },
309}`,
310 expectedBazelTargets: []string{
311 makeBazelTarget("custom", "has_dep", attrNameToString{
312 "arch_paths": `[":dep__BP2BUILD__MISSING__DEP"]`,
313 }),
314 },
315 },
316 {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400317 description: "arch-variant srcs",
Jingwen Chen5146ac02021-09-02 11:44:42 +0000318 blueprint: `custom {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400319 name: "arch_paths",
320 arch: {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400321 x86: { arch_paths: ["x86.txt"] },
322 x86_64: { arch_paths: ["x86_64.txt"] },
323 arm: { arch_paths: ["arm.txt"] },
324 arm64: { arch_paths: ["arm64.txt"] },
325 },
326 target: {
327 linux: { arch_paths: ["linux.txt"] },
328 bionic: { arch_paths: ["bionic.txt"] },
329 host: { arch_paths: ["host.txt"] },
330 not_windows: { arch_paths: ["not_windows.txt"] },
331 android: { arch_paths: ["android.txt"] },
332 linux_musl: { arch_paths: ["linux_musl.txt"] },
333 musl: { arch_paths: ["musl.txt"] },
334 linux_glibc: { arch_paths: ["linux_glibc.txt"] },
335 glibc: { arch_paths: ["glibc.txt"] },
336 linux_bionic: { arch_paths: ["linux_bionic.txt"] },
337 darwin: { arch_paths: ["darwin.txt"] },
338 windows: { arch_paths: ["windows.txt"] },
339 },
340 multilib: {
341 lib32: { arch_paths: ["lib32.txt"] },
342 lib64: { arch_paths: ["lib64.txt"] },
Liz Kammer4562a3b2021-04-21 18:15:34 -0400343 },
344 bazel_module: { bp2build_available: true },
345}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500346 expectedBazelTargets: []string{
347 makeBazelTarget("custom", "arch_paths", attrNameToString{
348 "arch_paths": `select({
Liz Kammerfdd72e62021-10-11 15:41:03 -0400349 "//build/bazel/platforms/arch:arm": [
350 "arm.txt",
351 "lib32.txt",
352 ],
353 "//build/bazel/platforms/arch:arm64": [
354 "arm64.txt",
355 "lib64.txt",
356 ],
357 "//build/bazel/platforms/arch:x86": [
358 "x86.txt",
359 "lib32.txt",
360 ],
361 "//build/bazel/platforms/arch:x86_64": [
362 "x86_64.txt",
363 "lib64.txt",
364 ],
365 "//conditions:default": [],
366 }) + select({
367 "//build/bazel/platforms/os:android": [
368 "linux.txt",
369 "bionic.txt",
370 "android.txt",
371 ],
372 "//build/bazel/platforms/os:darwin": [
373 "host.txt",
374 "darwin.txt",
375 "not_windows.txt",
376 ],
377 "//build/bazel/platforms/os:linux": [
378 "host.txt",
379 "linux.txt",
380 "glibc.txt",
381 "linux_glibc.txt",
382 "not_windows.txt",
383 ],
384 "//build/bazel/platforms/os:linux_bionic": [
385 "host.txt",
386 "linux.txt",
387 "bionic.txt",
388 "linux_bionic.txt",
389 "not_windows.txt",
390 ],
391 "//build/bazel/platforms/os:linux_musl": [
392 "host.txt",
393 "linux.txt",
394 "musl.txt",
395 "linux_musl.txt",
396 "not_windows.txt",
397 ],
398 "//build/bazel/platforms/os:windows": [
399 "host.txt",
400 "windows.txt",
401 ],
Liz Kammer4562a3b2021-04-21 18:15:34 -0400402 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500403 })`,
404 }),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400405 },
406 },
407 {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400408 description: "arch-variant deps",
Jingwen Chen5146ac02021-09-02 11:44:42 +0000409 blueprint: `custom {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400410 name: "has_dep",
411 arch: {
412 x86: {
413 arch_paths: [":dep"],
414 },
415 },
416 bazel_module: { bp2build_available: true },
417}
418
419custom {
420 name: "dep",
421 arch_paths: ["abc"],
422 bazel_module: { bp2build_available: true },
423}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500424 expectedBazelTargets: []string{
425 makeBazelTarget("custom", "dep", attrNameToString{
426 "arch_paths": `["abc"]`,
427 }),
428 makeBazelTarget("custom", "has_dep", attrNameToString{
429 "arch_paths": `select({
Liz Kammer4562a3b2021-04-21 18:15:34 -0400430 "//build/bazel/platforms/arch:x86": [":dep"],
431 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500432 })`,
433 }),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400434 },
Jingwen Chen58a12b82021-03-30 13:08:36 +0000435 },
Liz Kammer32a03392021-09-14 11:17:21 -0400436 {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400437 description: "embedded props",
Liz Kammer32a03392021-09-14 11:17:21 -0400438 blueprint: `custom {
439 name: "embedded_props",
440 embedded_prop: "abc",
441 bazel_module: { bp2build_available: true },
442}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500443 expectedBazelTargets: []string{
444 makeBazelTarget("custom", "embedded_props", attrNameToString{
445 "embedded_attr": `"abc"`,
446 }),
Liz Kammer32a03392021-09-14 11:17:21 -0400447 },
448 },
449 {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400450 description: "ptr to embedded props",
Liz Kammer32a03392021-09-14 11:17:21 -0400451 blueprint: `custom {
452 name: "ptr_to_embedded_props",
453 other_embedded_prop: "abc",
454 bazel_module: { bp2build_available: true },
455}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500456 expectedBazelTargets: []string{
457 makeBazelTarget("custom", "ptr_to_embedded_props", attrNameToString{
458 "other_embedded_attr": `"abc"`,
459 }),
Liz Kammer32a03392021-09-14 11:17:21 -0400460 },
461 },
Jingwen Chen73850672020-12-14 08:25:34 -0500462 }
463
464 dir := "."
465 for _, testCase := range testCases {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400466 t.Run(testCase.description, func(t *testing.T) {
467 config := android.TestConfig(buildDir, nil, testCase.blueprint, nil)
468 ctx := android.NewTestContext(config)
Jingwen Chen164e0862021-02-19 00:48:40 -0500469
Liz Kammerfdd72e62021-10-11 15:41:03 -0400470 registerCustomModuleForBp2buildConversion(ctx)
Jingwen Chen73850672020-12-14 08:25:34 -0500471
Liz Kammerfdd72e62021-10-11 15:41:03 -0400472 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
473 if errored(t, testCase, errs) {
474 return
475 }
476 _, errs = ctx.ResolveDependencies(config)
477 if errored(t, testCase, errs) {
478 return
479 }
Jingwen Chen73850672020-12-14 08:25:34 -0500480
Liz Kammerfdd72e62021-10-11 15:41:03 -0400481 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
482 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
483 android.FailIfErrored(t, err)
Jingwen Chen164e0862021-02-19 00:48:40 -0500484
Liz Kammerfdd72e62021-10-11 15:41:03 -0400485 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400486 t.Errorf("Expected %d bazel target (%s),\ngot %d (%s)", expectedCount, testCase.expectedBazelTargets, actualCount, bazelTargets)
Liz Kammerfdd72e62021-10-11 15:41:03 -0400487 } else {
488 for i, expectedBazelTarget := range testCase.expectedBazelTargets {
489 actualBazelTarget := bazelTargets[i]
490 if actualBazelTarget.content != expectedBazelTarget {
491 t.Errorf(
492 "Expected generated Bazel target to be '%s', got '%s'",
493 expectedBazelTarget,
494 actualBazelTarget.content,
495 )
496 }
Liz Kammer4562a3b2021-04-21 18:15:34 -0400497 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500498 }
Liz Kammerfdd72e62021-10-11 15:41:03 -0400499 })
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800500 }
501}
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500502
Jingwen Chen40067de2021-01-26 21:58:43 -0500503func TestLoadStatements(t *testing.T) {
504 testCases := []struct {
505 bazelTargets BazelTargets
506 expectedLoadStatements string
507 }{
508 {
509 bazelTargets: BazelTargets{
510 BazelTarget{
511 name: "foo",
512 ruleClass: "cc_library",
513 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
514 },
515 },
516 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
517 },
518 {
519 bazelTargets: BazelTargets{
520 BazelTarget{
521 name: "foo",
522 ruleClass: "cc_library",
523 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
524 },
525 BazelTarget{
526 name: "bar",
527 ruleClass: "cc_library",
528 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
529 },
530 },
531 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
532 },
533 {
534 bazelTargets: BazelTargets{
535 BazelTarget{
536 name: "foo",
537 ruleClass: "cc_library",
538 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
539 },
540 BazelTarget{
541 name: "bar",
542 ruleClass: "cc_binary",
543 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
544 },
545 },
546 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")`,
547 },
548 {
549 bazelTargets: BazelTargets{
550 BazelTarget{
551 name: "foo",
552 ruleClass: "cc_library",
553 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
554 },
555 BazelTarget{
556 name: "bar",
557 ruleClass: "cc_binary",
558 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
559 },
560 BazelTarget{
561 name: "baz",
562 ruleClass: "java_binary",
563 bzlLoadLocation: "//build/bazel/rules:java.bzl",
564 },
565 },
566 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")
567load("//build/bazel/rules:java.bzl", "java_binary")`,
568 },
569 {
570 bazelTargets: BazelTargets{
571 BazelTarget{
572 name: "foo",
573 ruleClass: "cc_binary",
574 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
575 },
576 BazelTarget{
577 name: "bar",
578 ruleClass: "java_binary",
579 bzlLoadLocation: "//build/bazel/rules:java.bzl",
580 },
581 BazelTarget{
582 name: "baz",
583 ruleClass: "genrule",
584 // Note: no bzlLoadLocation for native rules
585 },
586 },
587 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary")
588load("//build/bazel/rules:java.bzl", "java_binary")`,
589 },
590 }
591
592 for _, testCase := range testCases {
593 actual := testCase.bazelTargets.LoadStatements()
594 expected := testCase.expectedLoadStatements
595 if actual != expected {
596 t.Fatalf("Expected load statements to be %s, got %s", expected, actual)
597 }
598 }
599
600}
601
602func TestGenerateBazelTargetModules_OneToMany_LoadedFromStarlark(t *testing.T) {
603 testCases := []struct {
604 bp string
605 expectedBazelTarget string
606 expectedBazelTargetCount int
607 expectedLoadStatements string
608 }{
609 {
610 bp: `custom {
611 name: "bar",
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400612 one_to_many_prop: true,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500613 bazel_module: { bp2build_available: true },
Jingwen Chen40067de2021-01-26 21:58:43 -0500614}`,
615 expectedBazelTarget: `my_library(
616 name = "bar",
617)
618
Jingwen Chen40067de2021-01-26 21:58:43 -0500619proto_library(
620 name = "bar_proto_library_deps",
Liz Kammer2ada09a2021-08-11 00:17:36 -0400621)
622
623my_proto_library(
624 name = "bar_my_proto_library_deps",
Jingwen Chen40067de2021-01-26 21:58:43 -0500625)`,
626 expectedBazelTargetCount: 3,
627 expectedLoadStatements: `load("//build/bazel/rules:proto.bzl", "my_proto_library", "proto_library")
628load("//build/bazel/rules:rules.bzl", "my_library")`,
629 },
630 }
631
632 dir := "."
633 for _, testCase := range testCases {
634 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
635 ctx := android.NewTestContext(config)
636 ctx.RegisterModuleType("custom", customModuleFactory)
Jingwen Chen40067de2021-01-26 21:58:43 -0500637 ctx.RegisterForBazelConversion()
638
639 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
640 android.FailIfErrored(t, errs)
641 _, errs = ctx.ResolveDependencies(config)
642 android.FailIfErrored(t, errs)
643
Jingwen Chen164e0862021-02-19 00:48:40 -0500644 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Liz Kammer6eff3232021-08-26 08:37:59 -0400645 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
646 android.FailIfErrored(t, err)
Jingwen Chen40067de2021-01-26 21:58:43 -0500647 if actualCount := len(bazelTargets); actualCount != testCase.expectedBazelTargetCount {
648 t.Fatalf("Expected %d bazel target, got %d", testCase.expectedBazelTargetCount, actualCount)
649 }
650
651 actualBazelTargets := bazelTargets.String()
652 if actualBazelTargets != testCase.expectedBazelTarget {
653 t.Errorf(
654 "Expected generated Bazel target to be '%s', got '%s'",
655 testCase.expectedBazelTarget,
656 actualBazelTargets,
657 )
658 }
659
660 actualLoadStatements := bazelTargets.LoadStatements()
661 if actualLoadStatements != testCase.expectedLoadStatements {
662 t.Errorf(
663 "Expected generated load statements to be '%s', got '%s'",
664 testCase.expectedLoadStatements,
665 actualLoadStatements,
666 )
667 }
668 }
669}
670
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500671func TestModuleTypeBp2Build(t *testing.T) {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000672 testCases := []bp2buildTestCase{
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500673 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400674 description: "filegroup with does not specify srcs",
675 moduleTypeUnderTest: "filegroup",
676 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000677 blueprint: `filegroup {
Liz Kammerebfcf672021-02-16 15:00:05 -0500678 name: "fg_foo",
679 bazel_module: { bp2build_available: true },
680}`,
681 expectedBazelTargets: []string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500682 makeBazelTarget("filegroup", "fg_foo", map[string]string{}),
Liz Kammerebfcf672021-02-16 15:00:05 -0500683 },
684 },
685 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400686 description: "filegroup with no 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: [],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500692 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500693}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500694 expectedBazelTargets: []string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500695 makeBazelTarget("filegroup", "fg_foo", map[string]string{}),
Liz Kammer356f7d42021-01-26 09:18:53 -0500696 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500697 },
698 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400699 description: "filegroup with srcs",
700 moduleTypeUnderTest: "filegroup",
701 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000702 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500703 name: "fg_foo",
704 srcs: ["a", "b"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500705 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500706}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500707 expectedBazelTargets: []string{
708 makeBazelTarget("filegroup", "fg_foo", map[string]string{
709 "srcs": `[
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500710 "a",
711 "b",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500712 ]`,
713 }),
Liz Kammer356f7d42021-01-26 09:18:53 -0500714 },
715 },
716 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400717 description: "filegroup with excludes srcs",
718 moduleTypeUnderTest: "filegroup",
719 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000720 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500721 name: "fg_foo",
722 srcs: ["a", "b"],
723 exclude_srcs: ["a"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500724 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500725}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500726 expectedBazelTargets: []string{
727 makeBazelTarget("filegroup", "fg_foo", map[string]string{
728 "srcs": `["b"]`,
729 }),
Liz Kammer356f7d42021-01-26 09:18:53 -0500730 },
731 },
732 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400733 description: "filegroup with glob",
734 moduleTypeUnderTest: "filegroup",
735 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000736 blueprint: `filegroup {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500737 name: "fg_foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500738 srcs: ["**/*.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500739 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500740}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500741 expectedBazelTargets: []string{
742 makeBazelTarget("filegroup", "fg_foo", map[string]string{
743 "srcs": `[
Liz Kammer356f7d42021-01-26 09:18:53 -0500744 "other/a.txt",
745 "other/b.txt",
746 "other/subdir/a.txt",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500747 ]`,
748 }),
Liz Kammer356f7d42021-01-26 09:18:53 -0500749 },
Jingwen Chen5146ac02021-09-02 11:44:42 +0000750 filesystem: map[string]string{
Liz Kammer356f7d42021-01-26 09:18:53 -0500751 "other/a.txt": "",
752 "other/b.txt": "",
753 "other/subdir/a.txt": "",
754 "other/file": "",
755 },
756 },
757 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400758 description: "filegroup with glob in subdir",
759 moduleTypeUnderTest: "filegroup",
760 moduleTypeUnderTestFactory: android.FileGroupFactory,
761 dir: "other",
Jingwen Chen5146ac02021-09-02 11:44:42 +0000762 filesystem: map[string]string{
Liz Kammer356f7d42021-01-26 09:18:53 -0500763 "other/Android.bp": `filegroup {
764 name: "fg_foo",
765 srcs: ["**/*.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500766 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500767}`,
768 "other/a.txt": "",
769 "other/b.txt": "",
770 "other/subdir/a.txt": "",
771 "other/file": "",
772 },
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500773 expectedBazelTargets: []string{
774 makeBazelTarget("filegroup", "fg_foo", map[string]string{
775 "srcs": `[
776 "a.txt",
777 "b.txt",
778 "subdir/a.txt",
779 ]`,
780 }),
781 },
Liz Kammer356f7d42021-01-26 09:18:53 -0500782 },
783 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400784 description: "depends_on_other_dir_module",
785 moduleTypeUnderTest: "filegroup",
786 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000787 blueprint: `filegroup {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500788 name: "fg_foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500789 srcs: [
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000790 ":foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500791 "c",
792 ],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500793 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500794}`,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000795 filesystem: map[string]string{
Liz Kammer356f7d42021-01-26 09:18:53 -0500796 "other/Android.bp": `filegroup {
797 name: "foo",
798 srcs: ["a", "b"],
Liz Kammer6eff3232021-08-26 08:37:59 -0400799 bazel_module: { bp2build_available: true },
800}`,
801 },
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500802 expectedBazelTargets: []string{
803 makeBazelTarget("filegroup", "fg_foo", map[string]string{
804 "srcs": `[
805 "//other:foo",
806 "c",
807 ]`,
808 }),
809 },
Liz Kammer6eff3232021-08-26 08:37:59 -0400810 },
811 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400812 description: "depends_on_other_unconverted_module_error",
813 moduleTypeUnderTest: "filegroup",
814 moduleTypeUnderTestFactory: android.FileGroupFactory,
815 unconvertedDepsMode: errorModulesUnconvertedDeps,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500816 blueprint: `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400817 name: "foobar",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500818 srcs: [
819 ":foo",
820 "c",
821 ],
822 bazel_module: { bp2build_available: true },
823}`,
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400824 expectedErr: fmt.Errorf(`"foobar" depends on unconverted modules: foo`),
825 filesystem: map[string]string{
826 "other/Android.bp": `filegroup {
827 name: "foo",
828 srcs: ["a", "b"],
829}`,
830 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500831 },
832 }
833
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500834 for _, testCase := range testCases {
Liz Kammer6eff3232021-08-26 08:37:59 -0400835 t.Run(testCase.description, func(t *testing.T) {
836 runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, testCase)
837 })
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500838 }
839}
Jingwen Chen041b1842021-02-01 00:23:25 -0500840
841type bp2buildMutator = func(android.TopDownMutatorContext)
842
Jingwen Chen12b4c272021-03-10 02:05:59 -0500843func TestAllowlistingBp2buildTargetsExplicitly(t *testing.T) {
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500844 testCases := []struct {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400845 moduleTypeUnderTest string
846 moduleTypeUnderTestFactory android.ModuleFactory
847 bp string
848 expectedCount int
849 description string
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500850 }{
851 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400852 description: "explicitly unavailable",
853 moduleTypeUnderTest: "filegroup",
854 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500855 bp: `filegroup {
856 name: "foo",
857 srcs: ["a", "b"],
858 bazel_module: { bp2build_available: false },
859}`,
860 expectedCount: 0,
861 },
862 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400863 description: "implicitly unavailable",
864 moduleTypeUnderTest: "filegroup",
865 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500866 bp: `filegroup {
867 name: "foo",
868 srcs: ["a", "b"],
869}`,
870 expectedCount: 0,
871 },
872 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400873 description: "explicitly available",
874 moduleTypeUnderTest: "filegroup",
875 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500876 bp: `filegroup {
877 name: "foo",
878 srcs: ["a", "b"],
879 bazel_module: { bp2build_available: true },
880}`,
881 expectedCount: 1,
882 },
883 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400884 description: "generates more than 1 target if needed",
885 moduleTypeUnderTest: "custom",
886 moduleTypeUnderTestFactory: customModuleFactory,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500887 bp: `custom {
888 name: "foo",
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400889 one_to_many_prop: true,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500890 bazel_module: { bp2build_available: true },
891}`,
892 expectedCount: 3,
893 },
894 }
895
896 dir := "."
897 for _, testCase := range testCases {
Liz Kammer2ada09a2021-08-11 00:17:36 -0400898 t.Run(testCase.description, func(t *testing.T) {
899 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
900 ctx := android.NewTestContext(config)
901 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Liz Kammer2ada09a2021-08-11 00:17:36 -0400902 ctx.RegisterForBazelConversion()
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500903
Liz Kammer2ada09a2021-08-11 00:17:36 -0400904 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
905 android.FailIfErrored(t, errs)
906 _, errs = ctx.ResolveDependencies(config)
907 android.FailIfErrored(t, errs)
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500908
Liz Kammer2ada09a2021-08-11 00:17:36 -0400909 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Liz Kammer6eff3232021-08-26 08:37:59 -0400910 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
911 android.FailIfErrored(t, err)
Liz Kammer2ada09a2021-08-11 00:17:36 -0400912 if actualCount := len(bazelTargets); actualCount != testCase.expectedCount {
913 t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, testCase.expectedCount, actualCount)
914 }
915 })
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500916 }
917}
Liz Kammerba3ea162021-02-17 13:22:03 -0500918
Jingwen Chen12b4c272021-03-10 02:05:59 -0500919func TestAllowlistingBp2buildTargetsWithConfig(t *testing.T) {
920 testCases := []struct {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400921 moduleTypeUnderTest string
922 moduleTypeUnderTestFactory android.ModuleFactory
923 expectedCount map[string]int
924 description string
925 bp2buildConfig android.Bp2BuildConfig
926 checkDir string
927 fs map[string]string
Jingwen Chen12b4c272021-03-10 02:05:59 -0500928 }{
929 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400930 description: "test bp2build config package and subpackages config",
931 moduleTypeUnderTest: "filegroup",
932 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen12b4c272021-03-10 02:05:59 -0500933 expectedCount: map[string]int{
934 "migrated": 1,
935 "migrated/but_not_really": 0,
936 "migrated/but_not_really/but_really": 1,
937 "not_migrated": 0,
938 "also_not_migrated": 0,
939 },
940 bp2buildConfig: android.Bp2BuildConfig{
941 "migrated": android.Bp2BuildDefaultTrueRecursively,
942 "migrated/but_not_really": android.Bp2BuildDefaultFalse,
943 "not_migrated": android.Bp2BuildDefaultFalse,
944 },
945 fs: map[string]string{
946 "migrated/Android.bp": `filegroup { name: "a" }`,
947 "migrated/but_not_really/Android.bp": `filegroup { name: "b" }`,
948 "migrated/but_not_really/but_really/Android.bp": `filegroup { name: "c" }`,
949 "not_migrated/Android.bp": `filegroup { name: "d" }`,
950 "also_not_migrated/Android.bp": `filegroup { name: "e" }`,
951 },
952 },
953 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400954 description: "test bp2build config opt-in and opt-out",
955 moduleTypeUnderTest: "filegroup",
956 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen12b4c272021-03-10 02:05:59 -0500957 expectedCount: map[string]int{
958 "package-opt-in": 2,
959 "package-opt-in/subpackage": 0,
960 "package-opt-out": 1,
961 "package-opt-out/subpackage": 0,
962 },
963 bp2buildConfig: android.Bp2BuildConfig{
964 "package-opt-in": android.Bp2BuildDefaultFalse,
965 "package-opt-out": android.Bp2BuildDefaultTrueRecursively,
966 },
967 fs: map[string]string{
968 "package-opt-in/Android.bp": `
969filegroup { name: "opt-in-a" }
970filegroup { name: "opt-in-b", bazel_module: { bp2build_available: true } }
971filegroup { name: "opt-in-c", bazel_module: { bp2build_available: true } }
972`,
973
974 "package-opt-in/subpackage/Android.bp": `
975filegroup { name: "opt-in-d" } // parent package not configured to DefaultTrueRecursively
976`,
977
978 "package-opt-out/Android.bp": `
979filegroup { name: "opt-out-a" }
980filegroup { name: "opt-out-b", bazel_module: { bp2build_available: false } }
981filegroup { name: "opt-out-c", bazel_module: { bp2build_available: false } }
982`,
983
984 "package-opt-out/subpackage/Android.bp": `
985filegroup { name: "opt-out-g", bazel_module: { bp2build_available: false } }
986filegroup { name: "opt-out-h", bazel_module: { bp2build_available: false } }
987`,
988 },
989 },
990 }
991
992 dir := "."
993 for _, testCase := range testCases {
994 fs := make(map[string][]byte)
995 toParse := []string{
996 "Android.bp",
997 }
998 for f, content := range testCase.fs {
999 if strings.HasSuffix(f, "Android.bp") {
1000 toParse = append(toParse, f)
1001 }
1002 fs[f] = []byte(content)
1003 }
1004 config := android.TestConfig(buildDir, nil, "", fs)
1005 ctx := android.NewTestContext(config)
1006 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Jingwen Chen12b4c272021-03-10 02:05:59 -05001007 ctx.RegisterBp2BuildConfig(testCase.bp2buildConfig)
1008 ctx.RegisterForBazelConversion()
1009
1010 _, errs := ctx.ParseFileList(dir, toParse)
1011 android.FailIfErrored(t, errs)
1012 _, errs = ctx.ResolveDependencies(config)
1013 android.FailIfErrored(t, errs)
1014
1015 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
1016
1017 // For each directory, test that the expected number of generated targets is correct.
1018 for dir, expectedCount := range testCase.expectedCount {
Liz Kammer6eff3232021-08-26 08:37:59 -04001019 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
1020 android.FailIfErrored(t, err)
Jingwen Chen12b4c272021-03-10 02:05:59 -05001021 if actualCount := len(bazelTargets); actualCount != expectedCount {
1022 t.Fatalf(
1023 "%s: Expected %d bazel target for %s package, got %d",
1024 testCase.description,
1025 expectedCount,
1026 dir,
1027 actualCount)
1028 }
1029
1030 }
1031 }
1032}
1033
Liz Kammerba3ea162021-02-17 13:22:03 -05001034func TestCombineBuildFilesBp2buildTargets(t *testing.T) {
Jingwen Chen5146ac02021-09-02 11:44:42 +00001035 testCases := []bp2buildTestCase{
Liz Kammerba3ea162021-02-17 13:22:03 -05001036 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001037 description: "filegroup bazel_module.label",
1038 moduleTypeUnderTest: "filegroup",
1039 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen5146ac02021-09-02 11:44:42 +00001040 blueprint: `filegroup {
Liz Kammerba3ea162021-02-17 13:22:03 -05001041 name: "fg_foo",
1042 bazel_module: { label: "//other:fg_foo" },
1043}`,
1044 expectedBazelTargets: []string{
1045 `// BUILD file`,
1046 },
Jingwen Chen5146ac02021-09-02 11:44:42 +00001047 filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -05001048 "other/BUILD.bazel": `// BUILD file`,
1049 },
1050 },
1051 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001052 description: "multiple bazel_module.label same BUILD",
1053 moduleTypeUnderTest: "filegroup",
1054 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen5146ac02021-09-02 11:44:42 +00001055 blueprint: `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001056 name: "fg_foo",
1057 bazel_module: { label: "//other:fg_foo" },
1058 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001059
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001060 filegroup {
1061 name: "foo",
1062 bazel_module: { label: "//other:foo" },
1063 }`,
Liz Kammerba3ea162021-02-17 13:22:03 -05001064 expectedBazelTargets: []string{
1065 `// BUILD file`,
1066 },
Jingwen Chen5146ac02021-09-02 11:44:42 +00001067 filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -05001068 "other/BUILD.bazel": `// BUILD file`,
1069 },
1070 },
1071 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001072 description: "filegroup bazel_module.label and bp2build in subdir",
1073 moduleTypeUnderTest: "filegroup",
1074 moduleTypeUnderTestFactory: android.FileGroupFactory,
1075 dir: "other",
1076 blueprint: ``,
Jingwen Chen5146ac02021-09-02 11:44:42 +00001077 filesystem: map[string]string{
Jingwen Chenc63677b2021-06-17 05:43:19 +00001078 "other/Android.bp": `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001079 name: "fg_foo",
1080 bazel_module: {
1081 bp2build_available: true,
1082 },
1083 }
1084 filegroup {
1085 name: "fg_bar",
1086 bazel_module: {
1087 label: "//other:fg_bar"
1088 },
1089 }`,
Jingwen Chenc63677b2021-06-17 05:43:19 +00001090 "other/BUILD.bazel": `// definition for fg_bar`,
1091 },
Liz Kammerba3ea162021-02-17 13:22:03 -05001092 expectedBazelTargets: []string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001093 makeBazelTarget("filegroup", "fg_foo", map[string]string{}),
1094 `// definition for fg_bar`,
Liz Kammerba3ea162021-02-17 13:22:03 -05001095 },
1096 },
1097 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001098 description: "filegroup bazel_module.label and filegroup bp2build",
1099 moduleTypeUnderTest: "filegroup",
1100 moduleTypeUnderTestFactory: android.FileGroupFactory,
1101
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001102 filesystem: map[string]string{
1103 "other/BUILD.bazel": `// BUILD file`,
1104 },
Jingwen Chen5146ac02021-09-02 11:44:42 +00001105 blueprint: `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001106 name: "fg_foo",
1107 bazel_module: {
1108 label: "//other:fg_foo",
1109 },
1110 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001111
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001112 filegroup {
1113 name: "fg_bar",
1114 bazel_module: {
1115 bp2build_available: true,
1116 },
1117 }`,
Liz Kammerba3ea162021-02-17 13:22:03 -05001118 expectedBazelTargets: []string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001119 makeBazelTarget("filegroup", "fg_bar", map[string]string{}),
Liz Kammerba3ea162021-02-17 13:22:03 -05001120 `// BUILD file`,
1121 },
Liz Kammerba3ea162021-02-17 13:22:03 -05001122 },
1123 }
1124
1125 dir := "."
1126 for _, testCase := range testCases {
Jingwen Chen49109762021-05-25 05:16:48 +00001127 t.Run(testCase.description, func(t *testing.T) {
1128 fs := make(map[string][]byte)
1129 toParse := []string{
1130 "Android.bp",
Liz Kammerba3ea162021-02-17 13:22:03 -05001131 }
Jingwen Chen5146ac02021-09-02 11:44:42 +00001132 for f, content := range testCase.filesystem {
Jingwen Chen49109762021-05-25 05:16:48 +00001133 if strings.HasSuffix(f, "Android.bp") {
1134 toParse = append(toParse, f)
1135 }
1136 fs[f] = []byte(content)
1137 }
Jingwen Chen5146ac02021-09-02 11:44:42 +00001138 config := android.TestConfig(buildDir, nil, testCase.blueprint, fs)
Jingwen Chen49109762021-05-25 05:16:48 +00001139 ctx := android.NewTestContext(config)
1140 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Jingwen Chen49109762021-05-25 05:16:48 +00001141 ctx.RegisterForBazelConversion()
Liz Kammerba3ea162021-02-17 13:22:03 -05001142
Jingwen Chen49109762021-05-25 05:16:48 +00001143 _, errs := ctx.ParseFileList(dir, toParse)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001144 if errored(t, testCase, errs) {
Jingwen Chen49109762021-05-25 05:16:48 +00001145 return
1146 }
1147 _, errs = ctx.ResolveDependencies(config)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001148 if errored(t, testCase, errs) {
Jingwen Chen49109762021-05-25 05:16:48 +00001149 return
1150 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001151
Jingwen Chen49109762021-05-25 05:16:48 +00001152 checkDir := dir
1153 if testCase.dir != "" {
1154 checkDir = testCase.dir
1155 }
Liz Kammer6eff3232021-08-26 08:37:59 -04001156 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
1157 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, checkDir)
1158 android.FailIfErrored(t, err)
Jingwen Chen49109762021-05-25 05:16:48 +00001159 bazelTargets.sort()
1160 actualCount := len(bazelTargets)
1161 expectedCount := len(testCase.expectedBazelTargets)
1162 if actualCount != expectedCount {
1163 t.Errorf("Expected %d bazel target, got %d\n%s", expectedCount, actualCount, bazelTargets)
1164 }
1165 if !strings.Contains(bazelTargets.String(), "Section: Handcrafted targets. ") {
1166 t.Errorf("Expected string representation of bazelTargets to contain handcrafted section header.")
1167 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001168 for i, target := range bazelTargets {
Jingwen Chen49109762021-05-25 05:16:48 +00001169 actualContent := target.content
1170 expectedContent := testCase.expectedBazelTargets[i]
1171 if expectedContent != actualContent {
Liz Kammerba3ea162021-02-17 13:22:03 -05001172 t.Errorf(
Jingwen Chen49109762021-05-25 05:16:48 +00001173 "Expected generated Bazel target to be '%s', got '%s'",
1174 expectedContent,
1175 actualContent,
Liz Kammerba3ea162021-02-17 13:22:03 -05001176 )
1177 }
1178 }
Jingwen Chen49109762021-05-25 05:16:48 +00001179 })
Liz Kammerba3ea162021-02-17 13:22:03 -05001180 }
1181}
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001182
1183func TestGlobExcludeSrcs(t *testing.T) {
Jingwen Chen5146ac02021-09-02 11:44:42 +00001184 testCases := []bp2buildTestCase{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001185 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001186 description: "filegroup top level exclude_srcs",
1187 moduleTypeUnderTest: "filegroup",
1188 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen5146ac02021-09-02 11:44:42 +00001189 blueprint: `filegroup {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001190 name: "fg_foo",
1191 srcs: ["**/*.txt"],
1192 exclude_srcs: ["c.txt"],
1193 bazel_module: { bp2build_available: true },
1194}`,
Jingwen Chen5146ac02021-09-02 11:44:42 +00001195 filesystem: map[string]string{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001196 "a.txt": "",
1197 "b.txt": "",
1198 "c.txt": "",
1199 "dir/Android.bp": "",
1200 "dir/e.txt": "",
1201 "dir/f.txt": "",
1202 },
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001203 expectedBazelTargets: []string{
1204 makeBazelTarget("filegroup", "fg_foo", map[string]string{
1205 "srcs": `[
1206 "a.txt",
1207 "b.txt",
1208 "//dir:e.txt",
1209 "//dir:f.txt",
1210 ]`,
1211 }),
1212 },
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001213 },
1214 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001215 description: "filegroup in subdir exclude_srcs",
1216 moduleTypeUnderTest: "filegroup",
1217 moduleTypeUnderTestFactory: android.FileGroupFactory,
1218 blueprint: "",
1219 dir: "dir",
Jingwen Chen5146ac02021-09-02 11:44:42 +00001220 filesystem: map[string]string{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001221 "dir/Android.bp": `filegroup {
1222 name: "fg_foo",
1223 srcs: ["**/*.txt"],
1224 exclude_srcs: ["b.txt"],
1225 bazel_module: { bp2build_available: true },
1226}
1227`,
1228 "dir/a.txt": "",
1229 "dir/b.txt": "",
1230 "dir/subdir/Android.bp": "",
1231 "dir/subdir/e.txt": "",
1232 "dir/subdir/f.txt": "",
1233 },
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001234 expectedBazelTargets: []string{
1235 makeBazelTarget("filegroup", "fg_foo", map[string]string{
1236 "srcs": `[
Liz Kammer9abd62d2021-05-21 08:37:59 -04001237 "a.txt",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001238 "//dir/subdir:e.txt",
1239 "//dir/subdir:f.txt",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001240 ]`,
1241 }),
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001242 },
1243 },
1244 }
1245
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001246 for _, testCase := range testCases {
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001247 t.Run(testCase.description, func(t *testing.T) {
1248 runBp2BuildTestCaseSimple(t, testCase)
1249 })
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001250 }
1251}
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001252
1253func TestCommonBp2BuildModuleAttrs(t *testing.T) {
1254 testCases := []bp2buildTestCase{
1255 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001256 description: "Required into data test",
1257 moduleTypeUnderTest: "filegroup",
1258 moduleTypeUnderTestFactory: android.FileGroupFactory,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001259 blueprint: simpleModuleDoNotConvertBp2build("filegroup", "reqd") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001260filegroup {
1261 name: "fg_foo",
1262 required: ["reqd"],
1263 bazel_module: { bp2build_available: true },
1264}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001265 expectedBazelTargets: []string{
1266 makeBazelTarget("filegroup", "fg_foo", map[string]string{
1267 "data": `[":reqd"]`,
1268 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001269 },
1270 },
1271 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001272 description: "Required via arch into data test",
1273 moduleTypeUnderTest: "python_library",
1274 moduleTypeUnderTestFactory: python.PythonLibraryFactory,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001275 blueprint: simpleModuleDoNotConvertBp2build("python_library", "reqdx86") +
1276 simpleModuleDoNotConvertBp2build("python_library", "reqdarm") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001277python_library {
1278 name: "fg_foo",
1279 arch: {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001280 arm: {
1281 required: ["reqdarm"],
1282 },
1283 x86: {
1284 required: ["reqdx86"],
1285 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001286 },
1287 bazel_module: { bp2build_available: true },
1288}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001289 expectedBazelTargets: []string{
1290 makeBazelTarget("py_library", "fg_foo", map[string]string{
1291 "data": `select({
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001292 "//build/bazel/platforms/arch:arm": [":reqdarm"],
1293 "//build/bazel/platforms/arch:x86": [":reqdx86"],
1294 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001295 })`,
1296 "srcs_version": `"PY3"`,
1297 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001298 },
1299 },
1300 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001301 description: "Required appended to data test",
1302 moduleTypeUnderTest: "python_library",
1303 moduleTypeUnderTestFactory: python.PythonLibraryFactory,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001304 filesystem: map[string]string{
1305 "data.bin": "",
1306 "src.py": "",
1307 },
1308 blueprint: simpleModuleDoNotConvertBp2build("python_library", "reqd") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001309python_library {
1310 name: "fg_foo",
1311 data: ["data.bin"],
1312 required: ["reqd"],
1313 bazel_module: { bp2build_available: true },
1314}`,
1315 expectedBazelTargets: []string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001316 makeBazelTarget("py_library", "fg_foo", map[string]string{
1317 "data": `[
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001318 "data.bin",
1319 ":reqd",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001320 ]`,
1321 "srcs_version": `"PY3"`,
1322 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001323 },
1324 },
1325 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001326 description: "All props-to-attrs at once together test",
1327 moduleTypeUnderTest: "filegroup",
1328 moduleTypeUnderTestFactory: android.FileGroupFactory,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001329 blueprint: simpleModuleDoNotConvertBp2build("filegroup", "reqd") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001330filegroup {
1331 name: "fg_foo",
1332 required: ["reqd"],
1333 bazel_module: { bp2build_available: true },
1334}`,
1335 expectedBazelTargets: []string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001336 makeBazelTarget("filegroup", "fg_foo", map[string]string{
1337 "data": `[":reqd"]`,
1338 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001339 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001340 },
1341 }
1342
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001343 for _, tc := range testCases {
1344 t.Run(tc.description, func(t *testing.T) {
1345 runBp2BuildTestCaseSimple(t, tc)
1346 })
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001347 }
1348}