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