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