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" |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 23 | "android/soong/android/allowlists" |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 24 | "android/soong/python" |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | func TestGenerateSoongModuleTargets(t *testing.T) { |
| 28 | testCases := []struct { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 29 | description string |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 30 | bp string |
| 31 | expectedBazelTarget string |
| 32 | }{ |
| 33 | { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 34 | description: "only name", |
Jingwen Chen | b4628eb | 2021-04-08 14:40:57 +0000 | [diff] [blame] | 35 | bp: `custom { name: "foo" } |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 36 | `, |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 37 | expectedBazelTarget: `soong_module( |
| 38 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 39 | soong_module_name = "foo", |
| 40 | soong_module_type = "custom", |
| 41 | soong_module_variant = "", |
| 42 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 43 | ], |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 44 | bool_prop = False, |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 45 | string_prop = "", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 46 | )`, |
| 47 | }, |
| 48 | { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 49 | description: "handles bool", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 50 | bp: `custom { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 51 | name: "foo", |
| 52 | bool_prop: true, |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 53 | } |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 54 | `, |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 55 | expectedBazelTarget: `soong_module( |
| 56 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 57 | soong_module_name = "foo", |
| 58 | soong_module_type = "custom", |
| 59 | soong_module_variant = "", |
| 60 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 61 | ], |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 62 | bool_prop = True, |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 63 | string_prop = "", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 64 | )`, |
| 65 | }, |
| 66 | { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 67 | description: "string escaping", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 68 | bp: `custom { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 69 | name: "foo", |
| 70 | owner: "a_string_with\"quotes\"_and_\\backslashes\\\\", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 71 | } |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 72 | `, |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 73 | expectedBazelTarget: `soong_module( |
| 74 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 75 | soong_module_name = "foo", |
| 76 | soong_module_type = "custom", |
| 77 | soong_module_variant = "", |
| 78 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 79 | ], |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 80 | bool_prop = False, |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 81 | owner = "a_string_with\"quotes\"_and_\\backslashes\\\\", |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 82 | string_prop = "", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 83 | )`, |
| 84 | }, |
| 85 | { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 86 | description: "single item string list", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 87 | bp: `custom { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 88 | name: "foo", |
| 89 | required: ["bar"], |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 90 | } |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 91 | `, |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 92 | expectedBazelTarget: `soong_module( |
| 93 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 94 | soong_module_name = "foo", |
| 95 | soong_module_type = "custom", |
| 96 | soong_module_variant = "", |
| 97 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 98 | ], |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 99 | bool_prop = False, |
Jingwen Chen | b4628eb | 2021-04-08 14:40:57 +0000 | [diff] [blame] | 100 | required = ["bar"], |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 101 | string_prop = "", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 102 | )`, |
| 103 | }, |
| 104 | { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 105 | description: "list of strings", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 106 | bp: `custom { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 107 | name: "foo", |
| 108 | target_required: ["qux", "bazqux"], |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 109 | } |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 110 | `, |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 111 | expectedBazelTarget: `soong_module( |
| 112 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 113 | soong_module_name = "foo", |
| 114 | soong_module_type = "custom", |
| 115 | soong_module_variant = "", |
| 116 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 117 | ], |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 118 | bool_prop = False, |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 119 | string_prop = "", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 120 | target_required = [ |
| 121 | "qux", |
| 122 | "bazqux", |
| 123 | ], |
| 124 | )`, |
| 125 | }, |
| 126 | { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 127 | description: "dist/dists", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 128 | bp: `custom { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 129 | name: "foo", |
| 130 | dist: { |
| 131 | targets: ["goal_foo"], |
| 132 | tag: ".foo", |
| 133 | }, |
| 134 | dists: [{ |
| 135 | targets: ["goal_bar"], |
| 136 | tag: ".bar", |
| 137 | }], |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 138 | } |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 139 | `, |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 140 | expectedBazelTarget: `soong_module( |
| 141 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 142 | soong_module_name = "foo", |
| 143 | soong_module_type = "custom", |
| 144 | soong_module_variant = "", |
| 145 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 146 | ], |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 147 | bool_prop = False, |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 148 | dist = { |
| 149 | "tag": ".foo", |
Jingwen Chen | b4628eb | 2021-04-08 14:40:57 +0000 | [diff] [blame] | 150 | "targets": ["goal_foo"], |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 151 | }, |
Jingwen Chen | b4628eb | 2021-04-08 14:40:57 +0000 | [diff] [blame] | 152 | dists = [{ |
| 153 | "tag": ".bar", |
| 154 | "targets": ["goal_bar"], |
| 155 | }], |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 156 | string_prop = "", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 157 | )`, |
| 158 | }, |
| 159 | { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 160 | description: "put it together", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 161 | bp: `custom { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 162 | name: "foo", |
| 163 | required: ["bar"], |
| 164 | target_required: ["qux", "bazqux"], |
| 165 | bool_prop: true, |
| 166 | owner: "custom_owner", |
| 167 | dists: [ |
| 168 | { |
| 169 | tag: ".tag", |
| 170 | targets: ["my_goal"], |
| 171 | }, |
| 172 | ], |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 173 | } |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 174 | `, |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 175 | expectedBazelTarget: `soong_module( |
| 176 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 177 | soong_module_name = "foo", |
| 178 | soong_module_type = "custom", |
| 179 | soong_module_variant = "", |
| 180 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 181 | ], |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 182 | bool_prop = True, |
Jingwen Chen | b4628eb | 2021-04-08 14:40:57 +0000 | [diff] [blame] | 183 | dists = [{ |
| 184 | "tag": ".tag", |
| 185 | "targets": ["my_goal"], |
| 186 | }], |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 187 | owner = "custom_owner", |
Jingwen Chen | b4628eb | 2021-04-08 14:40:57 +0000 | [diff] [blame] | 188 | required = ["bar"], |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 189 | string_prop = "", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 190 | target_required = [ |
| 191 | "qux", |
| 192 | "bazqux", |
| 193 | ], |
| 194 | )`, |
| 195 | }, |
| 196 | } |
| 197 | |
| 198 | dir := "." |
| 199 | for _, testCase := range testCases { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 200 | t.Run(testCase.description, func(t *testing.T) { |
| 201 | config := android.TestConfig(buildDir, nil, testCase.bp, nil) |
| 202 | ctx := android.NewTestContext(config) |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 203 | |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 204 | ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice) |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 205 | ctx.Register() |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 206 | |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 207 | _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) |
| 208 | android.FailIfErrored(t, errs) |
| 209 | _, errs = ctx.PrepareBuildActions(config) |
| 210 | android.FailIfErrored(t, errs) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 211 | |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 212 | codegenCtx := NewCodegenContext(config, *ctx.Context, QueryView) |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 213 | bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir) |
| 214 | android.FailIfErrored(t, err) |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 215 | if actualCount, expectedCount := len(bazelTargets), 1; actualCount != expectedCount { |
| 216 | t.Fatalf("Expected %d bazel target, got %d", expectedCount, actualCount) |
| 217 | } |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 218 | |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 219 | actualBazelTarget := bazelTargets[0] |
| 220 | if actualBazelTarget.content != testCase.expectedBazelTarget { |
| 221 | t.Errorf( |
| 222 | "Expected generated Bazel target to be '%s', got '%s'", |
| 223 | testCase.expectedBazelTarget, |
| 224 | actualBazelTarget.content, |
| 225 | ) |
| 226 | } |
| 227 | }) |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
| 231 | func TestGenerateBazelTargetModules(t *testing.T) { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 232 | testCases := []Bp2buildTestCase{ |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 233 | { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 3a019a6 | 2022-06-23 16:02:44 +0000 | [diff] [blame] | 234 | Description: "string prop empty", |
| 235 | Blueprint: `custom { |
| 236 | name: "foo", |
| 237 | string_literal_prop: "", |
| 238 | bazel_module: { bp2build_available: true }, |
| 239 | }`, |
| 240 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 241 | MakeBazelTarget("custom", "foo", AttrNameToString{ |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 3a019a6 | 2022-06-23 16:02:44 +0000 | [diff] [blame] | 242 | "string_literal_prop": `""`, |
| 243 | }), |
| 244 | }, |
| 245 | }, |
| 246 | { |
| 247 | Description: `string prop "PROP"`, |
| 248 | Blueprint: `custom { |
| 249 | name: "foo", |
| 250 | string_literal_prop: "PROP", |
| 251 | bazel_module: { bp2build_available: true }, |
| 252 | }`, |
| 253 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 254 | MakeBazelTarget("custom", "foo", AttrNameToString{ |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 3a019a6 | 2022-06-23 16:02:44 +0000 | [diff] [blame] | 255 | "string_literal_prop": `"PROP"`, |
| 256 | }), |
| 257 | }, |
| 258 | }, |
| 259 | { |
| 260 | Description: `string prop arch variant`, |
| 261 | Blueprint: `custom { |
| 262 | name: "foo", |
| 263 | arch: { |
| 264 | arm: { string_literal_prop: "ARM" }, |
| 265 | arm64: { string_literal_prop: "ARM64" }, |
| 266 | }, |
| 267 | bazel_module: { bp2build_available: true }, |
| 268 | }`, |
| 269 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 270 | MakeBazelTarget("custom", "foo", AttrNameToString{ |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 3a019a6 | 2022-06-23 16:02:44 +0000 | [diff] [blame] | 271 | "string_literal_prop": `select({ |
| 272 | "//build/bazel/platforms/arch:arm": "ARM", |
| 273 | "//build/bazel/platforms/arch:arm64": "ARM64", |
| 274 | "//conditions:default": None, |
| 275 | })`, |
| 276 | }), |
| 277 | }, |
| 278 | }, |
| 279 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 280 | Description: "string ptr props", |
| 281 | Blueprint: `custom { |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 282 | name: "foo", |
| 283 | string_ptr_prop: "", |
| 284 | bazel_module: { bp2build_available: true }, |
| 285 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 286 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 287 | MakeBazelTarget("custom", "foo", AttrNameToString{ |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 288 | "string_ptr_prop": `""`, |
| 289 | }), |
| 290 | }, |
| 291 | }, |
| 292 | { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 3a019a6 | 2022-06-23 16:02:44 +0000 | [diff] [blame] | 293 | Description: "string list props", |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 294 | Blueprint: `custom { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 295 | name: "foo", |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 296 | string_list_prop: ["a", "b"], |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 297 | string_ptr_prop: "a", |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 298 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 299 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 300 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 301 | MakeBazelTarget("custom", "foo", AttrNameToString{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 302 | "string_list_prop": `[ |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 303 | "a", |
| 304 | "b", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 305 | ]`, |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 306 | "string_ptr_prop": `"a"`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 307 | }), |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 308 | }, |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 309 | }, |
Jingwen Chen | 58a12b8 | 2021-03-30 13:08:36 +0000 | [diff] [blame] | 310 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 311 | Description: "control characters", |
| 312 | Blueprint: `custom { |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 313 | name: "foo", |
Jingwen Chen | 58a12b8 | 2021-03-30 13:08:36 +0000 | [diff] [blame] | 314 | string_list_prop: ["\t", "\n"], |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 315 | string_ptr_prop: "a\t\n\r", |
Jingwen Chen | 58a12b8 | 2021-03-30 13:08:36 +0000 | [diff] [blame] | 316 | bazel_module: { bp2build_available: true }, |
| 317 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 318 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 319 | MakeBazelTarget("custom", "foo", AttrNameToString{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 320 | "string_list_prop": `[ |
Jingwen Chen | 58a12b8 | 2021-03-30 13:08:36 +0000 | [diff] [blame] | 321 | "\t", |
| 322 | "\n", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 323 | ]`, |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 324 | "string_ptr_prop": `"a\t\n\r"`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 325 | }), |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 326 | }, |
| 327 | }, |
| 328 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 329 | Description: "handles dep", |
| 330 | Blueprint: `custom { |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 331 | name: "has_dep", |
| 332 | arch_paths: [":dep"], |
| 333 | bazel_module: { bp2build_available: true }, |
| 334 | } |
| 335 | |
| 336 | custom { |
| 337 | name: "dep", |
| 338 | arch_paths: ["abc"], |
| 339 | bazel_module: { bp2build_available: true }, |
| 340 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 341 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 342 | MakeBazelTarget("custom", "dep", AttrNameToString{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 343 | "arch_paths": `["abc"]`, |
| 344 | }), |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 345 | MakeBazelTarget("custom", "has_dep", AttrNameToString{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 346 | "arch_paths": `[":dep"]`, |
| 347 | }), |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 348 | }, |
| 349 | }, |
| 350 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 351 | Description: "non-existent dep", |
| 352 | Blueprint: `custom { |
Liz Kammer | daa09ef | 2021-12-15 15:35:38 -0500 | [diff] [blame] | 353 | name: "has_dep", |
| 354 | arch_paths: [":dep"], |
| 355 | bazel_module: { bp2build_available: true }, |
| 356 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 357 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 358 | MakeBazelTarget("custom", "has_dep", AttrNameToString{ |
Liz Kammer | daa09ef | 2021-12-15 15:35:38 -0500 | [diff] [blame] | 359 | "arch_paths": `[":dep__BP2BUILD__MISSING__DEP"]`, |
| 360 | }), |
| 361 | }, |
| 362 | }, |
| 363 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 364 | Description: "arch-variant srcs", |
| 365 | Blueprint: `custom { |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 366 | name: "arch_paths", |
| 367 | arch: { |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 368 | x86: { arch_paths: ["x86.txt"] }, |
| 369 | x86_64: { arch_paths: ["x86_64.txt"] }, |
| 370 | arm: { arch_paths: ["arm.txt"] }, |
| 371 | arm64: { arch_paths: ["arm64.txt"] }, |
| 372 | }, |
| 373 | target: { |
| 374 | linux: { arch_paths: ["linux.txt"] }, |
| 375 | bionic: { arch_paths: ["bionic.txt"] }, |
| 376 | host: { arch_paths: ["host.txt"] }, |
| 377 | not_windows: { arch_paths: ["not_windows.txt"] }, |
| 378 | android: { arch_paths: ["android.txt"] }, |
| 379 | linux_musl: { arch_paths: ["linux_musl.txt"] }, |
| 380 | musl: { arch_paths: ["musl.txt"] }, |
| 381 | linux_glibc: { arch_paths: ["linux_glibc.txt"] }, |
| 382 | glibc: { arch_paths: ["glibc.txt"] }, |
| 383 | linux_bionic: { arch_paths: ["linux_bionic.txt"] }, |
| 384 | darwin: { arch_paths: ["darwin.txt"] }, |
| 385 | windows: { arch_paths: ["windows.txt"] }, |
| 386 | }, |
| 387 | multilib: { |
| 388 | lib32: { arch_paths: ["lib32.txt"] }, |
| 389 | lib64: { arch_paths: ["lib64.txt"] }, |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 390 | }, |
| 391 | bazel_module: { bp2build_available: true }, |
| 392 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 393 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 394 | MakeBazelTarget("custom", "arch_paths", AttrNameToString{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 395 | "arch_paths": `select({ |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 396 | "//build/bazel/platforms/arch:arm": [ |
| 397 | "arm.txt", |
| 398 | "lib32.txt", |
| 399 | ], |
| 400 | "//build/bazel/platforms/arch:arm64": [ |
| 401 | "arm64.txt", |
| 402 | "lib64.txt", |
| 403 | ], |
| 404 | "//build/bazel/platforms/arch:x86": [ |
| 405 | "x86.txt", |
| 406 | "lib32.txt", |
| 407 | ], |
| 408 | "//build/bazel/platforms/arch:x86_64": [ |
| 409 | "x86_64.txt", |
| 410 | "lib64.txt", |
| 411 | ], |
| 412 | "//conditions:default": [], |
| 413 | }) + select({ |
| 414 | "//build/bazel/platforms/os:android": [ |
| 415 | "linux.txt", |
| 416 | "bionic.txt", |
| 417 | "android.txt", |
| 418 | ], |
| 419 | "//build/bazel/platforms/os:darwin": [ |
| 420 | "host.txt", |
| 421 | "darwin.txt", |
| 422 | "not_windows.txt", |
| 423 | ], |
| 424 | "//build/bazel/platforms/os:linux": [ |
| 425 | "host.txt", |
| 426 | "linux.txt", |
| 427 | "glibc.txt", |
| 428 | "linux_glibc.txt", |
| 429 | "not_windows.txt", |
| 430 | ], |
| 431 | "//build/bazel/platforms/os:linux_bionic": [ |
| 432 | "host.txt", |
| 433 | "linux.txt", |
| 434 | "bionic.txt", |
| 435 | "linux_bionic.txt", |
| 436 | "not_windows.txt", |
| 437 | ], |
| 438 | "//build/bazel/platforms/os:linux_musl": [ |
| 439 | "host.txt", |
| 440 | "linux.txt", |
| 441 | "musl.txt", |
| 442 | "linux_musl.txt", |
| 443 | "not_windows.txt", |
| 444 | ], |
| 445 | "//build/bazel/platforms/os:windows": [ |
| 446 | "host.txt", |
| 447 | "windows.txt", |
| 448 | ], |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 449 | "//conditions:default": [], |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 450 | })`, |
| 451 | }), |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 452 | }, |
| 453 | }, |
| 454 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 455 | Description: "arch-variant deps", |
| 456 | Blueprint: `custom { |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 457 | name: "has_dep", |
| 458 | arch: { |
| 459 | x86: { |
| 460 | arch_paths: [":dep"], |
| 461 | }, |
| 462 | }, |
| 463 | bazel_module: { bp2build_available: true }, |
| 464 | } |
| 465 | |
| 466 | custom { |
| 467 | name: "dep", |
| 468 | arch_paths: ["abc"], |
| 469 | bazel_module: { bp2build_available: true }, |
| 470 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 471 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 472 | MakeBazelTarget("custom", "dep", AttrNameToString{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 473 | "arch_paths": `["abc"]`, |
| 474 | }), |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 475 | MakeBazelTarget("custom", "has_dep", AttrNameToString{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 476 | "arch_paths": `select({ |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 477 | "//build/bazel/platforms/arch:x86": [":dep"], |
| 478 | "//conditions:default": [], |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 479 | })`, |
| 480 | }), |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 481 | }, |
Jingwen Chen | 58a12b8 | 2021-03-30 13:08:36 +0000 | [diff] [blame] | 482 | }, |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 483 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 484 | Description: "embedded props", |
| 485 | Blueprint: `custom { |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 486 | name: "embedded_props", |
| 487 | embedded_prop: "abc", |
| 488 | bazel_module: { bp2build_available: true }, |
| 489 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 490 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 491 | MakeBazelTarget("custom", "embedded_props", AttrNameToString{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 492 | "embedded_attr": `"abc"`, |
| 493 | }), |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 494 | }, |
| 495 | }, |
| 496 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 497 | Description: "ptr to embedded props", |
| 498 | Blueprint: `custom { |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 499 | name: "ptr_to_embedded_props", |
| 500 | other_embedded_prop: "abc", |
| 501 | bazel_module: { bp2build_available: true }, |
| 502 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 503 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 504 | MakeBazelTarget("custom", "ptr_to_embedded_props", AttrNameToString{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 505 | "other_embedded_attr": `"abc"`, |
| 506 | }), |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 507 | }, |
| 508 | }, |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | dir := "." |
| 512 | for _, testCase := range testCases { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 513 | t.Run(testCase.Description, func(t *testing.T) { |
| 514 | config := android.TestConfig(buildDir, nil, testCase.Blueprint, nil) |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 515 | ctx := android.NewTestContext(config) |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 516 | |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 517 | registerCustomModuleForBp2buildConversion(ctx) |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 518 | |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 519 | _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) |
| 520 | if errored(t, testCase, errs) { |
| 521 | return |
| 522 | } |
| 523 | _, errs = ctx.ResolveDependencies(config) |
| 524 | if errored(t, testCase, errs) { |
| 525 | return |
| 526 | } |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 527 | |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 528 | codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build) |
| 529 | bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir) |
| 530 | android.FailIfErrored(t, err) |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 531 | |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 532 | if actualCount, expectedCount := len(bazelTargets), len(testCase.ExpectedBazelTargets); actualCount != expectedCount { |
| 533 | t.Errorf("Expected %d bazel target (%s),\ngot %d (%s)", expectedCount, testCase.ExpectedBazelTargets, actualCount, bazelTargets) |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 534 | } else { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 535 | for i, expectedBazelTarget := range testCase.ExpectedBazelTargets { |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 536 | actualBazelTarget := bazelTargets[i] |
| 537 | if actualBazelTarget.content != expectedBazelTarget { |
| 538 | t.Errorf( |
| 539 | "Expected generated Bazel target to be '%s', got '%s'", |
| 540 | expectedBazelTarget, |
| 541 | actualBazelTarget.content, |
| 542 | ) |
| 543 | } |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 544 | } |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 545 | } |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 546 | }) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 547 | } |
| 548 | } |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 549 | |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 550 | func TestBp2buildHostAndDevice(t *testing.T) { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 551 | testCases := []Bp2buildTestCase{ |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 552 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 553 | Description: "host and device, device only", |
| 554 | ModuleTypeUnderTest: "custom", |
| 555 | ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice, |
| 556 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 557 | name: "foo", |
| 558 | bazel_module: { bp2build_available: true }, |
| 559 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 560 | ExpectedBazelTargets: []string{ |
| 561 | makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported), |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 562 | }, |
| 563 | }, |
| 564 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 565 | Description: "host and device, both", |
| 566 | ModuleTypeUnderTest: "custom", |
| 567 | ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice, |
| 568 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 569 | name: "foo", |
| 570 | host_supported: true, |
| 571 | bazel_module: { bp2build_available: true }, |
| 572 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 573 | ExpectedBazelTargets: []string{ |
| 574 | MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{}), |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 575 | }, |
| 576 | }, |
| 577 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 578 | Description: "host and device, host explicitly disabled", |
| 579 | ModuleTypeUnderTest: "custom", |
| 580 | ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice, |
| 581 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 582 | name: "foo", |
| 583 | host_supported: false, |
| 584 | bazel_module: { bp2build_available: true }, |
| 585 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 586 | ExpectedBazelTargets: []string{ |
| 587 | makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported), |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 588 | }, |
| 589 | }, |
| 590 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 591 | Description: "host and device, neither", |
| 592 | ModuleTypeUnderTest: "custom", |
| 593 | ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice, |
| 594 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 595 | name: "foo", |
| 596 | host_supported: false, |
| 597 | device_supported: false, |
| 598 | bazel_module: { bp2build_available: true }, |
| 599 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 600 | ExpectedBazelTargets: []string{ |
| 601 | MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{ |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 602 | "target_compatible_with": `["@platforms//:incompatible"]`, |
| 603 | }), |
| 604 | }, |
| 605 | }, |
| 606 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 607 | Description: "host and device, neither, cannot override with product_var", |
| 608 | ModuleTypeUnderTest: "custom", |
| 609 | ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice, |
| 610 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 611 | name: "foo", |
| 612 | host_supported: false, |
| 613 | device_supported: false, |
| 614 | product_variables: { unbundled_build: { enabled: true } }, |
| 615 | bazel_module: { bp2build_available: true }, |
| 616 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 617 | ExpectedBazelTargets: []string{ |
| 618 | MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{ |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 619 | "target_compatible_with": `["@platforms//:incompatible"]`, |
| 620 | }), |
| 621 | }, |
| 622 | }, |
| 623 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 624 | Description: "host and device, both, disabled overrided with product_var", |
| 625 | ModuleTypeUnderTest: "custom", |
| 626 | ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice, |
| 627 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 628 | name: "foo", |
| 629 | host_supported: true, |
| 630 | device_supported: true, |
| 631 | enabled: false, |
| 632 | product_variables: { unbundled_build: { enabled: true } }, |
| 633 | bazel_module: { bp2build_available: true }, |
| 634 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 635 | ExpectedBazelTargets: []string{ |
| 636 | MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{ |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 637 | "target_compatible_with": `["//build/bazel/product_variables:unbundled_build"]`, |
| 638 | }), |
| 639 | }, |
| 640 | }, |
| 641 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 642 | Description: "host and device, neither, cannot override with arch enabled", |
| 643 | ModuleTypeUnderTest: "custom", |
| 644 | ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice, |
| 645 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 646 | name: "foo", |
| 647 | host_supported: false, |
| 648 | device_supported: false, |
| 649 | arch: { x86: { enabled: true } }, |
| 650 | bazel_module: { bp2build_available: true }, |
| 651 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 652 | ExpectedBazelTargets: []string{ |
| 653 | MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{ |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 654 | "target_compatible_with": `["@platforms//:incompatible"]`, |
| 655 | }), |
| 656 | }, |
| 657 | }, |
| 658 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 659 | Description: "host and device, host only", |
| 660 | ModuleTypeUnderTest: "custom", |
| 661 | ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice, |
| 662 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 663 | name: "foo", |
| 664 | host_supported: true, |
| 665 | device_supported: false, |
| 666 | bazel_module: { bp2build_available: true }, |
| 667 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 668 | ExpectedBazelTargets: []string{ |
| 669 | makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.HostSupported), |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 670 | }, |
| 671 | }, |
| 672 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 673 | Description: "host only", |
| 674 | ModuleTypeUnderTest: "custom", |
| 675 | ModuleTypeUnderTestFactory: customModuleFactoryHostSupported, |
| 676 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 677 | name: "foo", |
| 678 | bazel_module: { bp2build_available: true }, |
| 679 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 680 | ExpectedBazelTargets: []string{ |
| 681 | makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.HostSupported), |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 682 | }, |
| 683 | }, |
| 684 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 685 | Description: "device only", |
| 686 | ModuleTypeUnderTest: "custom", |
| 687 | ModuleTypeUnderTestFactory: customModuleFactoryDeviceSupported, |
| 688 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 689 | name: "foo", |
| 690 | bazel_module: { bp2build_available: true }, |
| 691 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 692 | ExpectedBazelTargets: []string{ |
| 693 | makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported), |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 694 | }, |
| 695 | }, |
| 696 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 697 | Description: "host and device default, default", |
| 698 | ModuleTypeUnderTest: "custom", |
| 699 | ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault, |
| 700 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 701 | name: "foo", |
| 702 | bazel_module: { bp2build_available: true }, |
| 703 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 704 | ExpectedBazelTargets: []string{ |
| 705 | MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{}), |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 706 | }, |
| 707 | }, |
| 708 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 709 | Description: "host and device default, device only", |
| 710 | ModuleTypeUnderTest: "custom", |
| 711 | ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault, |
| 712 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 713 | name: "foo", |
| 714 | host_supported: false, |
| 715 | bazel_module: { bp2build_available: true }, |
| 716 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 717 | ExpectedBazelTargets: []string{ |
| 718 | makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported), |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 719 | }, |
| 720 | }, |
| 721 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 722 | Description: "host and device default, host only", |
| 723 | ModuleTypeUnderTest: "custom", |
| 724 | ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault, |
| 725 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 726 | name: "foo", |
| 727 | device_supported: false, |
| 728 | bazel_module: { bp2build_available: true }, |
| 729 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 730 | ExpectedBazelTargets: []string{ |
| 731 | makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.HostSupported), |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 732 | }, |
| 733 | }, |
| 734 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 735 | Description: "host and device default, neither", |
| 736 | ModuleTypeUnderTest: "custom", |
| 737 | ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault, |
| 738 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 739 | name: "foo", |
| 740 | host_supported: false, |
| 741 | device_supported: false, |
| 742 | bazel_module: { bp2build_available: true }, |
| 743 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 744 | ExpectedBazelTargets: []string{ |
| 745 | MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{ |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 746 | "target_compatible_with": `["@platforms//:incompatible"]`, |
| 747 | }), |
| 748 | }, |
| 749 | }, |
| 750 | } |
| 751 | |
| 752 | for _, tc := range testCases { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 753 | t.Run(tc.Description, func(t *testing.T) { |
Trevor Radcliffe | 1b4b2d9 | 2022-09-01 18:57:01 +0000 | [diff] [blame] | 754 | RunBp2BuildTestCaseSimple(t, tc) |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 755 | }) |
| 756 | } |
| 757 | } |
| 758 | |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 759 | func TestLoadStatements(t *testing.T) { |
| 760 | testCases := []struct { |
| 761 | bazelTargets BazelTargets |
| 762 | expectedLoadStatements string |
| 763 | }{ |
| 764 | { |
| 765 | bazelTargets: BazelTargets{ |
| 766 | BazelTarget{ |
| 767 | name: "foo", |
| 768 | ruleClass: "cc_library", |
| 769 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 770 | }, |
| 771 | }, |
| 772 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`, |
| 773 | }, |
| 774 | { |
| 775 | bazelTargets: BazelTargets{ |
| 776 | BazelTarget{ |
| 777 | name: "foo", |
| 778 | ruleClass: "cc_library", |
| 779 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 780 | }, |
| 781 | BazelTarget{ |
| 782 | name: "bar", |
| 783 | ruleClass: "cc_library", |
| 784 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 785 | }, |
| 786 | }, |
| 787 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`, |
| 788 | }, |
| 789 | { |
| 790 | bazelTargets: BazelTargets{ |
| 791 | BazelTarget{ |
| 792 | name: "foo", |
| 793 | ruleClass: "cc_library", |
| 794 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 795 | }, |
| 796 | BazelTarget{ |
| 797 | name: "bar", |
| 798 | ruleClass: "cc_binary", |
| 799 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 800 | }, |
| 801 | }, |
| 802 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")`, |
| 803 | }, |
| 804 | { |
| 805 | bazelTargets: BazelTargets{ |
| 806 | BazelTarget{ |
| 807 | name: "foo", |
| 808 | ruleClass: "cc_library", |
| 809 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 810 | }, |
| 811 | BazelTarget{ |
| 812 | name: "bar", |
| 813 | ruleClass: "cc_binary", |
| 814 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 815 | }, |
| 816 | BazelTarget{ |
| 817 | name: "baz", |
| 818 | ruleClass: "java_binary", |
| 819 | bzlLoadLocation: "//build/bazel/rules:java.bzl", |
| 820 | }, |
| 821 | }, |
| 822 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library") |
| 823 | load("//build/bazel/rules:java.bzl", "java_binary")`, |
| 824 | }, |
| 825 | { |
| 826 | bazelTargets: BazelTargets{ |
| 827 | BazelTarget{ |
| 828 | name: "foo", |
| 829 | ruleClass: "cc_binary", |
| 830 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 831 | }, |
| 832 | BazelTarget{ |
| 833 | name: "bar", |
| 834 | ruleClass: "java_binary", |
| 835 | bzlLoadLocation: "//build/bazel/rules:java.bzl", |
| 836 | }, |
| 837 | BazelTarget{ |
| 838 | name: "baz", |
| 839 | ruleClass: "genrule", |
| 840 | // Note: no bzlLoadLocation for native rules |
| 841 | }, |
| 842 | }, |
| 843 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary") |
| 844 | load("//build/bazel/rules:java.bzl", "java_binary")`, |
| 845 | }, |
| 846 | } |
| 847 | |
| 848 | for _, testCase := range testCases { |
| 849 | actual := testCase.bazelTargets.LoadStatements() |
| 850 | expected := testCase.expectedLoadStatements |
| 851 | if actual != expected { |
| 852 | t.Fatalf("Expected load statements to be %s, got %s", expected, actual) |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | } |
| 857 | |
| 858 | func TestGenerateBazelTargetModules_OneToMany_LoadedFromStarlark(t *testing.T) { |
| 859 | testCases := []struct { |
| 860 | bp string |
| 861 | expectedBazelTarget string |
| 862 | expectedBazelTargetCount int |
| 863 | expectedLoadStatements string |
| 864 | }{ |
| 865 | { |
| 866 | bp: `custom { |
| 867 | name: "bar", |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 868 | host_supported: true, |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 869 | one_to_many_prop: true, |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 870 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 871 | }`, |
| 872 | expectedBazelTarget: `my_library( |
| 873 | name = "bar", |
| 874 | ) |
| 875 | |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 876 | proto_library( |
| 877 | name = "bar_proto_library_deps", |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 878 | ) |
| 879 | |
| 880 | my_proto_library( |
| 881 | name = "bar_my_proto_library_deps", |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 882 | )`, |
| 883 | expectedBazelTargetCount: 3, |
| 884 | expectedLoadStatements: `load("//build/bazel/rules:proto.bzl", "my_proto_library", "proto_library") |
| 885 | load("//build/bazel/rules:rules.bzl", "my_library")`, |
| 886 | }, |
| 887 | } |
| 888 | |
| 889 | dir := "." |
| 890 | for _, testCase := range testCases { |
| 891 | config := android.TestConfig(buildDir, nil, testCase.bp, nil) |
| 892 | ctx := android.NewTestContext(config) |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 893 | ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice) |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 894 | ctx.RegisterForBazelConversion() |
| 895 | |
| 896 | _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) |
| 897 | android.FailIfErrored(t, errs) |
| 898 | _, errs = ctx.ResolveDependencies(config) |
| 899 | android.FailIfErrored(t, errs) |
| 900 | |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 901 | codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build) |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 902 | bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir) |
| 903 | android.FailIfErrored(t, err) |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 904 | if actualCount := len(bazelTargets); actualCount != testCase.expectedBazelTargetCount { |
| 905 | t.Fatalf("Expected %d bazel target, got %d", testCase.expectedBazelTargetCount, actualCount) |
| 906 | } |
| 907 | |
| 908 | actualBazelTargets := bazelTargets.String() |
| 909 | if actualBazelTargets != testCase.expectedBazelTarget { |
| 910 | t.Errorf( |
| 911 | "Expected generated Bazel target to be '%s', got '%s'", |
| 912 | testCase.expectedBazelTarget, |
| 913 | actualBazelTargets, |
| 914 | ) |
| 915 | } |
| 916 | |
| 917 | actualLoadStatements := bazelTargets.LoadStatements() |
| 918 | if actualLoadStatements != testCase.expectedLoadStatements { |
| 919 | t.Errorf( |
| 920 | "Expected generated load statements to be '%s', got '%s'", |
| 921 | testCase.expectedLoadStatements, |
| 922 | actualLoadStatements, |
| 923 | ) |
| 924 | } |
| 925 | } |
| 926 | } |
| 927 | |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 928 | func TestModuleTypeBp2Build(t *testing.T) { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 929 | testCases := []Bp2buildTestCase{ |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 930 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 931 | Description: "filegroup with does not specify srcs", |
| 932 | ModuleTypeUnderTest: "filegroup", |
| 933 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 934 | Blueprint: `filegroup { |
Liz Kammer | ebfcf67 | 2021-02-16 15:00:05 -0500 | [diff] [blame] | 935 | name: "fg_foo", |
| 936 | bazel_module: { bp2build_available: true }, |
| 937 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 938 | ExpectedBazelTargets: []string{ |
| 939 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{}), |
Liz Kammer | ebfcf67 | 2021-02-16 15:00:05 -0500 | [diff] [blame] | 940 | }, |
| 941 | }, |
| 942 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 943 | Description: "filegroup with no srcs", |
| 944 | ModuleTypeUnderTest: "filegroup", |
| 945 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 946 | Blueprint: `filegroup { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 947 | name: "fg_foo", |
| 948 | srcs: [], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 949 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 950 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 951 | ExpectedBazelTargets: []string{ |
| 952 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{}), |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 953 | }, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 954 | }, |
| 955 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 956 | Description: "filegroup with srcs", |
| 957 | ModuleTypeUnderTest: "filegroup", |
| 958 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 959 | Blueprint: `filegroup { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 960 | name: "fg_foo", |
| 961 | srcs: ["a", "b"], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 962 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 963 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 964 | ExpectedBazelTargets: []string{ |
| 965 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 966 | "srcs": `[ |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 967 | "a", |
| 968 | "b", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 969 | ]`, |
| 970 | }), |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 971 | }, |
| 972 | }, |
| 973 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 974 | Description: "filegroup with excludes srcs", |
| 975 | ModuleTypeUnderTest: "filegroup", |
| 976 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 977 | Blueprint: `filegroup { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 978 | name: "fg_foo", |
| 979 | srcs: ["a", "b"], |
| 980 | exclude_srcs: ["a"], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 981 | bazel_module: { bp2build_available: true }, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 982 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 983 | ExpectedBazelTargets: []string{ |
| 984 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 985 | "srcs": `["b"]`, |
| 986 | }), |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 987 | }, |
| 988 | }, |
| 989 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 990 | Description: "depends_on_other_dir_module", |
| 991 | ModuleTypeUnderTest: "filegroup", |
| 992 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 993 | Blueprint: `filegroup { |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 994 | name: "fg_foo", |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 995 | srcs: [ |
Jingwen Chen | b4628eb | 2021-04-08 14:40:57 +0000 | [diff] [blame] | 996 | ":foo", |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 997 | "c", |
| 998 | ], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 999 | bazel_module: { bp2build_available: true }, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 1000 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1001 | Filesystem: map[string]string{ |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 1002 | "other/Android.bp": `filegroup { |
| 1003 | name: "foo", |
| 1004 | srcs: ["a", "b"], |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 1005 | bazel_module: { bp2build_available: true }, |
| 1006 | }`, |
| 1007 | }, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1008 | ExpectedBazelTargets: []string{ |
| 1009 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1010 | "srcs": `[ |
| 1011 | "//other:foo", |
| 1012 | "c", |
| 1013 | ]`, |
| 1014 | }), |
| 1015 | }, |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 1016 | }, |
| 1017 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1018 | Description: "depends_on_other_unconverted_module_error", |
| 1019 | ModuleTypeUnderTest: "filegroup", |
| 1020 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1021 | UnconvertedDepsMode: errorModulesUnconvertedDeps, |
| 1022 | Blueprint: `filegroup { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1023 | name: "foobar", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1024 | srcs: [ |
| 1025 | ":foo", |
| 1026 | "c", |
| 1027 | ], |
| 1028 | bazel_module: { bp2build_available: true }, |
| 1029 | }`, |
Sasha Smundak | f2bb26f | 2022-08-04 11:28:15 -0700 | [diff] [blame] | 1030 | ExpectedErr: fmt.Errorf(`filegroup .:foobar depends on unconverted modules: foo`), |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1031 | Filesystem: map[string]string{ |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1032 | "other/Android.bp": `filegroup { |
| 1033 | name: "foo", |
| 1034 | srcs: ["a", "b"], |
| 1035 | }`, |
| 1036 | }, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 1037 | }, |
| 1038 | } |
| 1039 | |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 1040 | for _, testCase := range testCases { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1041 | t.Run(testCase.Description, func(t *testing.T) { |
| 1042 | RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, testCase) |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 1043 | }) |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 1044 | } |
| 1045 | } |
Jingwen Chen | 041b184 | 2021-02-01 00:23:25 -0500 | [diff] [blame] | 1046 | |
| 1047 | type bp2buildMutator = func(android.TopDownMutatorContext) |
| 1048 | |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1049 | func TestAllowlistingBp2buildTargetsExplicitly(t *testing.T) { |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1050 | testCases := []struct { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1051 | moduleTypeUnderTest string |
| 1052 | moduleTypeUnderTestFactory android.ModuleFactory |
| 1053 | bp string |
| 1054 | expectedCount int |
| 1055 | description string |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1056 | }{ |
| 1057 | { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1058 | description: "explicitly unavailable", |
| 1059 | moduleTypeUnderTest: "filegroup", |
| 1060 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1061 | bp: `filegroup { |
| 1062 | name: "foo", |
| 1063 | srcs: ["a", "b"], |
| 1064 | bazel_module: { bp2build_available: false }, |
| 1065 | }`, |
| 1066 | expectedCount: 0, |
| 1067 | }, |
| 1068 | { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1069 | description: "implicitly unavailable", |
| 1070 | moduleTypeUnderTest: "filegroup", |
| 1071 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1072 | bp: `filegroup { |
| 1073 | name: "foo", |
| 1074 | srcs: ["a", "b"], |
| 1075 | }`, |
| 1076 | expectedCount: 0, |
| 1077 | }, |
| 1078 | { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1079 | description: "explicitly available", |
| 1080 | moduleTypeUnderTest: "filegroup", |
| 1081 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1082 | bp: `filegroup { |
| 1083 | name: "foo", |
| 1084 | srcs: ["a", "b"], |
| 1085 | bazel_module: { bp2build_available: true }, |
| 1086 | }`, |
| 1087 | expectedCount: 1, |
| 1088 | }, |
| 1089 | { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1090 | description: "generates more than 1 target if needed", |
| 1091 | moduleTypeUnderTest: "custom", |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 1092 | moduleTypeUnderTestFactory: customModuleFactoryHostAndDevice, |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1093 | bp: `custom { |
| 1094 | name: "foo", |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1095 | one_to_many_prop: true, |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1096 | bazel_module: { bp2build_available: true }, |
| 1097 | }`, |
| 1098 | expectedCount: 3, |
| 1099 | }, |
| 1100 | } |
| 1101 | |
| 1102 | dir := "." |
| 1103 | for _, testCase := range testCases { |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 1104 | t.Run(testCase.description, func(t *testing.T) { |
| 1105 | config := android.TestConfig(buildDir, nil, testCase.bp, nil) |
| 1106 | ctx := android.NewTestContext(config) |
| 1107 | ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory) |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 1108 | ctx.RegisterForBazelConversion() |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1109 | |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 1110 | _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) |
| 1111 | android.FailIfErrored(t, errs) |
| 1112 | _, errs = ctx.ResolveDependencies(config) |
| 1113 | android.FailIfErrored(t, errs) |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1114 | |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 1115 | codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build) |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 1116 | bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir) |
| 1117 | android.FailIfErrored(t, err) |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 1118 | if actualCount := len(bazelTargets); actualCount != testCase.expectedCount { |
| 1119 | t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, testCase.expectedCount, actualCount) |
| 1120 | } |
| 1121 | }) |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1122 | } |
| 1123 | } |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1124 | |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1125 | func TestAllowlistingBp2buildTargetsWithConfig(t *testing.T) { |
| 1126 | testCases := []struct { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1127 | moduleTypeUnderTest string |
| 1128 | moduleTypeUnderTestFactory android.ModuleFactory |
| 1129 | expectedCount map[string]int |
| 1130 | description string |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 1131 | bp2buildConfig allowlists.Bp2BuildConfig |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1132 | checkDir string |
| 1133 | fs map[string]string |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1134 | }{ |
| 1135 | { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1136 | description: "test bp2build config package and subpackages config", |
| 1137 | moduleTypeUnderTest: "filegroup", |
| 1138 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1139 | expectedCount: map[string]int{ |
| 1140 | "migrated": 1, |
| 1141 | "migrated/but_not_really": 0, |
| 1142 | "migrated/but_not_really/but_really": 1, |
| 1143 | "not_migrated": 0, |
| 1144 | "also_not_migrated": 0, |
| 1145 | }, |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 1146 | bp2buildConfig: allowlists.Bp2BuildConfig{ |
| 1147 | "migrated": allowlists.Bp2BuildDefaultTrueRecursively, |
| 1148 | "migrated/but_not_really": allowlists.Bp2BuildDefaultFalse, |
| 1149 | "not_migrated": allowlists.Bp2BuildDefaultFalse, |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1150 | }, |
| 1151 | fs: map[string]string{ |
| 1152 | "migrated/Android.bp": `filegroup { name: "a" }`, |
| 1153 | "migrated/but_not_really/Android.bp": `filegroup { name: "b" }`, |
| 1154 | "migrated/but_not_really/but_really/Android.bp": `filegroup { name: "c" }`, |
| 1155 | "not_migrated/Android.bp": `filegroup { name: "d" }`, |
| 1156 | "also_not_migrated/Android.bp": `filegroup { name: "e" }`, |
| 1157 | }, |
| 1158 | }, |
| 1159 | { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1160 | description: "test bp2build config opt-in and opt-out", |
| 1161 | moduleTypeUnderTest: "filegroup", |
| 1162 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1163 | expectedCount: map[string]int{ |
| 1164 | "package-opt-in": 2, |
| 1165 | "package-opt-in/subpackage": 0, |
| 1166 | "package-opt-out": 1, |
| 1167 | "package-opt-out/subpackage": 0, |
| 1168 | }, |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 1169 | bp2buildConfig: allowlists.Bp2BuildConfig{ |
| 1170 | "package-opt-in": allowlists.Bp2BuildDefaultFalse, |
| 1171 | "package-opt-out": allowlists.Bp2BuildDefaultTrueRecursively, |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1172 | }, |
| 1173 | fs: map[string]string{ |
| 1174 | "package-opt-in/Android.bp": ` |
| 1175 | filegroup { name: "opt-in-a" } |
| 1176 | filegroup { name: "opt-in-b", bazel_module: { bp2build_available: true } } |
| 1177 | filegroup { name: "opt-in-c", bazel_module: { bp2build_available: true } } |
| 1178 | `, |
| 1179 | |
| 1180 | "package-opt-in/subpackage/Android.bp": ` |
| 1181 | filegroup { name: "opt-in-d" } // parent package not configured to DefaultTrueRecursively |
| 1182 | `, |
| 1183 | |
| 1184 | "package-opt-out/Android.bp": ` |
| 1185 | filegroup { name: "opt-out-a" } |
| 1186 | filegroup { name: "opt-out-b", bazel_module: { bp2build_available: false } } |
| 1187 | filegroup { name: "opt-out-c", bazel_module: { bp2build_available: false } } |
| 1188 | `, |
| 1189 | |
| 1190 | "package-opt-out/subpackage/Android.bp": ` |
| 1191 | filegroup { name: "opt-out-g", bazel_module: { bp2build_available: false } } |
| 1192 | filegroup { name: "opt-out-h", bazel_module: { bp2build_available: false } } |
| 1193 | `, |
| 1194 | }, |
| 1195 | }, |
| 1196 | } |
| 1197 | |
| 1198 | dir := "." |
| 1199 | for _, testCase := range testCases { |
| 1200 | fs := make(map[string][]byte) |
| 1201 | toParse := []string{ |
| 1202 | "Android.bp", |
| 1203 | } |
| 1204 | for f, content := range testCase.fs { |
| 1205 | if strings.HasSuffix(f, "Android.bp") { |
| 1206 | toParse = append(toParse, f) |
| 1207 | } |
| 1208 | fs[f] = []byte(content) |
| 1209 | } |
| 1210 | config := android.TestConfig(buildDir, nil, "", fs) |
| 1211 | ctx := android.NewTestContext(config) |
| 1212 | ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory) |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 1213 | allowlist := android.NewBp2BuildAllowlist().SetDefaultConfig(testCase.bp2buildConfig) |
| 1214 | ctx.RegisterBp2BuildConfig(allowlist) |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1215 | ctx.RegisterForBazelConversion() |
| 1216 | |
| 1217 | _, errs := ctx.ParseFileList(dir, toParse) |
| 1218 | android.FailIfErrored(t, errs) |
| 1219 | _, errs = ctx.ResolveDependencies(config) |
| 1220 | android.FailIfErrored(t, errs) |
| 1221 | |
| 1222 | codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build) |
| 1223 | |
| 1224 | // For each directory, test that the expected number of generated targets is correct. |
| 1225 | for dir, expectedCount := range testCase.expectedCount { |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 1226 | bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir) |
| 1227 | android.FailIfErrored(t, err) |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1228 | if actualCount := len(bazelTargets); actualCount != expectedCount { |
| 1229 | t.Fatalf( |
| 1230 | "%s: Expected %d bazel target for %s package, got %d", |
| 1231 | testCase.description, |
| 1232 | expectedCount, |
| 1233 | dir, |
| 1234 | actualCount) |
| 1235 | } |
| 1236 | |
| 1237 | } |
| 1238 | } |
| 1239 | } |
| 1240 | |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1241 | func TestCombineBuildFilesBp2buildTargets(t *testing.T) { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1242 | testCases := []Bp2buildTestCase{ |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1243 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1244 | Description: "filegroup bazel_module.label", |
| 1245 | ModuleTypeUnderTest: "filegroup", |
| 1246 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1247 | Blueprint: `filegroup { |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1248 | name: "fg_foo", |
| 1249 | bazel_module: { label: "//other:fg_foo" }, |
| 1250 | }`, |
Cole Faust | ea602c5 | 2022-08-31 14:48:26 -0700 | [diff] [blame] | 1251 | ExpectedBazelTargets: []string{}, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1252 | Filesystem: map[string]string{ |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1253 | "other/BUILD.bazel": `// BUILD file`, |
| 1254 | }, |
| 1255 | }, |
| 1256 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1257 | Description: "multiple bazel_module.label same BUILD", |
| 1258 | ModuleTypeUnderTest: "filegroup", |
| 1259 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1260 | Blueprint: `filegroup { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1261 | name: "fg_foo", |
| 1262 | bazel_module: { label: "//other:fg_foo" }, |
| 1263 | } |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1264 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1265 | filegroup { |
| 1266 | name: "foo", |
| 1267 | bazel_module: { label: "//other:foo" }, |
| 1268 | }`, |
Cole Faust | ea602c5 | 2022-08-31 14:48:26 -0700 | [diff] [blame] | 1269 | ExpectedBazelTargets: []string{}, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1270 | Filesystem: map[string]string{ |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1271 | "other/BUILD.bazel": `// BUILD file`, |
| 1272 | }, |
| 1273 | }, |
| 1274 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1275 | Description: "filegroup bazel_module.label and bp2build in subdir", |
| 1276 | ModuleTypeUnderTest: "filegroup", |
| 1277 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1278 | Dir: "other", |
| 1279 | Blueprint: ``, |
| 1280 | Filesystem: map[string]string{ |
Jingwen Chen | c63677b | 2021-06-17 05:43:19 +0000 | [diff] [blame] | 1281 | "other/Android.bp": `filegroup { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1282 | name: "fg_foo", |
| 1283 | bazel_module: { |
| 1284 | bp2build_available: true, |
| 1285 | }, |
| 1286 | } |
| 1287 | filegroup { |
| 1288 | name: "fg_bar", |
| 1289 | bazel_module: { |
| 1290 | label: "//other:fg_bar" |
| 1291 | }, |
| 1292 | }`, |
Jingwen Chen | c63677b | 2021-06-17 05:43:19 +0000 | [diff] [blame] | 1293 | "other/BUILD.bazel": `// definition for fg_bar`, |
| 1294 | }, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1295 | ExpectedBazelTargets: []string{ |
| 1296 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{}), |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1297 | }, |
| 1298 | }, |
| 1299 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1300 | Description: "filegroup bazel_module.label and filegroup bp2build", |
| 1301 | ModuleTypeUnderTest: "filegroup", |
| 1302 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1303 | |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1304 | Filesystem: map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1305 | "other/BUILD.bazel": `// BUILD file`, |
| 1306 | }, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1307 | Blueprint: `filegroup { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1308 | name: "fg_foo", |
| 1309 | bazel_module: { |
| 1310 | label: "//other:fg_foo", |
| 1311 | }, |
| 1312 | } |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1313 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1314 | filegroup { |
| 1315 | name: "fg_bar", |
| 1316 | bazel_module: { |
| 1317 | bp2build_available: true, |
| 1318 | }, |
| 1319 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1320 | ExpectedBazelTargets: []string{ |
| 1321 | MakeBazelTargetNoRestrictions("filegroup", "fg_bar", map[string]string{}), |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1322 | }, |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1323 | }, |
| 1324 | } |
| 1325 | |
| 1326 | dir := "." |
| 1327 | for _, testCase := range testCases { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1328 | t.Run(testCase.Description, func(t *testing.T) { |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1329 | fs := make(map[string][]byte) |
| 1330 | toParse := []string{ |
| 1331 | "Android.bp", |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1332 | } |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1333 | for f, content := range testCase.Filesystem { |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1334 | if strings.HasSuffix(f, "Android.bp") { |
| 1335 | toParse = append(toParse, f) |
| 1336 | } |
| 1337 | fs[f] = []byte(content) |
| 1338 | } |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1339 | config := android.TestConfig(buildDir, nil, testCase.Blueprint, fs) |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1340 | ctx := android.NewTestContext(config) |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1341 | ctx.RegisterModuleType(testCase.ModuleTypeUnderTest, testCase.ModuleTypeUnderTestFactory) |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1342 | ctx.RegisterForBazelConversion() |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1343 | |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1344 | _, errs := ctx.ParseFileList(dir, toParse) |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 1345 | if errored(t, testCase, errs) { |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1346 | return |
| 1347 | } |
| 1348 | _, errs = ctx.ResolveDependencies(config) |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 1349 | if errored(t, testCase, errs) { |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1350 | return |
| 1351 | } |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1352 | |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1353 | checkDir := dir |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1354 | if testCase.Dir != "" { |
| 1355 | checkDir = testCase.Dir |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1356 | } |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 1357 | codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build) |
| 1358 | bazelTargets, err := generateBazelTargetsForDir(codegenCtx, checkDir) |
| 1359 | android.FailIfErrored(t, err) |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1360 | bazelTargets.sort() |
| 1361 | actualCount := len(bazelTargets) |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1362 | expectedCount := len(testCase.ExpectedBazelTargets) |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1363 | if actualCount != expectedCount { |
| 1364 | t.Errorf("Expected %d bazel target, got %d\n%s", expectedCount, actualCount, bazelTargets) |
| 1365 | } |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1366 | for i, target := range bazelTargets { |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1367 | actualContent := target.content |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1368 | expectedContent := testCase.ExpectedBazelTargets[i] |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1369 | if expectedContent != actualContent { |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1370 | t.Errorf( |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1371 | "Expected generated Bazel target to be '%s', got '%s'", |
| 1372 | expectedContent, |
| 1373 | actualContent, |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1374 | ) |
| 1375 | } |
| 1376 | } |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1377 | }) |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1378 | } |
| 1379 | } |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1380 | |
Jingwen Chen | 0eeaeb8 | 2022-09-21 10:27:42 +0000 | [diff] [blame] | 1381 | func TestGlob(t *testing.T) { |
| 1382 | testCases := []Bp2buildTestCase{ |
| 1383 | { |
| 1384 | Description: "filegroup with glob", |
| 1385 | ModuleTypeUnderTest: "filegroup", |
| 1386 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1387 | Blueprint: `filegroup { |
| 1388 | name: "fg_foo", |
| 1389 | srcs: ["**/*.txt"], |
| 1390 | bazel_module: { bp2build_available: true }, |
| 1391 | }`, |
| 1392 | ExpectedBazelTargets: []string{ |
| 1393 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
| 1394 | "srcs": `[ |
| 1395 | "other/a.txt", |
| 1396 | "other/b.txt", |
| 1397 | "other/subdir/a.txt", |
| 1398 | ]`, |
| 1399 | }), |
| 1400 | }, |
| 1401 | Filesystem: map[string]string{ |
| 1402 | "other/a.txt": "", |
| 1403 | "other/b.txt": "", |
| 1404 | "other/subdir/a.txt": "", |
| 1405 | "other/file": "", |
| 1406 | }, |
| 1407 | }, |
| 1408 | { |
| 1409 | Description: "filegroup with glob in subdir", |
| 1410 | ModuleTypeUnderTest: "filegroup", |
| 1411 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1412 | Dir: "other", |
| 1413 | Filesystem: map[string]string{ |
| 1414 | "other/Android.bp": `filegroup { |
| 1415 | name: "fg_foo", |
| 1416 | srcs: ["**/*.txt"], |
| 1417 | bazel_module: { bp2build_available: true }, |
| 1418 | }`, |
| 1419 | "other/a.txt": "", |
| 1420 | "other/b.txt": "", |
| 1421 | "other/subdir/a.txt": "", |
| 1422 | "other/file": "", |
| 1423 | }, |
| 1424 | ExpectedBazelTargets: []string{ |
| 1425 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
| 1426 | "srcs": `[ |
| 1427 | "a.txt", |
| 1428 | "b.txt", |
| 1429 | "subdir/a.txt", |
| 1430 | ]`, |
| 1431 | }), |
| 1432 | }, |
| 1433 | }, |
| 1434 | { |
| 1435 | Description: "filegroup with glob with no kept BUILD files", |
| 1436 | ModuleTypeUnderTest: "filegroup", |
| 1437 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1438 | KeepBuildFileForDirs: []string{ |
| 1439 | // empty |
| 1440 | }, |
| 1441 | Blueprint: `filegroup { |
| 1442 | name: "fg_foo", |
| 1443 | srcs: ["**/*.txt"], |
| 1444 | bazel_module: { bp2build_available: true }, |
| 1445 | }`, |
| 1446 | Filesystem: map[string]string{ |
| 1447 | "a.txt": "", |
| 1448 | "b.txt": "", |
| 1449 | "foo/BUILD": "", |
| 1450 | "foo/a.txt": "", |
| 1451 | "foo/bar/BUILD": "", |
| 1452 | "foo/bar/b.txt": "", |
| 1453 | }, |
| 1454 | ExpectedBazelTargets: []string{ |
| 1455 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
| 1456 | "srcs": `[ |
| 1457 | "a.txt", |
| 1458 | "b.txt", |
| 1459 | "foo/a.txt", |
| 1460 | "foo/bar/b.txt", |
| 1461 | ]`, |
| 1462 | }), |
| 1463 | }, |
| 1464 | }, |
| 1465 | { |
| 1466 | Description: "filegroup with glob with kept BUILD file", |
| 1467 | ModuleTypeUnderTest: "filegroup", |
| 1468 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1469 | KeepBuildFileForDirs: []string{ |
| 1470 | "foo", |
| 1471 | }, |
| 1472 | Blueprint: `filegroup { |
| 1473 | name: "fg_foo", |
| 1474 | srcs: ["**/*.txt"], |
| 1475 | bazel_module: { bp2build_available: true }, |
| 1476 | }`, |
| 1477 | Filesystem: map[string]string{ |
| 1478 | "a.txt": "", |
| 1479 | "b.txt": "", |
| 1480 | "foo/BUILD": "", |
| 1481 | "foo/a.txt": "", |
| 1482 | "foo/bar/BUILD": "", |
| 1483 | "foo/bar/b.txt": "", |
| 1484 | }, |
| 1485 | ExpectedBazelTargets: []string{ |
| 1486 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
| 1487 | "srcs": `[ |
| 1488 | "a.txt", |
| 1489 | "b.txt", |
| 1490 | "//foo:a.txt", |
| 1491 | "//foo:bar/b.txt", |
| 1492 | ]`, |
| 1493 | }), |
| 1494 | }, |
| 1495 | }, |
| 1496 | { |
| 1497 | Description: "filegroup with glob with kept BUILD.bazel file", |
| 1498 | ModuleTypeUnderTest: "filegroup", |
| 1499 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1500 | KeepBuildFileForDirs: []string{ |
| 1501 | "foo", |
| 1502 | }, |
| 1503 | Blueprint: `filegroup { |
| 1504 | name: "fg_foo", |
| 1505 | srcs: ["**/*.txt"], |
| 1506 | bazel_module: { bp2build_available: true }, |
| 1507 | }`, |
| 1508 | Filesystem: map[string]string{ |
| 1509 | "a.txt": "", |
| 1510 | "b.txt": "", |
| 1511 | "foo/BUILD.bazel": "", |
| 1512 | "foo/a.txt": "", |
| 1513 | "foo/bar/BUILD.bazel": "", |
| 1514 | "foo/bar/b.txt": "", |
| 1515 | }, |
| 1516 | ExpectedBazelTargets: []string{ |
| 1517 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
| 1518 | "srcs": `[ |
| 1519 | "a.txt", |
| 1520 | "b.txt", |
| 1521 | "//foo:a.txt", |
| 1522 | "//foo:bar/b.txt", |
| 1523 | ]`, |
| 1524 | }), |
| 1525 | }, |
| 1526 | }, |
| 1527 | { |
| 1528 | Description: "filegroup with glob with Android.bp file as boundary", |
| 1529 | ModuleTypeUnderTest: "filegroup", |
| 1530 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1531 | Blueprint: `filegroup { |
| 1532 | name: "fg_foo", |
| 1533 | srcs: ["**/*.txt"], |
| 1534 | bazel_module: { bp2build_available: true }, |
| 1535 | }`, |
| 1536 | Filesystem: map[string]string{ |
| 1537 | "a.txt": "", |
| 1538 | "b.txt": "", |
| 1539 | "foo/Android.bp": "", |
| 1540 | "foo/a.txt": "", |
| 1541 | "foo/bar/Android.bp": "", |
| 1542 | "foo/bar/b.txt": "", |
| 1543 | }, |
| 1544 | ExpectedBazelTargets: []string{ |
| 1545 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
| 1546 | "srcs": `[ |
| 1547 | "a.txt", |
| 1548 | "b.txt", |
| 1549 | "//foo:a.txt", |
| 1550 | "//foo/bar:b.txt", |
| 1551 | ]`, |
| 1552 | }), |
| 1553 | }, |
| 1554 | }, |
| 1555 | { |
| 1556 | Description: "filegroup with glob in subdir with kept BUILD and BUILD.bazel file", |
| 1557 | ModuleTypeUnderTest: "filegroup", |
| 1558 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1559 | Dir: "other", |
| 1560 | KeepBuildFileForDirs: []string{ |
| 1561 | "other/foo", |
| 1562 | "other/foo/bar", |
| 1563 | // deliberately not other/foo/baz/BUILD. |
| 1564 | }, |
| 1565 | Filesystem: map[string]string{ |
| 1566 | "other/Android.bp": `filegroup { |
| 1567 | name: "fg_foo", |
| 1568 | srcs: ["**/*.txt"], |
| 1569 | bazel_module: { bp2build_available: true }, |
| 1570 | }`, |
| 1571 | "other/a.txt": "", |
| 1572 | "other/b.txt": "", |
| 1573 | "other/foo/BUILD": "", |
| 1574 | "other/foo/a.txt": "", |
| 1575 | "other/foo/bar/BUILD.bazel": "", |
| 1576 | "other/foo/bar/b.txt": "", |
| 1577 | "other/foo/baz/BUILD": "", |
| 1578 | "other/foo/baz/c.txt": "", |
| 1579 | }, |
| 1580 | ExpectedBazelTargets: []string{ |
| 1581 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
| 1582 | "srcs": `[ |
| 1583 | "a.txt", |
| 1584 | "b.txt", |
| 1585 | "//other/foo:a.txt", |
| 1586 | "//other/foo/bar:b.txt", |
| 1587 | "//other/foo:baz/c.txt", |
| 1588 | ]`, |
| 1589 | }), |
| 1590 | }, |
| 1591 | }, |
| 1592 | } |
| 1593 | |
| 1594 | for _, testCase := range testCases { |
| 1595 | t.Run(testCase.Description, func(t *testing.T) { |
| 1596 | RunBp2BuildTestCaseSimple(t, testCase) |
| 1597 | }) |
| 1598 | } |
| 1599 | } |
| 1600 | |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1601 | func TestGlobExcludeSrcs(t *testing.T) { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1602 | testCases := []Bp2buildTestCase{ |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1603 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1604 | Description: "filegroup top level exclude_srcs", |
| 1605 | ModuleTypeUnderTest: "filegroup", |
| 1606 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1607 | Blueprint: `filegroup { |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1608 | name: "fg_foo", |
| 1609 | srcs: ["**/*.txt"], |
| 1610 | exclude_srcs: ["c.txt"], |
| 1611 | bazel_module: { bp2build_available: true }, |
| 1612 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1613 | Filesystem: map[string]string{ |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1614 | "a.txt": "", |
| 1615 | "b.txt": "", |
| 1616 | "c.txt": "", |
| 1617 | "dir/Android.bp": "", |
| 1618 | "dir/e.txt": "", |
| 1619 | "dir/f.txt": "", |
| 1620 | }, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1621 | ExpectedBazelTargets: []string{ |
| 1622 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1623 | "srcs": `[ |
| 1624 | "a.txt", |
| 1625 | "b.txt", |
| 1626 | "//dir:e.txt", |
| 1627 | "//dir:f.txt", |
| 1628 | ]`, |
| 1629 | }), |
| 1630 | }, |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1631 | }, |
| 1632 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1633 | Description: "filegroup in subdir exclude_srcs", |
| 1634 | ModuleTypeUnderTest: "filegroup", |
| 1635 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1636 | Blueprint: "", |
| 1637 | Dir: "dir", |
| 1638 | Filesystem: map[string]string{ |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1639 | "dir/Android.bp": `filegroup { |
| 1640 | name: "fg_foo", |
| 1641 | srcs: ["**/*.txt"], |
| 1642 | exclude_srcs: ["b.txt"], |
| 1643 | bazel_module: { bp2build_available: true }, |
| 1644 | } |
| 1645 | `, |
| 1646 | "dir/a.txt": "", |
| 1647 | "dir/b.txt": "", |
| 1648 | "dir/subdir/Android.bp": "", |
| 1649 | "dir/subdir/e.txt": "", |
| 1650 | "dir/subdir/f.txt": "", |
| 1651 | }, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1652 | ExpectedBazelTargets: []string{ |
| 1653 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1654 | "srcs": `[ |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 1655 | "a.txt", |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1656 | "//dir/subdir:e.txt", |
| 1657 | "//dir/subdir:f.txt", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1658 | ]`, |
| 1659 | }), |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1660 | }, |
| 1661 | }, |
| 1662 | } |
| 1663 | |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1664 | for _, testCase := range testCases { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1665 | t.Run(testCase.Description, func(t *testing.T) { |
Trevor Radcliffe | 1b4b2d9 | 2022-09-01 18:57:01 +0000 | [diff] [blame] | 1666 | RunBp2BuildTestCaseSimple(t, testCase) |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1667 | }) |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1668 | } |
| 1669 | } |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1670 | |
| 1671 | func TestCommonBp2BuildModuleAttrs(t *testing.T) { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1672 | testCases := []Bp2buildTestCase{ |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1673 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1674 | Description: "Required into data test", |
| 1675 | ModuleTypeUnderTest: "filegroup", |
| 1676 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1677 | 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] | 1678 | filegroup { |
| 1679 | name: "fg_foo", |
| 1680 | required: ["reqd"], |
| 1681 | bazel_module: { bp2build_available: true }, |
| 1682 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1683 | ExpectedBazelTargets: []string{ |
| 1684 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1685 | "data": `[":reqd"]`, |
| 1686 | }), |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1687 | }, |
| 1688 | }, |
| 1689 | { |
Jingwen Chen | a5ecb37 | 2022-09-21 09:05:37 +0000 | [diff] [blame^] | 1690 | Description: "Required into data test, cyclic self reference is filtered out", |
| 1691 | ModuleTypeUnderTest: "filegroup", |
| 1692 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1693 | Blueprint: simpleModuleDoNotConvertBp2build("filegroup", "reqd") + ` |
| 1694 | filegroup { |
| 1695 | name: "fg_foo", |
| 1696 | required: ["reqd", "fg_foo"], |
| 1697 | bazel_module: { bp2build_available: true }, |
| 1698 | }`, |
| 1699 | ExpectedBazelTargets: []string{ |
| 1700 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
| 1701 | "data": `[":reqd"]`, |
| 1702 | }), |
| 1703 | }, |
| 1704 | }, |
| 1705 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1706 | Description: "Required via arch into data test", |
| 1707 | ModuleTypeUnderTest: "python_library", |
| 1708 | ModuleTypeUnderTestFactory: python.PythonLibraryFactory, |
| 1709 | Blueprint: simpleModuleDoNotConvertBp2build("python_library", "reqdx86") + |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1710 | 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] | 1711 | python_library { |
| 1712 | name: "fg_foo", |
| 1713 | arch: { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1714 | arm: { |
| 1715 | required: ["reqdarm"], |
| 1716 | }, |
| 1717 | x86: { |
| 1718 | required: ["reqdx86"], |
| 1719 | }, |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1720 | }, |
| 1721 | bazel_module: { bp2build_available: true }, |
| 1722 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1723 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 1724 | MakeBazelTarget("py_library", "fg_foo", map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1725 | "data": `select({ |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1726 | "//build/bazel/platforms/arch:arm": [":reqdarm"], |
| 1727 | "//build/bazel/platforms/arch:x86": [":reqdx86"], |
| 1728 | "//conditions:default": [], |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1729 | })`, |
| 1730 | "srcs_version": `"PY3"`, |
Cole Faust | b09da7e | 2022-05-18 10:57:33 -0700 | [diff] [blame] | 1731 | "imports": `["."]`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1732 | }), |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1733 | }, |
| 1734 | }, |
| 1735 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1736 | Description: "Required appended to data test", |
| 1737 | ModuleTypeUnderTest: "python_library", |
| 1738 | ModuleTypeUnderTestFactory: python.PythonLibraryFactory, |
| 1739 | Filesystem: map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1740 | "data.bin": "", |
| 1741 | "src.py": "", |
| 1742 | }, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1743 | 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] | 1744 | python_library { |
| 1745 | name: "fg_foo", |
| 1746 | data: ["data.bin"], |
| 1747 | required: ["reqd"], |
| 1748 | bazel_module: { bp2build_available: true }, |
| 1749 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1750 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 1751 | MakeBazelTarget("py_library", "fg_foo", map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1752 | "data": `[ |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1753 | "data.bin", |
| 1754 | ":reqd", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1755 | ]`, |
| 1756 | "srcs_version": `"PY3"`, |
Cole Faust | b09da7e | 2022-05-18 10:57:33 -0700 | [diff] [blame] | 1757 | "imports": `["."]`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1758 | }), |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1759 | }, |
| 1760 | }, |
| 1761 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1762 | Description: "All props-to-attrs at once together test", |
| 1763 | ModuleTypeUnderTest: "filegroup", |
| 1764 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1765 | 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] | 1766 | filegroup { |
| 1767 | name: "fg_foo", |
| 1768 | required: ["reqd"], |
| 1769 | bazel_module: { bp2build_available: true }, |
| 1770 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1771 | ExpectedBazelTargets: []string{ |
| 1772 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1773 | "data": `[":reqd"]`, |
| 1774 | }), |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1775 | }, |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1776 | }, |
| 1777 | } |
| 1778 | |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1779 | for _, tc := range testCases { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1780 | t.Run(tc.Description, func(t *testing.T) { |
Trevor Radcliffe | 1b4b2d9 | 2022-09-01 18:57:01 +0000 | [diff] [blame] | 1781 | RunBp2BuildTestCaseSimple(t, tc) |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1782 | }) |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1783 | } |
| 1784 | } |