Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 1 | // 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 | |
| 15 | package bp2build |
| 16 | |
| 17 | import ( |
| 18 | "android/soong/android" |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 19 | "android/soong/genrule" |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 20 | "strings" |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 21 | "testing" |
| 22 | ) |
| 23 | |
| 24 | func TestGenerateSoongModuleTargets(t *testing.T) { |
| 25 | testCases := []struct { |
| 26 | bp string |
| 27 | expectedBazelTarget string |
| 28 | }{ |
| 29 | { |
| 30 | bp: `custom { |
| 31 | name: "foo", |
| 32 | } |
| 33 | `, |
| 34 | expectedBazelTarget: `soong_module( |
| 35 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 36 | soong_module_name = "foo", |
| 37 | soong_module_type = "custom", |
| 38 | soong_module_variant = "", |
| 39 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 40 | ], |
| 41 | )`, |
| 42 | }, |
| 43 | { |
| 44 | bp: `custom { |
| 45 | name: "foo", |
| 46 | ramdisk: true, |
| 47 | } |
| 48 | `, |
| 49 | expectedBazelTarget: `soong_module( |
| 50 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 51 | soong_module_name = "foo", |
| 52 | soong_module_type = "custom", |
| 53 | soong_module_variant = "", |
| 54 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 55 | ], |
| 56 | ramdisk = True, |
| 57 | )`, |
| 58 | }, |
| 59 | { |
| 60 | bp: `custom { |
| 61 | name: "foo", |
| 62 | owner: "a_string_with\"quotes\"_and_\\backslashes\\\\", |
| 63 | } |
| 64 | `, |
| 65 | expectedBazelTarget: `soong_module( |
| 66 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 67 | soong_module_name = "foo", |
| 68 | soong_module_type = "custom", |
| 69 | soong_module_variant = "", |
| 70 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 71 | ], |
| 72 | owner = "a_string_with\"quotes\"_and_\\backslashes\\\\", |
| 73 | )`, |
| 74 | }, |
| 75 | { |
| 76 | bp: `custom { |
| 77 | name: "foo", |
| 78 | required: ["bar"], |
| 79 | } |
| 80 | `, |
| 81 | expectedBazelTarget: `soong_module( |
| 82 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 83 | soong_module_name = "foo", |
| 84 | soong_module_type = "custom", |
| 85 | soong_module_variant = "", |
| 86 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 87 | ], |
| 88 | required = [ |
| 89 | "bar", |
| 90 | ], |
| 91 | )`, |
| 92 | }, |
| 93 | { |
| 94 | bp: `custom { |
| 95 | name: "foo", |
| 96 | target_required: ["qux", "bazqux"], |
| 97 | } |
| 98 | `, |
| 99 | expectedBazelTarget: `soong_module( |
| 100 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 101 | soong_module_name = "foo", |
| 102 | soong_module_type = "custom", |
| 103 | soong_module_variant = "", |
| 104 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 105 | ], |
| 106 | target_required = [ |
| 107 | "qux", |
| 108 | "bazqux", |
| 109 | ], |
| 110 | )`, |
| 111 | }, |
| 112 | { |
| 113 | bp: `custom { |
| 114 | name: "foo", |
| 115 | dist: { |
| 116 | targets: ["goal_foo"], |
| 117 | tag: ".foo", |
| 118 | }, |
| 119 | dists: [ |
| 120 | { |
| 121 | targets: ["goal_bar"], |
| 122 | tag: ".bar", |
| 123 | }, |
| 124 | ], |
| 125 | } |
| 126 | `, |
| 127 | expectedBazelTarget: `soong_module( |
| 128 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 129 | soong_module_name = "foo", |
| 130 | soong_module_type = "custom", |
| 131 | soong_module_variant = "", |
| 132 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 133 | ], |
| 134 | dist = { |
| 135 | "tag": ".foo", |
| 136 | "targets": [ |
| 137 | "goal_foo", |
| 138 | ], |
| 139 | }, |
| 140 | dists = [ |
| 141 | { |
| 142 | "tag": ".bar", |
| 143 | "targets": [ |
| 144 | "goal_bar", |
| 145 | ], |
| 146 | }, |
| 147 | ], |
| 148 | )`, |
| 149 | }, |
| 150 | { |
| 151 | bp: `custom { |
| 152 | name: "foo", |
| 153 | required: ["bar"], |
| 154 | target_required: ["qux", "bazqux"], |
| 155 | ramdisk: true, |
| 156 | owner: "custom_owner", |
| 157 | dists: [ |
| 158 | { |
| 159 | tag: ".tag", |
| 160 | targets: ["my_goal"], |
| 161 | }, |
| 162 | ], |
| 163 | } |
| 164 | `, |
| 165 | expectedBazelTarget: `soong_module( |
| 166 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 167 | soong_module_name = "foo", |
| 168 | soong_module_type = "custom", |
| 169 | soong_module_variant = "", |
| 170 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 171 | ], |
| 172 | dists = [ |
| 173 | { |
| 174 | "tag": ".tag", |
| 175 | "targets": [ |
| 176 | "my_goal", |
| 177 | ], |
| 178 | }, |
| 179 | ], |
| 180 | owner = "custom_owner", |
| 181 | ramdisk = True, |
| 182 | required = [ |
| 183 | "bar", |
| 184 | ], |
| 185 | target_required = [ |
| 186 | "qux", |
| 187 | "bazqux", |
| 188 | ], |
| 189 | )`, |
| 190 | }, |
| 191 | } |
| 192 | |
| 193 | dir := "." |
| 194 | for _, testCase := range testCases { |
| 195 | config := android.TestConfig(buildDir, nil, testCase.bp, nil) |
| 196 | ctx := android.NewTestContext(config) |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 197 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 198 | ctx.RegisterModuleType("custom", customModuleFactory) |
| 199 | ctx.Register() |
| 200 | |
| 201 | _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) |
| 202 | android.FailIfErrored(t, errs) |
| 203 | _, errs = ctx.PrepareBuildActions(config) |
| 204 | android.FailIfErrored(t, errs) |
| 205 | |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 206 | codegenCtx := NewCodegenContext(config, *ctx.Context, QueryView) |
Jingwen Chen | ba369ad | 2021-02-22 10:19:34 -0500 | [diff] [blame] | 207 | bazelTargets := generateBazelTargetsForDir(codegenCtx, dir) |
Jingwen Chen | 4e4756d | 2021-01-24 21:13:13 -0500 | [diff] [blame] | 208 | if actualCount, expectedCount := len(bazelTargets), 1; actualCount != expectedCount { |
| 209 | t.Fatalf("Expected %d bazel target, got %d", expectedCount, actualCount) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | actualBazelTarget := bazelTargets[0] |
| 213 | if actualBazelTarget.content != testCase.expectedBazelTarget { |
| 214 | t.Errorf( |
| 215 | "Expected generated Bazel target to be '%s', got '%s'", |
| 216 | testCase.expectedBazelTarget, |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 217 | actualBazelTarget.content, |
| 218 | ) |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | func TestGenerateBazelTargetModules(t *testing.T) { |
| 224 | testCases := []struct { |
| 225 | bp string |
| 226 | expectedBazelTarget string |
| 227 | }{ |
| 228 | { |
| 229 | bp: `custom { |
| 230 | name: "foo", |
| 231 | string_list_prop: ["a", "b"], |
| 232 | string_prop: "a", |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 233 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 234 | }`, |
| 235 | expectedBazelTarget: `custom( |
| 236 | name = "foo", |
| 237 | string_list_prop = [ |
| 238 | "a", |
| 239 | "b", |
| 240 | ], |
| 241 | string_prop = "a", |
| 242 | )`, |
| 243 | }, |
| 244 | } |
| 245 | |
| 246 | dir := "." |
| 247 | for _, testCase := range testCases { |
| 248 | config := android.TestConfig(buildDir, nil, testCase.bp, nil) |
| 249 | ctx := android.NewTestContext(config) |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 250 | |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 251 | ctx.RegisterModuleType("custom", customModuleFactory) |
| 252 | ctx.RegisterBp2BuildMutator("custom", customBp2BuildMutator) |
| 253 | ctx.RegisterForBazelConversion() |
| 254 | |
| 255 | _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 256 | if Errored(t, "", errs) { |
| 257 | continue |
| 258 | } |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 259 | _, errs = ctx.ResolveDependencies(config) |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 260 | if Errored(t, "", errs) { |
| 261 | continue |
| 262 | } |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 263 | |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 264 | codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build) |
Jingwen Chen | ba369ad | 2021-02-22 10:19:34 -0500 | [diff] [blame] | 265 | bazelTargets := generateBazelTargetsForDir(codegenCtx, dir) |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 266 | |
Jingwen Chen | 4e4756d | 2021-01-24 21:13:13 -0500 | [diff] [blame] | 267 | if actualCount, expectedCount := len(bazelTargets), 1; actualCount != expectedCount { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 268 | t.Errorf("Expected %d bazel target, got %d", expectedCount, actualCount) |
| 269 | } else { |
| 270 | actualBazelTarget := bazelTargets[0] |
| 271 | if actualBazelTarget.content != testCase.expectedBazelTarget { |
| 272 | t.Errorf( |
| 273 | "Expected generated Bazel target to be '%s', got '%s'", |
| 274 | testCase.expectedBazelTarget, |
| 275 | actualBazelTarget.content, |
| 276 | ) |
| 277 | } |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | } |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 281 | |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 282 | func TestLoadStatements(t *testing.T) { |
| 283 | testCases := []struct { |
| 284 | bazelTargets BazelTargets |
| 285 | expectedLoadStatements string |
| 286 | }{ |
| 287 | { |
| 288 | bazelTargets: BazelTargets{ |
| 289 | BazelTarget{ |
| 290 | name: "foo", |
| 291 | ruleClass: "cc_library", |
| 292 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 293 | }, |
| 294 | }, |
| 295 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`, |
| 296 | }, |
| 297 | { |
| 298 | bazelTargets: BazelTargets{ |
| 299 | BazelTarget{ |
| 300 | name: "foo", |
| 301 | ruleClass: "cc_library", |
| 302 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 303 | }, |
| 304 | BazelTarget{ |
| 305 | name: "bar", |
| 306 | ruleClass: "cc_library", |
| 307 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 308 | }, |
| 309 | }, |
| 310 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`, |
| 311 | }, |
| 312 | { |
| 313 | bazelTargets: BazelTargets{ |
| 314 | BazelTarget{ |
| 315 | name: "foo", |
| 316 | ruleClass: "cc_library", |
| 317 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 318 | }, |
| 319 | BazelTarget{ |
| 320 | name: "bar", |
| 321 | ruleClass: "cc_binary", |
| 322 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 323 | }, |
| 324 | }, |
| 325 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")`, |
| 326 | }, |
| 327 | { |
| 328 | bazelTargets: BazelTargets{ |
| 329 | BazelTarget{ |
| 330 | name: "foo", |
| 331 | ruleClass: "cc_library", |
| 332 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 333 | }, |
| 334 | BazelTarget{ |
| 335 | name: "bar", |
| 336 | ruleClass: "cc_binary", |
| 337 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 338 | }, |
| 339 | BazelTarget{ |
| 340 | name: "baz", |
| 341 | ruleClass: "java_binary", |
| 342 | bzlLoadLocation: "//build/bazel/rules:java.bzl", |
| 343 | }, |
| 344 | }, |
| 345 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library") |
| 346 | load("//build/bazel/rules:java.bzl", "java_binary")`, |
| 347 | }, |
| 348 | { |
| 349 | bazelTargets: BazelTargets{ |
| 350 | BazelTarget{ |
| 351 | name: "foo", |
| 352 | ruleClass: "cc_binary", |
| 353 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 354 | }, |
| 355 | BazelTarget{ |
| 356 | name: "bar", |
| 357 | ruleClass: "java_binary", |
| 358 | bzlLoadLocation: "//build/bazel/rules:java.bzl", |
| 359 | }, |
| 360 | BazelTarget{ |
| 361 | name: "baz", |
| 362 | ruleClass: "genrule", |
| 363 | // Note: no bzlLoadLocation for native rules |
| 364 | }, |
| 365 | }, |
| 366 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary") |
| 367 | load("//build/bazel/rules:java.bzl", "java_binary")`, |
| 368 | }, |
| 369 | } |
| 370 | |
| 371 | for _, testCase := range testCases { |
| 372 | actual := testCase.bazelTargets.LoadStatements() |
| 373 | expected := testCase.expectedLoadStatements |
| 374 | if actual != expected { |
| 375 | t.Fatalf("Expected load statements to be %s, got %s", expected, actual) |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | } |
| 380 | |
| 381 | func TestGenerateBazelTargetModules_OneToMany_LoadedFromStarlark(t *testing.T) { |
| 382 | testCases := []struct { |
| 383 | bp string |
| 384 | expectedBazelTarget string |
| 385 | expectedBazelTargetCount int |
| 386 | expectedLoadStatements string |
| 387 | }{ |
| 388 | { |
| 389 | bp: `custom { |
| 390 | name: "bar", |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 391 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 392 | }`, |
| 393 | expectedBazelTarget: `my_library( |
| 394 | name = "bar", |
| 395 | ) |
| 396 | |
| 397 | my_proto_library( |
| 398 | name = "bar_my_proto_library_deps", |
| 399 | ) |
| 400 | |
| 401 | proto_library( |
| 402 | name = "bar_proto_library_deps", |
| 403 | )`, |
| 404 | expectedBazelTargetCount: 3, |
| 405 | expectedLoadStatements: `load("//build/bazel/rules:proto.bzl", "my_proto_library", "proto_library") |
| 406 | load("//build/bazel/rules:rules.bzl", "my_library")`, |
| 407 | }, |
| 408 | } |
| 409 | |
| 410 | dir := "." |
| 411 | for _, testCase := range testCases { |
| 412 | config := android.TestConfig(buildDir, nil, testCase.bp, nil) |
| 413 | ctx := android.NewTestContext(config) |
| 414 | ctx.RegisterModuleType("custom", customModuleFactory) |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 415 | ctx.RegisterBp2BuildMutator("custom", customBp2BuildMutatorFromStarlark) |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 416 | ctx.RegisterForBazelConversion() |
| 417 | |
| 418 | _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) |
| 419 | android.FailIfErrored(t, errs) |
| 420 | _, errs = ctx.ResolveDependencies(config) |
| 421 | android.FailIfErrored(t, errs) |
| 422 | |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 423 | codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build) |
Jingwen Chen | ba369ad | 2021-02-22 10:19:34 -0500 | [diff] [blame] | 424 | bazelTargets := generateBazelTargetsForDir(codegenCtx, dir) |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 425 | if actualCount := len(bazelTargets); actualCount != testCase.expectedBazelTargetCount { |
| 426 | t.Fatalf("Expected %d bazel target, got %d", testCase.expectedBazelTargetCount, actualCount) |
| 427 | } |
| 428 | |
| 429 | actualBazelTargets := bazelTargets.String() |
| 430 | if actualBazelTargets != testCase.expectedBazelTarget { |
| 431 | t.Errorf( |
| 432 | "Expected generated Bazel target to be '%s', got '%s'", |
| 433 | testCase.expectedBazelTarget, |
| 434 | actualBazelTargets, |
| 435 | ) |
| 436 | } |
| 437 | |
| 438 | actualLoadStatements := bazelTargets.LoadStatements() |
| 439 | if actualLoadStatements != testCase.expectedLoadStatements { |
| 440 | t.Errorf( |
| 441 | "Expected generated load statements to be '%s', got '%s'", |
| 442 | testCase.expectedLoadStatements, |
| 443 | actualLoadStatements, |
| 444 | ) |
| 445 | } |
| 446 | } |
| 447 | } |
| 448 | |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 449 | func TestModuleTypeBp2Build(t *testing.T) { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 450 | otherGenruleBp := map[string]string{ |
| 451 | "other/Android.bp": `genrule { |
| 452 | name: "foo.tool", |
| 453 | out: ["foo_tool.out"], |
| 454 | srcs: ["foo_tool.in"], |
| 455 | cmd: "cp $(in) $(out)", |
| 456 | } |
| 457 | genrule { |
| 458 | name: "other.tool", |
| 459 | out: ["other_tool.out"], |
| 460 | srcs: ["other_tool.in"], |
| 461 | cmd: "cp $(in) $(out)", |
| 462 | }`, |
| 463 | } |
| 464 | |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 465 | testCases := []struct { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 466 | description string |
Jingwen Chen | a42d641 | 2021-01-26 21:57:27 -0500 | [diff] [blame] | 467 | moduleTypeUnderTest string |
| 468 | moduleTypeUnderTestFactory android.ModuleFactory |
| 469 | moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext) |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 470 | preArchMutators []android.RegisterMutatorFunc |
| 471 | depsMutators []android.RegisterMutatorFunc |
Jingwen Chen | a42d641 | 2021-01-26 21:57:27 -0500 | [diff] [blame] | 472 | bp string |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 473 | expectedBazelTargets []string |
| 474 | fs map[string]string |
| 475 | dir string |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 476 | }{ |
| 477 | { |
Liz Kammer | ebfcf67 | 2021-02-16 15:00:05 -0500 | [diff] [blame] | 478 | description: "filegroup with does not specify srcs", |
| 479 | moduleTypeUnderTest: "filegroup", |
| 480 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 481 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
| 482 | bp: `filegroup { |
| 483 | name: "fg_foo", |
| 484 | bazel_module: { bp2build_available: true }, |
| 485 | }`, |
| 486 | expectedBazelTargets: []string{ |
| 487 | `filegroup( |
| 488 | name = "fg_foo", |
| 489 | )`, |
| 490 | }, |
| 491 | }, |
| 492 | { |
Jingwen Chen | a42d641 | 2021-01-26 21:57:27 -0500 | [diff] [blame] | 493 | description: "filegroup with no srcs", |
| 494 | moduleTypeUnderTest: "filegroup", |
| 495 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 496 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 497 | bp: `filegroup { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 498 | name: "fg_foo", |
| 499 | srcs: [], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 500 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 501 | }`, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 502 | expectedBazelTargets: []string{ |
| 503 | `filegroup( |
| 504 | name = "fg_foo", |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 505 | )`, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 506 | }, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 507 | }, |
| 508 | { |
Jingwen Chen | a42d641 | 2021-01-26 21:57:27 -0500 | [diff] [blame] | 509 | description: "filegroup with srcs", |
| 510 | moduleTypeUnderTest: "filegroup", |
| 511 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 512 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 513 | bp: `filegroup { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 514 | name: "fg_foo", |
| 515 | srcs: ["a", "b"], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 516 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 517 | }`, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 518 | expectedBazelTargets: []string{`filegroup( |
| 519 | name = "fg_foo", |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 520 | srcs = [ |
| 521 | "a", |
| 522 | "b", |
| 523 | ], |
| 524 | )`, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 525 | }, |
| 526 | }, |
| 527 | { |
| 528 | description: "filegroup with excludes srcs", |
| 529 | moduleTypeUnderTest: "filegroup", |
| 530 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 531 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
| 532 | bp: `filegroup { |
| 533 | name: "fg_foo", |
| 534 | srcs: ["a", "b"], |
| 535 | exclude_srcs: ["a"], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 536 | bazel_module: { bp2build_available: true }, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 537 | }`, |
| 538 | expectedBazelTargets: []string{`filegroup( |
| 539 | name = "fg_foo", |
| 540 | srcs = [ |
| 541 | "b", |
| 542 | ], |
| 543 | )`, |
| 544 | }, |
| 545 | }, |
| 546 | { |
| 547 | description: "filegroup with glob", |
| 548 | moduleTypeUnderTest: "filegroup", |
| 549 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 550 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
| 551 | bp: `filegroup { |
| 552 | name: "foo", |
| 553 | srcs: ["**/*.txt"], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 554 | bazel_module: { bp2build_available: true }, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 555 | }`, |
| 556 | expectedBazelTargets: []string{`filegroup( |
| 557 | name = "foo", |
| 558 | srcs = [ |
| 559 | "other/a.txt", |
| 560 | "other/b.txt", |
| 561 | "other/subdir/a.txt", |
| 562 | ], |
| 563 | )`, |
| 564 | }, |
| 565 | fs: map[string]string{ |
| 566 | "other/a.txt": "", |
| 567 | "other/b.txt": "", |
| 568 | "other/subdir/a.txt": "", |
| 569 | "other/file": "", |
| 570 | }, |
| 571 | }, |
| 572 | { |
| 573 | description: "filegroup with glob in subdir", |
| 574 | moduleTypeUnderTest: "filegroup", |
| 575 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 576 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
| 577 | bp: `filegroup { |
| 578 | name: "foo", |
| 579 | srcs: ["a.txt"], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 580 | bazel_module: { bp2build_available: true }, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 581 | }`, |
| 582 | dir: "other", |
| 583 | expectedBazelTargets: []string{`filegroup( |
| 584 | name = "fg_foo", |
| 585 | srcs = [ |
| 586 | "a.txt", |
| 587 | "b.txt", |
| 588 | "subdir/a.txt", |
| 589 | ], |
| 590 | )`, |
| 591 | }, |
| 592 | fs: map[string]string{ |
| 593 | "other/Android.bp": `filegroup { |
| 594 | name: "fg_foo", |
| 595 | srcs: ["**/*.txt"], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 596 | bazel_module: { bp2build_available: true }, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 597 | }`, |
| 598 | "other/a.txt": "", |
| 599 | "other/b.txt": "", |
| 600 | "other/subdir/a.txt": "", |
| 601 | "other/file": "", |
| 602 | }, |
| 603 | }, |
| 604 | { |
| 605 | description: "depends_on_other_dir_module", |
| 606 | moduleTypeUnderTest: "filegroup", |
| 607 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 608 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
| 609 | bp: `filegroup { |
| 610 | name: "foobar", |
| 611 | srcs: [ |
| 612 | ":foo", |
| 613 | "c", |
| 614 | ], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 615 | bazel_module: { bp2build_available: true }, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 616 | }`, |
| 617 | expectedBazelTargets: []string{`filegroup( |
| 618 | name = "foobar", |
| 619 | srcs = [ |
| 620 | "//other:foo", |
| 621 | "c", |
| 622 | ], |
| 623 | )`, |
| 624 | }, |
| 625 | fs: map[string]string{ |
| 626 | "other/Android.bp": `filegroup { |
| 627 | name: "foo", |
| 628 | srcs: ["a", "b"], |
| 629 | }`, |
| 630 | }, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 631 | }, |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 632 | { |
Jingwen Chen | a42d641 | 2021-01-26 21:57:27 -0500 | [diff] [blame] | 633 | description: "genrule with command line variable replacements", |
| 634 | moduleTypeUnderTest: "genrule", |
| 635 | moduleTypeUnderTestFactory: genrule.GenRuleFactory, |
| 636 | moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 637 | depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps}, |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 638 | bp: `genrule { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 639 | name: "foo.tool", |
| 640 | out: ["foo_tool.out"], |
| 641 | srcs: ["foo_tool.in"], |
| 642 | cmd: "cp $(in) $(out)", |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 643 | bazel_module: { bp2build_available: true }, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | genrule { |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 647 | name: "foo", |
| 648 | out: ["foo.out"], |
| 649 | srcs: ["foo.in"], |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 650 | tools: [":foo.tool"], |
| 651 | cmd: "$(location :foo.tool) --genDir=$(genDir) arg $(in) $(out)", |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 652 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 653 | }`, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 654 | expectedBazelTargets: []string{ |
| 655 | `genrule( |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 656 | name = "foo", |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 657 | cmd = "$(location :foo.tool) --genDir=$(GENDIR) arg $(SRCS) $(OUTS)", |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 658 | outs = [ |
| 659 | "foo.out", |
| 660 | ], |
| 661 | srcs = [ |
| 662 | "foo.in", |
| 663 | ], |
| 664 | tools = [ |
| 665 | ":foo.tool", |
| 666 | ], |
| 667 | )`, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 668 | `genrule( |
| 669 | name = "foo.tool", |
| 670 | cmd = "cp $(SRCS) $(OUTS)", |
| 671 | outs = [ |
| 672 | "foo_tool.out", |
| 673 | ], |
| 674 | srcs = [ |
| 675 | "foo_tool.in", |
| 676 | ], |
| 677 | )`, |
| 678 | }, |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 679 | }, |
| 680 | { |
Jingwen Chen | a42d641 | 2021-01-26 21:57:27 -0500 | [diff] [blame] | 681 | description: "genrule using $(locations :label)", |
| 682 | moduleTypeUnderTest: "genrule", |
| 683 | moduleTypeUnderTestFactory: genrule.GenRuleFactory, |
| 684 | moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 685 | depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps}, |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 686 | bp: `genrule { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 687 | name: "foo.tools", |
| 688 | out: ["foo_tool.out", "foo_tool2.out"], |
| 689 | srcs: ["foo_tool.in"], |
| 690 | cmd: "cp $(in) $(out)", |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 691 | bazel_module: { bp2build_available: true }, |
| 692 | } |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 693 | |
| 694 | genrule { |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 695 | name: "foo", |
| 696 | out: ["foo.out"], |
| 697 | srcs: ["foo.in"], |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 698 | tools: [":foo.tools"], |
| 699 | cmd: "$(locations :foo.tools) -s $(out) $(in)", |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 700 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 701 | }`, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 702 | expectedBazelTargets: []string{`genrule( |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 703 | name = "foo", |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 704 | cmd = "$(locations :foo.tools) -s $(OUTS) $(SRCS)", |
| 705 | outs = [ |
| 706 | "foo.out", |
| 707 | ], |
| 708 | srcs = [ |
| 709 | "foo.in", |
| 710 | ], |
| 711 | tools = [ |
| 712 | ":foo.tools", |
| 713 | ], |
| 714 | )`, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 715 | `genrule( |
| 716 | name = "foo.tools", |
| 717 | cmd = "cp $(SRCS) $(OUTS)", |
| 718 | outs = [ |
| 719 | "foo_tool.out", |
| 720 | "foo_tool2.out", |
| 721 | ], |
| 722 | srcs = [ |
| 723 | "foo_tool.in", |
| 724 | ], |
| 725 | )`, |
| 726 | }, |
| 727 | }, |
| 728 | { |
| 729 | description: "genrule using $(locations //absolute:label)", |
| 730 | moduleTypeUnderTest: "genrule", |
| 731 | moduleTypeUnderTestFactory: genrule.GenRuleFactory, |
| 732 | moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build, |
| 733 | depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps}, |
| 734 | bp: `genrule { |
| 735 | name: "foo", |
| 736 | out: ["foo.out"], |
| 737 | srcs: ["foo.in"], |
| 738 | tool_files: [":foo.tool"], |
| 739 | cmd: "$(locations :foo.tool) -s $(out) $(in)", |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 740 | bazel_module: { bp2build_available: true }, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 741 | }`, |
| 742 | expectedBazelTargets: []string{`genrule( |
| 743 | name = "foo", |
| 744 | cmd = "$(locations //other:foo.tool) -s $(OUTS) $(SRCS)", |
| 745 | outs = [ |
| 746 | "foo.out", |
| 747 | ], |
| 748 | srcs = [ |
| 749 | "foo.in", |
| 750 | ], |
| 751 | tools = [ |
| 752 | "//other:foo.tool", |
| 753 | ], |
| 754 | )`, |
| 755 | }, |
| 756 | fs: otherGenruleBp, |
| 757 | }, |
| 758 | { |
| 759 | description: "genrule srcs using $(locations //absolute:label)", |
| 760 | moduleTypeUnderTest: "genrule", |
| 761 | moduleTypeUnderTestFactory: genrule.GenRuleFactory, |
| 762 | moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build, |
| 763 | depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps}, |
| 764 | bp: `genrule { |
| 765 | name: "foo", |
| 766 | out: ["foo.out"], |
| 767 | srcs: [":other.tool"], |
| 768 | tool_files: [":foo.tool"], |
| 769 | cmd: "$(locations :foo.tool) -s $(out) $(location :other.tool)", |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 770 | bazel_module: { bp2build_available: true }, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 771 | }`, |
| 772 | expectedBazelTargets: []string{`genrule( |
| 773 | name = "foo", |
| 774 | cmd = "$(locations //other:foo.tool) -s $(OUTS) $(location //other:other.tool)", |
| 775 | outs = [ |
| 776 | "foo.out", |
| 777 | ], |
| 778 | srcs = [ |
| 779 | "//other:other.tool", |
| 780 | ], |
| 781 | tools = [ |
| 782 | "//other:foo.tool", |
| 783 | ], |
| 784 | )`, |
| 785 | }, |
| 786 | fs: otherGenruleBp, |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 787 | }, |
| 788 | { |
Jingwen Chen | a42d641 | 2021-01-26 21:57:27 -0500 | [diff] [blame] | 789 | description: "genrule using $(location) label should substitute first tool label automatically", |
| 790 | moduleTypeUnderTest: "genrule", |
| 791 | moduleTypeUnderTestFactory: genrule.GenRuleFactory, |
| 792 | moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 793 | depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps}, |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 794 | bp: `genrule { |
| 795 | name: "foo", |
| 796 | out: ["foo.out"], |
| 797 | srcs: ["foo.in"], |
| 798 | tool_files: [":foo.tool", ":other.tool"], |
| 799 | cmd: "$(location) -s $(out) $(in)", |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 800 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 801 | }`, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 802 | expectedBazelTargets: []string{`genrule( |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 803 | name = "foo", |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 804 | cmd = "$(location //other:foo.tool) -s $(OUTS) $(SRCS)", |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 805 | outs = [ |
| 806 | "foo.out", |
| 807 | ], |
| 808 | srcs = [ |
| 809 | "foo.in", |
| 810 | ], |
| 811 | tools = [ |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 812 | "//other:foo.tool", |
| 813 | "//other:other.tool", |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 814 | ], |
| 815 | )`, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 816 | }, |
| 817 | fs: otherGenruleBp, |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 818 | }, |
| 819 | { |
Jingwen Chen | a42d641 | 2021-01-26 21:57:27 -0500 | [diff] [blame] | 820 | description: "genrule using $(locations) label should substitute first tool label automatically", |
| 821 | moduleTypeUnderTest: "genrule", |
| 822 | moduleTypeUnderTestFactory: genrule.GenRuleFactory, |
| 823 | moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 824 | depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps}, |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 825 | bp: `genrule { |
| 826 | name: "foo", |
| 827 | out: ["foo.out"], |
| 828 | srcs: ["foo.in"], |
| 829 | tools: [":foo.tool", ":other.tool"], |
| 830 | cmd: "$(locations) -s $(out) $(in)", |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 831 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 832 | }`, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 833 | expectedBazelTargets: []string{`genrule( |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 834 | name = "foo", |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 835 | cmd = "$(locations //other:foo.tool) -s $(OUTS) $(SRCS)", |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 836 | outs = [ |
| 837 | "foo.out", |
| 838 | ], |
| 839 | srcs = [ |
| 840 | "foo.in", |
| 841 | ], |
| 842 | tools = [ |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 843 | "//other:foo.tool", |
| 844 | "//other:other.tool", |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 845 | ], |
| 846 | )`, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 847 | }, |
| 848 | fs: otherGenruleBp, |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 849 | }, |
| 850 | { |
Jingwen Chen | a42d641 | 2021-01-26 21:57:27 -0500 | [diff] [blame] | 851 | description: "genrule without tools or tool_files can convert successfully", |
| 852 | moduleTypeUnderTest: "genrule", |
| 853 | moduleTypeUnderTestFactory: genrule.GenRuleFactory, |
| 854 | moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 855 | depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps}, |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 856 | bp: `genrule { |
| 857 | name: "foo", |
| 858 | out: ["foo.out"], |
| 859 | srcs: ["foo.in"], |
| 860 | cmd: "cp $(in) $(out)", |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 861 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 862 | }`, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 863 | expectedBazelTargets: []string{`genrule( |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 864 | name = "foo", |
| 865 | cmd = "cp $(SRCS) $(OUTS)", |
| 866 | outs = [ |
| 867 | "foo.out", |
| 868 | ], |
| 869 | srcs = [ |
| 870 | "foo.in", |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 871 | ], |
| 872 | )`, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 873 | }, |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 874 | }, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | dir := "." |
| 878 | for _, testCase := range testCases { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 879 | fs := make(map[string][]byte) |
| 880 | toParse := []string{ |
| 881 | "Android.bp", |
| 882 | } |
| 883 | for f, content := range testCase.fs { |
| 884 | if strings.HasSuffix(f, "Android.bp") { |
| 885 | toParse = append(toParse, f) |
| 886 | } |
| 887 | fs[f] = []byte(content) |
| 888 | } |
| 889 | config := android.TestConfig(buildDir, nil, testCase.bp, fs) |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 890 | ctx := android.NewTestContext(config) |
| 891 | ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory) |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 892 | for _, m := range testCase.depsMutators { |
| 893 | ctx.DepsBp2BuildMutators(m) |
| 894 | } |
Jingwen Chen | a42d641 | 2021-01-26 21:57:27 -0500 | [diff] [blame] | 895 | ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator) |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 896 | ctx.RegisterForBazelConversion() |
| 897 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 898 | _, errs := ctx.ParseFileList(dir, toParse) |
| 899 | if Errored(t, testCase.description, errs) { |
| 900 | continue |
| 901 | } |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 902 | _, errs = ctx.ResolveDependencies(config) |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 903 | if Errored(t, testCase.description, errs) { |
| 904 | continue |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 905 | } |
| 906 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 907 | checkDir := dir |
| 908 | if testCase.dir != "" { |
| 909 | checkDir = testCase.dir |
| 910 | } |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 911 | |
| 912 | codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build) |
Jingwen Chen | ba369ad | 2021-02-22 10:19:34 -0500 | [diff] [blame] | 913 | bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir) |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 914 | if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount { |
| 915 | t.Errorf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount) |
| 916 | } else { |
| 917 | for i, target := range bazelTargets { |
| 918 | if w, g := testCase.expectedBazelTargets[i], target.content; w != g { |
| 919 | t.Errorf( |
| 920 | "%s: Expected generated Bazel target to be '%s', got '%s'", |
| 921 | testCase.description, |
| 922 | w, |
| 923 | g, |
| 924 | ) |
| 925 | } |
| 926 | } |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 927 | } |
| 928 | } |
| 929 | } |
Jingwen Chen | 041b184 | 2021-02-01 00:23:25 -0500 | [diff] [blame] | 930 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 931 | func Errored(t *testing.T, desc string, errs []error) bool { |
| 932 | t.Helper() |
| 933 | if len(errs) > 0 { |
| 934 | for _, err := range errs { |
| 935 | t.Errorf("%s: %s", desc, err) |
| 936 | } |
| 937 | return true |
| 938 | } |
| 939 | return false |
| 940 | } |
| 941 | |
Jingwen Chen | 041b184 | 2021-02-01 00:23:25 -0500 | [diff] [blame] | 942 | type bp2buildMutator = func(android.TopDownMutatorContext) |
| 943 | |
| 944 | func TestBp2BuildInlinesDefaults(t *testing.T) { |
| 945 | testCases := []struct { |
| 946 | moduleTypesUnderTest map[string]android.ModuleFactory |
| 947 | bp2buildMutatorsUnderTest map[string]bp2buildMutator |
| 948 | bp string |
| 949 | expectedBazelTarget string |
| 950 | description string |
| 951 | }{ |
| 952 | { |
| 953 | moduleTypesUnderTest: map[string]android.ModuleFactory{ |
| 954 | "genrule": genrule.GenRuleFactory, |
| 955 | "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() }, |
| 956 | }, |
| 957 | bp2buildMutatorsUnderTest: map[string]bp2buildMutator{ |
| 958 | "genrule": genrule.GenruleBp2Build, |
| 959 | }, |
| 960 | bp: `genrule_defaults { |
| 961 | name: "gen_defaults", |
| 962 | cmd: "do-something $(in) $(out)", |
| 963 | } |
| 964 | genrule { |
| 965 | name: "gen", |
| 966 | out: ["out"], |
| 967 | srcs: ["in1"], |
| 968 | defaults: ["gen_defaults"], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 969 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 041b184 | 2021-02-01 00:23:25 -0500 | [diff] [blame] | 970 | } |
| 971 | `, |
| 972 | expectedBazelTarget: `genrule( |
| 973 | name = "gen", |
| 974 | cmd = "do-something $(SRCS) $(OUTS)", |
| 975 | outs = [ |
| 976 | "out", |
| 977 | ], |
| 978 | srcs = [ |
| 979 | "in1", |
| 980 | ], |
| 981 | )`, |
| 982 | description: "genrule applies properties from a genrule_defaults dependency if not specified", |
| 983 | }, |
| 984 | { |
| 985 | moduleTypesUnderTest: map[string]android.ModuleFactory{ |
| 986 | "genrule": genrule.GenRuleFactory, |
| 987 | "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() }, |
| 988 | }, |
| 989 | bp2buildMutatorsUnderTest: map[string]bp2buildMutator{ |
| 990 | "genrule": genrule.GenruleBp2Build, |
| 991 | }, |
| 992 | bp: `genrule_defaults { |
| 993 | name: "gen_defaults", |
| 994 | out: ["out-from-defaults"], |
| 995 | srcs: ["in-from-defaults"], |
| 996 | cmd: "cmd-from-defaults", |
| 997 | } |
| 998 | genrule { |
| 999 | name: "gen", |
| 1000 | out: ["out"], |
| 1001 | srcs: ["in1"], |
| 1002 | defaults: ["gen_defaults"], |
| 1003 | cmd: "do-something $(in) $(out)", |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1004 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 041b184 | 2021-02-01 00:23:25 -0500 | [diff] [blame] | 1005 | } |
| 1006 | `, |
| 1007 | expectedBazelTarget: `genrule( |
| 1008 | name = "gen", |
| 1009 | cmd = "do-something $(SRCS) $(OUTS)", |
| 1010 | outs = [ |
| 1011 | "out-from-defaults", |
| 1012 | "out", |
| 1013 | ], |
| 1014 | srcs = [ |
| 1015 | "in-from-defaults", |
| 1016 | "in1", |
| 1017 | ], |
| 1018 | )`, |
| 1019 | description: "genrule does merges properties from a genrule_defaults dependency, latest-first", |
| 1020 | }, |
| 1021 | { |
| 1022 | moduleTypesUnderTest: map[string]android.ModuleFactory{ |
| 1023 | "genrule": genrule.GenRuleFactory, |
| 1024 | "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() }, |
| 1025 | }, |
| 1026 | bp2buildMutatorsUnderTest: map[string]bp2buildMutator{ |
| 1027 | "genrule": genrule.GenruleBp2Build, |
| 1028 | }, |
| 1029 | bp: `genrule_defaults { |
| 1030 | name: "gen_defaults1", |
| 1031 | cmd: "cp $(in) $(out)", |
| 1032 | } |
| 1033 | |
| 1034 | genrule_defaults { |
| 1035 | name: "gen_defaults2", |
| 1036 | srcs: ["in1"], |
| 1037 | } |
| 1038 | |
| 1039 | genrule { |
| 1040 | name: "gen", |
| 1041 | out: ["out"], |
| 1042 | defaults: ["gen_defaults1", "gen_defaults2"], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1043 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 041b184 | 2021-02-01 00:23:25 -0500 | [diff] [blame] | 1044 | } |
| 1045 | `, |
| 1046 | expectedBazelTarget: `genrule( |
| 1047 | name = "gen", |
| 1048 | cmd = "cp $(SRCS) $(OUTS)", |
| 1049 | outs = [ |
| 1050 | "out", |
| 1051 | ], |
| 1052 | srcs = [ |
| 1053 | "in1", |
| 1054 | ], |
| 1055 | )`, |
| 1056 | description: "genrule applies properties from list of genrule_defaults", |
| 1057 | }, |
| 1058 | { |
| 1059 | moduleTypesUnderTest: map[string]android.ModuleFactory{ |
| 1060 | "genrule": genrule.GenRuleFactory, |
| 1061 | "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() }, |
| 1062 | }, |
| 1063 | bp2buildMutatorsUnderTest: map[string]bp2buildMutator{ |
| 1064 | "genrule": genrule.GenruleBp2Build, |
| 1065 | }, |
| 1066 | bp: `genrule_defaults { |
| 1067 | name: "gen_defaults1", |
| 1068 | defaults: ["gen_defaults2"], |
| 1069 | cmd: "cmd1 $(in) $(out)", // overrides gen_defaults2's cmd property value. |
| 1070 | } |
| 1071 | |
| 1072 | genrule_defaults { |
| 1073 | name: "gen_defaults2", |
| 1074 | defaults: ["gen_defaults3"], |
| 1075 | cmd: "cmd2 $(in) $(out)", |
| 1076 | out: ["out-from-2"], |
| 1077 | srcs: ["in1"], |
| 1078 | } |
| 1079 | |
| 1080 | genrule_defaults { |
| 1081 | name: "gen_defaults3", |
| 1082 | out: ["out-from-3"], |
| 1083 | srcs: ["srcs-from-3"], |
| 1084 | } |
| 1085 | |
| 1086 | genrule { |
| 1087 | name: "gen", |
| 1088 | out: ["out"], |
| 1089 | defaults: ["gen_defaults1"], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1090 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 041b184 | 2021-02-01 00:23:25 -0500 | [diff] [blame] | 1091 | } |
| 1092 | `, |
| 1093 | expectedBazelTarget: `genrule( |
| 1094 | name = "gen", |
| 1095 | cmd = "cmd1 $(SRCS) $(OUTS)", |
| 1096 | outs = [ |
| 1097 | "out-from-3", |
| 1098 | "out-from-2", |
| 1099 | "out", |
| 1100 | ], |
| 1101 | srcs = [ |
Jingwen Chen | 041b184 | 2021-02-01 00:23:25 -0500 | [diff] [blame] | 1102 | "in1", |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame^] | 1103 | "srcs-from-3", |
Jingwen Chen | 041b184 | 2021-02-01 00:23:25 -0500 | [diff] [blame] | 1104 | ], |
| 1105 | )`, |
| 1106 | description: "genrule applies properties from genrule_defaults transitively", |
| 1107 | }, |
| 1108 | } |
| 1109 | |
| 1110 | dir := "." |
| 1111 | for _, testCase := range testCases { |
| 1112 | config := android.TestConfig(buildDir, nil, testCase.bp, nil) |
| 1113 | ctx := android.NewTestContext(config) |
| 1114 | for m, factory := range testCase.moduleTypesUnderTest { |
| 1115 | ctx.RegisterModuleType(m, factory) |
| 1116 | } |
| 1117 | for mutator, f := range testCase.bp2buildMutatorsUnderTest { |
| 1118 | ctx.RegisterBp2BuildMutator(mutator, f) |
| 1119 | } |
| 1120 | ctx.RegisterForBazelConversion() |
| 1121 | |
| 1122 | _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) |
| 1123 | android.FailIfErrored(t, errs) |
| 1124 | _, errs = ctx.ResolveDependencies(config) |
| 1125 | android.FailIfErrored(t, errs) |
| 1126 | |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 1127 | codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build) |
Jingwen Chen | ba369ad | 2021-02-22 10:19:34 -0500 | [diff] [blame] | 1128 | bazelTargets := generateBazelTargetsForDir(codegenCtx, dir) |
Jingwen Chen | 041b184 | 2021-02-01 00:23:25 -0500 | [diff] [blame] | 1129 | if actualCount := len(bazelTargets); actualCount != 1 { |
| 1130 | t.Fatalf("%s: Expected 1 bazel target, got %d", testCase.description, actualCount) |
| 1131 | } |
| 1132 | |
| 1133 | actualBazelTarget := bazelTargets[0] |
| 1134 | if actualBazelTarget.content != testCase.expectedBazelTarget { |
| 1135 | t.Errorf( |
| 1136 | "%s: Expected generated Bazel target to be '%s', got '%s'", |
| 1137 | testCase.description, |
| 1138 | testCase.expectedBazelTarget, |
| 1139 | actualBazelTarget.content, |
| 1140 | ) |
| 1141 | } |
| 1142 | } |
| 1143 | } |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1144 | |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1145 | func TestAllowlistingBp2buildTargetsExplicitly(t *testing.T) { |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1146 | testCases := []struct { |
| 1147 | moduleTypeUnderTest string |
| 1148 | moduleTypeUnderTestFactory android.ModuleFactory |
| 1149 | moduleTypeUnderTestBp2BuildMutator bp2buildMutator |
| 1150 | bp string |
| 1151 | expectedCount int |
| 1152 | description string |
| 1153 | }{ |
| 1154 | { |
| 1155 | description: "explicitly unavailable", |
| 1156 | moduleTypeUnderTest: "filegroup", |
| 1157 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 1158 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
| 1159 | bp: `filegroup { |
| 1160 | name: "foo", |
| 1161 | srcs: ["a", "b"], |
| 1162 | bazel_module: { bp2build_available: false }, |
| 1163 | }`, |
| 1164 | expectedCount: 0, |
| 1165 | }, |
| 1166 | { |
| 1167 | description: "implicitly unavailable", |
| 1168 | moduleTypeUnderTest: "filegroup", |
| 1169 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 1170 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
| 1171 | bp: `filegroup { |
| 1172 | name: "foo", |
| 1173 | srcs: ["a", "b"], |
| 1174 | }`, |
| 1175 | expectedCount: 0, |
| 1176 | }, |
| 1177 | { |
| 1178 | description: "explicitly available", |
| 1179 | moduleTypeUnderTest: "filegroup", |
| 1180 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 1181 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
| 1182 | bp: `filegroup { |
| 1183 | name: "foo", |
| 1184 | srcs: ["a", "b"], |
| 1185 | bazel_module: { bp2build_available: true }, |
| 1186 | }`, |
| 1187 | expectedCount: 1, |
| 1188 | }, |
| 1189 | { |
| 1190 | description: "generates more than 1 target if needed", |
| 1191 | moduleTypeUnderTest: "custom", |
| 1192 | moduleTypeUnderTestFactory: customModuleFactory, |
| 1193 | moduleTypeUnderTestBp2BuildMutator: customBp2BuildMutatorFromStarlark, |
| 1194 | bp: `custom { |
| 1195 | name: "foo", |
| 1196 | bazel_module: { bp2build_available: true }, |
| 1197 | }`, |
| 1198 | expectedCount: 3, |
| 1199 | }, |
| 1200 | } |
| 1201 | |
| 1202 | dir := "." |
| 1203 | for _, testCase := range testCases { |
| 1204 | config := android.TestConfig(buildDir, nil, testCase.bp, nil) |
| 1205 | ctx := android.NewTestContext(config) |
| 1206 | ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory) |
| 1207 | ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator) |
| 1208 | ctx.RegisterForBazelConversion() |
| 1209 | |
| 1210 | _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) |
| 1211 | android.FailIfErrored(t, errs) |
| 1212 | _, errs = ctx.ResolveDependencies(config) |
| 1213 | android.FailIfErrored(t, errs) |
| 1214 | |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 1215 | codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build) |
Jingwen Chen | ba369ad | 2021-02-22 10:19:34 -0500 | [diff] [blame] | 1216 | bazelTargets := generateBazelTargetsForDir(codegenCtx, dir) |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1217 | if actualCount := len(bazelTargets); actualCount != testCase.expectedCount { |
| 1218 | t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, testCase.expectedCount, actualCount) |
| 1219 | } |
| 1220 | } |
| 1221 | } |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1222 | |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1223 | func 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": ` |
| 1276 | filegroup { name: "opt-in-a" } |
| 1277 | filegroup { name: "opt-in-b", bazel_module: { bp2build_available: true } } |
| 1278 | filegroup { name: "opt-in-c", bazel_module: { bp2build_available: true } } |
| 1279 | `, |
| 1280 | |
| 1281 | "package-opt-in/subpackage/Android.bp": ` |
| 1282 | filegroup { name: "opt-in-d" } // parent package not configured to DefaultTrueRecursively |
| 1283 | `, |
| 1284 | |
| 1285 | "package-opt-out/Android.bp": ` |
| 1286 | filegroup { name: "opt-out-a" } |
| 1287 | filegroup { name: "opt-out-b", bazel_module: { bp2build_available: false } } |
| 1288 | filegroup { name: "opt-out-c", bazel_module: { bp2build_available: false } } |
| 1289 | `, |
| 1290 | |
| 1291 | "package-opt-out/subpackage/Android.bp": ` |
| 1292 | filegroup { name: "opt-out-g", bazel_module: { bp2build_available: false } } |
| 1293 | filegroup { 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 Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1341 | func TestCombineBuildFilesBp2buildTargets(t *testing.T) { |
| 1342 | testCases := []struct { |
| 1343 | description string |
| 1344 | moduleTypeUnderTest string |
| 1345 | moduleTypeUnderTestFactory android.ModuleFactory |
| 1346 | moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext) |
| 1347 | preArchMutators []android.RegisterMutatorFunc |
| 1348 | depsMutators []android.RegisterMutatorFunc |
| 1349 | bp string |
| 1350 | expectedBazelTargets []string |
| 1351 | fs map[string]string |
| 1352 | dir string |
| 1353 | }{ |
| 1354 | { |
| 1355 | description: "filegroup bazel_module.label", |
| 1356 | moduleTypeUnderTest: "filegroup", |
| 1357 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 1358 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
| 1359 | bp: `filegroup { |
| 1360 | name: "fg_foo", |
| 1361 | bazel_module: { label: "//other:fg_foo" }, |
| 1362 | }`, |
| 1363 | expectedBazelTargets: []string{ |
| 1364 | `// BUILD file`, |
| 1365 | }, |
| 1366 | fs: map[string]string{ |
| 1367 | "other/BUILD.bazel": `// BUILD file`, |
| 1368 | }, |
| 1369 | }, |
| 1370 | { |
| 1371 | description: "multiple bazel_module.label same BUILD", |
| 1372 | moduleTypeUnderTest: "filegroup", |
| 1373 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 1374 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
| 1375 | bp: `filegroup { |
| 1376 | name: "fg_foo", |
| 1377 | bazel_module: { label: "//other:fg_foo" }, |
| 1378 | } |
| 1379 | |
| 1380 | filegroup { |
| 1381 | name: "foo", |
| 1382 | bazel_module: { label: "//other:foo" }, |
| 1383 | }`, |
| 1384 | expectedBazelTargets: []string{ |
| 1385 | `// BUILD file`, |
| 1386 | }, |
| 1387 | fs: map[string]string{ |
| 1388 | "other/BUILD.bazel": `// BUILD file`, |
| 1389 | }, |
| 1390 | }, |
| 1391 | { |
| 1392 | description: "filegroup bazel_module.label and bp2build", |
| 1393 | moduleTypeUnderTest: "filegroup", |
| 1394 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 1395 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
| 1396 | bp: `filegroup { |
| 1397 | name: "fg_foo", |
| 1398 | bazel_module: { |
| 1399 | label: "//other:fg_foo", |
| 1400 | bp2build_available: true, |
| 1401 | }, |
| 1402 | }`, |
| 1403 | expectedBazelTargets: []string{ |
| 1404 | `filegroup( |
| 1405 | name = "fg_foo", |
| 1406 | )`, |
| 1407 | `// BUILD file`, |
| 1408 | }, |
| 1409 | fs: map[string]string{ |
| 1410 | "other/BUILD.bazel": `// BUILD file`, |
| 1411 | }, |
| 1412 | }, |
| 1413 | { |
| 1414 | description: "filegroup bazel_module.label and filegroup bp2build", |
| 1415 | moduleTypeUnderTest: "filegroup", |
| 1416 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 1417 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
| 1418 | bp: `filegroup { |
| 1419 | name: "fg_foo", |
| 1420 | bazel_module: { |
| 1421 | label: "//other:fg_foo", |
| 1422 | }, |
| 1423 | } |
| 1424 | |
| 1425 | filegroup { |
| 1426 | name: "fg_bar", |
| 1427 | bazel_module: { |
| 1428 | bp2build_available: true, |
| 1429 | }, |
| 1430 | }`, |
| 1431 | expectedBazelTargets: []string{ |
| 1432 | `filegroup( |
| 1433 | name = "fg_bar", |
| 1434 | )`, |
| 1435 | `// BUILD file`, |
| 1436 | }, |
| 1437 | fs: map[string]string{ |
| 1438 | "other/BUILD.bazel": `// BUILD file`, |
| 1439 | }, |
| 1440 | }, |
| 1441 | } |
| 1442 | |
| 1443 | dir := "." |
| 1444 | for _, testCase := range testCases { |
| 1445 | fs := make(map[string][]byte) |
| 1446 | toParse := []string{ |
| 1447 | "Android.bp", |
| 1448 | } |
| 1449 | for f, content := range testCase.fs { |
| 1450 | if strings.HasSuffix(f, "Android.bp") { |
| 1451 | toParse = append(toParse, f) |
| 1452 | } |
| 1453 | fs[f] = []byte(content) |
| 1454 | } |
| 1455 | config := android.TestConfig(buildDir, nil, testCase.bp, fs) |
| 1456 | ctx := android.NewTestContext(config) |
| 1457 | ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory) |
| 1458 | for _, m := range testCase.depsMutators { |
| 1459 | ctx.DepsBp2BuildMutators(m) |
| 1460 | } |
| 1461 | ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator) |
| 1462 | ctx.RegisterForBazelConversion() |
| 1463 | |
| 1464 | _, errs := ctx.ParseFileList(dir, toParse) |
| 1465 | if Errored(t, testCase.description, errs) { |
| 1466 | continue |
| 1467 | } |
| 1468 | _, errs = ctx.ResolveDependencies(config) |
| 1469 | if Errored(t, testCase.description, errs) { |
| 1470 | continue |
| 1471 | } |
| 1472 | |
| 1473 | checkDir := dir |
| 1474 | if testCase.dir != "" { |
| 1475 | checkDir = testCase.dir |
| 1476 | } |
| 1477 | bazelTargets := generateBazelTargetsForDir(NewCodegenContext(config, *ctx.Context, Bp2Build), checkDir) |
| 1478 | if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount { |
| 1479 | t.Errorf("%s: Expected %d bazel target, got %d\n%s", testCase.description, expectedCount, actualCount, bazelTargets) |
| 1480 | } else { |
| 1481 | for i, target := range bazelTargets { |
| 1482 | if w, g := testCase.expectedBazelTargets[i], target.content; w != g { |
| 1483 | t.Errorf( |
| 1484 | "%s: Expected generated Bazel target to be '%s', got '%s'", |
| 1485 | testCase.description, |
| 1486 | w, |
| 1487 | g, |
| 1488 | ) |
| 1489 | } |
| 1490 | } |
| 1491 | } |
| 1492 | } |
| 1493 | } |