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 | }, |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame^] | 325 | { |
| 326 | blueprint: `custom { |
| 327 | name: "embedded_props", |
| 328 | embedded_prop: "abc", |
| 329 | bazel_module: { bp2build_available: true }, |
| 330 | }`, |
| 331 | expectedBazelTargets: []string{`custom( |
| 332 | name = "embedded_props", |
| 333 | embedded_attr = "abc", |
| 334 | )`, |
| 335 | }, |
| 336 | }, |
| 337 | { |
| 338 | blueprint: `custom { |
| 339 | name: "ptr_to_embedded_props", |
| 340 | other_embedded_prop: "abc", |
| 341 | bazel_module: { bp2build_available: true }, |
| 342 | }`, |
| 343 | expectedBazelTargets: []string{`custom( |
| 344 | name = "ptr_to_embedded_props", |
| 345 | other_embedded_attr = "abc", |
| 346 | )`, |
| 347 | }, |
| 348 | }, |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | dir := "." |
| 352 | for _, testCase := range testCases { |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 353 | config := android.TestConfig(buildDir, nil, testCase.blueprint, nil) |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 354 | ctx := android.NewTestContext(config) |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 355 | |
Liz Kammer | 32b77cf | 2021-08-04 15:17:02 -0400 | [diff] [blame] | 356 | registerCustomModuleForBp2buildConversion(ctx) |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 357 | |
| 358 | _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 359 | if errored(t, testCase, errs) { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 360 | continue |
| 361 | } |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 362 | _, errs = ctx.ResolveDependencies(config) |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 363 | if errored(t, testCase, errs) { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 364 | continue |
| 365 | } |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 366 | |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 367 | codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build) |
Jingwen Chen | ba369ad | 2021-02-22 10:19:34 -0500 | [diff] [blame] | 368 | bazelTargets := generateBazelTargetsForDir(codegenCtx, dir) |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 369 | |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 370 | if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 371 | t.Errorf("Expected %d bazel target, got %d", expectedCount, actualCount) |
| 372 | } else { |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 373 | for i, expectedBazelTarget := range testCase.expectedBazelTargets { |
| 374 | actualBazelTarget := bazelTargets[i] |
| 375 | if actualBazelTarget.content != expectedBazelTarget { |
| 376 | t.Errorf( |
| 377 | "Expected generated Bazel target to be '%s', got '%s'", |
| 378 | expectedBazelTarget, |
| 379 | actualBazelTarget.content, |
| 380 | ) |
| 381 | } |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 382 | } |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | } |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 386 | |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 387 | func TestLoadStatements(t *testing.T) { |
| 388 | testCases := []struct { |
| 389 | bazelTargets BazelTargets |
| 390 | expectedLoadStatements string |
| 391 | }{ |
| 392 | { |
| 393 | bazelTargets: BazelTargets{ |
| 394 | BazelTarget{ |
| 395 | name: "foo", |
| 396 | ruleClass: "cc_library", |
| 397 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 398 | }, |
| 399 | }, |
| 400 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`, |
| 401 | }, |
| 402 | { |
| 403 | bazelTargets: BazelTargets{ |
| 404 | BazelTarget{ |
| 405 | name: "foo", |
| 406 | ruleClass: "cc_library", |
| 407 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 408 | }, |
| 409 | BazelTarget{ |
| 410 | name: "bar", |
| 411 | ruleClass: "cc_library", |
| 412 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 413 | }, |
| 414 | }, |
| 415 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`, |
| 416 | }, |
| 417 | { |
| 418 | bazelTargets: BazelTargets{ |
| 419 | BazelTarget{ |
| 420 | name: "foo", |
| 421 | ruleClass: "cc_library", |
| 422 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 423 | }, |
| 424 | BazelTarget{ |
| 425 | name: "bar", |
| 426 | ruleClass: "cc_binary", |
| 427 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 428 | }, |
| 429 | }, |
| 430 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")`, |
| 431 | }, |
| 432 | { |
| 433 | bazelTargets: BazelTargets{ |
| 434 | BazelTarget{ |
| 435 | name: "foo", |
| 436 | ruleClass: "cc_library", |
| 437 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 438 | }, |
| 439 | BazelTarget{ |
| 440 | name: "bar", |
| 441 | ruleClass: "cc_binary", |
| 442 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 443 | }, |
| 444 | BazelTarget{ |
| 445 | name: "baz", |
| 446 | ruleClass: "java_binary", |
| 447 | bzlLoadLocation: "//build/bazel/rules:java.bzl", |
| 448 | }, |
| 449 | }, |
| 450 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library") |
| 451 | load("//build/bazel/rules:java.bzl", "java_binary")`, |
| 452 | }, |
| 453 | { |
| 454 | bazelTargets: BazelTargets{ |
| 455 | BazelTarget{ |
| 456 | name: "foo", |
| 457 | ruleClass: "cc_binary", |
| 458 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 459 | }, |
| 460 | BazelTarget{ |
| 461 | name: "bar", |
| 462 | ruleClass: "java_binary", |
| 463 | bzlLoadLocation: "//build/bazel/rules:java.bzl", |
| 464 | }, |
| 465 | BazelTarget{ |
| 466 | name: "baz", |
| 467 | ruleClass: "genrule", |
| 468 | // Note: no bzlLoadLocation for native rules |
| 469 | }, |
| 470 | }, |
| 471 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary") |
| 472 | load("//build/bazel/rules:java.bzl", "java_binary")`, |
| 473 | }, |
| 474 | } |
| 475 | |
| 476 | for _, testCase := range testCases { |
| 477 | actual := testCase.bazelTargets.LoadStatements() |
| 478 | expected := testCase.expectedLoadStatements |
| 479 | if actual != expected { |
| 480 | t.Fatalf("Expected load statements to be %s, got %s", expected, actual) |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | } |
| 485 | |
| 486 | func TestGenerateBazelTargetModules_OneToMany_LoadedFromStarlark(t *testing.T) { |
| 487 | testCases := []struct { |
| 488 | bp string |
| 489 | expectedBazelTarget string |
| 490 | expectedBazelTargetCount int |
| 491 | expectedLoadStatements string |
| 492 | }{ |
| 493 | { |
| 494 | bp: `custom { |
| 495 | name: "bar", |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 496 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 497 | }`, |
| 498 | expectedBazelTarget: `my_library( |
| 499 | name = "bar", |
| 500 | ) |
| 501 | |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 502 | proto_library( |
| 503 | name = "bar_proto_library_deps", |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 504 | ) |
| 505 | |
| 506 | my_proto_library( |
| 507 | name = "bar_my_proto_library_deps", |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 508 | )`, |
| 509 | expectedBazelTargetCount: 3, |
| 510 | expectedLoadStatements: `load("//build/bazel/rules:proto.bzl", "my_proto_library", "proto_library") |
| 511 | load("//build/bazel/rules:rules.bzl", "my_library")`, |
| 512 | }, |
| 513 | } |
| 514 | |
| 515 | dir := "." |
| 516 | for _, testCase := range testCases { |
| 517 | config := android.TestConfig(buildDir, nil, testCase.bp, nil) |
| 518 | ctx := android.NewTestContext(config) |
| 519 | ctx.RegisterModuleType("custom", customModuleFactory) |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 520 | ctx.RegisterBp2BuildMutator("custom", customBp2BuildMutatorFromStarlark) |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 521 | ctx.RegisterForBazelConversion() |
| 522 | |
| 523 | _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) |
| 524 | android.FailIfErrored(t, errs) |
| 525 | _, errs = ctx.ResolveDependencies(config) |
| 526 | android.FailIfErrored(t, errs) |
| 527 | |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 528 | codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build) |
Jingwen Chen | ba369ad | 2021-02-22 10:19:34 -0500 | [diff] [blame] | 529 | bazelTargets := generateBazelTargetsForDir(codegenCtx, dir) |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 530 | if actualCount := len(bazelTargets); actualCount != testCase.expectedBazelTargetCount { |
| 531 | t.Fatalf("Expected %d bazel target, got %d", testCase.expectedBazelTargetCount, actualCount) |
| 532 | } |
| 533 | |
| 534 | actualBazelTargets := bazelTargets.String() |
| 535 | if actualBazelTargets != testCase.expectedBazelTarget { |
| 536 | t.Errorf( |
| 537 | "Expected generated Bazel target to be '%s', got '%s'", |
| 538 | testCase.expectedBazelTarget, |
| 539 | actualBazelTargets, |
| 540 | ) |
| 541 | } |
| 542 | |
| 543 | actualLoadStatements := bazelTargets.LoadStatements() |
| 544 | if actualLoadStatements != testCase.expectedLoadStatements { |
| 545 | t.Errorf( |
| 546 | "Expected generated load statements to be '%s', got '%s'", |
| 547 | testCase.expectedLoadStatements, |
| 548 | actualLoadStatements, |
| 549 | ) |
| 550 | } |
| 551 | } |
| 552 | } |
| 553 | |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 554 | func TestModuleTypeBp2Build(t *testing.T) { |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 555 | testCases := []bp2buildTestCase{ |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 556 | { |
Liz Kammer | ebfcf67 | 2021-02-16 15:00:05 -0500 | [diff] [blame] | 557 | description: "filegroup with does not specify srcs", |
| 558 | moduleTypeUnderTest: "filegroup", |
| 559 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 560 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 561 | blueprint: `filegroup { |
Liz Kammer | ebfcf67 | 2021-02-16 15:00:05 -0500 | [diff] [blame] | 562 | name: "fg_foo", |
| 563 | bazel_module: { bp2build_available: true }, |
| 564 | }`, |
| 565 | expectedBazelTargets: []string{ |
| 566 | `filegroup( |
| 567 | name = "fg_foo", |
| 568 | )`, |
| 569 | }, |
| 570 | }, |
| 571 | { |
Jingwen Chen | a42d641 | 2021-01-26 21:57:27 -0500 | [diff] [blame] | 572 | description: "filegroup with no srcs", |
| 573 | moduleTypeUnderTest: "filegroup", |
| 574 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 575 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 576 | blueprint: `filegroup { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 577 | name: "fg_foo", |
| 578 | srcs: [], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 579 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 580 | }`, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 581 | expectedBazelTargets: []string{ |
| 582 | `filegroup( |
| 583 | name = "fg_foo", |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 584 | )`, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 585 | }, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 586 | }, |
| 587 | { |
Jingwen Chen | a42d641 | 2021-01-26 21:57:27 -0500 | [diff] [blame] | 588 | description: "filegroup with srcs", |
| 589 | moduleTypeUnderTest: "filegroup", |
| 590 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 591 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 592 | blueprint: `filegroup { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 593 | name: "fg_foo", |
| 594 | srcs: ["a", "b"], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 595 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 596 | }`, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 597 | expectedBazelTargets: []string{`filegroup( |
| 598 | name = "fg_foo", |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 599 | srcs = [ |
| 600 | "a", |
| 601 | "b", |
| 602 | ], |
| 603 | )`, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 604 | }, |
| 605 | }, |
| 606 | { |
| 607 | description: "filegroup with excludes srcs", |
| 608 | moduleTypeUnderTest: "filegroup", |
| 609 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 610 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 611 | blueprint: `filegroup { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 612 | name: "fg_foo", |
| 613 | srcs: ["a", "b"], |
| 614 | exclude_srcs: ["a"], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 615 | bazel_module: { bp2build_available: true }, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 616 | }`, |
| 617 | expectedBazelTargets: []string{`filegroup( |
| 618 | name = "fg_foo", |
Jingwen Chen | b4628eb | 2021-04-08 14:40:57 +0000 | [diff] [blame] | 619 | srcs = ["b"], |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 620 | )`, |
| 621 | }, |
| 622 | }, |
| 623 | { |
| 624 | description: "filegroup with glob", |
| 625 | moduleTypeUnderTest: "filegroup", |
| 626 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 627 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 628 | blueprint: `filegroup { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 629 | name: "foo", |
| 630 | srcs: ["**/*.txt"], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 631 | bazel_module: { bp2build_available: true }, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 632 | }`, |
| 633 | expectedBazelTargets: []string{`filegroup( |
| 634 | name = "foo", |
| 635 | srcs = [ |
| 636 | "other/a.txt", |
| 637 | "other/b.txt", |
| 638 | "other/subdir/a.txt", |
| 639 | ], |
| 640 | )`, |
| 641 | }, |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 642 | filesystem: map[string]string{ |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 643 | "other/a.txt": "", |
| 644 | "other/b.txt": "", |
| 645 | "other/subdir/a.txt": "", |
| 646 | "other/file": "", |
| 647 | }, |
| 648 | }, |
| 649 | { |
| 650 | description: "filegroup with glob in subdir", |
| 651 | moduleTypeUnderTest: "filegroup", |
| 652 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 653 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 654 | blueprint: `filegroup { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 655 | name: "foo", |
| 656 | srcs: ["a.txt"], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 657 | bazel_module: { bp2build_available: true }, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 658 | }`, |
| 659 | dir: "other", |
| 660 | expectedBazelTargets: []string{`filegroup( |
| 661 | name = "fg_foo", |
| 662 | srcs = [ |
| 663 | "a.txt", |
| 664 | "b.txt", |
| 665 | "subdir/a.txt", |
| 666 | ], |
| 667 | )`, |
| 668 | }, |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 669 | filesystem: map[string]string{ |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 670 | "other/Android.bp": `filegroup { |
| 671 | name: "fg_foo", |
| 672 | srcs: ["**/*.txt"], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 673 | bazel_module: { bp2build_available: true }, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 674 | }`, |
| 675 | "other/a.txt": "", |
| 676 | "other/b.txt": "", |
| 677 | "other/subdir/a.txt": "", |
| 678 | "other/file": "", |
| 679 | }, |
| 680 | }, |
| 681 | { |
| 682 | description: "depends_on_other_dir_module", |
| 683 | moduleTypeUnderTest: "filegroup", |
| 684 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 685 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 686 | blueprint: `filegroup { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 687 | name: "foobar", |
| 688 | srcs: [ |
Jingwen Chen | b4628eb | 2021-04-08 14:40:57 +0000 | [diff] [blame] | 689 | ":foo", |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 690 | "c", |
| 691 | ], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 692 | bazel_module: { bp2build_available: true }, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 693 | }`, |
| 694 | expectedBazelTargets: []string{`filegroup( |
| 695 | name = "foobar", |
| 696 | srcs = [ |
| 697 | "//other:foo", |
| 698 | "c", |
| 699 | ], |
| 700 | )`, |
| 701 | }, |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 702 | filesystem: map[string]string{ |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 703 | "other/Android.bp": `filegroup { |
| 704 | name: "foo", |
| 705 | srcs: ["a", "b"], |
| 706 | }`, |
| 707 | }, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 708 | }, |
| 709 | } |
| 710 | |
| 711 | dir := "." |
| 712 | for _, testCase := range testCases { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 713 | fs := make(map[string][]byte) |
| 714 | toParse := []string{ |
| 715 | "Android.bp", |
| 716 | } |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 717 | for f, content := range testCase.filesystem { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 718 | if strings.HasSuffix(f, "Android.bp") { |
| 719 | toParse = append(toParse, f) |
| 720 | } |
| 721 | fs[f] = []byte(content) |
| 722 | } |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 723 | config := android.TestConfig(buildDir, nil, testCase.blueprint, fs) |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 724 | ctx := android.NewTestContext(config) |
| 725 | ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory) |
Jingwen Chen | a42d641 | 2021-01-26 21:57:27 -0500 | [diff] [blame] | 726 | ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator) |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 727 | ctx.RegisterForBazelConversion() |
| 728 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 729 | _, errs := ctx.ParseFileList(dir, toParse) |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 730 | if errored(t, testCase, errs) { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 731 | continue |
| 732 | } |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 733 | _, errs = ctx.ResolveDependencies(config) |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 734 | if errored(t, testCase, errs) { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 735 | continue |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 736 | } |
| 737 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 738 | checkDir := dir |
| 739 | if testCase.dir != "" { |
| 740 | checkDir = testCase.dir |
| 741 | } |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 742 | |
| 743 | codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build) |
Jingwen Chen | ba369ad | 2021-02-22 10:19:34 -0500 | [diff] [blame] | 744 | bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir) |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 745 | if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount { |
| 746 | t.Errorf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount) |
| 747 | } else { |
| 748 | for i, target := range bazelTargets { |
| 749 | if w, g := testCase.expectedBazelTargets[i], target.content; w != g { |
| 750 | t.Errorf( |
| 751 | "%s: Expected generated Bazel target to be '%s', got '%s'", |
| 752 | testCase.description, |
| 753 | w, |
| 754 | g, |
| 755 | ) |
| 756 | } |
| 757 | } |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 758 | } |
| 759 | } |
| 760 | } |
Jingwen Chen | 041b184 | 2021-02-01 00:23:25 -0500 | [diff] [blame] | 761 | |
| 762 | type bp2buildMutator = func(android.TopDownMutatorContext) |
| 763 | |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 764 | func TestAllowlistingBp2buildTargetsExplicitly(t *testing.T) { |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 765 | testCases := []struct { |
| 766 | moduleTypeUnderTest string |
| 767 | moduleTypeUnderTestFactory android.ModuleFactory |
| 768 | moduleTypeUnderTestBp2BuildMutator bp2buildMutator |
| 769 | bp string |
| 770 | expectedCount int |
| 771 | description string |
| 772 | }{ |
| 773 | { |
| 774 | description: "explicitly unavailable", |
| 775 | moduleTypeUnderTest: "filegroup", |
| 776 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 777 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
| 778 | bp: `filegroup { |
| 779 | name: "foo", |
| 780 | srcs: ["a", "b"], |
| 781 | bazel_module: { bp2build_available: false }, |
| 782 | }`, |
| 783 | expectedCount: 0, |
| 784 | }, |
| 785 | { |
| 786 | description: "implicitly unavailable", |
| 787 | moduleTypeUnderTest: "filegroup", |
| 788 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 789 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
| 790 | bp: `filegroup { |
| 791 | name: "foo", |
| 792 | srcs: ["a", "b"], |
| 793 | }`, |
| 794 | expectedCount: 0, |
| 795 | }, |
| 796 | { |
| 797 | description: "explicitly available", |
| 798 | moduleTypeUnderTest: "filegroup", |
| 799 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 800 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
| 801 | bp: `filegroup { |
| 802 | name: "foo", |
| 803 | srcs: ["a", "b"], |
| 804 | bazel_module: { bp2build_available: true }, |
| 805 | }`, |
| 806 | expectedCount: 1, |
| 807 | }, |
| 808 | { |
| 809 | description: "generates more than 1 target if needed", |
| 810 | moduleTypeUnderTest: "custom", |
| 811 | moduleTypeUnderTestFactory: customModuleFactory, |
| 812 | moduleTypeUnderTestBp2BuildMutator: customBp2BuildMutatorFromStarlark, |
| 813 | bp: `custom { |
| 814 | name: "foo", |
| 815 | bazel_module: { bp2build_available: true }, |
| 816 | }`, |
| 817 | expectedCount: 3, |
| 818 | }, |
| 819 | } |
| 820 | |
| 821 | dir := "." |
| 822 | for _, testCase := range testCases { |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 823 | t.Run(testCase.description, func(t *testing.T) { |
| 824 | config := android.TestConfig(buildDir, nil, testCase.bp, nil) |
| 825 | ctx := android.NewTestContext(config) |
| 826 | ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory) |
| 827 | ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator) |
| 828 | ctx.RegisterForBazelConversion() |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 829 | |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 830 | _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) |
| 831 | android.FailIfErrored(t, errs) |
| 832 | _, errs = ctx.ResolveDependencies(config) |
| 833 | android.FailIfErrored(t, errs) |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 834 | |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 835 | codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build) |
| 836 | bazelTargets := generateBazelTargetsForDir(codegenCtx, dir) |
| 837 | if actualCount := len(bazelTargets); actualCount != testCase.expectedCount { |
| 838 | t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, testCase.expectedCount, actualCount) |
| 839 | } |
| 840 | }) |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 841 | } |
| 842 | } |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 843 | |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 844 | func TestAllowlistingBp2buildTargetsWithConfig(t *testing.T) { |
| 845 | testCases := []struct { |
| 846 | moduleTypeUnderTest string |
| 847 | moduleTypeUnderTestFactory android.ModuleFactory |
| 848 | moduleTypeUnderTestBp2BuildMutator bp2buildMutator |
| 849 | expectedCount map[string]int |
| 850 | description string |
| 851 | bp2buildConfig android.Bp2BuildConfig |
| 852 | checkDir string |
| 853 | fs map[string]string |
| 854 | }{ |
| 855 | { |
| 856 | description: "test bp2build config package and subpackages config", |
| 857 | moduleTypeUnderTest: "filegroup", |
| 858 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 859 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
| 860 | expectedCount: map[string]int{ |
| 861 | "migrated": 1, |
| 862 | "migrated/but_not_really": 0, |
| 863 | "migrated/but_not_really/but_really": 1, |
| 864 | "not_migrated": 0, |
| 865 | "also_not_migrated": 0, |
| 866 | }, |
| 867 | bp2buildConfig: android.Bp2BuildConfig{ |
| 868 | "migrated": android.Bp2BuildDefaultTrueRecursively, |
| 869 | "migrated/but_not_really": android.Bp2BuildDefaultFalse, |
| 870 | "not_migrated": android.Bp2BuildDefaultFalse, |
| 871 | }, |
| 872 | fs: map[string]string{ |
| 873 | "migrated/Android.bp": `filegroup { name: "a" }`, |
| 874 | "migrated/but_not_really/Android.bp": `filegroup { name: "b" }`, |
| 875 | "migrated/but_not_really/but_really/Android.bp": `filegroup { name: "c" }`, |
| 876 | "not_migrated/Android.bp": `filegroup { name: "d" }`, |
| 877 | "also_not_migrated/Android.bp": `filegroup { name: "e" }`, |
| 878 | }, |
| 879 | }, |
| 880 | { |
| 881 | description: "test bp2build config opt-in and opt-out", |
| 882 | moduleTypeUnderTest: "filegroup", |
| 883 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 884 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
| 885 | expectedCount: map[string]int{ |
| 886 | "package-opt-in": 2, |
| 887 | "package-opt-in/subpackage": 0, |
| 888 | "package-opt-out": 1, |
| 889 | "package-opt-out/subpackage": 0, |
| 890 | }, |
| 891 | bp2buildConfig: android.Bp2BuildConfig{ |
| 892 | "package-opt-in": android.Bp2BuildDefaultFalse, |
| 893 | "package-opt-out": android.Bp2BuildDefaultTrueRecursively, |
| 894 | }, |
| 895 | fs: map[string]string{ |
| 896 | "package-opt-in/Android.bp": ` |
| 897 | filegroup { name: "opt-in-a" } |
| 898 | filegroup { name: "opt-in-b", bazel_module: { bp2build_available: true } } |
| 899 | filegroup { name: "opt-in-c", bazel_module: { bp2build_available: true } } |
| 900 | `, |
| 901 | |
| 902 | "package-opt-in/subpackage/Android.bp": ` |
| 903 | filegroup { name: "opt-in-d" } // parent package not configured to DefaultTrueRecursively |
| 904 | `, |
| 905 | |
| 906 | "package-opt-out/Android.bp": ` |
| 907 | filegroup { name: "opt-out-a" } |
| 908 | filegroup { name: "opt-out-b", bazel_module: { bp2build_available: false } } |
| 909 | filegroup { name: "opt-out-c", bazel_module: { bp2build_available: false } } |
| 910 | `, |
| 911 | |
| 912 | "package-opt-out/subpackage/Android.bp": ` |
| 913 | filegroup { name: "opt-out-g", bazel_module: { bp2build_available: false } } |
| 914 | filegroup { name: "opt-out-h", bazel_module: { bp2build_available: false } } |
| 915 | `, |
| 916 | }, |
| 917 | }, |
| 918 | } |
| 919 | |
| 920 | dir := "." |
| 921 | for _, testCase := range testCases { |
| 922 | fs := make(map[string][]byte) |
| 923 | toParse := []string{ |
| 924 | "Android.bp", |
| 925 | } |
| 926 | for f, content := range testCase.fs { |
| 927 | if strings.HasSuffix(f, "Android.bp") { |
| 928 | toParse = append(toParse, f) |
| 929 | } |
| 930 | fs[f] = []byte(content) |
| 931 | } |
| 932 | config := android.TestConfig(buildDir, nil, "", fs) |
| 933 | ctx := android.NewTestContext(config) |
| 934 | ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory) |
| 935 | ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator) |
| 936 | ctx.RegisterBp2BuildConfig(testCase.bp2buildConfig) |
| 937 | ctx.RegisterForBazelConversion() |
| 938 | |
| 939 | _, errs := ctx.ParseFileList(dir, toParse) |
| 940 | android.FailIfErrored(t, errs) |
| 941 | _, errs = ctx.ResolveDependencies(config) |
| 942 | android.FailIfErrored(t, errs) |
| 943 | |
| 944 | codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build) |
| 945 | |
| 946 | // For each directory, test that the expected number of generated targets is correct. |
| 947 | for dir, expectedCount := range testCase.expectedCount { |
| 948 | bazelTargets := generateBazelTargetsForDir(codegenCtx, dir) |
| 949 | if actualCount := len(bazelTargets); actualCount != expectedCount { |
| 950 | t.Fatalf( |
| 951 | "%s: Expected %d bazel target for %s package, got %d", |
| 952 | testCase.description, |
| 953 | expectedCount, |
| 954 | dir, |
| 955 | actualCount) |
| 956 | } |
| 957 | |
| 958 | } |
| 959 | } |
| 960 | } |
| 961 | |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 962 | func TestCombineBuildFilesBp2buildTargets(t *testing.T) { |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 963 | testCases := []bp2buildTestCase{ |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 964 | { |
| 965 | description: "filegroup bazel_module.label", |
| 966 | moduleTypeUnderTest: "filegroup", |
| 967 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 968 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 969 | blueprint: `filegroup { |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 970 | name: "fg_foo", |
| 971 | bazel_module: { label: "//other:fg_foo" }, |
| 972 | }`, |
| 973 | expectedBazelTargets: []string{ |
| 974 | `// BUILD file`, |
| 975 | }, |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 976 | filesystem: map[string]string{ |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 977 | "other/BUILD.bazel": `// BUILD file`, |
| 978 | }, |
| 979 | }, |
| 980 | { |
| 981 | description: "multiple bazel_module.label same BUILD", |
| 982 | moduleTypeUnderTest: "filegroup", |
| 983 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 984 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 985 | blueprint: `filegroup { |
Jingwen Chen | c63677b | 2021-06-17 05:43:19 +0000 | [diff] [blame] | 986 | name: "fg_foo", |
| 987 | bazel_module: { label: "//other:fg_foo" }, |
| 988 | } |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 989 | |
Jingwen Chen | c63677b | 2021-06-17 05:43:19 +0000 | [diff] [blame] | 990 | filegroup { |
| 991 | name: "foo", |
| 992 | bazel_module: { label: "//other:foo" }, |
| 993 | }`, |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 994 | expectedBazelTargets: []string{ |
| 995 | `// BUILD file`, |
| 996 | }, |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 997 | filesystem: map[string]string{ |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 998 | "other/BUILD.bazel": `// BUILD file`, |
| 999 | }, |
| 1000 | }, |
| 1001 | { |
Jingwen Chen | c63677b | 2021-06-17 05:43:19 +0000 | [diff] [blame] | 1002 | description: "filegroup bazel_module.label and bp2build in subdir", |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1003 | moduleTypeUnderTest: "filegroup", |
| 1004 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 1005 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
Jingwen Chen | c63677b | 2021-06-17 05:43:19 +0000 | [diff] [blame] | 1006 | dir: "other", |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 1007 | blueprint: ``, |
| 1008 | filesystem: map[string]string{ |
Jingwen Chen | c63677b | 2021-06-17 05:43:19 +0000 | [diff] [blame] | 1009 | "other/Android.bp": `filegroup { |
| 1010 | name: "fg_foo", |
| 1011 | bazel_module: { |
| 1012 | bp2build_available: true, |
| 1013 | }, |
| 1014 | } |
| 1015 | filegroup { |
| 1016 | name: "fg_bar", |
| 1017 | bazel_module: { |
| 1018 | label: "//other:fg_bar" |
| 1019 | }, |
| 1020 | }`, |
| 1021 | "other/BUILD.bazel": `// definition for fg_bar`, |
| 1022 | }, |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1023 | expectedBazelTargets: []string{ |
| 1024 | `filegroup( |
| 1025 | name = "fg_foo", |
Jingwen Chen | c63677b | 2021-06-17 05:43:19 +0000 | [diff] [blame] | 1026 | )`, `// definition for fg_bar`, |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1027 | }, |
| 1028 | }, |
| 1029 | { |
| 1030 | description: "filegroup bazel_module.label and filegroup bp2build", |
| 1031 | moduleTypeUnderTest: "filegroup", |
| 1032 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 1033 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 1034 | blueprint: `filegroup { |
Jingwen Chen | c63677b | 2021-06-17 05:43:19 +0000 | [diff] [blame] | 1035 | name: "fg_foo", |
| 1036 | bazel_module: { |
| 1037 | label: "//other:fg_foo", |
| 1038 | }, |
| 1039 | } |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1040 | |
Jingwen Chen | c63677b | 2021-06-17 05:43:19 +0000 | [diff] [blame] | 1041 | filegroup { |
| 1042 | name: "fg_bar", |
| 1043 | bazel_module: { |
| 1044 | bp2build_available: true, |
| 1045 | }, |
| 1046 | }`, |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1047 | expectedBazelTargets: []string{ |
| 1048 | `filegroup( |
| 1049 | name = "fg_bar", |
| 1050 | )`, |
| 1051 | `// BUILD file`, |
| 1052 | }, |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 1053 | filesystem: map[string]string{ |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1054 | "other/BUILD.bazel": `// BUILD file`, |
| 1055 | }, |
| 1056 | }, |
| 1057 | } |
| 1058 | |
| 1059 | dir := "." |
| 1060 | for _, testCase := range testCases { |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1061 | t.Run(testCase.description, func(t *testing.T) { |
| 1062 | fs := make(map[string][]byte) |
| 1063 | toParse := []string{ |
| 1064 | "Android.bp", |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1065 | } |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 1066 | for f, content := range testCase.filesystem { |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1067 | if strings.HasSuffix(f, "Android.bp") { |
| 1068 | toParse = append(toParse, f) |
| 1069 | } |
| 1070 | fs[f] = []byte(content) |
| 1071 | } |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 1072 | config := android.TestConfig(buildDir, nil, testCase.blueprint, fs) |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1073 | ctx := android.NewTestContext(config) |
| 1074 | ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory) |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1075 | ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator) |
| 1076 | ctx.RegisterForBazelConversion() |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1077 | |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1078 | _, errs := ctx.ParseFileList(dir, toParse) |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 1079 | if errored(t, testCase, errs) { |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1080 | return |
| 1081 | } |
| 1082 | _, errs = ctx.ResolveDependencies(config) |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 1083 | if errored(t, testCase, errs) { |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1084 | return |
| 1085 | } |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1086 | |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1087 | checkDir := dir |
| 1088 | if testCase.dir != "" { |
| 1089 | checkDir = testCase.dir |
| 1090 | } |
| 1091 | bazelTargets := generateBazelTargetsForDir(NewCodegenContext(config, *ctx.Context, Bp2Build), checkDir) |
| 1092 | bazelTargets.sort() |
| 1093 | actualCount := len(bazelTargets) |
| 1094 | expectedCount := len(testCase.expectedBazelTargets) |
| 1095 | if actualCount != expectedCount { |
| 1096 | t.Errorf("Expected %d bazel target, got %d\n%s", expectedCount, actualCount, bazelTargets) |
| 1097 | } |
| 1098 | if !strings.Contains(bazelTargets.String(), "Section: Handcrafted targets. ") { |
| 1099 | t.Errorf("Expected string representation of bazelTargets to contain handcrafted section header.") |
| 1100 | } |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1101 | for i, target := range bazelTargets { |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1102 | actualContent := target.content |
| 1103 | expectedContent := testCase.expectedBazelTargets[i] |
| 1104 | if expectedContent != actualContent { |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1105 | t.Errorf( |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1106 | "Expected generated Bazel target to be '%s', got '%s'", |
| 1107 | expectedContent, |
| 1108 | actualContent, |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1109 | ) |
| 1110 | } |
| 1111 | } |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1112 | }) |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1113 | } |
| 1114 | } |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1115 | |
| 1116 | func TestGlobExcludeSrcs(t *testing.T) { |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 1117 | testCases := []bp2buildTestCase{ |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1118 | { |
| 1119 | description: "filegroup top level exclude_srcs", |
| 1120 | moduleTypeUnderTest: "filegroup", |
| 1121 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 1122 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 1123 | blueprint: `filegroup { |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1124 | name: "fg_foo", |
| 1125 | srcs: ["**/*.txt"], |
| 1126 | exclude_srcs: ["c.txt"], |
| 1127 | bazel_module: { bp2build_available: true }, |
| 1128 | }`, |
| 1129 | expectedBazelTargets: []string{`filegroup( |
| 1130 | name = "fg_foo", |
| 1131 | srcs = [ |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1132 | "a.txt", |
| 1133 | "b.txt", |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 1134 | "//dir:e.txt", |
| 1135 | "//dir:f.txt", |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1136 | ], |
| 1137 | )`, |
| 1138 | }, |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 1139 | filesystem: map[string]string{ |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1140 | "a.txt": "", |
| 1141 | "b.txt": "", |
| 1142 | "c.txt": "", |
| 1143 | "dir/Android.bp": "", |
| 1144 | "dir/e.txt": "", |
| 1145 | "dir/f.txt": "", |
| 1146 | }, |
| 1147 | }, |
| 1148 | { |
| 1149 | description: "filegroup in subdir exclude_srcs", |
| 1150 | moduleTypeUnderTest: "filegroup", |
| 1151 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 1152 | moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build, |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 1153 | blueprint: "", |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1154 | dir: "dir", |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 1155 | filesystem: map[string]string{ |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1156 | "dir/Android.bp": `filegroup { |
| 1157 | name: "fg_foo", |
| 1158 | srcs: ["**/*.txt"], |
| 1159 | exclude_srcs: ["b.txt"], |
| 1160 | bazel_module: { bp2build_available: true }, |
| 1161 | } |
| 1162 | `, |
| 1163 | "dir/a.txt": "", |
| 1164 | "dir/b.txt": "", |
| 1165 | "dir/subdir/Android.bp": "", |
| 1166 | "dir/subdir/e.txt": "", |
| 1167 | "dir/subdir/f.txt": "", |
| 1168 | }, |
| 1169 | expectedBazelTargets: []string{`filegroup( |
| 1170 | name = "fg_foo", |
| 1171 | srcs = [ |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 1172 | "a.txt", |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1173 | "//dir/subdir:e.txt", |
| 1174 | "//dir/subdir:f.txt", |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1175 | ], |
| 1176 | )`, |
| 1177 | }, |
| 1178 | }, |
| 1179 | } |
| 1180 | |
| 1181 | dir := "." |
| 1182 | for _, testCase := range testCases { |
| 1183 | fs := make(map[string][]byte) |
| 1184 | toParse := []string{ |
| 1185 | "Android.bp", |
| 1186 | } |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 1187 | for f, content := range testCase.filesystem { |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1188 | if strings.HasSuffix(f, "Android.bp") { |
| 1189 | toParse = append(toParse, f) |
| 1190 | } |
| 1191 | fs[f] = []byte(content) |
| 1192 | } |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 1193 | config := android.TestConfig(buildDir, nil, testCase.blueprint, fs) |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1194 | ctx := android.NewTestContext(config) |
| 1195 | ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory) |
| 1196 | ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator) |
| 1197 | ctx.RegisterForBazelConversion() |
| 1198 | |
| 1199 | _, errs := ctx.ParseFileList(dir, toParse) |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 1200 | if errored(t, testCase, errs) { |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1201 | continue |
| 1202 | } |
| 1203 | _, errs = ctx.ResolveDependencies(config) |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 1204 | if errored(t, testCase, errs) { |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1205 | continue |
| 1206 | } |
| 1207 | |
| 1208 | checkDir := dir |
| 1209 | if testCase.dir != "" { |
| 1210 | checkDir = testCase.dir |
| 1211 | } |
| 1212 | bazelTargets := generateBazelTargetsForDir(NewCodegenContext(config, *ctx.Context, Bp2Build), checkDir) |
| 1213 | if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount { |
| 1214 | t.Errorf("%s: Expected %d bazel target, got %d\n%s", testCase.description, expectedCount, actualCount, bazelTargets) |
| 1215 | } else { |
| 1216 | for i, target := range bazelTargets { |
| 1217 | if w, g := testCase.expectedBazelTargets[i], target.content; w != g { |
| 1218 | t.Errorf( |
| 1219 | "%s: Expected generated Bazel target to be '%s', got '%s'", |
| 1220 | testCase.description, |
| 1221 | w, |
| 1222 | g, |
| 1223 | ) |
| 1224 | } |
| 1225 | } |
| 1226 | } |
| 1227 | } |
| 1228 | } |