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