blob: 684fd03bdb9ddae56dd06ad9a2b98520346f7408 [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",
124 "isolated": "True",
125 "local_includes": `["."]`,
Liz Kammerefc51d92023-04-21 15:11:25 -0400126 "dynamic_deps": `[":cc_test_lib2"] + select({
Jingwen Chen537242c2022-08-24 11:53:27 +0000127 "//build/bazel/platforms/os:android": [":foolib"],
128 "//conditions:default": [],
129 })`,
130 "srcs": `["test.cpp"] + select({
131 "//build/bazel/platforms/os:android": [
132 "linux.cpp",
133 "android.cpp",
134 ],
Jingwen Chen537242c2022-08-24 11:53:27 +0000135 "//build/bazel/platforms/os:linux_bionic": ["linux.cpp"],
Colin Cross133782e2022-12-20 15:29:31 -0800136 "//build/bazel/platforms/os:linux_glibc": ["linux.cpp"],
Jingwen Chen537242c2022-08-24 11:53:27 +0000137 "//build/bazel/platforms/os:linux_musl": ["linux.cpp"],
138 "//conditions:default": [],
139 })`,
140 },
141 },
142 },
143 })
144}
145
146func TestBasicCcTestGtestIsolatedDisabled(t *testing.T) {
147 runCcTestTestCase(t, ccTestBp2buildTestCase{
148 description: "cc test with disabled gtest and isolated props",
149 blueprint: `
150cc_test {
151 name: "mytest",
152 host_supported: true,
153 srcs: ["test.cpp"],
154 gtest: false,
155 isolated: false,
156}
157`,
158 targets: []testBazelTarget{
159 {"cc_test", "mytest", AttrNameToString{
160 "gtest": "False",
161 "isolated": "False",
162 "local_includes": `["."]`,
163 "srcs": `["test.cpp"]`,
164 },
165 },
166 },
167 })
168}
Jingwen Chenfbff97a2022-09-16 02:32:03 +0000169
170func TestCcTest_TestOptions_Tags(t *testing.T) {
171 runCcTestTestCase(t, ccTestBp2buildTestCase{
172 description: "cc test with test_options.tags converted to tags",
173 blueprint: `
174cc_test {
175 name: "mytest",
176 host_supported: true,
177 srcs: ["test.cpp"],
178 test_options: { tags: ["no-remote"] },
179}
Spandan Das651203d2023-07-21 22:55:32 +0000180` + simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_main") +
181 simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest"),
Jingwen Chenfbff97a2022-09-16 02:32:03 +0000182 targets: []testBazelTarget{
183 {"cc_test", "mytest", AttrNameToString{
184 "tags": `["no-remote"]`,
185 "local_includes": `["."]`,
186 "srcs": `["test.cpp"]`,
187 "gtest": "True",
188 "isolated": "True",
Spandan Das651203d2023-07-21 22:55:32 +0000189 "deps": `[
190 ":libgtest_main",
191 ":libgtest",
192 ]`,
Jingwen Chenfbff97a2022-09-16 02:32:03 +0000193 },
194 },
195 },
196 })
197}
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000198
199func TestCcTest_TestConfig(t *testing.T) {
200 runCcTestTestCase(t, ccTestBp2buildTestCase{
201 description: "cc test that sets a test_config",
202 filesystem: map[string]string{
203 "test_config.xml": "",
204 },
205 blueprint: `
206cc_test {
207 name: "mytest",
208 srcs: ["test.cpp"],
209 test_config: "test_config.xml",
210}
Spandan Das651203d2023-07-21 22:55:32 +0000211` + simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_main") +
212 simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest"),
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000213 targets: []testBazelTarget{
214 {"cc_test", "mytest", AttrNameToString{
215 "gtest": "True",
216 "isolated": "True",
217 "local_includes": `["."]`,
218 "srcs": `["test.cpp"]`,
219 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
220 "test_config": `"test_config.xml"`,
Spandan Das651203d2023-07-21 22:55:32 +0000221 "deps": `[
222 ":libgtest_main",
223 ":libgtest",
224 ]`,
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000225 },
226 },
227 },
228 })
229}
230
231func TestCcTest_TestConfigAndroidTestXML(t *testing.T) {
232 runCcTestTestCase(t, ccTestBp2buildTestCase{
233 description: "cc test that defaults to test config AndroidTest.xml",
234 filesystem: map[string]string{
235 "AndroidTest.xml": "",
236 },
237 blueprint: `
238cc_test {
239 name: "mytest",
240 srcs: ["test.cpp"],
241}
Spandan Das651203d2023-07-21 22:55:32 +0000242` + simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_main") +
243 simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest"),
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000244 targets: []testBazelTarget{
245 {"cc_test", "mytest", AttrNameToString{
246 "gtest": "True",
247 "isolated": "True",
248 "local_includes": `["."]`,
249 "srcs": `["test.cpp"]`,
250 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
251 "test_config": `"AndroidTest.xml"`,
Spandan Das651203d2023-07-21 22:55:32 +0000252 "deps": `[
253 ":libgtest_main",
254 ":libgtest",
255 ]`,
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000256 },
257 },
258 },
259 })
260}
261
262func TestCcTest_TestConfigTemplateOptions(t *testing.T) {
263 runCcTestTestCase(t, ccTestBp2buildTestCase{
264 description: "cc test that sets test config template attributes",
265 filesystem: map[string]string{
266 "test_config_template.xml": "",
267 },
268 blueprint: `
269cc_test {
270 name: "mytest",
271 srcs: ["test.cpp"],
272 test_config_template: "test_config_template.xml",
273 auto_gen_config: true,
274}
Spandan Das651203d2023-07-21 22:55:32 +0000275` + simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_main") +
276 simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest"),
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000277 targets: []testBazelTarget{
278 {"cc_test", "mytest", AttrNameToString{
279 "auto_generate_test_config": "True",
280 "gtest": "True",
281 "isolated": "True",
282 "local_includes": `["."]`,
283 "srcs": `["test.cpp"]`,
284 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
285 "template_configs": `[
286 "'<target_preparer class=\"com.android.tradefed.targetprep.RootTargetPreparer\">\\n <option name=\"force-root\" value=\"false\" />\\n </target_preparer>'",
287 "'<option name=\"not-shardable\" value=\"true\" />'",
288 ]`,
289 "template_install_base": `"/data/local/tmp"`,
290 "template_test_config": `"test_config_template.xml"`,
Spandan Das651203d2023-07-21 22:55:32 +0000291 "deps": `[
292 ":libgtest_main",
293 ":libgtest",
294 ]`,
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000295 },
296 },
297 },
298 })
299}
Spandan Das651203d2023-07-21 22:55:32 +0000300
301func TestCcTest_WithExplicitGTestDepInAndroidBp(t *testing.T) {
302 runCcTestTestCase(t, ccTestBp2buildTestCase{
303 description: "cc test that lists libgtest in Android.bp should not have dups of libgtest in BUILD file",
304 blueprint: `
305cc_test {
306 name: "mytest",
307 srcs: ["test.cpp"],
308 static_libs: ["libgtest"],
309}
310` + simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_main") +
311 simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest"),
312 targets: []testBazelTarget{
313 {"cc_test", "mytest", AttrNameToString{
314 "gtest": "True",
315 "isolated": "True",
316 "local_includes": `["."]`,
317 "srcs": `["test.cpp"]`,
318 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
319 "deps": `[
320 ":libgtest",
321 ":libgtest_main",
322 ]`,
323 },
324 },
325 },
326 })
327
328}