blob: abceac844c7bdc3909ac18d9f55d0c80a7c068cc [file] [log] [blame]
Jingwen Chen537242c2022-08-24 11:53:27 +00001// Copyright 2022 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 (
18 "fmt"
19 "testing"
20
21 "android/soong/android"
22 "android/soong/cc"
23 "android/soong/genrule"
24)
25
26type ccTestBp2buildTestCase struct {
27 description string
28 blueprint string
Kevin Dagostino32edd1a2022-12-04 11:16:42 +000029 filesystem map[string]string
Jingwen Chen537242c2022-08-24 11:53:27 +000030 targets []testBazelTarget
31}
32
33func registerCcTestModuleTypes(ctx android.RegistrationContext) {
34 cc.RegisterCCBuildComponents(ctx)
35 ctx.RegisterModuleType("cc_binary", cc.BinaryFactory)
36 ctx.RegisterModuleType("cc_library_static", cc.LibraryStaticFactory)
37 ctx.RegisterModuleType("cc_library", cc.LibraryFactory)
38 ctx.RegisterModuleType("cc_test_library", cc.TestLibraryFactory)
39 ctx.RegisterModuleType("genrule", genrule.GenRuleFactory)
40}
41
42func runCcTestTestCase(t *testing.T, testCase ccTestBp2buildTestCase) {
43 t.Helper()
44 moduleTypeUnderTest := "cc_test"
Jingwen Chen537242c2022-08-24 11:53:27 +000045 description := fmt.Sprintf("%s %s", moduleTypeUnderTest, testCase.description)
46 t.Run(description, func(t *testing.T) {
47 t.Helper()
48 RunBp2BuildTestCase(t, registerCcTestModuleTypes, Bp2buildTestCase{
49 ExpectedBazelTargets: generateBazelTargetsForTest(testCase.targets, android.HostAndDeviceSupported),
Kevin Dagostino32edd1a2022-12-04 11:16:42 +000050 Filesystem: testCase.filesystem,
Jingwen Chen537242c2022-08-24 11:53:27 +000051 ModuleTypeUnderTest: moduleTypeUnderTest,
52 ModuleTypeUnderTestFactory: cc.TestFactory,
53 Description: description,
54 Blueprint: testCase.blueprint,
55 })
56 })
57}
58
59func TestBasicCcTest(t *testing.T) {
60 runCcTestTestCase(t, ccTestBp2buildTestCase{
61 description: "basic cc_test with commonly used attributes",
62 blueprint: `
63cc_test {
64 name: "mytest",
65 host_supported: true,
66 srcs: ["test.cpp"],
67 target: {
68 android: {
69 srcs: ["android.cpp"],
70 shared_libs: ["foolib"],
71 },
72 linux: {
73 srcs: ["linux.cpp"],
74 },
75 host: {
76 static_libs: ["hostlib"],
77 },
78 },
Liz Kammerefc51d92023-04-21 15:11:25 -040079 static_libs: ["cc_test_lib1"],
80 shared_libs: ["cc_test_lib2"],
Jingwen Chen537242c2022-08-24 11:53:27 +000081 data: [":data_mod", "file.txt"],
82 data_bins: [":cc_bin"],
83 data_libs: [":cc_lib"],
84 cflags: ["-Wall"],
85}
Liz Kammerefc51d92023-04-21 15:11:25 -040086
87cc_test_library {
88 name: "cc_test_lib1",
89 host_supported: true,
90 include_build_directory: false,
91}
Jingwen Chen537242c2022-08-24 11:53:27 +000092` + simpleModuleDoNotConvertBp2build("cc_library", "foolib") +
93 simpleModuleDoNotConvertBp2build("cc_library_static", "hostlib") +
94 simpleModuleDoNotConvertBp2build("genrule", "data_mod") +
95 simpleModuleDoNotConvertBp2build("cc_binary", "cc_bin") +
Liz Kammerefc51d92023-04-21 15:11:25 -040096 simpleModuleDoNotConvertBp2build("cc_library", "cc_lib") +
Spandan Das651203d2023-07-21 22:55:32 +000097 simpleModuleDoNotConvertBp2build("cc_test_library", "cc_test_lib2") +
98 simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_main") +
99 simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest"),
Jingwen Chen537242c2022-08-24 11:53:27 +0000100 targets: []testBazelTarget{
Liz Kammerefc51d92023-04-21 15:11:25 -0400101 {"cc_library_shared", "cc_test_lib1", AttrNameToString{}},
102 {"cc_library_static", "cc_test_lib1_bp2build_cc_library_static", AttrNameToString{}},
Jingwen Chen537242c2022-08-24 11:53:27 +0000103 {"cc_test", "mytest", AttrNameToString{
104 "copts": `["-Wall"]`,
105 "data": `[
106 ":data_mod",
107 "file.txt",
108 ":cc_bin",
109 ":cc_lib",
110 ]`,
Spandan Das651203d2023-07-21 22:55:32 +0000111 "deps": `[
112 ":cc_test_lib1_bp2build_cc_library_static",
113 ":libgtest_main",
114 ":libgtest",
115 ] + select({
Jingwen Chen537242c2022-08-24 11:53:27 +0000116 "//build/bazel/platforms/os:darwin": [":hostlib"],
Jingwen Chen537242c2022-08-24 11:53:27 +0000117 "//build/bazel/platforms/os:linux_bionic": [":hostlib"],
Colin Cross133782e2022-12-20 15:29:31 -0800118 "//build/bazel/platforms/os:linux_glibc": [":hostlib"],
Jingwen Chen537242c2022-08-24 11:53:27 +0000119 "//build/bazel/platforms/os:linux_musl": [":hostlib"],
120 "//build/bazel/platforms/os:windows": [":hostlib"],
121 "//conditions:default": [],
122 })`,
Jingwen Chen537242c2022-08-24 11:53:27 +0000123 "local_includes": `["."]`,
Liz Kammerefc51d92023-04-21 15:11:25 -0400124 "dynamic_deps": `[":cc_test_lib2"] + select({
Jingwen Chen537242c2022-08-24 11:53:27 +0000125 "//build/bazel/platforms/os:android": [":foolib"],
126 "//conditions:default": [],
127 })`,
128 "srcs": `["test.cpp"] + select({
129 "//build/bazel/platforms/os:android": [
130 "linux.cpp",
131 "android.cpp",
132 ],
Jingwen Chen537242c2022-08-24 11:53:27 +0000133 "//build/bazel/platforms/os:linux_bionic": ["linux.cpp"],
Colin Cross133782e2022-12-20 15:29:31 -0800134 "//build/bazel/platforms/os:linux_glibc": ["linux.cpp"],
Jingwen Chen537242c2022-08-24 11:53:27 +0000135 "//build/bazel/platforms/os:linux_musl": ["linux.cpp"],
136 "//conditions:default": [],
137 })`,
yikefdca7fe2023-08-17 01:03:37 +0000138 "runs_on": `[
139 "host_without_device",
140 "device",
141 ]`,
Jingwen Chen537242c2022-08-24 11:53:27 +0000142 },
143 },
144 },
145 })
146}
147
148func TestBasicCcTestGtestIsolatedDisabled(t *testing.T) {
149 runCcTestTestCase(t, ccTestBp2buildTestCase{
150 description: "cc test with disabled gtest and isolated props",
151 blueprint: `
152cc_test {
153 name: "mytest",
154 host_supported: true,
155 srcs: ["test.cpp"],
156 gtest: false,
157 isolated: false,
158}
159`,
160 targets: []testBazelTarget{
161 {"cc_test", "mytest", AttrNameToString{
162 "gtest": "False",
Jingwen Chen537242c2022-08-24 11:53:27 +0000163 "local_includes": `["."]`,
164 "srcs": `["test.cpp"]`,
yikefdca7fe2023-08-17 01:03:37 +0000165 "runs_on": `[
166 "host_without_device",
167 "device",
168 ]`,
Jingwen Chen537242c2022-08-24 11:53:27 +0000169 },
170 },
171 },
172 })
173}
Jingwen Chenfbff97a2022-09-16 02:32:03 +0000174
175func TestCcTest_TestOptions_Tags(t *testing.T) {
176 runCcTestTestCase(t, ccTestBp2buildTestCase{
177 description: "cc test with test_options.tags converted to tags",
178 blueprint: `
179cc_test {
180 name: "mytest",
181 host_supported: true,
182 srcs: ["test.cpp"],
183 test_options: { tags: ["no-remote"] },
184}
Spandan Das651203d2023-07-21 22:55:32 +0000185` + simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_main") +
186 simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest"),
Jingwen Chenfbff97a2022-09-16 02:32:03 +0000187 targets: []testBazelTarget{
188 {"cc_test", "mytest", AttrNameToString{
189 "tags": `["no-remote"]`,
190 "local_includes": `["."]`,
191 "srcs": `["test.cpp"]`,
Spandan Das651203d2023-07-21 22:55:32 +0000192 "deps": `[
193 ":libgtest_main",
194 ":libgtest",
195 ]`,
yikefdca7fe2023-08-17 01:03:37 +0000196 "runs_on": `[
197 "host_without_device",
198 "device",
199 ]`,
Jingwen Chenfbff97a2022-09-16 02:32:03 +0000200 },
201 },
202 },
203 })
204}
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000205
206func TestCcTest_TestConfig(t *testing.T) {
207 runCcTestTestCase(t, ccTestBp2buildTestCase{
208 description: "cc test that sets a test_config",
209 filesystem: map[string]string{
210 "test_config.xml": "",
211 },
212 blueprint: `
213cc_test {
214 name: "mytest",
215 srcs: ["test.cpp"],
216 test_config: "test_config.xml",
217}
Spandan Das651203d2023-07-21 22:55:32 +0000218` + simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_main") +
219 simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest"),
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000220 targets: []testBazelTarget{
221 {"cc_test", "mytest", AttrNameToString{
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000222 "local_includes": `["."]`,
223 "srcs": `["test.cpp"]`,
224 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
225 "test_config": `"test_config.xml"`,
Spandan Das651203d2023-07-21 22:55:32 +0000226 "deps": `[
227 ":libgtest_main",
228 ":libgtest",
229 ]`,
yikefdca7fe2023-08-17 01:03:37 +0000230 "runs_on": `["device"]`,
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000231 },
232 },
233 },
234 })
235}
236
237func TestCcTest_TestConfigAndroidTestXML(t *testing.T) {
238 runCcTestTestCase(t, ccTestBp2buildTestCase{
239 description: "cc test that defaults to test config AndroidTest.xml",
240 filesystem: map[string]string{
241 "AndroidTest.xml": "",
242 },
243 blueprint: `
244cc_test {
245 name: "mytest",
246 srcs: ["test.cpp"],
247}
Spandan Das651203d2023-07-21 22:55:32 +0000248` + simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_main") +
249 simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest"),
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000250 targets: []testBazelTarget{
251 {"cc_test", "mytest", AttrNameToString{
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000252 "local_includes": `["."]`,
253 "srcs": `["test.cpp"]`,
254 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
255 "test_config": `"AndroidTest.xml"`,
Spandan Das651203d2023-07-21 22:55:32 +0000256 "deps": `[
257 ":libgtest_main",
258 ":libgtest",
259 ]`,
yikefdca7fe2023-08-17 01:03:37 +0000260 "runs_on": `["device"]`,
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000261 },
262 },
263 },
264 })
265}
266
267func TestCcTest_TestConfigTemplateOptions(t *testing.T) {
268 runCcTestTestCase(t, ccTestBp2buildTestCase{
269 description: "cc test that sets test config template attributes",
270 filesystem: map[string]string{
271 "test_config_template.xml": "",
272 },
273 blueprint: `
274cc_test {
275 name: "mytest",
276 srcs: ["test.cpp"],
277 test_config_template: "test_config_template.xml",
278 auto_gen_config: true,
Spandan Dasf5a86552023-07-22 03:16:53 +0000279 isolated: true,
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000280}
Spandan Dasd1cd3512023-07-22 02:19:42 +0000281` + simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_isolated_main") +
282 simpleModuleDoNotConvertBp2build("cc_library", "liblog"),
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000283 targets: []testBazelTarget{
284 {"cc_test", "mytest", AttrNameToString{
285 "auto_generate_test_config": "True",
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000286 "local_includes": `["."]`,
287 "srcs": `["test.cpp"]`,
288 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
289 "template_configs": `[
290 "'<target_preparer class=\"com.android.tradefed.targetprep.RootTargetPreparer\">\\n <option name=\"force-root\" value=\"false\" />\\n </target_preparer>'",
291 "'<option name=\"not-shardable\" value=\"true\" />'",
292 ]`,
293 "template_install_base": `"/data/local/tmp"`,
294 "template_test_config": `"test_config_template.xml"`,
Spandan Dasd1cd3512023-07-22 02:19:42 +0000295 "deps": `[":libgtest_isolated_main"]`,
296 "dynamic_deps": `[":liblog"]`,
yikefdca7fe2023-08-17 01:03:37 +0000297 "runs_on": `["device"]`,
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000298 },
299 },
300 },
301 })
302}
Spandan Das651203d2023-07-21 22:55:32 +0000303
304func TestCcTest_WithExplicitGTestDepInAndroidBp(t *testing.T) {
305 runCcTestTestCase(t, ccTestBp2buildTestCase{
306 description: "cc test that lists libgtest in Android.bp should not have dups of libgtest in BUILD file",
307 blueprint: `
308cc_test {
309 name: "mytest",
310 srcs: ["test.cpp"],
311 static_libs: ["libgtest"],
312}
313` + simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_main") +
314 simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest"),
315 targets: []testBazelTarget{
316 {"cc_test", "mytest", AttrNameToString{
Spandan Das651203d2023-07-21 22:55:32 +0000317 "local_includes": `["."]`,
318 "srcs": `["test.cpp"]`,
319 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
320 "deps": `[
321 ":libgtest",
322 ":libgtest_main",
323 ]`,
yikefdca7fe2023-08-17 01:03:37 +0000324 "runs_on": `["device"]`,
Spandan Das651203d2023-07-21 22:55:32 +0000325 },
326 },
327 },
328 })
329
330}
Spandan Dasd1cd3512023-07-22 02:19:42 +0000331
332func TestCcTest_WithIsolatedTurnedOn(t *testing.T) {
333 runCcTestTestCase(t, ccTestBp2buildTestCase{
334 description: "cc test that sets `isolated: true` should run with ligtest_isolated_main instead of libgtest_main",
335 blueprint: `
336cc_test {
337 name: "mytest",
338 srcs: ["test.cpp"],
339 isolated: true,
340}
341` + simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_isolated_main") +
342 simpleModuleDoNotConvertBp2build("cc_library", "liblog"),
343 targets: []testBazelTarget{
344 {"cc_test", "mytest", AttrNameToString{
Spandan Dasd1cd3512023-07-22 02:19:42 +0000345 "local_includes": `["."]`,
346 "srcs": `["test.cpp"]`,
347 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
348 "deps": `[":libgtest_isolated_main"]`,
349 "dynamic_deps": `[":liblog"]`,
yikefdca7fe2023-08-17 01:03:37 +0000350 "runs_on": `["device"]`,
Spandan Dasd1cd3512023-07-22 02:19:42 +0000351 },
352 },
353 },
354 })
355
356}
Spandan Dase50fd112023-07-26 17:35:20 +0000357
358func TestCcTest_GtestExplicitlySpecifiedInAndroidBp(t *testing.T) {
359 runCcTestTestCase(t, ccTestBp2buildTestCase{
360 description: "If `gtest` is explicit in Android.bp, it should be explicit in BUILD files as well",
361 blueprint: `
362cc_test {
363 name: "mytest_with_gtest",
364 gtest: true,
365}
366cc_test {
367 name: "mytest_with_no_gtest",
368 gtest: false,
369}
370` + simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_main") +
371 simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest"),
372 targets: []testBazelTarget{
373 {"cc_test", "mytest_with_gtest", AttrNameToString{
374 "local_includes": `["."]`,
375 "deps": `[
376 ":libgtest_main",
377 ":libgtest",
378 ]`,
379 "gtest": "True",
380 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
yikefdca7fe2023-08-17 01:03:37 +0000381 "runs_on": `["device"]`,
Spandan Dase50fd112023-07-26 17:35:20 +0000382 },
383 },
384 {"cc_test", "mytest_with_no_gtest", AttrNameToString{
385 "local_includes": `["."]`,
386 "gtest": "False",
387 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
yikefdca7fe2023-08-17 01:03:37 +0000388 "runs_on": `["device"]`,
Spandan Dase50fd112023-07-26 17:35:20 +0000389 },
390 },
391 },
392 })
393}