Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 1 | // 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 | |
| 15 | package bp2build |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "testing" |
| 20 | |
| 21 | "android/soong/android" |
| 22 | "android/soong/cc" |
| 23 | "android/soong/genrule" |
| 24 | ) |
| 25 | |
| 26 | type ccTestBp2buildTestCase struct { |
| 27 | description string |
| 28 | blueprint string |
Kevin Dagostino | 32edd1a | 2022-12-04 11:16:42 +0000 | [diff] [blame] | 29 | filesystem map[string]string |
Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 30 | targets []testBazelTarget |
| 31 | } |
| 32 | |
| 33 | func 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 | |
| 42 | func runCcTestTestCase(t *testing.T, testCase ccTestBp2buildTestCase) { |
| 43 | t.Helper() |
| 44 | moduleTypeUnderTest := "cc_test" |
Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 45 | 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 Dagostino | 32edd1a | 2022-12-04 11:16:42 +0000 | [diff] [blame] | 50 | Filesystem: testCase.filesystem, |
Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 51 | ModuleTypeUnderTest: moduleTypeUnderTest, |
| 52 | ModuleTypeUnderTestFactory: cc.TestFactory, |
| 53 | Description: description, |
| 54 | Blueprint: testCase.blueprint, |
| 55 | }) |
| 56 | }) |
| 57 | } |
| 58 | |
| 59 | func TestBasicCcTest(t *testing.T) { |
| 60 | runCcTestTestCase(t, ccTestBp2buildTestCase{ |
| 61 | description: "basic cc_test with commonly used attributes", |
| 62 | blueprint: ` |
| 63 | cc_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 Kammer | efc51d9 | 2023-04-21 15:11:25 -0400 | [diff] [blame] | 79 | static_libs: ["cc_test_lib1"], |
| 80 | shared_libs: ["cc_test_lib2"], |
Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 81 | data: [":data_mod", "file.txt"], |
| 82 | data_bins: [":cc_bin"], |
| 83 | data_libs: [":cc_lib"], |
| 84 | cflags: ["-Wall"], |
| 85 | } |
Liz Kammer | efc51d9 | 2023-04-21 15:11:25 -0400 | [diff] [blame] | 86 | |
| 87 | cc_test_library { |
| 88 | name: "cc_test_lib1", |
| 89 | host_supported: true, |
| 90 | include_build_directory: false, |
| 91 | } |
Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 92 | ` + simpleModuleDoNotConvertBp2build("cc_library", "foolib") + |
| 93 | simpleModuleDoNotConvertBp2build("cc_library_static", "hostlib") + |
| 94 | simpleModuleDoNotConvertBp2build("genrule", "data_mod") + |
| 95 | simpleModuleDoNotConvertBp2build("cc_binary", "cc_bin") + |
Liz Kammer | efc51d9 | 2023-04-21 15:11:25 -0400 | [diff] [blame] | 96 | simpleModuleDoNotConvertBp2build("cc_library", "cc_lib") + |
Spandan Das | 651203d | 2023-07-21 22:55:32 +0000 | [diff] [blame] | 97 | simpleModuleDoNotConvertBp2build("cc_test_library", "cc_test_lib2") + |
| 98 | simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_main") + |
| 99 | simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest"), |
Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 100 | targets: []testBazelTarget{ |
Liz Kammer | efc51d9 | 2023-04-21 15:11:25 -0400 | [diff] [blame] | 101 | {"cc_library_shared", "cc_test_lib1", AttrNameToString{}}, |
| 102 | {"cc_library_static", "cc_test_lib1_bp2build_cc_library_static", AttrNameToString{}}, |
Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 103 | {"cc_test", "mytest", AttrNameToString{ |
| 104 | "copts": `["-Wall"]`, |
| 105 | "data": `[ |
| 106 | ":data_mod", |
| 107 | "file.txt", |
| 108 | ":cc_bin", |
| 109 | ":cc_lib", |
| 110 | ]`, |
Spandan Das | 651203d | 2023-07-21 22:55:32 +0000 | [diff] [blame] | 111 | "deps": `[ |
| 112 | ":cc_test_lib1_bp2build_cc_library_static", |
| 113 | ":libgtest_main", |
| 114 | ":libgtest", |
| 115 | ] + select({ |
Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 116 | "//build/bazel/platforms/os:darwin": [":hostlib"], |
Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 117 | "//build/bazel/platforms/os:linux_bionic": [":hostlib"], |
Colin Cross | 133782e | 2022-12-20 15:29:31 -0800 | [diff] [blame] | 118 | "//build/bazel/platforms/os:linux_glibc": [":hostlib"], |
Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 119 | "//build/bazel/platforms/os:linux_musl": [":hostlib"], |
| 120 | "//build/bazel/platforms/os:windows": [":hostlib"], |
| 121 | "//conditions:default": [], |
| 122 | })`, |
| 123 | "gtest": "True", |
Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 124 | "local_includes": `["."]`, |
Liz Kammer | efc51d9 | 2023-04-21 15:11:25 -0400 | [diff] [blame] | 125 | "dynamic_deps": `[":cc_test_lib2"] + select({ |
Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 126 | "//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 Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 134 | "//build/bazel/platforms/os:linux_bionic": ["linux.cpp"], |
Colin Cross | 133782e | 2022-12-20 15:29:31 -0800 | [diff] [blame] | 135 | "//build/bazel/platforms/os:linux_glibc": ["linux.cpp"], |
Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 136 | "//build/bazel/platforms/os:linux_musl": ["linux.cpp"], |
| 137 | "//conditions:default": [], |
| 138 | })`, |
| 139 | }, |
| 140 | }, |
| 141 | }, |
| 142 | }) |
| 143 | } |
| 144 | |
| 145 | func TestBasicCcTestGtestIsolatedDisabled(t *testing.T) { |
| 146 | runCcTestTestCase(t, ccTestBp2buildTestCase{ |
| 147 | description: "cc test with disabled gtest and isolated props", |
| 148 | blueprint: ` |
| 149 | cc_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 Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 160 | "local_includes": `["."]`, |
| 161 | "srcs": `["test.cpp"]`, |
| 162 | }, |
| 163 | }, |
| 164 | }, |
| 165 | }) |
| 166 | } |
Jingwen Chen | fbff97a | 2022-09-16 02:32:03 +0000 | [diff] [blame] | 167 | |
| 168 | func TestCcTest_TestOptions_Tags(t *testing.T) { |
| 169 | runCcTestTestCase(t, ccTestBp2buildTestCase{ |
| 170 | description: "cc test with test_options.tags converted to tags", |
| 171 | blueprint: ` |
| 172 | cc_test { |
| 173 | name: "mytest", |
| 174 | host_supported: true, |
| 175 | srcs: ["test.cpp"], |
| 176 | test_options: { tags: ["no-remote"] }, |
| 177 | } |
Spandan Das | 651203d | 2023-07-21 22:55:32 +0000 | [diff] [blame] | 178 | ` + simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_main") + |
| 179 | simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest"), |
Jingwen Chen | fbff97a | 2022-09-16 02:32:03 +0000 | [diff] [blame] | 180 | targets: []testBazelTarget{ |
| 181 | {"cc_test", "mytest", AttrNameToString{ |
| 182 | "tags": `["no-remote"]`, |
| 183 | "local_includes": `["."]`, |
| 184 | "srcs": `["test.cpp"]`, |
| 185 | "gtest": "True", |
Spandan Das | 651203d | 2023-07-21 22:55:32 +0000 | [diff] [blame] | 186 | "deps": `[ |
| 187 | ":libgtest_main", |
| 188 | ":libgtest", |
| 189 | ]`, |
Jingwen Chen | fbff97a | 2022-09-16 02:32:03 +0000 | [diff] [blame] | 190 | }, |
| 191 | }, |
| 192 | }, |
| 193 | }) |
| 194 | } |
Kevin Dagostino | 32edd1a | 2022-12-04 11:16:42 +0000 | [diff] [blame] | 195 | |
| 196 | func 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: ` |
| 203 | cc_test { |
| 204 | name: "mytest", |
| 205 | srcs: ["test.cpp"], |
| 206 | test_config: "test_config.xml", |
| 207 | } |
Spandan Das | 651203d | 2023-07-21 22:55:32 +0000 | [diff] [blame] | 208 | ` + simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_main") + |
| 209 | simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest"), |
Kevin Dagostino | 32edd1a | 2022-12-04 11:16:42 +0000 | [diff] [blame] | 210 | targets: []testBazelTarget{ |
| 211 | {"cc_test", "mytest", AttrNameToString{ |
| 212 | "gtest": "True", |
Kevin Dagostino | 32edd1a | 2022-12-04 11:16:42 +0000 | [diff] [blame] | 213 | "local_includes": `["."]`, |
| 214 | "srcs": `["test.cpp"]`, |
| 215 | "target_compatible_with": `["//build/bazel/platforms/os:android"]`, |
| 216 | "test_config": `"test_config.xml"`, |
Spandan Das | 651203d | 2023-07-21 22:55:32 +0000 | [diff] [blame] | 217 | "deps": `[ |
| 218 | ":libgtest_main", |
| 219 | ":libgtest", |
| 220 | ]`, |
Kevin Dagostino | 32edd1a | 2022-12-04 11:16:42 +0000 | [diff] [blame] | 221 | }, |
| 222 | }, |
| 223 | }, |
| 224 | }) |
| 225 | } |
| 226 | |
| 227 | func 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: ` |
| 234 | cc_test { |
| 235 | name: "mytest", |
| 236 | srcs: ["test.cpp"], |
| 237 | } |
Spandan Das | 651203d | 2023-07-21 22:55:32 +0000 | [diff] [blame] | 238 | ` + simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_main") + |
| 239 | simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest"), |
Kevin Dagostino | 32edd1a | 2022-12-04 11:16:42 +0000 | [diff] [blame] | 240 | targets: []testBazelTarget{ |
| 241 | {"cc_test", "mytest", AttrNameToString{ |
| 242 | "gtest": "True", |
Kevin Dagostino | 32edd1a | 2022-12-04 11:16:42 +0000 | [diff] [blame] | 243 | "local_includes": `["."]`, |
| 244 | "srcs": `["test.cpp"]`, |
| 245 | "target_compatible_with": `["//build/bazel/platforms/os:android"]`, |
| 246 | "test_config": `"AndroidTest.xml"`, |
Spandan Das | 651203d | 2023-07-21 22:55:32 +0000 | [diff] [blame] | 247 | "deps": `[ |
| 248 | ":libgtest_main", |
| 249 | ":libgtest", |
| 250 | ]`, |
Kevin Dagostino | 32edd1a | 2022-12-04 11:16:42 +0000 | [diff] [blame] | 251 | }, |
| 252 | }, |
| 253 | }, |
| 254 | }) |
| 255 | } |
| 256 | |
| 257 | func 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: ` |
| 264 | cc_test { |
| 265 | name: "mytest", |
| 266 | srcs: ["test.cpp"], |
| 267 | test_config_template: "test_config_template.xml", |
| 268 | auto_gen_config: true, |
Spandan Das | f5a8655 | 2023-07-22 03:16:53 +0000 | [diff] [blame] | 269 | isolated: true, |
Kevin Dagostino | 32edd1a | 2022-12-04 11:16:42 +0000 | [diff] [blame] | 270 | } |
Spandan Das | d1cd351 | 2023-07-22 02:19:42 +0000 | [diff] [blame^] | 271 | ` + simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_isolated_main") + |
| 272 | simpleModuleDoNotConvertBp2build("cc_library", "liblog"), |
Kevin Dagostino | 32edd1a | 2022-12-04 11:16:42 +0000 | [diff] [blame] | 273 | targets: []testBazelTarget{ |
| 274 | {"cc_test", "mytest", AttrNameToString{ |
| 275 | "auto_generate_test_config": "True", |
| 276 | "gtest": "True", |
Kevin Dagostino | 32edd1a | 2022-12-04 11:16:42 +0000 | [diff] [blame] | 277 | "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 Das | d1cd351 | 2023-07-22 02:19:42 +0000 | [diff] [blame^] | 286 | "deps": `[":libgtest_isolated_main"]`, |
| 287 | "dynamic_deps": `[":liblog"]`, |
Kevin Dagostino | 32edd1a | 2022-12-04 11:16:42 +0000 | [diff] [blame] | 288 | }, |
| 289 | }, |
| 290 | }, |
| 291 | }) |
| 292 | } |
Spandan Das | 651203d | 2023-07-21 22:55:32 +0000 | [diff] [blame] | 293 | |
| 294 | func 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: ` |
| 298 | cc_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 Das | 651203d | 2023-07-21 22:55:32 +0000 | [diff] [blame] | 308 | "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 Das | d1cd351 | 2023-07-22 02:19:42 +0000 | [diff] [blame^] | 321 | |
| 322 | func 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: ` |
| 326 | cc_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 | } |