blob: 09d536fdced2f16f32d78ecedf8991d3d818fe93 [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 (
18 "android/soong/android"
Liz Kammer6eff3232021-08-26 08:37:59 -040019 "fmt"
Liz Kammer356f7d42021-01-26 09:18:53 -050020 "strings"
Liz Kammer2dd9ca42020-11-25 16:06:39 -080021 "testing"
22)
23
24func TestGenerateSoongModuleTargets(t *testing.T) {
25 testCases := []struct {
Liz Kammerd366c902021-06-03 13:43:01 -040026 description string
Liz Kammer2dd9ca42020-11-25 16:06:39 -080027 bp string
28 expectedBazelTarget string
29 }{
30 {
Liz Kammerd366c902021-06-03 13:43:01 -040031 description: "only name",
Jingwen Chenb4628eb2021-04-08 14:40:57 +000032 bp: `custom { name: "foo" }
Liz Kammerd366c902021-06-03 13:43:01 -040033 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080034 expectedBazelTarget: `soong_module(
35 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050036 soong_module_name = "foo",
37 soong_module_type = "custom",
38 soong_module_variant = "",
39 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080040 ],
Liz Kammerd366c902021-06-03 13:43:01 -040041 bool_prop = False,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080042)`,
43 },
44 {
Liz Kammerd366c902021-06-03 13:43:01 -040045 description: "handles bool",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080046 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -040047 name: "foo",
48 bool_prop: true,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080049}
Liz Kammerd366c902021-06-03 13:43:01 -040050 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080051 expectedBazelTarget: `soong_module(
52 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050053 soong_module_name = "foo",
54 soong_module_type = "custom",
55 soong_module_variant = "",
56 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080057 ],
Liz Kammerd366c902021-06-03 13:43:01 -040058 bool_prop = True,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080059)`,
60 },
61 {
Liz Kammerd366c902021-06-03 13:43:01 -040062 description: "string escaping",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080063 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -040064 name: "foo",
65 owner: "a_string_with\"quotes\"_and_\\backslashes\\\\",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080066}
Liz Kammerd366c902021-06-03 13:43:01 -040067 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080068 expectedBazelTarget: `soong_module(
69 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050070 soong_module_name = "foo",
71 soong_module_type = "custom",
72 soong_module_variant = "",
73 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080074 ],
Liz Kammerd366c902021-06-03 13:43:01 -040075 bool_prop = False,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080076 owner = "a_string_with\"quotes\"_and_\\backslashes\\\\",
77)`,
78 },
79 {
Liz Kammerd366c902021-06-03 13:43:01 -040080 description: "single item string list",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080081 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -040082 name: "foo",
83 required: ["bar"],
Liz Kammer2dd9ca42020-11-25 16:06:39 -080084}
Liz Kammerd366c902021-06-03 13:43:01 -040085 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080086 expectedBazelTarget: `soong_module(
87 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050088 soong_module_name = "foo",
89 soong_module_type = "custom",
90 soong_module_variant = "",
91 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080092 ],
Liz Kammerd366c902021-06-03 13:43:01 -040093 bool_prop = False,
Jingwen Chenb4628eb2021-04-08 14:40:57 +000094 required = ["bar"],
Liz Kammer2dd9ca42020-11-25 16:06:39 -080095)`,
96 },
97 {
Liz Kammerd366c902021-06-03 13:43:01 -040098 description: "list of strings",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080099 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -0400100 name: "foo",
101 target_required: ["qux", "bazqux"],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800102}
Liz Kammerd366c902021-06-03 13:43:01 -0400103 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800104 expectedBazelTarget: `soong_module(
105 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500106 soong_module_name = "foo",
107 soong_module_type = "custom",
108 soong_module_variant = "",
109 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800110 ],
Liz Kammerd366c902021-06-03 13:43:01 -0400111 bool_prop = False,
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800112 target_required = [
113 "qux",
114 "bazqux",
115 ],
116)`,
117 },
118 {
Liz Kammerd366c902021-06-03 13:43:01 -0400119 description: "dist/dists",
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800120 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -0400121 name: "foo",
122 dist: {
123 targets: ["goal_foo"],
124 tag: ".foo",
125 },
126 dists: [{
127 targets: ["goal_bar"],
128 tag: ".bar",
129 }],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800130}
Liz Kammerd366c902021-06-03 13:43:01 -0400131 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800132 expectedBazelTarget: `soong_module(
133 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500134 soong_module_name = "foo",
135 soong_module_type = "custom",
136 soong_module_variant = "",
137 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800138 ],
Liz Kammerd366c902021-06-03 13:43:01 -0400139 bool_prop = False,
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800140 dist = {
141 "tag": ".foo",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000142 "targets": ["goal_foo"],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800143 },
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000144 dists = [{
145 "tag": ".bar",
146 "targets": ["goal_bar"],
147 }],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800148)`,
149 },
150 {
Liz Kammerd366c902021-06-03 13:43:01 -0400151 description: "put it together",
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800152 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -0400153 name: "foo",
154 required: ["bar"],
155 target_required: ["qux", "bazqux"],
156 bool_prop: true,
157 owner: "custom_owner",
158 dists: [
159 {
160 tag: ".tag",
161 targets: ["my_goal"],
162 },
163 ],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800164}
Liz Kammerd366c902021-06-03 13:43:01 -0400165 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800166 expectedBazelTarget: `soong_module(
167 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500168 soong_module_name = "foo",
169 soong_module_type = "custom",
170 soong_module_variant = "",
171 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800172 ],
Liz Kammerd366c902021-06-03 13:43:01 -0400173 bool_prop = True,
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000174 dists = [{
175 "tag": ".tag",
176 "targets": ["my_goal"],
177 }],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800178 owner = "custom_owner",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000179 required = ["bar"],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800180 target_required = [
181 "qux",
182 "bazqux",
183 ],
184)`,
185 },
186 }
187
188 dir := "."
189 for _, testCase := range testCases {
Liz Kammerd366c902021-06-03 13:43:01 -0400190 t.Run(testCase.description, func(t *testing.T) {
191 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
192 ctx := android.NewTestContext(config)
Jingwen Chen164e0862021-02-19 00:48:40 -0500193
Liz Kammerd366c902021-06-03 13:43:01 -0400194 ctx.RegisterModuleType("custom", customModuleFactory)
195 ctx.Register()
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800196
Liz Kammerd366c902021-06-03 13:43:01 -0400197 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
198 android.FailIfErrored(t, errs)
199 _, errs = ctx.PrepareBuildActions(config)
200 android.FailIfErrored(t, errs)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800201
Liz Kammerd366c902021-06-03 13:43:01 -0400202 codegenCtx := NewCodegenContext(config, *ctx.Context, QueryView)
Liz Kammer6eff3232021-08-26 08:37:59 -0400203 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
204 android.FailIfErrored(t, err)
Liz Kammerd366c902021-06-03 13:43:01 -0400205 if actualCount, expectedCount := len(bazelTargets), 1; actualCount != expectedCount {
206 t.Fatalf("Expected %d bazel target, got %d", expectedCount, actualCount)
207 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800208
Liz Kammerd366c902021-06-03 13:43:01 -0400209 actualBazelTarget := bazelTargets[0]
210 if actualBazelTarget.content != testCase.expectedBazelTarget {
211 t.Errorf(
212 "Expected generated Bazel target to be '%s', got '%s'",
213 testCase.expectedBazelTarget,
214 actualBazelTarget.content,
215 )
216 }
217 })
Jingwen Chen73850672020-12-14 08:25:34 -0500218 }
219}
220
221func TestGenerateBazelTargetModules(t *testing.T) {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000222 testCases := []bp2buildTestCase{
Jingwen Chen73850672020-12-14 08:25:34 -0500223 {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000224 blueprint: `custom {
Jingwen Chen73850672020-12-14 08:25:34 -0500225 name: "foo",
226 string_list_prop: ["a", "b"],
227 string_prop: "a",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500228 bazel_module: { bp2build_available: true },
Jingwen Chen73850672020-12-14 08:25:34 -0500229}`,
Liz Kammer4562a3b2021-04-21 18:15:34 -0400230 expectedBazelTargets: []string{`custom(
Jingwen Chen73850672020-12-14 08:25:34 -0500231 name = "foo",
232 string_list_prop = [
233 "a",
234 "b",
235 ],
236 string_prop = "a",
237)`,
Liz Kammer4562a3b2021-04-21 18:15:34 -0400238 },
Jingwen Chen73850672020-12-14 08:25:34 -0500239 },
Jingwen Chen58a12b82021-03-30 13:08:36 +0000240 {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000241 blueprint: `custom {
Jingwen Chen58a12b82021-03-30 13:08:36 +0000242 name: "control_characters",
243 string_list_prop: ["\t", "\n"],
244 string_prop: "a\t\n\r",
245 bazel_module: { bp2build_available: true },
246}`,
Liz Kammer4562a3b2021-04-21 18:15:34 -0400247 expectedBazelTargets: []string{`custom(
Jingwen Chen58a12b82021-03-30 13:08:36 +0000248 name = "control_characters",
249 string_list_prop = [
250 "\t",
251 "\n",
252 ],
253 string_prop = "a\t\n\r",
254)`,
Liz Kammer4562a3b2021-04-21 18:15:34 -0400255 },
256 },
257 {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000258 blueprint: `custom {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400259 name: "has_dep",
260 arch_paths: [":dep"],
261 bazel_module: { bp2build_available: true },
262}
263
264custom {
265 name: "dep",
266 arch_paths: ["abc"],
267 bazel_module: { bp2build_available: true },
268}`,
269 expectedBazelTargets: []string{`custom(
270 name = "dep",
271 arch_paths = ["abc"],
272)`,
273 `custom(
274 name = "has_dep",
275 arch_paths = [":dep"],
276)`,
277 },
278 },
279 {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000280 blueprint: `custom {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400281 name: "arch_paths",
282 arch: {
283 x86: {
284 arch_paths: ["abc"],
285 },
286 },
287 bazel_module: { bp2build_available: true },
288}`,
289 expectedBazelTargets: []string{`custom(
290 name = "arch_paths",
291 arch_paths = select({
292 "//build/bazel/platforms/arch:x86": ["abc"],
293 "//conditions:default": [],
294 }),
295)`,
296 },
297 },
298 {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000299 blueprint: `custom {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400300 name: "has_dep",
301 arch: {
302 x86: {
303 arch_paths: [":dep"],
304 },
305 },
306 bazel_module: { bp2build_available: true },
307}
308
309custom {
310 name: "dep",
311 arch_paths: ["abc"],
312 bazel_module: { bp2build_available: true },
313}`,
314 expectedBazelTargets: []string{`custom(
315 name = "dep",
316 arch_paths = ["abc"],
317)`,
318 `custom(
319 name = "has_dep",
320 arch_paths = select({
321 "//build/bazel/platforms/arch:x86": [":dep"],
322 "//conditions:default": [],
323 }),
324)`,
325 },
Jingwen Chen58a12b82021-03-30 13:08:36 +0000326 },
Jingwen Chen73850672020-12-14 08:25:34 -0500327 }
328
329 dir := "."
330 for _, testCase := range testCases {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000331 config := android.TestConfig(buildDir, nil, testCase.blueprint, nil)
Jingwen Chen73850672020-12-14 08:25:34 -0500332 ctx := android.NewTestContext(config)
Jingwen Chen164e0862021-02-19 00:48:40 -0500333
Liz Kammer32b77cf2021-08-04 15:17:02 -0400334 registerCustomModuleForBp2buildConversion(ctx)
Jingwen Chen73850672020-12-14 08:25:34 -0500335
336 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
Jingwen Chen5146ac02021-09-02 11:44:42 +0000337 if errored(t, testCase, errs) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500338 continue
339 }
Jingwen Chen73850672020-12-14 08:25:34 -0500340 _, errs = ctx.ResolveDependencies(config)
Jingwen Chen5146ac02021-09-02 11:44:42 +0000341 if errored(t, testCase, errs) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500342 continue
343 }
Jingwen Chen73850672020-12-14 08:25:34 -0500344
Jingwen Chen164e0862021-02-19 00:48:40 -0500345 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Liz Kammer6eff3232021-08-26 08:37:59 -0400346 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
347 android.FailIfErrored(t, err)
Jingwen Chen164e0862021-02-19 00:48:40 -0500348
Liz Kammer4562a3b2021-04-21 18:15:34 -0400349 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
Liz Kammer356f7d42021-01-26 09:18:53 -0500350 t.Errorf("Expected %d bazel target, got %d", expectedCount, actualCount)
351 } else {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400352 for i, expectedBazelTarget := range testCase.expectedBazelTargets {
353 actualBazelTarget := bazelTargets[i]
354 if actualBazelTarget.content != expectedBazelTarget {
355 t.Errorf(
356 "Expected generated Bazel target to be '%s', got '%s'",
357 expectedBazelTarget,
358 actualBazelTarget.content,
359 )
360 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500361 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800362 }
363 }
364}
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500365
Jingwen Chen40067de2021-01-26 21:58:43 -0500366func TestLoadStatements(t *testing.T) {
367 testCases := []struct {
368 bazelTargets BazelTargets
369 expectedLoadStatements string
370 }{
371 {
372 bazelTargets: BazelTargets{
373 BazelTarget{
374 name: "foo",
375 ruleClass: "cc_library",
376 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
377 },
378 },
379 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
380 },
381 {
382 bazelTargets: BazelTargets{
383 BazelTarget{
384 name: "foo",
385 ruleClass: "cc_library",
386 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
387 },
388 BazelTarget{
389 name: "bar",
390 ruleClass: "cc_library",
391 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
392 },
393 },
394 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
395 },
396 {
397 bazelTargets: BazelTargets{
398 BazelTarget{
399 name: "foo",
400 ruleClass: "cc_library",
401 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
402 },
403 BazelTarget{
404 name: "bar",
405 ruleClass: "cc_binary",
406 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
407 },
408 },
409 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")`,
410 },
411 {
412 bazelTargets: BazelTargets{
413 BazelTarget{
414 name: "foo",
415 ruleClass: "cc_library",
416 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
417 },
418 BazelTarget{
419 name: "bar",
420 ruleClass: "cc_binary",
421 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
422 },
423 BazelTarget{
424 name: "baz",
425 ruleClass: "java_binary",
426 bzlLoadLocation: "//build/bazel/rules:java.bzl",
427 },
428 },
429 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")
430load("//build/bazel/rules:java.bzl", "java_binary")`,
431 },
432 {
433 bazelTargets: BazelTargets{
434 BazelTarget{
435 name: "foo",
436 ruleClass: "cc_binary",
437 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
438 },
439 BazelTarget{
440 name: "bar",
441 ruleClass: "java_binary",
442 bzlLoadLocation: "//build/bazel/rules:java.bzl",
443 },
444 BazelTarget{
445 name: "baz",
446 ruleClass: "genrule",
447 // Note: no bzlLoadLocation for native rules
448 },
449 },
450 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary")
451load("//build/bazel/rules:java.bzl", "java_binary")`,
452 },
453 }
454
455 for _, testCase := range testCases {
456 actual := testCase.bazelTargets.LoadStatements()
457 expected := testCase.expectedLoadStatements
458 if actual != expected {
459 t.Fatalf("Expected load statements to be %s, got %s", expected, actual)
460 }
461 }
462
463}
464
465func TestGenerateBazelTargetModules_OneToMany_LoadedFromStarlark(t *testing.T) {
466 testCases := []struct {
467 bp string
468 expectedBazelTarget string
469 expectedBazelTargetCount int
470 expectedLoadStatements string
471 }{
472 {
473 bp: `custom {
474 name: "bar",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500475 bazel_module: { bp2build_available: true },
Jingwen Chen40067de2021-01-26 21:58:43 -0500476}`,
477 expectedBazelTarget: `my_library(
478 name = "bar",
479)
480
Jingwen Chen40067de2021-01-26 21:58:43 -0500481proto_library(
482 name = "bar_proto_library_deps",
Liz Kammer2ada09a2021-08-11 00:17:36 -0400483)
484
485my_proto_library(
486 name = "bar_my_proto_library_deps",
Jingwen Chen40067de2021-01-26 21:58:43 -0500487)`,
488 expectedBazelTargetCount: 3,
489 expectedLoadStatements: `load("//build/bazel/rules:proto.bzl", "my_proto_library", "proto_library")
490load("//build/bazel/rules:rules.bzl", "my_library")`,
491 },
492 }
493
494 dir := "."
495 for _, testCase := range testCases {
496 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
497 ctx := android.NewTestContext(config)
498 ctx.RegisterModuleType("custom", customModuleFactory)
Jingwen Chen12b4c272021-03-10 02:05:59 -0500499 ctx.RegisterBp2BuildMutator("custom", customBp2BuildMutatorFromStarlark)
Jingwen Chen40067de2021-01-26 21:58:43 -0500500 ctx.RegisterForBazelConversion()
501
502 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
503 android.FailIfErrored(t, errs)
504 _, errs = ctx.ResolveDependencies(config)
505 android.FailIfErrored(t, errs)
506
Jingwen Chen164e0862021-02-19 00:48:40 -0500507 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Liz Kammer6eff3232021-08-26 08:37:59 -0400508 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
509 android.FailIfErrored(t, err)
Jingwen Chen40067de2021-01-26 21:58:43 -0500510 if actualCount := len(bazelTargets); actualCount != testCase.expectedBazelTargetCount {
511 t.Fatalf("Expected %d bazel target, got %d", testCase.expectedBazelTargetCount, actualCount)
512 }
513
514 actualBazelTargets := bazelTargets.String()
515 if actualBazelTargets != testCase.expectedBazelTarget {
516 t.Errorf(
517 "Expected generated Bazel target to be '%s', got '%s'",
518 testCase.expectedBazelTarget,
519 actualBazelTargets,
520 )
521 }
522
523 actualLoadStatements := bazelTargets.LoadStatements()
524 if actualLoadStatements != testCase.expectedLoadStatements {
525 t.Errorf(
526 "Expected generated load statements to be '%s', got '%s'",
527 testCase.expectedLoadStatements,
528 actualLoadStatements,
529 )
530 }
531 }
532}
533
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500534func TestModuleTypeBp2Build(t *testing.T) {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000535 testCases := []bp2buildTestCase{
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500536 {
Liz Kammerebfcf672021-02-16 15:00:05 -0500537 description: "filegroup with does not specify srcs",
538 moduleTypeUnderTest: "filegroup",
539 moduleTypeUnderTestFactory: android.FileGroupFactory,
540 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000541 blueprint: `filegroup {
Liz Kammerebfcf672021-02-16 15:00:05 -0500542 name: "fg_foo",
543 bazel_module: { bp2build_available: true },
544}`,
545 expectedBazelTargets: []string{
546 `filegroup(
547 name = "fg_foo",
548)`,
549 },
550 },
551 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500552 description: "filegroup with no srcs",
553 moduleTypeUnderTest: "filegroup",
554 moduleTypeUnderTestFactory: android.FileGroupFactory,
555 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000556 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500557 name: "fg_foo",
558 srcs: [],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500559 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500560}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500561 expectedBazelTargets: []string{
562 `filegroup(
563 name = "fg_foo",
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500564)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500565 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500566 },
567 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500568 description: "filegroup with srcs",
569 moduleTypeUnderTest: "filegroup",
570 moduleTypeUnderTestFactory: android.FileGroupFactory,
571 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000572 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500573 name: "fg_foo",
574 srcs: ["a", "b"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500575 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500576}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500577 expectedBazelTargets: []string{`filegroup(
578 name = "fg_foo",
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500579 srcs = [
580 "a",
581 "b",
582 ],
583)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500584 },
585 },
586 {
587 description: "filegroup with excludes srcs",
588 moduleTypeUnderTest: "filegroup",
589 moduleTypeUnderTestFactory: android.FileGroupFactory,
590 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000591 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500592 name: "fg_foo",
593 srcs: ["a", "b"],
594 exclude_srcs: ["a"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500595 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500596}`,
597 expectedBazelTargets: []string{`filegroup(
598 name = "fg_foo",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000599 srcs = ["b"],
Liz Kammer356f7d42021-01-26 09:18:53 -0500600)`,
601 },
602 },
603 {
604 description: "filegroup with glob",
605 moduleTypeUnderTest: "filegroup",
606 moduleTypeUnderTestFactory: android.FileGroupFactory,
607 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000608 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500609 name: "foo",
610 srcs: ["**/*.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500611 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500612}`,
613 expectedBazelTargets: []string{`filegroup(
614 name = "foo",
615 srcs = [
616 "other/a.txt",
617 "other/b.txt",
618 "other/subdir/a.txt",
619 ],
620)`,
621 },
Jingwen Chen5146ac02021-09-02 11:44:42 +0000622 filesystem: map[string]string{
Liz Kammer356f7d42021-01-26 09:18:53 -0500623 "other/a.txt": "",
624 "other/b.txt": "",
625 "other/subdir/a.txt": "",
626 "other/file": "",
627 },
628 },
629 {
630 description: "filegroup with glob in subdir",
631 moduleTypeUnderTest: "filegroup",
632 moduleTypeUnderTestFactory: android.FileGroupFactory,
633 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000634 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500635 name: "foo",
636 srcs: ["a.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500637 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500638}`,
639 dir: "other",
640 expectedBazelTargets: []string{`filegroup(
641 name = "fg_foo",
642 srcs = [
643 "a.txt",
644 "b.txt",
645 "subdir/a.txt",
646 ],
647)`,
648 },
Jingwen Chen5146ac02021-09-02 11:44:42 +0000649 filesystem: map[string]string{
Liz Kammer356f7d42021-01-26 09:18:53 -0500650 "other/Android.bp": `filegroup {
651 name: "fg_foo",
652 srcs: ["**/*.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500653 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500654}`,
655 "other/a.txt": "",
656 "other/b.txt": "",
657 "other/subdir/a.txt": "",
658 "other/file": "",
659 },
660 },
661 {
662 description: "depends_on_other_dir_module",
663 moduleTypeUnderTest: "filegroup",
664 moduleTypeUnderTestFactory: android.FileGroupFactory,
665 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000666 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500667 name: "foobar",
668 srcs: [
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000669 ":foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500670 "c",
671 ],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500672 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500673}`,
674 expectedBazelTargets: []string{`filegroup(
675 name = "foobar",
676 srcs = [
677 "//other:foo",
678 "c",
679 ],
680)`,
681 },
Jingwen Chen5146ac02021-09-02 11:44:42 +0000682 filesystem: map[string]string{
Liz Kammer356f7d42021-01-26 09:18:53 -0500683 "other/Android.bp": `filegroup {
684 name: "foo",
685 srcs: ["a", "b"],
Liz Kammer6eff3232021-08-26 08:37:59 -0400686 bazel_module: { bp2build_available: true },
687}`,
688 },
689 },
690 {
691 description: "depends_on_other_unconverted_module_error",
692 moduleTypeUnderTest: "filegroup",
693 moduleTypeUnderTestFactory: android.FileGroupFactory,
694 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
695 unconvertedDepsMode: errorModulesUnconvertedDeps,
696 blueprint: `filegroup {
697 name: "foobar",
698 srcs: [
699 ":foo",
700 "c",
701 ],
702 bazel_module: { bp2build_available: true },
703}`,
704 expectedErr: fmt.Errorf(`"foobar" depends on unconverted modules: foo`),
705 filesystem: map[string]string{
706 "other/Android.bp": `filegroup {
707 name: "foo",
708 srcs: ["a", "b"],
Liz Kammer356f7d42021-01-26 09:18:53 -0500709}`,
710 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500711 },
712 }
713
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500714 for _, testCase := range testCases {
Liz Kammer6eff3232021-08-26 08:37:59 -0400715 t.Run(testCase.description, func(t *testing.T) {
716 runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, testCase)
717 })
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500718 }
719}
Jingwen Chen041b1842021-02-01 00:23:25 -0500720
721type bp2buildMutator = func(android.TopDownMutatorContext)
722
Jingwen Chen12b4c272021-03-10 02:05:59 -0500723func TestAllowlistingBp2buildTargetsExplicitly(t *testing.T) {
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500724 testCases := []struct {
725 moduleTypeUnderTest string
726 moduleTypeUnderTestFactory android.ModuleFactory
727 moduleTypeUnderTestBp2BuildMutator bp2buildMutator
728 bp string
729 expectedCount int
730 description string
731 }{
732 {
733 description: "explicitly unavailable",
734 moduleTypeUnderTest: "filegroup",
735 moduleTypeUnderTestFactory: android.FileGroupFactory,
736 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
737 bp: `filegroup {
738 name: "foo",
739 srcs: ["a", "b"],
740 bazel_module: { bp2build_available: false },
741}`,
742 expectedCount: 0,
743 },
744 {
745 description: "implicitly unavailable",
746 moduleTypeUnderTest: "filegroup",
747 moduleTypeUnderTestFactory: android.FileGroupFactory,
748 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
749 bp: `filegroup {
750 name: "foo",
751 srcs: ["a", "b"],
752}`,
753 expectedCount: 0,
754 },
755 {
756 description: "explicitly available",
757 moduleTypeUnderTest: "filegroup",
758 moduleTypeUnderTestFactory: android.FileGroupFactory,
759 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
760 bp: `filegroup {
761 name: "foo",
762 srcs: ["a", "b"],
763 bazel_module: { bp2build_available: true },
764}`,
765 expectedCount: 1,
766 },
767 {
768 description: "generates more than 1 target if needed",
769 moduleTypeUnderTest: "custom",
770 moduleTypeUnderTestFactory: customModuleFactory,
771 moduleTypeUnderTestBp2BuildMutator: customBp2BuildMutatorFromStarlark,
772 bp: `custom {
773 name: "foo",
774 bazel_module: { bp2build_available: true },
775}`,
776 expectedCount: 3,
777 },
778 }
779
780 dir := "."
781 for _, testCase := range testCases {
Liz Kammer2ada09a2021-08-11 00:17:36 -0400782 t.Run(testCase.description, func(t *testing.T) {
783 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
784 ctx := android.NewTestContext(config)
785 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
786 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
787 ctx.RegisterForBazelConversion()
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500788
Liz Kammer2ada09a2021-08-11 00:17:36 -0400789 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
790 android.FailIfErrored(t, errs)
791 _, errs = ctx.ResolveDependencies(config)
792 android.FailIfErrored(t, errs)
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500793
Liz Kammer2ada09a2021-08-11 00:17:36 -0400794 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Liz Kammer6eff3232021-08-26 08:37:59 -0400795 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
796 android.FailIfErrored(t, err)
Liz Kammer2ada09a2021-08-11 00:17:36 -0400797 if actualCount := len(bazelTargets); actualCount != testCase.expectedCount {
798 t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, testCase.expectedCount, actualCount)
799 }
800 })
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500801 }
802}
Liz Kammerba3ea162021-02-17 13:22:03 -0500803
Jingwen Chen12b4c272021-03-10 02:05:59 -0500804func TestAllowlistingBp2buildTargetsWithConfig(t *testing.T) {
805 testCases := []struct {
806 moduleTypeUnderTest string
807 moduleTypeUnderTestFactory android.ModuleFactory
808 moduleTypeUnderTestBp2BuildMutator bp2buildMutator
809 expectedCount map[string]int
810 description string
811 bp2buildConfig android.Bp2BuildConfig
812 checkDir string
813 fs map[string]string
814 }{
815 {
816 description: "test bp2build config package and subpackages config",
817 moduleTypeUnderTest: "filegroup",
818 moduleTypeUnderTestFactory: android.FileGroupFactory,
819 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
820 expectedCount: map[string]int{
821 "migrated": 1,
822 "migrated/but_not_really": 0,
823 "migrated/but_not_really/but_really": 1,
824 "not_migrated": 0,
825 "also_not_migrated": 0,
826 },
827 bp2buildConfig: android.Bp2BuildConfig{
828 "migrated": android.Bp2BuildDefaultTrueRecursively,
829 "migrated/but_not_really": android.Bp2BuildDefaultFalse,
830 "not_migrated": android.Bp2BuildDefaultFalse,
831 },
832 fs: map[string]string{
833 "migrated/Android.bp": `filegroup { name: "a" }`,
834 "migrated/but_not_really/Android.bp": `filegroup { name: "b" }`,
835 "migrated/but_not_really/but_really/Android.bp": `filegroup { name: "c" }`,
836 "not_migrated/Android.bp": `filegroup { name: "d" }`,
837 "also_not_migrated/Android.bp": `filegroup { name: "e" }`,
838 },
839 },
840 {
841 description: "test bp2build config opt-in and opt-out",
842 moduleTypeUnderTest: "filegroup",
843 moduleTypeUnderTestFactory: android.FileGroupFactory,
844 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
845 expectedCount: map[string]int{
846 "package-opt-in": 2,
847 "package-opt-in/subpackage": 0,
848 "package-opt-out": 1,
849 "package-opt-out/subpackage": 0,
850 },
851 bp2buildConfig: android.Bp2BuildConfig{
852 "package-opt-in": android.Bp2BuildDefaultFalse,
853 "package-opt-out": android.Bp2BuildDefaultTrueRecursively,
854 },
855 fs: map[string]string{
856 "package-opt-in/Android.bp": `
857filegroup { name: "opt-in-a" }
858filegroup { name: "opt-in-b", bazel_module: { bp2build_available: true } }
859filegroup { name: "opt-in-c", bazel_module: { bp2build_available: true } }
860`,
861
862 "package-opt-in/subpackage/Android.bp": `
863filegroup { name: "opt-in-d" } // parent package not configured to DefaultTrueRecursively
864`,
865
866 "package-opt-out/Android.bp": `
867filegroup { name: "opt-out-a" }
868filegroup { name: "opt-out-b", bazel_module: { bp2build_available: false } }
869filegroup { name: "opt-out-c", bazel_module: { bp2build_available: false } }
870`,
871
872 "package-opt-out/subpackage/Android.bp": `
873filegroup { name: "opt-out-g", bazel_module: { bp2build_available: false } }
874filegroup { name: "opt-out-h", bazel_module: { bp2build_available: false } }
875`,
876 },
877 },
878 }
879
880 dir := "."
881 for _, testCase := range testCases {
882 fs := make(map[string][]byte)
883 toParse := []string{
884 "Android.bp",
885 }
886 for f, content := range testCase.fs {
887 if strings.HasSuffix(f, "Android.bp") {
888 toParse = append(toParse, f)
889 }
890 fs[f] = []byte(content)
891 }
892 config := android.TestConfig(buildDir, nil, "", fs)
893 ctx := android.NewTestContext(config)
894 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
895 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
896 ctx.RegisterBp2BuildConfig(testCase.bp2buildConfig)
897 ctx.RegisterForBazelConversion()
898
899 _, errs := ctx.ParseFileList(dir, toParse)
900 android.FailIfErrored(t, errs)
901 _, errs = ctx.ResolveDependencies(config)
902 android.FailIfErrored(t, errs)
903
904 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
905
906 // For each directory, test that the expected number of generated targets is correct.
907 for dir, expectedCount := range testCase.expectedCount {
Liz Kammer6eff3232021-08-26 08:37:59 -0400908 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir)
909 android.FailIfErrored(t, err)
Jingwen Chen12b4c272021-03-10 02:05:59 -0500910 if actualCount := len(bazelTargets); actualCount != expectedCount {
911 t.Fatalf(
912 "%s: Expected %d bazel target for %s package, got %d",
913 testCase.description,
914 expectedCount,
915 dir,
916 actualCount)
917 }
918
919 }
920 }
921}
922
Liz Kammerba3ea162021-02-17 13:22:03 -0500923func TestCombineBuildFilesBp2buildTargets(t *testing.T) {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000924 testCases := []bp2buildTestCase{
Liz Kammerba3ea162021-02-17 13:22:03 -0500925 {
926 description: "filegroup bazel_module.label",
927 moduleTypeUnderTest: "filegroup",
928 moduleTypeUnderTestFactory: android.FileGroupFactory,
929 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000930 blueprint: `filegroup {
Liz Kammerba3ea162021-02-17 13:22:03 -0500931 name: "fg_foo",
932 bazel_module: { label: "//other:fg_foo" },
933}`,
934 expectedBazelTargets: []string{
935 `// BUILD file`,
936 },
Jingwen Chen5146ac02021-09-02 11:44:42 +0000937 filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -0500938 "other/BUILD.bazel": `// BUILD file`,
939 },
940 },
941 {
942 description: "multiple bazel_module.label same BUILD",
943 moduleTypeUnderTest: "filegroup",
944 moduleTypeUnderTestFactory: android.FileGroupFactory,
945 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000946 blueprint: `filegroup {
Jingwen Chenc63677b2021-06-17 05:43:19 +0000947 name: "fg_foo",
948 bazel_module: { label: "//other:fg_foo" },
949 }
Liz Kammerba3ea162021-02-17 13:22:03 -0500950
Jingwen Chenc63677b2021-06-17 05:43:19 +0000951 filegroup {
952 name: "foo",
953 bazel_module: { label: "//other:foo" },
954 }`,
Liz Kammerba3ea162021-02-17 13:22:03 -0500955 expectedBazelTargets: []string{
956 `// BUILD file`,
957 },
Jingwen Chen5146ac02021-09-02 11:44:42 +0000958 filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -0500959 "other/BUILD.bazel": `// BUILD file`,
960 },
961 },
962 {
Jingwen Chenc63677b2021-06-17 05:43:19 +0000963 description: "filegroup bazel_module.label and bp2build in subdir",
Liz Kammerba3ea162021-02-17 13:22:03 -0500964 moduleTypeUnderTest: "filegroup",
965 moduleTypeUnderTestFactory: android.FileGroupFactory,
966 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chenc63677b2021-06-17 05:43:19 +0000967 dir: "other",
Jingwen Chen5146ac02021-09-02 11:44:42 +0000968 blueprint: ``,
969 filesystem: map[string]string{
Jingwen Chenc63677b2021-06-17 05:43:19 +0000970 "other/Android.bp": `filegroup {
971 name: "fg_foo",
972 bazel_module: {
973 bp2build_available: true,
974 },
975 }
976 filegroup {
977 name: "fg_bar",
978 bazel_module: {
979 label: "//other:fg_bar"
980 },
981 }`,
982 "other/BUILD.bazel": `// definition for fg_bar`,
983 },
Liz Kammerba3ea162021-02-17 13:22:03 -0500984 expectedBazelTargets: []string{
985 `filegroup(
986 name = "fg_foo",
Jingwen Chenc63677b2021-06-17 05:43:19 +0000987)`, `// definition for fg_bar`,
Liz Kammerba3ea162021-02-17 13:22:03 -0500988 },
989 },
990 {
991 description: "filegroup bazel_module.label and filegroup bp2build",
992 moduleTypeUnderTest: "filegroup",
993 moduleTypeUnderTestFactory: android.FileGroupFactory,
994 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000995 blueprint: `filegroup {
Jingwen Chenc63677b2021-06-17 05:43:19 +0000996 name: "fg_foo",
997 bazel_module: {
998 label: "//other:fg_foo",
999 },
1000 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001001
Jingwen Chenc63677b2021-06-17 05:43:19 +00001002 filegroup {
1003 name: "fg_bar",
1004 bazel_module: {
1005 bp2build_available: true,
1006 },
1007 }`,
Liz Kammerba3ea162021-02-17 13:22:03 -05001008 expectedBazelTargets: []string{
1009 `filegroup(
1010 name = "fg_bar",
1011)`,
1012 `// BUILD file`,
1013 },
Jingwen Chen5146ac02021-09-02 11:44:42 +00001014 filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -05001015 "other/BUILD.bazel": `// BUILD file`,
1016 },
1017 },
1018 }
1019
1020 dir := "."
1021 for _, testCase := range testCases {
Jingwen Chen49109762021-05-25 05:16:48 +00001022 t.Run(testCase.description, func(t *testing.T) {
1023 fs := make(map[string][]byte)
1024 toParse := []string{
1025 "Android.bp",
Liz Kammerba3ea162021-02-17 13:22:03 -05001026 }
Jingwen Chen5146ac02021-09-02 11:44:42 +00001027 for f, content := range testCase.filesystem {
Jingwen Chen49109762021-05-25 05:16:48 +00001028 if strings.HasSuffix(f, "Android.bp") {
1029 toParse = append(toParse, f)
1030 }
1031 fs[f] = []byte(content)
1032 }
Jingwen Chen5146ac02021-09-02 11:44:42 +00001033 config := android.TestConfig(buildDir, nil, testCase.blueprint, fs)
Jingwen Chen49109762021-05-25 05:16:48 +00001034 ctx := android.NewTestContext(config)
1035 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Jingwen Chen49109762021-05-25 05:16:48 +00001036 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1037 ctx.RegisterForBazelConversion()
Liz Kammerba3ea162021-02-17 13:22:03 -05001038
Jingwen Chen49109762021-05-25 05:16:48 +00001039 _, errs := ctx.ParseFileList(dir, toParse)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001040 if errored(t, testCase, errs) {
Jingwen Chen49109762021-05-25 05:16:48 +00001041 return
1042 }
1043 _, errs = ctx.ResolveDependencies(config)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001044 if errored(t, testCase, errs) {
Jingwen Chen49109762021-05-25 05:16:48 +00001045 return
1046 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001047
Jingwen Chen49109762021-05-25 05:16:48 +00001048 checkDir := dir
1049 if testCase.dir != "" {
1050 checkDir = testCase.dir
1051 }
Liz Kammer6eff3232021-08-26 08:37:59 -04001052 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
1053 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, checkDir)
1054 android.FailIfErrored(t, err)
Jingwen Chen49109762021-05-25 05:16:48 +00001055 bazelTargets.sort()
1056 actualCount := len(bazelTargets)
1057 expectedCount := len(testCase.expectedBazelTargets)
1058 if actualCount != expectedCount {
1059 t.Errorf("Expected %d bazel target, got %d\n%s", expectedCount, actualCount, bazelTargets)
1060 }
1061 if !strings.Contains(bazelTargets.String(), "Section: Handcrafted targets. ") {
1062 t.Errorf("Expected string representation of bazelTargets to contain handcrafted section header.")
1063 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001064 for i, target := range bazelTargets {
Jingwen Chen49109762021-05-25 05:16:48 +00001065 actualContent := target.content
1066 expectedContent := testCase.expectedBazelTargets[i]
1067 if expectedContent != actualContent {
Liz Kammerba3ea162021-02-17 13:22:03 -05001068 t.Errorf(
Jingwen Chen49109762021-05-25 05:16:48 +00001069 "Expected generated Bazel target to be '%s', got '%s'",
1070 expectedContent,
1071 actualContent,
Liz Kammerba3ea162021-02-17 13:22:03 -05001072 )
1073 }
1074 }
Jingwen Chen49109762021-05-25 05:16:48 +00001075 })
Liz Kammerba3ea162021-02-17 13:22:03 -05001076 }
1077}
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001078
1079func TestGlobExcludeSrcs(t *testing.T) {
Jingwen Chen5146ac02021-09-02 11:44:42 +00001080 testCases := []bp2buildTestCase{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001081 {
1082 description: "filegroup top level exclude_srcs",
1083 moduleTypeUnderTest: "filegroup",
1084 moduleTypeUnderTestFactory: android.FileGroupFactory,
1085 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +00001086 blueprint: `filegroup {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001087 name: "fg_foo",
1088 srcs: ["**/*.txt"],
1089 exclude_srcs: ["c.txt"],
1090 bazel_module: { bp2build_available: true },
1091}`,
1092 expectedBazelTargets: []string{`filegroup(
1093 name = "fg_foo",
1094 srcs = [
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001095 "a.txt",
1096 "b.txt",
Liz Kammer9abd62d2021-05-21 08:37:59 -04001097 "//dir:e.txt",
1098 "//dir:f.txt",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001099 ],
1100)`,
1101 },
Jingwen Chen5146ac02021-09-02 11:44:42 +00001102 filesystem: map[string]string{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001103 "a.txt": "",
1104 "b.txt": "",
1105 "c.txt": "",
1106 "dir/Android.bp": "",
1107 "dir/e.txt": "",
1108 "dir/f.txt": "",
1109 },
1110 },
1111 {
1112 description: "filegroup in subdir exclude_srcs",
1113 moduleTypeUnderTest: "filegroup",
1114 moduleTypeUnderTestFactory: android.FileGroupFactory,
1115 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +00001116 blueprint: "",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001117 dir: "dir",
Jingwen Chen5146ac02021-09-02 11:44:42 +00001118 filesystem: map[string]string{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001119 "dir/Android.bp": `filegroup {
1120 name: "fg_foo",
1121 srcs: ["**/*.txt"],
1122 exclude_srcs: ["b.txt"],
1123 bazel_module: { bp2build_available: true },
1124}
1125`,
1126 "dir/a.txt": "",
1127 "dir/b.txt": "",
1128 "dir/subdir/Android.bp": "",
1129 "dir/subdir/e.txt": "",
1130 "dir/subdir/f.txt": "",
1131 },
1132 expectedBazelTargets: []string{`filegroup(
1133 name = "fg_foo",
1134 srcs = [
Liz Kammer9abd62d2021-05-21 08:37:59 -04001135 "a.txt",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001136 "//dir/subdir:e.txt",
1137 "//dir/subdir:f.txt",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001138 ],
1139)`,
1140 },
1141 },
1142 }
1143
1144 dir := "."
1145 for _, testCase := range testCases {
1146 fs := make(map[string][]byte)
1147 toParse := []string{
1148 "Android.bp",
1149 }
Jingwen Chen5146ac02021-09-02 11:44:42 +00001150 for f, content := range testCase.filesystem {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001151 if strings.HasSuffix(f, "Android.bp") {
1152 toParse = append(toParse, f)
1153 }
1154 fs[f] = []byte(content)
1155 }
Jingwen Chen5146ac02021-09-02 11:44:42 +00001156 config := android.TestConfig(buildDir, nil, testCase.blueprint, fs)
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001157 ctx := android.NewTestContext(config)
1158 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
1159 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1160 ctx.RegisterForBazelConversion()
1161
1162 _, errs := ctx.ParseFileList(dir, toParse)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001163 if errored(t, testCase, errs) {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001164 continue
1165 }
1166 _, errs = ctx.ResolveDependencies(config)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001167 if errored(t, testCase, errs) {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001168 continue
1169 }
1170
1171 checkDir := dir
1172 if testCase.dir != "" {
1173 checkDir = testCase.dir
1174 }
Liz Kammer6eff3232021-08-26 08:37:59 -04001175 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
1176 bazelTargets, err := generateBazelTargetsForDir(codegenCtx, checkDir)
1177 android.FailIfErrored(t, err)
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001178 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
1179 t.Errorf("%s: Expected %d bazel target, got %d\n%s", testCase.description, expectedCount, actualCount, bazelTargets)
1180 } else {
1181 for i, target := range bazelTargets {
1182 if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
1183 t.Errorf(
1184 "%s: Expected generated Bazel target to be '%s', got '%s'",
1185 testCase.description,
1186 w,
1187 g,
1188 )
1189 }
1190 }
1191 }
1192 }
1193}