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" |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 19 | "strings" |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 20 | "testing" |
| 21 | ) |
| 22 | |
| 23 | func TestGenerateSoongModuleTargets(t *testing.T) { |
| 24 | testCases := []struct { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 25 | description string |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 26 | bp string |
| 27 | expectedBazelTarget string |
| 28 | }{ |
| 29 | { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 30 | description: "only name", |
Jingwen Chen | b4628eb | 2021-04-08 14:40:57 +0000 | [diff] [blame] | 31 | bp: `custom { name: "foo" } |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 32 | `, |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 33 | expectedBazelTarget: `soong_module( |
| 34 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 35 | soong_module_name = "foo", |
| 36 | soong_module_type = "custom", |
| 37 | soong_module_variant = "", |
| 38 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 39 | ], |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 40 | bool_prop = False, |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 41 | )`, |
| 42 | }, |
| 43 | { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 44 | description: "handles bool", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 45 | bp: `custom { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 46 | name: "foo", |
| 47 | bool_prop: true, |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 48 | } |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 49 | `, |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 50 | expectedBazelTarget: `soong_module( |
| 51 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 52 | soong_module_name = "foo", |
| 53 | soong_module_type = "custom", |
| 54 | soong_module_variant = "", |
| 55 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 56 | ], |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 57 | bool_prop = True, |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 58 | )`, |
| 59 | }, |
| 60 | { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 61 | description: "string escaping", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 62 | bp: `custom { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 63 | name: "foo", |
| 64 | owner: "a_string_with\"quotes\"_and_\\backslashes\\\\", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 65 | } |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 66 | `, |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 67 | expectedBazelTarget: `soong_module( |
| 68 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 69 | soong_module_name = "foo", |
| 70 | soong_module_type = "custom", |
| 71 | soong_module_variant = "", |
| 72 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 73 | ], |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 74 | bool_prop = False, |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 75 | owner = "a_string_with\"quotes\"_and_\\backslashes\\\\", |
| 76 | )`, |
| 77 | }, |
| 78 | { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 79 | description: "single item string list", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 80 | bp: `custom { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 81 | name: "foo", |
| 82 | required: ["bar"], |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 83 | } |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 84 | `, |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 85 | expectedBazelTarget: `soong_module( |
| 86 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 87 | soong_module_name = "foo", |
| 88 | soong_module_type = "custom", |
| 89 | soong_module_variant = "", |
| 90 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 91 | ], |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 92 | bool_prop = False, |
Jingwen Chen | b4628eb | 2021-04-08 14:40:57 +0000 | [diff] [blame] | 93 | required = ["bar"], |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 94 | )`, |
| 95 | }, |
| 96 | { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 97 | description: "list of strings", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 98 | bp: `custom { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 99 | name: "foo", |
| 100 | target_required: ["qux", "bazqux"], |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 101 | } |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 102 | `, |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 103 | expectedBazelTarget: `soong_module( |
| 104 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 105 | soong_module_name = "foo", |
| 106 | soong_module_type = "custom", |
| 107 | soong_module_variant = "", |
| 108 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 109 | ], |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 110 | bool_prop = False, |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 111 | target_required = [ |
| 112 | "qux", |
| 113 | "bazqux", |
| 114 | ], |
| 115 | )`, |
| 116 | }, |
| 117 | { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 118 | description: "dist/dists", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 119 | bp: `custom { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 120 | name: "foo", |
| 121 | dist: { |
| 122 | targets: ["goal_foo"], |
| 123 | tag: ".foo", |
| 124 | }, |
| 125 | dists: [{ |
| 126 | targets: ["goal_bar"], |
| 127 | tag: ".bar", |
| 128 | }], |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 129 | } |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 130 | `, |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 131 | expectedBazelTarget: `soong_module( |
| 132 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 133 | soong_module_name = "foo", |
| 134 | soong_module_type = "custom", |
| 135 | soong_module_variant = "", |
| 136 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 137 | ], |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 138 | bool_prop = False, |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 139 | dist = { |
| 140 | "tag": ".foo", |
Jingwen Chen | b4628eb | 2021-04-08 14:40:57 +0000 | [diff] [blame] | 141 | "targets": ["goal_foo"], |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 142 | }, |
Jingwen Chen | b4628eb | 2021-04-08 14:40:57 +0000 | [diff] [blame] | 143 | dists = [{ |
| 144 | "tag": ".bar", |
| 145 | "targets": ["goal_bar"], |
| 146 | }], |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 147 | )`, |
| 148 | }, |
| 149 | { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 150 | description: "put it together", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 151 | bp: `custom { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 152 | name: "foo", |
| 153 | required: ["bar"], |
| 154 | target_required: ["qux", "bazqux"], |
| 155 | bool_prop: true, |
| 156 | owner: "custom_owner", |
| 157 | dists: [ |
| 158 | { |
| 159 | tag: ".tag", |
| 160 | targets: ["my_goal"], |
| 161 | }, |
| 162 | ], |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 163 | } |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 164 | `, |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 165 | expectedBazelTarget: `soong_module( |
| 166 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 167 | soong_module_name = "foo", |
| 168 | soong_module_type = "custom", |
| 169 | soong_module_variant = "", |
| 170 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 171 | ], |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 172 | bool_prop = True, |
Jingwen Chen | b4628eb | 2021-04-08 14:40:57 +0000 | [diff] [blame] | 173 | dists = [{ |
| 174 | "tag": ".tag", |
| 175 | "targets": ["my_goal"], |
| 176 | }], |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 177 | owner = "custom_owner", |
Jingwen Chen | b4628eb | 2021-04-08 14:40:57 +0000 | [diff] [blame] | 178 | required = ["bar"], |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 179 | target_required = [ |
| 180 | "qux", |
| 181 | "bazqux", |
| 182 | ], |
| 183 | )`, |
| 184 | }, |
| 185 | } |
| 186 | |
| 187 | dir := "." |
| 188 | for _, testCase := range testCases { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 189 | t.Run(testCase.description, func(t *testing.T) { |
| 190 | config := android.TestConfig(buildDir, nil, testCase.bp, nil) |
| 191 | ctx := android.NewTestContext(config) |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 192 | |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 193 | ctx.RegisterModuleType("custom", customModuleFactory) |
| 194 | ctx.Register() |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 195 | |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 196 | _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) |
| 197 | android.FailIfErrored(t, errs) |
| 198 | _, errs = ctx.PrepareBuildActions(config) |
| 199 | android.FailIfErrored(t, errs) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 200 | |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 201 | codegenCtx := NewCodegenContext(config, *ctx.Context, QueryView) |
| 202 | bazelTargets := generateBazelTargetsForDir(codegenCtx, dir) |
| 203 | if actualCount, expectedCount := len(bazelTargets), 1; actualCount != expectedCount { |
| 204 | t.Fatalf("Expected %d bazel target, got %d", expectedCount, actualCount) |
| 205 | } |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 206 | |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 207 | actualBazelTarget := bazelTargets[0] |
| 208 | if actualBazelTarget.content != testCase.expectedBazelTarget { |
| 209 | t.Errorf( |
| 210 | "Expected generated Bazel target to be '%s', got '%s'", |
| 211 | testCase.expectedBazelTarget, |
| 212 | actualBazelTarget.content, |
| 213 | ) |
| 214 | } |
| 215 | }) |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | |
| 219 | func TestGenerateBazelTargetModules(t *testing.T) { |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 220 | testCases := []bp2buildTestCase{ |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 221 | { |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 222 | blueprint: `custom { |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 223 | name: "foo", |
| 224 | string_list_prop: ["a", "b"], |
| 225 | string_prop: "a", |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 226 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 227 | }`, |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 228 | expectedBazelTargets: []string{`custom( |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 229 | name = "foo", |
| 230 | string_list_prop = [ |
| 231 | "a", |
| 232 | "b", |
| 233 | ], |
| 234 | string_prop = "a", |
| 235 | )`, |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 236 | }, |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 237 | }, |
Jingwen Chen | 58a12b8 | 2021-03-30 13:08:36 +0000 | [diff] [blame] | 238 | { |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 239 | blueprint: `custom { |
Jingwen Chen | 58a12b8 | 2021-03-30 13:08:36 +0000 | [diff] [blame] | 240 | name: "control_characters", |
| 241 | string_list_prop: ["\t", "\n"], |
| 242 | string_prop: "a\t\n\r", |
| 243 | bazel_module: { bp2build_available: true }, |
| 244 | }`, |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 245 | expectedBazelTargets: []string{`custom( |
Jingwen Chen | 58a12b8 | 2021-03-30 13:08:36 +0000 | [diff] [blame] | 246 | name = "control_characters", |
| 247 | string_list_prop = [ |
| 248 | "\t", |
| 249 | "\n", |
| 250 | ], |
| 251 | string_prop = "a\t\n\r", |
| 252 | )`, |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 253 | }, |
| 254 | }, |
| 255 | { |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 256 | blueprint: `custom { |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 257 | name: "has_dep", |
| 258 | arch_paths: [":dep"], |
| 259 | bazel_module: { bp2build_available: true }, |
| 260 | } |
| 261 | |
| 262 | custom { |
| 263 | name: "dep", |
| 264 | arch_paths: ["abc"], |
| 265 | bazel_module: { bp2build_available: true }, |
| 266 | }`, |
| 267 | expectedBazelTargets: []string{`custom( |
| 268 | name = "dep", |
| 269 | arch_paths = ["abc"], |
| 270 | )`, |
| 271 | `custom( |
| 272 | name = "has_dep", |
| 273 | arch_paths = [":dep"], |
| 274 | )`, |
| 275 | }, |
| 276 | }, |
| 277 | { |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 278 | blueprint: `custom { |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 279 | name: "arch_paths", |
| 280 | arch: { |
| 281 | x86: { |
| 282 | arch_paths: ["abc"], |
| 283 | }, |
| 284 | }, |
| 285 | bazel_module: { bp2build_available: true }, |
| 286 | }`, |
| 287 | expectedBazelTargets: []string{`custom( |
| 288 | name = "arch_paths", |
| 289 | arch_paths = select({ |
| 290 | "//build/bazel/platforms/arch:x86": ["abc"], |
| 291 | "//conditions:default": [], |
| 292 | }), |
| 293 | )`, |
| 294 | }, |
| 295 | }, |
| 296 | { |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 297 | blueprint: `custom { |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 298 | name: "has_dep", |
| 299 | arch: { |
| 300 | x86: { |
| 301 | arch_paths: [":dep"], |
| 302 | }, |
| 303 | }, |
| 304 | bazel_module: { bp2build_available: true }, |
| 305 | } |
| 306 | |
| 307 | custom { |
| 308 | name: "dep", |
| 309 | arch_paths: ["abc"], |
| 310 | bazel_module: { bp2build_available: true }, |
| 311 | }`, |
| 312 | expectedBazelTargets: []string{`custom( |
| 313 | name = "dep", |
| 314 | arch_paths = ["abc"], |
| 315 | )`, |
| 316 | `custom( |
| 317 | name = "has_dep", |
| 318 | arch_paths = select({ |
| 319 | "//build/bazel/platforms/arch:x86": [":dep"], |
| 320 | "//conditions:default": [], |
| 321 | }), |
| 322 | )`, |
| 323 | }, |
Jingwen Chen | 58a12b8 | 2021-03-30 13:08:36 +0000 | [diff] [blame] | 324 | }, |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | dir := "." |
| 328 | for _, testCase := range testCases { |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 329 | config := android.TestConfig(buildDir, nil, testCase.blueprint, nil) |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 330 | ctx := android.NewTestContext(config) |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 331 | |
Liz Kammer | 32b77cf | 2021-08-04 15:17:02 -0400 | [diff] [blame] | 332 | registerCustomModuleForBp2buildConversion(ctx) |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 333 | |
| 334 | _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 335 | if errored(t, testCase, errs) { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 336 | continue |
| 337 | } |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 338 | _, errs = ctx.ResolveDependencies(config) |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 339 | if errored(t, testCase, errs) { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 340 | continue |
| 341 | } |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 342 | |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 343 | codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build) |
Jingwen Chen | ba369ad | 2021-02-22 10:19:34 -0500 | [diff] [blame] | 344 | bazelTargets := generateBazelTargetsForDir(codegenCtx, dir) |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 345 | |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 346 | if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 347 | t.Errorf("Expected %d bazel target, got %d", expectedCount, actualCount) |
| 348 | } else { |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 349 | for i, expectedBazelTarget := range testCase.expectedBazelTargets { |
| 350 | actualBazelTarget := bazelTargets[i] |
| 351 | if actualBazelTarget.content != expectedBazelTarget { |
| 352 | t.Errorf( |
| 353 | "Expected generated Bazel target to be '%s', got '%s'", |
| 354 | expectedBazelTarget, |
| 355 | actualBazelTarget.content, |
| 356 | ) |
| 357 | } |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 358 | } |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 359 | } |
| 360 | } |
| 361 | } |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 362 | |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 363 | func TestLoadStatements(t *testing.T) { |
| 364 | testCases := []struct { |
| 365 | bazelTargets BazelTargets |
| 366 | expectedLoadStatements string |
| 367 | }{ |
| 368 | { |
| 369 | bazelTargets: BazelTargets{ |
| 370 | BazelTarget{ |
| 371 | name: "foo", |
| 372 | ruleClass: "cc_library", |
| 373 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 374 | }, |
| 375 | }, |
| 376 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`, |
| 377 | }, |
| 378 | { |
| 379 | bazelTargets: BazelTargets{ |
| 380 | BazelTarget{ |
| 381 | name: "foo", |
| 382 | ruleClass: "cc_library", |
| 383 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 384 | }, |
| 385 | BazelTarget{ |
| 386 | name: "bar", |
| 387 | ruleClass: "cc_library", |
| 388 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 389 | }, |
| 390 | }, |
| 391 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`, |
| 392 | }, |
| 393 | { |
| 394 | bazelTargets: BazelTargets{ |
| 395 | BazelTarget{ |
| 396 | name: "foo", |
| 397 | ruleClass: "cc_library", |
| 398 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 399 | }, |
| 400 | BazelTarget{ |
| 401 | name: "bar", |
| 402 | ruleClass: "cc_binary", |
| 403 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 404 | }, |
| 405 | }, |
| 406 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")`, |
| 407 | }, |
| 408 | { |
| 409 | bazelTargets: BazelTargets{ |
| 410 | BazelTarget{ |
| 411 | name: "foo", |
| 412 | ruleClass: "cc_library", |
| 413 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 414 | }, |
| 415 | BazelTarget{ |
| 416 | name: "bar", |
| 417 | ruleClass: "cc_binary", |
| 418 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 419 | }, |
| 420 | BazelTarget{ |
| 421 | name: "baz", |
| 422 | ruleClass: "java_binary", |
| 423 | bzlLoadLocation: "//build/bazel/rules:java.bzl", |
| 424 | }, |
| 425 | }, |
| 426 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library") |
| 427 | load("//build/bazel/rules:java.bzl", "java_binary")`, |
| 428 | }, |
| 429 | { |
| 430 | bazelTargets: BazelTargets{ |
| 431 | BazelTarget{ |
| 432 | name: "foo", |
| 433 | ruleClass: "cc_binary", |
| 434 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 435 | }, |
| 436 | BazelTarget{ |
| 437 | name: "bar", |
| 438 | ruleClass: "java_binary", |
| 439 | bzlLoadLocation: "//build/bazel/rules:java.bzl", |
| 440 | }, |
| 441 | BazelTarget{ |
| 442 | name: "baz", |
| 443 | ruleClass: "genrule", |
| 444 | // Note: no bzlLoadLocation for native rules |
| 445 | }, |
| 446 | }, |
| 447 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary") |
| 448 | load("//build/bazel/rules:java.bzl", "java_binary")`, |
| 449 | }, |
| 450 | } |
| 451 | |
| 452 | for _, testCase := range testCases { |
| 453 | actual := testCase.bazelTargets.LoadStatements() |
| 454 | expected := testCase.expectedLoadStatements |
| 455 | if actual != expected { |
| 456 | t.Fatalf("Expected load statements to be %s, got %s", expected, actual) |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | } |
| 461 | |
| 462 | func TestGenerateBazelTargetModules_OneToMany_LoadedFromStarlark(t *testing.T) { |
| 463 | testCases := []struct { |
| 464 | bp string |
| 465 | expectedBazelTarget string |
| 466 | expectedBazelTargetCount int |
| 467 | expectedLoadStatements string |
| 468 | }{ |
| 469 | { |
| 470 | bp: `custom { |
| 471 | name: "bar", |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 472 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 473 | }`, |
| 474 | expectedBazelTarget: `my_library( |
| 475 | name = "bar", |
| 476 | ) |
| 477 | |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 478 | proto_library( |
| 479 | name = "bar_proto_library_deps", |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 480 | ) |
| 481 | |
| 482 | my_proto_library( |
| 483 | name = "bar_my_proto_library_deps", |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 484 | )`, |
| 485 | expectedBazelTargetCount: 3, |
| 486 | expectedLoadStatements: `load("//build/bazel/rules:proto.bzl", "my_proto_library", "proto_library") |
| 487 | load("//build/bazel/rules:rules.bzl", "my_library")`, |
| 488 | }, |
| 489 | } |
| 490 | |
| 491 | dir := "." |
| 492 | for _, testCase := range testCases { |
| 493 | config := android.TestConfig(buildDir, nil, testCase.bp, nil) |
| 494 | ctx := android.NewTestContext(config) |
| 495 | ctx.RegisterModuleType("custom", customModuleFactory) |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 496 | ctx.RegisterBp2BuildMutator("custom", customBp2BuildMutatorFromStarlark) |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 497 | ctx.RegisterForBazelConversion() |
| 498 | |
| 499 | _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) |
| 500 | android.FailIfErrored(t, errs) |
| 501 | _, errs = ctx.ResolveDependencies(config) |
| 502 | android.FailIfErrored(t, errs) |
| 503 | |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 504 | codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build) |
Jingwen Chen | ba369ad | 2021-02-22 10:19:34 -0500 | [diff] [blame] | 505 | bazelTargets := generateBazelTargetsForDir(codegenCtx, dir) |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 506 | if actualCount := len(bazelTargets); actualCount != testCase.expectedBazelTargetCount { |
| 507 | t.Fatalf("Expected %d bazel target, got %d", testCase.expectedBazelTargetCount, actualCount) |
| 508 | } |
| 509 | |
| 510 | actualBazelTargets := bazelTargets.String() |
| 511 | if actualBazelTargets != testCase.expectedBazelTarget { |
| 512 | t.Errorf( |
| 513 | "Expected generated Bazel target to be '%s', got '%s'", |
| 514 | testCase.expectedBazelTarget, |
| 515 | actualBazelTargets, |
| 516 | ) |
| 517 | } |
| 518 | |
| 519 | actualLoadStatements := bazelTargets.LoadStatements() |
| 520 | if actualLoadStatements != testCase.expectedLoadStatements { |
| 521 | t.Errorf( |
| 522 | "Expected generated load statements to be '%s', got '%s'", |
| 523 | testCase.expectedLoadStatements, |
| 524 | actualLoadStatements, |
| 525 | ) |
| 526 | } |
| 527 | } |
| 528 | } |
| 529 | |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 530 | func TestModuleTypeBp2Build(t *testing.T) { |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 531 | testCases := []bp2buildTestCase{ |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 532 | { |
Liz Kammer | ebfcf67 | 2021-02-16 15:00:05 -0500 | [diff] [blame] | 533 | description: "filegroup with does not specify srcs", |
| 534 | moduleTypeUnderTest: "filegroup", |
| 535 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 536 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 537 | blueprint: `filegroup { |
Liz Kammer | ebfcf67 | 2021-02-16 15:00:05 -0500 | [diff] [blame] | 538 | name: "fg_foo", |
| 539 | bazel_module: { bp2build_available: true }, |
| 540 | }`, |
| 541 | expectedBazelTargets: []string{ |
| 542 | `filegroup( |
| 543 | name = "fg_foo", |
| 544 | )`, |
| 545 | }, |
| 546 | }, |
| 547 | { |
Jingwen Chen | a42d641 | 2021-01-26 21:57:27 -0500 | [diff] [blame] | 548 | description: "filegroup with no srcs", |
| 549 | moduleTypeUnderTest: "filegroup", |
| 550 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 551 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 552 | blueprint: `filegroup { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 553 | name: "fg_foo", |
| 554 | srcs: [], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 555 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 556 | }`, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 557 | expectedBazelTargets: []string{ |
| 558 | `filegroup( |
| 559 | name = "fg_foo", |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 560 | )`, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 561 | }, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 562 | }, |
| 563 | { |
Jingwen Chen | a42d641 | 2021-01-26 21:57:27 -0500 | [diff] [blame] | 564 | description: "filegroup with 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: ["a", "b"], |
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{`filegroup( |
| 574 | name = "fg_foo", |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 575 | srcs = [ |
| 576 | "a", |
| 577 | "b", |
| 578 | ], |
| 579 | )`, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 580 | }, |
| 581 | }, |
| 582 | { |
| 583 | description: "filegroup with excludes srcs", |
| 584 | moduleTypeUnderTest: "filegroup", |
| 585 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 586 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 587 | blueprint: `filegroup { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 588 | name: "fg_foo", |
| 589 | srcs: ["a", "b"], |
| 590 | exclude_srcs: ["a"], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 591 | bazel_module: { bp2build_available: true }, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 592 | }`, |
| 593 | expectedBazelTargets: []string{`filegroup( |
| 594 | name = "fg_foo", |
Jingwen Chen | b4628eb | 2021-04-08 14:40:57 +0000 | [diff] [blame] | 595 | srcs = ["b"], |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 596 | )`, |
| 597 | }, |
| 598 | }, |
| 599 | { |
| 600 | description: "filegroup with glob", |
| 601 | moduleTypeUnderTest: "filegroup", |
| 602 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 603 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 604 | blueprint: `filegroup { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 605 | name: "foo", |
| 606 | srcs: ["**/*.txt"], |
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 = "foo", |
| 611 | srcs = [ |
| 612 | "other/a.txt", |
| 613 | "other/b.txt", |
| 614 | "other/subdir/a.txt", |
| 615 | ], |
| 616 | )`, |
| 617 | }, |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 618 | filesystem: map[string]string{ |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 619 | "other/a.txt": "", |
| 620 | "other/b.txt": "", |
| 621 | "other/subdir/a.txt": "", |
| 622 | "other/file": "", |
| 623 | }, |
| 624 | }, |
| 625 | { |
| 626 | description: "filegroup with glob in subdir", |
| 627 | moduleTypeUnderTest: "filegroup", |
| 628 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 629 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 630 | blueprint: `filegroup { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 631 | name: "foo", |
| 632 | srcs: ["a.txt"], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 633 | bazel_module: { bp2build_available: true }, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 634 | }`, |
| 635 | dir: "other", |
| 636 | expectedBazelTargets: []string{`filegroup( |
| 637 | name = "fg_foo", |
| 638 | srcs = [ |
| 639 | "a.txt", |
| 640 | "b.txt", |
| 641 | "subdir/a.txt", |
| 642 | ], |
| 643 | )`, |
| 644 | }, |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 645 | filesystem: map[string]string{ |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 646 | "other/Android.bp": `filegroup { |
| 647 | name: "fg_foo", |
| 648 | srcs: ["**/*.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 | "other/a.txt": "", |
| 652 | "other/b.txt": "", |
| 653 | "other/subdir/a.txt": "", |
| 654 | "other/file": "", |
| 655 | }, |
| 656 | }, |
| 657 | { |
| 658 | description: "depends_on_other_dir_module", |
| 659 | moduleTypeUnderTest: "filegroup", |
| 660 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 661 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 662 | blueprint: `filegroup { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 663 | name: "foobar", |
| 664 | srcs: [ |
Jingwen Chen | b4628eb | 2021-04-08 14:40:57 +0000 | [diff] [blame] | 665 | ":foo", |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 666 | "c", |
| 667 | ], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 668 | bazel_module: { bp2build_available: true }, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 669 | }`, |
| 670 | expectedBazelTargets: []string{`filegroup( |
| 671 | name = "foobar", |
| 672 | srcs = [ |
| 673 | "//other:foo", |
| 674 | "c", |
| 675 | ], |
| 676 | )`, |
| 677 | }, |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 678 | filesystem: map[string]string{ |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 679 | "other/Android.bp": `filegroup { |
| 680 | name: "foo", |
| 681 | srcs: ["a", "b"], |
| 682 | }`, |
| 683 | }, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 684 | }, |
| 685 | } |
| 686 | |
| 687 | dir := "." |
| 688 | for _, testCase := range testCases { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 689 | fs := make(map[string][]byte) |
| 690 | toParse := []string{ |
| 691 | "Android.bp", |
| 692 | } |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 693 | for f, content := range testCase.filesystem { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 694 | if strings.HasSuffix(f, "Android.bp") { |
| 695 | toParse = append(toParse, f) |
| 696 | } |
| 697 | fs[f] = []byte(content) |
| 698 | } |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 699 | config := android.TestConfig(buildDir, nil, testCase.blueprint, fs) |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 700 | ctx := android.NewTestContext(config) |
| 701 | ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory) |
Jingwen Chen | a42d641 | 2021-01-26 21:57:27 -0500 | [diff] [blame] | 702 | ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator) |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 703 | ctx.RegisterForBazelConversion() |
| 704 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 705 | _, errs := ctx.ParseFileList(dir, toParse) |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 706 | if errored(t, testCase, errs) { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 707 | continue |
| 708 | } |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 709 | _, errs = ctx.ResolveDependencies(config) |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 710 | if errored(t, testCase, errs) { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 711 | continue |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 712 | } |
| 713 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 714 | checkDir := dir |
| 715 | if testCase.dir != "" { |
| 716 | checkDir = testCase.dir |
| 717 | } |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 718 | |
| 719 | codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build) |
Jingwen Chen | ba369ad | 2021-02-22 10:19:34 -0500 | [diff] [blame] | 720 | bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir) |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 721 | if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount { |
| 722 | t.Errorf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount) |
| 723 | } else { |
| 724 | for i, target := range bazelTargets { |
| 725 | if w, g := testCase.expectedBazelTargets[i], target.content; w != g { |
| 726 | t.Errorf( |
| 727 | "%s: Expected generated Bazel target to be '%s', got '%s'", |
| 728 | testCase.description, |
| 729 | w, |
| 730 | g, |
| 731 | ) |
| 732 | } |
| 733 | } |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 734 | } |
| 735 | } |
| 736 | } |
Jingwen Chen | 041b184 | 2021-02-01 00:23:25 -0500 | [diff] [blame] | 737 | |
| 738 | type bp2buildMutator = func(android.TopDownMutatorContext) |
| 739 | |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 740 | func TestAllowlistingBp2buildTargetsExplicitly(t *testing.T) { |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 741 | testCases := []struct { |
| 742 | moduleTypeUnderTest string |
| 743 | moduleTypeUnderTestFactory android.ModuleFactory |
| 744 | moduleTypeUnderTestBp2BuildMutator bp2buildMutator |
| 745 | bp string |
| 746 | expectedCount int |
| 747 | description string |
| 748 | }{ |
| 749 | { |
| 750 | description: "explicitly unavailable", |
| 751 | moduleTypeUnderTest: "filegroup", |
| 752 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 753 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
| 754 | bp: `filegroup { |
| 755 | name: "foo", |
| 756 | srcs: ["a", "b"], |
| 757 | bazel_module: { bp2build_available: false }, |
| 758 | }`, |
| 759 | expectedCount: 0, |
| 760 | }, |
| 761 | { |
| 762 | description: "implicitly unavailable", |
| 763 | moduleTypeUnderTest: "filegroup", |
| 764 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 765 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
| 766 | bp: `filegroup { |
| 767 | name: "foo", |
| 768 | srcs: ["a", "b"], |
| 769 | }`, |
| 770 | expectedCount: 0, |
| 771 | }, |
| 772 | { |
| 773 | description: "explicitly available", |
| 774 | moduleTypeUnderTest: "filegroup", |
| 775 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 776 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
| 777 | bp: `filegroup { |
| 778 | name: "foo", |
| 779 | srcs: ["a", "b"], |
| 780 | bazel_module: { bp2build_available: true }, |
| 781 | }`, |
| 782 | expectedCount: 1, |
| 783 | }, |
| 784 | { |
| 785 | description: "generates more than 1 target if needed", |
| 786 | moduleTypeUnderTest: "custom", |
| 787 | moduleTypeUnderTestFactory: customModuleFactory, |
| 788 | moduleTypeUnderTestBp2BuildMutator: customBp2BuildMutatorFromStarlark, |
| 789 | bp: `custom { |
| 790 | name: "foo", |
| 791 | bazel_module: { bp2build_available: true }, |
| 792 | }`, |
| 793 | expectedCount: 3, |
| 794 | }, |
| 795 | } |
| 796 | |
| 797 | dir := "." |
| 798 | for _, testCase := range testCases { |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 799 | t.Run(testCase.description, func(t *testing.T) { |
| 800 | config := android.TestConfig(buildDir, nil, testCase.bp, nil) |
| 801 | ctx := android.NewTestContext(config) |
| 802 | ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory) |
| 803 | ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator) |
| 804 | ctx.RegisterForBazelConversion() |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 805 | |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 806 | _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) |
| 807 | android.FailIfErrored(t, errs) |
| 808 | _, errs = ctx.ResolveDependencies(config) |
| 809 | android.FailIfErrored(t, errs) |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 810 | |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 811 | codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build) |
| 812 | bazelTargets := generateBazelTargetsForDir(codegenCtx, dir) |
| 813 | if actualCount := len(bazelTargets); actualCount != testCase.expectedCount { |
| 814 | t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, testCase.expectedCount, actualCount) |
| 815 | } |
| 816 | }) |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 817 | } |
| 818 | } |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 819 | |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 820 | func TestAllowlistingBp2buildTargetsWithConfig(t *testing.T) { |
| 821 | testCases := []struct { |
| 822 | moduleTypeUnderTest string |
| 823 | moduleTypeUnderTestFactory android.ModuleFactory |
| 824 | moduleTypeUnderTestBp2BuildMutator bp2buildMutator |
| 825 | expectedCount map[string]int |
| 826 | description string |
| 827 | bp2buildConfig android.Bp2BuildConfig |
| 828 | checkDir string |
| 829 | fs map[string]string |
| 830 | }{ |
| 831 | { |
| 832 | description: "test bp2build config package and subpackages config", |
| 833 | moduleTypeUnderTest: "filegroup", |
| 834 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 835 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
| 836 | expectedCount: map[string]int{ |
| 837 | "migrated": 1, |
| 838 | "migrated/but_not_really": 0, |
| 839 | "migrated/but_not_really/but_really": 1, |
| 840 | "not_migrated": 0, |
| 841 | "also_not_migrated": 0, |
| 842 | }, |
| 843 | bp2buildConfig: android.Bp2BuildConfig{ |
| 844 | "migrated": android.Bp2BuildDefaultTrueRecursively, |
| 845 | "migrated/but_not_really": android.Bp2BuildDefaultFalse, |
| 846 | "not_migrated": android.Bp2BuildDefaultFalse, |
| 847 | }, |
| 848 | fs: map[string]string{ |
| 849 | "migrated/Android.bp": `filegroup { name: "a" }`, |
| 850 | "migrated/but_not_really/Android.bp": `filegroup { name: "b" }`, |
| 851 | "migrated/but_not_really/but_really/Android.bp": `filegroup { name: "c" }`, |
| 852 | "not_migrated/Android.bp": `filegroup { name: "d" }`, |
| 853 | "also_not_migrated/Android.bp": `filegroup { name: "e" }`, |
| 854 | }, |
| 855 | }, |
| 856 | { |
| 857 | description: "test bp2build config opt-in and opt-out", |
| 858 | moduleTypeUnderTest: "filegroup", |
| 859 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 860 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
| 861 | expectedCount: map[string]int{ |
| 862 | "package-opt-in": 2, |
| 863 | "package-opt-in/subpackage": 0, |
| 864 | "package-opt-out": 1, |
| 865 | "package-opt-out/subpackage": 0, |
| 866 | }, |
| 867 | bp2buildConfig: android.Bp2BuildConfig{ |
| 868 | "package-opt-in": android.Bp2BuildDefaultFalse, |
| 869 | "package-opt-out": android.Bp2BuildDefaultTrueRecursively, |
| 870 | }, |
| 871 | fs: map[string]string{ |
| 872 | "package-opt-in/Android.bp": ` |
| 873 | filegroup { name: "opt-in-a" } |
| 874 | filegroup { name: "opt-in-b", bazel_module: { bp2build_available: true } } |
| 875 | filegroup { name: "opt-in-c", bazel_module: { bp2build_available: true } } |
| 876 | `, |
| 877 | |
| 878 | "package-opt-in/subpackage/Android.bp": ` |
| 879 | filegroup { name: "opt-in-d" } // parent package not configured to DefaultTrueRecursively |
| 880 | `, |
| 881 | |
| 882 | "package-opt-out/Android.bp": ` |
| 883 | filegroup { name: "opt-out-a" } |
| 884 | filegroup { name: "opt-out-b", bazel_module: { bp2build_available: false } } |
| 885 | filegroup { name: "opt-out-c", bazel_module: { bp2build_available: false } } |
| 886 | `, |
| 887 | |
| 888 | "package-opt-out/subpackage/Android.bp": ` |
| 889 | filegroup { name: "opt-out-g", bazel_module: { bp2build_available: false } } |
| 890 | filegroup { name: "opt-out-h", bazel_module: { bp2build_available: false } } |
| 891 | `, |
| 892 | }, |
| 893 | }, |
| 894 | } |
| 895 | |
| 896 | dir := "." |
| 897 | for _, testCase := range testCases { |
| 898 | fs := make(map[string][]byte) |
| 899 | toParse := []string{ |
| 900 | "Android.bp", |
| 901 | } |
| 902 | for f, content := range testCase.fs { |
| 903 | if strings.HasSuffix(f, "Android.bp") { |
| 904 | toParse = append(toParse, f) |
| 905 | } |
| 906 | fs[f] = []byte(content) |
| 907 | } |
| 908 | config := android.TestConfig(buildDir, nil, "", fs) |
| 909 | ctx := android.NewTestContext(config) |
| 910 | ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory) |
| 911 | ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator) |
| 912 | ctx.RegisterBp2BuildConfig(testCase.bp2buildConfig) |
| 913 | ctx.RegisterForBazelConversion() |
| 914 | |
| 915 | _, errs := ctx.ParseFileList(dir, toParse) |
| 916 | android.FailIfErrored(t, errs) |
| 917 | _, errs = ctx.ResolveDependencies(config) |
| 918 | android.FailIfErrored(t, errs) |
| 919 | |
| 920 | codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build) |
| 921 | |
| 922 | // For each directory, test that the expected number of generated targets is correct. |
| 923 | for dir, expectedCount := range testCase.expectedCount { |
| 924 | bazelTargets := generateBazelTargetsForDir(codegenCtx, dir) |
| 925 | if actualCount := len(bazelTargets); actualCount != expectedCount { |
| 926 | t.Fatalf( |
| 927 | "%s: Expected %d bazel target for %s package, got %d", |
| 928 | testCase.description, |
| 929 | expectedCount, |
| 930 | dir, |
| 931 | actualCount) |
| 932 | } |
| 933 | |
| 934 | } |
| 935 | } |
| 936 | } |
| 937 | |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 938 | func TestCombineBuildFilesBp2buildTargets(t *testing.T) { |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 939 | testCases := []bp2buildTestCase{ |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 940 | { |
| 941 | description: "filegroup bazel_module.label", |
| 942 | moduleTypeUnderTest: "filegroup", |
| 943 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 944 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 945 | blueprint: `filegroup { |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 946 | name: "fg_foo", |
| 947 | bazel_module: { label: "//other:fg_foo" }, |
| 948 | }`, |
| 949 | expectedBazelTargets: []string{ |
| 950 | `// BUILD file`, |
| 951 | }, |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 952 | filesystem: map[string]string{ |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 953 | "other/BUILD.bazel": `// BUILD file`, |
| 954 | }, |
| 955 | }, |
| 956 | { |
| 957 | description: "multiple bazel_module.label same BUILD", |
| 958 | moduleTypeUnderTest: "filegroup", |
| 959 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 960 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 961 | blueprint: `filegroup { |
Jingwen Chen | c63677b | 2021-06-17 05:43:19 +0000 | [diff] [blame] | 962 | name: "fg_foo", |
| 963 | bazel_module: { label: "//other:fg_foo" }, |
| 964 | } |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 965 | |
Jingwen Chen | c63677b | 2021-06-17 05:43:19 +0000 | [diff] [blame] | 966 | filegroup { |
| 967 | name: "foo", |
| 968 | bazel_module: { label: "//other:foo" }, |
| 969 | }`, |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 970 | expectedBazelTargets: []string{ |
| 971 | `// BUILD file`, |
| 972 | }, |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 973 | filesystem: map[string]string{ |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 974 | "other/BUILD.bazel": `// BUILD file`, |
| 975 | }, |
| 976 | }, |
| 977 | { |
Jingwen Chen | c63677b | 2021-06-17 05:43:19 +0000 | [diff] [blame] | 978 | description: "filegroup bazel_module.label and bp2build in subdir", |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 979 | moduleTypeUnderTest: "filegroup", |
| 980 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 981 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
Jingwen Chen | c63677b | 2021-06-17 05:43:19 +0000 | [diff] [blame] | 982 | dir: "other", |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 983 | blueprint: ``, |
| 984 | filesystem: map[string]string{ |
Jingwen Chen | c63677b | 2021-06-17 05:43:19 +0000 | [diff] [blame] | 985 | "other/Android.bp": `filegroup { |
| 986 | name: "fg_foo", |
| 987 | bazel_module: { |
| 988 | bp2build_available: true, |
| 989 | }, |
| 990 | } |
| 991 | filegroup { |
| 992 | name: "fg_bar", |
| 993 | bazel_module: { |
| 994 | label: "//other:fg_bar" |
| 995 | }, |
| 996 | }`, |
| 997 | "other/BUILD.bazel": `// definition for fg_bar`, |
| 998 | }, |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 999 | expectedBazelTargets: []string{ |
| 1000 | `filegroup( |
| 1001 | name = "fg_foo", |
Jingwen Chen | c63677b | 2021-06-17 05:43:19 +0000 | [diff] [blame] | 1002 | )`, `// definition for fg_bar`, |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1003 | }, |
| 1004 | }, |
| 1005 | { |
| 1006 | description: "filegroup bazel_module.label and filegroup bp2build", |
| 1007 | moduleTypeUnderTest: "filegroup", |
| 1008 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 1009 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 1010 | blueprint: `filegroup { |
Jingwen Chen | c63677b | 2021-06-17 05:43:19 +0000 | [diff] [blame] | 1011 | name: "fg_foo", |
| 1012 | bazel_module: { |
| 1013 | label: "//other:fg_foo", |
| 1014 | }, |
| 1015 | } |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1016 | |
Jingwen Chen | c63677b | 2021-06-17 05:43:19 +0000 | [diff] [blame] | 1017 | filegroup { |
| 1018 | name: "fg_bar", |
| 1019 | bazel_module: { |
| 1020 | bp2build_available: true, |
| 1021 | }, |
| 1022 | }`, |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1023 | expectedBazelTargets: []string{ |
| 1024 | `filegroup( |
| 1025 | name = "fg_bar", |
| 1026 | )`, |
| 1027 | `// BUILD file`, |
| 1028 | }, |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 1029 | filesystem: map[string]string{ |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1030 | "other/BUILD.bazel": `// BUILD file`, |
| 1031 | }, |
| 1032 | }, |
| 1033 | } |
| 1034 | |
| 1035 | dir := "." |
| 1036 | for _, testCase := range testCases { |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1037 | t.Run(testCase.description, func(t *testing.T) { |
| 1038 | fs := make(map[string][]byte) |
| 1039 | toParse := []string{ |
| 1040 | "Android.bp", |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1041 | } |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 1042 | for f, content := range testCase.filesystem { |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1043 | if strings.HasSuffix(f, "Android.bp") { |
| 1044 | toParse = append(toParse, f) |
| 1045 | } |
| 1046 | fs[f] = []byte(content) |
| 1047 | } |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 1048 | config := android.TestConfig(buildDir, nil, testCase.blueprint, fs) |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1049 | ctx := android.NewTestContext(config) |
| 1050 | ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory) |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1051 | ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator) |
| 1052 | ctx.RegisterForBazelConversion() |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1053 | |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1054 | _, errs := ctx.ParseFileList(dir, toParse) |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 1055 | if errored(t, testCase, errs) { |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1056 | return |
| 1057 | } |
| 1058 | _, errs = ctx.ResolveDependencies(config) |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 1059 | if errored(t, testCase, errs) { |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1060 | return |
| 1061 | } |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1062 | |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1063 | checkDir := dir |
| 1064 | if testCase.dir != "" { |
| 1065 | checkDir = testCase.dir |
| 1066 | } |
| 1067 | bazelTargets := generateBazelTargetsForDir(NewCodegenContext(config, *ctx.Context, Bp2Build), checkDir) |
| 1068 | bazelTargets.sort() |
| 1069 | actualCount := len(bazelTargets) |
| 1070 | expectedCount := len(testCase.expectedBazelTargets) |
| 1071 | if actualCount != expectedCount { |
| 1072 | t.Errorf("Expected %d bazel target, got %d\n%s", expectedCount, actualCount, bazelTargets) |
| 1073 | } |
| 1074 | if !strings.Contains(bazelTargets.String(), "Section: Handcrafted targets. ") { |
| 1075 | t.Errorf("Expected string representation of bazelTargets to contain handcrafted section header.") |
| 1076 | } |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1077 | for i, target := range bazelTargets { |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1078 | actualContent := target.content |
| 1079 | expectedContent := testCase.expectedBazelTargets[i] |
| 1080 | if expectedContent != actualContent { |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1081 | t.Errorf( |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1082 | "Expected generated Bazel target to be '%s', got '%s'", |
| 1083 | expectedContent, |
| 1084 | actualContent, |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1085 | ) |
| 1086 | } |
| 1087 | } |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1088 | }) |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1089 | } |
| 1090 | } |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1091 | |
| 1092 | func TestGlobExcludeSrcs(t *testing.T) { |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 1093 | testCases := []bp2buildTestCase{ |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1094 | { |
| 1095 | description: "filegroup top level exclude_srcs", |
| 1096 | moduleTypeUnderTest: "filegroup", |
| 1097 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 1098 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 1099 | blueprint: `filegroup { |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1100 | name: "fg_foo", |
| 1101 | srcs: ["**/*.txt"], |
| 1102 | exclude_srcs: ["c.txt"], |
| 1103 | bazel_module: { bp2build_available: true }, |
| 1104 | }`, |
| 1105 | expectedBazelTargets: []string{`filegroup( |
| 1106 | name = "fg_foo", |
| 1107 | srcs = [ |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1108 | "a.txt", |
| 1109 | "b.txt", |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 1110 | "//dir:e.txt", |
| 1111 | "//dir:f.txt", |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1112 | ], |
| 1113 | )`, |
| 1114 | }, |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 1115 | filesystem: map[string]string{ |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1116 | "a.txt": "", |
| 1117 | "b.txt": "", |
| 1118 | "c.txt": "", |
| 1119 | "dir/Android.bp": "", |
| 1120 | "dir/e.txt": "", |
| 1121 | "dir/f.txt": "", |
| 1122 | }, |
| 1123 | }, |
| 1124 | { |
| 1125 | description: "filegroup in subdir exclude_srcs", |
| 1126 | moduleTypeUnderTest: "filegroup", |
| 1127 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 1128 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 1129 | blueprint: "", |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1130 | dir: "dir", |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 1131 | filesystem: map[string]string{ |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1132 | "dir/Android.bp": `filegroup { |
| 1133 | name: "fg_foo", |
| 1134 | srcs: ["**/*.txt"], |
| 1135 | exclude_srcs: ["b.txt"], |
| 1136 | bazel_module: { bp2build_available: true }, |
| 1137 | } |
| 1138 | `, |
| 1139 | "dir/a.txt": "", |
| 1140 | "dir/b.txt": "", |
| 1141 | "dir/subdir/Android.bp": "", |
| 1142 | "dir/subdir/e.txt": "", |
| 1143 | "dir/subdir/f.txt": "", |
| 1144 | }, |
| 1145 | expectedBazelTargets: []string{`filegroup( |
| 1146 | name = "fg_foo", |
| 1147 | srcs = [ |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 1148 | "a.txt", |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1149 | "//dir/subdir:e.txt", |
| 1150 | "//dir/subdir:f.txt", |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1151 | ], |
| 1152 | )`, |
| 1153 | }, |
| 1154 | }, |
| 1155 | } |
| 1156 | |
| 1157 | dir := "." |
| 1158 | for _, testCase := range testCases { |
| 1159 | fs := make(map[string][]byte) |
| 1160 | toParse := []string{ |
| 1161 | "Android.bp", |
| 1162 | } |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 1163 | for f, content := range testCase.filesystem { |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1164 | if strings.HasSuffix(f, "Android.bp") { |
| 1165 | toParse = append(toParse, f) |
| 1166 | } |
| 1167 | fs[f] = []byte(content) |
| 1168 | } |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 1169 | config := android.TestConfig(buildDir, nil, testCase.blueprint, fs) |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1170 | ctx := android.NewTestContext(config) |
| 1171 | ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory) |
| 1172 | ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator) |
| 1173 | ctx.RegisterForBazelConversion() |
| 1174 | |
| 1175 | _, errs := ctx.ParseFileList(dir, toParse) |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 1176 | if errored(t, testCase, errs) { |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1177 | continue |
| 1178 | } |
| 1179 | _, errs = ctx.ResolveDependencies(config) |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 1180 | if errored(t, testCase, errs) { |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1181 | continue |
| 1182 | } |
| 1183 | |
| 1184 | checkDir := dir |
| 1185 | if testCase.dir != "" { |
| 1186 | checkDir = testCase.dir |
| 1187 | } |
| 1188 | bazelTargets := generateBazelTargetsForDir(NewCodegenContext(config, *ctx.Context, Bp2Build), checkDir) |
| 1189 | if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount { |
| 1190 | t.Errorf("%s: Expected %d bazel target, got %d\n%s", testCase.description, expectedCount, actualCount, bazelTargets) |
| 1191 | } else { |
| 1192 | for i, target := range bazelTargets { |
| 1193 | if w, g := testCase.expectedBazelTargets[i], target.content; w != g { |
| 1194 | t.Errorf( |
| 1195 | "%s: Expected generated Bazel target to be '%s', got '%s'", |
| 1196 | testCase.description, |
| 1197 | w, |
| 1198 | g, |
| 1199 | ) |
| 1200 | } |
| 1201 | } |
| 1202 | } |
| 1203 | } |
| 1204 | } |