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: "non-existent dep", |
| 353 | Blueprint: `custom { |
Liz Kammer | daa09ef | 2021-12-15 15:35:38 -0500 | [diff] [blame] | 354 | name: "has_dep", |
| 355 | arch_paths: [":dep"], |
| 356 | bazel_module: { bp2build_available: true }, |
| 357 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 358 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 359 | MakeBazelTarget("custom", "has_dep", AttrNameToString{ |
Liz Kammer | daa09ef | 2021-12-15 15:35:38 -0500 | [diff] [blame] | 360 | "arch_paths": `[":dep__BP2BUILD__MISSING__DEP"]`, |
| 361 | }), |
| 362 | }, |
| 363 | }, |
| 364 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 365 | Description: "arch-variant srcs", |
| 366 | Blueprint: `custom { |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 367 | name: "arch_paths", |
| 368 | arch: { |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 369 | x86: { arch_paths: ["x86.txt"] }, |
| 370 | x86_64: { arch_paths: ["x86_64.txt"] }, |
| 371 | arm: { arch_paths: ["arm.txt"] }, |
| 372 | arm64: { arch_paths: ["arm64.txt"] }, |
Colin Cross | f05b0d3 | 2022-07-14 18:10:34 -0700 | [diff] [blame] | 373 | riscv64: { arch_paths: ["riscv64.txt"] }, |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 374 | }, |
| 375 | target: { |
| 376 | linux: { arch_paths: ["linux.txt"] }, |
| 377 | bionic: { arch_paths: ["bionic.txt"] }, |
| 378 | host: { arch_paths: ["host.txt"] }, |
| 379 | not_windows: { arch_paths: ["not_windows.txt"] }, |
| 380 | android: { arch_paths: ["android.txt"] }, |
| 381 | linux_musl: { arch_paths: ["linux_musl.txt"] }, |
| 382 | musl: { arch_paths: ["musl.txt"] }, |
| 383 | linux_glibc: { arch_paths: ["linux_glibc.txt"] }, |
| 384 | glibc: { arch_paths: ["glibc.txt"] }, |
| 385 | linux_bionic: { arch_paths: ["linux_bionic.txt"] }, |
| 386 | darwin: { arch_paths: ["darwin.txt"] }, |
| 387 | windows: { arch_paths: ["windows.txt"] }, |
| 388 | }, |
| 389 | multilib: { |
| 390 | lib32: { arch_paths: ["lib32.txt"] }, |
| 391 | lib64: { arch_paths: ["lib64.txt"] }, |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 392 | }, |
| 393 | bazel_module: { bp2build_available: true }, |
| 394 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 395 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 396 | MakeBazelTarget("custom", "arch_paths", AttrNameToString{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 397 | "arch_paths": `select({ |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 398 | "//build/bazel/platforms/arch:arm": [ |
| 399 | "arm.txt", |
| 400 | "lib32.txt", |
| 401 | ], |
| 402 | "//build/bazel/platforms/arch:arm64": [ |
| 403 | "arm64.txt", |
| 404 | "lib64.txt", |
| 405 | ], |
Colin Cross | f05b0d3 | 2022-07-14 18:10:34 -0700 | [diff] [blame] | 406 | "//build/bazel/platforms/arch:riscv64": [ |
| 407 | "riscv64.txt", |
| 408 | "lib64.txt", |
| 409 | ], |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 410 | "//build/bazel/platforms/arch:x86": [ |
| 411 | "x86.txt", |
| 412 | "lib32.txt", |
| 413 | ], |
| 414 | "//build/bazel/platforms/arch:x86_64": [ |
| 415 | "x86_64.txt", |
| 416 | "lib64.txt", |
| 417 | ], |
| 418 | "//conditions:default": [], |
| 419 | }) + select({ |
| 420 | "//build/bazel/platforms/os:android": [ |
| 421 | "linux.txt", |
| 422 | "bionic.txt", |
| 423 | "android.txt", |
| 424 | ], |
| 425 | "//build/bazel/platforms/os:darwin": [ |
| 426 | "host.txt", |
| 427 | "darwin.txt", |
| 428 | "not_windows.txt", |
| 429 | ], |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 430 | "//build/bazel/platforms/os:linux_bionic": [ |
| 431 | "host.txt", |
| 432 | "linux.txt", |
| 433 | "bionic.txt", |
| 434 | "linux_bionic.txt", |
| 435 | "not_windows.txt", |
| 436 | ], |
Colin Cross | 133782e | 2022-12-20 15:29:31 -0800 | [diff] [blame] | 437 | "//build/bazel/platforms/os:linux_glibc": [ |
| 438 | "host.txt", |
| 439 | "linux.txt", |
| 440 | "glibc.txt", |
| 441 | "linux_glibc.txt", |
| 442 | "not_windows.txt", |
| 443 | ], |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 444 | "//build/bazel/platforms/os:linux_musl": [ |
| 445 | "host.txt", |
| 446 | "linux.txt", |
| 447 | "musl.txt", |
| 448 | "linux_musl.txt", |
| 449 | "not_windows.txt", |
| 450 | ], |
| 451 | "//build/bazel/platforms/os:windows": [ |
| 452 | "host.txt", |
| 453 | "windows.txt", |
| 454 | ], |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 455 | "//conditions:default": [], |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 456 | })`, |
| 457 | }), |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 458 | }, |
| 459 | }, |
| 460 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 461 | Description: "arch-variant deps", |
| 462 | Blueprint: `custom { |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 463 | name: "has_dep", |
| 464 | arch: { |
| 465 | x86: { |
| 466 | arch_paths: [":dep"], |
| 467 | }, |
| 468 | }, |
| 469 | bazel_module: { bp2build_available: true }, |
| 470 | } |
| 471 | |
| 472 | custom { |
| 473 | name: "dep", |
| 474 | arch_paths: ["abc"], |
| 475 | bazel_module: { bp2build_available: true }, |
| 476 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 477 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 478 | MakeBazelTarget("custom", "dep", AttrNameToString{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 479 | "arch_paths": `["abc"]`, |
| 480 | }), |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 481 | MakeBazelTarget("custom", "has_dep", AttrNameToString{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 482 | "arch_paths": `select({ |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 483 | "//build/bazel/platforms/arch:x86": [":dep"], |
| 484 | "//conditions:default": [], |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 485 | })`, |
| 486 | }), |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 487 | }, |
Jingwen Chen | 58a12b8 | 2021-03-30 13:08:36 +0000 | [diff] [blame] | 488 | }, |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 489 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 490 | Description: "embedded props", |
| 491 | Blueprint: `custom { |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 492 | name: "embedded_props", |
| 493 | 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", "embedded_props", AttrNameToString{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 498 | "embedded_attr": `"abc"`, |
| 499 | }), |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 500 | }, |
| 501 | }, |
| 502 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 503 | Description: "ptr to embedded props", |
| 504 | Blueprint: `custom { |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 505 | name: "ptr_to_embedded_props", |
| 506 | other_embedded_prop: "abc", |
| 507 | bazel_module: { bp2build_available: true }, |
| 508 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 509 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 510 | MakeBazelTarget("custom", "ptr_to_embedded_props", AttrNameToString{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 511 | "other_embedded_attr": `"abc"`, |
| 512 | }), |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 513 | }, |
| 514 | }, |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | dir := "." |
| 518 | for _, testCase := range testCases { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 519 | t.Run(testCase.Description, func(t *testing.T) { |
| 520 | config := android.TestConfig(buildDir, nil, testCase.Blueprint, nil) |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 521 | ctx := android.NewTestContext(config) |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 522 | |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 523 | registerCustomModuleForBp2buildConversion(ctx) |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 524 | |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 525 | _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) |
| 526 | if errored(t, testCase, errs) { |
| 527 | return |
| 528 | } |
| 529 | _, errs = ctx.ResolveDependencies(config) |
| 530 | if errored(t, testCase, errs) { |
| 531 | return |
| 532 | } |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 533 | |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 534 | codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "") |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 535 | bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir) |
| 536 | android.FailIfErrored(t, err) |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 537 | |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 538 | if actualCount, expectedCount := len(bazelTargets), len(testCase.ExpectedBazelTargets); actualCount != expectedCount { |
| 539 | 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] | 540 | } else { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 541 | for i, expectedBazelTarget := range testCase.ExpectedBazelTargets { |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 542 | actualBazelTarget := bazelTargets[i] |
| 543 | if actualBazelTarget.content != expectedBazelTarget { |
| 544 | t.Errorf( |
| 545 | "Expected generated Bazel target to be '%s', got '%s'", |
| 546 | expectedBazelTarget, |
| 547 | actualBazelTarget.content, |
| 548 | ) |
| 549 | } |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 550 | } |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 551 | } |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 552 | }) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 553 | } |
| 554 | } |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 555 | |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 556 | func TestBp2buildHostAndDevice(t *testing.T) { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 557 | testCases := []Bp2buildTestCase{ |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 558 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 559 | Description: "host and device, device only", |
| 560 | ModuleTypeUnderTest: "custom", |
| 561 | ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice, |
| 562 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 563 | name: "foo", |
| 564 | bazel_module: { bp2build_available: true }, |
| 565 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 566 | ExpectedBazelTargets: []string{ |
| 567 | makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported), |
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, both", |
| 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: true, |
| 577 | bazel_module: { bp2build_available: true }, |
| 578 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 579 | ExpectedBazelTargets: []string{ |
| 580 | MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{}), |
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, host explicitly disabled", |
| 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 | bazel_module: { bp2build_available: true }, |
| 591 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 592 | ExpectedBazelTargets: []string{ |
| 593 | makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported), |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 594 | }, |
| 595 | }, |
| 596 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 597 | Description: "host and device, neither", |
| 598 | ModuleTypeUnderTest: "custom", |
| 599 | ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice, |
| 600 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 601 | name: "foo", |
| 602 | host_supported: false, |
| 603 | device_supported: false, |
| 604 | bazel_module: { bp2build_available: true }, |
| 605 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 606 | ExpectedBazelTargets: []string{ |
| 607 | MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{ |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 608 | "target_compatible_with": `["@platforms//:incompatible"]`, |
| 609 | }), |
| 610 | }, |
| 611 | }, |
| 612 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 613 | Description: "host and device, neither, cannot override with product_var", |
| 614 | ModuleTypeUnderTest: "custom", |
| 615 | ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice, |
| 616 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 617 | name: "foo", |
| 618 | host_supported: false, |
| 619 | device_supported: false, |
| 620 | product_variables: { unbundled_build: { enabled: true } }, |
| 621 | bazel_module: { bp2build_available: true }, |
| 622 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 623 | ExpectedBazelTargets: []string{ |
| 624 | MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{ |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 625 | "target_compatible_with": `["@platforms//:incompatible"]`, |
| 626 | }), |
| 627 | }, |
| 628 | }, |
| 629 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 630 | Description: "host and device, both, disabled overrided with product_var", |
| 631 | ModuleTypeUnderTest: "custom", |
| 632 | ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice, |
| 633 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 634 | name: "foo", |
| 635 | host_supported: true, |
| 636 | device_supported: true, |
| 637 | enabled: false, |
| 638 | product_variables: { unbundled_build: { enabled: true } }, |
| 639 | bazel_module: { bp2build_available: true }, |
| 640 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 641 | ExpectedBazelTargets: []string{ |
| 642 | MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{ |
Cole Faust | 87c0c33 | 2023-07-31 12:10:12 -0700 | [diff] [blame] | 643 | "target_compatible_with": `select({ |
| 644 | "//build/bazel/product_config/config_settings:unbundled_build": [], |
| 645 | "//conditions:default": ["@platforms//:incompatible"], |
| 646 | })`, |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 647 | }), |
| 648 | }, |
| 649 | }, |
| 650 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 651 | Description: "host and device, neither, cannot override with arch enabled", |
| 652 | ModuleTypeUnderTest: "custom", |
| 653 | ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice, |
| 654 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 655 | name: "foo", |
| 656 | host_supported: false, |
| 657 | device_supported: false, |
| 658 | arch: { x86: { enabled: true } }, |
| 659 | bazel_module: { bp2build_available: true }, |
| 660 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 661 | ExpectedBazelTargets: []string{ |
| 662 | MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{ |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 663 | "target_compatible_with": `["@platforms//:incompatible"]`, |
| 664 | }), |
| 665 | }, |
| 666 | }, |
| 667 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 668 | Description: "host and device, host only", |
| 669 | ModuleTypeUnderTest: "custom", |
| 670 | ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice, |
| 671 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 672 | name: "foo", |
| 673 | host_supported: true, |
| 674 | device_supported: false, |
| 675 | bazel_module: { bp2build_available: true }, |
| 676 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 677 | ExpectedBazelTargets: []string{ |
| 678 | makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.HostSupported), |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 679 | }, |
| 680 | }, |
| 681 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 682 | Description: "host only", |
| 683 | ModuleTypeUnderTest: "custom", |
| 684 | ModuleTypeUnderTestFactory: customModuleFactoryHostSupported, |
| 685 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 686 | name: "foo", |
| 687 | bazel_module: { bp2build_available: true }, |
| 688 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 689 | ExpectedBazelTargets: []string{ |
| 690 | makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.HostSupported), |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 691 | }, |
| 692 | }, |
| 693 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 694 | Description: "device only", |
| 695 | ModuleTypeUnderTest: "custom", |
| 696 | ModuleTypeUnderTestFactory: customModuleFactoryDeviceSupported, |
| 697 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 698 | name: "foo", |
| 699 | bazel_module: { bp2build_available: true }, |
| 700 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 701 | ExpectedBazelTargets: []string{ |
| 702 | makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported), |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 703 | }, |
| 704 | }, |
| 705 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 706 | Description: "host and device default, default", |
| 707 | ModuleTypeUnderTest: "custom", |
| 708 | ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault, |
| 709 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 710 | name: "foo", |
| 711 | bazel_module: { bp2build_available: true }, |
| 712 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 713 | ExpectedBazelTargets: []string{ |
| 714 | MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{}), |
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, device 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 | host_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.DeviceSupported), |
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, host only", |
| 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 | device_supported: false, |
| 737 | bazel_module: { bp2build_available: true }, |
| 738 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 739 | ExpectedBazelTargets: []string{ |
| 740 | makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.HostSupported), |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 741 | }, |
| 742 | }, |
| 743 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 744 | Description: "host and device default, neither", |
| 745 | ModuleTypeUnderTest: "custom", |
| 746 | ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault, |
| 747 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 748 | name: "foo", |
| 749 | host_supported: false, |
| 750 | device_supported: false, |
| 751 | bazel_module: { bp2build_available: true }, |
| 752 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 753 | ExpectedBazelTargets: []string{ |
| 754 | MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{ |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 755 | "target_compatible_with": `["@platforms//:incompatible"]`, |
| 756 | }), |
| 757 | }, |
| 758 | }, |
| 759 | } |
| 760 | |
| 761 | for _, tc := range testCases { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 762 | t.Run(tc.Description, func(t *testing.T) { |
Trevor Radcliffe | 1b4b2d9 | 2022-09-01 18:57:01 +0000 | [diff] [blame] | 763 | RunBp2BuildTestCaseSimple(t, tc) |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 764 | }) |
| 765 | } |
| 766 | } |
| 767 | |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 768 | func TestLoadStatements(t *testing.T) { |
| 769 | testCases := []struct { |
| 770 | bazelTargets BazelTargets |
| 771 | expectedLoadStatements string |
| 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 | }, |
| 784 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`, |
| 785 | }, |
| 786 | { |
| 787 | bazelTargets: BazelTargets{ |
| 788 | BazelTarget{ |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 789 | name: "foo", |
| 790 | ruleClass: "cc_library", |
| 791 | loads: []BazelLoad{{ |
| 792 | file: "//build/bazel/rules:cc.bzl", |
| 793 | symbols: []BazelLoadSymbol{{symbol: "cc_library"}}, |
| 794 | }}, |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 795 | }, |
| 796 | BazelTarget{ |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 797 | name: "bar", |
| 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 | }, |
| 805 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`, |
| 806 | }, |
| 807 | { |
| 808 | bazelTargets: BazelTargets{ |
| 809 | BazelTarget{ |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 810 | name: "foo", |
| 811 | ruleClass: "cc_library", |
| 812 | loads: []BazelLoad{{ |
| 813 | file: "//build/bazel/rules:cc.bzl", |
| 814 | symbols: []BazelLoadSymbol{{symbol: "cc_library"}}, |
| 815 | }}, |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 816 | }, |
| 817 | BazelTarget{ |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 818 | name: "bar", |
| 819 | ruleClass: "cc_binary", |
| 820 | loads: []BazelLoad{{ |
| 821 | file: "//build/bazel/rules:cc.bzl", |
| 822 | symbols: []BazelLoadSymbol{{symbol: "cc_binary"}}, |
| 823 | }}, |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 824 | }, |
| 825 | }, |
| 826 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")`, |
| 827 | }, |
| 828 | { |
| 829 | bazelTargets: BazelTargets{ |
| 830 | BazelTarget{ |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 831 | name: "foo", |
| 832 | ruleClass: "cc_library", |
| 833 | loads: []BazelLoad{{ |
| 834 | file: "//build/bazel/rules:cc.bzl", |
| 835 | symbols: []BazelLoadSymbol{{symbol: "cc_library"}}, |
| 836 | }}, |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 837 | }, |
| 838 | BazelTarget{ |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 839 | name: "bar", |
| 840 | ruleClass: "cc_binary", |
| 841 | loads: []BazelLoad{{ |
| 842 | file: "//build/bazel/rules:cc.bzl", |
| 843 | symbols: []BazelLoadSymbol{{symbol: "cc_binary"}}, |
| 844 | }}, |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 845 | }, |
| 846 | BazelTarget{ |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 847 | name: "baz", |
| 848 | ruleClass: "java_binary", |
| 849 | loads: []BazelLoad{{ |
| 850 | file: "//build/bazel/rules:java.bzl", |
| 851 | symbols: []BazelLoadSymbol{{symbol: "java_binary"}}, |
| 852 | }}, |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 853 | }, |
| 854 | }, |
| 855 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library") |
| 856 | load("//build/bazel/rules:java.bzl", "java_binary")`, |
| 857 | }, |
| 858 | { |
| 859 | bazelTargets: BazelTargets{ |
| 860 | BazelTarget{ |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 861 | name: "foo", |
| 862 | ruleClass: "cc_binary", |
| 863 | loads: []BazelLoad{{ |
| 864 | file: "//build/bazel/rules:cc.bzl", |
| 865 | symbols: []BazelLoadSymbol{{symbol: "cc_binary"}}, |
| 866 | }}, |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 867 | }, |
| 868 | BazelTarget{ |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 869 | name: "bar", |
| 870 | ruleClass: "java_binary", |
| 871 | loads: []BazelLoad{{ |
| 872 | file: "//build/bazel/rules:java.bzl", |
| 873 | symbols: []BazelLoadSymbol{{symbol: "java_binary"}}, |
| 874 | }}, |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 875 | }, |
| 876 | BazelTarget{ |
| 877 | name: "baz", |
| 878 | ruleClass: "genrule", |
Cole Faust | b4cb0c8 | 2023-09-14 15:16:58 -0700 | [diff] [blame] | 879 | // Note: no loads for native rules |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 880 | }, |
| 881 | }, |
| 882 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary") |
| 883 | load("//build/bazel/rules:java.bzl", "java_binary")`, |
| 884 | }, |
| 885 | } |
| 886 | |
| 887 | for _, testCase := range testCases { |
| 888 | actual := testCase.bazelTargets.LoadStatements() |
| 889 | expected := testCase.expectedLoadStatements |
| 890 | if actual != expected { |
| 891 | t.Fatalf("Expected load statements to be %s, got %s", expected, actual) |
| 892 | } |
| 893 | } |
| 894 | |
| 895 | } |
| 896 | |
| 897 | func TestGenerateBazelTargetModules_OneToMany_LoadedFromStarlark(t *testing.T) { |
| 898 | testCases := []struct { |
| 899 | bp string |
| 900 | expectedBazelTarget string |
| 901 | expectedBazelTargetCount int |
| 902 | expectedLoadStatements string |
| 903 | }{ |
| 904 | { |
| 905 | bp: `custom { |
| 906 | name: "bar", |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 907 | host_supported: true, |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 908 | one_to_many_prop: true, |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 909 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 910 | }`, |
| 911 | expectedBazelTarget: `my_library( |
| 912 | name = "bar", |
| 913 | ) |
| 914 | |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 915 | proto_library( |
| 916 | name = "bar_proto_library_deps", |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 917 | ) |
| 918 | |
| 919 | my_proto_library( |
| 920 | name = "bar_my_proto_library_deps", |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 921 | )`, |
| 922 | expectedBazelTargetCount: 3, |
| 923 | expectedLoadStatements: `load("//build/bazel/rules:proto.bzl", "my_proto_library", "proto_library") |
| 924 | load("//build/bazel/rules:rules.bzl", "my_library")`, |
| 925 | }, |
| 926 | } |
| 927 | |
| 928 | dir := "." |
| 929 | for _, testCase := range testCases { |
| 930 | config := android.TestConfig(buildDir, nil, testCase.bp, nil) |
| 931 | ctx := android.NewTestContext(config) |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 932 | ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice) |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 933 | ctx.RegisterForBazelConversion() |
| 934 | |
| 935 | _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) |
| 936 | android.FailIfErrored(t, errs) |
| 937 | _, errs = ctx.ResolveDependencies(config) |
| 938 | android.FailIfErrored(t, errs) |
| 939 | |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 940 | codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "") |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 941 | bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir) |
| 942 | android.FailIfErrored(t, err) |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 943 | if actualCount := len(bazelTargets); actualCount != testCase.expectedBazelTargetCount { |
| 944 | t.Fatalf("Expected %d bazel target, got %d", testCase.expectedBazelTargetCount, actualCount) |
| 945 | } |
| 946 | |
| 947 | actualBazelTargets := bazelTargets.String() |
| 948 | if actualBazelTargets != testCase.expectedBazelTarget { |
| 949 | t.Errorf( |
| 950 | "Expected generated Bazel target to be '%s', got '%s'", |
| 951 | testCase.expectedBazelTarget, |
| 952 | actualBazelTargets, |
| 953 | ) |
| 954 | } |
| 955 | |
| 956 | actualLoadStatements := bazelTargets.LoadStatements() |
| 957 | if actualLoadStatements != testCase.expectedLoadStatements { |
| 958 | t.Errorf( |
| 959 | "Expected generated load statements to be '%s', got '%s'", |
| 960 | testCase.expectedLoadStatements, |
| 961 | actualLoadStatements, |
| 962 | ) |
| 963 | } |
| 964 | } |
| 965 | } |
| 966 | |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 967 | func TestModuleTypeBp2Build(t *testing.T) { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 968 | testCases := []Bp2buildTestCase{ |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 969 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 970 | Description: "filegroup with does not specify srcs", |
| 971 | ModuleTypeUnderTest: "filegroup", |
| 972 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 973 | Blueprint: `filegroup { |
Liz Kammer | ebfcf67 | 2021-02-16 15:00:05 -0500 | [diff] [blame] | 974 | name: "fg_foo", |
| 975 | bazel_module: { bp2build_available: true }, |
| 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 | ebfcf67 | 2021-02-16 15:00:05 -0500 | [diff] [blame] | 979 | }, |
| 980 | }, |
| 981 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 982 | Description: "filegroup with no 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: [], |
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 | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 992 | }, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 993 | }, |
| 994 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 995 | Description: "filegroup with srcs", |
| 996 | ModuleTypeUnderTest: "filegroup", |
| 997 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 998 | Blueprint: `filegroup { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 999 | name: "fg_foo", |
| 1000 | srcs: ["a", "b"], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1001 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 1002 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1003 | ExpectedBazelTargets: []string{ |
| 1004 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1005 | "srcs": `[ |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 1006 | "a", |
| 1007 | "b", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1008 | ]`, |
| 1009 | }), |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 1010 | }, |
| 1011 | }, |
| 1012 | { |
Usta Shrestha | d558031 | 2022-09-23 16:46:38 -0400 | [diff] [blame] | 1013 | Description: "filegroup with dot-slash-prefixed srcs", |
| 1014 | ModuleTypeUnderTest: "filegroup", |
| 1015 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1016 | Blueprint: `filegroup { |
| 1017 | name: "fg_foo", |
| 1018 | srcs: ["./a", "./b"], |
| 1019 | bazel_module: { bp2build_available: true }, |
| 1020 | }`, |
| 1021 | ExpectedBazelTargets: []string{ |
| 1022 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
| 1023 | "srcs": `[ |
| 1024 | "a", |
| 1025 | "b", |
| 1026 | ]`, |
| 1027 | }), |
| 1028 | }, |
| 1029 | }, |
| 1030 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1031 | Description: "filegroup with excludes srcs", |
| 1032 | ModuleTypeUnderTest: "filegroup", |
| 1033 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1034 | Blueprint: `filegroup { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 1035 | name: "fg_foo", |
| 1036 | srcs: ["a", "b"], |
| 1037 | exclude_srcs: ["a"], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1038 | bazel_module: { bp2build_available: true }, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 1039 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1040 | ExpectedBazelTargets: []string{ |
| 1041 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1042 | "srcs": `["b"]`, |
| 1043 | }), |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 1044 | }, |
| 1045 | }, |
| 1046 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1047 | Description: "depends_on_other_dir_module", |
| 1048 | ModuleTypeUnderTest: "filegroup", |
| 1049 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1050 | Blueprint: `filegroup { |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1051 | name: "fg_foo", |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 1052 | srcs: [ |
Jingwen Chen | b4628eb | 2021-04-08 14:40:57 +0000 | [diff] [blame] | 1053 | ":foo", |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 1054 | "c", |
| 1055 | ], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1056 | bazel_module: { bp2build_available: true }, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 1057 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1058 | Filesystem: map[string]string{ |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 1059 | "other/Android.bp": `filegroup { |
| 1060 | name: "foo", |
| 1061 | srcs: ["a", "b"], |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 1062 | bazel_module: { bp2build_available: true }, |
| 1063 | }`, |
| 1064 | }, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1065 | ExpectedBazelTargets: []string{ |
| 1066 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1067 | "srcs": `[ |
| 1068 | "//other:foo", |
| 1069 | "c", |
| 1070 | ]`, |
| 1071 | }), |
| 1072 | }, |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 1073 | }, |
| 1074 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1075 | Description: "depends_on_other_unconverted_module_error", |
| 1076 | ModuleTypeUnderTest: "filegroup", |
| 1077 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1078 | UnconvertedDepsMode: errorModulesUnconvertedDeps, |
| 1079 | Blueprint: `filegroup { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1080 | name: "foobar", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1081 | srcs: [ |
| 1082 | ":foo", |
| 1083 | "c", |
| 1084 | ], |
| 1085 | bazel_module: { bp2build_available: true }, |
| 1086 | }`, |
Sasha Smundak | f2bb26f | 2022-08-04 11:28:15 -0700 | [diff] [blame] | 1087 | ExpectedErr: fmt.Errorf(`filegroup .:foobar depends on unconverted modules: foo`), |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1088 | Filesystem: map[string]string{ |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1089 | "other/Android.bp": `filegroup { |
| 1090 | name: "foo", |
| 1091 | srcs: ["a", "b"], |
| 1092 | }`, |
| 1093 | }, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 1094 | }, |
Usta Shrestha | 808bc71 | 2022-09-23 23:18:18 -0400 | [diff] [blame] | 1095 | { |
| 1096 | Description: "depends_on_other_missing_module_error", |
| 1097 | ModuleTypeUnderTest: "filegroup", |
| 1098 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1099 | UnconvertedDepsMode: errorModulesUnconvertedDeps, |
| 1100 | Blueprint: `filegroup { |
| 1101 | name: "foobar", |
| 1102 | srcs: [ |
| 1103 | "c", |
| 1104 | "//other:foo", |
| 1105 | "//other:goo", |
| 1106 | ], |
| 1107 | bazel_module: { bp2build_available: true }, |
| 1108 | }`, |
| 1109 | ExpectedErr: fmt.Errorf(`filegroup .:foobar depends on missing modules: //other:goo`), |
| 1110 | Filesystem: map[string]string{"other/Android.bp": `filegroup { |
| 1111 | name: "foo", |
| 1112 | srcs: ["a"], |
| 1113 | bazel_module: { bp2build_available: true }, |
| 1114 | } |
| 1115 | `, |
| 1116 | }, |
| 1117 | }, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 1118 | } |
| 1119 | |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 1120 | for _, testCase := range testCases { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1121 | t.Run(testCase.Description, func(t *testing.T) { |
| 1122 | RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, testCase) |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 1123 | }) |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 1124 | } |
| 1125 | } |
Jingwen Chen | 041b184 | 2021-02-01 00:23:25 -0500 | [diff] [blame] | 1126 | |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1127 | func TestAllowlistingBp2buildTargetsExplicitly(t *testing.T) { |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1128 | testCases := []struct { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1129 | moduleTypeUnderTest string |
| 1130 | moduleTypeUnderTestFactory android.ModuleFactory |
| 1131 | bp string |
| 1132 | expectedCount int |
| 1133 | description string |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1134 | }{ |
| 1135 | { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1136 | description: "explicitly unavailable", |
| 1137 | moduleTypeUnderTest: "filegroup", |
| 1138 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1139 | bp: `filegroup { |
| 1140 | name: "foo", |
| 1141 | srcs: ["a", "b"], |
| 1142 | bazel_module: { bp2build_available: false }, |
| 1143 | }`, |
| 1144 | expectedCount: 0, |
| 1145 | }, |
| 1146 | { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1147 | description: "implicitly unavailable", |
| 1148 | moduleTypeUnderTest: "filegroup", |
| 1149 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1150 | bp: `filegroup { |
| 1151 | name: "foo", |
| 1152 | srcs: ["a", "b"], |
| 1153 | }`, |
| 1154 | expectedCount: 0, |
| 1155 | }, |
| 1156 | { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1157 | description: "explicitly available", |
| 1158 | moduleTypeUnderTest: "filegroup", |
| 1159 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1160 | bp: `filegroup { |
| 1161 | name: "foo", |
| 1162 | srcs: ["a", "b"], |
| 1163 | bazel_module: { bp2build_available: true }, |
| 1164 | }`, |
| 1165 | expectedCount: 1, |
| 1166 | }, |
| 1167 | { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1168 | description: "generates more than 1 target if needed", |
| 1169 | moduleTypeUnderTest: "custom", |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 1170 | moduleTypeUnderTestFactory: customModuleFactoryHostAndDevice, |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1171 | bp: `custom { |
| 1172 | name: "foo", |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1173 | one_to_many_prop: true, |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1174 | bazel_module: { bp2build_available: true }, |
| 1175 | }`, |
| 1176 | expectedCount: 3, |
| 1177 | }, |
| 1178 | } |
| 1179 | |
| 1180 | dir := "." |
| 1181 | for _, testCase := range testCases { |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 1182 | t.Run(testCase.description, func(t *testing.T) { |
| 1183 | config := android.TestConfig(buildDir, nil, testCase.bp, nil) |
| 1184 | ctx := android.NewTestContext(config) |
| 1185 | ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory) |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 1186 | ctx.RegisterForBazelConversion() |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1187 | |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 1188 | _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) |
| 1189 | android.FailIfErrored(t, errs) |
| 1190 | _, errs = ctx.ResolveDependencies(config) |
| 1191 | android.FailIfErrored(t, errs) |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1192 | |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 1193 | codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "") |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 1194 | bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir) |
| 1195 | android.FailIfErrored(t, err) |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 1196 | if actualCount := len(bazelTargets); actualCount != testCase.expectedCount { |
| 1197 | t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, testCase.expectedCount, actualCount) |
| 1198 | } |
| 1199 | }) |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1200 | } |
| 1201 | } |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1202 | |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1203 | func TestAllowlistingBp2buildTargetsWithConfig(t *testing.T) { |
| 1204 | testCases := []struct { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1205 | moduleTypeUnderTest string |
| 1206 | moduleTypeUnderTestFactory android.ModuleFactory |
| 1207 | expectedCount map[string]int |
| 1208 | description string |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 1209 | bp2buildConfig allowlists.Bp2BuildConfig |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1210 | checkDir string |
| 1211 | fs map[string]string |
MarkDacek | 9c094ca | 2023-03-16 19:15:19 +0000 | [diff] [blame] | 1212 | forceEnabledModules []string |
| 1213 | expectedErrorMessages []string |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1214 | }{ |
| 1215 | { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1216 | description: "test bp2build config package and subpackages config", |
| 1217 | moduleTypeUnderTest: "filegroup", |
| 1218 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1219 | expectedCount: map[string]int{ |
| 1220 | "migrated": 1, |
| 1221 | "migrated/but_not_really": 0, |
| 1222 | "migrated/but_not_really/but_really": 1, |
| 1223 | "not_migrated": 0, |
| 1224 | "also_not_migrated": 0, |
| 1225 | }, |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 1226 | bp2buildConfig: allowlists.Bp2BuildConfig{ |
| 1227 | "migrated": allowlists.Bp2BuildDefaultTrueRecursively, |
| 1228 | "migrated/but_not_really": allowlists.Bp2BuildDefaultFalse, |
| 1229 | "not_migrated": allowlists.Bp2BuildDefaultFalse, |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1230 | }, |
| 1231 | fs: map[string]string{ |
| 1232 | "migrated/Android.bp": `filegroup { name: "a" }`, |
| 1233 | "migrated/but_not_really/Android.bp": `filegroup { name: "b" }`, |
| 1234 | "migrated/but_not_really/but_really/Android.bp": `filegroup { name: "c" }`, |
| 1235 | "not_migrated/Android.bp": `filegroup { name: "d" }`, |
| 1236 | "also_not_migrated/Android.bp": `filegroup { name: "e" }`, |
| 1237 | }, |
| 1238 | }, |
| 1239 | { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1240 | description: "test bp2build config opt-in and opt-out", |
| 1241 | moduleTypeUnderTest: "filegroup", |
| 1242 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1243 | expectedCount: map[string]int{ |
| 1244 | "package-opt-in": 2, |
| 1245 | "package-opt-in/subpackage": 0, |
| 1246 | "package-opt-out": 1, |
| 1247 | "package-opt-out/subpackage": 0, |
| 1248 | }, |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 1249 | bp2buildConfig: allowlists.Bp2BuildConfig{ |
| 1250 | "package-opt-in": allowlists.Bp2BuildDefaultFalse, |
| 1251 | "package-opt-out": allowlists.Bp2BuildDefaultTrueRecursively, |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1252 | }, |
| 1253 | fs: map[string]string{ |
| 1254 | "package-opt-in/Android.bp": ` |
| 1255 | filegroup { name: "opt-in-a" } |
| 1256 | filegroup { name: "opt-in-b", bazel_module: { bp2build_available: true } } |
| 1257 | filegroup { name: "opt-in-c", bazel_module: { bp2build_available: true } } |
| 1258 | `, |
| 1259 | |
| 1260 | "package-opt-in/subpackage/Android.bp": ` |
| 1261 | filegroup { name: "opt-in-d" } // parent package not configured to DefaultTrueRecursively |
| 1262 | `, |
| 1263 | |
| 1264 | "package-opt-out/Android.bp": ` |
| 1265 | filegroup { name: "opt-out-a" } |
| 1266 | filegroup { name: "opt-out-b", bazel_module: { bp2build_available: false } } |
| 1267 | filegroup { name: "opt-out-c", bazel_module: { bp2build_available: false } } |
| 1268 | `, |
| 1269 | |
| 1270 | "package-opt-out/subpackage/Android.bp": ` |
| 1271 | filegroup { name: "opt-out-g", bazel_module: { bp2build_available: false } } |
| 1272 | filegroup { name: "opt-out-h", bazel_module: { bp2build_available: false } } |
| 1273 | `, |
| 1274 | }, |
| 1275 | }, |
MarkDacek | 9c094ca | 2023-03-16 19:15:19 +0000 | [diff] [blame] | 1276 | { |
| 1277 | description: "test force-enabled errors out", |
| 1278 | moduleTypeUnderTest: "filegroup", |
| 1279 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 1280 | expectedCount: map[string]int{ |
| 1281 | "migrated": 0, |
| 1282 | "not_migrated": 0, |
| 1283 | }, |
| 1284 | bp2buildConfig: allowlists.Bp2BuildConfig{ |
| 1285 | "migrated/but_not_really": allowlists.Bp2BuildDefaultFalse, |
| 1286 | "not_migrated": allowlists.Bp2BuildDefaultFalse, |
| 1287 | }, |
| 1288 | fs: map[string]string{ |
| 1289 | "migrated/Android.bp": `filegroup { name: "a" }`, |
| 1290 | }, |
| 1291 | forceEnabledModules: []string{"a"}, |
| 1292 | expectedErrorMessages: []string{"Force Enabled Module a not converted"}, |
| 1293 | }, |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1294 | } |
| 1295 | |
| 1296 | dir := "." |
| 1297 | for _, testCase := range testCases { |
| 1298 | fs := make(map[string][]byte) |
| 1299 | toParse := []string{ |
| 1300 | "Android.bp", |
| 1301 | } |
| 1302 | for f, content := range testCase.fs { |
| 1303 | if strings.HasSuffix(f, "Android.bp") { |
| 1304 | toParse = append(toParse, f) |
| 1305 | } |
| 1306 | fs[f] = []byte(content) |
| 1307 | } |
| 1308 | config := android.TestConfig(buildDir, nil, "", fs) |
MarkDacek | 9c094ca | 2023-03-16 19:15:19 +0000 | [diff] [blame] | 1309 | config.AddForceEnabledModules(testCase.forceEnabledModules) |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1310 | ctx := android.NewTestContext(config) |
| 1311 | ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory) |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 1312 | allowlist := android.NewBp2BuildAllowlist().SetDefaultConfig(testCase.bp2buildConfig) |
| 1313 | ctx.RegisterBp2BuildConfig(allowlist) |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1314 | ctx.RegisterForBazelConversion() |
| 1315 | |
| 1316 | _, errs := ctx.ParseFileList(dir, toParse) |
| 1317 | android.FailIfErrored(t, errs) |
| 1318 | _, errs = ctx.ResolveDependencies(config) |
| 1319 | android.FailIfErrored(t, errs) |
| 1320 | |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 1321 | codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "") |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1322 | |
| 1323 | // For each directory, test that the expected number of generated targets is correct. |
| 1324 | for dir, expectedCount := range testCase.expectedCount { |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 1325 | bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir) |
MarkDacek | 9c094ca | 2023-03-16 19:15:19 +0000 | [diff] [blame] | 1326 | android.CheckErrorsAgainstExpectations(t, err, testCase.expectedErrorMessages) |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1327 | if actualCount := len(bazelTargets); actualCount != expectedCount { |
| 1328 | t.Fatalf( |
| 1329 | "%s: Expected %d bazel target for %s package, got %d", |
| 1330 | testCase.description, |
| 1331 | expectedCount, |
| 1332 | dir, |
| 1333 | actualCount) |
| 1334 | } |
| 1335 | |
| 1336 | } |
| 1337 | } |
| 1338 | } |
| 1339 | |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1340 | func TestCombineBuildFilesBp2buildTargets(t *testing.T) { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1341 | testCases := []Bp2buildTestCase{ |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1342 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1343 | Description: "filegroup bazel_module.label", |
| 1344 | ModuleTypeUnderTest: "filegroup", |
| 1345 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1346 | Blueprint: `filegroup { |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1347 | name: "fg_foo", |
| 1348 | bazel_module: { label: "//other:fg_foo" }, |
| 1349 | }`, |
Cole Faust | ea602c5 | 2022-08-31 14:48:26 -0700 | [diff] [blame] | 1350 | ExpectedBazelTargets: []string{}, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1351 | Filesystem: map[string]string{ |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1352 | "other/BUILD.bazel": `// BUILD file`, |
| 1353 | }, |
| 1354 | }, |
| 1355 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1356 | Description: "multiple bazel_module.label same BUILD", |
| 1357 | ModuleTypeUnderTest: "filegroup", |
| 1358 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1359 | Blueprint: `filegroup { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1360 | name: "fg_foo", |
| 1361 | bazel_module: { label: "//other:fg_foo" }, |
| 1362 | } |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1363 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1364 | filegroup { |
| 1365 | name: "foo", |
| 1366 | bazel_module: { label: "//other:foo" }, |
| 1367 | }`, |
Cole Faust | ea602c5 | 2022-08-31 14:48:26 -0700 | [diff] [blame] | 1368 | ExpectedBazelTargets: []string{}, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1369 | Filesystem: map[string]string{ |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1370 | "other/BUILD.bazel": `// BUILD file`, |
| 1371 | }, |
| 1372 | }, |
| 1373 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1374 | Description: "filegroup bazel_module.label and bp2build in subdir", |
| 1375 | ModuleTypeUnderTest: "filegroup", |
| 1376 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1377 | Dir: "other", |
| 1378 | Blueprint: ``, |
| 1379 | Filesystem: map[string]string{ |
Jingwen Chen | c63677b | 2021-06-17 05:43:19 +0000 | [diff] [blame] | 1380 | "other/Android.bp": `filegroup { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1381 | name: "fg_foo", |
| 1382 | bazel_module: { |
| 1383 | bp2build_available: true, |
| 1384 | }, |
| 1385 | } |
| 1386 | filegroup { |
| 1387 | name: "fg_bar", |
| 1388 | bazel_module: { |
| 1389 | label: "//other:fg_bar" |
| 1390 | }, |
| 1391 | }`, |
Jingwen Chen | c63677b | 2021-06-17 05:43:19 +0000 | [diff] [blame] | 1392 | "other/BUILD.bazel": `// definition for fg_bar`, |
| 1393 | }, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1394 | ExpectedBazelTargets: []string{ |
| 1395 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{}), |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1396 | }, |
| 1397 | }, |
| 1398 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1399 | Description: "filegroup bazel_module.label and filegroup bp2build", |
| 1400 | ModuleTypeUnderTest: "filegroup", |
| 1401 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1402 | |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1403 | Filesystem: map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1404 | "other/BUILD.bazel": `// BUILD file`, |
| 1405 | }, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1406 | Blueprint: `filegroup { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1407 | name: "fg_foo", |
| 1408 | bazel_module: { |
| 1409 | label: "//other:fg_foo", |
| 1410 | }, |
| 1411 | } |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1412 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1413 | filegroup { |
| 1414 | name: "fg_bar", |
| 1415 | bazel_module: { |
| 1416 | bp2build_available: true, |
| 1417 | }, |
| 1418 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1419 | ExpectedBazelTargets: []string{ |
| 1420 | MakeBazelTargetNoRestrictions("filegroup", "fg_bar", map[string]string{}), |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1421 | }, |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1422 | }, |
| 1423 | } |
| 1424 | |
| 1425 | dir := "." |
| 1426 | for _, testCase := range testCases { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1427 | t.Run(testCase.Description, func(t *testing.T) { |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1428 | fs := make(map[string][]byte) |
| 1429 | toParse := []string{ |
| 1430 | "Android.bp", |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1431 | } |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1432 | for f, content := range testCase.Filesystem { |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1433 | if strings.HasSuffix(f, "Android.bp") { |
| 1434 | toParse = append(toParse, f) |
| 1435 | } |
| 1436 | fs[f] = []byte(content) |
| 1437 | } |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1438 | config := android.TestConfig(buildDir, nil, testCase.Blueprint, fs) |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1439 | ctx := android.NewTestContext(config) |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1440 | ctx.RegisterModuleType(testCase.ModuleTypeUnderTest, testCase.ModuleTypeUnderTestFactory) |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1441 | ctx.RegisterForBazelConversion() |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1442 | |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1443 | _, errs := ctx.ParseFileList(dir, toParse) |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 1444 | if errored(t, testCase, errs) { |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1445 | return |
| 1446 | } |
| 1447 | _, errs = ctx.ResolveDependencies(config) |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 1448 | if errored(t, testCase, errs) { |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1449 | return |
| 1450 | } |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1451 | |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1452 | checkDir := dir |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1453 | if testCase.Dir != "" { |
| 1454 | checkDir = testCase.Dir |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1455 | } |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 1456 | codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "") |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 1457 | bazelTargets, err := generateBazelTargetsForDir(codegenCtx, checkDir) |
| 1458 | android.FailIfErrored(t, err) |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1459 | bazelTargets.sort() |
| 1460 | actualCount := len(bazelTargets) |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1461 | expectedCount := len(testCase.ExpectedBazelTargets) |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1462 | if actualCount != expectedCount { |
| 1463 | t.Errorf("Expected %d bazel target, got %d\n%s", expectedCount, actualCount, bazelTargets) |
| 1464 | } |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1465 | for i, target := range bazelTargets { |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1466 | actualContent := target.content |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1467 | expectedContent := testCase.ExpectedBazelTargets[i] |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1468 | if expectedContent != actualContent { |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1469 | t.Errorf( |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1470 | "Expected generated Bazel target to be '%s', got '%s'", |
| 1471 | expectedContent, |
| 1472 | actualContent, |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1473 | ) |
| 1474 | } |
| 1475 | } |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1476 | }) |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1477 | } |
| 1478 | } |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1479 | |
Jingwen Chen | 0eeaeb8 | 2022-09-21 10:27:42 +0000 | [diff] [blame] | 1480 | func TestGlob(t *testing.T) { |
| 1481 | testCases := []Bp2buildTestCase{ |
| 1482 | { |
| 1483 | Description: "filegroup with glob", |
| 1484 | ModuleTypeUnderTest: "filegroup", |
| 1485 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1486 | Blueprint: `filegroup { |
| 1487 | name: "fg_foo", |
| 1488 | srcs: ["**/*.txt"], |
| 1489 | bazel_module: { bp2build_available: true }, |
| 1490 | }`, |
| 1491 | ExpectedBazelTargets: []string{ |
| 1492 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
| 1493 | "srcs": `[ |
| 1494 | "other/a.txt", |
| 1495 | "other/b.txt", |
| 1496 | "other/subdir/a.txt", |
| 1497 | ]`, |
| 1498 | }), |
| 1499 | }, |
| 1500 | Filesystem: map[string]string{ |
| 1501 | "other/a.txt": "", |
| 1502 | "other/b.txt": "", |
| 1503 | "other/subdir/a.txt": "", |
| 1504 | "other/file": "", |
| 1505 | }, |
| 1506 | }, |
| 1507 | { |
| 1508 | Description: "filegroup with glob in subdir", |
| 1509 | ModuleTypeUnderTest: "filegroup", |
| 1510 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1511 | Dir: "other", |
| 1512 | Filesystem: map[string]string{ |
| 1513 | "other/Android.bp": `filegroup { |
| 1514 | name: "fg_foo", |
| 1515 | srcs: ["**/*.txt"], |
| 1516 | bazel_module: { bp2build_available: true }, |
| 1517 | }`, |
| 1518 | "other/a.txt": "", |
| 1519 | "other/b.txt": "", |
| 1520 | "other/subdir/a.txt": "", |
| 1521 | "other/file": "", |
| 1522 | }, |
| 1523 | ExpectedBazelTargets: []string{ |
| 1524 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
| 1525 | "srcs": `[ |
| 1526 | "a.txt", |
| 1527 | "b.txt", |
| 1528 | "subdir/a.txt", |
| 1529 | ]`, |
| 1530 | }), |
| 1531 | }, |
| 1532 | }, |
| 1533 | { |
| 1534 | Description: "filegroup with glob with no kept BUILD files", |
| 1535 | ModuleTypeUnderTest: "filegroup", |
| 1536 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1537 | KeepBuildFileForDirs: []string{ |
| 1538 | // empty |
| 1539 | }, |
| 1540 | Blueprint: `filegroup { |
| 1541 | name: "fg_foo", |
| 1542 | srcs: ["**/*.txt"], |
| 1543 | bazel_module: { bp2build_available: true }, |
| 1544 | }`, |
| 1545 | Filesystem: map[string]string{ |
| 1546 | "a.txt": "", |
| 1547 | "b.txt": "", |
| 1548 | "foo/BUILD": "", |
| 1549 | "foo/a.txt": "", |
| 1550 | "foo/bar/BUILD": "", |
| 1551 | "foo/bar/b.txt": "", |
| 1552 | }, |
| 1553 | ExpectedBazelTargets: []string{ |
| 1554 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
| 1555 | "srcs": `[ |
| 1556 | "a.txt", |
| 1557 | "b.txt", |
| 1558 | "foo/a.txt", |
| 1559 | "foo/bar/b.txt", |
| 1560 | ]`, |
| 1561 | }), |
| 1562 | }, |
| 1563 | }, |
| 1564 | { |
| 1565 | Description: "filegroup with glob with kept BUILD file", |
| 1566 | ModuleTypeUnderTest: "filegroup", |
| 1567 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1568 | KeepBuildFileForDirs: []string{ |
| 1569 | "foo", |
| 1570 | }, |
| 1571 | Blueprint: `filegroup { |
| 1572 | name: "fg_foo", |
| 1573 | srcs: ["**/*.txt"], |
| 1574 | bazel_module: { bp2build_available: true }, |
| 1575 | }`, |
| 1576 | Filesystem: map[string]string{ |
| 1577 | "a.txt": "", |
| 1578 | "b.txt": "", |
| 1579 | "foo/BUILD": "", |
| 1580 | "foo/a.txt": "", |
| 1581 | "foo/bar/BUILD": "", |
| 1582 | "foo/bar/b.txt": "", |
| 1583 | }, |
| 1584 | ExpectedBazelTargets: []string{ |
| 1585 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
| 1586 | "srcs": `[ |
| 1587 | "a.txt", |
| 1588 | "b.txt", |
| 1589 | "//foo:a.txt", |
| 1590 | "//foo:bar/b.txt", |
| 1591 | ]`, |
| 1592 | }), |
| 1593 | }, |
| 1594 | }, |
| 1595 | { |
| 1596 | Description: "filegroup with glob with kept BUILD.bazel file", |
| 1597 | ModuleTypeUnderTest: "filegroup", |
| 1598 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1599 | KeepBuildFileForDirs: []string{ |
| 1600 | "foo", |
| 1601 | }, |
| 1602 | Blueprint: `filegroup { |
| 1603 | name: "fg_foo", |
| 1604 | srcs: ["**/*.txt"], |
| 1605 | bazel_module: { bp2build_available: true }, |
| 1606 | }`, |
| 1607 | Filesystem: map[string]string{ |
| 1608 | "a.txt": "", |
| 1609 | "b.txt": "", |
| 1610 | "foo/BUILD.bazel": "", |
| 1611 | "foo/a.txt": "", |
| 1612 | "foo/bar/BUILD.bazel": "", |
| 1613 | "foo/bar/b.txt": "", |
| 1614 | }, |
| 1615 | ExpectedBazelTargets: []string{ |
| 1616 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
| 1617 | "srcs": `[ |
| 1618 | "a.txt", |
| 1619 | "b.txt", |
| 1620 | "//foo:a.txt", |
| 1621 | "//foo:bar/b.txt", |
| 1622 | ]`, |
| 1623 | }), |
| 1624 | }, |
| 1625 | }, |
| 1626 | { |
| 1627 | Description: "filegroup with glob with Android.bp file as boundary", |
| 1628 | ModuleTypeUnderTest: "filegroup", |
| 1629 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1630 | Blueprint: `filegroup { |
| 1631 | name: "fg_foo", |
| 1632 | srcs: ["**/*.txt"], |
| 1633 | bazel_module: { bp2build_available: true }, |
| 1634 | }`, |
| 1635 | Filesystem: map[string]string{ |
| 1636 | "a.txt": "", |
| 1637 | "b.txt": "", |
| 1638 | "foo/Android.bp": "", |
| 1639 | "foo/a.txt": "", |
| 1640 | "foo/bar/Android.bp": "", |
| 1641 | "foo/bar/b.txt": "", |
| 1642 | }, |
| 1643 | ExpectedBazelTargets: []string{ |
| 1644 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
| 1645 | "srcs": `[ |
| 1646 | "a.txt", |
| 1647 | "b.txt", |
| 1648 | "//foo:a.txt", |
| 1649 | "//foo/bar:b.txt", |
| 1650 | ]`, |
| 1651 | }), |
| 1652 | }, |
| 1653 | }, |
| 1654 | { |
| 1655 | Description: "filegroup with glob in subdir with kept BUILD and BUILD.bazel file", |
| 1656 | ModuleTypeUnderTest: "filegroup", |
| 1657 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1658 | Dir: "other", |
| 1659 | KeepBuildFileForDirs: []string{ |
| 1660 | "other/foo", |
| 1661 | "other/foo/bar", |
| 1662 | // deliberately not other/foo/baz/BUILD. |
| 1663 | }, |
| 1664 | Filesystem: map[string]string{ |
| 1665 | "other/Android.bp": `filegroup { |
| 1666 | name: "fg_foo", |
| 1667 | srcs: ["**/*.txt"], |
| 1668 | bazel_module: { bp2build_available: true }, |
| 1669 | }`, |
| 1670 | "other/a.txt": "", |
| 1671 | "other/b.txt": "", |
| 1672 | "other/foo/BUILD": "", |
| 1673 | "other/foo/a.txt": "", |
| 1674 | "other/foo/bar/BUILD.bazel": "", |
| 1675 | "other/foo/bar/b.txt": "", |
| 1676 | "other/foo/baz/BUILD": "", |
| 1677 | "other/foo/baz/c.txt": "", |
| 1678 | }, |
| 1679 | ExpectedBazelTargets: []string{ |
| 1680 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
| 1681 | "srcs": `[ |
| 1682 | "a.txt", |
| 1683 | "b.txt", |
| 1684 | "//other/foo:a.txt", |
| 1685 | "//other/foo/bar:b.txt", |
| 1686 | "//other/foo:baz/c.txt", |
| 1687 | ]`, |
| 1688 | }), |
| 1689 | }, |
| 1690 | }, |
| 1691 | } |
| 1692 | |
| 1693 | for _, testCase := range testCases { |
| 1694 | t.Run(testCase.Description, func(t *testing.T) { |
| 1695 | RunBp2BuildTestCaseSimple(t, testCase) |
| 1696 | }) |
| 1697 | } |
| 1698 | } |
| 1699 | |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1700 | func TestGlobExcludeSrcs(t *testing.T) { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1701 | testCases := []Bp2buildTestCase{ |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1702 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1703 | Description: "filegroup top level exclude_srcs", |
| 1704 | ModuleTypeUnderTest: "filegroup", |
| 1705 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1706 | Blueprint: `filegroup { |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1707 | name: "fg_foo", |
| 1708 | srcs: ["**/*.txt"], |
| 1709 | exclude_srcs: ["c.txt"], |
| 1710 | bazel_module: { bp2build_available: true }, |
| 1711 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1712 | Filesystem: map[string]string{ |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1713 | "a.txt": "", |
| 1714 | "b.txt": "", |
| 1715 | "c.txt": "", |
| 1716 | "dir/Android.bp": "", |
| 1717 | "dir/e.txt": "", |
| 1718 | "dir/f.txt": "", |
| 1719 | }, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1720 | ExpectedBazelTargets: []string{ |
| 1721 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1722 | "srcs": `[ |
| 1723 | "a.txt", |
| 1724 | "b.txt", |
| 1725 | "//dir:e.txt", |
| 1726 | "//dir:f.txt", |
| 1727 | ]`, |
| 1728 | }), |
| 1729 | }, |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1730 | }, |
| 1731 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1732 | Description: "filegroup in subdir exclude_srcs", |
| 1733 | ModuleTypeUnderTest: "filegroup", |
| 1734 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1735 | Blueprint: "", |
| 1736 | Dir: "dir", |
| 1737 | Filesystem: map[string]string{ |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1738 | "dir/Android.bp": `filegroup { |
| 1739 | name: "fg_foo", |
| 1740 | srcs: ["**/*.txt"], |
| 1741 | exclude_srcs: ["b.txt"], |
| 1742 | bazel_module: { bp2build_available: true }, |
| 1743 | } |
| 1744 | `, |
| 1745 | "dir/a.txt": "", |
| 1746 | "dir/b.txt": "", |
| 1747 | "dir/subdir/Android.bp": "", |
| 1748 | "dir/subdir/e.txt": "", |
| 1749 | "dir/subdir/f.txt": "", |
| 1750 | }, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1751 | ExpectedBazelTargets: []string{ |
| 1752 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1753 | "srcs": `[ |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 1754 | "a.txt", |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1755 | "//dir/subdir:e.txt", |
| 1756 | "//dir/subdir:f.txt", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1757 | ]`, |
| 1758 | }), |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1759 | }, |
| 1760 | }, |
| 1761 | } |
| 1762 | |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1763 | for _, testCase := range testCases { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1764 | t.Run(testCase.Description, func(t *testing.T) { |
Trevor Radcliffe | 1b4b2d9 | 2022-09-01 18:57:01 +0000 | [diff] [blame] | 1765 | RunBp2BuildTestCaseSimple(t, testCase) |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1766 | }) |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1767 | } |
| 1768 | } |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1769 | |
| 1770 | func TestCommonBp2BuildModuleAttrs(t *testing.T) { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1771 | testCases := []Bp2buildTestCase{ |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1772 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1773 | Description: "Required into data test", |
| 1774 | ModuleTypeUnderTest: "filegroup", |
| 1775 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
Chris Parsons | cd20903 | 2023-09-19 01:12:48 +0000 | [diff] [blame] | 1776 | StubbedBuildDefinitions: []string{"reqd"}, |
| 1777 | 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] | 1778 | filegroup { |
| 1779 | name: "fg_foo", |
| 1780 | required: ["reqd"], |
| 1781 | bazel_module: { bp2build_available: true }, |
| 1782 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1783 | ExpectedBazelTargets: []string{ |
| 1784 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1785 | "data": `[":reqd"]`, |
| 1786 | }), |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1787 | }, |
| 1788 | }, |
| 1789 | { |
Jingwen Chen | a5ecb37 | 2022-09-21 09:05:37 +0000 | [diff] [blame] | 1790 | Description: "Required into data test, cyclic self reference is filtered out", |
| 1791 | ModuleTypeUnderTest: "filegroup", |
| 1792 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
Chris Parsons | cd20903 | 2023-09-19 01:12:48 +0000 | [diff] [blame] | 1793 | StubbedBuildDefinitions: []string{"reqd"}, |
| 1794 | Blueprint: simpleModule("filegroup", "reqd") + ` |
Jingwen Chen | a5ecb37 | 2022-09-21 09:05:37 +0000 | [diff] [blame] | 1795 | filegroup { |
| 1796 | name: "fg_foo", |
| 1797 | required: ["reqd", "fg_foo"], |
| 1798 | bazel_module: { bp2build_available: true }, |
| 1799 | }`, |
| 1800 | ExpectedBazelTargets: []string{ |
| 1801 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
| 1802 | "data": `[":reqd"]`, |
| 1803 | }), |
| 1804 | }, |
| 1805 | }, |
| 1806 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1807 | Description: "Required via arch into data test", |
| 1808 | ModuleTypeUnderTest: "python_library", |
| 1809 | ModuleTypeUnderTestFactory: python.PythonLibraryFactory, |
Chris Parsons | cd20903 | 2023-09-19 01:12:48 +0000 | [diff] [blame] | 1810 | StubbedBuildDefinitions: []string{"reqdx86", "reqdarm"}, |
| 1811 | Blueprint: simpleModule("python_library", "reqdx86") + |
| 1812 | 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] | 1813 | python_library { |
| 1814 | name: "fg_foo", |
| 1815 | arch: { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1816 | arm: { |
| 1817 | required: ["reqdarm"], |
| 1818 | }, |
| 1819 | x86: { |
| 1820 | required: ["reqdx86"], |
| 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 | bazel_module: { bp2build_available: true }, |
| 1824 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1825 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 1826 | MakeBazelTarget("py_library", "fg_foo", map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1827 | "data": `select({ |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1828 | "//build/bazel/platforms/arch:arm": [":reqdarm"], |
| 1829 | "//build/bazel/platforms/arch:x86": [":reqdx86"], |
| 1830 | "//conditions:default": [], |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1831 | })`, |
| 1832 | "srcs_version": `"PY3"`, |
Cole Faust | b09da7e | 2022-05-18 10:57:33 -0700 | [diff] [blame] | 1833 | "imports": `["."]`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1834 | }), |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1835 | }, |
| 1836 | }, |
| 1837 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1838 | Description: "Required appended to data test", |
| 1839 | ModuleTypeUnderTest: "python_library", |
| 1840 | ModuleTypeUnderTestFactory: python.PythonLibraryFactory, |
| 1841 | Filesystem: map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1842 | "data.bin": "", |
| 1843 | "src.py": "", |
| 1844 | }, |
Chris Parsons | cd20903 | 2023-09-19 01:12:48 +0000 | [diff] [blame] | 1845 | StubbedBuildDefinitions: []string{"reqd"}, |
| 1846 | 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] | 1847 | python_library { |
| 1848 | name: "fg_foo", |
| 1849 | data: ["data.bin"], |
| 1850 | required: ["reqd"], |
| 1851 | bazel_module: { bp2build_available: true }, |
| 1852 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1853 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 1854 | MakeBazelTarget("py_library", "fg_foo", map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1855 | "data": `[ |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1856 | "data.bin", |
| 1857 | ":reqd", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1858 | ]`, |
| 1859 | "srcs_version": `"PY3"`, |
Cole Faust | b09da7e | 2022-05-18 10:57:33 -0700 | [diff] [blame] | 1860 | "imports": `["."]`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1861 | }), |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1862 | }, |
| 1863 | }, |
| 1864 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1865 | Description: "All props-to-attrs at once together test", |
| 1866 | ModuleTypeUnderTest: "filegroup", |
| 1867 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
Chris Parsons | cd20903 | 2023-09-19 01:12:48 +0000 | [diff] [blame] | 1868 | StubbedBuildDefinitions: []string{"reqd"}, |
| 1869 | 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] | 1870 | filegroup { |
| 1871 | name: "fg_foo", |
| 1872 | required: ["reqd"], |
| 1873 | bazel_module: { bp2build_available: true }, |
| 1874 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1875 | ExpectedBazelTargets: []string{ |
| 1876 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1877 | "data": `[":reqd"]`, |
| 1878 | }), |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1879 | }, |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1880 | }, |
| 1881 | } |
| 1882 | |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1883 | for _, tc := range testCases { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1884 | t.Run(tc.Description, func(t *testing.T) { |
Trevor Radcliffe | 1b4b2d9 | 2022-09-01 18:57:01 +0000 | [diff] [blame] | 1885 | RunBp2BuildTestCaseSimple(t, tc) |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1886 | }) |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1887 | } |
| 1888 | } |
Sasha Smundak | 05b0ba6 | 2022-09-26 18:15:45 -0700 | [diff] [blame] | 1889 | |
| 1890 | func TestLicensesAttrConversion(t *testing.T) { |
| 1891 | RunBp2BuildTestCase(t, |
| 1892 | func(ctx android.RegistrationContext) { |
| 1893 | ctx.RegisterModuleType("license", android.LicenseFactory) |
| 1894 | }, |
| 1895 | Bp2buildTestCase{ |
| 1896 | Description: "Test that licenses: attribute is converted", |
| 1897 | ModuleTypeUnderTest: "filegroup", |
| 1898 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1899 | Blueprint: ` |
| 1900 | license { |
| 1901 | name: "my_license", |
| 1902 | } |
| 1903 | filegroup { |
| 1904 | name: "my_filegroup", |
| 1905 | licenses: ["my_license"], |
| 1906 | } |
| 1907 | `, |
| 1908 | ExpectedBazelTargets: []string{ |
| 1909 | MakeBazelTargetNoRestrictions("filegroup", "my_filegroup", AttrNameToString{ |
| 1910 | "applicable_licenses": `[":my_license"]`, |
| 1911 | }), |
| 1912 | MakeBazelTargetNoRestrictions("android_license", "my_license", AttrNameToString{}), |
| 1913 | }, |
| 1914 | }) |
| 1915 | } |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 1916 | |
Spandan Das | 6a448ec | 2023-04-19 17:36:12 +0000 | [diff] [blame] | 1917 | func TestGenerateConfigSetting(t *testing.T) { |
| 1918 | bp := ` |
| 1919 | custom { |
| 1920 | name: "foo", |
| 1921 | test_config_setting: true, |
| 1922 | } |
| 1923 | ` |
| 1924 | expectedBazelTargets := []string{ |
| 1925 | MakeBazelTargetNoRestrictions( |
| 1926 | "config_setting", |
| 1927 | "foo_config_setting", |
| 1928 | AttrNameToString{ |
| 1929 | "flag_values": `{ |
| 1930 | "//build/bazel/rules/my_string_setting": "foo", |
| 1931 | }`, |
| 1932 | }, |
| 1933 | ), |
| 1934 | MakeBazelTarget( |
| 1935 | "custom", |
| 1936 | "foo", |
| 1937 | AttrNameToString{}, |
| 1938 | ), |
| 1939 | } |
| 1940 | registerCustomModule := func(ctx android.RegistrationContext) { |
| 1941 | ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice) |
| 1942 | } |
| 1943 | RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{ |
| 1944 | Blueprint: bp, |
| 1945 | ExpectedBazelTargets: expectedBazelTargets, |
| 1946 | Description: "Generating API contribution Bazel targets for custom module", |
| 1947 | }) |
| 1948 | } |
Spandan Das | 921af32 | 2023-04-26 02:56:37 +0000 | [diff] [blame] | 1949 | |
| 1950 | // If values of all keys in an axis are equal to //conditions:default, drop the axis and print the common value |
| 1951 | func TestPrettyPrintSelectMapEqualValues(t *testing.T) { |
| 1952 | lla := bazel.LabelListAttribute{ |
| 1953 | Value: bazel.LabelList{}, |
| 1954 | } |
| 1955 | libFooImplLabel := bazel.Label{ |
| 1956 | Label: ":libfoo.impl", |
| 1957 | } |
Spandan Das | 6d4d9da | 2023-04-18 06:20:40 +0000 | [diff] [blame] | 1958 | lla.SetSelectValue(bazel.OsAndInApexAxis, bazel.AndroidPlatform, bazel.MakeLabelList([]bazel.Label{libFooImplLabel})) |
Spandan Das | 921af32 | 2023-04-26 02:56:37 +0000 | [diff] [blame] | 1959 | lla.SetSelectValue(bazel.OsAndInApexAxis, bazel.ConditionsDefaultConfigKey, bazel.MakeLabelList([]bazel.Label{libFooImplLabel})) |
| 1960 | actual, _ := prettyPrintAttribute(lla, 0) |
| 1961 | android.AssertStringEquals(t, "Print the common value if all keys in an axis have the same value", `[":libfoo.impl"]`, actual) |
| 1962 | } |
Spandan Das | 3131d67 | 2023-08-03 22:33:47 +0000 | [diff] [blame] | 1963 | |
Chris Parsons | 5011e61 | 2023-09-13 23:33:20 +0000 | [diff] [blame] | 1964 | func TestAlreadyPresentBuildTarget(t *testing.T) { |
| 1965 | bp := ` |
| 1966 | custom { |
| 1967 | name: "foo", |
| 1968 | } |
| 1969 | custom { |
| 1970 | name: "bar", |
| 1971 | } |
| 1972 | ` |
| 1973 | alreadyPresentBuildFile := |
| 1974 | MakeBazelTarget( |
| 1975 | "custom", |
| 1976 | "foo", |
| 1977 | AttrNameToString{}, |
| 1978 | ) |
| 1979 | expectedBazelTargets := []string{ |
| 1980 | MakeBazelTarget( |
| 1981 | "custom", |
| 1982 | "bar", |
| 1983 | AttrNameToString{}, |
| 1984 | ), |
| 1985 | } |
| 1986 | registerCustomModule := func(ctx android.RegistrationContext) { |
| 1987 | ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice) |
| 1988 | } |
| 1989 | RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{ |
| 1990 | AlreadyExistingBuildContents: alreadyPresentBuildFile, |
| 1991 | Blueprint: bp, |
| 1992 | ExpectedBazelTargets: expectedBazelTargets, |
| 1993 | Description: "Not duplicating work for an already-present BUILD target.", |
| 1994 | }) |
| 1995 | } |
| 1996 | |
Chris Parsons | 0c4de1f | 2023-09-21 20:36:35 +0000 | [diff] [blame] | 1997 | func TestAlreadyPresentOneToManyBuildTarget(t *testing.T) { |
| 1998 | bp := ` |
| 1999 | custom { |
| 2000 | name: "foo", |
| 2001 | one_to_many_prop: true, |
| 2002 | } |
| 2003 | custom { |
| 2004 | name: "bar", |
| 2005 | } |
| 2006 | ` |
| 2007 | alreadyPresentBuildFile := |
| 2008 | MakeBazelTarget( |
| 2009 | "custom", |
| 2010 | // one_to_many_prop ensures that foo generates "foo_proto_library_deps". |
| 2011 | "foo_proto_library_deps", |
| 2012 | AttrNameToString{}, |
| 2013 | ) |
| 2014 | expectedBazelTargets := []string{ |
| 2015 | MakeBazelTarget( |
| 2016 | "custom", |
| 2017 | "bar", |
| 2018 | AttrNameToString{}, |
| 2019 | ), |
| 2020 | } |
| 2021 | registerCustomModule := func(ctx android.RegistrationContext) { |
| 2022 | ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice) |
| 2023 | } |
| 2024 | RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{ |
| 2025 | AlreadyExistingBuildContents: alreadyPresentBuildFile, |
| 2026 | Blueprint: bp, |
| 2027 | ExpectedBazelTargets: expectedBazelTargets, |
| 2028 | Description: "Not duplicating work for an already-present BUILD target (different generated name)", |
| 2029 | }) |
| 2030 | } |
| 2031 | |
Chris Parsons | 5011e61 | 2023-09-13 23:33:20 +0000 | [diff] [blame] | 2032 | // Verifies that if a module is defined in pkg1/Android.bp, that a target present |
| 2033 | // in pkg2/BUILD.bazel does not result in the module being labeled "already defined |
| 2034 | // in a BUILD file". |
| 2035 | func TestBuildTargetPresentOtherDirectory(t *testing.T) { |
| 2036 | bp := ` |
| 2037 | custom { |
| 2038 | name: "foo", |
| 2039 | } |
| 2040 | ` |
| 2041 | expectedBazelTargets := []string{ |
| 2042 | MakeBazelTarget( |
| 2043 | "custom", |
| 2044 | "foo", |
| 2045 | AttrNameToString{}, |
| 2046 | ), |
| 2047 | } |
| 2048 | registerCustomModule := func(ctx android.RegistrationContext) { |
| 2049 | ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice) |
| 2050 | } |
| 2051 | RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{ |
| 2052 | KeepBuildFileForDirs: []string{"other_pkg"}, |
| 2053 | Filesystem: map[string]string{ |
| 2054 | "other_pkg/BUILD.bazel": MakeBazelTarget("custom", "foo", AttrNameToString{}), |
| 2055 | }, |
| 2056 | Blueprint: bp, |
| 2057 | ExpectedBazelTargets: expectedBazelTargets, |
| 2058 | Description: "Not treating a BUILD target as the bazel definition for a module in another package", |
| 2059 | }) |
| 2060 | } |
| 2061 | |
Spandan Das | 3131d67 | 2023-08-03 22:33:47 +0000 | [diff] [blame] | 2062 | // If CommonAttributes.Dir is set, the bazel target should be created in that dir |
| 2063 | func TestCreateBazelTargetInDifferentDir(t *testing.T) { |
| 2064 | t.Parallel() |
| 2065 | bp := ` |
| 2066 | custom { |
| 2067 | name: "foo", |
| 2068 | dir: "subdir", |
| 2069 | } |
| 2070 | ` |
| 2071 | registerCustomModule := func(ctx android.RegistrationContext) { |
| 2072 | ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice) |
| 2073 | } |
| 2074 | // Check that foo is not created in root dir |
| 2075 | RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{ |
| 2076 | Description: "foo is not created in root dir because it sets dir explicitly", |
| 2077 | Blueprint: bp, |
| 2078 | Filesystem: map[string]string{ |
| 2079 | "subdir/Android.bp": "", |
| 2080 | }, |
| 2081 | ExpectedBazelTargets: []string{}, |
| 2082 | }) |
| 2083 | // Check that foo is created in `subdir` |
| 2084 | RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{ |
| 2085 | Description: "foo is created in `subdir` because it sets dir explicitly", |
| 2086 | Blueprint: bp, |
| 2087 | Filesystem: map[string]string{ |
| 2088 | "subdir/Android.bp": "", |
| 2089 | }, |
| 2090 | Dir: "subdir", |
| 2091 | ExpectedBazelTargets: []string{ |
| 2092 | MakeBazelTarget("custom", "foo", AttrNameToString{}), |
| 2093 | }, |
| 2094 | }) |
| 2095 | // Check that we cannot create target in different dir if it is does not an Android.bp |
| 2096 | RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{ |
| 2097 | Description: "foo cannot be created in `subdir` because it does not contain an Android.bp file", |
| 2098 | Blueprint: bp, |
| 2099 | Dir: "subdir", |
| 2100 | ExpectedErr: fmt.Errorf("Cannot use ca.Dir to create a BazelTarget in dir: subdir since it does not contain an Android.bp file"), |
| 2101 | }) |
| 2102 | |
| 2103 | } |