blob: 4df4d4d46fb7ad3e0bca5f3c52f67da8241af23a [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") +
97 simpleModuleDoNotConvertBp2build("cc_test_library", "cc_test_lib2"),
Jingwen Chen537242c2022-08-24 11:53:27 +000098 targets: []testBazelTarget{
Liz Kammerefc51d92023-04-21 15:11:25 -040099 {"cc_library_shared", "cc_test_lib1", AttrNameToString{}},
100 {"cc_library_static", "cc_test_lib1_bp2build_cc_library_static", AttrNameToString{}},
Jingwen Chen537242c2022-08-24 11:53:27 +0000101 {"cc_test", "mytest", AttrNameToString{
102 "copts": `["-Wall"]`,
103 "data": `[
104 ":data_mod",
105 "file.txt",
106 ":cc_bin",
107 ":cc_lib",
108 ]`,
Liz Kammerefc51d92023-04-21 15:11:25 -0400109 "deps": `[":cc_test_lib1_bp2build_cc_library_static"] + select({
Jingwen Chen537242c2022-08-24 11:53:27 +0000110 "//build/bazel/platforms/os:darwin": [":hostlib"],
Jingwen Chen537242c2022-08-24 11:53:27 +0000111 "//build/bazel/platforms/os:linux_bionic": [":hostlib"],
Colin Cross133782e2022-12-20 15:29:31 -0800112 "//build/bazel/platforms/os:linux_glibc": [":hostlib"],
Jingwen Chen537242c2022-08-24 11:53:27 +0000113 "//build/bazel/platforms/os:linux_musl": [":hostlib"],
114 "//build/bazel/platforms/os:windows": [":hostlib"],
115 "//conditions:default": [],
116 })`,
117 "gtest": "True",
118 "isolated": "True",
119 "local_includes": `["."]`,
Liz Kammerefc51d92023-04-21 15:11:25 -0400120 "dynamic_deps": `[":cc_test_lib2"] + select({
Jingwen Chen537242c2022-08-24 11:53:27 +0000121 "//build/bazel/platforms/os:android": [":foolib"],
122 "//conditions:default": [],
123 })`,
124 "srcs": `["test.cpp"] + select({
125 "//build/bazel/platforms/os:android": [
126 "linux.cpp",
127 "android.cpp",
128 ],
Jingwen Chen537242c2022-08-24 11:53:27 +0000129 "//build/bazel/platforms/os:linux_bionic": ["linux.cpp"],
Colin Cross133782e2022-12-20 15:29:31 -0800130 "//build/bazel/platforms/os:linux_glibc": ["linux.cpp"],
Jingwen Chen537242c2022-08-24 11:53:27 +0000131 "//build/bazel/platforms/os:linux_musl": ["linux.cpp"],
132 "//conditions:default": [],
133 })`,
134 },
135 },
136 },
137 })
138}
139
140func TestBasicCcTestGtestIsolatedDisabled(t *testing.T) {
141 runCcTestTestCase(t, ccTestBp2buildTestCase{
142 description: "cc test with disabled gtest and isolated props",
143 blueprint: `
144cc_test {
145 name: "mytest",
146 host_supported: true,
147 srcs: ["test.cpp"],
148 gtest: false,
149 isolated: false,
150}
151`,
152 targets: []testBazelTarget{
153 {"cc_test", "mytest", AttrNameToString{
154 "gtest": "False",
155 "isolated": "False",
156 "local_includes": `["."]`,
157 "srcs": `["test.cpp"]`,
158 },
159 },
160 },
161 })
162}
Jingwen Chenfbff97a2022-09-16 02:32:03 +0000163
164func TestCcTest_TestOptions_Tags(t *testing.T) {
165 runCcTestTestCase(t, ccTestBp2buildTestCase{
166 description: "cc test with test_options.tags converted to tags",
167 blueprint: `
168cc_test {
169 name: "mytest",
170 host_supported: true,
171 srcs: ["test.cpp"],
172 test_options: { tags: ["no-remote"] },
173}
174`,
175 targets: []testBazelTarget{
176 {"cc_test", "mytest", AttrNameToString{
177 "tags": `["no-remote"]`,
178 "local_includes": `["."]`,
179 "srcs": `["test.cpp"]`,
180 "gtest": "True",
181 "isolated": "True",
182 },
183 },
184 },
185 })
186}
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000187
188func TestCcTest_TestConfig(t *testing.T) {
189 runCcTestTestCase(t, ccTestBp2buildTestCase{
190 description: "cc test that sets a test_config",
191 filesystem: map[string]string{
192 "test_config.xml": "",
193 },
194 blueprint: `
195cc_test {
196 name: "mytest",
197 srcs: ["test.cpp"],
198 test_config: "test_config.xml",
199}
200`,
201 targets: []testBazelTarget{
202 {"cc_test", "mytest", AttrNameToString{
203 "gtest": "True",
204 "isolated": "True",
205 "local_includes": `["."]`,
206 "srcs": `["test.cpp"]`,
207 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
208 "test_config": `"test_config.xml"`,
209 },
210 },
211 },
212 })
213}
214
215func TestCcTest_TestConfigAndroidTestXML(t *testing.T) {
216 runCcTestTestCase(t, ccTestBp2buildTestCase{
217 description: "cc test that defaults to test config AndroidTest.xml",
218 filesystem: map[string]string{
219 "AndroidTest.xml": "",
220 },
221 blueprint: `
222cc_test {
223 name: "mytest",
224 srcs: ["test.cpp"],
225}
226`,
227 targets: []testBazelTarget{
228 {"cc_test", "mytest", AttrNameToString{
229 "gtest": "True",
230 "isolated": "True",
231 "local_includes": `["."]`,
232 "srcs": `["test.cpp"]`,
233 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
234 "test_config": `"AndroidTest.xml"`,
235 },
236 },
237 },
238 })
239}
240
241func TestCcTest_TestConfigTemplateOptions(t *testing.T) {
242 runCcTestTestCase(t, ccTestBp2buildTestCase{
243 description: "cc test that sets test config template attributes",
244 filesystem: map[string]string{
245 "test_config_template.xml": "",
246 },
247 blueprint: `
248cc_test {
249 name: "mytest",
250 srcs: ["test.cpp"],
251 test_config_template: "test_config_template.xml",
252 auto_gen_config: true,
253}
254`,
255 targets: []testBazelTarget{
256 {"cc_test", "mytest", AttrNameToString{
257 "auto_generate_test_config": "True",
258 "gtest": "True",
259 "isolated": "True",
260 "local_includes": `["."]`,
261 "srcs": `["test.cpp"]`,
262 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
263 "template_configs": `[
264 "'<target_preparer class=\"com.android.tradefed.targetprep.RootTargetPreparer\">\\n <option name=\"force-root\" value=\"false\" />\\n </target_preparer>'",
265 "'<option name=\"not-shardable\" value=\"true\" />'",
266 ]`,
267 "template_install_base": `"/data/local/tmp"`,
268 "template_test_config": `"test_config_template.xml"`,
269 },
270 },
271 },
272 })
273}