blob: 20eb092d1c1dd49369167f34c1efb06bdb8b8727 [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 })`,
123 "gtest": "True",
Jingwen Chen537242c2022-08-24 11:53:27 +0000124 "local_includes": `["."]`,
Liz Kammerefc51d92023-04-21 15:11:25 -0400125 "dynamic_deps": `[":cc_test_lib2"] + select({
Jingwen Chen537242c2022-08-24 11:53:27 +0000126 "//build/bazel/platforms/os:android": [":foolib"],
127 "//conditions:default": [],
128 })`,
129 "srcs": `["test.cpp"] + select({
130 "//build/bazel/platforms/os:android": [
131 "linux.cpp",
132 "android.cpp",
133 ],
Jingwen Chen537242c2022-08-24 11:53:27 +0000134 "//build/bazel/platforms/os:linux_bionic": ["linux.cpp"],
Colin Cross133782e2022-12-20 15:29:31 -0800135 "//build/bazel/platforms/os:linux_glibc": ["linux.cpp"],
Jingwen Chen537242c2022-08-24 11:53:27 +0000136 "//build/bazel/platforms/os:linux_musl": ["linux.cpp"],
137 "//conditions:default": [],
138 })`,
139 },
140 },
141 },
142 })
143}
144
145func TestBasicCcTestGtestIsolatedDisabled(t *testing.T) {
146 runCcTestTestCase(t, ccTestBp2buildTestCase{
147 description: "cc test with disabled gtest and isolated props",
148 blueprint: `
149cc_test {
150 name: "mytest",
151 host_supported: true,
152 srcs: ["test.cpp"],
153 gtest: false,
154 isolated: false,
155}
156`,
157 targets: []testBazelTarget{
158 {"cc_test", "mytest", AttrNameToString{
159 "gtest": "False",
Jingwen Chen537242c2022-08-24 11:53:27 +0000160 "local_includes": `["."]`,
161 "srcs": `["test.cpp"]`,
162 },
163 },
164 },
165 })
166}
Jingwen Chenfbff97a2022-09-16 02:32:03 +0000167
168func TestCcTest_TestOptions_Tags(t *testing.T) {
169 runCcTestTestCase(t, ccTestBp2buildTestCase{
170 description: "cc test with test_options.tags converted to tags",
171 blueprint: `
172cc_test {
173 name: "mytest",
174 host_supported: true,
175 srcs: ["test.cpp"],
176 test_options: { tags: ["no-remote"] },
177}
Spandan Das651203d2023-07-21 22:55:32 +0000178` + simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_main") +
179 simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest"),
Jingwen Chenfbff97a2022-09-16 02:32:03 +0000180 targets: []testBazelTarget{
181 {"cc_test", "mytest", AttrNameToString{
182 "tags": `["no-remote"]`,
183 "local_includes": `["."]`,
184 "srcs": `["test.cpp"]`,
185 "gtest": "True",
Spandan Das651203d2023-07-21 22:55:32 +0000186 "deps": `[
187 ":libgtest_main",
188 ":libgtest",
189 ]`,
Jingwen Chenfbff97a2022-09-16 02:32:03 +0000190 },
191 },
192 },
193 })
194}
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000195
196func TestCcTest_TestConfig(t *testing.T) {
197 runCcTestTestCase(t, ccTestBp2buildTestCase{
198 description: "cc test that sets a test_config",
199 filesystem: map[string]string{
200 "test_config.xml": "",
201 },
202 blueprint: `
203cc_test {
204 name: "mytest",
205 srcs: ["test.cpp"],
206 test_config: "test_config.xml",
207}
Spandan Das651203d2023-07-21 22:55:32 +0000208` + simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_main") +
209 simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest"),
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000210 targets: []testBazelTarget{
211 {"cc_test", "mytest", AttrNameToString{
212 "gtest": "True",
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000213 "local_includes": `["."]`,
214 "srcs": `["test.cpp"]`,
215 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
216 "test_config": `"test_config.xml"`,
Spandan Das651203d2023-07-21 22:55:32 +0000217 "deps": `[
218 ":libgtest_main",
219 ":libgtest",
220 ]`,
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000221 },
222 },
223 },
224 })
225}
226
227func TestCcTest_TestConfigAndroidTestXML(t *testing.T) {
228 runCcTestTestCase(t, ccTestBp2buildTestCase{
229 description: "cc test that defaults to test config AndroidTest.xml",
230 filesystem: map[string]string{
231 "AndroidTest.xml": "",
232 },
233 blueprint: `
234cc_test {
235 name: "mytest",
236 srcs: ["test.cpp"],
237}
Spandan Das651203d2023-07-21 22:55:32 +0000238` + simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_main") +
239 simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest"),
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000240 targets: []testBazelTarget{
241 {"cc_test", "mytest", AttrNameToString{
242 "gtest": "True",
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000243 "local_includes": `["."]`,
244 "srcs": `["test.cpp"]`,
245 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
246 "test_config": `"AndroidTest.xml"`,
Spandan Das651203d2023-07-21 22:55:32 +0000247 "deps": `[
248 ":libgtest_main",
249 ":libgtest",
250 ]`,
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000251 },
252 },
253 },
254 })
255}
256
257func TestCcTest_TestConfigTemplateOptions(t *testing.T) {
258 runCcTestTestCase(t, ccTestBp2buildTestCase{
259 description: "cc test that sets test config template attributes",
260 filesystem: map[string]string{
261 "test_config_template.xml": "",
262 },
263 blueprint: `
264cc_test {
265 name: "mytest",
266 srcs: ["test.cpp"],
267 test_config_template: "test_config_template.xml",
268 auto_gen_config: true,
Spandan Dasf5a86552023-07-22 03:16:53 +0000269 isolated: true,
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000270}
Spandan Dasd1cd3512023-07-22 02:19:42 +0000271` + simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_isolated_main") +
272 simpleModuleDoNotConvertBp2build("cc_library", "liblog"),
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000273 targets: []testBazelTarget{
274 {"cc_test", "mytest", AttrNameToString{
275 "auto_generate_test_config": "True",
276 "gtest": "True",
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000277 "local_includes": `["."]`,
278 "srcs": `["test.cpp"]`,
279 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
280 "template_configs": `[
281 "'<target_preparer class=\"com.android.tradefed.targetprep.RootTargetPreparer\">\\n <option name=\"force-root\" value=\"false\" />\\n </target_preparer>'",
282 "'<option name=\"not-shardable\" value=\"true\" />'",
283 ]`,
284 "template_install_base": `"/data/local/tmp"`,
285 "template_test_config": `"test_config_template.xml"`,
Spandan Dasd1cd3512023-07-22 02:19:42 +0000286 "deps": `[":libgtest_isolated_main"]`,
287 "dynamic_deps": `[":liblog"]`,
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000288 },
289 },
290 },
291 })
292}
Spandan Das651203d2023-07-21 22:55:32 +0000293
294func TestCcTest_WithExplicitGTestDepInAndroidBp(t *testing.T) {
295 runCcTestTestCase(t, ccTestBp2buildTestCase{
296 description: "cc test that lists libgtest in Android.bp should not have dups of libgtest in BUILD file",
297 blueprint: `
298cc_test {
299 name: "mytest",
300 srcs: ["test.cpp"],
301 static_libs: ["libgtest"],
302}
303` + simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_main") +
304 simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest"),
305 targets: []testBazelTarget{
306 {"cc_test", "mytest", AttrNameToString{
307 "gtest": "True",
Spandan Das651203d2023-07-21 22:55:32 +0000308 "local_includes": `["."]`,
309 "srcs": `["test.cpp"]`,
310 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
311 "deps": `[
312 ":libgtest",
313 ":libgtest_main",
314 ]`,
315 },
316 },
317 },
318 })
319
320}
Spandan Dasd1cd3512023-07-22 02:19:42 +0000321
322func TestCcTest_WithIsolatedTurnedOn(t *testing.T) {
323 runCcTestTestCase(t, ccTestBp2buildTestCase{
324 description: "cc test that sets `isolated: true` should run with ligtest_isolated_main instead of libgtest_main",
325 blueprint: `
326cc_test {
327 name: "mytest",
328 srcs: ["test.cpp"],
329 isolated: true,
330}
331` + simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_isolated_main") +
332 simpleModuleDoNotConvertBp2build("cc_library", "liblog"),
333 targets: []testBazelTarget{
334 {"cc_test", "mytest", AttrNameToString{
335 "gtest": "True",
336 "local_includes": `["."]`,
337 "srcs": `["test.cpp"]`,
338 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
339 "deps": `[":libgtest_isolated_main"]`,
340 "dynamic_deps": `[":liblog"]`,
341 },
342 },
343 },
344 })
345
346}