cc_test_conversion_test.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. data: [":data_mod", "file.txt"],
  73. data_bins: [":cc_bin"],
  74. data_libs: [":cc_lib"],
  75. cflags: ["-Wall"],
  76. }
  77. ` + simpleModuleDoNotConvertBp2build("cc_library", "foolib") +
  78. simpleModuleDoNotConvertBp2build("cc_library_static", "hostlib") +
  79. simpleModuleDoNotConvertBp2build("genrule", "data_mod") +
  80. simpleModuleDoNotConvertBp2build("cc_binary", "cc_bin") +
  81. simpleModuleDoNotConvertBp2build("cc_test_library", "cc_lib"),
  82. targets: []testBazelTarget{
  83. {"cc_test", "mytest", AttrNameToString{
  84. "copts": `["-Wall"]`,
  85. "data": `[
  86. ":data_mod",
  87. "file.txt",
  88. ":cc_bin",
  89. ":cc_lib",
  90. ]`,
  91. "deps": `select({
  92. "//build/bazel/platforms/os:darwin": [":hostlib"],
  93. "//build/bazel/platforms/os:linux": [":hostlib"],
  94. "//build/bazel/platforms/os:linux_bionic": [":hostlib"],
  95. "//build/bazel/platforms/os:linux_musl": [":hostlib"],
  96. "//build/bazel/platforms/os:windows": [":hostlib"],
  97. "//conditions:default": [],
  98. })`,
  99. "gtest": "True",
  100. "isolated": "True",
  101. "local_includes": `["."]`,
  102. "dynamic_deps": `select({
  103. "//build/bazel/platforms/os:android": [":foolib"],
  104. "//conditions:default": [],
  105. })`,
  106. "srcs": `["test.cpp"] + select({
  107. "//build/bazel/platforms/os:android": [
  108. "linux.cpp",
  109. "android.cpp",
  110. ],
  111. "//build/bazel/platforms/os:linux": ["linux.cpp"],
  112. "//build/bazel/platforms/os:linux_bionic": ["linux.cpp"],
  113. "//build/bazel/platforms/os:linux_musl": ["linux.cpp"],
  114. "//conditions:default": [],
  115. })`,
  116. },
  117. },
  118. },
  119. })
  120. }
  121. func TestBasicCcTestGtestIsolatedDisabled(t *testing.T) {
  122. runCcTestTestCase(t, ccTestBp2buildTestCase{
  123. description: "cc test with disabled gtest and isolated props",
  124. blueprint: `
  125. cc_test {
  126. name: "mytest",
  127. host_supported: true,
  128. srcs: ["test.cpp"],
  129. gtest: false,
  130. isolated: false,
  131. }
  132. `,
  133. targets: []testBazelTarget{
  134. {"cc_test", "mytest", AttrNameToString{
  135. "gtest": "False",
  136. "isolated": "False",
  137. "local_includes": `["."]`,
  138. "srcs": `["test.cpp"]`,
  139. },
  140. },
  141. },
  142. })
  143. }
  144. func TestCcTest_TestOptions_Tags(t *testing.T) {
  145. runCcTestTestCase(t, ccTestBp2buildTestCase{
  146. description: "cc test with test_options.tags converted to tags",
  147. blueprint: `
  148. cc_test {
  149. name: "mytest",
  150. host_supported: true,
  151. srcs: ["test.cpp"],
  152. test_options: { tags: ["no-remote"] },
  153. }
  154. `,
  155. targets: []testBazelTarget{
  156. {"cc_test", "mytest", AttrNameToString{
  157. "tags": `["no-remote"]`,
  158. "local_includes": `["."]`,
  159. "srcs": `["test.cpp"]`,
  160. "gtest": "True",
  161. "isolated": "True",
  162. },
  163. },
  164. },
  165. })
  166. }
  167. func TestCcTest_TestConfig(t *testing.T) {
  168. runCcTestTestCase(t, ccTestBp2buildTestCase{
  169. description: "cc test that sets a test_config",
  170. filesystem: map[string]string{
  171. "test_config.xml": "",
  172. },
  173. blueprint: `
  174. cc_test {
  175. name: "mytest",
  176. srcs: ["test.cpp"],
  177. test_config: "test_config.xml",
  178. }
  179. `,
  180. targets: []testBazelTarget{
  181. {"cc_test", "mytest", AttrNameToString{
  182. "gtest": "True",
  183. "isolated": "True",
  184. "local_includes": `["."]`,
  185. "srcs": `["test.cpp"]`,
  186. "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
  187. "test_config": `"test_config.xml"`,
  188. },
  189. },
  190. },
  191. })
  192. }
  193. func TestCcTest_TestConfigAndroidTestXML(t *testing.T) {
  194. runCcTestTestCase(t, ccTestBp2buildTestCase{
  195. description: "cc test that defaults to test config AndroidTest.xml",
  196. filesystem: map[string]string{
  197. "AndroidTest.xml": "",
  198. },
  199. blueprint: `
  200. cc_test {
  201. name: "mytest",
  202. srcs: ["test.cpp"],
  203. }
  204. `,
  205. targets: []testBazelTarget{
  206. {"cc_test", "mytest", AttrNameToString{
  207. "gtest": "True",
  208. "isolated": "True",
  209. "local_includes": `["."]`,
  210. "srcs": `["test.cpp"]`,
  211. "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
  212. "test_config": `"AndroidTest.xml"`,
  213. },
  214. },
  215. },
  216. })
  217. }
  218. func TestCcTest_TestConfigTemplateOptions(t *testing.T) {
  219. runCcTestTestCase(t, ccTestBp2buildTestCase{
  220. description: "cc test that sets test config template attributes",
  221. filesystem: map[string]string{
  222. "test_config_template.xml": "",
  223. },
  224. blueprint: `
  225. cc_test {
  226. name: "mytest",
  227. srcs: ["test.cpp"],
  228. test_config_template: "test_config_template.xml",
  229. auto_gen_config: true,
  230. }
  231. `,
  232. targets: []testBazelTarget{
  233. {"cc_test", "mytest", AttrNameToString{
  234. "auto_generate_test_config": "True",
  235. "gtest": "True",
  236. "isolated": "True",
  237. "local_includes": `["."]`,
  238. "srcs": `["test.cpp"]`,
  239. "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
  240. "template_configs": `[
  241. "'<target_preparer class=\"com.android.tradefed.targetprep.RootTargetPreparer\">\\n <option name=\"force-root\" value=\"false\" />\\n </target_preparer>'",
  242. "'<option name=\"not-shardable\" value=\"true\" />'",
  243. ]`,
  244. "template_install_base": `"/data/local/tmp"`,
  245. "template_test_config": `"test_config_template.xml"`,
  246. },
  247. },
  248. },
  249. })
  250. }