blob: 5b2d609acdda4c0437b97f8292a7629b119dd334 [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
29 RunBp2BuildTestCase(t, registerPrebuiltEtcModuleTypes, tc)
Rupert Shuttleworth378fc1b2021-07-28 08:03:16 -040030}
31
32func registerPrebuiltEtcModuleTypes(ctx android.RegistrationContext) {
33}
34
35func TestPrebuiltEtcSimple(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +000036 runPrebuiltEtcTestCase(t, Bp2buildTestCase{
37 Description: "prebuilt_etc - simple example",
38 Filesystem: map[string]string{},
39 Blueprint: `
Rupert Shuttleworth378fc1b2021-07-28 08:03:16 -040040prebuilt_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 Delmerico3177a6e2022-06-21 19:28:33 +000048 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +000049 MakeBazelTarget("prebuilt_file", "apex_tz_version", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -050050 "filename": `"tz_version"`,
51 "installable": `False`,
52 "src": `"version/tz_version"`,
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc78604e2022-02-28 18:22:59 -050053 "dir": `"etc/tz"`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -050054 })}})
Rupert Shuttleworth378fc1b2021-07-28 08:03:16 -040055}
Liz Kammerdff00ea2021-10-04 13:44:34 -040056
57func TestPrebuiltEtcArchVariant(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +000058 runPrebuiltEtcTestCase(t, Bp2buildTestCase{
59 Description: "prebuilt_etc - arch variant",
60 Filesystem: map[string]string{},
61 Blueprint: `
Liz Kammerdff00ea2021-10-04 13:44:34 -040062prebuilt_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 Delmerico3177a6e2022-06-21 19:28:33 +000078 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +000079 MakeBazelTarget("prebuilt_file", "apex_tz_version", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -050080 "filename": `"tz_version"`,
81 "installable": `False`,
82 "src": `select({
Liz Kammerdff00ea2021-10-04 13:44:34 -040083 "//build/bazel/platforms/arch:arm": "arm",
84 "//build/bazel/platforms/arch:arm64": "arm64",
85 "//conditions:default": "version/tz_version",
Liz Kammer78cfdaa2021-11-08 12:56:31 -050086 })`,
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc78604e2022-02-28 18:22:59 -050087 "dir": `"etc/tz"`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -050088 })}})
Liz Kammerdff00ea2021-10-04 13:44:34 -040089}
Chris Parsons58852a02021-12-09 18:10:18 -050090
91func TestPrebuiltEtcArchAndTargetVariant(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +000092 runPrebuiltEtcTestCase(t, Bp2buildTestCase{
93 Description: "prebuilt_etc - arch variant",
94 Filesystem: map[string]string{},
95 Blueprint: `
Chris Parsons58852a02021-12-09 18:10:18 -050096prebuilt_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 Delmerico3177a6e2022-06-21 19:28:33 +0000117 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000118 MakeBazelTarget("prebuilt_file", "apex_tz_version", AttrNameToString{
Chris Parsons58852a02021-12-09 18:10:18 -0500119 "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 Thaureauxc78604e2022-02-28 18:22:59 -0500129 "dir": `"etc/tz"`,
130 })}})
131}
Alixbbfd5382022-06-09 18:52:05 +0000132func TestPrebuiltEtcProductVariables(t *testing.T) {
133 runPrebuiltEtcTestCase(t, Bp2buildTestCase{
134 Description: "prebuilt etc - product variables",
135 Filesystem: map[string]string{},
136 Blueprint: `
137prebuilt_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 Faust87c0c332023-07-31 12:10:12 -0700152 "//build/bazel/product_config/config_settings:native_coverage": "src1",
Alixbbfd5382022-06-09 18:52:05 +0000153 "//conditions:default": "version/tz_version",
154 })`,
155 "dir": `"etc"`,
156 })}})
157}
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc78604e2022-02-28 18:22:59 -0500158
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000159func runPrebuiltUsrShareTestCase(t *testing.T, tc Bp2buildTestCase) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc78604e2022-02-28 18:22:59 -0500160 t.Helper()
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000161 (&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 Thaureauxc78604e2022-02-28 18:22:59 -0500164}
165
166func registerPrebuiltUsrShareModuleTypes(ctx android.RegistrationContext) {
167}
168
169func TestPrebuiltUsrShareSimple(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000170 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 Thaureauxc78604e2022-02-28 18:22:59 -0500174prebuilt_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 Delmerico3177a6e2022-06-21 19:28:33 +0000182 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000183 MakeBazelTarget("prebuilt_file", "apex_tz_version", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc78604e2022-02-28 18:22:59 -0500184 "filename": `"tz_version"`,
185 "installable": `False`,
186 "src": `"version/tz_version"`,
187 "dir": `"usr/share/tz"`,
188 })}})
189}
190
191func TestPrebuiltEtcNoSubdir(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000192 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 Thaureauxc78604e2022-02-28 18:22:59 -0500196prebuilt_etc {
197 name: "apex_tz_version",
198 src: "version/tz_version",
199 filename: "tz_version",
200 installable: false,
201}
202`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000203 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000204 MakeBazelTarget("prebuilt_file", "apex_tz_version", AttrNameToString{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc78604e2022-02-28 18:22:59 -0500205 "filename": `"tz_version"`,
206 "installable": `False`,
207 "src": `"version/tz_version"`,
208 "dir": `"etc"`,
Chris Parsons58852a02021-12-09 18:10:18 -0500209 })}})
210}
Alix993872a2022-06-15 17:42:14 +0000211
212func 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: `
217prebuilt_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
231func TestFileNameFromSrc(t *testing.T) {
232 runPrebuiltEtcTestCase(t, Bp2buildTestCase{
233 Description: "prebuilt_etc - filename_from_src is true ",
234 Filesystem: map[string]string{},
235 Blueprint: `
236prebuilt_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
250func 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: `
255prebuilt_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
280func 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: `
285prebuilt_etc {
286 name: "foo",
287}
288`,
289 ExpectedBazelTargets: []string{
290 MakeBazelTarget("prebuilt_file", "foo", AttrNameToString{
291 "filename": `"foo"`,
292 "dir": `"etc"`,
293 })}})
294}
Alixbbfd5382022-06-09 18:52:05 +0000295
296func 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: `
301prebuilt_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 Faust87c0c332023-07-31 12:10:12 -0700321 "//build/bazel/product_config/config_settings:native_coverage-arm": "nativeCoverageArmSrc",
Alixbbfd5382022-06-09 18:52:05 +0000322 "//conditions:default": None,
323 })`,
324 })}})
325}
326
327func TestPrebuiltEtcProductVariableError(t *testing.T) {
328 runPrebuiltEtcTestCase(t, Bp2buildTestCase{
329 Description: "",
330 Filesystem: map[string]string{},
331 Blueprint: `
332prebuilt_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}