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" |
Spandan Das | 921af32 | 2023-04-26 02:56:37 +0000 | [diff] [blame] | 24 | "android/soong/bazel" |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 25 | "android/soong/python" |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | func TestGenerateSoongModuleTargets(t *testing.T) { |
| 29 | testCases := []struct { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 30 | description string |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 31 | bp string |
| 32 | expectedBazelTarget string |
| 33 | }{ |
| 34 | { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 35 | description: "only name", |
Jingwen Chen | b4628eb | 2021-04-08 14:40:57 +0000 | [diff] [blame] | 36 | bp: `custom { name: "foo" } |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 37 | `, |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 38 | expectedBazelTarget: `soong_module( |
| 39 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 40 | soong_module_name = "foo", |
| 41 | soong_module_type = "custom", |
| 42 | soong_module_variant = "", |
| 43 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 44 | ], |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 45 | bool_prop = False, |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 46 | string_prop = "", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 47 | )`, |
| 48 | }, |
| 49 | { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 50 | description: "handles bool", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 51 | bp: `custom { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 52 | name: "foo", |
| 53 | bool_prop: true, |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 54 | } |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 55 | `, |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 56 | expectedBazelTarget: `soong_module( |
| 57 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 58 | soong_module_name = "foo", |
| 59 | soong_module_type = "custom", |
| 60 | soong_module_variant = "", |
| 61 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 62 | ], |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 63 | bool_prop = True, |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 64 | string_prop = "", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 65 | )`, |
| 66 | }, |
| 67 | { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 68 | description: "string escaping", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 69 | bp: `custom { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 70 | name: "foo", |
| 71 | owner: "a_string_with\"quotes\"_and_\\backslashes\\\\", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 72 | } |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 73 | `, |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 74 | expectedBazelTarget: `soong_module( |
| 75 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 76 | soong_module_name = "foo", |
| 77 | soong_module_type = "custom", |
| 78 | soong_module_variant = "", |
| 79 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 80 | ], |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 81 | bool_prop = False, |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 82 | owner = "a_string_with\"quotes\"_and_\\backslashes\\\\", |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 83 | string_prop = "", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 84 | )`, |
| 85 | }, |
| 86 | { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 87 | description: "single item string list", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 88 | bp: `custom { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 89 | name: "foo", |
| 90 | required: ["bar"], |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 91 | } |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 92 | `, |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 93 | expectedBazelTarget: `soong_module( |
| 94 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 95 | soong_module_name = "foo", |
| 96 | soong_module_type = "custom", |
| 97 | soong_module_variant = "", |
| 98 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 99 | ], |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 100 | bool_prop = False, |
Jingwen Chen | b4628eb | 2021-04-08 14:40:57 +0000 | [diff] [blame] | 101 | required = ["bar"], |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 102 | string_prop = "", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 103 | )`, |
| 104 | }, |
| 105 | { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 106 | description: "list of strings", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 107 | bp: `custom { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 108 | name: "foo", |
| 109 | target_required: ["qux", "bazqux"], |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 110 | } |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 111 | `, |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 112 | expectedBazelTarget: `soong_module( |
| 113 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 114 | soong_module_name = "foo", |
| 115 | soong_module_type = "custom", |
| 116 | soong_module_variant = "", |
| 117 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 118 | ], |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 119 | bool_prop = False, |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 120 | string_prop = "", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 121 | target_required = [ |
| 122 | "qux", |
| 123 | "bazqux", |
| 124 | ], |
| 125 | )`, |
| 126 | }, |
| 127 | { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 128 | description: "dist/dists", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 129 | bp: `custom { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 130 | name: "foo", |
| 131 | dist: { |
| 132 | targets: ["goal_foo"], |
| 133 | tag: ".foo", |
| 134 | }, |
| 135 | dists: [{ |
| 136 | targets: ["goal_bar"], |
| 137 | tag: ".bar", |
| 138 | }], |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 139 | } |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 140 | `, |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 141 | expectedBazelTarget: `soong_module( |
| 142 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 143 | soong_module_name = "foo", |
| 144 | soong_module_type = "custom", |
| 145 | soong_module_variant = "", |
| 146 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 147 | ], |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 148 | bool_prop = False, |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 149 | dist = { |
| 150 | "tag": ".foo", |
Jingwen Chen | b4628eb | 2021-04-08 14:40:57 +0000 | [diff] [blame] | 151 | "targets": ["goal_foo"], |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 152 | }, |
Jingwen Chen | b4628eb | 2021-04-08 14:40:57 +0000 | [diff] [blame] | 153 | dists = [{ |
| 154 | "tag": ".bar", |
| 155 | "targets": ["goal_bar"], |
| 156 | }], |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 157 | string_prop = "", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 158 | )`, |
| 159 | }, |
| 160 | { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 161 | description: "put it together", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 162 | bp: `custom { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 163 | name: "foo", |
| 164 | required: ["bar"], |
| 165 | target_required: ["qux", "bazqux"], |
| 166 | bool_prop: true, |
| 167 | owner: "custom_owner", |
| 168 | dists: [ |
| 169 | { |
| 170 | tag: ".tag", |
| 171 | targets: ["my_goal"], |
| 172 | }, |
| 173 | ], |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 174 | } |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 175 | `, |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 176 | expectedBazelTarget: `soong_module( |
| 177 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 178 | soong_module_name = "foo", |
| 179 | soong_module_type = "custom", |
| 180 | soong_module_variant = "", |
| 181 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 182 | ], |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 183 | bool_prop = True, |
Jingwen Chen | b4628eb | 2021-04-08 14:40:57 +0000 | [diff] [blame] | 184 | dists = [{ |
| 185 | "tag": ".tag", |
| 186 | "targets": ["my_goal"], |
| 187 | }], |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 188 | owner = "custom_owner", |
Jingwen Chen | b4628eb | 2021-04-08 14:40:57 +0000 | [diff] [blame] | 189 | required = ["bar"], |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 190 | string_prop = "", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 191 | target_required = [ |
| 192 | "qux", |
| 193 | "bazqux", |
| 194 | ], |
| 195 | )`, |
| 196 | }, |
| 197 | } |
| 198 | |
| 199 | dir := "." |
| 200 | for _, testCase := range testCases { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 201 | t.Run(testCase.description, func(t *testing.T) { |
| 202 | config := android.TestConfig(buildDir, nil, testCase.bp, nil) |
| 203 | ctx := android.NewTestContext(config) |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 204 | |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 205 | ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice) |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 206 | ctx.Register() |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 207 | |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 208 | _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) |
| 209 | android.FailIfErrored(t, errs) |
| 210 | _, errs = ctx.PrepareBuildActions(config) |
| 211 | android.FailIfErrored(t, errs) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 212 | |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 213 | codegenCtx := NewCodegenContext(config, ctx.Context, QueryView, "") |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 214 | bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir) |
| 215 | android.FailIfErrored(t, err) |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 216 | if actualCount, expectedCount := len(bazelTargets), 1; actualCount != expectedCount { |
| 217 | t.Fatalf("Expected %d bazel target, got %d", expectedCount, actualCount) |
| 218 | } |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 219 | |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 220 | actualBazelTarget := bazelTargets[0] |
| 221 | if actualBazelTarget.content != testCase.expectedBazelTarget { |
| 222 | t.Errorf( |
| 223 | "Expected generated Bazel target to be '%s', got '%s'", |
| 224 | testCase.expectedBazelTarget, |
| 225 | actualBazelTarget.content, |
| 226 | ) |
| 227 | } |
| 228 | }) |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
| 232 | func TestGenerateBazelTargetModules(t *testing.T) { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 233 | testCases := []Bp2buildTestCase{ |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 234 | { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 3a019a6 | 2022-06-23 16:02:44 +0000 | [diff] [blame] | 235 | Description: "string prop empty", |
| 236 | Blueprint: `custom { |
| 237 | name: "foo", |
| 238 | string_literal_prop: "", |
| 239 | bazel_module: { bp2build_available: true }, |
| 240 | }`, |
| 241 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 242 | 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] | 243 | "string_literal_prop": `""`, |
| 244 | }), |
| 245 | }, |
| 246 | }, |
| 247 | { |
| 248 | Description: `string prop "PROP"`, |
| 249 | Blueprint: `custom { |
| 250 | name: "foo", |
| 251 | string_literal_prop: "PROP", |
| 252 | bazel_module: { bp2build_available: true }, |
| 253 | }`, |
| 254 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 255 | 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] | 256 | "string_literal_prop": `"PROP"`, |
| 257 | }), |
| 258 | }, |
| 259 | }, |
| 260 | { |
| 261 | Description: `string prop arch variant`, |
| 262 | Blueprint: `custom { |
| 263 | name: "foo", |
| 264 | arch: { |
| 265 | arm: { string_literal_prop: "ARM" }, |
| 266 | arm64: { string_literal_prop: "ARM64" }, |
| 267 | }, |
| 268 | bazel_module: { bp2build_available: true }, |
| 269 | }`, |
| 270 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 271 | 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] | 272 | "string_literal_prop": `select({ |
| 273 | "//build/bazel/platforms/arch:arm": "ARM", |
| 274 | "//build/bazel/platforms/arch:arm64": "ARM64", |
| 275 | "//conditions:default": None, |
| 276 | })`, |
| 277 | }), |
| 278 | }, |
| 279 | }, |
| 280 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 281 | Description: "string ptr props", |
| 282 | Blueprint: `custom { |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 283 | name: "foo", |
| 284 | string_ptr_prop: "", |
| 285 | bazel_module: { bp2build_available: true }, |
| 286 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 287 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 288 | MakeBazelTarget("custom", "foo", AttrNameToString{ |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 289 | "string_ptr_prop": `""`, |
| 290 | }), |
| 291 | }, |
| 292 | }, |
| 293 | { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 3a019a6 | 2022-06-23 16:02:44 +0000 | [diff] [blame] | 294 | Description: "string list props", |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 295 | Blueprint: `custom { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 296 | name: "foo", |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 297 | string_list_prop: ["a", "b"], |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 298 | string_ptr_prop: "a", |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 299 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 300 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 301 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 302 | MakeBazelTarget("custom", "foo", AttrNameToString{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 303 | "string_list_prop": `[ |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 304 | "a", |
| 305 | "b", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 306 | ]`, |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 307 | "string_ptr_prop": `"a"`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 308 | }), |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 309 | }, |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 310 | }, |
Jingwen Chen | 58a12b8 | 2021-03-30 13:08:36 +0000 | [diff] [blame] | 311 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 312 | Description: "control characters", |
| 313 | Blueprint: `custom { |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 314 | name: "foo", |
Jingwen Chen | 58a12b8 | 2021-03-30 13:08:36 +0000 | [diff] [blame] | 315 | string_list_prop: ["\t", "\n"], |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 316 | string_ptr_prop: "a\t\n\r", |
Jingwen Chen | 58a12b8 | 2021-03-30 13:08:36 +0000 | [diff] [blame] | 317 | bazel_module: { bp2build_available: true }, |
| 318 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 319 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 320 | MakeBazelTarget("custom", "foo", AttrNameToString{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 321 | "string_list_prop": `[ |
Jingwen Chen | 58a12b8 | 2021-03-30 13:08:36 +0000 | [diff] [blame] | 322 | "\t", |
| 323 | "\n", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 324 | ]`, |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 325 | "string_ptr_prop": `"a\t\n\r"`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 326 | }), |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 327 | }, |
| 328 | }, |
| 329 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 330 | Description: "handles dep", |
| 331 | Blueprint: `custom { |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 332 | name: "has_dep", |
| 333 | arch_paths: [":dep"], |
| 334 | bazel_module: { bp2build_available: true }, |
| 335 | } |
| 336 | |
| 337 | custom { |
| 338 | name: "dep", |
| 339 | arch_paths: ["abc"], |
| 340 | bazel_module: { bp2build_available: true }, |
| 341 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 342 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 343 | MakeBazelTarget("custom", "dep", AttrNameToString{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 344 | "arch_paths": `["abc"]`, |
| 345 | }), |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 346 | MakeBazelTarget("custom", "has_dep", AttrNameToString{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 347 | "arch_paths": `[":dep"]`, |
| 348 | }), |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 349 | }, |
| 350 | }, |
| 351 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 352 | Description: "arch-variant srcs", |
| 353 | Blueprint: `custom { |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 354 | name: "arch_paths", |
| 355 | arch: { |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 356 | x86: { arch_paths: ["x86.txt"] }, |
| 357 | x86_64: { arch_paths: ["x86_64.txt"] }, |
| 358 | arm: { arch_paths: ["arm.txt"] }, |
| 359 | arm64: { arch_paths: ["arm64.txt"] }, |
Colin Cross | f05b0d3 | 2022-07-14 18:10:34 -0700 | [diff] [blame] | 360 | riscv64: { arch_paths: ["riscv64.txt"] }, |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 361 | }, |
| 362 | target: { |
| 363 | linux: { arch_paths: ["linux.txt"] }, |
| 364 | bionic: { arch_paths: ["bionic.txt"] }, |
| 365 | host: { arch_paths: ["host.txt"] }, |
| 366 | not_windows: { arch_paths: ["not_windows.txt"] }, |
| 367 | android: { arch_paths: ["android.txt"] }, |
| 368 | linux_musl: { arch_paths: ["linux_musl.txt"] }, |
| 369 | musl: { arch_paths: ["musl.txt"] }, |
| 370 | linux_glibc: { arch_paths: ["linux_glibc.txt"] }, |
| 371 | glibc: { arch_paths: ["glibc.txt"] }, |
| 372 | linux_bionic: { arch_paths: ["linux_bionic.txt"] }, |
| 373 | darwin: { arch_paths: ["darwin.txt"] }, |
| 374 | windows: { arch_paths: ["windows.txt"] }, |
| 375 | }, |
| 376 | multilib: { |
| 377 | lib32: { arch_paths: ["lib32.txt"] }, |
| 378 | lib64: { arch_paths: ["lib64.txt"] }, |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 379 | }, |
| 380 | bazel_module: { bp2build_available: true }, |
| 381 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 382 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 383 | MakeBazelTarget("custom", "arch_paths", AttrNameToString{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 384 | "arch_paths": `select({ |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 385 | "//build/bazel/platforms/arch:arm": [ |
| 386 | "arm.txt", |
| 387 | "lib32.txt", |
| 388 | ], |
| 389 | "//build/bazel/platforms/arch:arm64": [ |
| 390 | "arm64.txt", |
| 391 | "lib64.txt", |
| 392 | ], |
Colin Cross | f05b0d3 | 2022-07-14 18:10:34 -0700 | [diff] [blame] | 393 | "//build/bazel/platforms/arch:riscv64": [ |
| 394 | "riscv64.txt", |
| 395 | "lib64.txt", |
| 396 | ], |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 397 | "//build/bazel/platforms/arch:x86": [ |
| 398 | "x86.txt", |
| 399 | "lib32.txt", |
| 400 | ], |
| 401 | "//build/bazel/platforms/arch:x86_64": [ |
| 402 | "x86_64.txt", |
| 403 | "lib64.txt", |
| 404 | ], |
| 405 | "//conditions:default": [], |
| 406 | }) + select({ |
| 407 | "//build/bazel/platforms/os:android": [ |
| 408 | "linux.txt", |
| 409 | "bionic.txt", |
| 410 | "android.txt", |
| 411 | ], |
| 412 | "//build/bazel/platforms/os:darwin": [ |
| 413 | "host.txt", |
| 414 | "darwin.txt", |
| 415 | "not_windows.txt", |
| 416 | ], |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 417 | "//build/bazel/platforms/os:linux_bionic": [ |
| 418 | "host.txt", |
| 419 | "linux.txt", |
| 420 | "bionic.txt", |
| 421 | "linux_bionic.txt", |
| 422 | "not_windows.txt", |
| 423 | ], |
Colin Cross | 133782e | 2022-12-20 15:29:31 -0800 | [diff] [blame] | 424 | "//build/bazel/platforms/os:linux_glibc": [ |
| 425 | "host.txt", |
| 426 | "linux.txt", |
| 427 | "glibc.txt", |
| 428 | "linux_glibc.txt", |
| 429 | "not_windows.txt", |
| 430 | ], |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 431 | "//build/bazel/platforms/os:linux_musl": [ |
| 432 | "host.txt", |
| 433 | "linux.txt", |
| 434 | "musl.txt", |
| 435 | "linux_musl.txt", |
| 436 | "not_windows.txt", |
| 437 | ], |
| 438 | "//build/bazel/platforms/os:windows": [ |
| 439 | "host.txt", |
| 440 | "windows.txt", |
| 441 | ], |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 442 | "//conditions:default": [], |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 443 | })`, |
| 444 | }), |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 445 | }, |
| 446 | }, |
| 447 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 448 | Description: "arch-variant deps", |
| 449 | Blueprint: `custom { |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 450 | name: "has_dep", |
| 451 | arch: { |
| 452 | x86: { |
| 453 | arch_paths: [":dep"], |
| 454 | }, |
| 455 | }, |
| 456 | bazel_module: { bp2build_available: true }, |
| 457 | } |
| 458 | |
| 459 | custom { |
| 460 | name: "dep", |
| 461 | arch_paths: ["abc"], |
| 462 | bazel_module: { bp2build_available: true }, |
| 463 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 464 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 465 | MakeBazelTarget("custom", "dep", AttrNameToString{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 466 | "arch_paths": `["abc"]`, |
| 467 | }), |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 468 | MakeBazelTarget("custom", "has_dep", AttrNameToString{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 469 | "arch_paths": `select({ |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 470 | "//build/bazel/platforms/arch:x86": [":dep"], |
| 471 | "//conditions:default": [], |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 472 | })`, |
| 473 | }), |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 474 | }, |
Jingwen Chen | 58a12b8 | 2021-03-30 13:08:36 +0000 | [diff] [blame] | 475 | }, |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 476 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 477 | Description: "embedded props", |
| 478 | Blueprint: `custom { |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 479 | name: "embedded_props", |
| 480 | embedded_prop: "abc", |
| 481 | bazel_module: { bp2build_available: true }, |
| 482 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 483 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 484 | MakeBazelTarget("custom", "embedded_props", AttrNameToString{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 485 | "embedded_attr": `"abc"`, |
| 486 | }), |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 487 | }, |
| 488 | }, |
| 489 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 490 | Description: "ptr to embedded props", |
| 491 | Blueprint: `custom { |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 492 | name: "ptr_to_embedded_props", |
| 493 | other_embedded_prop: "abc", |
| 494 | bazel_module: { bp2build_available: true }, |
| 495 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 496 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 497 | MakeBazelTarget("custom", "ptr_to_embedded_props", AttrNameToString{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 498 | "other_embedded_attr": `"abc"`, |
| 499 | }), |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 500 | }, |
| 501 | }, |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | dir := "." |
| 505 | for _, testCase := range testCases { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 506 | t.Run(testCase.Description, func(t *testing.T) { |
| 507 | config := android.TestConfig(buildDir, nil, testCase.Blueprint, nil) |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 508 | ctx := android.NewTestContext(config) |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 509 | |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 510 | registerCustomModuleForBp2buildConversion(ctx) |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 511 | |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 512 | _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) |
| 513 | if errored(t, testCase, errs) { |
| 514 | return |
| 515 | } |
| 516 | _, errs = ctx.ResolveDependencies(config) |
| 517 | if errored(t, testCase, errs) { |
| 518 | return |
| 519 | } |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 520 | |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 521 | codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "") |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 522 | bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir) |
| 523 | android.FailIfErrored(t, err) |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 524 | |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 525 | if actualCount, expectedCount := len(bazelTargets), len(testCase.ExpectedBazelTargets); actualCount != expectedCount { |
| 526 | 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] | 527 | } else { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 528 | for i, expectedBazelTarget := range testCase.ExpectedBazelTargets { |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 529 | actualBazelTarget := bazelTargets[i] |
| 530 | if actualBazelTarget.content != expectedBazelTarget { |
| 531 | t.Errorf( |
| 532 | "Expected generated Bazel target to be '%s', got '%s'", |
| 533 | expectedBazelTarget, |
| 534 | actualBazelTarget.content, |
| 535 | ) |
| 536 | } |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 537 | } |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 538 | } |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 539 | }) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 540 | } |
| 541 | } |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 542 | |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 543 | func TestBp2buildHostAndDevice(t *testing.T) { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 544 | testCases := []Bp2buildTestCase{ |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 545 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 546 | Description: "host and device, device only", |
| 547 | ModuleTypeUnderTest: "custom", |
| 548 | ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice, |
| 549 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 550 | name: "foo", |
| 551 | bazel_module: { bp2build_available: true }, |
| 552 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 553 | ExpectedBazelTargets: []string{ |
| 554 | makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported), |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 555 | }, |
| 556 | }, |
| 557 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 558 | Description: "host and device, both", |
| 559 | ModuleTypeUnderTest: "custom", |
| 560 | ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice, |
| 561 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 562 | name: "foo", |
| 563 | host_supported: true, |
| 564 | bazel_module: { bp2build_available: true }, |
| 565 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 566 | ExpectedBazelTargets: []string{ |
| 567 | MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{}), |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 568 | }, |
| 569 | }, |
| 570 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 571 | Description: "host and device, host explicitly disabled", |
| 572 | ModuleTypeUnderTest: "custom", |
| 573 | ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice, |
| 574 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 575 | name: "foo", |
| 576 | host_supported: false, |
| 577 | bazel_module: { bp2build_available: true }, |
| 578 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 579 | ExpectedBazelTargets: []string{ |
| 580 | makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported), |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 581 | }, |
| 582 | }, |
| 583 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 584 | Description: "host and device, neither", |
| 585 | ModuleTypeUnderTest: "custom", |
| 586 | ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice, |
| 587 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 588 | name: "foo", |
| 589 | host_supported: false, |
| 590 | device_supported: false, |
| 591 | bazel_module: { bp2build_available: true }, |
| 592 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 593 | ExpectedBazelTargets: []string{ |
| 594 | MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{ |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 595 | "target_compatible_with": `["@platforms//:incompatible"]`, |
| 596 | }), |
| 597 | }, |
| 598 | }, |
| 599 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 600 | Description: "host and device, neither, cannot override with product_var", |
| 601 | ModuleTypeUnderTest: "custom", |
| 602 | ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice, |
| 603 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 604 | name: "foo", |
| 605 | host_supported: false, |
| 606 | device_supported: false, |
| 607 | product_variables: { unbundled_build: { enabled: true } }, |
| 608 | bazel_module: { bp2build_available: true }, |
| 609 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 610 | ExpectedBazelTargets: []string{ |
| 611 | MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{ |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 612 | "target_compatible_with": `["@platforms//:incompatible"]`, |
| 613 | }), |
| 614 | }, |
| 615 | }, |
| 616 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 617 | Description: "host and device, both, disabled overrided with product_var", |
| 618 | ModuleTypeUnderTest: "custom", |
| 619 | ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice, |
| 620 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 621 | name: "foo", |
| 622 | host_supported: true, |
| 623 | device_supported: true, |
| 624 | enabled: false, |
| 625 | product_variables: { unbundled_build: { enabled: true } }, |
| 626 | bazel_module: { bp2build_available: true }, |
| 627 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 628 | ExpectedBazelTargets: []string{ |
| 629 | MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{ |
Cole Faust | 87c0c33 | 2023-07-31 12:10:12 -0700 | [diff] [blame] | 630 | "target_compatible_with": `select({ |
| 631 | "//build/bazel/product_config/config_settings:unbundled_build": [], |
| 632 | "//conditions:default": ["@platforms//:incompatible"], |
| 633 | })`, |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 634 | }), |
| 635 | }, |
| 636 | }, |
| 637 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 638 | Description: "host and device, neither, cannot override with arch enabled", |
| 639 | ModuleTypeUnderTest: "custom", |
| 640 | ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice, |
| 641 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 642 | name: "foo", |
| 643 | host_supported: false, |
| 644 | device_supported: false, |
| 645 | arch: { x86: { enabled: true } }, |
| 646 | bazel_module: { bp2build_available: true }, |
| 647 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 648 | ExpectedBazelTargets: []string{ |
| 649 | MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{ |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 650 | "target_compatible_with": `["@platforms//:incompatible"]`, |
| 651 | }), |
| 652 | }, |
| 653 | }, |
| 654 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 655 | Description: "host and device, host only", |
| 656 | ModuleTypeUnderTest: "custom", |
| 657 | ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice, |
| 658 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 659 | name: "foo", |
| 660 | host_supported: true, |
| 661 | device_supported: false, |
| 662 | bazel_module: { bp2build_available: true }, |
| 663 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 664 | ExpectedBazelTargets: []string{ |
| 665 | makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.HostSupported), |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 666 | }, |
| 667 | }, |
| 668 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 669 | Description: "host only", |
| 670 | ModuleTypeUnderTest: "custom", |
| 671 | ModuleTypeUnderTestFactory: customModuleFactoryHostSupported, |
| 672 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 673 | name: "foo", |
| 674 | bazel_module: { bp2build_available: true }, |
| 675 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 676 | ExpectedBazelTargets: []string{ |
| 677 | makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.HostSupported), |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 678 | }, |
| 679 | }, |
| 680 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 681 | Description: "device only", |
| 682 | ModuleTypeUnderTest: "custom", |
| 683 | ModuleTypeUnderTestFactory: customModuleFactoryDeviceSupported, |
| 684 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 685 | name: "foo", |
| 686 | bazel_module: { bp2build_available: true }, |
| 687 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 688 | ExpectedBazelTargets: []string{ |
| 689 | makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported), |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 690 | }, |
| 691 | }, |
| 692 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 693 | Description: "host and device default, default", |
| 694 | ModuleTypeUnderTest: "custom", |
| 695 | ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault, |
| 696 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 697 | name: "foo", |
| 698 | bazel_module: { bp2build_available: true }, |
| 699 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 700 | ExpectedBazelTargets: []string{ |
| 701 | MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{}), |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 702 | }, |
| 703 | }, |
| 704 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 705 | Description: "host and device default, device only", |
| 706 | ModuleTypeUnderTest: "custom", |
| 707 | ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault, |
| 708 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 709 | name: "foo", |
| 710 | host_supported: false, |
| 711 | bazel_module: { bp2build_available: true }, |
| 712 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 713 | ExpectedBazelTargets: []string{ |
| 714 | makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported), |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 715 | }, |
| 716 | }, |
| 717 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 718 | Description: "host and device default, host only", |
| 719 | ModuleTypeUnderTest: "custom", |
| 720 | ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault, |
| 721 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 722 | name: "foo", |
| 723 | device_supported: false, |
| 724 | bazel_module: { bp2build_available: true }, |
| 725 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 726 | ExpectedBazelTargets: []string{ |
| 727 | makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.HostSupported), |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 728 | }, |
| 729 | }, |
| 730 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 731 | Description: "host and device default, neither", |
| 732 | ModuleTypeUnderTest: "custom", |
| 733 | ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault, |
| 734 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 735 | name: "foo", |
| 736 | host_supported: false, |
| 737 | device_supported: false, |
| 738 | bazel_module: { bp2build_available: true }, |
| 739 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 740 | ExpectedBazelTargets: []string{ |
| 741 | MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{ |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 742 | "target_compatible_with": `["@platforms//:incompatible"]`, |
| 743 | }), |
| 744 | }, |
| 745 | }, |
| 746 | } |
| 747 | |
| 748 | for _, tc := range testCases { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 749 | t.Run(tc.Description, func(t *testing.T) { |
Trevor Radcliffe | 1b4b2d9 | 2022-09-01 18:57:01 +0000 | [diff] [blame] | 750 | RunBp2BuildTestCaseSimple(t, tc) |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 751 | }) |
| 752 | } |
| 753 | } |
| 754 | |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 755 | func TestLoadStatements(t *testing.T) { |
| 756 | testCases := []struct { |
| 757 | bazelTargets BazelTargets |
| 758 | expectedLoadStatements string |
| 759 | }{ |
| 760 | { |
| 761 | bazelTargets: BazelTargets{ |
| 762 | BazelTarget{ |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 763 | name: "foo", |
| 764 | ruleClass: "cc_library", |
| 765 | loads: []BazelLoad{{ |
| 766 | file: "//build/bazel/rules:cc.bzl", |
| 767 | symbols: []BazelLoadSymbol{{symbol: "cc_library"}}, |
| 768 | }}, |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 769 | }, |
| 770 | }, |
| 771 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`, |
| 772 | }, |
| 773 | { |
| 774 | bazelTargets: BazelTargets{ |
| 775 | BazelTarget{ |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 776 | name: "foo", |
| 777 | ruleClass: "cc_library", |
| 778 | loads: []BazelLoad{{ |
| 779 | file: "//build/bazel/rules:cc.bzl", |
| 780 | symbols: []BazelLoadSymbol{{symbol: "cc_library"}}, |
| 781 | }}, |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 782 | }, |
| 783 | BazelTarget{ |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 784 | name: "bar", |
| 785 | ruleClass: "cc_library", |
| 786 | loads: []BazelLoad{{ |
| 787 | file: "//build/bazel/rules:cc.bzl", |
| 788 | symbols: []BazelLoadSymbol{{symbol: "cc_library"}}, |
| 789 | }}, |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 790 | }, |
| 791 | }, |
| 792 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`, |
| 793 | }, |
| 794 | { |
| 795 | bazelTargets: BazelTargets{ |
| 796 | BazelTarget{ |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 797 | name: "foo", |
| 798 | ruleClass: "cc_library", |
| 799 | loads: []BazelLoad{{ |
| 800 | file: "//build/bazel/rules:cc.bzl", |
| 801 | symbols: []BazelLoadSymbol{{symbol: "cc_library"}}, |
| 802 | }}, |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 803 | }, |
| 804 | BazelTarget{ |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 805 | name: "bar", |
| 806 | ruleClass: "cc_binary", |
| 807 | loads: []BazelLoad{{ |
| 808 | file: "//build/bazel/rules:cc.bzl", |
| 809 | symbols: []BazelLoadSymbol{{symbol: "cc_binary"}}, |
| 810 | }}, |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 811 | }, |
| 812 | }, |
| 813 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")`, |
| 814 | }, |
| 815 | { |
| 816 | bazelTargets: BazelTargets{ |
| 817 | BazelTarget{ |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 818 | name: "foo", |
| 819 | ruleClass: "cc_library", |
| 820 | loads: []BazelLoad{{ |
| 821 | file: "//build/bazel/rules:cc.bzl", |
| 822 | symbols: []BazelLoadSymbol{{symbol: "cc_library"}}, |
| 823 | }}, |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 824 | }, |
| 825 | BazelTarget{ |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 826 | name: "bar", |
| 827 | ruleClass: "cc_binary", |
| 828 | loads: []BazelLoad{{ |
| 829 | file: "//build/bazel/rules:cc.bzl", |
| 830 | symbols: []BazelLoadSymbol{{symbol: "cc_binary"}}, |
| 831 | }}, |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 832 | }, |
| 833 | BazelTarget{ |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 834 | name: "baz", |
| 835 | ruleClass: "java_binary", |
| 836 | loads: []BazelLoad{{ |
| 837 | file: "//build/bazel/rules:java.bzl", |
| 838 | symbols: []BazelLoadSymbol{{symbol: "java_binary"}}, |
| 839 | }}, |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 840 | }, |
| 841 | }, |
| 842 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library") |
| 843 | load("//build/bazel/rules:java.bzl", "java_binary")`, |
| 844 | }, |
| 845 | { |
| 846 | bazelTargets: BazelTargets{ |
| 847 | BazelTarget{ |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 848 | name: "foo", |
| 849 | ruleClass: "cc_binary", |
| 850 | loads: []BazelLoad{{ |
| 851 | file: "//build/bazel/rules:cc.bzl", |
| 852 | symbols: []BazelLoadSymbol{{symbol: "cc_binary"}}, |
| 853 | }}, |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 854 | }, |
| 855 | BazelTarget{ |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 856 | name: "bar", |
| 857 | ruleClass: "java_binary", |
| 858 | loads: []BazelLoad{{ |
| 859 | file: "//build/bazel/rules:java.bzl", |
| 860 | symbols: []BazelLoadSymbol{{symbol: "java_binary"}}, |
| 861 | }}, |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 862 | }, |
| 863 | BazelTarget{ |
| 864 | name: "baz", |
| 865 | ruleClass: "genrule", |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 866 | // Note: no loads for native rules |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 867 | }, |
| 868 | }, |
| 869 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary") |
| 870 | load("//build/bazel/rules:java.bzl", "java_binary")`, |
| 871 | }, |
| 872 | } |
| 873 | |
| 874 | for _, testCase := range testCases { |
| 875 | actual := testCase.bazelTargets.LoadStatements() |
| 876 | expected := testCase.expectedLoadStatements |
| 877 | if actual != expected { |
| 878 | t.Fatalf("Expected load statements to be %s, got %s", expected, actual) |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | } |
| 883 | |
| 884 | func TestGenerateBazelTargetModules_OneToMany_LoadedFromStarlark(t *testing.T) { |
| 885 | testCases := []struct { |
| 886 | bp string |
| 887 | expectedBazelTarget string |
| 888 | expectedBazelTargetCount int |
| 889 | expectedLoadStatements string |
| 890 | }{ |
| 891 | { |
| 892 | bp: `custom { |
| 893 | name: "bar", |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 894 | host_supported: true, |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 895 | one_to_many_prop: true, |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 896 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 897 | }`, |
| 898 | expectedBazelTarget: `my_library( |
| 899 | name = "bar", |
| 900 | ) |
| 901 | |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 902 | proto_library( |
| 903 | name = "bar_proto_library_deps", |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 904 | ) |
| 905 | |
| 906 | my_proto_library( |
| 907 | name = "bar_my_proto_library_deps", |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 908 | )`, |
| 909 | expectedBazelTargetCount: 3, |
| 910 | expectedLoadStatements: `load("//build/bazel/rules:proto.bzl", "my_proto_library", "proto_library") |
| 911 | load("//build/bazel/rules:rules.bzl", "my_library")`, |
| 912 | }, |
| 913 | } |
| 914 | |
| 915 | dir := "." |
| 916 | for _, testCase := range testCases { |
| 917 | config := android.TestConfig(buildDir, nil, testCase.bp, nil) |
| 918 | ctx := android.NewTestContext(config) |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 919 | ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice) |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 920 | ctx.RegisterForBazelConversion() |
| 921 | |
| 922 | _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) |
| 923 | android.FailIfErrored(t, errs) |
| 924 | _, errs = ctx.ResolveDependencies(config) |
| 925 | android.FailIfErrored(t, errs) |
| 926 | |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 927 | codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "") |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 928 | bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir) |
| 929 | android.FailIfErrored(t, err) |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 930 | if actualCount := len(bazelTargets); actualCount != testCase.expectedBazelTargetCount { |
| 931 | t.Fatalf("Expected %d bazel target, got %d", testCase.expectedBazelTargetCount, actualCount) |
| 932 | } |
| 933 | |
| 934 | actualBazelTargets := bazelTargets.String() |
| 935 | if actualBazelTargets != testCase.expectedBazelTarget { |
| 936 | t.Errorf( |
| 937 | "Expected generated Bazel target to be '%s', got '%s'", |
| 938 | testCase.expectedBazelTarget, |
| 939 | actualBazelTargets, |
| 940 | ) |
| 941 | } |
| 942 | |
| 943 | actualLoadStatements := bazelTargets.LoadStatements() |
| 944 | if actualLoadStatements != testCase.expectedLoadStatements { |
| 945 | t.Errorf( |
| 946 | "Expected generated load statements to be '%s', got '%s'", |
| 947 | testCase.expectedLoadStatements, |
| 948 | actualLoadStatements, |
| 949 | ) |
| 950 | } |
| 951 | } |
| 952 | } |
| 953 | |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 954 | func TestModuleTypeBp2Build(t *testing.T) { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 955 | testCases := []Bp2buildTestCase{ |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 956 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 957 | Description: "filegroup with does not specify srcs", |
| 958 | ModuleTypeUnderTest: "filegroup", |
| 959 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 960 | Blueprint: `filegroup { |
Liz Kammer | ebfcf67 | 2021-02-16 15:00:05 -0500 | [diff] [blame] | 961 | name: "fg_foo", |
| 962 | bazel_module: { bp2build_available: true }, |
| 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 | ebfcf67 | 2021-02-16 15:00:05 -0500 | [diff] [blame] | 966 | }, |
| 967 | }, |
| 968 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 969 | Description: "filegroup with no srcs", |
| 970 | ModuleTypeUnderTest: "filegroup", |
| 971 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 972 | Blueprint: `filegroup { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 973 | name: "fg_foo", |
| 974 | srcs: [], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 975 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 976 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 977 | ExpectedBazelTargets: []string{ |
| 978 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{}), |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 979 | }, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 980 | }, |
| 981 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 982 | Description: "filegroup with srcs", |
| 983 | ModuleTypeUnderTest: "filegroup", |
| 984 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 985 | Blueprint: `filegroup { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 986 | name: "fg_foo", |
| 987 | srcs: ["a", "b"], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 988 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 989 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 990 | ExpectedBazelTargets: []string{ |
| 991 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 992 | "srcs": `[ |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 993 | "a", |
| 994 | "b", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 995 | ]`, |
| 996 | }), |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 997 | }, |
| 998 | }, |
| 999 | { |
Usta Shrestha | d558031 | 2022-09-23 16:46:38 -0400 | [diff] [blame] | 1000 | Description: "filegroup with dot-slash-prefixed srcs", |
| 1001 | ModuleTypeUnderTest: "filegroup", |
| 1002 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1003 | Blueprint: `filegroup { |
| 1004 | name: "fg_foo", |
| 1005 | srcs: ["./a", "./b"], |
| 1006 | bazel_module: { bp2build_available: true }, |
| 1007 | }`, |
| 1008 | ExpectedBazelTargets: []string{ |
| 1009 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
| 1010 | "srcs": `[ |
| 1011 | "a", |
| 1012 | "b", |
| 1013 | ]`, |
| 1014 | }), |
| 1015 | }, |
| 1016 | }, |
| 1017 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1018 | Description: "filegroup with excludes srcs", |
| 1019 | ModuleTypeUnderTest: "filegroup", |
| 1020 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1021 | Blueprint: `filegroup { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 1022 | name: "fg_foo", |
| 1023 | srcs: ["a", "b"], |
| 1024 | exclude_srcs: ["a"], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1025 | bazel_module: { bp2build_available: true }, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 1026 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1027 | ExpectedBazelTargets: []string{ |
| 1028 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1029 | "srcs": `["b"]`, |
| 1030 | }), |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 1031 | }, |
| 1032 | }, |
| 1033 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1034 | Description: "depends_on_other_dir_module", |
| 1035 | ModuleTypeUnderTest: "filegroup", |
| 1036 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1037 | Blueprint: `filegroup { |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1038 | name: "fg_foo", |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 1039 | srcs: [ |
Jingwen Chen | b4628eb | 2021-04-08 14:40:57 +0000 | [diff] [blame] | 1040 | ":foo", |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 1041 | "c", |
| 1042 | ], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1043 | bazel_module: { bp2build_available: true }, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 1044 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1045 | Filesystem: map[string]string{ |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 1046 | "other/Android.bp": `filegroup { |
| 1047 | name: "foo", |
| 1048 | srcs: ["a", "b"], |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 1049 | bazel_module: { bp2build_available: true }, |
| 1050 | }`, |
| 1051 | }, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1052 | ExpectedBazelTargets: []string{ |
| 1053 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1054 | "srcs": `[ |
| 1055 | "//other:foo", |
| 1056 | "c", |
| 1057 | ]`, |
| 1058 | }), |
| 1059 | }, |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 1060 | }, |
| 1061 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1062 | Description: "depends_on_other_unconverted_module_error", |
| 1063 | ModuleTypeUnderTest: "filegroup", |
| 1064 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1065 | UnconvertedDepsMode: errorModulesUnconvertedDeps, |
| 1066 | Blueprint: `filegroup { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1067 | name: "foobar", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1068 | srcs: [ |
| 1069 | ":foo", |
| 1070 | "c", |
| 1071 | ], |
| 1072 | bazel_module: { bp2build_available: true }, |
| 1073 | }`, |
Sasha Smundak | f2bb26f | 2022-08-04 11:28:15 -0700 | [diff] [blame] | 1074 | ExpectedErr: fmt.Errorf(`filegroup .:foobar depends on unconverted modules: foo`), |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1075 | Filesystem: map[string]string{ |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1076 | "other/Android.bp": `filegroup { |
| 1077 | name: "foo", |
| 1078 | srcs: ["a", "b"], |
| 1079 | }`, |
| 1080 | }, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 1081 | }, |
Usta Shrestha | 808bc71 | 2022-09-23 23:18:18 -0400 | [diff] [blame] | 1082 | { |
| 1083 | Description: "depends_on_other_missing_module_error", |
| 1084 | ModuleTypeUnderTest: "filegroup", |
| 1085 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1086 | UnconvertedDepsMode: errorModulesUnconvertedDeps, |
| 1087 | Blueprint: `filegroup { |
| 1088 | name: "foobar", |
| 1089 | srcs: [ |
| 1090 | "c", |
| 1091 | "//other:foo", |
| 1092 | "//other:goo", |
| 1093 | ], |
| 1094 | bazel_module: { bp2build_available: true }, |
| 1095 | }`, |
| 1096 | ExpectedErr: fmt.Errorf(`filegroup .:foobar depends on missing modules: //other:goo`), |
| 1097 | Filesystem: map[string]string{"other/Android.bp": `filegroup { |
| 1098 | name: "foo", |
| 1099 | srcs: ["a"], |
| 1100 | bazel_module: { bp2build_available: true }, |
| 1101 | } |
| 1102 | `, |
| 1103 | }, |
| 1104 | }, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 1105 | } |
| 1106 | |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 1107 | for _, testCase := range testCases { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1108 | t.Run(testCase.Description, func(t *testing.T) { |
| 1109 | RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, testCase) |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 1110 | }) |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 1111 | } |
| 1112 | } |
Jingwen Chen | 041b184 | 2021-02-01 00:23:25 -0500 | [diff] [blame] | 1113 | |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1114 | func TestAllowlistingBp2buildTargetsExplicitly(t *testing.T) { |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1115 | testCases := []struct { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1116 | moduleTypeUnderTest string |
| 1117 | moduleTypeUnderTestFactory android.ModuleFactory |
| 1118 | bp string |
| 1119 | expectedCount int |
| 1120 | description string |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1121 | }{ |
| 1122 | { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1123 | description: "explicitly unavailable", |
| 1124 | moduleTypeUnderTest: "filegroup", |
| 1125 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1126 | bp: `filegroup { |
| 1127 | name: "foo", |
| 1128 | srcs: ["a", "b"], |
| 1129 | bazel_module: { bp2build_available: false }, |
| 1130 | }`, |
| 1131 | expectedCount: 0, |
| 1132 | }, |
| 1133 | { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1134 | description: "implicitly unavailable", |
| 1135 | moduleTypeUnderTest: "filegroup", |
| 1136 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1137 | bp: `filegroup { |
| 1138 | name: "foo", |
| 1139 | srcs: ["a", "b"], |
| 1140 | }`, |
| 1141 | expectedCount: 0, |
| 1142 | }, |
| 1143 | { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1144 | description: "explicitly available", |
| 1145 | moduleTypeUnderTest: "filegroup", |
| 1146 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1147 | bp: `filegroup { |
| 1148 | name: "foo", |
| 1149 | srcs: ["a", "b"], |
| 1150 | bazel_module: { bp2build_available: true }, |
| 1151 | }`, |
| 1152 | expectedCount: 1, |
| 1153 | }, |
| 1154 | { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1155 | description: "generates more than 1 target if needed", |
| 1156 | moduleTypeUnderTest: "custom", |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 1157 | moduleTypeUnderTestFactory: customModuleFactoryHostAndDevice, |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1158 | bp: `custom { |
| 1159 | name: "foo", |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1160 | one_to_many_prop: true, |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1161 | bazel_module: { bp2build_available: true }, |
| 1162 | }`, |
| 1163 | expectedCount: 3, |
| 1164 | }, |
| 1165 | } |
| 1166 | |
| 1167 | dir := "." |
| 1168 | for _, testCase := range testCases { |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 1169 | t.Run(testCase.description, func(t *testing.T) { |
| 1170 | config := android.TestConfig(buildDir, nil, testCase.bp, nil) |
| 1171 | ctx := android.NewTestContext(config) |
| 1172 | ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory) |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 1173 | ctx.RegisterForBazelConversion() |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1174 | |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 1175 | _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) |
| 1176 | android.FailIfErrored(t, errs) |
| 1177 | _, errs = ctx.ResolveDependencies(config) |
| 1178 | android.FailIfErrored(t, errs) |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1179 | |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 1180 | codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "") |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 1181 | bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir) |
| 1182 | android.FailIfErrored(t, err) |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 1183 | if actualCount := len(bazelTargets); actualCount != testCase.expectedCount { |
| 1184 | t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, testCase.expectedCount, actualCount) |
| 1185 | } |
| 1186 | }) |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1187 | } |
| 1188 | } |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1189 | |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1190 | func TestAllowlistingBp2buildTargetsWithConfig(t *testing.T) { |
| 1191 | testCases := []struct { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1192 | moduleTypeUnderTest string |
| 1193 | moduleTypeUnderTestFactory android.ModuleFactory |
| 1194 | expectedCount map[string]int |
| 1195 | description string |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 1196 | bp2buildConfig allowlists.Bp2BuildConfig |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1197 | checkDir string |
| 1198 | fs map[string]string |
MarkDacek | 9c094ca | 2023-03-16 19:15:19 +0000 | [diff] [blame] | 1199 | forceEnabledModules []string |
| 1200 | expectedErrorMessages []string |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1201 | }{ |
| 1202 | { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1203 | description: "test bp2build config package and subpackages config", |
| 1204 | moduleTypeUnderTest: "filegroup", |
| 1205 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1206 | expectedCount: map[string]int{ |
| 1207 | "migrated": 1, |
| 1208 | "migrated/but_not_really": 0, |
| 1209 | "migrated/but_not_really/but_really": 1, |
| 1210 | "not_migrated": 0, |
| 1211 | "also_not_migrated": 0, |
| 1212 | }, |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 1213 | bp2buildConfig: allowlists.Bp2BuildConfig{ |
| 1214 | "migrated": allowlists.Bp2BuildDefaultTrueRecursively, |
| 1215 | "migrated/but_not_really": allowlists.Bp2BuildDefaultFalse, |
| 1216 | "not_migrated": allowlists.Bp2BuildDefaultFalse, |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1217 | }, |
| 1218 | fs: map[string]string{ |
| 1219 | "migrated/Android.bp": `filegroup { name: "a" }`, |
| 1220 | "migrated/but_not_really/Android.bp": `filegroup { name: "b" }`, |
| 1221 | "migrated/but_not_really/but_really/Android.bp": `filegroup { name: "c" }`, |
| 1222 | "not_migrated/Android.bp": `filegroup { name: "d" }`, |
| 1223 | "also_not_migrated/Android.bp": `filegroup { name: "e" }`, |
| 1224 | }, |
| 1225 | }, |
| 1226 | { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1227 | description: "test bp2build config opt-in and opt-out", |
| 1228 | moduleTypeUnderTest: "filegroup", |
| 1229 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1230 | expectedCount: map[string]int{ |
| 1231 | "package-opt-in": 2, |
| 1232 | "package-opt-in/subpackage": 0, |
| 1233 | "package-opt-out": 1, |
| 1234 | "package-opt-out/subpackage": 0, |
| 1235 | }, |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 1236 | bp2buildConfig: allowlists.Bp2BuildConfig{ |
| 1237 | "package-opt-in": allowlists.Bp2BuildDefaultFalse, |
| 1238 | "package-opt-out": allowlists.Bp2BuildDefaultTrueRecursively, |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1239 | }, |
| 1240 | fs: map[string]string{ |
| 1241 | "package-opt-in/Android.bp": ` |
| 1242 | filegroup { name: "opt-in-a" } |
| 1243 | filegroup { name: "opt-in-b", bazel_module: { bp2build_available: true } } |
| 1244 | filegroup { name: "opt-in-c", bazel_module: { bp2build_available: true } } |
| 1245 | `, |
| 1246 | |
| 1247 | "package-opt-in/subpackage/Android.bp": ` |
| 1248 | filegroup { name: "opt-in-d" } // parent package not configured to DefaultTrueRecursively |
| 1249 | `, |
| 1250 | |
| 1251 | "package-opt-out/Android.bp": ` |
| 1252 | filegroup { name: "opt-out-a" } |
| 1253 | filegroup { name: "opt-out-b", bazel_module: { bp2build_available: false } } |
| 1254 | filegroup { name: "opt-out-c", bazel_module: { bp2build_available: false } } |
| 1255 | `, |
| 1256 | |
| 1257 | "package-opt-out/subpackage/Android.bp": ` |
| 1258 | filegroup { name: "opt-out-g", bazel_module: { bp2build_available: false } } |
| 1259 | filegroup { name: "opt-out-h", bazel_module: { bp2build_available: false } } |
| 1260 | `, |
| 1261 | }, |
| 1262 | }, |
MarkDacek | 9c094ca | 2023-03-16 19:15:19 +0000 | [diff] [blame] | 1263 | { |
| 1264 | description: "test force-enabled errors out", |
| 1265 | moduleTypeUnderTest: "filegroup", |
| 1266 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 1267 | expectedCount: map[string]int{ |
| 1268 | "migrated": 0, |
| 1269 | "not_migrated": 0, |
| 1270 | }, |
| 1271 | bp2buildConfig: allowlists.Bp2BuildConfig{ |
| 1272 | "migrated/but_not_really": allowlists.Bp2BuildDefaultFalse, |
| 1273 | "not_migrated": allowlists.Bp2BuildDefaultFalse, |
| 1274 | }, |
| 1275 | fs: map[string]string{ |
| 1276 | "migrated/Android.bp": `filegroup { name: "a" }`, |
| 1277 | }, |
| 1278 | forceEnabledModules: []string{"a"}, |
| 1279 | expectedErrorMessages: []string{"Force Enabled Module a not converted"}, |
| 1280 | }, |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1281 | } |
| 1282 | |
| 1283 | dir := "." |
| 1284 | for _, testCase := range testCases { |
| 1285 | fs := make(map[string][]byte) |
| 1286 | toParse := []string{ |
| 1287 | "Android.bp", |
| 1288 | } |
| 1289 | for f, content := range testCase.fs { |
| 1290 | if strings.HasSuffix(f, "Android.bp") { |
| 1291 | toParse = append(toParse, f) |
| 1292 | } |
| 1293 | fs[f] = []byte(content) |
| 1294 | } |
| 1295 | config := android.TestConfig(buildDir, nil, "", fs) |
MarkDacek | 9c094ca | 2023-03-16 19:15:19 +0000 | [diff] [blame] | 1296 | config.AddForceEnabledModules(testCase.forceEnabledModules) |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1297 | ctx := android.NewTestContext(config) |
| 1298 | ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory) |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 1299 | allowlist := android.NewBp2BuildAllowlist().SetDefaultConfig(testCase.bp2buildConfig) |
| 1300 | ctx.RegisterBp2BuildConfig(allowlist) |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1301 | ctx.RegisterForBazelConversion() |
| 1302 | |
| 1303 | _, errs := ctx.ParseFileList(dir, toParse) |
| 1304 | android.FailIfErrored(t, errs) |
| 1305 | _, errs = ctx.ResolveDependencies(config) |
| 1306 | android.FailIfErrored(t, errs) |
| 1307 | |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 1308 | codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "") |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1309 | |
| 1310 | // For each directory, test that the expected number of generated targets is correct. |
| 1311 | for dir, expectedCount := range testCase.expectedCount { |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 1312 | bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir) |
MarkDacek | 9c094ca | 2023-03-16 19:15:19 +0000 | [diff] [blame] | 1313 | android.CheckErrorsAgainstExpectations(t, err, testCase.expectedErrorMessages) |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1314 | if actualCount := len(bazelTargets); actualCount != expectedCount { |
| 1315 | t.Fatalf( |
| 1316 | "%s: Expected %d bazel target for %s package, got %d", |
| 1317 | testCase.description, |
| 1318 | expectedCount, |
| 1319 | dir, |
| 1320 | actualCount) |
| 1321 | } |
| 1322 | |
| 1323 | } |
| 1324 | } |
| 1325 | } |
| 1326 | |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1327 | func TestCombineBuildFilesBp2buildTargets(t *testing.T) { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1328 | testCases := []Bp2buildTestCase{ |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1329 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1330 | Description: "filegroup bazel_module.label", |
| 1331 | ModuleTypeUnderTest: "filegroup", |
| 1332 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1333 | Blueprint: `filegroup { |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1334 | name: "fg_foo", |
| 1335 | bazel_module: { label: "//other:fg_foo" }, |
| 1336 | }`, |
Cole Faust | ea602c5 | 2022-08-31 14:48:26 -0700 | [diff] [blame] | 1337 | ExpectedBazelTargets: []string{}, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1338 | Filesystem: map[string]string{ |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1339 | "other/BUILD.bazel": `// BUILD file`, |
| 1340 | }, |
| 1341 | }, |
| 1342 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1343 | Description: "multiple bazel_module.label same BUILD", |
| 1344 | ModuleTypeUnderTest: "filegroup", |
| 1345 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1346 | Blueprint: `filegroup { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1347 | name: "fg_foo", |
| 1348 | bazel_module: { label: "//other:fg_foo" }, |
| 1349 | } |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1350 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1351 | filegroup { |
| 1352 | name: "foo", |
| 1353 | bazel_module: { label: "//other:foo" }, |
| 1354 | }`, |
Cole Faust | ea602c5 | 2022-08-31 14:48:26 -0700 | [diff] [blame] | 1355 | ExpectedBazelTargets: []string{}, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1356 | Filesystem: map[string]string{ |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1357 | "other/BUILD.bazel": `// BUILD file`, |
| 1358 | }, |
| 1359 | }, |
| 1360 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1361 | Description: "filegroup bazel_module.label and bp2build in subdir", |
| 1362 | ModuleTypeUnderTest: "filegroup", |
| 1363 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1364 | Dir: "other", |
| 1365 | Blueprint: ``, |
| 1366 | Filesystem: map[string]string{ |
Jingwen Chen | c63677b | 2021-06-17 05:43:19 +0000 | [diff] [blame] | 1367 | "other/Android.bp": `filegroup { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1368 | name: "fg_foo", |
| 1369 | bazel_module: { |
| 1370 | bp2build_available: true, |
| 1371 | }, |
| 1372 | } |
| 1373 | filegroup { |
| 1374 | name: "fg_bar", |
| 1375 | bazel_module: { |
| 1376 | label: "//other:fg_bar" |
| 1377 | }, |
| 1378 | }`, |
Jingwen Chen | c63677b | 2021-06-17 05:43:19 +0000 | [diff] [blame] | 1379 | "other/BUILD.bazel": `// definition for fg_bar`, |
| 1380 | }, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1381 | ExpectedBazelTargets: []string{ |
| 1382 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{}), |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1383 | }, |
| 1384 | }, |
| 1385 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1386 | Description: "filegroup bazel_module.label and filegroup bp2build", |
| 1387 | ModuleTypeUnderTest: "filegroup", |
| 1388 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1389 | |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1390 | Filesystem: map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1391 | "other/BUILD.bazel": `// BUILD file`, |
| 1392 | }, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1393 | Blueprint: `filegroup { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1394 | name: "fg_foo", |
| 1395 | bazel_module: { |
| 1396 | label: "//other:fg_foo", |
| 1397 | }, |
| 1398 | } |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1399 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1400 | filegroup { |
| 1401 | name: "fg_bar", |
| 1402 | bazel_module: { |
| 1403 | bp2build_available: true, |
| 1404 | }, |
| 1405 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1406 | ExpectedBazelTargets: []string{ |
| 1407 | MakeBazelTargetNoRestrictions("filegroup", "fg_bar", map[string]string{}), |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1408 | }, |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1409 | }, |
| 1410 | } |
| 1411 | |
| 1412 | dir := "." |
| 1413 | for _, testCase := range testCases { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1414 | t.Run(testCase.Description, func(t *testing.T) { |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1415 | fs := make(map[string][]byte) |
| 1416 | toParse := []string{ |
| 1417 | "Android.bp", |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1418 | } |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1419 | for f, content := range testCase.Filesystem { |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1420 | if strings.HasSuffix(f, "Android.bp") { |
| 1421 | toParse = append(toParse, f) |
| 1422 | } |
| 1423 | fs[f] = []byte(content) |
| 1424 | } |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1425 | config := android.TestConfig(buildDir, nil, testCase.Blueprint, fs) |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1426 | ctx := android.NewTestContext(config) |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1427 | ctx.RegisterModuleType(testCase.ModuleTypeUnderTest, testCase.ModuleTypeUnderTestFactory) |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1428 | ctx.RegisterForBazelConversion() |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1429 | |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1430 | _, errs := ctx.ParseFileList(dir, toParse) |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 1431 | if errored(t, testCase, errs) { |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1432 | return |
| 1433 | } |
| 1434 | _, errs = ctx.ResolveDependencies(config) |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 1435 | if errored(t, testCase, errs) { |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1436 | return |
| 1437 | } |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1438 | |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1439 | checkDir := dir |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1440 | if testCase.Dir != "" { |
| 1441 | checkDir = testCase.Dir |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1442 | } |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 1443 | codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "") |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 1444 | bazelTargets, err := generateBazelTargetsForDir(codegenCtx, checkDir) |
| 1445 | android.FailIfErrored(t, err) |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1446 | bazelTargets.sort() |
| 1447 | actualCount := len(bazelTargets) |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1448 | expectedCount := len(testCase.ExpectedBazelTargets) |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1449 | if actualCount != expectedCount { |
| 1450 | t.Errorf("Expected %d bazel target, got %d\n%s", expectedCount, actualCount, bazelTargets) |
| 1451 | } |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1452 | for i, target := range bazelTargets { |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1453 | actualContent := target.content |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1454 | expectedContent := testCase.ExpectedBazelTargets[i] |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1455 | if expectedContent != actualContent { |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1456 | t.Errorf( |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1457 | "Expected generated Bazel target to be '%s', got '%s'", |
| 1458 | expectedContent, |
| 1459 | actualContent, |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1460 | ) |
| 1461 | } |
| 1462 | } |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1463 | }) |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1464 | } |
| 1465 | } |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1466 | |
Jingwen Chen | 0eeaeb8 | 2022-09-21 10:27:42 +0000 | [diff] [blame] | 1467 | func TestGlob(t *testing.T) { |
| 1468 | testCases := []Bp2buildTestCase{ |
| 1469 | { |
| 1470 | Description: "filegroup with glob", |
| 1471 | ModuleTypeUnderTest: "filegroup", |
| 1472 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1473 | Blueprint: `filegroup { |
| 1474 | name: "fg_foo", |
| 1475 | srcs: ["**/*.txt"], |
| 1476 | bazel_module: { bp2build_available: true }, |
| 1477 | }`, |
| 1478 | ExpectedBazelTargets: []string{ |
| 1479 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
| 1480 | "srcs": `[ |
| 1481 | "other/a.txt", |
| 1482 | "other/b.txt", |
| 1483 | "other/subdir/a.txt", |
| 1484 | ]`, |
| 1485 | }), |
| 1486 | }, |
| 1487 | Filesystem: map[string]string{ |
| 1488 | "other/a.txt": "", |
| 1489 | "other/b.txt": "", |
| 1490 | "other/subdir/a.txt": "", |
| 1491 | "other/file": "", |
| 1492 | }, |
| 1493 | }, |
| 1494 | { |
| 1495 | Description: "filegroup with glob in subdir", |
| 1496 | ModuleTypeUnderTest: "filegroup", |
| 1497 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1498 | Dir: "other", |
| 1499 | Filesystem: map[string]string{ |
| 1500 | "other/Android.bp": `filegroup { |
| 1501 | name: "fg_foo", |
| 1502 | srcs: ["**/*.txt"], |
| 1503 | bazel_module: { bp2build_available: true }, |
| 1504 | }`, |
| 1505 | "other/a.txt": "", |
| 1506 | "other/b.txt": "", |
| 1507 | "other/subdir/a.txt": "", |
| 1508 | "other/file": "", |
| 1509 | }, |
| 1510 | ExpectedBazelTargets: []string{ |
| 1511 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
| 1512 | "srcs": `[ |
| 1513 | "a.txt", |
| 1514 | "b.txt", |
| 1515 | "subdir/a.txt", |
| 1516 | ]`, |
| 1517 | }), |
| 1518 | }, |
| 1519 | }, |
| 1520 | { |
| 1521 | Description: "filegroup with glob with no kept BUILD files", |
| 1522 | ModuleTypeUnderTest: "filegroup", |
| 1523 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1524 | KeepBuildFileForDirs: []string{ |
| 1525 | // empty |
| 1526 | }, |
| 1527 | Blueprint: `filegroup { |
| 1528 | name: "fg_foo", |
| 1529 | srcs: ["**/*.txt"], |
| 1530 | bazel_module: { bp2build_available: true }, |
| 1531 | }`, |
| 1532 | Filesystem: map[string]string{ |
| 1533 | "a.txt": "", |
| 1534 | "b.txt": "", |
| 1535 | "foo/BUILD": "", |
| 1536 | "foo/a.txt": "", |
| 1537 | "foo/bar/BUILD": "", |
| 1538 | "foo/bar/b.txt": "", |
| 1539 | }, |
| 1540 | ExpectedBazelTargets: []string{ |
| 1541 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
| 1542 | "srcs": `[ |
| 1543 | "a.txt", |
| 1544 | "b.txt", |
| 1545 | "foo/a.txt", |
| 1546 | "foo/bar/b.txt", |
| 1547 | ]`, |
| 1548 | }), |
| 1549 | }, |
| 1550 | }, |
| 1551 | { |
| 1552 | Description: "filegroup with glob with kept BUILD file", |
| 1553 | ModuleTypeUnderTest: "filegroup", |
| 1554 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1555 | KeepBuildFileForDirs: []string{ |
| 1556 | "foo", |
| 1557 | }, |
| 1558 | Blueprint: `filegroup { |
| 1559 | name: "fg_foo", |
| 1560 | srcs: ["**/*.txt"], |
| 1561 | bazel_module: { bp2build_available: true }, |
| 1562 | }`, |
| 1563 | Filesystem: map[string]string{ |
| 1564 | "a.txt": "", |
| 1565 | "b.txt": "", |
| 1566 | "foo/BUILD": "", |
| 1567 | "foo/a.txt": "", |
| 1568 | "foo/bar/BUILD": "", |
| 1569 | "foo/bar/b.txt": "", |
| 1570 | }, |
| 1571 | ExpectedBazelTargets: []string{ |
| 1572 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
| 1573 | "srcs": `[ |
| 1574 | "a.txt", |
| 1575 | "b.txt", |
| 1576 | "//foo:a.txt", |
| 1577 | "//foo:bar/b.txt", |
| 1578 | ]`, |
| 1579 | }), |
| 1580 | }, |
| 1581 | }, |
| 1582 | { |
| 1583 | Description: "filegroup with glob with kept BUILD.bazel file", |
| 1584 | ModuleTypeUnderTest: "filegroup", |
| 1585 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1586 | KeepBuildFileForDirs: []string{ |
| 1587 | "foo", |
| 1588 | }, |
| 1589 | Blueprint: `filegroup { |
| 1590 | name: "fg_foo", |
| 1591 | srcs: ["**/*.txt"], |
| 1592 | bazel_module: { bp2build_available: true }, |
| 1593 | }`, |
| 1594 | Filesystem: map[string]string{ |
| 1595 | "a.txt": "", |
| 1596 | "b.txt": "", |
| 1597 | "foo/BUILD.bazel": "", |
| 1598 | "foo/a.txt": "", |
| 1599 | "foo/bar/BUILD.bazel": "", |
| 1600 | "foo/bar/b.txt": "", |
| 1601 | }, |
| 1602 | ExpectedBazelTargets: []string{ |
| 1603 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
| 1604 | "srcs": `[ |
| 1605 | "a.txt", |
| 1606 | "b.txt", |
| 1607 | "//foo:a.txt", |
| 1608 | "//foo:bar/b.txt", |
| 1609 | ]`, |
| 1610 | }), |
| 1611 | }, |
| 1612 | }, |
| 1613 | { |
| 1614 | Description: "filegroup with glob with Android.bp file as boundary", |
| 1615 | ModuleTypeUnderTest: "filegroup", |
| 1616 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1617 | Blueprint: `filegroup { |
| 1618 | name: "fg_foo", |
| 1619 | srcs: ["**/*.txt"], |
| 1620 | bazel_module: { bp2build_available: true }, |
| 1621 | }`, |
| 1622 | Filesystem: map[string]string{ |
| 1623 | "a.txt": "", |
| 1624 | "b.txt": "", |
| 1625 | "foo/Android.bp": "", |
| 1626 | "foo/a.txt": "", |
| 1627 | "foo/bar/Android.bp": "", |
| 1628 | "foo/bar/b.txt": "", |
| 1629 | }, |
| 1630 | ExpectedBazelTargets: []string{ |
| 1631 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
| 1632 | "srcs": `[ |
| 1633 | "a.txt", |
| 1634 | "b.txt", |
| 1635 | "//foo:a.txt", |
| 1636 | "//foo/bar:b.txt", |
| 1637 | ]`, |
| 1638 | }), |
| 1639 | }, |
| 1640 | }, |
| 1641 | { |
| 1642 | Description: "filegroup with glob in subdir with kept BUILD and BUILD.bazel file", |
| 1643 | ModuleTypeUnderTest: "filegroup", |
| 1644 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1645 | Dir: "other", |
| 1646 | KeepBuildFileForDirs: []string{ |
| 1647 | "other/foo", |
| 1648 | "other/foo/bar", |
| 1649 | // deliberately not other/foo/baz/BUILD. |
| 1650 | }, |
| 1651 | Filesystem: map[string]string{ |
| 1652 | "other/Android.bp": `filegroup { |
| 1653 | name: "fg_foo", |
| 1654 | srcs: ["**/*.txt"], |
| 1655 | bazel_module: { bp2build_available: true }, |
| 1656 | }`, |
| 1657 | "other/a.txt": "", |
| 1658 | "other/b.txt": "", |
| 1659 | "other/foo/BUILD": "", |
| 1660 | "other/foo/a.txt": "", |
| 1661 | "other/foo/bar/BUILD.bazel": "", |
| 1662 | "other/foo/bar/b.txt": "", |
| 1663 | "other/foo/baz/BUILD": "", |
| 1664 | "other/foo/baz/c.txt": "", |
| 1665 | }, |
| 1666 | ExpectedBazelTargets: []string{ |
| 1667 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
| 1668 | "srcs": `[ |
| 1669 | "a.txt", |
| 1670 | "b.txt", |
| 1671 | "//other/foo:a.txt", |
| 1672 | "//other/foo/bar:b.txt", |
| 1673 | "//other/foo:baz/c.txt", |
| 1674 | ]`, |
| 1675 | }), |
| 1676 | }, |
| 1677 | }, |
| 1678 | } |
| 1679 | |
| 1680 | for _, testCase := range testCases { |
| 1681 | t.Run(testCase.Description, func(t *testing.T) { |
| 1682 | RunBp2BuildTestCaseSimple(t, testCase) |
| 1683 | }) |
| 1684 | } |
| 1685 | } |
| 1686 | |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1687 | func TestGlobExcludeSrcs(t *testing.T) { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1688 | testCases := []Bp2buildTestCase{ |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1689 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1690 | Description: "filegroup top level exclude_srcs", |
| 1691 | ModuleTypeUnderTest: "filegroup", |
| 1692 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1693 | Blueprint: `filegroup { |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1694 | name: "fg_foo", |
| 1695 | srcs: ["**/*.txt"], |
| 1696 | exclude_srcs: ["c.txt"], |
| 1697 | bazel_module: { bp2build_available: true }, |
| 1698 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1699 | Filesystem: map[string]string{ |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1700 | "a.txt": "", |
| 1701 | "b.txt": "", |
| 1702 | "c.txt": "", |
| 1703 | "dir/Android.bp": "", |
| 1704 | "dir/e.txt": "", |
| 1705 | "dir/f.txt": "", |
| 1706 | }, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1707 | ExpectedBazelTargets: []string{ |
| 1708 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1709 | "srcs": `[ |
| 1710 | "a.txt", |
| 1711 | "b.txt", |
| 1712 | "//dir:e.txt", |
| 1713 | "//dir:f.txt", |
| 1714 | ]`, |
| 1715 | }), |
| 1716 | }, |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1717 | }, |
| 1718 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1719 | Description: "filegroup in subdir exclude_srcs", |
| 1720 | ModuleTypeUnderTest: "filegroup", |
| 1721 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1722 | Blueprint: "", |
| 1723 | Dir: "dir", |
| 1724 | Filesystem: map[string]string{ |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1725 | "dir/Android.bp": `filegroup { |
| 1726 | name: "fg_foo", |
| 1727 | srcs: ["**/*.txt"], |
| 1728 | exclude_srcs: ["b.txt"], |
| 1729 | bazel_module: { bp2build_available: true }, |
| 1730 | } |
| 1731 | `, |
| 1732 | "dir/a.txt": "", |
| 1733 | "dir/b.txt": "", |
| 1734 | "dir/subdir/Android.bp": "", |
| 1735 | "dir/subdir/e.txt": "", |
| 1736 | "dir/subdir/f.txt": "", |
| 1737 | }, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1738 | ExpectedBazelTargets: []string{ |
| 1739 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1740 | "srcs": `[ |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 1741 | "a.txt", |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1742 | "//dir/subdir:e.txt", |
| 1743 | "//dir/subdir:f.txt", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1744 | ]`, |
| 1745 | }), |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1746 | }, |
| 1747 | }, |
| 1748 | } |
| 1749 | |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1750 | for _, testCase := range testCases { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1751 | t.Run(testCase.Description, func(t *testing.T) { |
Trevor Radcliffe | 1b4b2d9 | 2022-09-01 18:57:01 +0000 | [diff] [blame] | 1752 | RunBp2BuildTestCaseSimple(t, testCase) |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1753 | }) |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1754 | } |
| 1755 | } |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1756 | |
| 1757 | func TestCommonBp2BuildModuleAttrs(t *testing.T) { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1758 | testCases := []Bp2buildTestCase{ |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1759 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1760 | Description: "Required into data test", |
| 1761 | ModuleTypeUnderTest: "filegroup", |
| 1762 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
Chris Parsons | cd20903 | 2023-09-19 01:12:48 +0000 | [diff] [blame] | 1763 | StubbedBuildDefinitions: []string{"reqd"}, |
| 1764 | Blueprint: simpleModule("filegroup", "reqd") + ` |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1765 | filegroup { |
| 1766 | name: "fg_foo", |
| 1767 | required: ["reqd"], |
| 1768 | bazel_module: { bp2build_available: true }, |
| 1769 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1770 | ExpectedBazelTargets: []string{ |
| 1771 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1772 | "data": `[":reqd"]`, |
| 1773 | }), |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1774 | }, |
| 1775 | }, |
| 1776 | { |
Jingwen Chen | a5ecb37 | 2022-09-21 09:05:37 +0000 | [diff] [blame] | 1777 | Description: "Required into data test, cyclic self reference is filtered out", |
| 1778 | ModuleTypeUnderTest: "filegroup", |
| 1779 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
Chris Parsons | cd20903 | 2023-09-19 01:12:48 +0000 | [diff] [blame] | 1780 | StubbedBuildDefinitions: []string{"reqd"}, |
| 1781 | Blueprint: simpleModule("filegroup", "reqd") + ` |
Jingwen Chen | a5ecb37 | 2022-09-21 09:05:37 +0000 | [diff] [blame] | 1782 | filegroup { |
| 1783 | name: "fg_foo", |
| 1784 | required: ["reqd", "fg_foo"], |
| 1785 | bazel_module: { bp2build_available: true }, |
| 1786 | }`, |
| 1787 | ExpectedBazelTargets: []string{ |
| 1788 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
| 1789 | "data": `[":reqd"]`, |
| 1790 | }), |
| 1791 | }, |
| 1792 | }, |
| 1793 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1794 | Description: "Required via arch into data test", |
| 1795 | ModuleTypeUnderTest: "python_library", |
| 1796 | ModuleTypeUnderTestFactory: python.PythonLibraryFactory, |
Chris Parsons | cd20903 | 2023-09-19 01:12:48 +0000 | [diff] [blame] | 1797 | StubbedBuildDefinitions: []string{"reqdx86", "reqdarm"}, |
| 1798 | Blueprint: simpleModule("python_library", "reqdx86") + |
| 1799 | simpleModule("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] | 1800 | python_library { |
| 1801 | name: "fg_foo", |
| 1802 | arch: { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1803 | arm: { |
| 1804 | required: ["reqdarm"], |
| 1805 | }, |
| 1806 | x86: { |
| 1807 | required: ["reqdx86"], |
| 1808 | }, |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1809 | }, |
| 1810 | bazel_module: { bp2build_available: true }, |
| 1811 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1812 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 1813 | MakeBazelTarget("py_library", "fg_foo", map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1814 | "data": `select({ |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1815 | "//build/bazel/platforms/arch:arm": [":reqdarm"], |
| 1816 | "//build/bazel/platforms/arch:x86": [":reqdx86"], |
| 1817 | "//conditions:default": [], |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1818 | })`, |
| 1819 | "srcs_version": `"PY3"`, |
Cole Faust | b09da7e | 2022-05-18 10:57:33 -0700 | [diff] [blame] | 1820 | "imports": `["."]`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1821 | }), |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1822 | }, |
| 1823 | }, |
| 1824 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1825 | Description: "Required appended to data test", |
| 1826 | ModuleTypeUnderTest: "python_library", |
| 1827 | ModuleTypeUnderTestFactory: python.PythonLibraryFactory, |
| 1828 | Filesystem: map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1829 | "data.bin": "", |
| 1830 | "src.py": "", |
| 1831 | }, |
Chris Parsons | cd20903 | 2023-09-19 01:12:48 +0000 | [diff] [blame] | 1832 | StubbedBuildDefinitions: []string{"reqd"}, |
| 1833 | Blueprint: simpleModule("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] | 1834 | python_library { |
| 1835 | name: "fg_foo", |
| 1836 | data: ["data.bin"], |
| 1837 | required: ["reqd"], |
| 1838 | bazel_module: { bp2build_available: true }, |
| 1839 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1840 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 1841 | MakeBazelTarget("py_library", "fg_foo", map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1842 | "data": `[ |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1843 | "data.bin", |
| 1844 | ":reqd", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1845 | ]`, |
| 1846 | "srcs_version": `"PY3"`, |
Cole Faust | b09da7e | 2022-05-18 10:57:33 -0700 | [diff] [blame] | 1847 | "imports": `["."]`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1848 | }), |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1849 | }, |
| 1850 | }, |
| 1851 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1852 | Description: "All props-to-attrs at once together test", |
| 1853 | ModuleTypeUnderTest: "filegroup", |
| 1854 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
Chris Parsons | cd20903 | 2023-09-19 01:12:48 +0000 | [diff] [blame] | 1855 | StubbedBuildDefinitions: []string{"reqd"}, |
| 1856 | Blueprint: simpleModule("filegroup", "reqd") + ` |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1857 | filegroup { |
| 1858 | name: "fg_foo", |
| 1859 | required: ["reqd"], |
| 1860 | bazel_module: { bp2build_available: true }, |
| 1861 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1862 | ExpectedBazelTargets: []string{ |
| 1863 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1864 | "data": `[":reqd"]`, |
| 1865 | }), |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1866 | }, |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1867 | }, |
| 1868 | } |
| 1869 | |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1870 | for _, tc := range testCases { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1871 | t.Run(tc.Description, func(t *testing.T) { |
Trevor Radcliffe | 1b4b2d9 | 2022-09-01 18:57:01 +0000 | [diff] [blame] | 1872 | RunBp2BuildTestCaseSimple(t, tc) |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1873 | }) |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1874 | } |
| 1875 | } |
Sasha Smundak | 05b0ba6 | 2022-09-26 18:15:45 -0700 | [diff] [blame] | 1876 | |
| 1877 | func TestLicensesAttrConversion(t *testing.T) { |
| 1878 | RunBp2BuildTestCase(t, |
| 1879 | func(ctx android.RegistrationContext) { |
| 1880 | ctx.RegisterModuleType("license", android.LicenseFactory) |
| 1881 | }, |
| 1882 | Bp2buildTestCase{ |
| 1883 | Description: "Test that licenses: attribute is converted", |
| 1884 | ModuleTypeUnderTest: "filegroup", |
| 1885 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1886 | Blueprint: ` |
| 1887 | license { |
| 1888 | name: "my_license", |
| 1889 | } |
| 1890 | filegroup { |
| 1891 | name: "my_filegroup", |
| 1892 | licenses: ["my_license"], |
| 1893 | } |
| 1894 | `, |
| 1895 | ExpectedBazelTargets: []string{ |
| 1896 | MakeBazelTargetNoRestrictions("filegroup", "my_filegroup", AttrNameToString{ |
| 1897 | "applicable_licenses": `[":my_license"]`, |
| 1898 | }), |
| 1899 | MakeBazelTargetNoRestrictions("android_license", "my_license", AttrNameToString{}), |
| 1900 | }, |
| 1901 | }) |
| 1902 | } |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 1903 | |
Spandan Das | 6a448ec | 2023-04-19 17:36:12 +0000 | [diff] [blame] | 1904 | func TestGenerateConfigSetting(t *testing.T) { |
| 1905 | bp := ` |
| 1906 | custom { |
| 1907 | name: "foo", |
| 1908 | test_config_setting: true, |
| 1909 | } |
| 1910 | ` |
| 1911 | expectedBazelTargets := []string{ |
| 1912 | MakeBazelTargetNoRestrictions( |
| 1913 | "config_setting", |
| 1914 | "foo_config_setting", |
| 1915 | AttrNameToString{ |
| 1916 | "flag_values": `{ |
| 1917 | "//build/bazel/rules/my_string_setting": "foo", |
| 1918 | }`, |
| 1919 | }, |
| 1920 | ), |
| 1921 | MakeBazelTarget( |
| 1922 | "custom", |
| 1923 | "foo", |
| 1924 | AttrNameToString{}, |
| 1925 | ), |
| 1926 | } |
| 1927 | registerCustomModule := func(ctx android.RegistrationContext) { |
| 1928 | ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice) |
| 1929 | } |
| 1930 | RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{ |
| 1931 | Blueprint: bp, |
| 1932 | ExpectedBazelTargets: expectedBazelTargets, |
| 1933 | Description: "Generating API contribution Bazel targets for custom module", |
| 1934 | }) |
| 1935 | } |
Spandan Das | 921af32 | 2023-04-26 02:56:37 +0000 | [diff] [blame] | 1936 | |
| 1937 | // If values of all keys in an axis are equal to //conditions:default, drop the axis and print the common value |
| 1938 | func TestPrettyPrintSelectMapEqualValues(t *testing.T) { |
| 1939 | lla := bazel.LabelListAttribute{ |
| 1940 | Value: bazel.LabelList{}, |
| 1941 | } |
| 1942 | libFooImplLabel := bazel.Label{ |
| 1943 | Label: ":libfoo.impl", |
| 1944 | } |
Spandan Das | 6d4d9da | 2023-04-18 06:20:40 +0000 | [diff] [blame] | 1945 | lla.SetSelectValue(bazel.OsAndInApexAxis, bazel.AndroidPlatform, bazel.MakeLabelList([]bazel.Label{libFooImplLabel})) |
Spandan Das | 921af32 | 2023-04-26 02:56:37 +0000 | [diff] [blame] | 1946 | lla.SetSelectValue(bazel.OsAndInApexAxis, bazel.ConditionsDefaultConfigKey, bazel.MakeLabelList([]bazel.Label{libFooImplLabel})) |
| 1947 | actual, _ := prettyPrintAttribute(lla, 0) |
| 1948 | android.AssertStringEquals(t, "Print the common value if all keys in an axis have the same value", `[":libfoo.impl"]`, actual) |
| 1949 | } |
Spandan Das | 3131d67 | 2023-08-03 22:33:47 +0000 | [diff] [blame] | 1950 | |
Chris Parsons | 5011e61 | 2023-09-13 23:33:20 +0000 | [diff] [blame] | 1951 | func TestAlreadyPresentBuildTarget(t *testing.T) { |
| 1952 | bp := ` |
| 1953 | custom { |
| 1954 | name: "foo", |
| 1955 | } |
| 1956 | custom { |
| 1957 | name: "bar", |
| 1958 | } |
| 1959 | ` |
| 1960 | alreadyPresentBuildFile := |
| 1961 | MakeBazelTarget( |
| 1962 | "custom", |
| 1963 | "foo", |
| 1964 | AttrNameToString{}, |
| 1965 | ) |
| 1966 | expectedBazelTargets := []string{ |
| 1967 | MakeBazelTarget( |
| 1968 | "custom", |
| 1969 | "bar", |
| 1970 | AttrNameToString{}, |
| 1971 | ), |
| 1972 | } |
| 1973 | registerCustomModule := func(ctx android.RegistrationContext) { |
| 1974 | ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice) |
| 1975 | } |
| 1976 | RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{ |
| 1977 | AlreadyExistingBuildContents: alreadyPresentBuildFile, |
| 1978 | Blueprint: bp, |
| 1979 | ExpectedBazelTargets: expectedBazelTargets, |
| 1980 | Description: "Not duplicating work for an already-present BUILD target.", |
| 1981 | }) |
| 1982 | } |
| 1983 | |
Chris Parsons | 0c4de1f | 2023-09-21 20:36:35 +0000 | [diff] [blame] | 1984 | func TestAlreadyPresentOneToManyBuildTarget(t *testing.T) { |
| 1985 | bp := ` |
| 1986 | custom { |
| 1987 | name: "foo", |
| 1988 | one_to_many_prop: true, |
| 1989 | } |
| 1990 | custom { |
| 1991 | name: "bar", |
| 1992 | } |
| 1993 | ` |
| 1994 | alreadyPresentBuildFile := |
| 1995 | MakeBazelTarget( |
| 1996 | "custom", |
| 1997 | // one_to_many_prop ensures that foo generates "foo_proto_library_deps". |
| 1998 | "foo_proto_library_deps", |
| 1999 | AttrNameToString{}, |
| 2000 | ) |
| 2001 | expectedBazelTargets := []string{ |
| 2002 | MakeBazelTarget( |
| 2003 | "custom", |
| 2004 | "bar", |
| 2005 | AttrNameToString{}, |
| 2006 | ), |
| 2007 | } |
| 2008 | registerCustomModule := func(ctx android.RegistrationContext) { |
| 2009 | ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice) |
| 2010 | } |
| 2011 | RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{ |
| 2012 | AlreadyExistingBuildContents: alreadyPresentBuildFile, |
| 2013 | Blueprint: bp, |
| 2014 | ExpectedBazelTargets: expectedBazelTargets, |
| 2015 | Description: "Not duplicating work for an already-present BUILD target (different generated name)", |
| 2016 | }) |
| 2017 | } |
| 2018 | |
Chris Parsons | 5011e61 | 2023-09-13 23:33:20 +0000 | [diff] [blame] | 2019 | // Verifies that if a module is defined in pkg1/Android.bp, that a target present |
| 2020 | // in pkg2/BUILD.bazel does not result in the module being labeled "already defined |
| 2021 | // in a BUILD file". |
| 2022 | func TestBuildTargetPresentOtherDirectory(t *testing.T) { |
| 2023 | bp := ` |
| 2024 | custom { |
| 2025 | name: "foo", |
| 2026 | } |
| 2027 | ` |
| 2028 | expectedBazelTargets := []string{ |
| 2029 | MakeBazelTarget( |
| 2030 | "custom", |
| 2031 | "foo", |
| 2032 | AttrNameToString{}, |
| 2033 | ), |
| 2034 | } |
| 2035 | registerCustomModule := func(ctx android.RegistrationContext) { |
| 2036 | ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice) |
| 2037 | } |
| 2038 | RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{ |
| 2039 | KeepBuildFileForDirs: []string{"other_pkg"}, |
| 2040 | Filesystem: map[string]string{ |
| 2041 | "other_pkg/BUILD.bazel": MakeBazelTarget("custom", "foo", AttrNameToString{}), |
| 2042 | }, |
| 2043 | Blueprint: bp, |
| 2044 | ExpectedBazelTargets: expectedBazelTargets, |
| 2045 | Description: "Not treating a BUILD target as the bazel definition for a module in another package", |
| 2046 | }) |
| 2047 | } |
| 2048 | |
Spandan Das | 3131d67 | 2023-08-03 22:33:47 +0000 | [diff] [blame] | 2049 | // If CommonAttributes.Dir is set, the bazel target should be created in that dir |
| 2050 | func TestCreateBazelTargetInDifferentDir(t *testing.T) { |
| 2051 | t.Parallel() |
| 2052 | bp := ` |
| 2053 | custom { |
| 2054 | name: "foo", |
| 2055 | dir: "subdir", |
| 2056 | } |
| 2057 | ` |
| 2058 | registerCustomModule := func(ctx android.RegistrationContext) { |
| 2059 | ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice) |
| 2060 | } |
| 2061 | // Check that foo is not created in root dir |
| 2062 | RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{ |
| 2063 | Description: "foo is not created in root dir because it sets dir explicitly", |
| 2064 | Blueprint: bp, |
| 2065 | Filesystem: map[string]string{ |
| 2066 | "subdir/Android.bp": "", |
| 2067 | }, |
| 2068 | ExpectedBazelTargets: []string{}, |
| 2069 | }) |
| 2070 | // Check that foo is created in `subdir` |
| 2071 | RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{ |
| 2072 | Description: "foo is created in `subdir` because it sets dir explicitly", |
| 2073 | Blueprint: bp, |
| 2074 | Filesystem: map[string]string{ |
| 2075 | "subdir/Android.bp": "", |
| 2076 | }, |
| 2077 | Dir: "subdir", |
| 2078 | ExpectedBazelTargets: []string{ |
| 2079 | MakeBazelTarget("custom", "foo", AttrNameToString{}), |
| 2080 | }, |
| 2081 | }) |
| 2082 | // Check that we cannot create target in different dir if it is does not an Android.bp |
| 2083 | RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{ |
| 2084 | Description: "foo cannot be created in `subdir` because it does not contain an Android.bp file", |
| 2085 | Blueprint: bp, |
| 2086 | Dir: "subdir", |
| 2087 | ExpectedErr: fmt.Errorf("Cannot use ca.Dir to create a BazelTarget in dir: subdir since it does not contain an Android.bp file"), |
| 2088 | }) |
| 2089 | |
| 2090 | } |
Chris Parsons | 5f1b3c7 | 2023-09-28 20:41:03 +0000 | [diff] [blame] | 2091 | |
| 2092 | func TestBp2buildDepsMutator_missingTransitiveDep(t *testing.T) { |
| 2093 | bp := ` |
| 2094 | custom { |
| 2095 | name: "foo", |
| 2096 | } |
| 2097 | |
| 2098 | custom { |
| 2099 | name: "has_deps", |
| 2100 | arch_paths: [":has_missing_dep", ":foo"], |
| 2101 | } |
| 2102 | |
| 2103 | custom { |
| 2104 | name: "has_missing_dep", |
| 2105 | arch_paths: [":missing"], |
| 2106 | } |
| 2107 | ` |
| 2108 | expectedBazelTargets := []string{ |
| 2109 | MakeBazelTarget( |
| 2110 | "custom", |
| 2111 | "foo", |
| 2112 | AttrNameToString{}, |
| 2113 | ), |
| 2114 | } |
| 2115 | registerCustomModule := func(ctx android.RegistrationContext) { |
| 2116 | ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice) |
| 2117 | } |
| 2118 | RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{ |
| 2119 | Blueprint: bp, |
| 2120 | ExpectedBazelTargets: expectedBazelTargets, |
| 2121 | Description: "Skipping conversion of a target with missing transitive dep", |
| 2122 | DepsMutator: true, |
| 2123 | }) |
| 2124 | } |
| 2125 | |
| 2126 | func TestBp2buildDepsMutator_missingDirectDep(t *testing.T) { |
| 2127 | bp := ` |
| 2128 | custom { |
| 2129 | name: "foo", |
| 2130 | arch_paths: [":exists"], |
| 2131 | } |
| 2132 | custom { |
| 2133 | name: "exists", |
| 2134 | } |
| 2135 | |
| 2136 | custom { |
| 2137 | name: "has_missing_dep", |
| 2138 | arch_paths: [":missing"], |
| 2139 | } |
| 2140 | ` |
| 2141 | expectedBazelTargets := []string{ |
| 2142 | MakeBazelTarget( |
| 2143 | "custom", |
| 2144 | "foo", |
| 2145 | AttrNameToString{"arch_paths": `[":exists"]`}, |
| 2146 | ), |
| 2147 | MakeBazelTarget( |
| 2148 | "custom", |
| 2149 | "exists", |
| 2150 | AttrNameToString{}, |
| 2151 | ), |
| 2152 | } |
| 2153 | registerCustomModule := func(ctx android.RegistrationContext) { |
| 2154 | ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice) |
| 2155 | } |
| 2156 | RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{ |
| 2157 | Blueprint: bp, |
| 2158 | ExpectedBazelTargets: expectedBazelTargets, |
| 2159 | Description: "Skipping conversion of a target with missing direct dep", |
| 2160 | DepsMutator: true, |
| 2161 | }) |
| 2162 | } |
| 2163 | |
| 2164 | func TestBp2buildDepsMutator_unconvertedDirectDep(t *testing.T) { |
| 2165 | bp := ` |
| 2166 | custom { |
| 2167 | name: "has_unconverted_dep", |
| 2168 | arch_paths: [":unconvertible"], |
| 2169 | } |
| 2170 | |
| 2171 | custom { |
| 2172 | name: "unconvertible", |
| 2173 | does_not_convert_to_bazel: true |
| 2174 | } |
| 2175 | ` |
| 2176 | registerCustomModule := func(ctx android.RegistrationContext) { |
| 2177 | ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice) |
| 2178 | } |
| 2179 | RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{ |
| 2180 | Blueprint: bp, |
| 2181 | ExpectedBazelTargets: []string{}, |
| 2182 | Description: "Skipping conversion of a target with unconverted direct dep", |
| 2183 | DepsMutator: true, |
| 2184 | }) |
| 2185 | } |
| 2186 | |
| 2187 | func TestBp2buildDepsMutator_unconvertedTransitiveDep(t *testing.T) { |
| 2188 | bp := ` |
| 2189 | custom { |
| 2190 | name: "foo", |
| 2191 | arch_paths: [":has_unconverted_dep", ":bar"], |
| 2192 | } |
| 2193 | |
| 2194 | custom { |
| 2195 | name: "bar", |
| 2196 | } |
| 2197 | |
| 2198 | custom { |
| 2199 | name: "has_unconverted_dep", |
| 2200 | arch_paths: [":unconvertible"], |
| 2201 | } |
| 2202 | |
| 2203 | custom { |
| 2204 | name: "unconvertible", |
| 2205 | does_not_convert_to_bazel: true |
| 2206 | } |
| 2207 | ` |
| 2208 | expectedBazelTargets := []string{ |
| 2209 | MakeBazelTarget( |
| 2210 | "custom", |
| 2211 | "bar", |
| 2212 | AttrNameToString{}, |
| 2213 | ), |
| 2214 | } |
| 2215 | registerCustomModule := func(ctx android.RegistrationContext) { |
| 2216 | ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice) |
| 2217 | } |
| 2218 | RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{ |
| 2219 | Blueprint: bp, |
| 2220 | ExpectedBazelTargets: expectedBazelTargets, |
| 2221 | Description: "Skipping conversion of a target with unconverted transitive dep", |
| 2222 | DepsMutator: true, |
| 2223 | }) |
| 2224 | } |
| 2225 | |
| 2226 | func TestBp2buildDepsMutator_alreadyExistsBuildDeps(t *testing.T) { |
| 2227 | bp := ` |
| 2228 | custom { |
| 2229 | name: "foo", |
| 2230 | arch_paths: [":bar"], |
| 2231 | } |
| 2232 | custom { |
| 2233 | name: "bar", |
| 2234 | arch_paths: [":checked_in"], |
| 2235 | } |
| 2236 | custom { |
| 2237 | name: "checked_in", |
| 2238 | arch_paths: [":checked_in"], |
| 2239 | does_not_convert_to_bazel: true |
| 2240 | } |
| 2241 | ` |
| 2242 | expectedBazelTargets := []string{ |
| 2243 | MakeBazelTarget( |
| 2244 | "custom", |
| 2245 | "foo", |
| 2246 | AttrNameToString{"arch_paths": `[":bar"]`}, |
| 2247 | ), |
| 2248 | MakeBazelTarget( |
| 2249 | "custom", |
| 2250 | "bar", |
| 2251 | AttrNameToString{"arch_paths": `[":checked_in"]`}, |
| 2252 | ), |
| 2253 | } |
| 2254 | registerCustomModule := func(ctx android.RegistrationContext) { |
| 2255 | ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice) |
| 2256 | } |
| 2257 | RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{ |
| 2258 | StubbedBuildDefinitions: []string{"checked_in"}, |
| 2259 | Blueprint: bp, |
| 2260 | ExpectedBazelTargets: expectedBazelTargets, |
| 2261 | Description: "Convert target with already-existing build dep", |
| 2262 | DepsMutator: true, |
| 2263 | }) |
| 2264 | } |
| 2265 | |
| 2266 | // Tests that deps of libc are always considered valid for libc. This circumvents |
| 2267 | // an issue that, in a variantless graph (such as bp2build's), libc has the |
| 2268 | // unique predicament that it depends on itself. |
| 2269 | func TestBp2buildDepsMutator_depOnLibc(t *testing.T) { |
| 2270 | bp := ` |
| 2271 | custom { |
| 2272 | name: "foo", |
| 2273 | arch_paths: [":libc"], |
| 2274 | } |
| 2275 | custom { |
| 2276 | name: "libc", |
| 2277 | arch_paths: [":libc_dep"], |
| 2278 | } |
| 2279 | custom { |
| 2280 | name: "libc_dep", |
| 2281 | does_not_convert_to_bazel: true |
| 2282 | } |
| 2283 | ` |
| 2284 | expectedBazelTargets := []string{ |
| 2285 | MakeBazelTarget( |
| 2286 | "custom", |
| 2287 | "foo", |
| 2288 | AttrNameToString{"arch_paths": `[":libc"]`}, |
| 2289 | ), |
| 2290 | MakeBazelTarget( |
| 2291 | "custom", |
| 2292 | "libc", |
| 2293 | AttrNameToString{"arch_paths": `[":libc_dep"]`}, |
| 2294 | ), |
| 2295 | } |
| 2296 | registerCustomModule := func(ctx android.RegistrationContext) { |
| 2297 | ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice) |
| 2298 | } |
| 2299 | RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{ |
| 2300 | StubbedBuildDefinitions: []string{"checked_in"}, |
| 2301 | Blueprint: bp, |
| 2302 | ExpectedBazelTargets: expectedBazelTargets, |
| 2303 | Description: "Convert target with dep on libc", |
| 2304 | DepsMutator: true, |
| 2305 | }) |
| 2306 | } |