blob: 8433f5213f43601c60101d20daaae8b212f2011c [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 Kammerdfeb1202022-05-13 17:20:20 -0400204 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
Liz Kammerd366c902021-06-03 13:43:01 -0400205 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
Paul Duffinc6390592022-11-04 13:35:21 +0000212 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) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000232 testCases := []Bp2buildTestCase{
Jingwen Chen73850672020-12-14 08:25:34 -0500233 {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000234 Description: "string prop empty",
235 Blueprint: `custom {
236 name: "foo",
237 string_literal_prop: "",
238 bazel_module: { bp2build_available: true },
239}`,
240 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000241 MakeBazelTarget("custom", "foo", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000242 "string_literal_prop": `""`,
243 }),
244 },
245 },
246 {
247 Description: `string prop "PROP"`,
248 Blueprint: `custom {
249 name: "foo",
250 string_literal_prop: "PROP",
251 bazel_module: { bp2build_available: true },
252}`,
253 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000254 MakeBazelTarget("custom", "foo", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000255 "string_literal_prop": `"PROP"`,
256 }),
257 },
258 },
259 {
260 Description: `string prop arch variant`,
261 Blueprint: `custom {
262 name: "foo",
263 arch: {
264 arm: { string_literal_prop: "ARM" },
265 arm64: { string_literal_prop: "ARM64" },
266 },
267 bazel_module: { bp2build_available: true },
268}`,
269 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000270 MakeBazelTarget("custom", "foo", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000271 "string_literal_prop": `select({
272 "//build/bazel/platforms/arch:arm": "ARM",
273 "//build/bazel/platforms/arch:arm64": "ARM64",
274 "//conditions:default": None,
275 })`,
276 }),
277 },
278 },
279 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000280 Description: "string ptr props",
281 Blueprint: `custom {
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500282 name: "foo",
283 string_ptr_prop: "",
284 bazel_module: { bp2build_available: true },
285}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000286 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000287 MakeBazelTarget("custom", "foo", AttrNameToString{
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500288 "string_ptr_prop": `""`,
289 }),
290 },
291 },
292 {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000293 Description: "string list props",
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000294 Blueprint: `custom {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400295 name: "foo",
Jingwen Chen73850672020-12-14 08:25:34 -0500296 string_list_prop: ["a", "b"],
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500297 string_ptr_prop: "a",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500298 bazel_module: { bp2build_available: true },
Jingwen Chen73850672020-12-14 08:25:34 -0500299}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000300 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000301 MakeBazelTarget("custom", "foo", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500302 "string_list_prop": `[
Jingwen Chen73850672020-12-14 08:25:34 -0500303 "a",
304 "b",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500305 ]`,
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500306 "string_ptr_prop": `"a"`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500307 }),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400308 },
Jingwen Chen73850672020-12-14 08:25:34 -0500309 },
Jingwen Chen58a12b82021-03-30 13:08:36 +0000310 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000311 Description: "control characters",
312 Blueprint: `custom {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500313 name: "foo",
Jingwen Chen58a12b82021-03-30 13:08:36 +0000314 string_list_prop: ["\t", "\n"],
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500315 string_ptr_prop: "a\t\n\r",
Jingwen Chen58a12b82021-03-30 13:08:36 +0000316 bazel_module: { bp2build_available: true },
317}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000318 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000319 MakeBazelTarget("custom", "foo", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500320 "string_list_prop": `[
Jingwen Chen58a12b82021-03-30 13:08:36 +0000321 "\t",
322 "\n",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500323 ]`,
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500324 "string_ptr_prop": `"a\t\n\r"`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500325 }),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400326 },
327 },
328 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000329 Description: "handles dep",
330 Blueprint: `custom {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400331 name: "has_dep",
332 arch_paths: [":dep"],
333 bazel_module: { bp2build_available: true },
334}
335
336custom {
337 name: "dep",
338 arch_paths: ["abc"],
339 bazel_module: { bp2build_available: true },
340}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000341 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000342 MakeBazelTarget("custom", "dep", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500343 "arch_paths": `["abc"]`,
344 }),
Alixe06d75b2022-08-31 18:28:19 +0000345 MakeBazelTarget("custom", "has_dep", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500346 "arch_paths": `[":dep"]`,
347 }),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400348 },
349 },
350 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000351 Description: "non-existent dep",
352 Blueprint: `custom {
Liz Kammerdaa09ef2021-12-15 15:35:38 -0500353 name: "has_dep",
354 arch_paths: [":dep"],
355 bazel_module: { bp2build_available: true },
356}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000357 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000358 MakeBazelTarget("custom", "has_dep", AttrNameToString{
Liz Kammerdaa09ef2021-12-15 15:35:38 -0500359 "arch_paths": `[":dep__BP2BUILD__MISSING__DEP"]`,
360 }),
361 },
362 },
363 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000364 Description: "arch-variant srcs",
365 Blueprint: `custom {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400366 name: "arch_paths",
367 arch: {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400368 x86: { arch_paths: ["x86.txt"] },
369 x86_64: { arch_paths: ["x86_64.txt"] },
370 arm: { arch_paths: ["arm.txt"] },
371 arm64: { arch_paths: ["arm64.txt"] },
Colin Crossf05b0d32022-07-14 18:10:34 -0700372 riscv64: { arch_paths: ["riscv64.txt"] },
Liz Kammerfdd72e62021-10-11 15:41:03 -0400373 },
374 target: {
375 linux: { arch_paths: ["linux.txt"] },
376 bionic: { arch_paths: ["bionic.txt"] },
377 host: { arch_paths: ["host.txt"] },
378 not_windows: { arch_paths: ["not_windows.txt"] },
379 android: { arch_paths: ["android.txt"] },
380 linux_musl: { arch_paths: ["linux_musl.txt"] },
381 musl: { arch_paths: ["musl.txt"] },
382 linux_glibc: { arch_paths: ["linux_glibc.txt"] },
383 glibc: { arch_paths: ["glibc.txt"] },
384 linux_bionic: { arch_paths: ["linux_bionic.txt"] },
385 darwin: { arch_paths: ["darwin.txt"] },
386 windows: { arch_paths: ["windows.txt"] },
387 },
388 multilib: {
389 lib32: { arch_paths: ["lib32.txt"] },
390 lib64: { arch_paths: ["lib64.txt"] },
Liz Kammer4562a3b2021-04-21 18:15:34 -0400391 },
392 bazel_module: { bp2build_available: true },
393}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000394 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000395 MakeBazelTarget("custom", "arch_paths", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500396 "arch_paths": `select({
Liz Kammerfdd72e62021-10-11 15:41:03 -0400397 "//build/bazel/platforms/arch:arm": [
398 "arm.txt",
399 "lib32.txt",
400 ],
401 "//build/bazel/platforms/arch:arm64": [
402 "arm64.txt",
403 "lib64.txt",
404 ],
Colin Crossf05b0d32022-07-14 18:10:34 -0700405 "//build/bazel/platforms/arch:riscv64": [
406 "riscv64.txt",
407 "lib64.txt",
408 ],
Liz Kammerfdd72e62021-10-11 15:41:03 -0400409 "//build/bazel/platforms/arch:x86": [
410 "x86.txt",
411 "lib32.txt",
412 ],
413 "//build/bazel/platforms/arch:x86_64": [
414 "x86_64.txt",
415 "lib64.txt",
416 ],
417 "//conditions:default": [],
418 }) + select({
419 "//build/bazel/platforms/os:android": [
420 "linux.txt",
421 "bionic.txt",
422 "android.txt",
423 ],
424 "//build/bazel/platforms/os:darwin": [
425 "host.txt",
426 "darwin.txt",
427 "not_windows.txt",
428 ],
Liz Kammerfdd72e62021-10-11 15:41:03 -0400429 "//build/bazel/platforms/os:linux_bionic": [
430 "host.txt",
431 "linux.txt",
432 "bionic.txt",
433 "linux_bionic.txt",
434 "not_windows.txt",
435 ],
Colin Cross133782e2022-12-20 15:29:31 -0800436 "//build/bazel/platforms/os:linux_glibc": [
437 "host.txt",
438 "linux.txt",
439 "glibc.txt",
440 "linux_glibc.txt",
441 "not_windows.txt",
442 ],
Liz Kammerfdd72e62021-10-11 15:41:03 -0400443 "//build/bazel/platforms/os:linux_musl": [
444 "host.txt",
445 "linux.txt",
446 "musl.txt",
447 "linux_musl.txt",
448 "not_windows.txt",
449 ],
450 "//build/bazel/platforms/os:windows": [
451 "host.txt",
452 "windows.txt",
453 ],
Liz Kammer4562a3b2021-04-21 18:15:34 -0400454 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500455 })`,
456 }),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400457 },
458 },
459 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000460 Description: "arch-variant deps",
461 Blueprint: `custom {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400462 name: "has_dep",
463 arch: {
464 x86: {
465 arch_paths: [":dep"],
466 },
467 },
468 bazel_module: { bp2build_available: true },
469}
470
471custom {
472 name: "dep",
473 arch_paths: ["abc"],
474 bazel_module: { bp2build_available: true },
475}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000476 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000477 MakeBazelTarget("custom", "dep", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500478 "arch_paths": `["abc"]`,
479 }),
Alixe06d75b2022-08-31 18:28:19 +0000480 MakeBazelTarget("custom", "has_dep", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500481 "arch_paths": `select({
Liz Kammer4562a3b2021-04-21 18:15:34 -0400482 "//build/bazel/platforms/arch:x86": [":dep"],
483 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500484 })`,
485 }),
Liz Kammer4562a3b2021-04-21 18:15:34 -0400486 },
Jingwen Chen58a12b82021-03-30 13:08:36 +0000487 },
Liz Kammer32a03392021-09-14 11:17:21 -0400488 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000489 Description: "embedded props",
490 Blueprint: `custom {
Liz Kammer32a03392021-09-14 11:17:21 -0400491 name: "embedded_props",
492 embedded_prop: "abc",
493 bazel_module: { bp2build_available: true },
494}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000495 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000496 MakeBazelTarget("custom", "embedded_props", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500497 "embedded_attr": `"abc"`,
498 }),
Liz Kammer32a03392021-09-14 11:17:21 -0400499 },
500 },
501 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000502 Description: "ptr to embedded props",
503 Blueprint: `custom {
Liz Kammer32a03392021-09-14 11:17:21 -0400504 name: "ptr_to_embedded_props",
505 other_embedded_prop: "abc",
506 bazel_module: { bp2build_available: true },
507}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000508 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000509 MakeBazelTarget("custom", "ptr_to_embedded_props", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500510 "other_embedded_attr": `"abc"`,
511 }),
Liz Kammer32a03392021-09-14 11:17:21 -0400512 },
513 },
Jingwen Chen73850672020-12-14 08:25:34 -0500514 }
515
516 dir := "."
517 for _, testCase := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000518 t.Run(testCase.Description, func(t *testing.T) {
519 config := android.TestConfig(buildDir, nil, testCase.Blueprint, nil)
Liz Kammerfdd72e62021-10-11 15:41:03 -0400520 ctx := android.NewTestContext(config)
Jingwen Chen164e0862021-02-19 00:48:40 -0500521
Liz Kammerfdd72e62021-10-11 15:41:03 -0400522 registerCustomModuleForBp2buildConversion(ctx)
Jingwen Chen73850672020-12-14 08:25:34 -0500523
Liz Kammerfdd72e62021-10-11 15:41:03 -0400524 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
525 if errored(t, testCase, errs) {
526 return
527 }
528 _, errs = ctx.ResolveDependencies(config)
529 if errored(t, testCase, errs) {
530 return
531 }
Jingwen Chen73850672020-12-14 08:25:34 -0500532
Paul Duffinc6390592022-11-04 13:35:21 +0000533 codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build)
Liz Kammerfdd72e62021-10-11 15:41:03 -0400534 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
535 android.FailIfErrored(t, err)
Jingwen Chen164e0862021-02-19 00:48:40 -0500536
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000537 if actualCount, expectedCount := len(bazelTargets), len(testCase.ExpectedBazelTargets); actualCount != expectedCount {
538 t.Errorf("Expected %d bazel target (%s),\ngot %d (%s)", expectedCount, testCase.ExpectedBazelTargets, actualCount, bazelTargets)
Liz Kammerfdd72e62021-10-11 15:41:03 -0400539 } else {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000540 for i, expectedBazelTarget := range testCase.ExpectedBazelTargets {
Liz Kammerfdd72e62021-10-11 15:41:03 -0400541 actualBazelTarget := bazelTargets[i]
542 if actualBazelTarget.content != expectedBazelTarget {
543 t.Errorf(
544 "Expected generated Bazel target to be '%s', got '%s'",
545 expectedBazelTarget,
546 actualBazelTarget.content,
547 )
548 }
Liz Kammer4562a3b2021-04-21 18:15:34 -0400549 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500550 }
Liz Kammerfdd72e62021-10-11 15:41:03 -0400551 })
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800552 }
553}
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500554
Liz Kammerdfeb1202022-05-13 17:20:20 -0400555func TestBp2buildHostAndDevice(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000556 testCases := []Bp2buildTestCase{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400557 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000558 Description: "host and device, device only",
559 ModuleTypeUnderTest: "custom",
560 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
561 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400562 name: "foo",
563 bazel_module: { bp2build_available: true },
564}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000565 ExpectedBazelTargets: []string{
566 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400567 },
568 },
569 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000570 Description: "host and device, both",
571 ModuleTypeUnderTest: "custom",
572 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
573 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400574 name: "foo",
575 host_supported: true,
576 bazel_module: { bp2build_available: true },
577}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000578 ExpectedBazelTargets: []string{
579 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{}),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400580 },
581 },
582 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000583 Description: "host and device, host explicitly disabled",
584 ModuleTypeUnderTest: "custom",
585 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
586 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400587 name: "foo",
588 host_supported: false,
589 bazel_module: { bp2build_available: true },
590}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000591 ExpectedBazelTargets: []string{
592 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400593 },
594 },
595 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000596 Description: "host and device, neither",
597 ModuleTypeUnderTest: "custom",
598 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
599 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400600 name: "foo",
601 host_supported: false,
602 device_supported: false,
603 bazel_module: { bp2build_available: true },
604}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000605 ExpectedBazelTargets: []string{
606 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400607 "target_compatible_with": `["@platforms//:incompatible"]`,
608 }),
609 },
610 },
611 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000612 Description: "host and device, neither, cannot override with product_var",
613 ModuleTypeUnderTest: "custom",
614 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
615 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400616 name: "foo",
617 host_supported: false,
618 device_supported: false,
619 product_variables: { unbundled_build: { enabled: true } },
620 bazel_module: { bp2build_available: true },
621}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000622 ExpectedBazelTargets: []string{
623 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400624 "target_compatible_with": `["@platforms//:incompatible"]`,
625 }),
626 },
627 },
628 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000629 Description: "host and device, both, disabled overrided with product_var",
630 ModuleTypeUnderTest: "custom",
631 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
632 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400633 name: "foo",
634 host_supported: true,
635 device_supported: true,
636 enabled: false,
637 product_variables: { unbundled_build: { enabled: true } },
638 bazel_module: { bp2build_available: true },
639}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000640 ExpectedBazelTargets: []string{
641 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400642 "target_compatible_with": `["//build/bazel/product_variables:unbundled_build"]`,
643 }),
644 },
645 },
646 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000647 Description: "host and device, neither, cannot override with arch enabled",
648 ModuleTypeUnderTest: "custom",
649 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
650 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400651 name: "foo",
652 host_supported: false,
653 device_supported: false,
654 arch: { x86: { enabled: true } },
655 bazel_module: { bp2build_available: true },
656}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000657 ExpectedBazelTargets: []string{
658 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400659 "target_compatible_with": `["@platforms//:incompatible"]`,
660 }),
661 },
662 },
663 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000664 Description: "host and device, host only",
665 ModuleTypeUnderTest: "custom",
666 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
667 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400668 name: "foo",
669 host_supported: true,
670 device_supported: false,
671 bazel_module: { bp2build_available: true },
672}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000673 ExpectedBazelTargets: []string{
674 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.HostSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400675 },
676 },
677 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000678 Description: "host only",
679 ModuleTypeUnderTest: "custom",
680 ModuleTypeUnderTestFactory: customModuleFactoryHostSupported,
681 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400682 name: "foo",
683 bazel_module: { bp2build_available: true },
684}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000685 ExpectedBazelTargets: []string{
686 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.HostSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400687 },
688 },
689 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000690 Description: "device only",
691 ModuleTypeUnderTest: "custom",
692 ModuleTypeUnderTestFactory: customModuleFactoryDeviceSupported,
693 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400694 name: "foo",
695 bazel_module: { bp2build_available: true },
696}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000697 ExpectedBazelTargets: []string{
698 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400699 },
700 },
701 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000702 Description: "host and device default, default",
703 ModuleTypeUnderTest: "custom",
704 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault,
705 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400706 name: "foo",
707 bazel_module: { bp2build_available: true },
708}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000709 ExpectedBazelTargets: []string{
710 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{}),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400711 },
712 },
713 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000714 Description: "host and device default, device only",
715 ModuleTypeUnderTest: "custom",
716 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault,
717 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400718 name: "foo",
719 host_supported: false,
720 bazel_module: { bp2build_available: true },
721}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000722 ExpectedBazelTargets: []string{
723 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400724 },
725 },
726 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000727 Description: "host and device default, host only",
728 ModuleTypeUnderTest: "custom",
729 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault,
730 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400731 name: "foo",
732 device_supported: false,
733 bazel_module: { bp2build_available: true },
734}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000735 ExpectedBazelTargets: []string{
736 makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.HostSupported),
Liz Kammerdfeb1202022-05-13 17:20:20 -0400737 },
738 },
739 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000740 Description: "host and device default, neither",
741 ModuleTypeUnderTest: "custom",
742 ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault,
743 Blueprint: `custom {
Liz Kammerdfeb1202022-05-13 17:20:20 -0400744 name: "foo",
745 host_supported: false,
746 device_supported: false,
747 bazel_module: { bp2build_available: true },
748}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000749 ExpectedBazelTargets: []string{
750 MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400751 "target_compatible_with": `["@platforms//:incompatible"]`,
752 }),
753 },
754 },
755 }
756
757 for _, tc := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000758 t.Run(tc.Description, func(t *testing.T) {
Trevor Radcliffe1b4b2d92022-09-01 18:57:01 +0000759 RunBp2BuildTestCaseSimple(t, tc)
Liz Kammerdfeb1202022-05-13 17:20:20 -0400760 })
761 }
762}
763
Jingwen Chen40067de2021-01-26 21:58:43 -0500764func TestLoadStatements(t *testing.T) {
765 testCases := []struct {
766 bazelTargets BazelTargets
767 expectedLoadStatements string
768 }{
769 {
770 bazelTargets: BazelTargets{
771 BazelTarget{
772 name: "foo",
773 ruleClass: "cc_library",
774 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
775 },
776 },
777 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
778 },
779 {
780 bazelTargets: BazelTargets{
781 BazelTarget{
782 name: "foo",
783 ruleClass: "cc_library",
784 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
785 },
786 BazelTarget{
787 name: "bar",
788 ruleClass: "cc_library",
789 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
790 },
791 },
792 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
793 },
794 {
795 bazelTargets: BazelTargets{
796 BazelTarget{
797 name: "foo",
798 ruleClass: "cc_library",
799 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
800 },
801 BazelTarget{
802 name: "bar",
803 ruleClass: "cc_binary",
804 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
805 },
806 },
807 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")`,
808 },
809 {
810 bazelTargets: BazelTargets{
811 BazelTarget{
812 name: "foo",
813 ruleClass: "cc_library",
814 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
815 },
816 BazelTarget{
817 name: "bar",
818 ruleClass: "cc_binary",
819 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
820 },
821 BazelTarget{
822 name: "baz",
823 ruleClass: "java_binary",
824 bzlLoadLocation: "//build/bazel/rules:java.bzl",
825 },
826 },
827 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")
828load("//build/bazel/rules:java.bzl", "java_binary")`,
829 },
830 {
831 bazelTargets: BazelTargets{
832 BazelTarget{
833 name: "foo",
834 ruleClass: "cc_binary",
835 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
836 },
837 BazelTarget{
838 name: "bar",
839 ruleClass: "java_binary",
840 bzlLoadLocation: "//build/bazel/rules:java.bzl",
841 },
842 BazelTarget{
843 name: "baz",
844 ruleClass: "genrule",
845 // Note: no bzlLoadLocation for native rules
846 },
847 },
848 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary")
849load("//build/bazel/rules:java.bzl", "java_binary")`,
850 },
851 }
852
853 for _, testCase := range testCases {
854 actual := testCase.bazelTargets.LoadStatements()
855 expected := testCase.expectedLoadStatements
856 if actual != expected {
857 t.Fatalf("Expected load statements to be %s, got %s", expected, actual)
858 }
859 }
860
861}
862
863func TestGenerateBazelTargetModules_OneToMany_LoadedFromStarlark(t *testing.T) {
864 testCases := []struct {
865 bp string
866 expectedBazelTarget string
867 expectedBazelTargetCount int
868 expectedLoadStatements string
869 }{
870 {
871 bp: `custom {
872 name: "bar",
Liz Kammerdfeb1202022-05-13 17:20:20 -0400873 host_supported: true,
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400874 one_to_many_prop: true,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500875 bazel_module: { bp2build_available: true },
Jingwen Chen40067de2021-01-26 21:58:43 -0500876}`,
877 expectedBazelTarget: `my_library(
878 name = "bar",
879)
880
Jingwen Chen40067de2021-01-26 21:58:43 -0500881proto_library(
882 name = "bar_proto_library_deps",
Liz Kammer2ada09a2021-08-11 00:17:36 -0400883)
884
885my_proto_library(
886 name = "bar_my_proto_library_deps",
Jingwen Chen40067de2021-01-26 21:58:43 -0500887)`,
888 expectedBazelTargetCount: 3,
889 expectedLoadStatements: `load("//build/bazel/rules:proto.bzl", "my_proto_library", "proto_library")
890load("//build/bazel/rules:rules.bzl", "my_library")`,
891 },
892 }
893
894 dir := "."
895 for _, testCase := range testCases {
896 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
897 ctx := android.NewTestContext(config)
Liz Kammerdfeb1202022-05-13 17:20:20 -0400898 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
Jingwen Chen40067de2021-01-26 21:58:43 -0500899 ctx.RegisterForBazelConversion()
900
901 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
902 android.FailIfErrored(t, errs)
903 _, errs = ctx.ResolveDependencies(config)
904 android.FailIfErrored(t, errs)
905
Paul Duffinc6390592022-11-04 13:35:21 +0000906 codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build)
Liz Kammer6eff3232021-08-26 08:37:59 -0400907 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
908 android.FailIfErrored(t, err)
Jingwen Chen40067de2021-01-26 21:58:43 -0500909 if actualCount := len(bazelTargets); actualCount != testCase.expectedBazelTargetCount {
910 t.Fatalf("Expected %d bazel target, got %d", testCase.expectedBazelTargetCount, actualCount)
911 }
912
913 actualBazelTargets := bazelTargets.String()
914 if actualBazelTargets != testCase.expectedBazelTarget {
915 t.Errorf(
916 "Expected generated Bazel target to be '%s', got '%s'",
917 testCase.expectedBazelTarget,
918 actualBazelTargets,
919 )
920 }
921
922 actualLoadStatements := bazelTargets.LoadStatements()
923 if actualLoadStatements != testCase.expectedLoadStatements {
924 t.Errorf(
925 "Expected generated load statements to be '%s', got '%s'",
926 testCase.expectedLoadStatements,
927 actualLoadStatements,
928 )
929 }
930 }
931}
932
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500933func TestModuleTypeBp2Build(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000934 testCases := []Bp2buildTestCase{
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500935 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000936 Description: "filegroup with does not specify srcs",
937 ModuleTypeUnderTest: "filegroup",
938 ModuleTypeUnderTestFactory: android.FileGroupFactory,
939 Blueprint: `filegroup {
Liz Kammerebfcf672021-02-16 15:00:05 -0500940 name: "fg_foo",
941 bazel_module: { bp2build_available: true },
942}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000943 ExpectedBazelTargets: []string{
944 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{}),
Liz Kammerebfcf672021-02-16 15:00:05 -0500945 },
946 },
947 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000948 Description: "filegroup with no srcs",
949 ModuleTypeUnderTest: "filegroup",
950 ModuleTypeUnderTestFactory: android.FileGroupFactory,
951 Blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500952 name: "fg_foo",
953 srcs: [],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500954 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500955}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000956 ExpectedBazelTargets: []string{
957 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{}),
Liz Kammer356f7d42021-01-26 09:18:53 -0500958 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500959 },
960 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000961 Description: "filegroup with srcs",
962 ModuleTypeUnderTest: "filegroup",
963 ModuleTypeUnderTestFactory: android.FileGroupFactory,
964 Blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500965 name: "fg_foo",
966 srcs: ["a", "b"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500967 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500968}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000969 ExpectedBazelTargets: []string{
970 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500971 "srcs": `[
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500972 "a",
973 "b",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500974 ]`,
975 }),
Liz Kammer356f7d42021-01-26 09:18:53 -0500976 },
977 },
978 {
Usta Shresthad5580312022-09-23 16:46:38 -0400979 Description: "filegroup with dot-slash-prefixed srcs",
980 ModuleTypeUnderTest: "filegroup",
981 ModuleTypeUnderTestFactory: android.FileGroupFactory,
982 Blueprint: `filegroup {
983 name: "fg_foo",
984 srcs: ["./a", "./b"],
985 bazel_module: { bp2build_available: true },
986}`,
987 ExpectedBazelTargets: []string{
988 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
989 "srcs": `[
990 "a",
991 "b",
992 ]`,
993 }),
994 },
995 },
996 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000997 Description: "filegroup with excludes srcs",
998 ModuleTypeUnderTest: "filegroup",
999 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1000 Blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -05001001 name: "fg_foo",
1002 srcs: ["a", "b"],
1003 exclude_srcs: ["a"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001004 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -05001005}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001006 ExpectedBazelTargets: []string{
1007 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001008 "srcs": `["b"]`,
1009 }),
Liz Kammer356f7d42021-01-26 09:18:53 -05001010 },
1011 },
1012 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001013 Description: "depends_on_other_dir_module",
1014 ModuleTypeUnderTest: "filegroup",
1015 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1016 Blueprint: `filegroup {
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001017 name: "fg_foo",
Liz Kammer356f7d42021-01-26 09:18:53 -05001018 srcs: [
Jingwen Chenb4628eb2021-04-08 14:40:57 +00001019 ":foo",
Liz Kammer356f7d42021-01-26 09:18:53 -05001020 "c",
1021 ],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001022 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -05001023}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001024 Filesystem: map[string]string{
Liz Kammer356f7d42021-01-26 09:18:53 -05001025 "other/Android.bp": `filegroup {
1026 name: "foo",
1027 srcs: ["a", "b"],
Liz Kammer6eff3232021-08-26 08:37:59 -04001028 bazel_module: { bp2build_available: true },
1029}`,
1030 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001031 ExpectedBazelTargets: []string{
1032 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001033 "srcs": `[
1034 "//other:foo",
1035 "c",
1036 ]`,
1037 }),
1038 },
Liz Kammer6eff3232021-08-26 08:37:59 -04001039 },
1040 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001041 Description: "depends_on_other_unconverted_module_error",
1042 ModuleTypeUnderTest: "filegroup",
1043 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1044 UnconvertedDepsMode: errorModulesUnconvertedDeps,
1045 Blueprint: `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001046 name: "foobar",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001047 srcs: [
1048 ":foo",
1049 "c",
1050 ],
1051 bazel_module: { bp2build_available: true },
1052}`,
Sasha Smundakf2bb26f2022-08-04 11:28:15 -07001053 ExpectedErr: fmt.Errorf(`filegroup .:foobar depends on unconverted modules: foo`),
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001054 Filesystem: map[string]string{
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001055 "other/Android.bp": `filegroup {
1056 name: "foo",
1057 srcs: ["a", "b"],
1058}`,
1059 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -05001060 },
Usta Shrestha808bc712022-09-23 23:18:18 -04001061 {
1062 Description: "depends_on_other_missing_module_error",
1063 ModuleTypeUnderTest: "filegroup",
1064 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1065 UnconvertedDepsMode: errorModulesUnconvertedDeps,
1066 Blueprint: `filegroup {
1067 name: "foobar",
1068 srcs: [
1069 "c",
1070 "//other:foo",
1071 "//other:goo",
1072 ],
1073 bazel_module: { bp2build_available: true },
1074}`,
1075 ExpectedErr: fmt.Errorf(`filegroup .:foobar depends on missing modules: //other:goo`),
1076 Filesystem: map[string]string{"other/Android.bp": `filegroup {
1077 name: "foo",
1078 srcs: ["a"],
1079 bazel_module: { bp2build_available: true },
1080}
1081`,
1082 },
1083 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -05001084 }
1085
Jingwen Chen32b4ece2021-01-21 03:20:18 -05001086 for _, testCase := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001087 t.Run(testCase.Description, func(t *testing.T) {
1088 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, testCase)
Liz Kammer6eff3232021-08-26 08:37:59 -04001089 })
Jingwen Chen32b4ece2021-01-21 03:20:18 -05001090 }
1091}
Jingwen Chen041b1842021-02-01 00:23:25 -05001092
Jingwen Chen12b4c272021-03-10 02:05:59 -05001093func TestAllowlistingBp2buildTargetsExplicitly(t *testing.T) {
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001094 testCases := []struct {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001095 moduleTypeUnderTest string
1096 moduleTypeUnderTestFactory android.ModuleFactory
1097 bp string
1098 expectedCount int
1099 description string
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001100 }{
1101 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001102 description: "explicitly unavailable",
1103 moduleTypeUnderTest: "filegroup",
1104 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001105 bp: `filegroup {
1106 name: "foo",
1107 srcs: ["a", "b"],
1108 bazel_module: { bp2build_available: false },
1109}`,
1110 expectedCount: 0,
1111 },
1112 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001113 description: "implicitly unavailable",
1114 moduleTypeUnderTest: "filegroup",
1115 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001116 bp: `filegroup {
1117 name: "foo",
1118 srcs: ["a", "b"],
1119}`,
1120 expectedCount: 0,
1121 },
1122 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001123 description: "explicitly available",
1124 moduleTypeUnderTest: "filegroup",
1125 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001126 bp: `filegroup {
1127 name: "foo",
1128 srcs: ["a", "b"],
1129 bazel_module: { bp2build_available: true },
1130}`,
1131 expectedCount: 1,
1132 },
1133 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001134 description: "generates more than 1 target if needed",
1135 moduleTypeUnderTest: "custom",
Liz Kammerdfeb1202022-05-13 17:20:20 -04001136 moduleTypeUnderTestFactory: customModuleFactoryHostAndDevice,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001137 bp: `custom {
1138 name: "foo",
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001139 one_to_many_prop: true,
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001140 bazel_module: { bp2build_available: true },
1141}`,
1142 expectedCount: 3,
1143 },
1144 }
1145
1146 dir := "."
1147 for _, testCase := range testCases {
Liz Kammer2ada09a2021-08-11 00:17:36 -04001148 t.Run(testCase.description, func(t *testing.T) {
1149 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
1150 ctx := android.NewTestContext(config)
1151 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Liz Kammer2ada09a2021-08-11 00:17:36 -04001152 ctx.RegisterForBazelConversion()
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001153
Liz Kammer2ada09a2021-08-11 00:17:36 -04001154 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
1155 android.FailIfErrored(t, errs)
1156 _, errs = ctx.ResolveDependencies(config)
1157 android.FailIfErrored(t, errs)
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001158
Paul Duffinc6390592022-11-04 13:35:21 +00001159 codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build)
Liz Kammer6eff3232021-08-26 08:37:59 -04001160 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
1161 android.FailIfErrored(t, err)
Liz Kammer2ada09a2021-08-11 00:17:36 -04001162 if actualCount := len(bazelTargets); actualCount != testCase.expectedCount {
1163 t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, testCase.expectedCount, actualCount)
1164 }
1165 })
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001166 }
1167}
Liz Kammerba3ea162021-02-17 13:22:03 -05001168
Jingwen Chen12b4c272021-03-10 02:05:59 -05001169func TestAllowlistingBp2buildTargetsWithConfig(t *testing.T) {
1170 testCases := []struct {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001171 moduleTypeUnderTest string
1172 moduleTypeUnderTestFactory android.ModuleFactory
1173 expectedCount map[string]int
1174 description string
Sam Delmerico24c56032022-03-28 19:53:03 +00001175 bp2buildConfig allowlists.Bp2BuildConfig
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001176 checkDir string
1177 fs map[string]string
Jingwen Chen12b4c272021-03-10 02:05:59 -05001178 }{
1179 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001180 description: "test bp2build config package and subpackages config",
1181 moduleTypeUnderTest: "filegroup",
1182 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen12b4c272021-03-10 02:05:59 -05001183 expectedCount: map[string]int{
1184 "migrated": 1,
1185 "migrated/but_not_really": 0,
1186 "migrated/but_not_really/but_really": 1,
1187 "not_migrated": 0,
1188 "also_not_migrated": 0,
1189 },
Sam Delmerico24c56032022-03-28 19:53:03 +00001190 bp2buildConfig: allowlists.Bp2BuildConfig{
1191 "migrated": allowlists.Bp2BuildDefaultTrueRecursively,
1192 "migrated/but_not_really": allowlists.Bp2BuildDefaultFalse,
1193 "not_migrated": allowlists.Bp2BuildDefaultFalse,
Jingwen Chen12b4c272021-03-10 02:05:59 -05001194 },
1195 fs: map[string]string{
1196 "migrated/Android.bp": `filegroup { name: "a" }`,
1197 "migrated/but_not_really/Android.bp": `filegroup { name: "b" }`,
1198 "migrated/but_not_really/but_really/Android.bp": `filegroup { name: "c" }`,
1199 "not_migrated/Android.bp": `filegroup { name: "d" }`,
1200 "also_not_migrated/Android.bp": `filegroup { name: "e" }`,
1201 },
1202 },
1203 {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001204 description: "test bp2build config opt-in and opt-out",
1205 moduleTypeUnderTest: "filegroup",
1206 moduleTypeUnderTestFactory: android.FileGroupFactory,
Jingwen Chen12b4c272021-03-10 02:05:59 -05001207 expectedCount: map[string]int{
1208 "package-opt-in": 2,
1209 "package-opt-in/subpackage": 0,
1210 "package-opt-out": 1,
1211 "package-opt-out/subpackage": 0,
1212 },
Sam Delmerico24c56032022-03-28 19:53:03 +00001213 bp2buildConfig: allowlists.Bp2BuildConfig{
1214 "package-opt-in": allowlists.Bp2BuildDefaultFalse,
1215 "package-opt-out": allowlists.Bp2BuildDefaultTrueRecursively,
Jingwen Chen12b4c272021-03-10 02:05:59 -05001216 },
1217 fs: map[string]string{
1218 "package-opt-in/Android.bp": `
1219filegroup { name: "opt-in-a" }
1220filegroup { name: "opt-in-b", bazel_module: { bp2build_available: true } }
1221filegroup { name: "opt-in-c", bazel_module: { bp2build_available: true } }
1222`,
1223
1224 "package-opt-in/subpackage/Android.bp": `
1225filegroup { name: "opt-in-d" } // parent package not configured to DefaultTrueRecursively
1226`,
1227
1228 "package-opt-out/Android.bp": `
1229filegroup { name: "opt-out-a" }
1230filegroup { name: "opt-out-b", bazel_module: { bp2build_available: false } }
1231filegroup { name: "opt-out-c", bazel_module: { bp2build_available: false } }
1232`,
1233
1234 "package-opt-out/subpackage/Android.bp": `
1235filegroup { name: "opt-out-g", bazel_module: { bp2build_available: false } }
1236filegroup { name: "opt-out-h", bazel_module: { bp2build_available: false } }
1237`,
1238 },
1239 },
1240 }
1241
1242 dir := "."
1243 for _, testCase := range testCases {
1244 fs := make(map[string][]byte)
1245 toParse := []string{
1246 "Android.bp",
1247 }
1248 for f, content := range testCase.fs {
1249 if strings.HasSuffix(f, "Android.bp") {
1250 toParse = append(toParse, f)
1251 }
1252 fs[f] = []byte(content)
1253 }
1254 config := android.TestConfig(buildDir, nil, "", fs)
1255 ctx := android.NewTestContext(config)
1256 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Sam Delmerico24c56032022-03-28 19:53:03 +00001257 allowlist := android.NewBp2BuildAllowlist().SetDefaultConfig(testCase.bp2buildConfig)
1258 ctx.RegisterBp2BuildConfig(allowlist)
Jingwen Chen12b4c272021-03-10 02:05:59 -05001259 ctx.RegisterForBazelConversion()
1260
1261 _, errs := ctx.ParseFileList(dir, toParse)
1262 android.FailIfErrored(t, errs)
1263 _, errs = ctx.ResolveDependencies(config)
1264 android.FailIfErrored(t, errs)
1265
Paul Duffinc6390592022-11-04 13:35:21 +00001266 codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build)
Jingwen Chen12b4c272021-03-10 02:05:59 -05001267
1268 // For each directory, test that the expected number of generated targets is correct.
1269 for dir, expectedCount := range testCase.expectedCount {
Liz Kammer6eff3232021-08-26 08:37:59 -04001270 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
1271 android.FailIfErrored(t, err)
Jingwen Chen12b4c272021-03-10 02:05:59 -05001272 if actualCount := len(bazelTargets); actualCount != expectedCount {
1273 t.Fatalf(
1274 "%s: Expected %d bazel target for %s package, got %d",
1275 testCase.description,
1276 expectedCount,
1277 dir,
1278 actualCount)
1279 }
1280
1281 }
1282 }
1283}
1284
Liz Kammerba3ea162021-02-17 13:22:03 -05001285func TestCombineBuildFilesBp2buildTargets(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001286 testCases := []Bp2buildTestCase{
Liz Kammerba3ea162021-02-17 13:22:03 -05001287 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001288 Description: "filegroup bazel_module.label",
1289 ModuleTypeUnderTest: "filegroup",
1290 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1291 Blueprint: `filegroup {
Liz Kammerba3ea162021-02-17 13:22:03 -05001292 name: "fg_foo",
1293 bazel_module: { label: "//other:fg_foo" },
1294}`,
Cole Faustea602c52022-08-31 14:48:26 -07001295 ExpectedBazelTargets: []string{},
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001296 Filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -05001297 "other/BUILD.bazel": `// BUILD file`,
1298 },
1299 },
1300 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001301 Description: "multiple bazel_module.label same BUILD",
1302 ModuleTypeUnderTest: "filegroup",
1303 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1304 Blueprint: `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001305 name: "fg_foo",
1306 bazel_module: { label: "//other:fg_foo" },
1307 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001308
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001309 filegroup {
1310 name: "foo",
1311 bazel_module: { label: "//other:foo" },
1312 }`,
Cole Faustea602c52022-08-31 14:48:26 -07001313 ExpectedBazelTargets: []string{},
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001314 Filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -05001315 "other/BUILD.bazel": `// BUILD file`,
1316 },
1317 },
1318 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001319 Description: "filegroup bazel_module.label and bp2build in subdir",
1320 ModuleTypeUnderTest: "filegroup",
1321 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1322 Dir: "other",
1323 Blueprint: ``,
1324 Filesystem: map[string]string{
Jingwen Chenc63677b2021-06-17 05:43:19 +00001325 "other/Android.bp": `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001326 name: "fg_foo",
1327 bazel_module: {
1328 bp2build_available: true,
1329 },
1330 }
1331 filegroup {
1332 name: "fg_bar",
1333 bazel_module: {
1334 label: "//other:fg_bar"
1335 },
1336 }`,
Jingwen Chenc63677b2021-06-17 05:43:19 +00001337 "other/BUILD.bazel": `// definition for fg_bar`,
1338 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001339 ExpectedBazelTargets: []string{
1340 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{}),
Liz Kammerba3ea162021-02-17 13:22:03 -05001341 },
1342 },
1343 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001344 Description: "filegroup bazel_module.label and filegroup bp2build",
1345 ModuleTypeUnderTest: "filegroup",
1346 ModuleTypeUnderTestFactory: android.FileGroupFactory,
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001347
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001348 Filesystem: map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001349 "other/BUILD.bazel": `// BUILD file`,
1350 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001351 Blueprint: `filegroup {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001352 name: "fg_foo",
1353 bazel_module: {
1354 label: "//other:fg_foo",
1355 },
1356 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001357
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001358 filegroup {
1359 name: "fg_bar",
1360 bazel_module: {
1361 bp2build_available: true,
1362 },
1363 }`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001364 ExpectedBazelTargets: []string{
1365 MakeBazelTargetNoRestrictions("filegroup", "fg_bar", map[string]string{}),
Liz Kammerba3ea162021-02-17 13:22:03 -05001366 },
Liz Kammerba3ea162021-02-17 13:22:03 -05001367 },
1368 }
1369
1370 dir := "."
1371 for _, testCase := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001372 t.Run(testCase.Description, func(t *testing.T) {
Jingwen Chen49109762021-05-25 05:16:48 +00001373 fs := make(map[string][]byte)
1374 toParse := []string{
1375 "Android.bp",
Liz Kammerba3ea162021-02-17 13:22:03 -05001376 }
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001377 for f, content := range testCase.Filesystem {
Jingwen Chen49109762021-05-25 05:16:48 +00001378 if strings.HasSuffix(f, "Android.bp") {
1379 toParse = append(toParse, f)
1380 }
1381 fs[f] = []byte(content)
1382 }
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001383 config := android.TestConfig(buildDir, nil, testCase.Blueprint, fs)
Jingwen Chen49109762021-05-25 05:16:48 +00001384 ctx := android.NewTestContext(config)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001385 ctx.RegisterModuleType(testCase.ModuleTypeUnderTest, testCase.ModuleTypeUnderTestFactory)
Jingwen Chen49109762021-05-25 05:16:48 +00001386 ctx.RegisterForBazelConversion()
Liz Kammerba3ea162021-02-17 13:22:03 -05001387
Jingwen Chen49109762021-05-25 05:16:48 +00001388 _, errs := ctx.ParseFileList(dir, toParse)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001389 if errored(t, testCase, errs) {
Jingwen Chen49109762021-05-25 05:16:48 +00001390 return
1391 }
1392 _, errs = ctx.ResolveDependencies(config)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001393 if errored(t, testCase, errs) {
Jingwen Chen49109762021-05-25 05:16:48 +00001394 return
1395 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001396
Jingwen Chen49109762021-05-25 05:16:48 +00001397 checkDir := dir
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001398 if testCase.Dir != "" {
1399 checkDir = testCase.Dir
Jingwen Chen49109762021-05-25 05:16:48 +00001400 }
Paul Duffinc6390592022-11-04 13:35:21 +00001401 codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build)
Liz Kammer6eff3232021-08-26 08:37:59 -04001402 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, checkDir)
1403 android.FailIfErrored(t, err)
Jingwen Chen49109762021-05-25 05:16:48 +00001404 bazelTargets.sort()
1405 actualCount := len(bazelTargets)
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001406 expectedCount := len(testCase.ExpectedBazelTargets)
Jingwen Chen49109762021-05-25 05:16:48 +00001407 if actualCount != expectedCount {
1408 t.Errorf("Expected %d bazel target, got %d\n%s", expectedCount, actualCount, bazelTargets)
1409 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001410 for i, target := range bazelTargets {
Jingwen Chen49109762021-05-25 05:16:48 +00001411 actualContent := target.content
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001412 expectedContent := testCase.ExpectedBazelTargets[i]
Jingwen Chen49109762021-05-25 05:16:48 +00001413 if expectedContent != actualContent {
Liz Kammerba3ea162021-02-17 13:22:03 -05001414 t.Errorf(
Jingwen Chen49109762021-05-25 05:16:48 +00001415 "Expected generated Bazel target to be '%s', got '%s'",
1416 expectedContent,
1417 actualContent,
Liz Kammerba3ea162021-02-17 13:22:03 -05001418 )
1419 }
1420 }
Jingwen Chen49109762021-05-25 05:16:48 +00001421 })
Liz Kammerba3ea162021-02-17 13:22:03 -05001422 }
1423}
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001424
Jingwen Chen0eeaeb82022-09-21 10:27:42 +00001425func TestGlob(t *testing.T) {
1426 testCases := []Bp2buildTestCase{
1427 {
1428 Description: "filegroup with glob",
1429 ModuleTypeUnderTest: "filegroup",
1430 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1431 Blueprint: `filegroup {
1432 name: "fg_foo",
1433 srcs: ["**/*.txt"],
1434 bazel_module: { bp2build_available: true },
1435}`,
1436 ExpectedBazelTargets: []string{
1437 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1438 "srcs": `[
1439 "other/a.txt",
1440 "other/b.txt",
1441 "other/subdir/a.txt",
1442 ]`,
1443 }),
1444 },
1445 Filesystem: map[string]string{
1446 "other/a.txt": "",
1447 "other/b.txt": "",
1448 "other/subdir/a.txt": "",
1449 "other/file": "",
1450 },
1451 },
1452 {
1453 Description: "filegroup with glob in subdir",
1454 ModuleTypeUnderTest: "filegroup",
1455 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1456 Dir: "other",
1457 Filesystem: map[string]string{
1458 "other/Android.bp": `filegroup {
1459 name: "fg_foo",
1460 srcs: ["**/*.txt"],
1461 bazel_module: { bp2build_available: true },
1462}`,
1463 "other/a.txt": "",
1464 "other/b.txt": "",
1465 "other/subdir/a.txt": "",
1466 "other/file": "",
1467 },
1468 ExpectedBazelTargets: []string{
1469 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1470 "srcs": `[
1471 "a.txt",
1472 "b.txt",
1473 "subdir/a.txt",
1474 ]`,
1475 }),
1476 },
1477 },
1478 {
1479 Description: "filegroup with glob with no kept BUILD files",
1480 ModuleTypeUnderTest: "filegroup",
1481 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1482 KeepBuildFileForDirs: []string{
1483 // empty
1484 },
1485 Blueprint: `filegroup {
1486 name: "fg_foo",
1487 srcs: ["**/*.txt"],
1488 bazel_module: { bp2build_available: true },
1489}`,
1490 Filesystem: map[string]string{
1491 "a.txt": "",
1492 "b.txt": "",
1493 "foo/BUILD": "",
1494 "foo/a.txt": "",
1495 "foo/bar/BUILD": "",
1496 "foo/bar/b.txt": "",
1497 },
1498 ExpectedBazelTargets: []string{
1499 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1500 "srcs": `[
1501 "a.txt",
1502 "b.txt",
1503 "foo/a.txt",
1504 "foo/bar/b.txt",
1505 ]`,
1506 }),
1507 },
1508 },
1509 {
1510 Description: "filegroup with glob with kept BUILD file",
1511 ModuleTypeUnderTest: "filegroup",
1512 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1513 KeepBuildFileForDirs: []string{
1514 "foo",
1515 },
1516 Blueprint: `filegroup {
1517 name: "fg_foo",
1518 srcs: ["**/*.txt"],
1519 bazel_module: { bp2build_available: true },
1520}`,
1521 Filesystem: map[string]string{
1522 "a.txt": "",
1523 "b.txt": "",
1524 "foo/BUILD": "",
1525 "foo/a.txt": "",
1526 "foo/bar/BUILD": "",
1527 "foo/bar/b.txt": "",
1528 },
1529 ExpectedBazelTargets: []string{
1530 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1531 "srcs": `[
1532 "a.txt",
1533 "b.txt",
1534 "//foo:a.txt",
1535 "//foo:bar/b.txt",
1536 ]`,
1537 }),
1538 },
1539 },
1540 {
1541 Description: "filegroup with glob with kept BUILD.bazel file",
1542 ModuleTypeUnderTest: "filegroup",
1543 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1544 KeepBuildFileForDirs: []string{
1545 "foo",
1546 },
1547 Blueprint: `filegroup {
1548 name: "fg_foo",
1549 srcs: ["**/*.txt"],
1550 bazel_module: { bp2build_available: true },
1551}`,
1552 Filesystem: map[string]string{
1553 "a.txt": "",
1554 "b.txt": "",
1555 "foo/BUILD.bazel": "",
1556 "foo/a.txt": "",
1557 "foo/bar/BUILD.bazel": "",
1558 "foo/bar/b.txt": "",
1559 },
1560 ExpectedBazelTargets: []string{
1561 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1562 "srcs": `[
1563 "a.txt",
1564 "b.txt",
1565 "//foo:a.txt",
1566 "//foo:bar/b.txt",
1567 ]`,
1568 }),
1569 },
1570 },
1571 {
1572 Description: "filegroup with glob with Android.bp file as boundary",
1573 ModuleTypeUnderTest: "filegroup",
1574 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1575 Blueprint: `filegroup {
1576 name: "fg_foo",
1577 srcs: ["**/*.txt"],
1578 bazel_module: { bp2build_available: true },
1579}`,
1580 Filesystem: map[string]string{
1581 "a.txt": "",
1582 "b.txt": "",
1583 "foo/Android.bp": "",
1584 "foo/a.txt": "",
1585 "foo/bar/Android.bp": "",
1586 "foo/bar/b.txt": "",
1587 },
1588 ExpectedBazelTargets: []string{
1589 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1590 "srcs": `[
1591 "a.txt",
1592 "b.txt",
1593 "//foo:a.txt",
1594 "//foo/bar:b.txt",
1595 ]`,
1596 }),
1597 },
1598 },
1599 {
1600 Description: "filegroup with glob in subdir with kept BUILD and BUILD.bazel file",
1601 ModuleTypeUnderTest: "filegroup",
1602 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1603 Dir: "other",
1604 KeepBuildFileForDirs: []string{
1605 "other/foo",
1606 "other/foo/bar",
1607 // deliberately not other/foo/baz/BUILD.
1608 },
1609 Filesystem: map[string]string{
1610 "other/Android.bp": `filegroup {
1611 name: "fg_foo",
1612 srcs: ["**/*.txt"],
1613 bazel_module: { bp2build_available: true },
1614}`,
1615 "other/a.txt": "",
1616 "other/b.txt": "",
1617 "other/foo/BUILD": "",
1618 "other/foo/a.txt": "",
1619 "other/foo/bar/BUILD.bazel": "",
1620 "other/foo/bar/b.txt": "",
1621 "other/foo/baz/BUILD": "",
1622 "other/foo/baz/c.txt": "",
1623 },
1624 ExpectedBazelTargets: []string{
1625 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1626 "srcs": `[
1627 "a.txt",
1628 "b.txt",
1629 "//other/foo:a.txt",
1630 "//other/foo/bar:b.txt",
1631 "//other/foo:baz/c.txt",
1632 ]`,
1633 }),
1634 },
1635 },
1636 }
1637
1638 for _, testCase := range testCases {
1639 t.Run(testCase.Description, func(t *testing.T) {
1640 RunBp2BuildTestCaseSimple(t, testCase)
1641 })
1642 }
1643}
1644
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001645func TestGlobExcludeSrcs(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001646 testCases := []Bp2buildTestCase{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001647 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001648 Description: "filegroup top level exclude_srcs",
1649 ModuleTypeUnderTest: "filegroup",
1650 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1651 Blueprint: `filegroup {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001652 name: "fg_foo",
1653 srcs: ["**/*.txt"],
1654 exclude_srcs: ["c.txt"],
1655 bazel_module: { bp2build_available: true },
1656}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001657 Filesystem: map[string]string{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001658 "a.txt": "",
1659 "b.txt": "",
1660 "c.txt": "",
1661 "dir/Android.bp": "",
1662 "dir/e.txt": "",
1663 "dir/f.txt": "",
1664 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001665 ExpectedBazelTargets: []string{
1666 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001667 "srcs": `[
1668 "a.txt",
1669 "b.txt",
1670 "//dir:e.txt",
1671 "//dir:f.txt",
1672 ]`,
1673 }),
1674 },
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001675 },
1676 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001677 Description: "filegroup in subdir exclude_srcs",
1678 ModuleTypeUnderTest: "filegroup",
1679 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1680 Blueprint: "",
1681 Dir: "dir",
1682 Filesystem: map[string]string{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001683 "dir/Android.bp": `filegroup {
1684 name: "fg_foo",
1685 srcs: ["**/*.txt"],
1686 exclude_srcs: ["b.txt"],
1687 bazel_module: { bp2build_available: true },
1688}
1689`,
1690 "dir/a.txt": "",
1691 "dir/b.txt": "",
1692 "dir/subdir/Android.bp": "",
1693 "dir/subdir/e.txt": "",
1694 "dir/subdir/f.txt": "",
1695 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001696 ExpectedBazelTargets: []string{
1697 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001698 "srcs": `[
Liz Kammer9abd62d2021-05-21 08:37:59 -04001699 "a.txt",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001700 "//dir/subdir:e.txt",
1701 "//dir/subdir:f.txt",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001702 ]`,
1703 }),
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001704 },
1705 },
1706 }
1707
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001708 for _, testCase := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001709 t.Run(testCase.Description, func(t *testing.T) {
Trevor Radcliffe1b4b2d92022-09-01 18:57:01 +00001710 RunBp2BuildTestCaseSimple(t, testCase)
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001711 })
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001712 }
1713}
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001714
1715func TestCommonBp2BuildModuleAttrs(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001716 testCases := []Bp2buildTestCase{
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001717 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001718 Description: "Required into data test",
1719 ModuleTypeUnderTest: "filegroup",
1720 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1721 Blueprint: simpleModuleDoNotConvertBp2build("filegroup", "reqd") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001722filegroup {
1723 name: "fg_foo",
1724 required: ["reqd"],
1725 bazel_module: { bp2build_available: true },
1726}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001727 ExpectedBazelTargets: []string{
1728 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001729 "data": `[":reqd"]`,
1730 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001731 },
1732 },
1733 {
Jingwen Chena5ecb372022-09-21 09:05:37 +00001734 Description: "Required into data test, cyclic self reference is filtered out",
1735 ModuleTypeUnderTest: "filegroup",
1736 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1737 Blueprint: simpleModuleDoNotConvertBp2build("filegroup", "reqd") + `
1738filegroup {
1739 name: "fg_foo",
1740 required: ["reqd", "fg_foo"],
1741 bazel_module: { bp2build_available: true },
1742}`,
1743 ExpectedBazelTargets: []string{
1744 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
1745 "data": `[":reqd"]`,
1746 }),
1747 },
1748 },
1749 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001750 Description: "Required via arch into data test",
1751 ModuleTypeUnderTest: "python_library",
1752 ModuleTypeUnderTestFactory: python.PythonLibraryFactory,
1753 Blueprint: simpleModuleDoNotConvertBp2build("python_library", "reqdx86") +
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001754 simpleModuleDoNotConvertBp2build("python_library", "reqdarm") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001755python_library {
1756 name: "fg_foo",
1757 arch: {
Liz Kammerbe46fcc2021-11-01 15:32:43 -04001758 arm: {
1759 required: ["reqdarm"],
1760 },
1761 x86: {
1762 required: ["reqdx86"],
1763 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001764 },
1765 bazel_module: { bp2build_available: true },
1766}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001767 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001768 MakeBazelTarget("py_library", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001769 "data": `select({
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001770 "//build/bazel/platforms/arch:arm": [":reqdarm"],
1771 "//build/bazel/platforms/arch:x86": [":reqdx86"],
1772 "//conditions:default": [],
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001773 })`,
1774 "srcs_version": `"PY3"`,
Cole Faustb09da7e2022-05-18 10:57:33 -07001775 "imports": `["."]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001776 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001777 },
1778 },
1779 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001780 Description: "Required appended to data test",
1781 ModuleTypeUnderTest: "python_library",
1782 ModuleTypeUnderTestFactory: python.PythonLibraryFactory,
1783 Filesystem: map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001784 "data.bin": "",
1785 "src.py": "",
1786 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001787 Blueprint: simpleModuleDoNotConvertBp2build("python_library", "reqd") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001788python_library {
1789 name: "fg_foo",
1790 data: ["data.bin"],
1791 required: ["reqd"],
1792 bazel_module: { bp2build_available: true },
1793}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001794 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +00001795 MakeBazelTarget("py_library", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001796 "data": `[
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001797 "data.bin",
1798 ":reqd",
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001799 ]`,
1800 "srcs_version": `"PY3"`,
Cole Faustb09da7e2022-05-18 10:57:33 -07001801 "imports": `["."]`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001802 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001803 },
1804 },
1805 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001806 Description: "All props-to-attrs at once together test",
1807 ModuleTypeUnderTest: "filegroup",
1808 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1809 Blueprint: simpleModuleDoNotConvertBp2build("filegroup", "reqd") + `
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001810filegroup {
1811 name: "fg_foo",
1812 required: ["reqd"],
1813 bazel_module: { bp2build_available: true },
1814}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001815 ExpectedBazelTargets: []string{
1816 MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001817 "data": `[":reqd"]`,
1818 }),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001819 },
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001820 },
1821 }
1822
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001823 for _, tc := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +00001824 t.Run(tc.Description, func(t *testing.T) {
Trevor Radcliffe1b4b2d92022-09-01 18:57:01 +00001825 RunBp2BuildTestCaseSimple(t, tc)
Liz Kammer78cfdaa2021-11-08 12:56:31 -05001826 })
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00001827 }
1828}
Sasha Smundak05b0ba62022-09-26 18:15:45 -07001829
1830func TestLicensesAttrConversion(t *testing.T) {
1831 RunBp2BuildTestCase(t,
1832 func(ctx android.RegistrationContext) {
1833 ctx.RegisterModuleType("license", android.LicenseFactory)
1834 },
1835 Bp2buildTestCase{
1836 Description: "Test that licenses: attribute is converted",
1837 ModuleTypeUnderTest: "filegroup",
1838 ModuleTypeUnderTestFactory: android.FileGroupFactory,
1839 Blueprint: `
1840license {
1841 name: "my_license",
1842}
1843filegroup {
1844 name: "my_filegroup",
1845 licenses: ["my_license"],
1846}
1847`,
1848 ExpectedBazelTargets: []string{
1849 MakeBazelTargetNoRestrictions("filegroup", "my_filegroup", AttrNameToString{
1850 "applicable_licenses": `[":my_license"]`,
1851 }),
1852 MakeBazelTargetNoRestrictions("android_license", "my_license", AttrNameToString{}),
1853 },
1854 })
1855}
Spandan Das5af0bd32022-09-28 20:43:08 +00001856
1857func TestGenerateApiBazelTargets(t *testing.T) {
1858 bp := `
1859 custom {
1860 name: "foo",
1861 api: "foo.txt",
1862 }
1863 `
1864 expectedBazelTarget := MakeBazelTarget(
1865 "custom_api_contribution",
1866 "foo",
1867 AttrNameToString{
1868 "api": `"foo.txt"`,
1869 },
1870 )
1871 registerCustomModule := func(ctx android.RegistrationContext) {
1872 ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
1873 }
1874 RunApiBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
1875 Blueprint: bp,
1876 ExpectedBazelTargets: []string{expectedBazelTarget},
1877 Description: "Generating API contribution Bazel targets for custom module",
1878 })
1879}