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