cc_test_conversion_test.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. package bp2build
  15. import (
  16. "fmt"
  17. "testing"
  18. "android/soong/android"
  19. "android/soong/cc"
  20. "android/soong/genrule"
  21. )
  22. type ccTestBp2buildTestCase struct {
  23. description string
  24. blueprint string
  25. filesystem map[string]string
  26. targets []testBazelTarget
  27. }
  28. func registerCcTestModuleTypes(ctx android.RegistrationContext) {
  29. cc.RegisterCCBuildComponents(ctx)
  30. ctx.RegisterModuleType("cc_binary", cc.BinaryFactory)
  31. ctx.RegisterModuleType("cc_library_static", cc.LibraryStaticFactory)
  32. ctx.RegisterModuleType("cc_library", cc.LibraryFactory)
  33. ctx.RegisterModuleType("cc_test_library", cc.TestLibraryFactory)
  34. ctx.RegisterModuleType("genrule", genrule.GenRuleFactory)
  35. }
  36. func runCcTestTestCase(t *testing.T, testCase ccTestBp2buildTestCase) {
  37. t.Helper()
  38. moduleTypeUnderTest := "cc_test"
  39. description := fmt.Sprintf("%s %s", moduleTypeUnderTest, testCase.description)
  40. t.Run(description, func(t *testing.T) {
  41. t.Helper()
  42. RunBp2BuildTestCase(t, registerCcTestModuleTypes, Bp2buildTestCase{
  43. ExpectedBazelTargets: generateBazelTargetsForTest(testCase.targets, android.HostAndDeviceSupported),
  44. Filesystem: testCase.filesystem,
  45. ModuleTypeUnderTest: moduleTypeUnderTest,
  46. ModuleTypeUnderTestFactory: cc.TestFactory,
  47. Description: description,
  48. Blueprint: testCase.blueprint,
  49. })
  50. })
  51. }
  52. func TestBasicCcTest(t *testing.T) {
  53. runCcTestTestCase(t, ccTestBp2buildTestCase{
  54. description: "basic cc_test with commonly used attributes",
  55. blueprint: `
  56. cc_test {
  57. name: "mytest",
  58. host_supported: true,
  59. srcs: ["test.cpp"],
  60. target: {
  61. android: {
  62. srcs: ["android.cpp"],
  63. shared_libs: ["foolib"],
  64. },
  65. linux: {
  66. srcs: ["linux.cpp"],
  67. },
  68. host: {
  69. static_libs: ["hostlib"],
  70. },
  71. },
  72. static_libs: ["cc_test_lib1"],
  73. shared_libs: ["cc_test_lib2"],
  74. data: [":data_mod", "file.txt"],
  75. data_bins: [":cc_bin"],
  76. data_libs: [":cc_lib"],
  77. cflags: ["-Wall"],
  78. }
  79. cc_test_library {
  80. name: "cc_test_lib1",
  81. host_supported: true,
  82. include_build_directory: false,
  83. }
  84. ` + simpleModuleDoNotConvertBp2build("cc_library", "foolib") +
  85. simpleModuleDoNotConvertBp2build("cc_library_static", "hostlib") +
  86. simpleModuleDoNotConvertBp2build("genrule", "data_mod") +
  87. simpleModuleDoNotConvertBp2build("cc_binary", "cc_bin") +
  88. simpleModuleDoNotConvertBp2build("cc_library", "cc_lib") +
  89. simpleModuleDoNotConvertBp2build("cc_test_library", "cc_test_lib2"),
  90. targets: []testBazelTarget{
  91. {"cc_library_shared", "cc_test_lib1", AttrNameToString{}},
  92. {"cc_library_static", "cc_test_lib1_bp2build_cc_library_static", AttrNameToString{}},
  93. {"cc_test", "mytest", AttrNameToString{
  94. "copts": `["-Wall"]`,
  95. "data": `[
  96. ":data_mod",
  97. "file.txt",
  98. ":cc_bin",
  99. ":cc_lib",
  100. ]`,
  101. "deps": `[":cc_test_lib1_bp2build_cc_library_static"] + select({
  102. "//build/bazel/platforms/os:darwin": [":hostlib"],
  103. "//build/bazel/platforms/os:linux_bionic": [":hostlib"],
  104. "//build/bazel/platforms/os:linux_glibc": [":hostlib"],
  105. "//build/bazel/platforms/os:linux_musl": [":hostlib"],
  106. "//build/bazel/platforms/os:windows": [":hostlib"],
  107. "//conditions:default": [],
  108. })`,
  109. "gtest": "True",
  110. "isolated": "True",
  111. "local_includes": `["."]`,
  112. "dynamic_deps": `[":cc_test_lib2"] + select({
  113. "//build/bazel/platforms/os:android": [":foolib"],
  114. "//conditions:default": [],
  115. })`,
  116. "srcs": `["test.cpp"] + select({
  117. "//build/bazel/platforms/os:android": [
  118. "linux.cpp",
  119. "android.cpp",
  120. ],
  121. "//build/bazel/platforms/os:linux_bionic": ["linux.cpp"],
  122. "//build/bazel/platforms/os:linux_glibc": ["linux.cpp"],
  123. "//build/bazel/platforms/os:linux_musl": ["linux.cpp"],
  124. "//conditions:default": [],
  125. })`,
  126. },
  127. },
  128. },
  129. })
  130. }
  131. func TestBasicCcTestGtestIsolatedDisabled(t *testing.T) {
  132. runCcTestTestCase(t, ccTestBp2buildTestCase{
  133. description: "cc test with disabled gtest and isolated props",
  134. blueprint: `
  135. cc_test {
  136. name: "mytest",
  137. host_supported: true,
  138. srcs: ["test.cpp"],
  139. gtest: false,
  140. isolated: false,
  141. }
  142. `,
  143. targets: []testBazelTarget{
  144. {"cc_test", "mytest", AttrNameToString{
  145. "gtest": "False",
  146. "isolated": "False",
  147. "local_includes": `["."]`,
  148. "srcs": `["test.cpp"]`,
  149. },
  150. },
  151. },
  152. })
  153. }
  154. func TestCcTest_TestOptions_Tags(t *testing.T) {
  155. runCcTestTestCase(t, ccTestBp2buildTestCase{
  156. description: "cc test with test_options.tags converted to tags",
  157. blueprint: `
  158. cc_test {
  159. name: "mytest",
  160. host_supported: true,
  161. srcs: ["test.cpp"],
  162. test_options: { tags: ["no-remote"] },
  163. }
  164. `,
  165. targets: []testBazelTarget{
  166. {"cc_test", "mytest", AttrNameToString{
  167. "tags": `["no-remote"]`,
  168. "local_includes": `["."]`,
  169. "srcs": `["test.cpp"]`,
  170. "gtest": "True",
  171. "isolated": "True",
  172. },
  173. },
  174. },
  175. })
  176. }
  177. func TestCcTest_TestConfig(t *testing.T) {
  178. runCcTestTestCase(t, ccTestBp2buildTestCase{
  179. description: "cc test that sets a test_config",
  180. filesystem: map[string]string{
  181. "test_config.xml": "",
  182. },
  183. blueprint: `
  184. cc_test {
  185. name: "mytest",
  186. srcs: ["test.cpp"],
  187. test_config: "test_config.xml",
  188. }
  189. `,
  190. targets: []testBazelTarget{
  191. {"cc_test", "mytest", AttrNameToString{
  192. "gtest": "True",
  193. "isolated": "True",
  194. "local_includes": `["."]`,
  195. "srcs": `["test.cpp"]`,
  196. "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
  197. "test_config": `"test_config.xml"`,
  198. },
  199. },
  200. },
  201. })
  202. }
  203. func TestCcTest_TestConfigAndroidTestXML(t *testing.T) {
  204. runCcTestTestCase(t, ccTestBp2buildTestCase{
  205. description: "cc test that defaults to test config AndroidTest.xml",
  206. filesystem: map[string]string{
  207. "AndroidTest.xml": "",
  208. },
  209. blueprint: `
  210. cc_test {
  211. name: "mytest",
  212. srcs: ["test.cpp"],
  213. }
  214. `,
  215. targets: []testBazelTarget{
  216. {"cc_test", "mytest", AttrNameToString{
  217. "gtest": "True",
  218. "isolated": "True",
  219. "local_includes": `["."]`,
  220. "srcs": `["test.cpp"]`,
  221. "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
  222. "test_config": `"AndroidTest.xml"`,
  223. },
  224. },
  225. },
  226. })
  227. }
  228. func TestCcTest_TestConfigTemplateOptions(t *testing.T) {
  229. runCcTestTestCase(t, ccTestBp2buildTestCase{
  230. description: "cc test that sets test config template attributes",
  231. filesystem: map[string]string{
  232. "test_config_template.xml": "",
  233. },
  234. blueprint: `
  235. cc_test {
  236. name: "mytest",
  237. srcs: ["test.cpp"],
  238. test_config_template: "test_config_template.xml",
  239. auto_gen_config: true,
  240. }
  241. `,
  242. targets: []testBazelTarget{
  243. {"cc_test", "mytest", AttrNameToString{
  244. "auto_generate_test_config": "True",
  245. "gtest": "True",
  246. "isolated": "True",
  247. "local_includes": `["."]`,
  248. "srcs": `["test.cpp"]`,
  249. "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
  250. "template_configs": `[
  251. "'<target_preparer class=\"com.android.tradefed.targetprep.RootTargetPreparer\">\\n <option name=\"force-root\" value=\"false\" />\\n </target_preparer>'",
  252. "'<option name=\"not-shardable\" value=\"true\" />'",
  253. ]`,
  254. "template_install_base": `"/data/local/tmp"`,
  255. "template_test_config": `"test_config_template.xml"`,
  256. },
  257. },
  258. },
  259. })
  260. }