blob: 3c037b49490223d44e72709b44fa8126237649be [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 })`,
138 },
139 },
140 },
141 })
142}
143
144func TestBasicCcTestGtestIsolatedDisabled(t *testing.T) {
145 runCcTestTestCase(t, ccTestBp2buildTestCase{
146 description: "cc test with disabled gtest and isolated props",
147 blueprint: `
148cc_test {
149 name: "mytest",
150 host_supported: true,
151 srcs: ["test.cpp"],
152 gtest: false,
153 isolated: false,
154}
155`,
156 targets: []testBazelTarget{
157 {"cc_test", "mytest", AttrNameToString{
158 "gtest": "False",
Jingwen Chen537242c2022-08-24 11:53:27 +0000159 "local_includes": `["."]`,
160 "srcs": `["test.cpp"]`,
161 },
162 },
163 },
164 })
165}
Jingwen Chenfbff97a2022-09-16 02:32:03 +0000166
167func TestCcTest_TestOptions_Tags(t *testing.T) {
168 runCcTestTestCase(t, ccTestBp2buildTestCase{
169 description: "cc test with test_options.tags converted to tags",
170 blueprint: `
171cc_test {
172 name: "mytest",
173 host_supported: true,
174 srcs: ["test.cpp"],
175 test_options: { tags: ["no-remote"] },
176}
Spandan Das651203d2023-07-21 22:55:32 +0000177` + simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_main") +
178 simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest"),
Jingwen Chenfbff97a2022-09-16 02:32:03 +0000179 targets: []testBazelTarget{
180 {"cc_test", "mytest", AttrNameToString{
181 "tags": `["no-remote"]`,
182 "local_includes": `["."]`,
183 "srcs": `["test.cpp"]`,
Spandan Das651203d2023-07-21 22:55:32 +0000184 "deps": `[
185 ":libgtest_main",
186 ":libgtest",
187 ]`,
Jingwen Chenfbff97a2022-09-16 02:32:03 +0000188 },
189 },
190 },
191 })
192}
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000193
194func TestCcTest_TestConfig(t *testing.T) {
195 runCcTestTestCase(t, ccTestBp2buildTestCase{
196 description: "cc test that sets a test_config",
197 filesystem: map[string]string{
198 "test_config.xml": "",
199 },
200 blueprint: `
201cc_test {
202 name: "mytest",
203 srcs: ["test.cpp"],
204 test_config: "test_config.xml",
205}
Spandan Das651203d2023-07-21 22:55:32 +0000206` + simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_main") +
207 simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest"),
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000208 targets: []testBazelTarget{
209 {"cc_test", "mytest", AttrNameToString{
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000210 "local_includes": `["."]`,
211 "srcs": `["test.cpp"]`,
212 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
213 "test_config": `"test_config.xml"`,
Spandan Das651203d2023-07-21 22:55:32 +0000214 "deps": `[
215 ":libgtest_main",
216 ":libgtest",
217 ]`,
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000218 },
219 },
220 },
221 })
222}
223
224func TestCcTest_TestConfigAndroidTestXML(t *testing.T) {
225 runCcTestTestCase(t, ccTestBp2buildTestCase{
226 description: "cc test that defaults to test config AndroidTest.xml",
227 filesystem: map[string]string{
228 "AndroidTest.xml": "",
229 },
230 blueprint: `
231cc_test {
232 name: "mytest",
233 srcs: ["test.cpp"],
234}
Spandan Das651203d2023-07-21 22:55:32 +0000235` + simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_main") +
236 simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest"),
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000237 targets: []testBazelTarget{
238 {"cc_test", "mytest", AttrNameToString{
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000239 "local_includes": `["."]`,
240 "srcs": `["test.cpp"]`,
241 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
242 "test_config": `"AndroidTest.xml"`,
Spandan Das651203d2023-07-21 22:55:32 +0000243 "deps": `[
244 ":libgtest_main",
245 ":libgtest",
246 ]`,
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000247 },
248 },
249 },
250 })
251}
252
253func TestCcTest_TestConfigTemplateOptions(t *testing.T) {
254 runCcTestTestCase(t, ccTestBp2buildTestCase{
255 description: "cc test that sets test config template attributes",
256 filesystem: map[string]string{
257 "test_config_template.xml": "",
258 },
259 blueprint: `
260cc_test {
261 name: "mytest",
262 srcs: ["test.cpp"],
263 test_config_template: "test_config_template.xml",
264 auto_gen_config: true,
Spandan Dasf5a86552023-07-22 03:16:53 +0000265 isolated: true,
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000266}
Spandan Dasd1cd3512023-07-22 02:19:42 +0000267` + simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_isolated_main") +
268 simpleModuleDoNotConvertBp2build("cc_library", "liblog"),
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000269 targets: []testBazelTarget{
270 {"cc_test", "mytest", AttrNameToString{
271 "auto_generate_test_config": "True",
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000272 "local_includes": `["."]`,
273 "srcs": `["test.cpp"]`,
274 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
275 "template_configs": `[
276 "'<target_preparer class=\"com.android.tradefed.targetprep.RootTargetPreparer\">\\n <option name=\"force-root\" value=\"false\" />\\n </target_preparer>'",
277 "'<option name=\"not-shardable\" value=\"true\" />'",
278 ]`,
279 "template_install_base": `"/data/local/tmp"`,
280 "template_test_config": `"test_config_template.xml"`,
Spandan Dasd1cd3512023-07-22 02:19:42 +0000281 "deps": `[":libgtest_isolated_main"]`,
282 "dynamic_deps": `[":liblog"]`,
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000283 },
284 },
285 },
286 })
287}
Spandan Das651203d2023-07-21 22:55:32 +0000288
289func TestCcTest_WithExplicitGTestDepInAndroidBp(t *testing.T) {
290 runCcTestTestCase(t, ccTestBp2buildTestCase{
291 description: "cc test that lists libgtest in Android.bp should not have dups of libgtest in BUILD file",
292 blueprint: `
293cc_test {
294 name: "mytest",
295 srcs: ["test.cpp"],
296 static_libs: ["libgtest"],
297}
298` + simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_main") +
299 simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest"),
300 targets: []testBazelTarget{
301 {"cc_test", "mytest", AttrNameToString{
Spandan Das651203d2023-07-21 22:55:32 +0000302 "local_includes": `["."]`,
303 "srcs": `["test.cpp"]`,
304 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
305 "deps": `[
306 ":libgtest",
307 ":libgtest_main",
308 ]`,
309 },
310 },
311 },
312 })
313
314}
Spandan Dasd1cd3512023-07-22 02:19:42 +0000315
316func TestCcTest_WithIsolatedTurnedOn(t *testing.T) {
317 runCcTestTestCase(t, ccTestBp2buildTestCase{
318 description: "cc test that sets `isolated: true` should run with ligtest_isolated_main instead of libgtest_main",
319 blueprint: `
320cc_test {
321 name: "mytest",
322 srcs: ["test.cpp"],
323 isolated: true,
324}
325` + simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_isolated_main") +
326 simpleModuleDoNotConvertBp2build("cc_library", "liblog"),
327 targets: []testBazelTarget{
328 {"cc_test", "mytest", AttrNameToString{
Spandan Dasd1cd3512023-07-22 02:19:42 +0000329 "local_includes": `["."]`,
330 "srcs": `["test.cpp"]`,
331 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
332 "deps": `[":libgtest_isolated_main"]`,
333 "dynamic_deps": `[":liblog"]`,
334 },
335 },
336 },
337 })
338
339}
Spandan Dase50fd112023-07-26 17:35:20 +0000340
341func TestCcTest_GtestExplicitlySpecifiedInAndroidBp(t *testing.T) {
342 runCcTestTestCase(t, ccTestBp2buildTestCase{
343 description: "If `gtest` is explicit in Android.bp, it should be explicit in BUILD files as well",
344 blueprint: `
345cc_test {
346 name: "mytest_with_gtest",
347 gtest: true,
348}
349cc_test {
350 name: "mytest_with_no_gtest",
351 gtest: false,
352}
353` + simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_main") +
354 simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest"),
355 targets: []testBazelTarget{
356 {"cc_test", "mytest_with_gtest", AttrNameToString{
357 "local_includes": `["."]`,
358 "deps": `[
359 ":libgtest_main",
360 ":libgtest",
361 ]`,
362 "gtest": "True",
363 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
364 },
365 },
366 {"cc_test", "mytest_with_no_gtest", AttrNameToString{
367 "local_includes": `["."]`,
368 "gtest": "False",
369 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
370 },
371 },
372 },
373 })
374}