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