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