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 | }, |
Jingwen Chen | 58a12b8 | 2021-03-30 13:08:36 +0000 | [diff] [blame] | 244 | { |
| 245 | bp: `custom { |
| 246 | name: "control_characters", |
| 247 | string_list_prop: ["\t", "\n"], |
| 248 | string_prop: "a\t\n\r", |
| 249 | bazel_module: { bp2build_available: true }, |
| 250 | }`, |
| 251 | expectedBazelTarget: `custom( |
| 252 | name = "control_characters", |
| 253 | string_list_prop = [ |
| 254 | "\t", |
| 255 | "\n", |
| 256 | ], |
| 257 | string_prop = "a\t\n\r", |
| 258 | )`, |
| 259 | }, |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | dir := "." |
| 263 | for _, testCase := range testCases { |
| 264 | config := android.TestConfig(buildDir, nil, testCase.bp, nil) |
| 265 | ctx := android.NewTestContext(config) |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 266 | |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 267 | ctx.RegisterModuleType("custom", customModuleFactory) |
| 268 | ctx.RegisterBp2BuildMutator("custom", customBp2BuildMutator) |
| 269 | ctx.RegisterForBazelConversion() |
| 270 | |
| 271 | _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 272 | if Errored(t, "", errs) { |
| 273 | continue |
| 274 | } |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 275 | _, errs = ctx.ResolveDependencies(config) |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 276 | if Errored(t, "", errs) { |
| 277 | continue |
| 278 | } |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 279 | |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 280 | codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build) |
Jingwen Chen | ba369ad | 2021-02-22 10:19:34 -0500 | [diff] [blame] | 281 | bazelTargets := generateBazelTargetsForDir(codegenCtx, dir) |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 282 | |
Jingwen Chen | 4e4756d | 2021-01-24 21:13:13 -0500 | [diff] [blame] | 283 | if actualCount, expectedCount := len(bazelTargets), 1; actualCount != expectedCount { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 284 | t.Errorf("Expected %d bazel target, got %d", expectedCount, actualCount) |
| 285 | } else { |
| 286 | actualBazelTarget := bazelTargets[0] |
| 287 | if actualBazelTarget.content != testCase.expectedBazelTarget { |
| 288 | t.Errorf( |
| 289 | "Expected generated Bazel target to be '%s', got '%s'", |
| 290 | testCase.expectedBazelTarget, |
| 291 | actualBazelTarget.content, |
| 292 | ) |
| 293 | } |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | } |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 297 | |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 298 | func TestLoadStatements(t *testing.T) { |
| 299 | testCases := []struct { |
| 300 | bazelTargets BazelTargets |
| 301 | expectedLoadStatements string |
| 302 | }{ |
| 303 | { |
| 304 | bazelTargets: BazelTargets{ |
| 305 | BazelTarget{ |
| 306 | name: "foo", |
| 307 | ruleClass: "cc_library", |
| 308 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 309 | }, |
| 310 | }, |
| 311 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`, |
| 312 | }, |
| 313 | { |
| 314 | bazelTargets: BazelTargets{ |
| 315 | BazelTarget{ |
| 316 | name: "foo", |
| 317 | ruleClass: "cc_library", |
| 318 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 319 | }, |
| 320 | BazelTarget{ |
| 321 | name: "bar", |
| 322 | ruleClass: "cc_library", |
| 323 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 324 | }, |
| 325 | }, |
| 326 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`, |
| 327 | }, |
| 328 | { |
| 329 | bazelTargets: BazelTargets{ |
| 330 | BazelTarget{ |
| 331 | name: "foo", |
| 332 | ruleClass: "cc_library", |
| 333 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 334 | }, |
| 335 | BazelTarget{ |
| 336 | name: "bar", |
| 337 | ruleClass: "cc_binary", |
| 338 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 339 | }, |
| 340 | }, |
| 341 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")`, |
| 342 | }, |
| 343 | { |
| 344 | bazelTargets: BazelTargets{ |
| 345 | BazelTarget{ |
| 346 | name: "foo", |
| 347 | ruleClass: "cc_library", |
| 348 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 349 | }, |
| 350 | BazelTarget{ |
| 351 | name: "bar", |
| 352 | ruleClass: "cc_binary", |
| 353 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 354 | }, |
| 355 | BazelTarget{ |
| 356 | name: "baz", |
| 357 | ruleClass: "java_binary", |
| 358 | bzlLoadLocation: "//build/bazel/rules:java.bzl", |
| 359 | }, |
| 360 | }, |
| 361 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library") |
| 362 | load("//build/bazel/rules:java.bzl", "java_binary")`, |
| 363 | }, |
| 364 | { |
| 365 | bazelTargets: BazelTargets{ |
| 366 | BazelTarget{ |
| 367 | name: "foo", |
| 368 | ruleClass: "cc_binary", |
| 369 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 370 | }, |
| 371 | BazelTarget{ |
| 372 | name: "bar", |
| 373 | ruleClass: "java_binary", |
| 374 | bzlLoadLocation: "//build/bazel/rules:java.bzl", |
| 375 | }, |
| 376 | BazelTarget{ |
| 377 | name: "baz", |
| 378 | ruleClass: "genrule", |
| 379 | // Note: no bzlLoadLocation for native rules |
| 380 | }, |
| 381 | }, |
| 382 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary") |
| 383 | load("//build/bazel/rules:java.bzl", "java_binary")`, |
| 384 | }, |
| 385 | } |
| 386 | |
| 387 | for _, testCase := range testCases { |
| 388 | actual := testCase.bazelTargets.LoadStatements() |
| 389 | expected := testCase.expectedLoadStatements |
| 390 | if actual != expected { |
| 391 | t.Fatalf("Expected load statements to be %s, got %s", expected, actual) |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | } |
| 396 | |
| 397 | func TestGenerateBazelTargetModules_OneToMany_LoadedFromStarlark(t *testing.T) { |
| 398 | testCases := []struct { |
| 399 | bp string |
| 400 | expectedBazelTarget string |
| 401 | expectedBazelTargetCount int |
| 402 | expectedLoadStatements string |
| 403 | }{ |
| 404 | { |
| 405 | bp: `custom { |
| 406 | name: "bar", |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 407 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 408 | }`, |
| 409 | expectedBazelTarget: `my_library( |
| 410 | name = "bar", |
| 411 | ) |
| 412 | |
| 413 | my_proto_library( |
| 414 | name = "bar_my_proto_library_deps", |
| 415 | ) |
| 416 | |
| 417 | proto_library( |
| 418 | name = "bar_proto_library_deps", |
| 419 | )`, |
| 420 | expectedBazelTargetCount: 3, |
| 421 | expectedLoadStatements: `load("//build/bazel/rules:proto.bzl", "my_proto_library", "proto_library") |
| 422 | load("//build/bazel/rules:rules.bzl", "my_library")`, |
| 423 | }, |
| 424 | } |
| 425 | |
| 426 | dir := "." |
| 427 | for _, testCase := range testCases { |
| 428 | config := android.TestConfig(buildDir, nil, testCase.bp, nil) |
| 429 | ctx := android.NewTestContext(config) |
| 430 | ctx.RegisterModuleType("custom", customModuleFactory) |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 431 | ctx.RegisterBp2BuildMutator("custom", customBp2BuildMutatorFromStarlark) |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 432 | ctx.RegisterForBazelConversion() |
| 433 | |
| 434 | _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) |
| 435 | android.FailIfErrored(t, errs) |
| 436 | _, errs = ctx.ResolveDependencies(config) |
| 437 | android.FailIfErrored(t, errs) |
| 438 | |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 439 | codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build) |
Jingwen Chen | ba369ad | 2021-02-22 10:19:34 -0500 | [diff] [blame] | 440 | bazelTargets := generateBazelTargetsForDir(codegenCtx, dir) |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 441 | if actualCount := len(bazelTargets); actualCount != testCase.expectedBazelTargetCount { |
| 442 | t.Fatalf("Expected %d bazel target, got %d", testCase.expectedBazelTargetCount, actualCount) |
| 443 | } |
| 444 | |
| 445 | actualBazelTargets := bazelTargets.String() |
| 446 | if actualBazelTargets != testCase.expectedBazelTarget { |
| 447 | t.Errorf( |
| 448 | "Expected generated Bazel target to be '%s', got '%s'", |
| 449 | testCase.expectedBazelTarget, |
| 450 | actualBazelTargets, |
| 451 | ) |
| 452 | } |
| 453 | |
| 454 | actualLoadStatements := bazelTargets.LoadStatements() |
| 455 | if actualLoadStatements != testCase.expectedLoadStatements { |
| 456 | t.Errorf( |
| 457 | "Expected generated load statements to be '%s', got '%s'", |
| 458 | testCase.expectedLoadStatements, |
| 459 | actualLoadStatements, |
| 460 | ) |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 465 | func TestModuleTypeBp2Build(t *testing.T) { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 466 | otherGenruleBp := map[string]string{ |
| 467 | "other/Android.bp": `genrule { |
| 468 | name: "foo.tool", |
| 469 | out: ["foo_tool.out"], |
| 470 | srcs: ["foo_tool.in"], |
| 471 | cmd: "cp $(in) $(out)", |
| 472 | } |
| 473 | genrule { |
| 474 | name: "other.tool", |
| 475 | out: ["other_tool.out"], |
| 476 | srcs: ["other_tool.in"], |
| 477 | cmd: "cp $(in) $(out)", |
| 478 | }`, |
| 479 | } |
| 480 | |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 481 | testCases := []struct { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 482 | description string |
Jingwen Chen | a42d641 | 2021-01-26 21:57:27 -0500 | [diff] [blame] | 483 | moduleTypeUnderTest string |
| 484 | moduleTypeUnderTestFactory android.ModuleFactory |
| 485 | moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext) |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 486 | preArchMutators []android.RegisterMutatorFunc |
| 487 | depsMutators []android.RegisterMutatorFunc |
Jingwen Chen | a42d641 | 2021-01-26 21:57:27 -0500 | [diff] [blame] | 488 | bp string |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 489 | expectedBazelTargets []string |
| 490 | fs map[string]string |
| 491 | dir string |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 492 | }{ |
| 493 | { |
Liz Kammer | ebfcf67 | 2021-02-16 15:00:05 -0500 | [diff] [blame] | 494 | description: "filegroup with does not specify srcs", |
| 495 | moduleTypeUnderTest: "filegroup", |
| 496 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 497 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
| 498 | bp: `filegroup { |
| 499 | name: "fg_foo", |
| 500 | bazel_module: { bp2build_available: true }, |
| 501 | }`, |
| 502 | expectedBazelTargets: []string{ |
| 503 | `filegroup( |
| 504 | name = "fg_foo", |
| 505 | )`, |
| 506 | }, |
| 507 | }, |
| 508 | { |
Jingwen Chen | a42d641 | 2021-01-26 21:57:27 -0500 | [diff] [blame] | 509 | description: "filegroup with no 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: [], |
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{ |
| 519 | `filegroup( |
| 520 | name = "fg_foo", |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 521 | )`, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 522 | }, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 523 | }, |
| 524 | { |
Jingwen Chen | a42d641 | 2021-01-26 21:57:27 -0500 | [diff] [blame] | 525 | description: "filegroup with srcs", |
| 526 | moduleTypeUnderTest: "filegroup", |
| 527 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 528 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 529 | bp: `filegroup { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 530 | name: "fg_foo", |
| 531 | srcs: ["a", "b"], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 532 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 533 | }`, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 534 | expectedBazelTargets: []string{`filegroup( |
| 535 | name = "fg_foo", |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 536 | srcs = [ |
| 537 | "a", |
| 538 | "b", |
| 539 | ], |
| 540 | )`, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 541 | }, |
| 542 | }, |
| 543 | { |
| 544 | description: "filegroup with excludes srcs", |
| 545 | moduleTypeUnderTest: "filegroup", |
| 546 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 547 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
| 548 | bp: `filegroup { |
| 549 | name: "fg_foo", |
| 550 | srcs: ["a", "b"], |
| 551 | exclude_srcs: ["a"], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 552 | bazel_module: { bp2build_available: true }, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 553 | }`, |
| 554 | expectedBazelTargets: []string{`filegroup( |
| 555 | name = "fg_foo", |
| 556 | srcs = [ |
| 557 | "b", |
| 558 | ], |
| 559 | )`, |
| 560 | }, |
| 561 | }, |
| 562 | { |
| 563 | description: "filegroup with glob", |
| 564 | moduleTypeUnderTest: "filegroup", |
| 565 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 566 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
| 567 | bp: `filegroup { |
| 568 | name: "foo", |
| 569 | srcs: ["**/*.txt"], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 570 | bazel_module: { bp2build_available: true }, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 571 | }`, |
| 572 | expectedBazelTargets: []string{`filegroup( |
| 573 | name = "foo", |
| 574 | srcs = [ |
| 575 | "other/a.txt", |
| 576 | "other/b.txt", |
| 577 | "other/subdir/a.txt", |
| 578 | ], |
| 579 | )`, |
| 580 | }, |
| 581 | fs: map[string]string{ |
| 582 | "other/a.txt": "", |
| 583 | "other/b.txt": "", |
| 584 | "other/subdir/a.txt": "", |
| 585 | "other/file": "", |
| 586 | }, |
| 587 | }, |
| 588 | { |
| 589 | description: "filegroup with glob in subdir", |
| 590 | moduleTypeUnderTest: "filegroup", |
| 591 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 592 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
| 593 | bp: `filegroup { |
| 594 | name: "foo", |
| 595 | srcs: ["a.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 | dir: "other", |
| 599 | expectedBazelTargets: []string{`filegroup( |
| 600 | name = "fg_foo", |
| 601 | srcs = [ |
| 602 | "a.txt", |
| 603 | "b.txt", |
| 604 | "subdir/a.txt", |
| 605 | ], |
| 606 | )`, |
| 607 | }, |
| 608 | fs: map[string]string{ |
| 609 | "other/Android.bp": `filegroup { |
| 610 | name: "fg_foo", |
| 611 | srcs: ["**/*.txt"], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 612 | bazel_module: { bp2build_available: true }, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 613 | }`, |
| 614 | "other/a.txt": "", |
| 615 | "other/b.txt": "", |
| 616 | "other/subdir/a.txt": "", |
| 617 | "other/file": "", |
| 618 | }, |
| 619 | }, |
| 620 | { |
| 621 | description: "depends_on_other_dir_module", |
| 622 | moduleTypeUnderTest: "filegroup", |
| 623 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 624 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
| 625 | bp: `filegroup { |
| 626 | name: "foobar", |
| 627 | srcs: [ |
| 628 | ":foo", |
| 629 | "c", |
| 630 | ], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 631 | bazel_module: { bp2build_available: true }, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 632 | }`, |
| 633 | expectedBazelTargets: []string{`filegroup( |
| 634 | name = "foobar", |
| 635 | srcs = [ |
| 636 | "//other:foo", |
| 637 | "c", |
| 638 | ], |
| 639 | )`, |
| 640 | }, |
| 641 | fs: map[string]string{ |
| 642 | "other/Android.bp": `filegroup { |
| 643 | name: "foo", |
| 644 | srcs: ["a", "b"], |
| 645 | }`, |
| 646 | }, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 647 | }, |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 648 | { |
Jingwen Chen | a42d641 | 2021-01-26 21:57:27 -0500 | [diff] [blame] | 649 | description: "genrule with command line variable replacements", |
| 650 | moduleTypeUnderTest: "genrule", |
| 651 | moduleTypeUnderTestFactory: genrule.GenRuleFactory, |
| 652 | moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 653 | depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps}, |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 654 | bp: `genrule { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 655 | name: "foo.tool", |
| 656 | out: ["foo_tool.out"], |
| 657 | srcs: ["foo_tool.in"], |
| 658 | cmd: "cp $(in) $(out)", |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 659 | bazel_module: { bp2build_available: true }, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | genrule { |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 663 | name: "foo", |
| 664 | out: ["foo.out"], |
| 665 | srcs: ["foo.in"], |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 666 | tools: [":foo.tool"], |
| 667 | cmd: "$(location :foo.tool) --genDir=$(genDir) arg $(in) $(out)", |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 668 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 669 | }`, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 670 | expectedBazelTargets: []string{ |
| 671 | `genrule( |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 672 | name = "foo", |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 673 | cmd = "$(location :foo.tool) --genDir=$(GENDIR) arg $(SRCS) $(OUTS)", |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 674 | outs = [ |
| 675 | "foo.out", |
| 676 | ], |
| 677 | srcs = [ |
| 678 | "foo.in", |
| 679 | ], |
| 680 | tools = [ |
| 681 | ":foo.tool", |
| 682 | ], |
| 683 | )`, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 684 | `genrule( |
| 685 | name = "foo.tool", |
| 686 | cmd = "cp $(SRCS) $(OUTS)", |
| 687 | outs = [ |
| 688 | "foo_tool.out", |
| 689 | ], |
| 690 | srcs = [ |
| 691 | "foo_tool.in", |
| 692 | ], |
| 693 | )`, |
| 694 | }, |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 695 | }, |
| 696 | { |
Jingwen Chen | a42d641 | 2021-01-26 21:57:27 -0500 | [diff] [blame] | 697 | description: "genrule using $(locations :label)", |
| 698 | moduleTypeUnderTest: "genrule", |
| 699 | moduleTypeUnderTestFactory: genrule.GenRuleFactory, |
| 700 | moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 701 | depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps}, |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 702 | bp: `genrule { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 703 | name: "foo.tools", |
| 704 | out: ["foo_tool.out", "foo_tool2.out"], |
| 705 | srcs: ["foo_tool.in"], |
| 706 | cmd: "cp $(in) $(out)", |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 707 | bazel_module: { bp2build_available: true }, |
| 708 | } |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 709 | |
| 710 | genrule { |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 711 | name: "foo", |
| 712 | out: ["foo.out"], |
| 713 | srcs: ["foo.in"], |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 714 | tools: [":foo.tools"], |
| 715 | cmd: "$(locations :foo.tools) -s $(out) $(in)", |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 716 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 717 | }`, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 718 | expectedBazelTargets: []string{`genrule( |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 719 | name = "foo", |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 720 | cmd = "$(locations :foo.tools) -s $(OUTS) $(SRCS)", |
| 721 | outs = [ |
| 722 | "foo.out", |
| 723 | ], |
| 724 | srcs = [ |
| 725 | "foo.in", |
| 726 | ], |
| 727 | tools = [ |
| 728 | ":foo.tools", |
| 729 | ], |
| 730 | )`, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 731 | `genrule( |
| 732 | name = "foo.tools", |
| 733 | cmd = "cp $(SRCS) $(OUTS)", |
| 734 | outs = [ |
| 735 | "foo_tool.out", |
| 736 | "foo_tool2.out", |
| 737 | ], |
| 738 | srcs = [ |
| 739 | "foo_tool.in", |
| 740 | ], |
| 741 | )`, |
| 742 | }, |
| 743 | }, |
| 744 | { |
| 745 | description: "genrule using $(locations //absolute:label)", |
| 746 | moduleTypeUnderTest: "genrule", |
| 747 | moduleTypeUnderTestFactory: genrule.GenRuleFactory, |
| 748 | moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build, |
| 749 | depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps}, |
| 750 | bp: `genrule { |
| 751 | name: "foo", |
| 752 | out: ["foo.out"], |
| 753 | srcs: ["foo.in"], |
| 754 | tool_files: [":foo.tool"], |
| 755 | cmd: "$(locations :foo.tool) -s $(out) $(in)", |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 756 | bazel_module: { bp2build_available: true }, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 757 | }`, |
| 758 | expectedBazelTargets: []string{`genrule( |
| 759 | name = "foo", |
| 760 | cmd = "$(locations //other:foo.tool) -s $(OUTS) $(SRCS)", |
| 761 | outs = [ |
| 762 | "foo.out", |
| 763 | ], |
| 764 | srcs = [ |
| 765 | "foo.in", |
| 766 | ], |
| 767 | tools = [ |
| 768 | "//other:foo.tool", |
| 769 | ], |
| 770 | )`, |
| 771 | }, |
| 772 | fs: otherGenruleBp, |
| 773 | }, |
| 774 | { |
| 775 | description: "genrule srcs using $(locations //absolute:label)", |
| 776 | moduleTypeUnderTest: "genrule", |
| 777 | moduleTypeUnderTestFactory: genrule.GenRuleFactory, |
| 778 | moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build, |
| 779 | depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps}, |
| 780 | bp: `genrule { |
| 781 | name: "foo", |
| 782 | out: ["foo.out"], |
| 783 | srcs: [":other.tool"], |
| 784 | tool_files: [":foo.tool"], |
| 785 | cmd: "$(locations :foo.tool) -s $(out) $(location :other.tool)", |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 786 | bazel_module: { bp2build_available: true }, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 787 | }`, |
| 788 | expectedBazelTargets: []string{`genrule( |
| 789 | name = "foo", |
| 790 | cmd = "$(locations //other:foo.tool) -s $(OUTS) $(location //other:other.tool)", |
| 791 | outs = [ |
| 792 | "foo.out", |
| 793 | ], |
| 794 | srcs = [ |
| 795 | "//other:other.tool", |
| 796 | ], |
| 797 | tools = [ |
| 798 | "//other:foo.tool", |
| 799 | ], |
| 800 | )`, |
| 801 | }, |
| 802 | fs: otherGenruleBp, |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 803 | }, |
| 804 | { |
Jingwen Chen | a42d641 | 2021-01-26 21:57:27 -0500 | [diff] [blame] | 805 | description: "genrule using $(location) label should substitute first tool label automatically", |
| 806 | moduleTypeUnderTest: "genrule", |
| 807 | moduleTypeUnderTestFactory: genrule.GenRuleFactory, |
| 808 | moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 809 | depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps}, |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 810 | bp: `genrule { |
| 811 | name: "foo", |
| 812 | out: ["foo.out"], |
| 813 | srcs: ["foo.in"], |
| 814 | tool_files: [":foo.tool", ":other.tool"], |
| 815 | cmd: "$(location) -s $(out) $(in)", |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 816 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 817 | }`, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 818 | expectedBazelTargets: []string{`genrule( |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 819 | name = "foo", |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 820 | cmd = "$(location //other:foo.tool) -s $(OUTS) $(SRCS)", |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 821 | outs = [ |
| 822 | "foo.out", |
| 823 | ], |
| 824 | srcs = [ |
| 825 | "foo.in", |
| 826 | ], |
| 827 | tools = [ |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 828 | "//other:foo.tool", |
| 829 | "//other:other.tool", |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 830 | ], |
| 831 | )`, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 832 | }, |
| 833 | fs: otherGenruleBp, |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 834 | }, |
| 835 | { |
Jingwen Chen | a42d641 | 2021-01-26 21:57:27 -0500 | [diff] [blame] | 836 | description: "genrule using $(locations) label should substitute first tool label automatically", |
| 837 | moduleTypeUnderTest: "genrule", |
| 838 | moduleTypeUnderTestFactory: genrule.GenRuleFactory, |
| 839 | moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 840 | depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps}, |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 841 | bp: `genrule { |
| 842 | name: "foo", |
| 843 | out: ["foo.out"], |
| 844 | srcs: ["foo.in"], |
| 845 | tools: [":foo.tool", ":other.tool"], |
| 846 | cmd: "$(locations) -s $(out) $(in)", |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 847 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 848 | }`, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 849 | expectedBazelTargets: []string{`genrule( |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 850 | name = "foo", |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 851 | cmd = "$(locations //other:foo.tool) -s $(OUTS) $(SRCS)", |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 852 | outs = [ |
| 853 | "foo.out", |
| 854 | ], |
| 855 | srcs = [ |
| 856 | "foo.in", |
| 857 | ], |
| 858 | tools = [ |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 859 | "//other:foo.tool", |
| 860 | "//other:other.tool", |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 861 | ], |
| 862 | )`, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 863 | }, |
| 864 | fs: otherGenruleBp, |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 865 | }, |
| 866 | { |
Jingwen Chen | a42d641 | 2021-01-26 21:57:27 -0500 | [diff] [blame] | 867 | description: "genrule without tools or tool_files can convert successfully", |
| 868 | moduleTypeUnderTest: "genrule", |
| 869 | moduleTypeUnderTestFactory: genrule.GenRuleFactory, |
| 870 | moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 871 | depsMutators: []android.RegisterMutatorFunc{genrule.RegisterGenruleBp2BuildDeps}, |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 872 | bp: `genrule { |
| 873 | name: "foo", |
| 874 | out: ["foo.out"], |
| 875 | srcs: ["foo.in"], |
| 876 | cmd: "cp $(in) $(out)", |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 877 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 878 | }`, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 879 | expectedBazelTargets: []string{`genrule( |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame] | 880 | name = "foo", |
| 881 | cmd = "cp $(SRCS) $(OUTS)", |
| 882 | outs = [ |
| 883 | "foo.out", |
| 884 | ], |
| 885 | srcs = [ |
| 886 | "foo.in", |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 887 | ], |
| 888 | )`, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 889 | }, |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 890 | }, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | dir := "." |
| 894 | for _, testCase := range testCases { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 895 | fs := make(map[string][]byte) |
| 896 | toParse := []string{ |
| 897 | "Android.bp", |
| 898 | } |
| 899 | for f, content := range testCase.fs { |
| 900 | if strings.HasSuffix(f, "Android.bp") { |
| 901 | toParse = append(toParse, f) |
| 902 | } |
| 903 | fs[f] = []byte(content) |
| 904 | } |
| 905 | config := android.TestConfig(buildDir, nil, testCase.bp, fs) |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 906 | ctx := android.NewTestContext(config) |
| 907 | ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory) |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 908 | for _, m := range testCase.depsMutators { |
| 909 | ctx.DepsBp2BuildMutators(m) |
| 910 | } |
Jingwen Chen | a42d641 | 2021-01-26 21:57:27 -0500 | [diff] [blame] | 911 | ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator) |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 912 | ctx.RegisterForBazelConversion() |
| 913 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 914 | _, errs := ctx.ParseFileList(dir, toParse) |
| 915 | if Errored(t, testCase.description, errs) { |
| 916 | continue |
| 917 | } |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 918 | _, errs = ctx.ResolveDependencies(config) |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 919 | if Errored(t, testCase.description, errs) { |
| 920 | continue |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 921 | } |
| 922 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 923 | checkDir := dir |
| 924 | if testCase.dir != "" { |
| 925 | checkDir = testCase.dir |
| 926 | } |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 927 | |
| 928 | codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build) |
Jingwen Chen | ba369ad | 2021-02-22 10:19:34 -0500 | [diff] [blame] | 929 | bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir) |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 930 | if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount { |
| 931 | t.Errorf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount) |
| 932 | } else { |
| 933 | for i, target := range bazelTargets { |
| 934 | if w, g := testCase.expectedBazelTargets[i], target.content; w != g { |
| 935 | t.Errorf( |
| 936 | "%s: Expected generated Bazel target to be '%s', got '%s'", |
| 937 | testCase.description, |
| 938 | w, |
| 939 | g, |
| 940 | ) |
| 941 | } |
| 942 | } |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 943 | } |
| 944 | } |
| 945 | } |
Jingwen Chen | 041b184 | 2021-02-01 00:23:25 -0500 | [diff] [blame] | 946 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 947 | func Errored(t *testing.T, desc string, errs []error) bool { |
| 948 | t.Helper() |
| 949 | if len(errs) > 0 { |
| 950 | for _, err := range errs { |
| 951 | t.Errorf("%s: %s", desc, err) |
| 952 | } |
| 953 | return true |
| 954 | } |
| 955 | return false |
| 956 | } |
| 957 | |
Jingwen Chen | 041b184 | 2021-02-01 00:23:25 -0500 | [diff] [blame] | 958 | type bp2buildMutator = func(android.TopDownMutatorContext) |
| 959 | |
| 960 | func TestBp2BuildInlinesDefaults(t *testing.T) { |
| 961 | testCases := []struct { |
| 962 | moduleTypesUnderTest map[string]android.ModuleFactory |
| 963 | bp2buildMutatorsUnderTest map[string]bp2buildMutator |
| 964 | bp string |
| 965 | expectedBazelTarget string |
| 966 | description string |
| 967 | }{ |
| 968 | { |
| 969 | moduleTypesUnderTest: map[string]android.ModuleFactory{ |
| 970 | "genrule": genrule.GenRuleFactory, |
| 971 | "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() }, |
| 972 | }, |
| 973 | bp2buildMutatorsUnderTest: map[string]bp2buildMutator{ |
| 974 | "genrule": genrule.GenruleBp2Build, |
| 975 | }, |
| 976 | bp: `genrule_defaults { |
| 977 | name: "gen_defaults", |
| 978 | cmd: "do-something $(in) $(out)", |
| 979 | } |
| 980 | genrule { |
| 981 | name: "gen", |
| 982 | out: ["out"], |
| 983 | srcs: ["in1"], |
| 984 | defaults: ["gen_defaults"], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 985 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 041b184 | 2021-02-01 00:23:25 -0500 | [diff] [blame] | 986 | } |
| 987 | `, |
| 988 | expectedBazelTarget: `genrule( |
| 989 | name = "gen", |
| 990 | cmd = "do-something $(SRCS) $(OUTS)", |
| 991 | outs = [ |
| 992 | "out", |
| 993 | ], |
| 994 | srcs = [ |
| 995 | "in1", |
| 996 | ], |
| 997 | )`, |
| 998 | description: "genrule applies properties from a genrule_defaults dependency if not specified", |
| 999 | }, |
| 1000 | { |
| 1001 | moduleTypesUnderTest: map[string]android.ModuleFactory{ |
| 1002 | "genrule": genrule.GenRuleFactory, |
| 1003 | "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() }, |
| 1004 | }, |
| 1005 | bp2buildMutatorsUnderTest: map[string]bp2buildMutator{ |
| 1006 | "genrule": genrule.GenruleBp2Build, |
| 1007 | }, |
| 1008 | bp: `genrule_defaults { |
| 1009 | name: "gen_defaults", |
| 1010 | out: ["out-from-defaults"], |
| 1011 | srcs: ["in-from-defaults"], |
| 1012 | cmd: "cmd-from-defaults", |
| 1013 | } |
| 1014 | genrule { |
| 1015 | name: "gen", |
| 1016 | out: ["out"], |
| 1017 | srcs: ["in1"], |
| 1018 | defaults: ["gen_defaults"], |
| 1019 | cmd: "do-something $(in) $(out)", |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1020 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 041b184 | 2021-02-01 00:23:25 -0500 | [diff] [blame] | 1021 | } |
| 1022 | `, |
| 1023 | expectedBazelTarget: `genrule( |
| 1024 | name = "gen", |
| 1025 | cmd = "do-something $(SRCS) $(OUTS)", |
| 1026 | outs = [ |
| 1027 | "out-from-defaults", |
| 1028 | "out", |
| 1029 | ], |
| 1030 | srcs = [ |
| 1031 | "in-from-defaults", |
| 1032 | "in1", |
| 1033 | ], |
| 1034 | )`, |
| 1035 | description: "genrule does merges properties from a genrule_defaults dependency, latest-first", |
| 1036 | }, |
| 1037 | { |
| 1038 | moduleTypesUnderTest: map[string]android.ModuleFactory{ |
| 1039 | "genrule": genrule.GenRuleFactory, |
| 1040 | "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() }, |
| 1041 | }, |
| 1042 | bp2buildMutatorsUnderTest: map[string]bp2buildMutator{ |
| 1043 | "genrule": genrule.GenruleBp2Build, |
| 1044 | }, |
| 1045 | bp: `genrule_defaults { |
| 1046 | name: "gen_defaults1", |
| 1047 | cmd: "cp $(in) $(out)", |
| 1048 | } |
| 1049 | |
| 1050 | genrule_defaults { |
| 1051 | name: "gen_defaults2", |
| 1052 | srcs: ["in1"], |
| 1053 | } |
| 1054 | |
| 1055 | genrule { |
| 1056 | name: "gen", |
| 1057 | out: ["out"], |
| 1058 | defaults: ["gen_defaults1", "gen_defaults2"], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1059 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 041b184 | 2021-02-01 00:23:25 -0500 | [diff] [blame] | 1060 | } |
| 1061 | `, |
| 1062 | expectedBazelTarget: `genrule( |
| 1063 | name = "gen", |
| 1064 | cmd = "cp $(SRCS) $(OUTS)", |
| 1065 | outs = [ |
| 1066 | "out", |
| 1067 | ], |
| 1068 | srcs = [ |
| 1069 | "in1", |
| 1070 | ], |
| 1071 | )`, |
| 1072 | description: "genrule applies properties from list of genrule_defaults", |
| 1073 | }, |
| 1074 | { |
| 1075 | moduleTypesUnderTest: map[string]android.ModuleFactory{ |
| 1076 | "genrule": genrule.GenRuleFactory, |
| 1077 | "genrule_defaults": func() android.Module { return genrule.DefaultsFactory() }, |
| 1078 | }, |
| 1079 | bp2buildMutatorsUnderTest: map[string]bp2buildMutator{ |
| 1080 | "genrule": genrule.GenruleBp2Build, |
| 1081 | }, |
| 1082 | bp: `genrule_defaults { |
| 1083 | name: "gen_defaults1", |
| 1084 | defaults: ["gen_defaults2"], |
| 1085 | cmd: "cmd1 $(in) $(out)", // overrides gen_defaults2's cmd property value. |
| 1086 | } |
| 1087 | |
| 1088 | genrule_defaults { |
| 1089 | name: "gen_defaults2", |
| 1090 | defaults: ["gen_defaults3"], |
| 1091 | cmd: "cmd2 $(in) $(out)", |
| 1092 | out: ["out-from-2"], |
| 1093 | srcs: ["in1"], |
| 1094 | } |
| 1095 | |
| 1096 | genrule_defaults { |
| 1097 | name: "gen_defaults3", |
| 1098 | out: ["out-from-3"], |
| 1099 | srcs: ["srcs-from-3"], |
| 1100 | } |
| 1101 | |
| 1102 | genrule { |
| 1103 | name: "gen", |
| 1104 | out: ["out"], |
| 1105 | defaults: ["gen_defaults1"], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1106 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 041b184 | 2021-02-01 00:23:25 -0500 | [diff] [blame] | 1107 | } |
| 1108 | `, |
| 1109 | expectedBazelTarget: `genrule( |
| 1110 | name = "gen", |
| 1111 | cmd = "cmd1 $(SRCS) $(OUTS)", |
| 1112 | outs = [ |
| 1113 | "out-from-3", |
| 1114 | "out-from-2", |
| 1115 | "out", |
| 1116 | ], |
| 1117 | srcs = [ |
Jingwen Chen | 041b184 | 2021-02-01 00:23:25 -0500 | [diff] [blame] | 1118 | "in1", |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 1119 | "srcs-from-3", |
Jingwen Chen | 041b184 | 2021-02-01 00:23:25 -0500 | [diff] [blame] | 1120 | ], |
| 1121 | )`, |
| 1122 | description: "genrule applies properties from genrule_defaults transitively", |
| 1123 | }, |
| 1124 | } |
| 1125 | |
| 1126 | dir := "." |
| 1127 | for _, testCase := range testCases { |
| 1128 | config := android.TestConfig(buildDir, nil, testCase.bp, nil) |
| 1129 | ctx := android.NewTestContext(config) |
| 1130 | for m, factory := range testCase.moduleTypesUnderTest { |
| 1131 | ctx.RegisterModuleType(m, factory) |
| 1132 | } |
| 1133 | for mutator, f := range testCase.bp2buildMutatorsUnderTest { |
| 1134 | ctx.RegisterBp2BuildMutator(mutator, f) |
| 1135 | } |
| 1136 | ctx.RegisterForBazelConversion() |
| 1137 | |
| 1138 | _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) |
| 1139 | android.FailIfErrored(t, errs) |
| 1140 | _, errs = ctx.ResolveDependencies(config) |
| 1141 | android.FailIfErrored(t, errs) |
| 1142 | |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 1143 | codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build) |
Jingwen Chen | ba369ad | 2021-02-22 10:19:34 -0500 | [diff] [blame] | 1144 | bazelTargets := generateBazelTargetsForDir(codegenCtx, dir) |
Jingwen Chen | 041b184 | 2021-02-01 00:23:25 -0500 | [diff] [blame] | 1145 | if actualCount := len(bazelTargets); actualCount != 1 { |
| 1146 | t.Fatalf("%s: Expected 1 bazel target, got %d", testCase.description, actualCount) |
| 1147 | } |
| 1148 | |
| 1149 | actualBazelTarget := bazelTargets[0] |
| 1150 | if actualBazelTarget.content != testCase.expectedBazelTarget { |
| 1151 | t.Errorf( |
| 1152 | "%s: Expected generated Bazel target to be '%s', got '%s'", |
| 1153 | testCase.description, |
| 1154 | testCase.expectedBazelTarget, |
| 1155 | actualBazelTarget.content, |
| 1156 | ) |
| 1157 | } |
| 1158 | } |
| 1159 | } |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1160 | |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1161 | func TestAllowlistingBp2buildTargetsExplicitly(t *testing.T) { |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1162 | testCases := []struct { |
| 1163 | moduleTypeUnderTest string |
| 1164 | moduleTypeUnderTestFactory android.ModuleFactory |
| 1165 | moduleTypeUnderTestBp2BuildMutator bp2buildMutator |
| 1166 | bp string |
| 1167 | expectedCount int |
| 1168 | description string |
| 1169 | }{ |
| 1170 | { |
| 1171 | description: "explicitly unavailable", |
| 1172 | moduleTypeUnderTest: "filegroup", |
| 1173 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 1174 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
| 1175 | bp: `filegroup { |
| 1176 | name: "foo", |
| 1177 | srcs: ["a", "b"], |
| 1178 | bazel_module: { bp2build_available: false }, |
| 1179 | }`, |
| 1180 | expectedCount: 0, |
| 1181 | }, |
| 1182 | { |
| 1183 | description: "implicitly unavailable", |
| 1184 | moduleTypeUnderTest: "filegroup", |
| 1185 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 1186 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
| 1187 | bp: `filegroup { |
| 1188 | name: "foo", |
| 1189 | srcs: ["a", "b"], |
| 1190 | }`, |
| 1191 | expectedCount: 0, |
| 1192 | }, |
| 1193 | { |
| 1194 | description: "explicitly available", |
| 1195 | moduleTypeUnderTest: "filegroup", |
| 1196 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 1197 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
| 1198 | bp: `filegroup { |
| 1199 | name: "foo", |
| 1200 | srcs: ["a", "b"], |
| 1201 | bazel_module: { bp2build_available: true }, |
| 1202 | }`, |
| 1203 | expectedCount: 1, |
| 1204 | }, |
| 1205 | { |
| 1206 | description: "generates more than 1 target if needed", |
| 1207 | moduleTypeUnderTest: "custom", |
| 1208 | moduleTypeUnderTestFactory: customModuleFactory, |
| 1209 | moduleTypeUnderTestBp2BuildMutator: customBp2BuildMutatorFromStarlark, |
| 1210 | bp: `custom { |
| 1211 | name: "foo", |
| 1212 | bazel_module: { bp2build_available: true }, |
| 1213 | }`, |
| 1214 | expectedCount: 3, |
| 1215 | }, |
| 1216 | } |
| 1217 | |
| 1218 | dir := "." |
| 1219 | for _, testCase := range testCases { |
| 1220 | config := android.TestConfig(buildDir, nil, testCase.bp, nil) |
| 1221 | ctx := android.NewTestContext(config) |
| 1222 | ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory) |
| 1223 | ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator) |
| 1224 | ctx.RegisterForBazelConversion() |
| 1225 | |
| 1226 | _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) |
| 1227 | android.FailIfErrored(t, errs) |
| 1228 | _, errs = ctx.ResolveDependencies(config) |
| 1229 | android.FailIfErrored(t, errs) |
| 1230 | |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 1231 | codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build) |
Jingwen Chen | ba369ad | 2021-02-22 10:19:34 -0500 | [diff] [blame] | 1232 | bazelTargets := generateBazelTargetsForDir(codegenCtx, dir) |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1233 | if actualCount := len(bazelTargets); actualCount != testCase.expectedCount { |
| 1234 | t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, testCase.expectedCount, actualCount) |
| 1235 | } |
| 1236 | } |
| 1237 | } |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1238 | |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1239 | func TestAllowlistingBp2buildTargetsWithConfig(t *testing.T) { |
| 1240 | testCases := []struct { |
| 1241 | moduleTypeUnderTest string |
| 1242 | moduleTypeUnderTestFactory android.ModuleFactory |
| 1243 | moduleTypeUnderTestBp2BuildMutator bp2buildMutator |
| 1244 | expectedCount map[string]int |
| 1245 | description string |
| 1246 | bp2buildConfig android.Bp2BuildConfig |
| 1247 | checkDir string |
| 1248 | fs map[string]string |
| 1249 | }{ |
| 1250 | { |
| 1251 | description: "test bp2build config package and subpackages config", |
| 1252 | moduleTypeUnderTest: "filegroup", |
| 1253 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 1254 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
| 1255 | expectedCount: map[string]int{ |
| 1256 | "migrated": 1, |
| 1257 | "migrated/but_not_really": 0, |
| 1258 | "migrated/but_not_really/but_really": 1, |
| 1259 | "not_migrated": 0, |
| 1260 | "also_not_migrated": 0, |
| 1261 | }, |
| 1262 | bp2buildConfig: android.Bp2BuildConfig{ |
| 1263 | "migrated": android.Bp2BuildDefaultTrueRecursively, |
| 1264 | "migrated/but_not_really": android.Bp2BuildDefaultFalse, |
| 1265 | "not_migrated": android.Bp2BuildDefaultFalse, |
| 1266 | }, |
| 1267 | fs: map[string]string{ |
| 1268 | "migrated/Android.bp": `filegroup { name: "a" }`, |
| 1269 | "migrated/but_not_really/Android.bp": `filegroup { name: "b" }`, |
| 1270 | "migrated/but_not_really/but_really/Android.bp": `filegroup { name: "c" }`, |
| 1271 | "not_migrated/Android.bp": `filegroup { name: "d" }`, |
| 1272 | "also_not_migrated/Android.bp": `filegroup { name: "e" }`, |
| 1273 | }, |
| 1274 | }, |
| 1275 | { |
| 1276 | description: "test bp2build config opt-in and opt-out", |
| 1277 | moduleTypeUnderTest: "filegroup", |
| 1278 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 1279 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
| 1280 | expectedCount: map[string]int{ |
| 1281 | "package-opt-in": 2, |
| 1282 | "package-opt-in/subpackage": 0, |
| 1283 | "package-opt-out": 1, |
| 1284 | "package-opt-out/subpackage": 0, |
| 1285 | }, |
| 1286 | bp2buildConfig: android.Bp2BuildConfig{ |
| 1287 | "package-opt-in": android.Bp2BuildDefaultFalse, |
| 1288 | "package-opt-out": android.Bp2BuildDefaultTrueRecursively, |
| 1289 | }, |
| 1290 | fs: map[string]string{ |
| 1291 | "package-opt-in/Android.bp": ` |
| 1292 | filegroup { name: "opt-in-a" } |
| 1293 | filegroup { name: "opt-in-b", bazel_module: { bp2build_available: true } } |
| 1294 | filegroup { name: "opt-in-c", bazel_module: { bp2build_available: true } } |
| 1295 | `, |
| 1296 | |
| 1297 | "package-opt-in/subpackage/Android.bp": ` |
| 1298 | filegroup { name: "opt-in-d" } // parent package not configured to DefaultTrueRecursively |
| 1299 | `, |
| 1300 | |
| 1301 | "package-opt-out/Android.bp": ` |
| 1302 | filegroup { name: "opt-out-a" } |
| 1303 | filegroup { name: "opt-out-b", bazel_module: { bp2build_available: false } } |
| 1304 | filegroup { name: "opt-out-c", bazel_module: { bp2build_available: false } } |
| 1305 | `, |
| 1306 | |
| 1307 | "package-opt-out/subpackage/Android.bp": ` |
| 1308 | filegroup { name: "opt-out-g", bazel_module: { bp2build_available: false } } |
| 1309 | filegroup { name: "opt-out-h", bazel_module: { bp2build_available: false } } |
| 1310 | `, |
| 1311 | }, |
| 1312 | }, |
| 1313 | } |
| 1314 | |
| 1315 | dir := "." |
| 1316 | for _, testCase := range testCases { |
| 1317 | fs := make(map[string][]byte) |
| 1318 | toParse := []string{ |
| 1319 | "Android.bp", |
| 1320 | } |
| 1321 | for f, content := range testCase.fs { |
| 1322 | if strings.HasSuffix(f, "Android.bp") { |
| 1323 | toParse = append(toParse, f) |
| 1324 | } |
| 1325 | fs[f] = []byte(content) |
| 1326 | } |
| 1327 | config := android.TestConfig(buildDir, nil, "", fs) |
| 1328 | ctx := android.NewTestContext(config) |
| 1329 | ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory) |
| 1330 | ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator) |
| 1331 | ctx.RegisterBp2BuildConfig(testCase.bp2buildConfig) |
| 1332 | ctx.RegisterForBazelConversion() |
| 1333 | |
| 1334 | _, errs := ctx.ParseFileList(dir, toParse) |
| 1335 | android.FailIfErrored(t, errs) |
| 1336 | _, errs = ctx.ResolveDependencies(config) |
| 1337 | android.FailIfErrored(t, errs) |
| 1338 | |
| 1339 | codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build) |
| 1340 | |
| 1341 | // For each directory, test that the expected number of generated targets is correct. |
| 1342 | for dir, expectedCount := range testCase.expectedCount { |
| 1343 | bazelTargets := generateBazelTargetsForDir(codegenCtx, dir) |
| 1344 | if actualCount := len(bazelTargets); actualCount != expectedCount { |
| 1345 | t.Fatalf( |
| 1346 | "%s: Expected %d bazel target for %s package, got %d", |
| 1347 | testCase.description, |
| 1348 | expectedCount, |
| 1349 | dir, |
| 1350 | actualCount) |
| 1351 | } |
| 1352 | |
| 1353 | } |
| 1354 | } |
| 1355 | } |
| 1356 | |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1357 | func TestCombineBuildFilesBp2buildTargets(t *testing.T) { |
| 1358 | testCases := []struct { |
| 1359 | description string |
| 1360 | moduleTypeUnderTest string |
| 1361 | moduleTypeUnderTestFactory android.ModuleFactory |
| 1362 | moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext) |
| 1363 | preArchMutators []android.RegisterMutatorFunc |
| 1364 | depsMutators []android.RegisterMutatorFunc |
| 1365 | bp string |
| 1366 | expectedBazelTargets []string |
| 1367 | fs map[string]string |
| 1368 | dir string |
| 1369 | }{ |
| 1370 | { |
| 1371 | description: "filegroup bazel_module.label", |
| 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 | expectedBazelTargets: []string{ |
| 1380 | `// BUILD file`, |
| 1381 | }, |
| 1382 | fs: map[string]string{ |
| 1383 | "other/BUILD.bazel": `// BUILD file`, |
| 1384 | }, |
| 1385 | }, |
| 1386 | { |
| 1387 | description: "multiple bazel_module.label same BUILD", |
| 1388 | moduleTypeUnderTest: "filegroup", |
| 1389 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 1390 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
| 1391 | bp: `filegroup { |
| 1392 | name: "fg_foo", |
| 1393 | bazel_module: { label: "//other:fg_foo" }, |
| 1394 | } |
| 1395 | |
| 1396 | filegroup { |
| 1397 | name: "foo", |
| 1398 | bazel_module: { label: "//other:foo" }, |
| 1399 | }`, |
| 1400 | expectedBazelTargets: []string{ |
| 1401 | `// BUILD file`, |
| 1402 | }, |
| 1403 | fs: map[string]string{ |
| 1404 | "other/BUILD.bazel": `// BUILD file`, |
| 1405 | }, |
| 1406 | }, |
| 1407 | { |
| 1408 | description: "filegroup bazel_module.label and bp2build", |
| 1409 | moduleTypeUnderTest: "filegroup", |
| 1410 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 1411 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
| 1412 | bp: `filegroup { |
| 1413 | name: "fg_foo", |
| 1414 | bazel_module: { |
| 1415 | label: "//other:fg_foo", |
| 1416 | bp2build_available: true, |
| 1417 | }, |
| 1418 | }`, |
| 1419 | expectedBazelTargets: []string{ |
| 1420 | `filegroup( |
| 1421 | name = "fg_foo", |
| 1422 | )`, |
| 1423 | `// BUILD file`, |
| 1424 | }, |
| 1425 | fs: map[string]string{ |
| 1426 | "other/BUILD.bazel": `// BUILD file`, |
| 1427 | }, |
| 1428 | }, |
| 1429 | { |
| 1430 | description: "filegroup bazel_module.label and filegroup bp2build", |
| 1431 | moduleTypeUnderTest: "filegroup", |
| 1432 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 1433 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
| 1434 | bp: `filegroup { |
| 1435 | name: "fg_foo", |
| 1436 | bazel_module: { |
| 1437 | label: "//other:fg_foo", |
| 1438 | }, |
| 1439 | } |
| 1440 | |
| 1441 | filegroup { |
| 1442 | name: "fg_bar", |
| 1443 | bazel_module: { |
| 1444 | bp2build_available: true, |
| 1445 | }, |
| 1446 | }`, |
| 1447 | expectedBazelTargets: []string{ |
| 1448 | `filegroup( |
| 1449 | name = "fg_bar", |
| 1450 | )`, |
| 1451 | `// BUILD file`, |
| 1452 | }, |
| 1453 | fs: map[string]string{ |
| 1454 | "other/BUILD.bazel": `// BUILD file`, |
| 1455 | }, |
| 1456 | }, |
| 1457 | } |
| 1458 | |
| 1459 | dir := "." |
| 1460 | for _, testCase := range testCases { |
| 1461 | fs := make(map[string][]byte) |
| 1462 | toParse := []string{ |
| 1463 | "Android.bp", |
| 1464 | } |
| 1465 | for f, content := range testCase.fs { |
| 1466 | if strings.HasSuffix(f, "Android.bp") { |
| 1467 | toParse = append(toParse, f) |
| 1468 | } |
| 1469 | fs[f] = []byte(content) |
| 1470 | } |
| 1471 | config := android.TestConfig(buildDir, nil, testCase.bp, fs) |
| 1472 | ctx := android.NewTestContext(config) |
| 1473 | ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory) |
| 1474 | for _, m := range testCase.depsMutators { |
| 1475 | ctx.DepsBp2BuildMutators(m) |
| 1476 | } |
| 1477 | ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator) |
| 1478 | ctx.RegisterForBazelConversion() |
| 1479 | |
| 1480 | _, errs := ctx.ParseFileList(dir, toParse) |
| 1481 | if Errored(t, testCase.description, errs) { |
| 1482 | continue |
| 1483 | } |
| 1484 | _, errs = ctx.ResolveDependencies(config) |
| 1485 | if Errored(t, testCase.description, errs) { |
| 1486 | continue |
| 1487 | } |
| 1488 | |
| 1489 | checkDir := dir |
| 1490 | if testCase.dir != "" { |
| 1491 | checkDir = testCase.dir |
| 1492 | } |
| 1493 | bazelTargets := generateBazelTargetsForDir(NewCodegenContext(config, *ctx.Context, Bp2Build), checkDir) |
| 1494 | if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount { |
| 1495 | t.Errorf("%s: Expected %d bazel target, got %d\n%s", testCase.description, expectedCount, actualCount, bazelTargets) |
| 1496 | } else { |
| 1497 | for i, target := range bazelTargets { |
| 1498 | if w, g := testCase.expectedBazelTargets[i], target.content; w != g { |
| 1499 | t.Errorf( |
| 1500 | "%s: Expected generated Bazel target to be '%s', got '%s'", |
| 1501 | testCase.description, |
| 1502 | w, |
| 1503 | g, |
| 1504 | ) |
| 1505 | } |
| 1506 | } |
| 1507 | } |
| 1508 | } |
| 1509 | } |