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