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