blob: ecea6b2d50fbfe64ee4fc2b4a43e4cad843fe496 [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 Kammer356f7d42021-01-26 09:18:53 -050019 "strings"
Liz Kammer2dd9ca42020-11-25 16:06:39 -080020 "testing"
21)
22
23func TestGenerateSoongModuleTargets(t *testing.T) {
24 testCases := []struct {
Liz Kammerd366c902021-06-03 13:43:01 -040025 description string
Liz Kammer2dd9ca42020-11-25 16:06:39 -080026 bp string
27 expectedBazelTarget string
28 }{
29 {
Liz Kammerd366c902021-06-03 13:43:01 -040030 description: "only name",
Jingwen Chenb4628eb2021-04-08 14:40:57 +000031 bp: `custom { name: "foo" }
Liz Kammerd366c902021-06-03 13:43:01 -040032 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080033 expectedBazelTarget: `soong_module(
34 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050035 soong_module_name = "foo",
36 soong_module_type = "custom",
37 soong_module_variant = "",
38 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080039 ],
Liz Kammerd366c902021-06-03 13:43:01 -040040 bool_prop = False,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080041)`,
42 },
43 {
Liz Kammerd366c902021-06-03 13:43:01 -040044 description: "handles bool",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080045 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -040046 name: "foo",
47 bool_prop: true,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080048}
Liz Kammerd366c902021-06-03 13:43:01 -040049 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080050 expectedBazelTarget: `soong_module(
51 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050052 soong_module_name = "foo",
53 soong_module_type = "custom",
54 soong_module_variant = "",
55 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080056 ],
Liz Kammerd366c902021-06-03 13:43:01 -040057 bool_prop = True,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080058)`,
59 },
60 {
Liz Kammerd366c902021-06-03 13:43:01 -040061 description: "string escaping",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080062 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -040063 name: "foo",
64 owner: "a_string_with\"quotes\"_and_\\backslashes\\\\",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080065}
Liz Kammerd366c902021-06-03 13:43:01 -040066 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080067 expectedBazelTarget: `soong_module(
68 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050069 soong_module_name = "foo",
70 soong_module_type = "custom",
71 soong_module_variant = "",
72 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080073 ],
Liz Kammerd366c902021-06-03 13:43:01 -040074 bool_prop = False,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080075 owner = "a_string_with\"quotes\"_and_\\backslashes\\\\",
76)`,
77 },
78 {
Liz Kammerd366c902021-06-03 13:43:01 -040079 description: "single item string list",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080080 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -040081 name: "foo",
82 required: ["bar"],
Liz Kammer2dd9ca42020-11-25 16:06:39 -080083}
Liz Kammerd366c902021-06-03 13:43:01 -040084 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080085 expectedBazelTarget: `soong_module(
86 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050087 soong_module_name = "foo",
88 soong_module_type = "custom",
89 soong_module_variant = "",
90 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080091 ],
Liz Kammerd366c902021-06-03 13:43:01 -040092 bool_prop = False,
Jingwen Chenb4628eb2021-04-08 14:40:57 +000093 required = ["bar"],
Liz Kammer2dd9ca42020-11-25 16:06:39 -080094)`,
95 },
96 {
Liz Kammerd366c902021-06-03 13:43:01 -040097 description: "list of strings",
Liz Kammer2dd9ca42020-11-25 16:06:39 -080098 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -040099 name: "foo",
100 target_required: ["qux", "bazqux"],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800101}
Liz Kammerd366c902021-06-03 13:43:01 -0400102 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800103 expectedBazelTarget: `soong_module(
104 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500105 soong_module_name = "foo",
106 soong_module_type = "custom",
107 soong_module_variant = "",
108 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800109 ],
Liz Kammerd366c902021-06-03 13:43:01 -0400110 bool_prop = False,
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800111 target_required = [
112 "qux",
113 "bazqux",
114 ],
115)`,
116 },
117 {
Liz Kammerd366c902021-06-03 13:43:01 -0400118 description: "dist/dists",
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800119 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -0400120 name: "foo",
121 dist: {
122 targets: ["goal_foo"],
123 tag: ".foo",
124 },
125 dists: [{
126 targets: ["goal_bar"],
127 tag: ".bar",
128 }],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800129}
Liz Kammerd366c902021-06-03 13:43:01 -0400130 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800131 expectedBazelTarget: `soong_module(
132 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500133 soong_module_name = "foo",
134 soong_module_type = "custom",
135 soong_module_variant = "",
136 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800137 ],
Liz Kammerd366c902021-06-03 13:43:01 -0400138 bool_prop = False,
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800139 dist = {
140 "tag": ".foo",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000141 "targets": ["goal_foo"],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800142 },
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000143 dists = [{
144 "tag": ".bar",
145 "targets": ["goal_bar"],
146 }],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800147)`,
148 },
149 {
Liz Kammerd366c902021-06-03 13:43:01 -0400150 description: "put it together",
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800151 bp: `custom {
Liz Kammerd366c902021-06-03 13:43:01 -0400152 name: "foo",
153 required: ["bar"],
154 target_required: ["qux", "bazqux"],
155 bool_prop: true,
156 owner: "custom_owner",
157 dists: [
158 {
159 tag: ".tag",
160 targets: ["my_goal"],
161 },
162 ],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800163}
Liz Kammerd366c902021-06-03 13:43:01 -0400164 `,
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800165 expectedBazelTarget: `soong_module(
166 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500167 soong_module_name = "foo",
168 soong_module_type = "custom",
169 soong_module_variant = "",
170 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800171 ],
Liz Kammerd366c902021-06-03 13:43:01 -0400172 bool_prop = True,
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000173 dists = [{
174 "tag": ".tag",
175 "targets": ["my_goal"],
176 }],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800177 owner = "custom_owner",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000178 required = ["bar"],
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800179 target_required = [
180 "qux",
181 "bazqux",
182 ],
183)`,
184 },
185 }
186
187 dir := "."
188 for _, testCase := range testCases {
Liz Kammerd366c902021-06-03 13:43:01 -0400189 t.Run(testCase.description, func(t *testing.T) {
190 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
191 ctx := android.NewTestContext(config)
Jingwen Chen164e0862021-02-19 00:48:40 -0500192
Liz Kammerd366c902021-06-03 13:43:01 -0400193 ctx.RegisterModuleType("custom", customModuleFactory)
194 ctx.Register()
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800195
Liz Kammerd366c902021-06-03 13:43:01 -0400196 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
197 android.FailIfErrored(t, errs)
198 _, errs = ctx.PrepareBuildActions(config)
199 android.FailIfErrored(t, errs)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800200
Liz Kammerd366c902021-06-03 13:43:01 -0400201 codegenCtx := NewCodegenContext(config, *ctx.Context, QueryView)
202 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
203 if actualCount, expectedCount := len(bazelTargets), 1; actualCount != expectedCount {
204 t.Fatalf("Expected %d bazel target, got %d", expectedCount, actualCount)
205 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800206
Liz Kammerd366c902021-06-03 13:43:01 -0400207 actualBazelTarget := bazelTargets[0]
208 if actualBazelTarget.content != testCase.expectedBazelTarget {
209 t.Errorf(
210 "Expected generated Bazel target to be '%s', got '%s'",
211 testCase.expectedBazelTarget,
212 actualBazelTarget.content,
213 )
214 }
215 })
Jingwen Chen73850672020-12-14 08:25:34 -0500216 }
217}
218
219func TestGenerateBazelTargetModules(t *testing.T) {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000220 testCases := []bp2buildTestCase{
Jingwen Chen73850672020-12-14 08:25:34 -0500221 {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000222 blueprint: `custom {
Jingwen Chen73850672020-12-14 08:25:34 -0500223 name: "foo",
224 string_list_prop: ["a", "b"],
225 string_prop: "a",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500226 bazel_module: { bp2build_available: true },
Jingwen Chen73850672020-12-14 08:25:34 -0500227}`,
Liz Kammer4562a3b2021-04-21 18:15:34 -0400228 expectedBazelTargets: []string{`custom(
Jingwen Chen73850672020-12-14 08:25:34 -0500229 name = "foo",
230 string_list_prop = [
231 "a",
232 "b",
233 ],
234 string_prop = "a",
235)`,
Liz Kammer4562a3b2021-04-21 18:15:34 -0400236 },
Jingwen Chen73850672020-12-14 08:25:34 -0500237 },
Jingwen Chen58a12b82021-03-30 13:08:36 +0000238 {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000239 blueprint: `custom {
Jingwen Chen58a12b82021-03-30 13:08:36 +0000240 name: "control_characters",
241 string_list_prop: ["\t", "\n"],
242 string_prop: "a\t\n\r",
243 bazel_module: { bp2build_available: true },
244}`,
Liz Kammer4562a3b2021-04-21 18:15:34 -0400245 expectedBazelTargets: []string{`custom(
Jingwen Chen58a12b82021-03-30 13:08:36 +0000246 name = "control_characters",
247 string_list_prop = [
248 "\t",
249 "\n",
250 ],
251 string_prop = "a\t\n\r",
252)`,
Liz Kammer4562a3b2021-04-21 18:15:34 -0400253 },
254 },
255 {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000256 blueprint: `custom {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400257 name: "has_dep",
258 arch_paths: [":dep"],
259 bazel_module: { bp2build_available: true },
260}
261
262custom {
263 name: "dep",
264 arch_paths: ["abc"],
265 bazel_module: { bp2build_available: true },
266}`,
267 expectedBazelTargets: []string{`custom(
268 name = "dep",
269 arch_paths = ["abc"],
270)`,
271 `custom(
272 name = "has_dep",
273 arch_paths = [":dep"],
274)`,
275 },
276 },
277 {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000278 blueprint: `custom {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400279 name: "arch_paths",
280 arch: {
281 x86: {
282 arch_paths: ["abc"],
283 },
284 },
285 bazel_module: { bp2build_available: true },
286}`,
287 expectedBazelTargets: []string{`custom(
288 name = "arch_paths",
289 arch_paths = select({
290 "//build/bazel/platforms/arch:x86": ["abc"],
291 "//conditions:default": [],
292 }),
293)`,
294 },
295 },
296 {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000297 blueprint: `custom {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400298 name: "has_dep",
299 arch: {
300 x86: {
301 arch_paths: [":dep"],
302 },
303 },
304 bazel_module: { bp2build_available: true },
305}
306
307custom {
308 name: "dep",
309 arch_paths: ["abc"],
310 bazel_module: { bp2build_available: true },
311}`,
312 expectedBazelTargets: []string{`custom(
313 name = "dep",
314 arch_paths = ["abc"],
315)`,
316 `custom(
317 name = "has_dep",
318 arch_paths = select({
319 "//build/bazel/platforms/arch:x86": [":dep"],
320 "//conditions:default": [],
321 }),
322)`,
323 },
Jingwen Chen58a12b82021-03-30 13:08:36 +0000324 },
Jingwen Chen73850672020-12-14 08:25:34 -0500325 }
326
327 dir := "."
328 for _, testCase := range testCases {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000329 config := android.TestConfig(buildDir, nil, testCase.blueprint, nil)
Jingwen Chen73850672020-12-14 08:25:34 -0500330 ctx := android.NewTestContext(config)
Jingwen Chen164e0862021-02-19 00:48:40 -0500331
Liz Kammer32b77cf2021-08-04 15:17:02 -0400332 registerCustomModuleForBp2buildConversion(ctx)
Jingwen Chen73850672020-12-14 08:25:34 -0500333
334 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
Jingwen Chen5146ac02021-09-02 11:44:42 +0000335 if errored(t, testCase, errs) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500336 continue
337 }
Jingwen Chen73850672020-12-14 08:25:34 -0500338 _, errs = ctx.ResolveDependencies(config)
Jingwen Chen5146ac02021-09-02 11:44:42 +0000339 if errored(t, testCase, errs) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500340 continue
341 }
Jingwen Chen73850672020-12-14 08:25:34 -0500342
Jingwen Chen164e0862021-02-19 00:48:40 -0500343 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -0500344 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
Jingwen Chen164e0862021-02-19 00:48:40 -0500345
Liz Kammer4562a3b2021-04-21 18:15:34 -0400346 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
Liz Kammer356f7d42021-01-26 09:18:53 -0500347 t.Errorf("Expected %d bazel target, got %d", expectedCount, actualCount)
348 } else {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400349 for i, expectedBazelTarget := range testCase.expectedBazelTargets {
350 actualBazelTarget := bazelTargets[i]
351 if actualBazelTarget.content != expectedBazelTarget {
352 t.Errorf(
353 "Expected generated Bazel target to be '%s', got '%s'",
354 expectedBazelTarget,
355 actualBazelTarget.content,
356 )
357 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500358 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800359 }
360 }
361}
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500362
Jingwen Chen40067de2021-01-26 21:58:43 -0500363func TestLoadStatements(t *testing.T) {
364 testCases := []struct {
365 bazelTargets BazelTargets
366 expectedLoadStatements string
367 }{
368 {
369 bazelTargets: BazelTargets{
370 BazelTarget{
371 name: "foo",
372 ruleClass: "cc_library",
373 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
374 },
375 },
376 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
377 },
378 {
379 bazelTargets: BazelTargets{
380 BazelTarget{
381 name: "foo",
382 ruleClass: "cc_library",
383 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
384 },
385 BazelTarget{
386 name: "bar",
387 ruleClass: "cc_library",
388 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
389 },
390 },
391 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
392 },
393 {
394 bazelTargets: BazelTargets{
395 BazelTarget{
396 name: "foo",
397 ruleClass: "cc_library",
398 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
399 },
400 BazelTarget{
401 name: "bar",
402 ruleClass: "cc_binary",
403 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
404 },
405 },
406 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")`,
407 },
408 {
409 bazelTargets: BazelTargets{
410 BazelTarget{
411 name: "foo",
412 ruleClass: "cc_library",
413 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
414 },
415 BazelTarget{
416 name: "bar",
417 ruleClass: "cc_binary",
418 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
419 },
420 BazelTarget{
421 name: "baz",
422 ruleClass: "java_binary",
423 bzlLoadLocation: "//build/bazel/rules:java.bzl",
424 },
425 },
426 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")
427load("//build/bazel/rules:java.bzl", "java_binary")`,
428 },
429 {
430 bazelTargets: BazelTargets{
431 BazelTarget{
432 name: "foo",
433 ruleClass: "cc_binary",
434 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
435 },
436 BazelTarget{
437 name: "bar",
438 ruleClass: "java_binary",
439 bzlLoadLocation: "//build/bazel/rules:java.bzl",
440 },
441 BazelTarget{
442 name: "baz",
443 ruleClass: "genrule",
444 // Note: no bzlLoadLocation for native rules
445 },
446 },
447 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary")
448load("//build/bazel/rules:java.bzl", "java_binary")`,
449 },
450 }
451
452 for _, testCase := range testCases {
453 actual := testCase.bazelTargets.LoadStatements()
454 expected := testCase.expectedLoadStatements
455 if actual != expected {
456 t.Fatalf("Expected load statements to be %s, got %s", expected, actual)
457 }
458 }
459
460}
461
462func TestGenerateBazelTargetModules_OneToMany_LoadedFromStarlark(t *testing.T) {
463 testCases := []struct {
464 bp string
465 expectedBazelTarget string
466 expectedBazelTargetCount int
467 expectedLoadStatements string
468 }{
469 {
470 bp: `custom {
471 name: "bar",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500472 bazel_module: { bp2build_available: true },
Jingwen Chen40067de2021-01-26 21:58:43 -0500473}`,
474 expectedBazelTarget: `my_library(
475 name = "bar",
476)
477
Jingwen Chen40067de2021-01-26 21:58:43 -0500478proto_library(
479 name = "bar_proto_library_deps",
Liz Kammer2ada09a2021-08-11 00:17:36 -0400480)
481
482my_proto_library(
483 name = "bar_my_proto_library_deps",
Jingwen Chen40067de2021-01-26 21:58:43 -0500484)`,
485 expectedBazelTargetCount: 3,
486 expectedLoadStatements: `load("//build/bazel/rules:proto.bzl", "my_proto_library", "proto_library")
487load("//build/bazel/rules:rules.bzl", "my_library")`,
488 },
489 }
490
491 dir := "."
492 for _, testCase := range testCases {
493 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
494 ctx := android.NewTestContext(config)
495 ctx.RegisterModuleType("custom", customModuleFactory)
Jingwen Chen12b4c272021-03-10 02:05:59 -0500496 ctx.RegisterBp2BuildMutator("custom", customBp2BuildMutatorFromStarlark)
Jingwen Chen40067de2021-01-26 21:58:43 -0500497 ctx.RegisterForBazelConversion()
498
499 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
500 android.FailIfErrored(t, errs)
501 _, errs = ctx.ResolveDependencies(config)
502 android.FailIfErrored(t, errs)
503
Jingwen Chen164e0862021-02-19 00:48:40 -0500504 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -0500505 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
Jingwen Chen40067de2021-01-26 21:58:43 -0500506 if actualCount := len(bazelTargets); actualCount != testCase.expectedBazelTargetCount {
507 t.Fatalf("Expected %d bazel target, got %d", testCase.expectedBazelTargetCount, actualCount)
508 }
509
510 actualBazelTargets := bazelTargets.String()
511 if actualBazelTargets != testCase.expectedBazelTarget {
512 t.Errorf(
513 "Expected generated Bazel target to be '%s', got '%s'",
514 testCase.expectedBazelTarget,
515 actualBazelTargets,
516 )
517 }
518
519 actualLoadStatements := bazelTargets.LoadStatements()
520 if actualLoadStatements != testCase.expectedLoadStatements {
521 t.Errorf(
522 "Expected generated load statements to be '%s', got '%s'",
523 testCase.expectedLoadStatements,
524 actualLoadStatements,
525 )
526 }
527 }
528}
529
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500530func TestModuleTypeBp2Build(t *testing.T) {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000531 testCases := []bp2buildTestCase{
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500532 {
Liz Kammerebfcf672021-02-16 15:00:05 -0500533 description: "filegroup with does not specify srcs",
534 moduleTypeUnderTest: "filegroup",
535 moduleTypeUnderTestFactory: android.FileGroupFactory,
536 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000537 blueprint: `filegroup {
Liz Kammerebfcf672021-02-16 15:00:05 -0500538 name: "fg_foo",
539 bazel_module: { bp2build_available: true },
540}`,
541 expectedBazelTargets: []string{
542 `filegroup(
543 name = "fg_foo",
544)`,
545 },
546 },
547 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500548 description: "filegroup with no srcs",
549 moduleTypeUnderTest: "filegroup",
550 moduleTypeUnderTestFactory: android.FileGroupFactory,
551 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000552 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500553 name: "fg_foo",
554 srcs: [],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500555 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500556}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500557 expectedBazelTargets: []string{
558 `filegroup(
559 name = "fg_foo",
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500560)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500561 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500562 },
563 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500564 description: "filegroup with srcs",
565 moduleTypeUnderTest: "filegroup",
566 moduleTypeUnderTestFactory: android.FileGroupFactory,
567 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000568 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500569 name: "fg_foo",
570 srcs: ["a", "b"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500571 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500572}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500573 expectedBazelTargets: []string{`filegroup(
574 name = "fg_foo",
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500575 srcs = [
576 "a",
577 "b",
578 ],
579)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500580 },
581 },
582 {
583 description: "filegroup with excludes srcs",
584 moduleTypeUnderTest: "filegroup",
585 moduleTypeUnderTestFactory: android.FileGroupFactory,
586 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000587 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500588 name: "fg_foo",
589 srcs: ["a", "b"],
590 exclude_srcs: ["a"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500591 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500592}`,
593 expectedBazelTargets: []string{`filegroup(
594 name = "fg_foo",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000595 srcs = ["b"],
Liz Kammer356f7d42021-01-26 09:18:53 -0500596)`,
597 },
598 },
599 {
600 description: "filegroup with glob",
601 moduleTypeUnderTest: "filegroup",
602 moduleTypeUnderTestFactory: android.FileGroupFactory,
603 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000604 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500605 name: "foo",
606 srcs: ["**/*.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500607 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500608}`,
609 expectedBazelTargets: []string{`filegroup(
610 name = "foo",
611 srcs = [
612 "other/a.txt",
613 "other/b.txt",
614 "other/subdir/a.txt",
615 ],
616)`,
617 },
Jingwen Chen5146ac02021-09-02 11:44:42 +0000618 filesystem: map[string]string{
Liz Kammer356f7d42021-01-26 09:18:53 -0500619 "other/a.txt": "",
620 "other/b.txt": "",
621 "other/subdir/a.txt": "",
622 "other/file": "",
623 },
624 },
625 {
626 description: "filegroup with glob in subdir",
627 moduleTypeUnderTest: "filegroup",
628 moduleTypeUnderTestFactory: android.FileGroupFactory,
629 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000630 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500631 name: "foo",
632 srcs: ["a.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500633 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500634}`,
635 dir: "other",
636 expectedBazelTargets: []string{`filegroup(
637 name = "fg_foo",
638 srcs = [
639 "a.txt",
640 "b.txt",
641 "subdir/a.txt",
642 ],
643)`,
644 },
Jingwen Chen5146ac02021-09-02 11:44:42 +0000645 filesystem: map[string]string{
Liz Kammer356f7d42021-01-26 09:18:53 -0500646 "other/Android.bp": `filegroup {
647 name: "fg_foo",
648 srcs: ["**/*.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500649 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500650}`,
651 "other/a.txt": "",
652 "other/b.txt": "",
653 "other/subdir/a.txt": "",
654 "other/file": "",
655 },
656 },
657 {
658 description: "depends_on_other_dir_module",
659 moduleTypeUnderTest: "filegroup",
660 moduleTypeUnderTestFactory: android.FileGroupFactory,
661 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000662 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500663 name: "foobar",
664 srcs: [
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000665 ":foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500666 "c",
667 ],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500668 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500669}`,
670 expectedBazelTargets: []string{`filegroup(
671 name = "foobar",
672 srcs = [
673 "//other:foo",
674 "c",
675 ],
676)`,
677 },
Jingwen Chen5146ac02021-09-02 11:44:42 +0000678 filesystem: map[string]string{
Liz Kammer356f7d42021-01-26 09:18:53 -0500679 "other/Android.bp": `filegroup {
680 name: "foo",
681 srcs: ["a", "b"],
682}`,
683 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500684 },
685 }
686
687 dir := "."
688 for _, testCase := range testCases {
Liz Kammer356f7d42021-01-26 09:18:53 -0500689 fs := make(map[string][]byte)
690 toParse := []string{
691 "Android.bp",
692 }
Jingwen Chen5146ac02021-09-02 11:44:42 +0000693 for f, content := range testCase.filesystem {
Liz Kammer356f7d42021-01-26 09:18:53 -0500694 if strings.HasSuffix(f, "Android.bp") {
695 toParse = append(toParse, f)
696 }
697 fs[f] = []byte(content)
698 }
Jingwen Chen5146ac02021-09-02 11:44:42 +0000699 config := android.TestConfig(buildDir, nil, testCase.blueprint, fs)
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500700 ctx := android.NewTestContext(config)
701 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Jingwen Chena42d6412021-01-26 21:57:27 -0500702 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500703 ctx.RegisterForBazelConversion()
704
Liz Kammer356f7d42021-01-26 09:18:53 -0500705 _, errs := ctx.ParseFileList(dir, toParse)
Jingwen Chen5146ac02021-09-02 11:44:42 +0000706 if errored(t, testCase, errs) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500707 continue
708 }
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500709 _, errs = ctx.ResolveDependencies(config)
Jingwen Chen5146ac02021-09-02 11:44:42 +0000710 if errored(t, testCase, errs) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500711 continue
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500712 }
713
Liz Kammer356f7d42021-01-26 09:18:53 -0500714 checkDir := dir
715 if testCase.dir != "" {
716 checkDir = testCase.dir
717 }
Jingwen Chen164e0862021-02-19 00:48:40 -0500718
719 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -0500720 bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir)
Liz Kammer356f7d42021-01-26 09:18:53 -0500721 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
722 t.Errorf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount)
723 } else {
724 for i, target := range bazelTargets {
725 if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
726 t.Errorf(
727 "%s: Expected generated Bazel target to be '%s', got '%s'",
728 testCase.description,
729 w,
730 g,
731 )
732 }
733 }
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500734 }
735 }
736}
Jingwen Chen041b1842021-02-01 00:23:25 -0500737
738type bp2buildMutator = func(android.TopDownMutatorContext)
739
Jingwen Chen12b4c272021-03-10 02:05:59 -0500740func TestAllowlistingBp2buildTargetsExplicitly(t *testing.T) {
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500741 testCases := []struct {
742 moduleTypeUnderTest string
743 moduleTypeUnderTestFactory android.ModuleFactory
744 moduleTypeUnderTestBp2BuildMutator bp2buildMutator
745 bp string
746 expectedCount int
747 description string
748 }{
749 {
750 description: "explicitly unavailable",
751 moduleTypeUnderTest: "filegroup",
752 moduleTypeUnderTestFactory: android.FileGroupFactory,
753 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
754 bp: `filegroup {
755 name: "foo",
756 srcs: ["a", "b"],
757 bazel_module: { bp2build_available: false },
758}`,
759 expectedCount: 0,
760 },
761 {
762 description: "implicitly unavailable",
763 moduleTypeUnderTest: "filegroup",
764 moduleTypeUnderTestFactory: android.FileGroupFactory,
765 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
766 bp: `filegroup {
767 name: "foo",
768 srcs: ["a", "b"],
769}`,
770 expectedCount: 0,
771 },
772 {
773 description: "explicitly available",
774 moduleTypeUnderTest: "filegroup",
775 moduleTypeUnderTestFactory: android.FileGroupFactory,
776 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
777 bp: `filegroup {
778 name: "foo",
779 srcs: ["a", "b"],
780 bazel_module: { bp2build_available: true },
781}`,
782 expectedCount: 1,
783 },
784 {
785 description: "generates more than 1 target if needed",
786 moduleTypeUnderTest: "custom",
787 moduleTypeUnderTestFactory: customModuleFactory,
788 moduleTypeUnderTestBp2BuildMutator: customBp2BuildMutatorFromStarlark,
789 bp: `custom {
790 name: "foo",
791 bazel_module: { bp2build_available: true },
792}`,
793 expectedCount: 3,
794 },
795 }
796
797 dir := "."
798 for _, testCase := range testCases {
Liz Kammer2ada09a2021-08-11 00:17:36 -0400799 t.Run(testCase.description, func(t *testing.T) {
800 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
801 ctx := android.NewTestContext(config)
802 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
803 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
804 ctx.RegisterForBazelConversion()
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500805
Liz Kammer2ada09a2021-08-11 00:17:36 -0400806 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
807 android.FailIfErrored(t, errs)
808 _, errs = ctx.ResolveDependencies(config)
809 android.FailIfErrored(t, errs)
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500810
Liz Kammer2ada09a2021-08-11 00:17:36 -0400811 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
812 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
813 if actualCount := len(bazelTargets); actualCount != testCase.expectedCount {
814 t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, testCase.expectedCount, actualCount)
815 }
816 })
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500817 }
818}
Liz Kammerba3ea162021-02-17 13:22:03 -0500819
Jingwen Chen12b4c272021-03-10 02:05:59 -0500820func TestAllowlistingBp2buildTargetsWithConfig(t *testing.T) {
821 testCases := []struct {
822 moduleTypeUnderTest string
823 moduleTypeUnderTestFactory android.ModuleFactory
824 moduleTypeUnderTestBp2BuildMutator bp2buildMutator
825 expectedCount map[string]int
826 description string
827 bp2buildConfig android.Bp2BuildConfig
828 checkDir string
829 fs map[string]string
830 }{
831 {
832 description: "test bp2build config package and subpackages config",
833 moduleTypeUnderTest: "filegroup",
834 moduleTypeUnderTestFactory: android.FileGroupFactory,
835 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
836 expectedCount: map[string]int{
837 "migrated": 1,
838 "migrated/but_not_really": 0,
839 "migrated/but_not_really/but_really": 1,
840 "not_migrated": 0,
841 "also_not_migrated": 0,
842 },
843 bp2buildConfig: android.Bp2BuildConfig{
844 "migrated": android.Bp2BuildDefaultTrueRecursively,
845 "migrated/but_not_really": android.Bp2BuildDefaultFalse,
846 "not_migrated": android.Bp2BuildDefaultFalse,
847 },
848 fs: map[string]string{
849 "migrated/Android.bp": `filegroup { name: "a" }`,
850 "migrated/but_not_really/Android.bp": `filegroup { name: "b" }`,
851 "migrated/but_not_really/but_really/Android.bp": `filegroup { name: "c" }`,
852 "not_migrated/Android.bp": `filegroup { name: "d" }`,
853 "also_not_migrated/Android.bp": `filegroup { name: "e" }`,
854 },
855 },
856 {
857 description: "test bp2build config opt-in and opt-out",
858 moduleTypeUnderTest: "filegroup",
859 moduleTypeUnderTestFactory: android.FileGroupFactory,
860 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
861 expectedCount: map[string]int{
862 "package-opt-in": 2,
863 "package-opt-in/subpackage": 0,
864 "package-opt-out": 1,
865 "package-opt-out/subpackage": 0,
866 },
867 bp2buildConfig: android.Bp2BuildConfig{
868 "package-opt-in": android.Bp2BuildDefaultFalse,
869 "package-opt-out": android.Bp2BuildDefaultTrueRecursively,
870 },
871 fs: map[string]string{
872 "package-opt-in/Android.bp": `
873filegroup { name: "opt-in-a" }
874filegroup { name: "opt-in-b", bazel_module: { bp2build_available: true } }
875filegroup { name: "opt-in-c", bazel_module: { bp2build_available: true } }
876`,
877
878 "package-opt-in/subpackage/Android.bp": `
879filegroup { name: "opt-in-d" } // parent package not configured to DefaultTrueRecursively
880`,
881
882 "package-opt-out/Android.bp": `
883filegroup { name: "opt-out-a" }
884filegroup { name: "opt-out-b", bazel_module: { bp2build_available: false } }
885filegroup { name: "opt-out-c", bazel_module: { bp2build_available: false } }
886`,
887
888 "package-opt-out/subpackage/Android.bp": `
889filegroup { name: "opt-out-g", bazel_module: { bp2build_available: false } }
890filegroup { name: "opt-out-h", bazel_module: { bp2build_available: false } }
891`,
892 },
893 },
894 }
895
896 dir := "."
897 for _, testCase := range testCases {
898 fs := make(map[string][]byte)
899 toParse := []string{
900 "Android.bp",
901 }
902 for f, content := range testCase.fs {
903 if strings.HasSuffix(f, "Android.bp") {
904 toParse = append(toParse, f)
905 }
906 fs[f] = []byte(content)
907 }
908 config := android.TestConfig(buildDir, nil, "", fs)
909 ctx := android.NewTestContext(config)
910 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
911 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
912 ctx.RegisterBp2BuildConfig(testCase.bp2buildConfig)
913 ctx.RegisterForBazelConversion()
914
915 _, errs := ctx.ParseFileList(dir, toParse)
916 android.FailIfErrored(t, errs)
917 _, errs = ctx.ResolveDependencies(config)
918 android.FailIfErrored(t, errs)
919
920 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
921
922 // For each directory, test that the expected number of generated targets is correct.
923 for dir, expectedCount := range testCase.expectedCount {
924 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
925 if actualCount := len(bazelTargets); actualCount != expectedCount {
926 t.Fatalf(
927 "%s: Expected %d bazel target for %s package, got %d",
928 testCase.description,
929 expectedCount,
930 dir,
931 actualCount)
932 }
933
934 }
935 }
936}
937
Liz Kammerba3ea162021-02-17 13:22:03 -0500938func TestCombineBuildFilesBp2buildTargets(t *testing.T) {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000939 testCases := []bp2buildTestCase{
Liz Kammerba3ea162021-02-17 13:22:03 -0500940 {
941 description: "filegroup bazel_module.label",
942 moduleTypeUnderTest: "filegroup",
943 moduleTypeUnderTestFactory: android.FileGroupFactory,
944 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000945 blueprint: `filegroup {
Liz Kammerba3ea162021-02-17 13:22:03 -0500946 name: "fg_foo",
947 bazel_module: { label: "//other:fg_foo" },
948}`,
949 expectedBazelTargets: []string{
950 `// BUILD file`,
951 },
Jingwen Chen5146ac02021-09-02 11:44:42 +0000952 filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -0500953 "other/BUILD.bazel": `// BUILD file`,
954 },
955 },
956 {
957 description: "multiple bazel_module.label same BUILD",
958 moduleTypeUnderTest: "filegroup",
959 moduleTypeUnderTestFactory: android.FileGroupFactory,
960 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000961 blueprint: `filegroup {
Jingwen Chenc63677b2021-06-17 05:43:19 +0000962 name: "fg_foo",
963 bazel_module: { label: "//other:fg_foo" },
964 }
Liz Kammerba3ea162021-02-17 13:22:03 -0500965
Jingwen Chenc63677b2021-06-17 05:43:19 +0000966 filegroup {
967 name: "foo",
968 bazel_module: { label: "//other:foo" },
969 }`,
Liz Kammerba3ea162021-02-17 13:22:03 -0500970 expectedBazelTargets: []string{
971 `// BUILD file`,
972 },
Jingwen Chen5146ac02021-09-02 11:44:42 +0000973 filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -0500974 "other/BUILD.bazel": `// BUILD file`,
975 },
976 },
977 {
Jingwen Chenc63677b2021-06-17 05:43:19 +0000978 description: "filegroup bazel_module.label and bp2build in subdir",
Liz Kammerba3ea162021-02-17 13:22:03 -0500979 moduleTypeUnderTest: "filegroup",
980 moduleTypeUnderTestFactory: android.FileGroupFactory,
981 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chenc63677b2021-06-17 05:43:19 +0000982 dir: "other",
Jingwen Chen5146ac02021-09-02 11:44:42 +0000983 blueprint: ``,
984 filesystem: map[string]string{
Jingwen Chenc63677b2021-06-17 05:43:19 +0000985 "other/Android.bp": `filegroup {
986 name: "fg_foo",
987 bazel_module: {
988 bp2build_available: true,
989 },
990 }
991 filegroup {
992 name: "fg_bar",
993 bazel_module: {
994 label: "//other:fg_bar"
995 },
996 }`,
997 "other/BUILD.bazel": `// definition for fg_bar`,
998 },
Liz Kammerba3ea162021-02-17 13:22:03 -0500999 expectedBazelTargets: []string{
1000 `filegroup(
1001 name = "fg_foo",
Jingwen Chenc63677b2021-06-17 05:43:19 +00001002)`, `// definition for fg_bar`,
Liz Kammerba3ea162021-02-17 13:22:03 -05001003 },
1004 },
1005 {
1006 description: "filegroup bazel_module.label and filegroup bp2build",
1007 moduleTypeUnderTest: "filegroup",
1008 moduleTypeUnderTestFactory: android.FileGroupFactory,
1009 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +00001010 blueprint: `filegroup {
Jingwen Chenc63677b2021-06-17 05:43:19 +00001011 name: "fg_foo",
1012 bazel_module: {
1013 label: "//other:fg_foo",
1014 },
1015 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001016
Jingwen Chenc63677b2021-06-17 05:43:19 +00001017 filegroup {
1018 name: "fg_bar",
1019 bazel_module: {
1020 bp2build_available: true,
1021 },
1022 }`,
Liz Kammerba3ea162021-02-17 13:22:03 -05001023 expectedBazelTargets: []string{
1024 `filegroup(
1025 name = "fg_bar",
1026)`,
1027 `// BUILD file`,
1028 },
Jingwen Chen5146ac02021-09-02 11:44:42 +00001029 filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -05001030 "other/BUILD.bazel": `// BUILD file`,
1031 },
1032 },
1033 }
1034
1035 dir := "."
1036 for _, testCase := range testCases {
Jingwen Chen49109762021-05-25 05:16:48 +00001037 t.Run(testCase.description, func(t *testing.T) {
1038 fs := make(map[string][]byte)
1039 toParse := []string{
1040 "Android.bp",
Liz Kammerba3ea162021-02-17 13:22:03 -05001041 }
Jingwen Chen5146ac02021-09-02 11:44:42 +00001042 for f, content := range testCase.filesystem {
Jingwen Chen49109762021-05-25 05:16:48 +00001043 if strings.HasSuffix(f, "Android.bp") {
1044 toParse = append(toParse, f)
1045 }
1046 fs[f] = []byte(content)
1047 }
Jingwen Chen5146ac02021-09-02 11:44:42 +00001048 config := android.TestConfig(buildDir, nil, testCase.blueprint, fs)
Jingwen Chen49109762021-05-25 05:16:48 +00001049 ctx := android.NewTestContext(config)
1050 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Jingwen Chen49109762021-05-25 05:16:48 +00001051 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1052 ctx.RegisterForBazelConversion()
Liz Kammerba3ea162021-02-17 13:22:03 -05001053
Jingwen Chen49109762021-05-25 05:16:48 +00001054 _, errs := ctx.ParseFileList(dir, toParse)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001055 if errored(t, testCase, errs) {
Jingwen Chen49109762021-05-25 05:16:48 +00001056 return
1057 }
1058 _, errs = ctx.ResolveDependencies(config)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001059 if errored(t, testCase, errs) {
Jingwen Chen49109762021-05-25 05:16:48 +00001060 return
1061 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001062
Jingwen Chen49109762021-05-25 05:16:48 +00001063 checkDir := dir
1064 if testCase.dir != "" {
1065 checkDir = testCase.dir
1066 }
1067 bazelTargets := generateBazelTargetsForDir(NewCodegenContext(config, *ctx.Context, Bp2Build), checkDir)
1068 bazelTargets.sort()
1069 actualCount := len(bazelTargets)
1070 expectedCount := len(testCase.expectedBazelTargets)
1071 if actualCount != expectedCount {
1072 t.Errorf("Expected %d bazel target, got %d\n%s", expectedCount, actualCount, bazelTargets)
1073 }
1074 if !strings.Contains(bazelTargets.String(), "Section: Handcrafted targets. ") {
1075 t.Errorf("Expected string representation of bazelTargets to contain handcrafted section header.")
1076 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001077 for i, target := range bazelTargets {
Jingwen Chen49109762021-05-25 05:16:48 +00001078 actualContent := target.content
1079 expectedContent := testCase.expectedBazelTargets[i]
1080 if expectedContent != actualContent {
Liz Kammerba3ea162021-02-17 13:22:03 -05001081 t.Errorf(
Jingwen Chen49109762021-05-25 05:16:48 +00001082 "Expected generated Bazel target to be '%s', got '%s'",
1083 expectedContent,
1084 actualContent,
Liz Kammerba3ea162021-02-17 13:22:03 -05001085 )
1086 }
1087 }
Jingwen Chen49109762021-05-25 05:16:48 +00001088 })
Liz Kammerba3ea162021-02-17 13:22:03 -05001089 }
1090}
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001091
1092func TestGlobExcludeSrcs(t *testing.T) {
Jingwen Chen5146ac02021-09-02 11:44:42 +00001093 testCases := []bp2buildTestCase{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001094 {
1095 description: "filegroup top level exclude_srcs",
1096 moduleTypeUnderTest: "filegroup",
1097 moduleTypeUnderTestFactory: android.FileGroupFactory,
1098 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +00001099 blueprint: `filegroup {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001100 name: "fg_foo",
1101 srcs: ["**/*.txt"],
1102 exclude_srcs: ["c.txt"],
1103 bazel_module: { bp2build_available: true },
1104}`,
1105 expectedBazelTargets: []string{`filegroup(
1106 name = "fg_foo",
1107 srcs = [
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001108 "a.txt",
1109 "b.txt",
Liz Kammer9abd62d2021-05-21 08:37:59 -04001110 "//dir:e.txt",
1111 "//dir:f.txt",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001112 ],
1113)`,
1114 },
Jingwen Chen5146ac02021-09-02 11:44:42 +00001115 filesystem: map[string]string{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001116 "a.txt": "",
1117 "b.txt": "",
1118 "c.txt": "",
1119 "dir/Android.bp": "",
1120 "dir/e.txt": "",
1121 "dir/f.txt": "",
1122 },
1123 },
1124 {
1125 description: "filegroup in subdir exclude_srcs",
1126 moduleTypeUnderTest: "filegroup",
1127 moduleTypeUnderTestFactory: android.FileGroupFactory,
1128 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +00001129 blueprint: "",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001130 dir: "dir",
Jingwen Chen5146ac02021-09-02 11:44:42 +00001131 filesystem: map[string]string{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001132 "dir/Android.bp": `filegroup {
1133 name: "fg_foo",
1134 srcs: ["**/*.txt"],
1135 exclude_srcs: ["b.txt"],
1136 bazel_module: { bp2build_available: true },
1137}
1138`,
1139 "dir/a.txt": "",
1140 "dir/b.txt": "",
1141 "dir/subdir/Android.bp": "",
1142 "dir/subdir/e.txt": "",
1143 "dir/subdir/f.txt": "",
1144 },
1145 expectedBazelTargets: []string{`filegroup(
1146 name = "fg_foo",
1147 srcs = [
Liz Kammer9abd62d2021-05-21 08:37:59 -04001148 "a.txt",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001149 "//dir/subdir:e.txt",
1150 "//dir/subdir:f.txt",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001151 ],
1152)`,
1153 },
1154 },
1155 }
1156
1157 dir := "."
1158 for _, testCase := range testCases {
1159 fs := make(map[string][]byte)
1160 toParse := []string{
1161 "Android.bp",
1162 }
Jingwen Chen5146ac02021-09-02 11:44:42 +00001163 for f, content := range testCase.filesystem {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001164 if strings.HasSuffix(f, "Android.bp") {
1165 toParse = append(toParse, f)
1166 }
1167 fs[f] = []byte(content)
1168 }
Jingwen Chen5146ac02021-09-02 11:44:42 +00001169 config := android.TestConfig(buildDir, nil, testCase.blueprint, fs)
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001170 ctx := android.NewTestContext(config)
1171 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
1172 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1173 ctx.RegisterForBazelConversion()
1174
1175 _, errs := ctx.ParseFileList(dir, toParse)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001176 if errored(t, testCase, errs) {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001177 continue
1178 }
1179 _, errs = ctx.ResolveDependencies(config)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001180 if errored(t, testCase, errs) {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001181 continue
1182 }
1183
1184 checkDir := dir
1185 if testCase.dir != "" {
1186 checkDir = testCase.dir
1187 }
1188 bazelTargets := generateBazelTargetsForDir(NewCodegenContext(config, *ctx.Context, Bp2Build), checkDir)
1189 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
1190 t.Errorf("%s: Expected %d bazel target, got %d\n%s", testCase.description, expectedCount, actualCount, bazelTargets)
1191 } else {
1192 for i, target := range bazelTargets {
1193 if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
1194 t.Errorf(
1195 "%s: Expected generated Bazel target to be '%s', got '%s'",
1196 testCase.description,
1197 w,
1198 g,
1199 )
1200 }
1201 }
1202 }
1203 }
1204}