bzl_conversion_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // Copyright 2020 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. "io/ioutil"
  17. "os"
  18. "strings"
  19. "testing"
  20. "android/soong/android"
  21. )
  22. func setUp() {
  23. var err error
  24. buildDir, err = ioutil.TempDir("", "bazel_queryview_test")
  25. if err != nil {
  26. panic(err)
  27. }
  28. }
  29. func tearDown() {
  30. os.RemoveAll(buildDir)
  31. }
  32. func TestMain(m *testing.M) {
  33. run := func() int {
  34. setUp()
  35. defer tearDown()
  36. return m.Run()
  37. }
  38. os.Exit(run())
  39. }
  40. func TestGenerateModuleRuleShims(t *testing.T) {
  41. moduleTypeFactories := map[string]android.ModuleFactory{
  42. "custom": customModuleFactoryBase,
  43. "custom_test": customTestModuleFactoryBase,
  44. "custom_defaults": customDefaultsModuleFactoryBasic,
  45. }
  46. ruleShims := CreateRuleShims(moduleTypeFactories)
  47. if len(ruleShims) != 1 {
  48. t.Errorf("Expected to generate 1 rule shim, but got %d", len(ruleShims))
  49. }
  50. ruleShim := ruleShims["bp2build"]
  51. expectedRules := []string{
  52. "custom",
  53. "custom_defaults",
  54. "custom_test_",
  55. }
  56. if len(ruleShim.rules) != len(expectedRules) {
  57. t.Errorf("Expected %d rules, but got %d", len(expectedRules), len(ruleShim.rules))
  58. }
  59. for i, rule := range ruleShim.rules {
  60. if rule != expectedRules[i] {
  61. t.Errorf("Expected rule shim to contain %s, but got %s", expectedRules[i], rule)
  62. }
  63. }
  64. expectedBzl := `load("//build/bazel/queryview_rules:providers.bzl", "SoongModuleInfo")
  65. def _custom_impl(ctx):
  66. return [SoongModuleInfo()]
  67. custom = rule(
  68. implementation = _custom_impl,
  69. attrs = {
  70. "soong_module_name": attr.string(mandatory = True),
  71. "soong_module_variant": attr.string(),
  72. "soong_module_deps": attr.label_list(providers = [SoongModuleInfo]),
  73. "api": attr.string(),
  74. "arch_paths": attr.string_list(),
  75. "arch_paths_exclude": attr.string_list(),
  76. # bazel_module start
  77. # "label": attr.string(),
  78. # "bp2build_available": attr.bool(),
  79. # bazel_module end
  80. "bool_prop": attr.bool(),
  81. "bool_ptr_prop": attr.bool(),
  82. "embedded_prop": attr.string(),
  83. "int64_ptr_prop": attr.int(),
  84. # nested_props start
  85. # "nested_prop": attr.string(),
  86. # nested_props end
  87. # nested_props_ptr start
  88. # "nested_prop": attr.string(),
  89. # nested_props_ptr end
  90. "one_to_many_prop": attr.bool(),
  91. "other_embedded_prop": attr.string(),
  92. "string_list_prop": attr.string_list(),
  93. "string_literal_prop": attr.string(),
  94. "string_prop": attr.string(),
  95. "string_ptr_prop": attr.string(),
  96. "test_config_setting": attr.bool(),
  97. },
  98. )
  99. def _custom_defaults_impl(ctx):
  100. return [SoongModuleInfo()]
  101. custom_defaults = rule(
  102. implementation = _custom_defaults_impl,
  103. attrs = {
  104. "soong_module_name": attr.string(mandatory = True),
  105. "soong_module_variant": attr.string(),
  106. "soong_module_deps": attr.label_list(providers = [SoongModuleInfo]),
  107. "api": attr.string(),
  108. "arch_paths": attr.string_list(),
  109. "arch_paths_exclude": attr.string_list(),
  110. "bool_prop": attr.bool(),
  111. "bool_ptr_prop": attr.bool(),
  112. "embedded_prop": attr.string(),
  113. "int64_ptr_prop": attr.int(),
  114. # nested_props start
  115. # "nested_prop": attr.string(),
  116. # nested_props end
  117. # nested_props_ptr start
  118. # "nested_prop": attr.string(),
  119. # nested_props_ptr end
  120. "one_to_many_prop": attr.bool(),
  121. "other_embedded_prop": attr.string(),
  122. "string_list_prop": attr.string_list(),
  123. "string_literal_prop": attr.string(),
  124. "string_prop": attr.string(),
  125. "string_ptr_prop": attr.string(),
  126. "test_config_setting": attr.bool(),
  127. },
  128. )
  129. def _custom_test__impl(ctx):
  130. return [SoongModuleInfo()]
  131. custom_test_ = rule(
  132. implementation = _custom_test__impl,
  133. attrs = {
  134. "soong_module_name": attr.string(mandatory = True),
  135. "soong_module_variant": attr.string(),
  136. "soong_module_deps": attr.label_list(providers = [SoongModuleInfo]),
  137. "api": attr.string(),
  138. "arch_paths": attr.string_list(),
  139. "arch_paths_exclude": attr.string_list(),
  140. "bool_prop": attr.bool(),
  141. "bool_ptr_prop": attr.bool(),
  142. "embedded_prop": attr.string(),
  143. "int64_ptr_prop": attr.int(),
  144. # nested_props start
  145. # "nested_prop": attr.string(),
  146. # nested_props end
  147. # nested_props_ptr start
  148. # "nested_prop": attr.string(),
  149. # nested_props_ptr end
  150. "one_to_many_prop": attr.bool(),
  151. "other_embedded_prop": attr.string(),
  152. "string_list_prop": attr.string_list(),
  153. "string_literal_prop": attr.string(),
  154. "string_prop": attr.string(),
  155. "string_ptr_prop": attr.string(),
  156. "test_config_setting": attr.bool(),
  157. # test_prop start
  158. # "test_string_prop": attr.string(),
  159. # test_prop end
  160. },
  161. )
  162. `
  163. if ruleShim.content != expectedBzl {
  164. t.Errorf(
  165. "Expected the generated rule shim bzl to be:\n%s\nbut got:\n%s",
  166. expectedBzl,
  167. ruleShim.content)
  168. }
  169. }
  170. func TestGenerateSoongModuleBzl(t *testing.T) {
  171. ruleShims := map[string]RuleShim{
  172. "file1": RuleShim{
  173. rules: []string{"a", "b"},
  174. content: "irrelevant",
  175. },
  176. "file2": RuleShim{
  177. rules: []string{"c", "d"},
  178. content: "irrelevant",
  179. },
  180. }
  181. files := CreateBazelFiles(android.NullConfig("out", "out/soong"), ruleShims, make(map[string]BazelTargets), QueryView)
  182. var actualSoongModuleBzl BazelFile
  183. for _, f := range files {
  184. if f.Basename == "soong_module.bzl" {
  185. actualSoongModuleBzl = f
  186. }
  187. }
  188. expectedLoad := `load("//build/bazel/queryview_rules:file1.bzl", "a", "b")
  189. load("//build/bazel/queryview_rules:file2.bzl", "c", "d")
  190. `
  191. expectedRuleMap := `soong_module_rule_map = {
  192. "a": a,
  193. "b": b,
  194. "c": c,
  195. "d": d,
  196. }`
  197. if !strings.Contains(actualSoongModuleBzl.Contents, expectedLoad) {
  198. t.Errorf(
  199. "Generated soong_module.bzl:\n\n%s\n\n"+
  200. "Could not find the load statement in the generated soong_module.bzl:\n%s",
  201. actualSoongModuleBzl.Contents,
  202. expectedLoad)
  203. }
  204. if !strings.Contains(actualSoongModuleBzl.Contents, expectedRuleMap) {
  205. t.Errorf(
  206. "Generated soong_module.bzl:\n\n%s\n\n"+
  207. "Could not find the module -> rule map in the generated soong_module.bzl:\n%s",
  208. actualSoongModuleBzl.Contents,
  209. expectedRuleMap)
  210. }
  211. }