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