blob: 55acab4f467befe17a778f891b04c28b954f1a3a [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"
Jingwen Chen316e07c2020-12-14 09:09:52 -050019 "android/soong/genrule"
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)
203 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
204 if actualCount, expectedCount := len(bazelTargets), 1; actualCount != expectedCount {
205 t.Fatalf("Expected %d bazel target, got %d", expectedCount, actualCount)
206 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800207
Liz Kammerd366c902021-06-03 13:43:01 -0400208 actualBazelTarget := bazelTargets[0]
209 if actualBazelTarget.content != testCase.expectedBazelTarget {
210 t.Errorf(
211 "Expected generated Bazel target to be '%s', got '%s'",
212 testCase.expectedBazelTarget,
213 actualBazelTarget.content,
214 )
215 }
216 })
Jingwen Chen73850672020-12-14 08:25:34 -0500217 }
218}
219
220func TestGenerateBazelTargetModules(t *testing.T) {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000221 testCases := []bp2buildTestCase{
Jingwen Chen73850672020-12-14 08:25:34 -0500222 {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000223 blueprint: `custom {
Jingwen Chen73850672020-12-14 08:25:34 -0500224 name: "foo",
225 string_list_prop: ["a", "b"],
226 string_prop: "a",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500227 bazel_module: { bp2build_available: true },
Jingwen Chen73850672020-12-14 08:25:34 -0500228}`,
Liz Kammer4562a3b2021-04-21 18:15:34 -0400229 expectedBazelTargets: []string{`custom(
Jingwen Chen73850672020-12-14 08:25:34 -0500230 name = "foo",
231 string_list_prop = [
232 "a",
233 "b",
234 ],
235 string_prop = "a",
236)`,
Liz Kammer4562a3b2021-04-21 18:15:34 -0400237 },
Jingwen Chen73850672020-12-14 08:25:34 -0500238 },
Jingwen Chen58a12b82021-03-30 13:08:36 +0000239 {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000240 blueprint: `custom {
Jingwen Chen58a12b82021-03-30 13:08:36 +0000241 name: "control_characters",
242 string_list_prop: ["\t", "\n"],
243 string_prop: "a\t\n\r",
244 bazel_module: { bp2build_available: true },
245}`,
Liz Kammer4562a3b2021-04-21 18:15:34 -0400246 expectedBazelTargets: []string{`custom(
Jingwen Chen58a12b82021-03-30 13:08:36 +0000247 name = "control_characters",
248 string_list_prop = [
249 "\t",
250 "\n",
251 ],
252 string_prop = "a\t\n\r",
253)`,
Liz Kammer4562a3b2021-04-21 18:15:34 -0400254 },
255 },
256 {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000257 blueprint: `custom {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400258 name: "has_dep",
259 arch_paths: [":dep"],
260 bazel_module: { bp2build_available: true },
261}
262
263custom {
264 name: "dep",
265 arch_paths: ["abc"],
266 bazel_module: { bp2build_available: true },
267}`,
268 expectedBazelTargets: []string{`custom(
269 name = "dep",
270 arch_paths = ["abc"],
271)`,
272 `custom(
273 name = "has_dep",
274 arch_paths = [":dep"],
275)`,
276 },
277 },
278 {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000279 blueprint: `custom {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400280 name: "arch_paths",
281 arch: {
282 x86: {
283 arch_paths: ["abc"],
284 },
285 },
286 bazel_module: { bp2build_available: true },
287}`,
288 expectedBazelTargets: []string{`custom(
289 name = "arch_paths",
290 arch_paths = select({
291 "//build/bazel/platforms/arch:x86": ["abc"],
292 "//conditions:default": [],
293 }),
294)`,
295 },
296 },
297 {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000298 blueprint: `custom {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400299 name: "has_dep",
300 arch: {
301 x86: {
302 arch_paths: [":dep"],
303 },
304 },
305 bazel_module: { bp2build_available: true },
306}
307
308custom {
309 name: "dep",
310 arch_paths: ["abc"],
311 bazel_module: { bp2build_available: true },
312}`,
313 expectedBazelTargets: []string{`custom(
314 name = "dep",
315 arch_paths = ["abc"],
316)`,
317 `custom(
318 name = "has_dep",
319 arch_paths = select({
320 "//build/bazel/platforms/arch:x86": [":dep"],
321 "//conditions:default": [],
322 }),
323)`,
324 },
Jingwen Chen58a12b82021-03-30 13:08:36 +0000325 },
Jingwen Chen73850672020-12-14 08:25:34 -0500326 }
327
328 dir := "."
329 for _, testCase := range testCases {
Jingwen Chen5146ac02021-09-02 11:44:42 +0000330 config := android.TestConfig(buildDir, nil, testCase.blueprint, nil)
Jingwen Chen73850672020-12-14 08:25:34 -0500331 ctx := android.NewTestContext(config)
Jingwen Chen164e0862021-02-19 00:48:40 -0500332
Liz Kammer32b77cf2021-08-04 15:17:02 -0400333 registerCustomModuleForBp2buildConversion(ctx)
Jingwen Chen73850672020-12-14 08:25:34 -0500334
335 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
Jingwen Chen5146ac02021-09-02 11:44:42 +0000336 if errored(t, testCase, errs) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500337 continue
338 }
Jingwen Chen73850672020-12-14 08:25:34 -0500339 _, errs = ctx.ResolveDependencies(config)
Jingwen Chen5146ac02021-09-02 11:44:42 +0000340 if errored(t, testCase, errs) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500341 continue
342 }
Jingwen Chen73850672020-12-14 08:25:34 -0500343
Jingwen Chen164e0862021-02-19 00:48:40 -0500344 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -0500345 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
Jingwen Chen164e0862021-02-19 00:48:40 -0500346
Liz Kammer4562a3b2021-04-21 18:15:34 -0400347 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
Liz Kammer356f7d42021-01-26 09:18:53 -0500348 t.Errorf("Expected %d bazel target, got %d", expectedCount, actualCount)
349 } else {
Liz Kammer4562a3b2021-04-21 18:15:34 -0400350 for i, expectedBazelTarget := range testCase.expectedBazelTargets {
351 actualBazelTarget := bazelTargets[i]
352 if actualBazelTarget.content != expectedBazelTarget {
353 t.Errorf(
354 "Expected generated Bazel target to be '%s', got '%s'",
355 expectedBazelTarget,
356 actualBazelTarget.content,
357 )
358 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500359 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800360 }
361 }
362}
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500363
Jingwen Chen40067de2021-01-26 21:58:43 -0500364func TestLoadStatements(t *testing.T) {
365 testCases := []struct {
366 bazelTargets BazelTargets
367 expectedLoadStatements string
368 }{
369 {
370 bazelTargets: BazelTargets{
371 BazelTarget{
372 name: "foo",
373 ruleClass: "cc_library",
374 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
375 },
376 },
377 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
378 },
379 {
380 bazelTargets: BazelTargets{
381 BazelTarget{
382 name: "foo",
383 ruleClass: "cc_library",
384 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
385 },
386 BazelTarget{
387 name: "bar",
388 ruleClass: "cc_library",
389 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
390 },
391 },
392 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`,
393 },
394 {
395 bazelTargets: BazelTargets{
396 BazelTarget{
397 name: "foo",
398 ruleClass: "cc_library",
399 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
400 },
401 BazelTarget{
402 name: "bar",
403 ruleClass: "cc_binary",
404 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
405 },
406 },
407 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")`,
408 },
409 {
410 bazelTargets: BazelTargets{
411 BazelTarget{
412 name: "foo",
413 ruleClass: "cc_library",
414 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
415 },
416 BazelTarget{
417 name: "bar",
418 ruleClass: "cc_binary",
419 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
420 },
421 BazelTarget{
422 name: "baz",
423 ruleClass: "java_binary",
424 bzlLoadLocation: "//build/bazel/rules:java.bzl",
425 },
426 },
427 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")
428load("//build/bazel/rules:java.bzl", "java_binary")`,
429 },
430 {
431 bazelTargets: BazelTargets{
432 BazelTarget{
433 name: "foo",
434 ruleClass: "cc_binary",
435 bzlLoadLocation: "//build/bazel/rules:cc.bzl",
436 },
437 BazelTarget{
438 name: "bar",
439 ruleClass: "java_binary",
440 bzlLoadLocation: "//build/bazel/rules:java.bzl",
441 },
442 BazelTarget{
443 name: "baz",
444 ruleClass: "genrule",
445 // Note: no bzlLoadLocation for native rules
446 },
447 },
448 expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary")
449load("//build/bazel/rules:java.bzl", "java_binary")`,
450 },
451 }
452
453 for _, testCase := range testCases {
454 actual := testCase.bazelTargets.LoadStatements()
455 expected := testCase.expectedLoadStatements
456 if actual != expected {
457 t.Fatalf("Expected load statements to be %s, got %s", expected, actual)
458 }
459 }
460
461}
462
463func TestGenerateBazelTargetModules_OneToMany_LoadedFromStarlark(t *testing.T) {
464 testCases := []struct {
465 bp string
466 expectedBazelTarget string
467 expectedBazelTargetCount int
468 expectedLoadStatements string
469 }{
470 {
471 bp: `custom {
472 name: "bar",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500473 bazel_module: { bp2build_available: true },
Jingwen Chen40067de2021-01-26 21:58:43 -0500474}`,
475 expectedBazelTarget: `my_library(
476 name = "bar",
477)
478
Jingwen Chen40067de2021-01-26 21:58:43 -0500479proto_library(
480 name = "bar_proto_library_deps",
Liz Kammer2ada09a2021-08-11 00:17:36 -0400481)
482
483my_proto_library(
484 name = "bar_my_proto_library_deps",
Jingwen Chen40067de2021-01-26 21:58:43 -0500485)`,
486 expectedBazelTargetCount: 3,
487 expectedLoadStatements: `load("//build/bazel/rules:proto.bzl", "my_proto_library", "proto_library")
488load("//build/bazel/rules:rules.bzl", "my_library")`,
489 },
490 }
491
492 dir := "."
493 for _, testCase := range testCases {
494 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
495 ctx := android.NewTestContext(config)
496 ctx.RegisterModuleType("custom", customModuleFactory)
Jingwen Chen12b4c272021-03-10 02:05:59 -0500497 ctx.RegisterBp2BuildMutator("custom", customBp2BuildMutatorFromStarlark)
Jingwen Chen40067de2021-01-26 21:58:43 -0500498 ctx.RegisterForBazelConversion()
499
500 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
501 android.FailIfErrored(t, errs)
502 _, errs = ctx.ResolveDependencies(config)
503 android.FailIfErrored(t, errs)
504
Jingwen Chen164e0862021-02-19 00:48:40 -0500505 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -0500506 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
Jingwen Chen40067de2021-01-26 21:58:43 -0500507 if actualCount := len(bazelTargets); actualCount != testCase.expectedBazelTargetCount {
508 t.Fatalf("Expected %d bazel target, got %d", testCase.expectedBazelTargetCount, actualCount)
509 }
510
511 actualBazelTargets := bazelTargets.String()
512 if actualBazelTargets != testCase.expectedBazelTarget {
513 t.Errorf(
514 "Expected generated Bazel target to be '%s', got '%s'",
515 testCase.expectedBazelTarget,
516 actualBazelTargets,
517 )
518 }
519
520 actualLoadStatements := bazelTargets.LoadStatements()
521 if actualLoadStatements != testCase.expectedLoadStatements {
522 t.Errorf(
523 "Expected generated load statements to be '%s', got '%s'",
524 testCase.expectedLoadStatements,
525 actualLoadStatements,
526 )
527 }
528 }
529}
530
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500531func TestModuleTypeBp2Build(t *testing.T) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500532 otherGenruleBp := map[string]string{
533 "other/Android.bp": `genrule {
534 name: "foo.tool",
535 out: ["foo_tool.out"],
536 srcs: ["foo_tool.in"],
537 cmd: "cp $(in) $(out)",
538}
539genrule {
540 name: "other.tool",
541 out: ["other_tool.out"],
542 srcs: ["other_tool.in"],
543 cmd: "cp $(in) $(out)",
544}`,
545 }
546
Jingwen Chen5146ac02021-09-02 11:44:42 +0000547 testCases := []bp2buildTestCase{
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500548 {
Liz Kammerebfcf672021-02-16 15:00:05 -0500549 description: "filegroup with does not specify srcs",
550 moduleTypeUnderTest: "filegroup",
551 moduleTypeUnderTestFactory: android.FileGroupFactory,
552 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000553 blueprint: `filegroup {
Liz Kammerebfcf672021-02-16 15:00:05 -0500554 name: "fg_foo",
555 bazel_module: { bp2build_available: true },
556}`,
557 expectedBazelTargets: []string{
558 `filegroup(
559 name = "fg_foo",
560)`,
561 },
562 },
563 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500564 description: "filegroup with no 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: [],
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{
574 `filegroup(
575 name = "fg_foo",
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500576)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500577 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500578 },
579 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500580 description: "filegroup with srcs",
581 moduleTypeUnderTest: "filegroup",
582 moduleTypeUnderTestFactory: android.FileGroupFactory,
583 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000584 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500585 name: "fg_foo",
586 srcs: ["a", "b"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500587 bazel_module: { bp2build_available: true },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500588}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500589 expectedBazelTargets: []string{`filegroup(
590 name = "fg_foo",
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500591 srcs = [
592 "a",
593 "b",
594 ],
595)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500596 },
597 },
598 {
599 description: "filegroup with excludes srcs",
600 moduleTypeUnderTest: "filegroup",
601 moduleTypeUnderTestFactory: android.FileGroupFactory,
602 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000603 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500604 name: "fg_foo",
605 srcs: ["a", "b"],
606 exclude_srcs: ["a"],
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 = "fg_foo",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000611 srcs = ["b"],
Liz Kammer356f7d42021-01-26 09:18:53 -0500612)`,
613 },
614 },
615 {
616 description: "filegroup with glob",
617 moduleTypeUnderTest: "filegroup",
618 moduleTypeUnderTestFactory: android.FileGroupFactory,
619 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000620 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500621 name: "foo",
622 srcs: ["**/*.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500623 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500624}`,
625 expectedBazelTargets: []string{`filegroup(
626 name = "foo",
627 srcs = [
628 "other/a.txt",
629 "other/b.txt",
630 "other/subdir/a.txt",
631 ],
632)`,
633 },
Jingwen Chen5146ac02021-09-02 11:44:42 +0000634 filesystem: map[string]string{
Liz Kammer356f7d42021-01-26 09:18:53 -0500635 "other/a.txt": "",
636 "other/b.txt": "",
637 "other/subdir/a.txt": "",
638 "other/file": "",
639 },
640 },
641 {
642 description: "filegroup with glob in subdir",
643 moduleTypeUnderTest: "filegroup",
644 moduleTypeUnderTestFactory: android.FileGroupFactory,
645 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000646 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500647 name: "foo",
648 srcs: ["a.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500649 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500650}`,
651 dir: "other",
652 expectedBazelTargets: []string{`filegroup(
653 name = "fg_foo",
654 srcs = [
655 "a.txt",
656 "b.txt",
657 "subdir/a.txt",
658 ],
659)`,
660 },
Jingwen Chen5146ac02021-09-02 11:44:42 +0000661 filesystem: map[string]string{
Liz Kammer356f7d42021-01-26 09:18:53 -0500662 "other/Android.bp": `filegroup {
663 name: "fg_foo",
664 srcs: ["**/*.txt"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500665 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500666}`,
667 "other/a.txt": "",
668 "other/b.txt": "",
669 "other/subdir/a.txt": "",
670 "other/file": "",
671 },
672 },
673 {
674 description: "depends_on_other_dir_module",
675 moduleTypeUnderTest: "filegroup",
676 moduleTypeUnderTestFactory: android.FileGroupFactory,
677 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000678 blueprint: `filegroup {
Liz Kammer356f7d42021-01-26 09:18:53 -0500679 name: "foobar",
680 srcs: [
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000681 ":foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500682 "c",
683 ],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500684 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500685}`,
686 expectedBazelTargets: []string{`filegroup(
687 name = "foobar",
688 srcs = [
689 "//other:foo",
690 "c",
691 ],
692)`,
693 },
Jingwen Chen5146ac02021-09-02 11:44:42 +0000694 filesystem: map[string]string{
Liz Kammer356f7d42021-01-26 09:18:53 -0500695 "other/Android.bp": `filegroup {
696 name: "foo",
697 srcs: ["a", "b"],
698}`,
699 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500700 },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500701 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500702 description: "genrule with command line variable replacements",
703 moduleTypeUnderTest: "genrule",
704 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
705 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000706 blueprint: `genrule {
Liz Kammer356f7d42021-01-26 09:18:53 -0500707 name: "foo.tool",
708 out: ["foo_tool.out"],
709 srcs: ["foo_tool.in"],
710 cmd: "cp $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500711 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500712}
713
714genrule {
Jingwen Chen316e07c2020-12-14 09:09:52 -0500715 name: "foo",
716 out: ["foo.out"],
717 srcs: ["foo.in"],
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500718 tools: [":foo.tool"],
719 cmd: "$(location :foo.tool) --genDir=$(genDir) arg $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500720 bazel_module: { bp2build_available: true },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500721}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500722 expectedBazelTargets: []string{
723 `genrule(
Jingwen Chen316e07c2020-12-14 09:09:52 -0500724 name = "foo",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500725 cmd = "$(location :foo.tool) --genDir=$(GENDIR) arg $(SRCS) $(OUTS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000726 outs = ["foo.out"],
727 srcs = ["foo.in"],
728 tools = [":foo.tool"],
Jingwen Chen316e07c2020-12-14 09:09:52 -0500729)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500730 `genrule(
731 name = "foo.tool",
732 cmd = "cp $(SRCS) $(OUTS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000733 outs = ["foo_tool.out"],
734 srcs = ["foo_tool.in"],
Liz Kammer356f7d42021-01-26 09:18:53 -0500735)`,
736 },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500737 },
738 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500739 description: "genrule using $(locations :label)",
740 moduleTypeUnderTest: "genrule",
741 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
742 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000743 blueprint: `genrule {
Liz Kammer356f7d42021-01-26 09:18:53 -0500744 name: "foo.tools",
745 out: ["foo_tool.out", "foo_tool2.out"],
746 srcs: ["foo_tool.in"],
747 cmd: "cp $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500748 bazel_module: { bp2build_available: true },
749}
Liz Kammer356f7d42021-01-26 09:18:53 -0500750
751genrule {
Jingwen Chen316e07c2020-12-14 09:09:52 -0500752 name: "foo",
753 out: ["foo.out"],
754 srcs: ["foo.in"],
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500755 tools: [":foo.tools"],
756 cmd: "$(locations :foo.tools) -s $(out) $(in)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500757 bazel_module: { bp2build_available: true },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500758}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500759 expectedBazelTargets: []string{`genrule(
Jingwen Chen316e07c2020-12-14 09:09:52 -0500760 name = "foo",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500761 cmd = "$(locations :foo.tools) -s $(OUTS) $(SRCS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000762 outs = ["foo.out"],
763 srcs = ["foo.in"],
764 tools = [":foo.tools"],
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500765)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500766 `genrule(
767 name = "foo.tools",
768 cmd = "cp $(SRCS) $(OUTS)",
769 outs = [
770 "foo_tool.out",
771 "foo_tool2.out",
772 ],
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000773 srcs = ["foo_tool.in"],
Liz Kammer356f7d42021-01-26 09:18:53 -0500774)`,
775 },
776 },
777 {
778 description: "genrule using $(locations //absolute:label)",
779 moduleTypeUnderTest: "genrule",
780 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
781 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000782 blueprint: `genrule {
Liz Kammer356f7d42021-01-26 09:18:53 -0500783 name: "foo",
784 out: ["foo.out"],
785 srcs: ["foo.in"],
786 tool_files: [":foo.tool"],
787 cmd: "$(locations :foo.tool) -s $(out) $(in)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500788 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500789}`,
790 expectedBazelTargets: []string{`genrule(
791 name = "foo",
792 cmd = "$(locations //other:foo.tool) -s $(OUTS) $(SRCS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000793 outs = ["foo.out"],
794 srcs = ["foo.in"],
795 tools = ["//other:foo.tool"],
Liz Kammer356f7d42021-01-26 09:18:53 -0500796)`,
797 },
Jingwen Chen5146ac02021-09-02 11:44:42 +0000798 filesystem: otherGenruleBp,
Liz Kammer356f7d42021-01-26 09:18:53 -0500799 },
800 {
801 description: "genrule srcs using $(locations //absolute:label)",
802 moduleTypeUnderTest: "genrule",
803 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
804 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000805 blueprint: `genrule {
Liz Kammer356f7d42021-01-26 09:18:53 -0500806 name: "foo",
807 out: ["foo.out"],
808 srcs: [":other.tool"],
809 tool_files: [":foo.tool"],
810 cmd: "$(locations :foo.tool) -s $(out) $(location :other.tool)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500811 bazel_module: { bp2build_available: true },
Liz Kammer356f7d42021-01-26 09:18:53 -0500812}`,
813 expectedBazelTargets: []string{`genrule(
814 name = "foo",
815 cmd = "$(locations //other:foo.tool) -s $(OUTS) $(location //other:other.tool)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000816 outs = ["foo.out"],
817 srcs = ["//other:other.tool"],
818 tools = ["//other:foo.tool"],
Liz Kammer356f7d42021-01-26 09:18:53 -0500819)`,
820 },
Jingwen Chen5146ac02021-09-02 11:44:42 +0000821 filesystem: otherGenruleBp,
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500822 },
823 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500824 description: "genrule using $(location) label should substitute first tool label automatically",
825 moduleTypeUnderTest: "genrule",
826 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
827 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000828 blueprint: `genrule {
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500829 name: "foo",
830 out: ["foo.out"],
831 srcs: ["foo.in"],
832 tool_files: [":foo.tool", ":other.tool"],
833 cmd: "$(location) -s $(out) $(in)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500834 bazel_module: { bp2build_available: true },
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500835}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500836 expectedBazelTargets: []string{`genrule(
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500837 name = "foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500838 cmd = "$(location //other:foo.tool) -s $(OUTS) $(SRCS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000839 outs = ["foo.out"],
840 srcs = ["foo.in"],
Jingwen Chen316e07c2020-12-14 09:09:52 -0500841 tools = [
Liz Kammer356f7d42021-01-26 09:18:53 -0500842 "//other:foo.tool",
843 "//other:other.tool",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500844 ],
845)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500846 },
Jingwen Chen5146ac02021-09-02 11:44:42 +0000847 filesystem: otherGenruleBp,
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500848 },
849 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500850 description: "genrule using $(locations) label should substitute first tool label automatically",
851 moduleTypeUnderTest: "genrule",
852 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
853 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000854 blueprint: `genrule {
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500855 name: "foo",
856 out: ["foo.out"],
857 srcs: ["foo.in"],
858 tools: [":foo.tool", ":other.tool"],
859 cmd: "$(locations) -s $(out) $(in)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500860 bazel_module: { bp2build_available: true },
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500861}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500862 expectedBazelTargets: []string{`genrule(
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500863 name = "foo",
Liz Kammer356f7d42021-01-26 09:18:53 -0500864 cmd = "$(locations //other:foo.tool) -s $(OUTS) $(SRCS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000865 outs = ["foo.out"],
866 srcs = ["foo.in"],
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500867 tools = [
Liz Kammer356f7d42021-01-26 09:18:53 -0500868 "//other:foo.tool",
869 "//other:other.tool",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500870 ],
871)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500872 },
Jingwen Chen5146ac02021-09-02 11:44:42 +0000873 filesystem: otherGenruleBp,
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500874 },
875 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500876 description: "genrule without tools or tool_files can convert successfully",
877 moduleTypeUnderTest: "genrule",
878 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
879 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +0000880 blueprint: `genrule {
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500881 name: "foo",
882 out: ["foo.out"],
883 srcs: ["foo.in"],
884 cmd: "cp $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500885 bazel_module: { bp2build_available: true },
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500886}`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500887 expectedBazelTargets: []string{`genrule(
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500888 name = "foo",
889 cmd = "cp $(SRCS) $(OUTS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000890 outs = ["foo.out"],
891 srcs = ["foo.in"],
Jingwen Chen316e07c2020-12-14 09:09:52 -0500892)`,
Liz Kammer356f7d42021-01-26 09:18:53 -0500893 },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500894 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500895 }
896
897 dir := "."
898 for _, testCase := range testCases {
Liz Kammer356f7d42021-01-26 09:18:53 -0500899 fs := make(map[string][]byte)
900 toParse := []string{
901 "Android.bp",
902 }
Jingwen Chen5146ac02021-09-02 11:44:42 +0000903 for f, content := range testCase.filesystem {
Liz Kammer356f7d42021-01-26 09:18:53 -0500904 if strings.HasSuffix(f, "Android.bp") {
905 toParse = append(toParse, f)
906 }
907 fs[f] = []byte(content)
908 }
Jingwen Chen5146ac02021-09-02 11:44:42 +0000909 config := android.TestConfig(buildDir, nil, testCase.blueprint, fs)
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500910 ctx := android.NewTestContext(config)
911 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Jingwen Chena42d6412021-01-26 21:57:27 -0500912 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500913 ctx.RegisterForBazelConversion()
914
Liz Kammer356f7d42021-01-26 09:18:53 -0500915 _, errs := ctx.ParseFileList(dir, toParse)
Jingwen Chen5146ac02021-09-02 11:44:42 +0000916 if errored(t, testCase, errs) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500917 continue
918 }
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500919 _, errs = ctx.ResolveDependencies(config)
Jingwen Chen5146ac02021-09-02 11:44:42 +0000920 if errored(t, testCase, errs) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500921 continue
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500922 }
923
Liz Kammer356f7d42021-01-26 09:18:53 -0500924 checkDir := dir
925 if testCase.dir != "" {
926 checkDir = testCase.dir
927 }
Jingwen Chen164e0862021-02-19 00:48:40 -0500928
929 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -0500930 bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir)
Liz Kammer356f7d42021-01-26 09:18:53 -0500931 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
932 t.Errorf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount)
933 } else {
934 for i, target := range bazelTargets {
935 if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
936 t.Errorf(
937 "%s: Expected generated Bazel target to be '%s', got '%s'",
938 testCase.description,
939 w,
940 g,
941 )
942 }
943 }
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500944 }
945 }
946}
Jingwen Chen041b1842021-02-01 00:23:25 -0500947
948type bp2buildMutator = func(android.TopDownMutatorContext)
949
950func TestBp2BuildInlinesDefaults(t *testing.T) {
951 testCases := []struct {
952 moduleTypesUnderTest map[string]android.ModuleFactory
953 bp2buildMutatorsUnderTest map[string]bp2buildMutator
954 bp string
955 expectedBazelTarget string
956 description string
957 }{
958 {
959 moduleTypesUnderTest: map[string]android.ModuleFactory{
960 "genrule": genrule.GenRuleFactory,
961 "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
962 },
963 bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
964 "genrule": genrule.GenruleBp2Build,
965 },
966 bp: `genrule_defaults {
967 name: "gen_defaults",
968 cmd: "do-something $(in) $(out)",
969}
970genrule {
971 name: "gen",
972 out: ["out"],
973 srcs: ["in1"],
974 defaults: ["gen_defaults"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -0500975 bazel_module: { bp2build_available: true },
Jingwen Chen041b1842021-02-01 00:23:25 -0500976}
977`,
978 expectedBazelTarget: `genrule(
979 name = "gen",
980 cmd = "do-something $(SRCS) $(OUTS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000981 outs = ["out"],
982 srcs = ["in1"],
Jingwen Chen041b1842021-02-01 00:23:25 -0500983)`,
984 description: "genrule applies properties from a genrule_defaults dependency if not specified",
985 },
986 {
987 moduleTypesUnderTest: map[string]android.ModuleFactory{
988 "genrule": genrule.GenRuleFactory,
989 "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
990 },
991 bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
992 "genrule": genrule.GenruleBp2Build,
993 },
994 bp: `genrule_defaults {
995 name: "gen_defaults",
996 out: ["out-from-defaults"],
997 srcs: ["in-from-defaults"],
998 cmd: "cmd-from-defaults",
999}
1000genrule {
1001 name: "gen",
1002 out: ["out"],
1003 srcs: ["in1"],
1004 defaults: ["gen_defaults"],
1005 cmd: "do-something $(in) $(out)",
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001006 bazel_module: { bp2build_available: true },
Jingwen Chen041b1842021-02-01 00:23:25 -05001007}
1008`,
1009 expectedBazelTarget: `genrule(
1010 name = "gen",
1011 cmd = "do-something $(SRCS) $(OUTS)",
1012 outs = [
1013 "out-from-defaults",
1014 "out",
1015 ],
1016 srcs = [
1017 "in-from-defaults",
1018 "in1",
1019 ],
1020)`,
1021 description: "genrule does merges properties from a genrule_defaults dependency, latest-first",
1022 },
1023 {
1024 moduleTypesUnderTest: map[string]android.ModuleFactory{
1025 "genrule": genrule.GenRuleFactory,
1026 "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
1027 },
1028 bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
1029 "genrule": genrule.GenruleBp2Build,
1030 },
1031 bp: `genrule_defaults {
1032 name: "gen_defaults1",
1033 cmd: "cp $(in) $(out)",
1034}
1035
1036genrule_defaults {
1037 name: "gen_defaults2",
1038 srcs: ["in1"],
1039}
1040
1041genrule {
1042 name: "gen",
1043 out: ["out"],
1044 defaults: ["gen_defaults1", "gen_defaults2"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001045 bazel_module: { bp2build_available: true },
Jingwen Chen041b1842021-02-01 00:23:25 -05001046}
1047`,
1048 expectedBazelTarget: `genrule(
1049 name = "gen",
1050 cmd = "cp $(SRCS) $(OUTS)",
Jingwen Chenb4628eb2021-04-08 14:40:57 +00001051 outs = ["out"],
1052 srcs = ["in1"],
Jingwen Chen041b1842021-02-01 00:23:25 -05001053)`,
1054 description: "genrule applies properties from list of genrule_defaults",
1055 },
1056 {
1057 moduleTypesUnderTest: map[string]android.ModuleFactory{
1058 "genrule": genrule.GenRuleFactory,
1059 "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() },
1060 },
1061 bp2buildMutatorsUnderTest: map[string]bp2buildMutator{
1062 "genrule": genrule.GenruleBp2Build,
1063 },
1064 bp: `genrule_defaults {
1065 name: "gen_defaults1",
1066 defaults: ["gen_defaults2"],
1067 cmd: "cmd1 $(in) $(out)", // overrides gen_defaults2's cmd property value.
1068}
1069
1070genrule_defaults {
1071 name: "gen_defaults2",
1072 defaults: ["gen_defaults3"],
1073 cmd: "cmd2 $(in) $(out)",
1074 out: ["out-from-2"],
1075 srcs: ["in1"],
1076}
1077
1078genrule_defaults {
1079 name: "gen_defaults3",
1080 out: ["out-from-3"],
1081 srcs: ["srcs-from-3"],
1082}
1083
1084genrule {
1085 name: "gen",
1086 out: ["out"],
1087 defaults: ["gen_defaults1"],
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001088 bazel_module: { bp2build_available: true },
Jingwen Chen041b1842021-02-01 00:23:25 -05001089}
1090`,
1091 expectedBazelTarget: `genrule(
1092 name = "gen",
1093 cmd = "cmd1 $(SRCS) $(OUTS)",
1094 outs = [
1095 "out-from-3",
1096 "out-from-2",
1097 "out",
1098 ],
1099 srcs = [
Jingwen Chen07027912021-03-15 06:02:43 -04001100 "srcs-from-3",
Liz Kammer9abd62d2021-05-21 08:37:59 -04001101 "in1",
Jingwen Chen041b1842021-02-01 00:23:25 -05001102 ],
1103)`,
1104 description: "genrule applies properties from genrule_defaults transitively",
1105 },
1106 }
1107
1108 dir := "."
1109 for _, testCase := range testCases {
1110 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
1111 ctx := android.NewTestContext(config)
1112 for m, factory := range testCase.moduleTypesUnderTest {
1113 ctx.RegisterModuleType(m, factory)
1114 }
1115 for mutator, f := range testCase.bp2buildMutatorsUnderTest {
1116 ctx.RegisterBp2BuildMutator(mutator, f)
1117 }
1118 ctx.RegisterForBazelConversion()
1119
1120 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
1121 android.FailIfErrored(t, errs)
1122 _, errs = ctx.ResolveDependencies(config)
1123 android.FailIfErrored(t, errs)
1124
Jingwen Chen164e0862021-02-19 00:48:40 -05001125 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -05001126 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
Jingwen Chen041b1842021-02-01 00:23:25 -05001127 if actualCount := len(bazelTargets); actualCount != 1 {
1128 t.Fatalf("%s: Expected 1 bazel target, got %d", testCase.description, actualCount)
1129 }
1130
1131 actualBazelTarget := bazelTargets[0]
1132 if actualBazelTarget.content != testCase.expectedBazelTarget {
1133 t.Errorf(
1134 "%s: Expected generated Bazel target to be '%s', got '%s'",
1135 testCase.description,
1136 testCase.expectedBazelTarget,
1137 actualBazelTarget.content,
1138 )
1139 }
1140 }
1141}
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001142
Jingwen Chen12b4c272021-03-10 02:05:59 -05001143func TestAllowlistingBp2buildTargetsExplicitly(t *testing.T) {
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001144 testCases := []struct {
1145 moduleTypeUnderTest string
1146 moduleTypeUnderTestFactory android.ModuleFactory
1147 moduleTypeUnderTestBp2BuildMutator bp2buildMutator
1148 bp string
1149 expectedCount int
1150 description string
1151 }{
1152 {
1153 description: "explicitly unavailable",
1154 moduleTypeUnderTest: "filegroup",
1155 moduleTypeUnderTestFactory: android.FileGroupFactory,
1156 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1157 bp: `filegroup {
1158 name: "foo",
1159 srcs: ["a", "b"],
1160 bazel_module: { bp2build_available: false },
1161}`,
1162 expectedCount: 0,
1163 },
1164 {
1165 description: "implicitly unavailable",
1166 moduleTypeUnderTest: "filegroup",
1167 moduleTypeUnderTestFactory: android.FileGroupFactory,
1168 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1169 bp: `filegroup {
1170 name: "foo",
1171 srcs: ["a", "b"],
1172}`,
1173 expectedCount: 0,
1174 },
1175 {
1176 description: "explicitly available",
1177 moduleTypeUnderTest: "filegroup",
1178 moduleTypeUnderTestFactory: android.FileGroupFactory,
1179 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1180 bp: `filegroup {
1181 name: "foo",
1182 srcs: ["a", "b"],
1183 bazel_module: { bp2build_available: true },
1184}`,
1185 expectedCount: 1,
1186 },
1187 {
1188 description: "generates more than 1 target if needed",
1189 moduleTypeUnderTest: "custom",
1190 moduleTypeUnderTestFactory: customModuleFactory,
1191 moduleTypeUnderTestBp2BuildMutator: customBp2BuildMutatorFromStarlark,
1192 bp: `custom {
1193 name: "foo",
1194 bazel_module: { bp2build_available: true },
1195}`,
1196 expectedCount: 3,
1197 },
1198 }
1199
1200 dir := "."
1201 for _, testCase := range testCases {
Liz Kammer2ada09a2021-08-11 00:17:36 -04001202 t.Run(testCase.description, func(t *testing.T) {
1203 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
1204 ctx := android.NewTestContext(config)
1205 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
1206 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1207 ctx.RegisterForBazelConversion()
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001208
Liz Kammer2ada09a2021-08-11 00:17:36 -04001209 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
1210 android.FailIfErrored(t, errs)
1211 _, errs = ctx.ResolveDependencies(config)
1212 android.FailIfErrored(t, errs)
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001213
Liz Kammer2ada09a2021-08-11 00:17:36 -04001214 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
1215 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
1216 if actualCount := len(bazelTargets); actualCount != testCase.expectedCount {
1217 t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, testCase.expectedCount, actualCount)
1218 }
1219 })
Jingwen Chen77e8b7b2021-02-05 03:03:24 -05001220 }
1221}
Liz Kammerba3ea162021-02-17 13:22:03 -05001222
Jingwen Chen12b4c272021-03-10 02:05:59 -05001223func TestAllowlistingBp2buildTargetsWithConfig(t *testing.T) {
1224 testCases := []struct {
1225 moduleTypeUnderTest string
1226 moduleTypeUnderTestFactory android.ModuleFactory
1227 moduleTypeUnderTestBp2BuildMutator bp2buildMutator
1228 expectedCount map[string]int
1229 description string
1230 bp2buildConfig android.Bp2BuildConfig
1231 checkDir string
1232 fs map[string]string
1233 }{
1234 {
1235 description: "test bp2build config package and subpackages config",
1236 moduleTypeUnderTest: "filegroup",
1237 moduleTypeUnderTestFactory: android.FileGroupFactory,
1238 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1239 expectedCount: map[string]int{
1240 "migrated": 1,
1241 "migrated/but_not_really": 0,
1242 "migrated/but_not_really/but_really": 1,
1243 "not_migrated": 0,
1244 "also_not_migrated": 0,
1245 },
1246 bp2buildConfig: android.Bp2BuildConfig{
1247 "migrated": android.Bp2BuildDefaultTrueRecursively,
1248 "migrated/but_not_really": android.Bp2BuildDefaultFalse,
1249 "not_migrated": android.Bp2BuildDefaultFalse,
1250 },
1251 fs: map[string]string{
1252 "migrated/Android.bp": `filegroup { name: "a" }`,
1253 "migrated/but_not_really/Android.bp": `filegroup { name: "b" }`,
1254 "migrated/but_not_really/but_really/Android.bp": `filegroup { name: "c" }`,
1255 "not_migrated/Android.bp": `filegroup { name: "d" }`,
1256 "also_not_migrated/Android.bp": `filegroup { name: "e" }`,
1257 },
1258 },
1259 {
1260 description: "test bp2build config opt-in and opt-out",
1261 moduleTypeUnderTest: "filegroup",
1262 moduleTypeUnderTestFactory: android.FileGroupFactory,
1263 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
1264 expectedCount: map[string]int{
1265 "package-opt-in": 2,
1266 "package-opt-in/subpackage": 0,
1267 "package-opt-out": 1,
1268 "package-opt-out/subpackage": 0,
1269 },
1270 bp2buildConfig: android.Bp2BuildConfig{
1271 "package-opt-in": android.Bp2BuildDefaultFalse,
1272 "package-opt-out": android.Bp2BuildDefaultTrueRecursively,
1273 },
1274 fs: map[string]string{
1275 "package-opt-in/Android.bp": `
1276filegroup { name: "opt-in-a" }
1277filegroup { name: "opt-in-b", bazel_module: { bp2build_available: true } }
1278filegroup { name: "opt-in-c", bazel_module: { bp2build_available: true } }
1279`,
1280
1281 "package-opt-in/subpackage/Android.bp": `
1282filegroup { name: "opt-in-d" } // parent package not configured to DefaultTrueRecursively
1283`,
1284
1285 "package-opt-out/Android.bp": `
1286filegroup { name: "opt-out-a" }
1287filegroup { name: "opt-out-b", bazel_module: { bp2build_available: false } }
1288filegroup { name: "opt-out-c", bazel_module: { bp2build_available: false } }
1289`,
1290
1291 "package-opt-out/subpackage/Android.bp": `
1292filegroup { name: "opt-out-g", bazel_module: { bp2build_available: false } }
1293filegroup { name: "opt-out-h", bazel_module: { bp2build_available: false } }
1294`,
1295 },
1296 },
1297 }
1298
1299 dir := "."
1300 for _, testCase := range testCases {
1301 fs := make(map[string][]byte)
1302 toParse := []string{
1303 "Android.bp",
1304 }
1305 for f, content := range testCase.fs {
1306 if strings.HasSuffix(f, "Android.bp") {
1307 toParse = append(toParse, f)
1308 }
1309 fs[f] = []byte(content)
1310 }
1311 config := android.TestConfig(buildDir, nil, "", fs)
1312 ctx := android.NewTestContext(config)
1313 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
1314 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1315 ctx.RegisterBp2BuildConfig(testCase.bp2buildConfig)
1316 ctx.RegisterForBazelConversion()
1317
1318 _, errs := ctx.ParseFileList(dir, toParse)
1319 android.FailIfErrored(t, errs)
1320 _, errs = ctx.ResolveDependencies(config)
1321 android.FailIfErrored(t, errs)
1322
1323 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
1324
1325 // For each directory, test that the expected number of generated targets is correct.
1326 for dir, expectedCount := range testCase.expectedCount {
1327 bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
1328 if actualCount := len(bazelTargets); actualCount != expectedCount {
1329 t.Fatalf(
1330 "%s: Expected %d bazel target for %s package, got %d",
1331 testCase.description,
1332 expectedCount,
1333 dir,
1334 actualCount)
1335 }
1336
1337 }
1338 }
1339}
1340
Liz Kammerba3ea162021-02-17 13:22:03 -05001341func TestCombineBuildFilesBp2buildTargets(t *testing.T) {
Jingwen Chen5146ac02021-09-02 11:44:42 +00001342 testCases := []bp2buildTestCase{
Liz Kammerba3ea162021-02-17 13:22:03 -05001343 {
1344 description: "filegroup bazel_module.label",
1345 moduleTypeUnderTest: "filegroup",
1346 moduleTypeUnderTestFactory: android.FileGroupFactory,
1347 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +00001348 blueprint: `filegroup {
Liz Kammerba3ea162021-02-17 13:22:03 -05001349 name: "fg_foo",
1350 bazel_module: { label: "//other:fg_foo" },
1351}`,
1352 expectedBazelTargets: []string{
1353 `// BUILD file`,
1354 },
Jingwen Chen5146ac02021-09-02 11:44:42 +00001355 filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -05001356 "other/BUILD.bazel": `// BUILD file`,
1357 },
1358 },
1359 {
1360 description: "multiple bazel_module.label same BUILD",
1361 moduleTypeUnderTest: "filegroup",
1362 moduleTypeUnderTestFactory: android.FileGroupFactory,
1363 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +00001364 blueprint: `filegroup {
Jingwen Chenc63677b2021-06-17 05:43:19 +00001365 name: "fg_foo",
1366 bazel_module: { label: "//other:fg_foo" },
1367 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001368
Jingwen Chenc63677b2021-06-17 05:43:19 +00001369 filegroup {
1370 name: "foo",
1371 bazel_module: { label: "//other:foo" },
1372 }`,
Liz Kammerba3ea162021-02-17 13:22:03 -05001373 expectedBazelTargets: []string{
1374 `// BUILD file`,
1375 },
Jingwen Chen5146ac02021-09-02 11:44:42 +00001376 filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -05001377 "other/BUILD.bazel": `// BUILD file`,
1378 },
1379 },
1380 {
Jingwen Chenc63677b2021-06-17 05:43:19 +00001381 description: "filegroup bazel_module.label and bp2build in subdir",
Liz Kammerba3ea162021-02-17 13:22:03 -05001382 moduleTypeUnderTest: "filegroup",
1383 moduleTypeUnderTestFactory: android.FileGroupFactory,
1384 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chenc63677b2021-06-17 05:43:19 +00001385 dir: "other",
Jingwen Chen5146ac02021-09-02 11:44:42 +00001386 blueprint: ``,
1387 filesystem: map[string]string{
Jingwen Chenc63677b2021-06-17 05:43:19 +00001388 "other/Android.bp": `filegroup {
1389 name: "fg_foo",
1390 bazel_module: {
1391 bp2build_available: true,
1392 },
1393 }
1394 filegroup {
1395 name: "fg_bar",
1396 bazel_module: {
1397 label: "//other:fg_bar"
1398 },
1399 }`,
1400 "other/BUILD.bazel": `// definition for fg_bar`,
1401 },
Liz Kammerba3ea162021-02-17 13:22:03 -05001402 expectedBazelTargets: []string{
1403 `filegroup(
1404 name = "fg_foo",
Jingwen Chenc63677b2021-06-17 05:43:19 +00001405)`, `// definition for fg_bar`,
Liz Kammerba3ea162021-02-17 13:22:03 -05001406 },
1407 },
1408 {
1409 description: "filegroup bazel_module.label and filegroup bp2build",
1410 moduleTypeUnderTest: "filegroup",
1411 moduleTypeUnderTestFactory: android.FileGroupFactory,
1412 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +00001413 blueprint: `filegroup {
Jingwen Chenc63677b2021-06-17 05:43:19 +00001414 name: "fg_foo",
1415 bazel_module: {
1416 label: "//other:fg_foo",
1417 },
1418 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001419
Jingwen Chenc63677b2021-06-17 05:43:19 +00001420 filegroup {
1421 name: "fg_bar",
1422 bazel_module: {
1423 bp2build_available: true,
1424 },
1425 }`,
Liz Kammerba3ea162021-02-17 13:22:03 -05001426 expectedBazelTargets: []string{
1427 `filegroup(
1428 name = "fg_bar",
1429)`,
1430 `// BUILD file`,
1431 },
Jingwen Chen5146ac02021-09-02 11:44:42 +00001432 filesystem: map[string]string{
Liz Kammerba3ea162021-02-17 13:22:03 -05001433 "other/BUILD.bazel": `// BUILD file`,
1434 },
1435 },
1436 }
1437
1438 dir := "."
1439 for _, testCase := range testCases {
Jingwen Chen49109762021-05-25 05:16:48 +00001440 t.Run(testCase.description, func(t *testing.T) {
1441 fs := make(map[string][]byte)
1442 toParse := []string{
1443 "Android.bp",
Liz Kammerba3ea162021-02-17 13:22:03 -05001444 }
Jingwen Chen5146ac02021-09-02 11:44:42 +00001445 for f, content := range testCase.filesystem {
Jingwen Chen49109762021-05-25 05:16:48 +00001446 if strings.HasSuffix(f, "Android.bp") {
1447 toParse = append(toParse, f)
1448 }
1449 fs[f] = []byte(content)
1450 }
Jingwen Chen5146ac02021-09-02 11:44:42 +00001451 config := android.TestConfig(buildDir, nil, testCase.blueprint, fs)
Jingwen Chen49109762021-05-25 05:16:48 +00001452 ctx := android.NewTestContext(config)
1453 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Jingwen Chen49109762021-05-25 05:16:48 +00001454 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1455 ctx.RegisterForBazelConversion()
Liz Kammerba3ea162021-02-17 13:22:03 -05001456
Jingwen Chen49109762021-05-25 05:16:48 +00001457 _, errs := ctx.ParseFileList(dir, toParse)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001458 if errored(t, testCase, errs) {
Jingwen Chen49109762021-05-25 05:16:48 +00001459 return
1460 }
1461 _, errs = ctx.ResolveDependencies(config)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001462 if errored(t, testCase, errs) {
Jingwen Chen49109762021-05-25 05:16:48 +00001463 return
1464 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001465
Jingwen Chen49109762021-05-25 05:16:48 +00001466 checkDir := dir
1467 if testCase.dir != "" {
1468 checkDir = testCase.dir
1469 }
1470 bazelTargets := generateBazelTargetsForDir(NewCodegenContext(config, *ctx.Context, Bp2Build), checkDir)
1471 bazelTargets.sort()
1472 actualCount := len(bazelTargets)
1473 expectedCount := len(testCase.expectedBazelTargets)
1474 if actualCount != expectedCount {
1475 t.Errorf("Expected %d bazel target, got %d\n%s", expectedCount, actualCount, bazelTargets)
1476 }
1477 if !strings.Contains(bazelTargets.String(), "Section: Handcrafted targets. ") {
1478 t.Errorf("Expected string representation of bazelTargets to contain handcrafted section header.")
1479 }
Liz Kammerba3ea162021-02-17 13:22:03 -05001480 for i, target := range bazelTargets {
Jingwen Chen49109762021-05-25 05:16:48 +00001481 actualContent := target.content
1482 expectedContent := testCase.expectedBazelTargets[i]
1483 if expectedContent != actualContent {
Liz Kammerba3ea162021-02-17 13:22:03 -05001484 t.Errorf(
Jingwen Chen49109762021-05-25 05:16:48 +00001485 "Expected generated Bazel target to be '%s', got '%s'",
1486 expectedContent,
1487 actualContent,
Liz Kammerba3ea162021-02-17 13:22:03 -05001488 )
1489 }
1490 }
Jingwen Chen49109762021-05-25 05:16:48 +00001491 })
Liz Kammerba3ea162021-02-17 13:22:03 -05001492 }
1493}
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001494
1495func TestGlobExcludeSrcs(t *testing.T) {
Jingwen Chen5146ac02021-09-02 11:44:42 +00001496 testCases := []bp2buildTestCase{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001497 {
1498 description: "filegroup top level exclude_srcs",
1499 moduleTypeUnderTest: "filegroup",
1500 moduleTypeUnderTestFactory: android.FileGroupFactory,
1501 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +00001502 blueprint: `filegroup {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001503 name: "fg_foo",
1504 srcs: ["**/*.txt"],
1505 exclude_srcs: ["c.txt"],
1506 bazel_module: { bp2build_available: true },
1507}`,
1508 expectedBazelTargets: []string{`filegroup(
1509 name = "fg_foo",
1510 srcs = [
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001511 "a.txt",
1512 "b.txt",
Liz Kammer9abd62d2021-05-21 08:37:59 -04001513 "//dir:e.txt",
1514 "//dir:f.txt",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001515 ],
1516)`,
1517 },
Jingwen Chen5146ac02021-09-02 11:44:42 +00001518 filesystem: map[string]string{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001519 "a.txt": "",
1520 "b.txt": "",
1521 "c.txt": "",
1522 "dir/Android.bp": "",
1523 "dir/e.txt": "",
1524 "dir/f.txt": "",
1525 },
1526 },
1527 {
1528 description: "filegroup in subdir exclude_srcs",
1529 moduleTypeUnderTest: "filegroup",
1530 moduleTypeUnderTestFactory: android.FileGroupFactory,
1531 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen5146ac02021-09-02 11:44:42 +00001532 blueprint: "",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001533 dir: "dir",
Jingwen Chen5146ac02021-09-02 11:44:42 +00001534 filesystem: map[string]string{
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001535 "dir/Android.bp": `filegroup {
1536 name: "fg_foo",
1537 srcs: ["**/*.txt"],
1538 exclude_srcs: ["b.txt"],
1539 bazel_module: { bp2build_available: true },
1540}
1541`,
1542 "dir/a.txt": "",
1543 "dir/b.txt": "",
1544 "dir/subdir/Android.bp": "",
1545 "dir/subdir/e.txt": "",
1546 "dir/subdir/f.txt": "",
1547 },
1548 expectedBazelTargets: []string{`filegroup(
1549 name = "fg_foo",
1550 srcs = [
Liz Kammer9abd62d2021-05-21 08:37:59 -04001551 "a.txt",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001552 "//dir/subdir:e.txt",
1553 "//dir/subdir:f.txt",
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001554 ],
1555)`,
1556 },
1557 },
1558 }
1559
1560 dir := "."
1561 for _, testCase := range testCases {
1562 fs := make(map[string][]byte)
1563 toParse := []string{
1564 "Android.bp",
1565 }
Jingwen Chen5146ac02021-09-02 11:44:42 +00001566 for f, content := range testCase.filesystem {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001567 if strings.HasSuffix(f, "Android.bp") {
1568 toParse = append(toParse, f)
1569 }
1570 fs[f] = []byte(content)
1571 }
Jingwen Chen5146ac02021-09-02 11:44:42 +00001572 config := android.TestConfig(buildDir, nil, testCase.blueprint, fs)
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001573 ctx := android.NewTestContext(config)
1574 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
1575 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
1576 ctx.RegisterForBazelConversion()
1577
1578 _, errs := ctx.ParseFileList(dir, toParse)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001579 if errored(t, testCase, errs) {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001580 continue
1581 }
1582 _, errs = ctx.ResolveDependencies(config)
Jingwen Chen5146ac02021-09-02 11:44:42 +00001583 if errored(t, testCase, errs) {
Jingwen Chen4ecc67d2021-04-27 09:47:02 +00001584 continue
1585 }
1586
1587 checkDir := dir
1588 if testCase.dir != "" {
1589 checkDir = testCase.dir
1590 }
1591 bazelTargets := generateBazelTargetsForDir(NewCodegenContext(config, *ctx.Context, Bp2Build), checkDir)
1592 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
1593 t.Errorf("%s: Expected %d bazel target, got %d\n%s", testCase.description, expectedCount, actualCount, bazelTargets)
1594 } else {
1595 for i, target := range bazelTargets {
1596 if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
1597 t.Errorf(
1598 "%s: Expected generated Bazel target to be '%s', got '%s'",
1599 testCase.description,
1600 w,
1601 g,
1602 )
1603 }
1604 }
1605 }
1606 }
1607}