blob: 76bbb57987a19e60ce319460404e2ee3e061b2d0 [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{
Yu Liud136a6a2023-08-21 21:23:04 +0000241 "AndroidTest.xml": "",
242 "DynamicConfig.xml": "",
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000243 },
244 blueprint: `
245cc_test {
246 name: "mytest",
247 srcs: ["test.cpp"],
248}
Spandan Das651203d2023-07-21 22:55:32 +0000249` + simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_main") +
250 simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest"),
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000251 targets: []testBazelTarget{
252 {"cc_test", "mytest", AttrNameToString{
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000253 "local_includes": `["."]`,
254 "srcs": `["test.cpp"]`,
255 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
256 "test_config": `"AndroidTest.xml"`,
Yu Liud136a6a2023-08-21 21:23:04 +0000257 "dynamic_config": `"DynamicConfig.xml"`,
Spandan Das651203d2023-07-21 22:55:32 +0000258 "deps": `[
259 ":libgtest_main",
260 ":libgtest",
261 ]`,
yikefdca7fe2023-08-17 01:03:37 +0000262 "runs_on": `["device"]`,
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000263 },
264 },
265 },
266 })
267}
268
269func TestCcTest_TestConfigTemplateOptions(t *testing.T) {
270 runCcTestTestCase(t, ccTestBp2buildTestCase{
271 description: "cc test that sets test config template attributes",
272 filesystem: map[string]string{
273 "test_config_template.xml": "",
274 },
275 blueprint: `
276cc_test {
277 name: "mytest",
278 srcs: ["test.cpp"],
279 test_config_template: "test_config_template.xml",
280 auto_gen_config: true,
Spandan Dasf5a86552023-07-22 03:16:53 +0000281 isolated: true,
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000282}
Spandan Dasd1cd3512023-07-22 02:19:42 +0000283` + simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_isolated_main") +
284 simpleModuleDoNotConvertBp2build("cc_library", "liblog"),
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000285 targets: []testBazelTarget{
286 {"cc_test", "mytest", AttrNameToString{
287 "auto_generate_test_config": "True",
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000288 "local_includes": `["."]`,
289 "srcs": `["test.cpp"]`,
290 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
291 "template_configs": `[
292 "'<target_preparer class=\"com.android.tradefed.targetprep.RootTargetPreparer\">\\n <option name=\"force-root\" value=\"false\" />\\n </target_preparer>'",
293 "'<option name=\"not-shardable\" value=\"true\" />'",
294 ]`,
295 "template_install_base": `"/data/local/tmp"`,
296 "template_test_config": `"test_config_template.xml"`,
Spandan Dasd1cd3512023-07-22 02:19:42 +0000297 "deps": `[":libgtest_isolated_main"]`,
298 "dynamic_deps": `[":liblog"]`,
yikefdca7fe2023-08-17 01:03:37 +0000299 "runs_on": `["device"]`,
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000300 },
301 },
302 },
303 })
304}
Spandan Das651203d2023-07-21 22:55:32 +0000305
306func TestCcTest_WithExplicitGTestDepInAndroidBp(t *testing.T) {
307 runCcTestTestCase(t, ccTestBp2buildTestCase{
308 description: "cc test that lists libgtest in Android.bp should not have dups of libgtest in BUILD file",
309 blueprint: `
310cc_test {
311 name: "mytest",
312 srcs: ["test.cpp"],
313 static_libs: ["libgtest"],
314}
315` + simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_main") +
316 simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest"),
317 targets: []testBazelTarget{
318 {"cc_test", "mytest", AttrNameToString{
Spandan Das651203d2023-07-21 22:55:32 +0000319 "local_includes": `["."]`,
320 "srcs": `["test.cpp"]`,
321 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
322 "deps": `[
323 ":libgtest",
324 ":libgtest_main",
325 ]`,
yikefdca7fe2023-08-17 01:03:37 +0000326 "runs_on": `["device"]`,
Spandan Das651203d2023-07-21 22:55:32 +0000327 },
328 },
329 },
330 })
331
332}
Spandan Dasd1cd3512023-07-22 02:19:42 +0000333
334func TestCcTest_WithIsolatedTurnedOn(t *testing.T) {
335 runCcTestTestCase(t, ccTestBp2buildTestCase{
336 description: "cc test that sets `isolated: true` should run with ligtest_isolated_main instead of libgtest_main",
337 blueprint: `
338cc_test {
339 name: "mytest",
340 srcs: ["test.cpp"],
341 isolated: true,
342}
343` + simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_isolated_main") +
344 simpleModuleDoNotConvertBp2build("cc_library", "liblog"),
345 targets: []testBazelTarget{
346 {"cc_test", "mytest", AttrNameToString{
Spandan Dasd1cd3512023-07-22 02:19:42 +0000347 "local_includes": `["."]`,
348 "srcs": `["test.cpp"]`,
349 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
350 "deps": `[":libgtest_isolated_main"]`,
351 "dynamic_deps": `[":liblog"]`,
yikefdca7fe2023-08-17 01:03:37 +0000352 "runs_on": `["device"]`,
Spandan Dasd1cd3512023-07-22 02:19:42 +0000353 },
354 },
355 },
356 })
357
358}
Spandan Dase50fd112023-07-26 17:35:20 +0000359
360func TestCcTest_GtestExplicitlySpecifiedInAndroidBp(t *testing.T) {
361 runCcTestTestCase(t, ccTestBp2buildTestCase{
362 description: "If `gtest` is explicit in Android.bp, it should be explicit in BUILD files as well",
363 blueprint: `
364cc_test {
365 name: "mytest_with_gtest",
366 gtest: true,
367}
368cc_test {
369 name: "mytest_with_no_gtest",
370 gtest: false,
371}
372` + simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_main") +
373 simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest"),
374 targets: []testBazelTarget{
375 {"cc_test", "mytest_with_gtest", AttrNameToString{
376 "local_includes": `["."]`,
377 "deps": `[
378 ":libgtest_main",
379 ":libgtest",
380 ]`,
381 "gtest": "True",
382 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
yikefdca7fe2023-08-17 01:03:37 +0000383 "runs_on": `["device"]`,
Spandan Dase50fd112023-07-26 17:35:20 +0000384 },
385 },
386 {"cc_test", "mytest_with_no_gtest", AttrNameToString{
387 "local_includes": `["."]`,
388 "gtest": "False",
389 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
yikefdca7fe2023-08-17 01:03:37 +0000390 "runs_on": `["device"]`,
Spandan Dase50fd112023-07-26 17:35:20 +0000391 },
392 },
393 },
394 })
395}