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 | })`, |
Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 123 | "local_includes": `["."]`, |
Liz Kammer | efc51d9 | 2023-04-21 15:11:25 -0400 | [diff] [blame] | 124 | "dynamic_deps": `[":cc_test_lib2"] + select({ |
Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 125 | "//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 Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 133 | "//build/bazel/platforms/os:linux_bionic": ["linux.cpp"], |
Colin Cross | 133782e | 2022-12-20 15:29:31 -0800 | [diff] [blame] | 134 | "//build/bazel/platforms/os:linux_glibc": ["linux.cpp"], |
Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 135 | "//build/bazel/platforms/os:linux_musl": ["linux.cpp"], |
| 136 | "//conditions:default": [], |
| 137 | })`, |
| 138 | }, |
| 139 | }, |
| 140 | }, |
| 141 | }) |
| 142 | } |
| 143 | |
| 144 | func TestBasicCcTestGtestIsolatedDisabled(t *testing.T) { |
| 145 | runCcTestTestCase(t, ccTestBp2buildTestCase{ |
| 146 | description: "cc test with disabled gtest and isolated props", |
| 147 | blueprint: ` |
| 148 | cc_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 Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 159 | "local_includes": `["."]`, |
| 160 | "srcs": `["test.cpp"]`, |
| 161 | }, |
| 162 | }, |
| 163 | }, |
| 164 | }) |
| 165 | } |
Jingwen Chen | fbff97a | 2022-09-16 02:32:03 +0000 | [diff] [blame] | 166 | |
| 167 | func TestCcTest_TestOptions_Tags(t *testing.T) { |
| 168 | runCcTestTestCase(t, ccTestBp2buildTestCase{ |
| 169 | description: "cc test with test_options.tags converted to tags", |
| 170 | blueprint: ` |
| 171 | cc_test { |
| 172 | name: "mytest", |
| 173 | host_supported: true, |
| 174 | srcs: ["test.cpp"], |
| 175 | test_options: { tags: ["no-remote"] }, |
| 176 | } |
Spandan Das | 651203d | 2023-07-21 22:55:32 +0000 | [diff] [blame] | 177 | ` + simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_main") + |
| 178 | simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest"), |
Jingwen Chen | fbff97a | 2022-09-16 02:32:03 +0000 | [diff] [blame] | 179 | targets: []testBazelTarget{ |
| 180 | {"cc_test", "mytest", AttrNameToString{ |
| 181 | "tags": `["no-remote"]`, |
| 182 | "local_includes": `["."]`, |
| 183 | "srcs": `["test.cpp"]`, |
Spandan Das | 651203d | 2023-07-21 22:55:32 +0000 | [diff] [blame] | 184 | "deps": `[ |
| 185 | ":libgtest_main", |
| 186 | ":libgtest", |
| 187 | ]`, |
Jingwen Chen | fbff97a | 2022-09-16 02:32:03 +0000 | [diff] [blame] | 188 | }, |
| 189 | }, |
| 190 | }, |
| 191 | }) |
| 192 | } |
Kevin Dagostino | 32edd1a | 2022-12-04 11:16:42 +0000 | [diff] [blame] | 193 | |
| 194 | func 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: ` |
| 201 | cc_test { |
| 202 | name: "mytest", |
| 203 | srcs: ["test.cpp"], |
| 204 | test_config: "test_config.xml", |
| 205 | } |
Spandan Das | 651203d | 2023-07-21 22:55:32 +0000 | [diff] [blame] | 206 | ` + simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_main") + |
| 207 | simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest"), |
Kevin Dagostino | 32edd1a | 2022-12-04 11:16:42 +0000 | [diff] [blame] | 208 | targets: []testBazelTarget{ |
| 209 | {"cc_test", "mytest", AttrNameToString{ |
Kevin Dagostino | 32edd1a | 2022-12-04 11:16:42 +0000 | [diff] [blame] | 210 | "local_includes": `["."]`, |
| 211 | "srcs": `["test.cpp"]`, |
| 212 | "target_compatible_with": `["//build/bazel/platforms/os:android"]`, |
| 213 | "test_config": `"test_config.xml"`, |
Spandan Das | 651203d | 2023-07-21 22:55:32 +0000 | [diff] [blame] | 214 | "deps": `[ |
| 215 | ":libgtest_main", |
| 216 | ":libgtest", |
| 217 | ]`, |
Kevin Dagostino | 32edd1a | 2022-12-04 11:16:42 +0000 | [diff] [blame] | 218 | }, |
| 219 | }, |
| 220 | }, |
| 221 | }) |
| 222 | } |
| 223 | |
| 224 | func 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: ` |
| 231 | cc_test { |
| 232 | name: "mytest", |
| 233 | srcs: ["test.cpp"], |
| 234 | } |
Spandan Das | 651203d | 2023-07-21 22:55:32 +0000 | [diff] [blame] | 235 | ` + simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_main") + |
| 236 | simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest"), |
Kevin Dagostino | 32edd1a | 2022-12-04 11:16:42 +0000 | [diff] [blame] | 237 | targets: []testBazelTarget{ |
| 238 | {"cc_test", "mytest", AttrNameToString{ |
Kevin Dagostino | 32edd1a | 2022-12-04 11:16:42 +0000 | [diff] [blame] | 239 | "local_includes": `["."]`, |
| 240 | "srcs": `["test.cpp"]`, |
| 241 | "target_compatible_with": `["//build/bazel/platforms/os:android"]`, |
| 242 | "test_config": `"AndroidTest.xml"`, |
Spandan Das | 651203d | 2023-07-21 22:55:32 +0000 | [diff] [blame] | 243 | "deps": `[ |
| 244 | ":libgtest_main", |
| 245 | ":libgtest", |
| 246 | ]`, |
Kevin Dagostino | 32edd1a | 2022-12-04 11:16:42 +0000 | [diff] [blame] | 247 | }, |
| 248 | }, |
| 249 | }, |
| 250 | }) |
| 251 | } |
| 252 | |
| 253 | func 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: ` |
| 260 | cc_test { |
| 261 | name: "mytest", |
| 262 | srcs: ["test.cpp"], |
| 263 | test_config_template: "test_config_template.xml", |
| 264 | auto_gen_config: true, |
Spandan Das | f5a8655 | 2023-07-22 03:16:53 +0000 | [diff] [blame] | 265 | isolated: true, |
Kevin Dagostino | 32edd1a | 2022-12-04 11:16:42 +0000 | [diff] [blame] | 266 | } |
Spandan Das | d1cd351 | 2023-07-22 02:19:42 +0000 | [diff] [blame] | 267 | ` + simpleModuleDoNotConvertBp2build("cc_library_static", "libgtest_isolated_main") + |
| 268 | simpleModuleDoNotConvertBp2build("cc_library", "liblog"), |
Kevin Dagostino | 32edd1a | 2022-12-04 11:16:42 +0000 | [diff] [blame] | 269 | targets: []testBazelTarget{ |
| 270 | {"cc_test", "mytest", AttrNameToString{ |
| 271 | "auto_generate_test_config": "True", |
Kevin Dagostino | 32edd1a | 2022-12-04 11:16:42 +0000 | [diff] [blame] | 272 | "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 Das | d1cd351 | 2023-07-22 02:19:42 +0000 | [diff] [blame] | 281 | "deps": `[":libgtest_isolated_main"]`, |
| 282 | "dynamic_deps": `[":liblog"]`, |
Kevin Dagostino | 32edd1a | 2022-12-04 11:16:42 +0000 | [diff] [blame] | 283 | }, |
| 284 | }, |
| 285 | }, |
| 286 | }) |
| 287 | } |
Spandan Das | 651203d | 2023-07-21 22:55:32 +0000 | [diff] [blame] | 288 | |
| 289 | func 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: ` |
| 293 | cc_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 Das | 651203d | 2023-07-21 22:55:32 +0000 | [diff] [blame] | 302 | "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 Das | d1cd351 | 2023-07-22 02:19:42 +0000 | [diff] [blame] | 315 | |
| 316 | func 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: ` |
| 320 | cc_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 Das | d1cd351 | 2023-07-22 02:19:42 +0000 | [diff] [blame] | 329 | "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 Das | e50fd11 | 2023-07-26 17:35:20 +0000 | [diff] [blame^] | 340 | |
| 341 | func 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: ` |
| 345 | cc_test { |
| 346 | name: "mytest_with_gtest", |
| 347 | gtest: true, |
| 348 | } |
| 349 | cc_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 | } |