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") + |
| 97 | simpleModuleDoNotConvertBp2build("cc_test_library", "cc_test_lib2"), |
Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 98 | targets: []testBazelTarget{ |
Liz Kammer | efc51d9 | 2023-04-21 15:11:25 -0400 | [diff] [blame] | 99 | {"cc_library_shared", "cc_test_lib1", AttrNameToString{}}, |
| 100 | {"cc_library_static", "cc_test_lib1_bp2build_cc_library_static", AttrNameToString{}}, |
Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 101 | {"cc_test", "mytest", AttrNameToString{ |
| 102 | "copts": `["-Wall"]`, |
| 103 | "data": `[ |
| 104 | ":data_mod", |
| 105 | "file.txt", |
| 106 | ":cc_bin", |
| 107 | ":cc_lib", |
| 108 | ]`, |
Liz Kammer | efc51d9 | 2023-04-21 15:11:25 -0400 | [diff] [blame] | 109 | "deps": `[":cc_test_lib1_bp2build_cc_library_static"] + select({ |
Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 110 | "//build/bazel/platforms/os:darwin": [":hostlib"], |
Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 111 | "//build/bazel/platforms/os:linux_bionic": [":hostlib"], |
Colin Cross | 133782e | 2022-12-20 15:29:31 -0800 | [diff] [blame] | 112 | "//build/bazel/platforms/os:linux_glibc": [":hostlib"], |
Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 113 | "//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 Kammer | efc51d9 | 2023-04-21 15:11:25 -0400 | [diff] [blame] | 120 | "dynamic_deps": `[":cc_test_lib2"] + select({ |
Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 121 | "//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 Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 129 | "//build/bazel/platforms/os:linux_bionic": ["linux.cpp"], |
Colin Cross | 133782e | 2022-12-20 15:29:31 -0800 | [diff] [blame] | 130 | "//build/bazel/platforms/os:linux_glibc": ["linux.cpp"], |
Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 131 | "//build/bazel/platforms/os:linux_musl": ["linux.cpp"], |
| 132 | "//conditions:default": [], |
| 133 | })`, |
| 134 | }, |
| 135 | }, |
| 136 | }, |
| 137 | }) |
| 138 | } |
| 139 | |
| 140 | func TestBasicCcTestGtestIsolatedDisabled(t *testing.T) { |
| 141 | runCcTestTestCase(t, ccTestBp2buildTestCase{ |
| 142 | description: "cc test with disabled gtest and isolated props", |
| 143 | blueprint: ` |
| 144 | cc_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 Chen | fbff97a | 2022-09-16 02:32:03 +0000 | [diff] [blame] | 163 | |
| 164 | func TestCcTest_TestOptions_Tags(t *testing.T) { |
| 165 | runCcTestTestCase(t, ccTestBp2buildTestCase{ |
| 166 | description: "cc test with test_options.tags converted to tags", |
| 167 | blueprint: ` |
| 168 | cc_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 Dagostino | 32edd1a | 2022-12-04 11:16:42 +0000 | [diff] [blame] | 187 | |
| 188 | func 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: ` |
| 195 | cc_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 | |
| 215 | func 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: ` |
| 222 | cc_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 | |
| 241 | func 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: ` |
| 248 | cc_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 | } |