| Rupert Shuttleworth | 378fc1b | 2021-07-28 08:03:16 -0400 | [diff] [blame] | 1 | // Copyright 2021 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 ( | 
| Alix | bbfd538 | 2022-06-09 18:52:05 +0000 | [diff] [blame] | 18 | "fmt" | 
|  | 19 | "testing" | 
|  | 20 |  | 
| Rupert Shuttleworth | 378fc1b | 2021-07-28 08:03:16 -0400 | [diff] [blame] | 21 | "android/soong/android" | 
|  | 22 | "android/soong/etc" | 
| Rupert Shuttleworth | 378fc1b | 2021-07-28 08:03:16 -0400 | [diff] [blame] | 23 | ) | 
|  | 24 |  | 
| Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 25 | func runPrebuiltEtcTestCase(t *testing.T, tc Bp2buildTestCase) { | 
| Rupert Shuttleworth | 378fc1b | 2021-07-28 08:03:16 -0400 | [diff] [blame] | 26 | t.Helper() | 
| Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 27 | (&tc).ModuleTypeUnderTest = "prebuilt_etc" | 
|  | 28 | (&tc).ModuleTypeUnderTestFactory = etc.PrebuiltEtcFactory | 
|  | 29 | RunBp2BuildTestCase(t, registerPrebuiltEtcModuleTypes, tc) | 
| Rupert Shuttleworth | 378fc1b | 2021-07-28 08:03:16 -0400 | [diff] [blame] | 30 | } | 
|  | 31 |  | 
|  | 32 | func registerPrebuiltEtcModuleTypes(ctx android.RegistrationContext) { | 
|  | 33 | } | 
|  | 34 |  | 
|  | 35 | func TestPrebuiltEtcSimple(t *testing.T) { | 
| Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 36 | runPrebuiltEtcTestCase(t, Bp2buildTestCase{ | 
|  | 37 | Description: "prebuilt_etc - simple example", | 
|  | 38 | Filesystem:  map[string]string{}, | 
|  | 39 | Blueprint: ` | 
| Rupert Shuttleworth | 378fc1b | 2021-07-28 08:03:16 -0400 | [diff] [blame] | 40 | prebuilt_etc { | 
|  | 41 | name: "apex_tz_version", | 
|  | 42 | src: "version/tz_version", | 
|  | 43 | filename: "tz_version", | 
|  | 44 | sub_dir: "tz", | 
|  | 45 | installable: false, | 
|  | 46 | } | 
|  | 47 | `, | 
| Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 48 | ExpectedBazelTargets: []string{ | 
| Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 49 | MakeBazelTarget("prebuilt_file", "apex_tz_version", AttrNameToString{ | 
| Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 50 | "filename":    `"tz_version"`, | 
|  | 51 | "installable": `False`, | 
|  | 52 | "src":         `"version/tz_version"`, | 
| Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | c78604e | 2022-02-28 18:22:59 -0500 | [diff] [blame] | 53 | "dir":         `"etc/tz"`, | 
| Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 54 | })}}) | 
| Rupert Shuttleworth | 378fc1b | 2021-07-28 08:03:16 -0400 | [diff] [blame] | 55 | } | 
| Liz Kammer | dff00ea | 2021-10-04 13:44:34 -0400 | [diff] [blame] | 56 |  | 
|  | 57 | func TestPrebuiltEtcArchVariant(t *testing.T) { | 
| Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 58 | runPrebuiltEtcTestCase(t, Bp2buildTestCase{ | 
|  | 59 | Description: "prebuilt_etc - arch variant", | 
|  | 60 | Filesystem:  map[string]string{}, | 
|  | 61 | Blueprint: ` | 
| Liz Kammer | dff00ea | 2021-10-04 13:44:34 -0400 | [diff] [blame] | 62 | prebuilt_etc { | 
|  | 63 | name: "apex_tz_version", | 
|  | 64 | src: "version/tz_version", | 
|  | 65 | filename: "tz_version", | 
|  | 66 | sub_dir: "tz", | 
|  | 67 | installable: false, | 
|  | 68 | arch: { | 
|  | 69 | arm: { | 
|  | 70 | src: "arm", | 
|  | 71 | }, | 
|  | 72 | arm64: { | 
|  | 73 | src: "arm64", | 
|  | 74 | }, | 
|  | 75 | } | 
|  | 76 | } | 
|  | 77 | `, | 
| Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 78 | ExpectedBazelTargets: []string{ | 
| Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 79 | MakeBazelTarget("prebuilt_file", "apex_tz_version", AttrNameToString{ | 
| Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 80 | "filename":    `"tz_version"`, | 
|  | 81 | "installable": `False`, | 
|  | 82 | "src": `select({ | 
| Liz Kammer | dff00ea | 2021-10-04 13:44:34 -0400 | [diff] [blame] | 83 | "//build/bazel/platforms/arch:arm": "arm", | 
|  | 84 | "//build/bazel/platforms/arch:arm64": "arm64", | 
|  | 85 | "//conditions:default": "version/tz_version", | 
| Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 86 | })`, | 
| Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | c78604e | 2022-02-28 18:22:59 -0500 | [diff] [blame] | 87 | "dir": `"etc/tz"`, | 
| Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 88 | })}}) | 
| Liz Kammer | dff00ea | 2021-10-04 13:44:34 -0400 | [diff] [blame] | 89 | } | 
| Chris Parsons | 58852a0 | 2021-12-09 18:10:18 -0500 | [diff] [blame] | 90 |  | 
|  | 91 | func TestPrebuiltEtcArchAndTargetVariant(t *testing.T) { | 
| Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 92 | runPrebuiltEtcTestCase(t, Bp2buildTestCase{ | 
|  | 93 | Description: "prebuilt_etc - arch variant", | 
|  | 94 | Filesystem:  map[string]string{}, | 
|  | 95 | Blueprint: ` | 
| Chris Parsons | 58852a0 | 2021-12-09 18:10:18 -0500 | [diff] [blame] | 96 | prebuilt_etc { | 
|  | 97 | name: "apex_tz_version", | 
|  | 98 | src: "version/tz_version", | 
|  | 99 | filename: "tz_version", | 
|  | 100 | sub_dir: "tz", | 
|  | 101 | installable: false, | 
|  | 102 | arch: { | 
|  | 103 | arm: { | 
|  | 104 | src: "arm", | 
|  | 105 | }, | 
|  | 106 | arm64: { | 
|  | 107 | src: "darwin_or_arm64", | 
|  | 108 | }, | 
|  | 109 | }, | 
|  | 110 | target: { | 
|  | 111 | darwin: { | 
|  | 112 | src: "darwin_or_arm64", | 
|  | 113 | } | 
|  | 114 | }, | 
|  | 115 | } | 
|  | 116 | `, | 
| Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 117 | ExpectedBazelTargets: []string{ | 
| Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 118 | MakeBazelTarget("prebuilt_file", "apex_tz_version", AttrNameToString{ | 
| Chris Parsons | 58852a0 | 2021-12-09 18:10:18 -0500 | [diff] [blame] | 119 | "filename":    `"tz_version"`, | 
|  | 120 | "installable": `False`, | 
|  | 121 | "src": `select({ | 
|  | 122 | "//build/bazel/platforms/os_arch:android_arm": "arm", | 
|  | 123 | "//build/bazel/platforms/os_arch:android_arm64": "darwin_or_arm64", | 
|  | 124 | "//build/bazel/platforms/os_arch:darwin_arm64": "darwin_or_arm64", | 
|  | 125 | "//build/bazel/platforms/os_arch:darwin_x86_64": "darwin_or_arm64", | 
|  | 126 | "//build/bazel/platforms/os_arch:linux_bionic_arm64": "darwin_or_arm64", | 
|  | 127 | "//conditions:default": "version/tz_version", | 
|  | 128 | })`, | 
| Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | c78604e | 2022-02-28 18:22:59 -0500 | [diff] [blame] | 129 | "dir": `"etc/tz"`, | 
|  | 130 | })}}) | 
|  | 131 | } | 
| Alix | bbfd538 | 2022-06-09 18:52:05 +0000 | [diff] [blame] | 132 | func TestPrebuiltEtcProductVariables(t *testing.T) { | 
|  | 133 | runPrebuiltEtcTestCase(t, Bp2buildTestCase{ | 
|  | 134 | Description: "prebuilt etc - product variables", | 
|  | 135 | Filesystem:  map[string]string{}, | 
|  | 136 | Blueprint: ` | 
|  | 137 | prebuilt_etc { | 
|  | 138 | name: "apex_tz_version", | 
|  | 139 | src: "version/tz_version", | 
|  | 140 | filename: "tz_version", | 
|  | 141 | product_variables: { | 
|  | 142 | native_coverage: { | 
|  | 143 | src: "src1", | 
|  | 144 | }, | 
|  | 145 | }, | 
|  | 146 | } | 
|  | 147 | `, | 
|  | 148 | ExpectedBazelTargets: []string{ | 
|  | 149 | MakeBazelTarget("prebuilt_file", "apex_tz_version", AttrNameToString{ | 
|  | 150 | "filename": `"tz_version"`, | 
|  | 151 | "src": `select({ | 
| Cole Faust | 87c0c33 | 2023-07-31 12:10:12 -0700 | [diff] [blame] | 152 | "//build/bazel/product_config/config_settings:native_coverage": "src1", | 
| Alix | bbfd538 | 2022-06-09 18:52:05 +0000 | [diff] [blame] | 153 | "//conditions:default": "version/tz_version", | 
|  | 154 | })`, | 
|  | 155 | "dir": `"etc"`, | 
|  | 156 | })}}) | 
|  | 157 | } | 
| Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | c78604e | 2022-02-28 18:22:59 -0500 | [diff] [blame] | 158 |  | 
| Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 159 | func runPrebuiltUsrShareTestCase(t *testing.T, tc Bp2buildTestCase) { | 
| Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | c78604e | 2022-02-28 18:22:59 -0500 | [diff] [blame] | 160 | t.Helper() | 
| Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 161 | (&tc).ModuleTypeUnderTest = "prebuilt_usr_share" | 
|  | 162 | (&tc).ModuleTypeUnderTestFactory = etc.PrebuiltUserShareFactory | 
|  | 163 | RunBp2BuildTestCase(t, registerPrebuiltEtcModuleTypes, tc) | 
| Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | c78604e | 2022-02-28 18:22:59 -0500 | [diff] [blame] | 164 | } | 
|  | 165 |  | 
|  | 166 | func registerPrebuiltUsrShareModuleTypes(ctx android.RegistrationContext) { | 
|  | 167 | } | 
|  | 168 |  | 
|  | 169 | func TestPrebuiltUsrShareSimple(t *testing.T) { | 
| Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 170 | runPrebuiltUsrShareTestCase(t, Bp2buildTestCase{ | 
|  | 171 | Description: "prebuilt_usr_share - simple example", | 
|  | 172 | Filesystem:  map[string]string{}, | 
|  | 173 | Blueprint: ` | 
| Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | c78604e | 2022-02-28 18:22:59 -0500 | [diff] [blame] | 174 | prebuilt_usr_share { | 
|  | 175 | name: "apex_tz_version", | 
|  | 176 | src: "version/tz_version", | 
|  | 177 | filename: "tz_version", | 
|  | 178 | sub_dir: "tz", | 
|  | 179 | installable: false, | 
|  | 180 | } | 
|  | 181 | `, | 
| Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 182 | ExpectedBazelTargets: []string{ | 
| Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 183 | MakeBazelTarget("prebuilt_file", "apex_tz_version", AttrNameToString{ | 
| Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | c78604e | 2022-02-28 18:22:59 -0500 | [diff] [blame] | 184 | "filename":    `"tz_version"`, | 
|  | 185 | "installable": `False`, | 
|  | 186 | "src":         `"version/tz_version"`, | 
|  | 187 | "dir":         `"usr/share/tz"`, | 
|  | 188 | })}}) | 
|  | 189 | } | 
|  | 190 |  | 
|  | 191 | func TestPrebuiltEtcNoSubdir(t *testing.T) { | 
| Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 192 | runPrebuiltEtcTestCase(t, Bp2buildTestCase{ | 
|  | 193 | Description: "prebuilt_etc - no subdir", | 
|  | 194 | Filesystem:  map[string]string{}, | 
|  | 195 | Blueprint: ` | 
| Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | c78604e | 2022-02-28 18:22:59 -0500 | [diff] [blame] | 196 | prebuilt_etc { | 
|  | 197 | name: "apex_tz_version", | 
|  | 198 | src: "version/tz_version", | 
|  | 199 | filename: "tz_version", | 
|  | 200 | installable: false, | 
|  | 201 | } | 
|  | 202 | `, | 
| Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 203 | ExpectedBazelTargets: []string{ | 
| Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 204 | MakeBazelTarget("prebuilt_file", "apex_tz_version", AttrNameToString{ | 
| Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | c78604e | 2022-02-28 18:22:59 -0500 | [diff] [blame] | 205 | "filename":    `"tz_version"`, | 
|  | 206 | "installable": `False`, | 
|  | 207 | "src":         `"version/tz_version"`, | 
|  | 208 | "dir":         `"etc"`, | 
| Chris Parsons | 58852a0 | 2021-12-09 18:10:18 -0500 | [diff] [blame] | 209 | })}}) | 
|  | 210 | } | 
| Alix | 993872a | 2022-06-15 17:42:14 +0000 | [diff] [blame] | 211 |  | 
|  | 212 | func TestFilenameAsProperty(t *testing.T) { | 
|  | 213 | runPrebuiltEtcTestCase(t, Bp2buildTestCase{ | 
|  | 214 | Description: "prebuilt_etc - filename is specified as a property ", | 
|  | 215 | Filesystem:  map[string]string{}, | 
|  | 216 | Blueprint: ` | 
|  | 217 | prebuilt_etc { | 
|  | 218 | name: "foo", | 
|  | 219 | src: "fooSrc", | 
|  | 220 | filename: "fooFileName", | 
|  | 221 | } | 
|  | 222 | `, | 
|  | 223 | ExpectedBazelTargets: []string{ | 
|  | 224 | MakeBazelTarget("prebuilt_file", "foo", AttrNameToString{ | 
|  | 225 | "filename": `"fooFileName"`, | 
|  | 226 | "src":      `"fooSrc"`, | 
|  | 227 | "dir":      `"etc"`, | 
|  | 228 | })}}) | 
|  | 229 | } | 
|  | 230 |  | 
|  | 231 | func TestFileNameFromSrc(t *testing.T) { | 
|  | 232 | runPrebuiltEtcTestCase(t, Bp2buildTestCase{ | 
|  | 233 | Description: "prebuilt_etc - filename_from_src is true  ", | 
|  | 234 | Filesystem:  map[string]string{}, | 
|  | 235 | Blueprint: ` | 
|  | 236 | prebuilt_etc { | 
|  | 237 | name: "foo", | 
|  | 238 | filename_from_src: true, | 
|  | 239 | src: "fooSrc", | 
|  | 240 | } | 
|  | 241 | `, | 
|  | 242 | ExpectedBazelTargets: []string{ | 
|  | 243 | MakeBazelTarget("prebuilt_file", "foo", AttrNameToString{ | 
|  | 244 | "filename": `"fooSrc"`, | 
|  | 245 | "src":      `"fooSrc"`, | 
|  | 246 | "dir":      `"etc"`, | 
|  | 247 | })}}) | 
|  | 248 | } | 
|  | 249 |  | 
|  | 250 | func TestFileNameFromSrcMultipleSrcs(t *testing.T) { | 
|  | 251 | runPrebuiltEtcTestCase(t, Bp2buildTestCase{ | 
|  | 252 | Description: "prebuilt_etc - filename_from_src is true but there are multiple srcs", | 
|  | 253 | Filesystem:  map[string]string{}, | 
|  | 254 | Blueprint: ` | 
|  | 255 | prebuilt_etc { | 
|  | 256 | name: "foo", | 
|  | 257 | filename_from_src: true, | 
|  | 258 | arch: { | 
|  | 259 | arm: { | 
|  | 260 | src: "barSrc", | 
|  | 261 | }, | 
|  | 262 | arm64: { | 
|  | 263 | src: "bazSrc", | 
|  | 264 | }, | 
|  | 265 | } | 
|  | 266 | } | 
|  | 267 | `, | 
|  | 268 | ExpectedBazelTargets: []string{ | 
|  | 269 | MakeBazelTarget("prebuilt_file", "foo", AttrNameToString{ | 
|  | 270 | "filename_from_src": `True`, | 
|  | 271 | "dir":               `"etc"`, | 
|  | 272 | "src": `select({ | 
|  | 273 | "//build/bazel/platforms/arch:arm": "barSrc", | 
|  | 274 | "//build/bazel/platforms/arch:arm64": "bazSrc", | 
|  | 275 | "//conditions:default": None, | 
|  | 276 | })`, | 
|  | 277 | })}}) | 
|  | 278 | } | 
|  | 279 |  | 
|  | 280 | func TestFilenameFromModuleName(t *testing.T) { | 
|  | 281 | runPrebuiltEtcTestCase(t, Bp2buildTestCase{ | 
|  | 282 | Description: "prebuilt_etc - neither filename nor filename_from_src are specified ", | 
|  | 283 | Filesystem:  map[string]string{}, | 
|  | 284 | Blueprint: ` | 
|  | 285 | prebuilt_etc { | 
|  | 286 | name: "foo", | 
|  | 287 | } | 
|  | 288 | `, | 
|  | 289 | ExpectedBazelTargets: []string{ | 
|  | 290 | MakeBazelTarget("prebuilt_file", "foo", AttrNameToString{ | 
|  | 291 | "filename": `"foo"`, | 
|  | 292 | "dir":      `"etc"`, | 
|  | 293 | })}}) | 
|  | 294 | } | 
| Alix | bbfd538 | 2022-06-09 18:52:05 +0000 | [diff] [blame] | 295 |  | 
|  | 296 | func TestPrebuiltEtcProductVariableArchSrcs(t *testing.T) { | 
|  | 297 | runPrebuiltEtcTestCase(t, Bp2buildTestCase{ | 
|  | 298 | Description: "prebuilt etc- SRcs from arch variant product variables", | 
|  | 299 | Filesystem:  map[string]string{}, | 
|  | 300 | Blueprint: ` | 
|  | 301 | prebuilt_etc { | 
|  | 302 | name: "foo", | 
|  | 303 | filename: "fooFilename", | 
|  | 304 | arch: { | 
|  | 305 | arm: { | 
|  | 306 | src: "armSrc", | 
|  | 307 | product_variables: { | 
|  | 308 | native_coverage: { | 
|  | 309 | src: "nativeCoverageArmSrc", | 
|  | 310 | }, | 
|  | 311 | }, | 
|  | 312 | }, | 
|  | 313 | }, | 
|  | 314 | }`, | 
|  | 315 | ExpectedBazelTargets: []string{ | 
|  | 316 | MakeBazelTarget("prebuilt_file", "foo", AttrNameToString{ | 
|  | 317 | "filename": `"fooFilename"`, | 
|  | 318 | "dir":      `"etc"`, | 
|  | 319 | "src": `select({ | 
|  | 320 | "//build/bazel/platforms/arch:arm": "armSrc", | 
| Cole Faust | 87c0c33 | 2023-07-31 12:10:12 -0700 | [diff] [blame] | 321 | "//build/bazel/product_config/config_settings:native_coverage-arm": "nativeCoverageArmSrc", | 
| Alix | bbfd538 | 2022-06-09 18:52:05 +0000 | [diff] [blame] | 322 | "//conditions:default": None, | 
|  | 323 | })`, | 
|  | 324 | })}}) | 
|  | 325 | } | 
|  | 326 |  | 
|  | 327 | func TestPrebuiltEtcProductVariableError(t *testing.T) { | 
|  | 328 | runPrebuiltEtcTestCase(t, Bp2buildTestCase{ | 
|  | 329 | Description: "", | 
|  | 330 | Filesystem:  map[string]string{}, | 
|  | 331 | Blueprint: ` | 
|  | 332 | prebuilt_etc { | 
|  | 333 | name: "foo", | 
|  | 334 | filename: "fooFilename", | 
|  | 335 | arch: { | 
|  | 336 | arm: { | 
|  | 337 | src: "armSrc", | 
|  | 338 | }, | 
|  | 339 | }, | 
|  | 340 | product_variables: { | 
|  | 341 | native_coverage: { | 
|  | 342 | src: "nativeCoverageArmSrc", | 
|  | 343 | }, | 
|  | 344 | }, | 
|  | 345 | }`, | 
|  | 346 | ExpectedErr: fmt.Errorf("label attribute could not be collapsed"), | 
|  | 347 | }) | 
|  | 348 | } |