blob: 030dbcca360b95ba1ad444ea022201d991872d26 [file] [log] [blame]
Rupert Shuttleworth378fc1b2021-07-28 08:03:16 -04001// 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
15package bp2build
16
17import (
Alixbbfd5382022-06-09 18:52:05 +000018 "fmt"
19 "testing"
20
Rupert Shuttleworth378fc1b2021-07-28 08:03:16 -040021 "android/soong/android"
22 "android/soong/etc"
Rupert Shuttleworth378fc1b2021-07-28 08:03:16 -040023)
24
Sam Delmerico3177a6e2022-06-21 19:28:33 +000025func runPrebuiltEtcTestCase(t *testing.T, tc Bp2buildTestCase) {
Rupert Shuttleworth378fc1b2021-07-28 08:03:16 -040026 t.Helper()
Sam Delmerico3177a6e2022-06-21 19:28:33 +000027 (&tc).ModuleTypeUnderTest = "prebuilt_etc"
28 (&tc).ModuleTypeUnderTestFactory = etc.PrebuiltEtcFactory
Zi Wang04054142023-10-10 14:49:18 -070029 RunBp2BuildTestCase(t, registerPrebuiltModuleTypes, tc)
Rupert Shuttleworth378fc1b2021-07-28 08:03:16 -040030}
31
Zi Wang04054142023-10-10 14:49:18 -070032func runPrebuiltRootHostTestCase(t *testing.T, tc Bp2buildTestCase) {
33 t.Helper()
34 (&tc).ModuleTypeUnderTest = "prebuilt_root_host"
35 (&tc).ModuleTypeUnderTestFactory = etc.PrebuiltRootHostFactory
36 RunBp2BuildTestCase(t, registerPrebuiltModuleTypes, tc)
37}
38
39func registerPrebuiltModuleTypes(ctx android.RegistrationContext) {
Rupert Shuttleworth378fc1b2021-07-28 08:03:16 -040040}
41
42func TestPrebuiltEtcSimple(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +000043 runPrebuiltEtcTestCase(t, Bp2buildTestCase{
44 Description: "prebuilt_etc - simple example",
45 Filesystem: map[string]string{},
46 Blueprint: `
Rupert Shuttleworth378fc1b2021-07-28 08:03:16 -040047prebuilt_etc {
48 name: "apex_tz_version",
49 src: "version/tz_version",
50 filename: "tz_version",
51 sub_dir: "tz",
52 installable: false,
53}
54`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +000055 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +000056 MakeBazelTarget("prebuilt_file", "apex_tz_version", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -050057 "filename": `"tz_version"`,
58 "installable": `False`,
59 "src": `"version/tz_version"`,
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc78604e2022-02-28 18:22:59 -050060 "dir": `"etc/tz"`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -050061 })}})
Rupert Shuttleworth378fc1b2021-07-28 08:03:16 -040062}
Liz Kammerdff00ea2021-10-04 13:44:34 -040063
64func TestPrebuiltEtcArchVariant(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +000065 runPrebuiltEtcTestCase(t, Bp2buildTestCase{
66 Description: "prebuilt_etc - arch variant",
67 Filesystem: map[string]string{},
68 Blueprint: `
Liz Kammerdff00ea2021-10-04 13:44:34 -040069prebuilt_etc {
70 name: "apex_tz_version",
71 src: "version/tz_version",
72 filename: "tz_version",
73 sub_dir: "tz",
74 installable: false,
75 arch: {
76 arm: {
77 src: "arm",
78 },
79 arm64: {
80 src: "arm64",
81 },
82 }
83}
84`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +000085 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +000086 MakeBazelTarget("prebuilt_file", "apex_tz_version", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -050087 "filename": `"tz_version"`,
88 "installable": `False`,
89 "src": `select({
Liz Kammerdff00ea2021-10-04 13:44:34 -040090 "//build/bazel/platforms/arch:arm": "arm",
91 "//build/bazel/platforms/arch:arm64": "arm64",
92 "//conditions:default": "version/tz_version",
Liz Kammer78cfdaa2021-11-08 12:56:31 -050093 })`,
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc78604e2022-02-28 18:22:59 -050094 "dir": `"etc/tz"`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -050095 })}})
Liz Kammerdff00ea2021-10-04 13:44:34 -040096}
Chris Parsons58852a02021-12-09 18:10:18 -050097
98func TestPrebuiltEtcArchAndTargetVariant(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +000099 runPrebuiltEtcTestCase(t, Bp2buildTestCase{
100 Description: "prebuilt_etc - arch variant",
101 Filesystem: map[string]string{},
102 Blueprint: `
Chris Parsons58852a02021-12-09 18:10:18 -0500103prebuilt_etc {
104 name: "apex_tz_version",
105 src: "version/tz_version",
106 filename: "tz_version",
107 sub_dir: "tz",
108 installable: false,
109 arch: {
110 arm: {
111 src: "arm",
112 },
113 arm64: {
114 src: "darwin_or_arm64",
115 },
116 },
117 target: {
118 darwin: {
119 src: "darwin_or_arm64",
120 }
121 },
122}
123`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000124 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000125 MakeBazelTarget("prebuilt_file", "apex_tz_version", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -0500126 "filename": `"tz_version"`,
127 "installable": `False`,
128 "src": `select({
129 "//build/bazel/platforms/os_arch:android_arm": "arm",
130 "//build/bazel/platforms/os_arch:android_arm64": "darwin_or_arm64",
131 "//build/bazel/platforms/os_arch:darwin_arm64": "darwin_or_arm64",
132 "//build/bazel/platforms/os_arch:darwin_x86_64": "darwin_or_arm64",
133 "//build/bazel/platforms/os_arch:linux_bionic_arm64": "darwin_or_arm64",
134 "//conditions:default": "version/tz_version",
135 })`,
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc78604e2022-02-28 18:22:59 -0500136 "dir": `"etc/tz"`,
137 })}})
138}
Alixbbfd5382022-06-09 18:52:05 +0000139func TestPrebuiltEtcProductVariables(t *testing.T) {
140 runPrebuiltEtcTestCase(t, Bp2buildTestCase{
141 Description: "prebuilt etc - product variables",
142 Filesystem: map[string]string{},
143 Blueprint: `
144prebuilt_etc {
145 name: "apex_tz_version",
146 src: "version/tz_version",
147 filename: "tz_version",
148 product_variables: {
149 native_coverage: {
150 src: "src1",
151 },
152 },
153}
154`,
155 ExpectedBazelTargets: []string{
156 MakeBazelTarget("prebuilt_file", "apex_tz_version", AttrNameToString{
157 "filename": `"tz_version"`,
158 "src": `select({
Cole Faust87c0c332023-07-31 12:10:12 -0700159 "//build/bazel/product_config/config_settings:native_coverage": "src1",
Alixbbfd5382022-06-09 18:52:05 +0000160 "//conditions:default": "version/tz_version",
161 })`,
162 "dir": `"etc"`,
163 })}})
164}
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc78604e2022-02-28 18:22:59 -0500165
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000166func runPrebuiltUsrShareTestCase(t *testing.T, tc Bp2buildTestCase) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc78604e2022-02-28 18:22:59 -0500167 t.Helper()
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000168 (&tc).ModuleTypeUnderTest = "prebuilt_usr_share"
169 (&tc).ModuleTypeUnderTestFactory = etc.PrebuiltUserShareFactory
Zi Wang04054142023-10-10 14:49:18 -0700170 RunBp2BuildTestCase(t, registerPrebuiltModuleTypes, tc)
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc78604e2022-02-28 18:22:59 -0500171}
172
173func registerPrebuiltUsrShareModuleTypes(ctx android.RegistrationContext) {
174}
175
176func TestPrebuiltUsrShareSimple(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000177 runPrebuiltUsrShareTestCase(t, Bp2buildTestCase{
178 Description: "prebuilt_usr_share - simple example",
179 Filesystem: map[string]string{},
180 Blueprint: `
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc78604e2022-02-28 18:22:59 -0500181prebuilt_usr_share {
182 name: "apex_tz_version",
183 src: "version/tz_version",
184 filename: "tz_version",
185 sub_dir: "tz",
186 installable: false,
187}
188`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000189 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000190 MakeBazelTarget("prebuilt_file", "apex_tz_version", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc78604e2022-02-28 18:22:59 -0500191 "filename": `"tz_version"`,
192 "installable": `False`,
193 "src": `"version/tz_version"`,
194 "dir": `"usr/share/tz"`,
195 })}})
196}
197
198func TestPrebuiltEtcNoSubdir(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000199 runPrebuiltEtcTestCase(t, Bp2buildTestCase{
200 Description: "prebuilt_etc - no subdir",
201 Filesystem: map[string]string{},
202 Blueprint: `
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc78604e2022-02-28 18:22:59 -0500203prebuilt_etc {
204 name: "apex_tz_version",
205 src: "version/tz_version",
206 filename: "tz_version",
207 installable: false,
208}
209`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000210 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000211 MakeBazelTarget("prebuilt_file", "apex_tz_version", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc78604e2022-02-28 18:22:59 -0500212 "filename": `"tz_version"`,
213 "installable": `False`,
214 "src": `"version/tz_version"`,
215 "dir": `"etc"`,
Chris Parsons58852a02021-12-09 18:10:18 -0500216 })}})
217}
Alix993872a2022-06-15 17:42:14 +0000218
219func TestFilenameAsProperty(t *testing.T) {
220 runPrebuiltEtcTestCase(t, Bp2buildTestCase{
221 Description: "prebuilt_etc - filename is specified as a property ",
222 Filesystem: map[string]string{},
223 Blueprint: `
224prebuilt_etc {
225 name: "foo",
226 src: "fooSrc",
227 filename: "fooFileName",
228}
229`,
230 ExpectedBazelTargets: []string{
231 MakeBazelTarget("prebuilt_file", "foo", AttrNameToString{
232 "filename": `"fooFileName"`,
233 "src": `"fooSrc"`,
234 "dir": `"etc"`,
235 })}})
236}
237
238func TestFileNameFromSrc(t *testing.T) {
239 runPrebuiltEtcTestCase(t, Bp2buildTestCase{
240 Description: "prebuilt_etc - filename_from_src is true ",
241 Filesystem: map[string]string{},
242 Blueprint: `
243prebuilt_etc {
244 name: "foo",
245 filename_from_src: true,
246 src: "fooSrc",
247}
248`,
249 ExpectedBazelTargets: []string{
250 MakeBazelTarget("prebuilt_file", "foo", AttrNameToString{
251 "filename": `"fooSrc"`,
252 "src": `"fooSrc"`,
253 "dir": `"etc"`,
254 })}})
255}
256
257func TestFileNameFromSrcMultipleSrcs(t *testing.T) {
258 runPrebuiltEtcTestCase(t, Bp2buildTestCase{
259 Description: "prebuilt_etc - filename_from_src is true but there are multiple srcs",
260 Filesystem: map[string]string{},
261 Blueprint: `
262prebuilt_etc {
263 name: "foo",
264 filename_from_src: true,
265 arch: {
266 arm: {
267 src: "barSrc",
268 },
269 arm64: {
270 src: "bazSrc",
271 },
272 }
273}
274`,
275 ExpectedBazelTargets: []string{
276 MakeBazelTarget("prebuilt_file", "foo", AttrNameToString{
277 "filename_from_src": `True`,
278 "dir": `"etc"`,
279 "src": `select({
280 "//build/bazel/platforms/arch:arm": "barSrc",
281 "//build/bazel/platforms/arch:arm64": "bazSrc",
282 "//conditions:default": None,
283 })`,
284 })}})
285}
286
287func TestFilenameFromModuleName(t *testing.T) {
288 runPrebuiltEtcTestCase(t, Bp2buildTestCase{
289 Description: "prebuilt_etc - neither filename nor filename_from_src are specified ",
290 Filesystem: map[string]string{},
291 Blueprint: `
292prebuilt_etc {
293 name: "foo",
294}
295`,
296 ExpectedBazelTargets: []string{
297 MakeBazelTarget("prebuilt_file", "foo", AttrNameToString{
298 "filename": `"foo"`,
299 "dir": `"etc"`,
300 })}})
301}
Alixbbfd5382022-06-09 18:52:05 +0000302
303func TestPrebuiltEtcProductVariableArchSrcs(t *testing.T) {
304 runPrebuiltEtcTestCase(t, Bp2buildTestCase{
305 Description: "prebuilt etc- SRcs from arch variant product variables",
306 Filesystem: map[string]string{},
307 Blueprint: `
308prebuilt_etc {
309 name: "foo",
310 filename: "fooFilename",
311 arch: {
312 arm: {
313 src: "armSrc",
314 product_variables: {
315 native_coverage: {
316 src: "nativeCoverageArmSrc",
317 },
318 },
319 },
320 },
321}`,
322 ExpectedBazelTargets: []string{
323 MakeBazelTarget("prebuilt_file", "foo", AttrNameToString{
324 "filename": `"fooFilename"`,
325 "dir": `"etc"`,
326 "src": `select({
327 "//build/bazel/platforms/arch:arm": "armSrc",
Cole Faust87c0c332023-07-31 12:10:12 -0700328 "//build/bazel/product_config/config_settings:native_coverage-arm": "nativeCoverageArmSrc",
Alixbbfd5382022-06-09 18:52:05 +0000329 "//conditions:default": None,
330 })`,
331 })}})
332}
333
334func TestPrebuiltEtcProductVariableError(t *testing.T) {
335 runPrebuiltEtcTestCase(t, Bp2buildTestCase{
336 Description: "",
337 Filesystem: map[string]string{},
338 Blueprint: `
339prebuilt_etc {
340 name: "foo",
341 filename: "fooFilename",
342 arch: {
343 arm: {
344 src: "armSrc",
345 },
346 },
347 product_variables: {
348 native_coverage: {
349 src: "nativeCoverageArmSrc",
350 },
351 },
352}`,
353 ExpectedErr: fmt.Errorf("label attribute could not be collapsed"),
354 })
355}
Liz Kammerd5d12d02023-09-05 09:18:50 -0400356
357func TestPrebuiltEtcNoConversionIfSrcEqualsName(t *testing.T) {
358 runPrebuiltEtcTestCase(t, Bp2buildTestCase{
359 Description: "",
360 Filesystem: map[string]string{},
361 Blueprint: `
362prebuilt_etc {
363 name: "foo",
364 filename: "fooFilename",
365 src: "foo",
366}`,
367 ExpectedBazelTargets: []string{},
368 })
369}
Zi Wang04054142023-10-10 14:49:18 -0700370
371func TestPrebuiltRootHostWithWildCardInSrc(t *testing.T) {
372 runPrebuiltRootHostTestCase(t, Bp2buildTestCase{
373 Description: "prebuilt_root_host - src string has wild card",
374 Filesystem: map[string]string{
375 "prh.dat": "",
376 },
377 Blueprint: `
378prebuilt_root_host {
379 name: "prh_test",
380 src: "*.dat",
381 filename_from_src: true,
382 relative_install_path: "test/install/path",
383 bazel_module: { bp2build_available: true },
384}
385`,
386 ExpectedBazelTargets: []string{
387 MakeBazelTarget("prebuilt_file", "prh_test", AttrNameToString{
388 "filename": `"prh.dat"`,
389 "src": `"prh.dat"`,
390 "dir": `"./test/install/path"`,
391 "target_compatible_with": `select({
392 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
393 "//conditions:default": [],
394 })`,
395 })}})
396}