conversion_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. "sort"
  17. "testing"
  18. "android/soong/android"
  19. )
  20. type bazelFilepath struct {
  21. dir string
  22. basename string
  23. }
  24. func TestCreateBazelFiles_QueryView_AddsTopLevelFiles(t *testing.T) {
  25. files := CreateBazelFiles(android.NullConfig("out", "out/soong"),
  26. map[string]RuleShim{}, map[string]BazelTargets{}, QueryView)
  27. expectedFilePaths := []bazelFilepath{
  28. {
  29. dir: "",
  30. basename: "BUILD.bazel",
  31. },
  32. {
  33. dir: "",
  34. basename: "WORKSPACE",
  35. },
  36. {
  37. dir: bazelRulesSubDir,
  38. basename: "BUILD.bazel",
  39. },
  40. {
  41. dir: bazelRulesSubDir,
  42. basename: "providers.bzl",
  43. },
  44. {
  45. dir: bazelRulesSubDir,
  46. basename: "soong_module.bzl",
  47. },
  48. }
  49. // Compare number of files
  50. if a, e := len(files), len(expectedFilePaths); a != e {
  51. t.Errorf("Expected %d files, got %d", e, a)
  52. }
  53. // Sort the files to be deterministic
  54. sort.Slice(files, func(i, j int) bool {
  55. if dir1, dir2 := files[i].Dir, files[j].Dir; dir1 == dir2 {
  56. return files[i].Basename < files[j].Basename
  57. } else {
  58. return dir1 < dir2
  59. }
  60. })
  61. // Compare the file contents
  62. for i := range files {
  63. actualFile, expectedFile := files[i], expectedFilePaths[i]
  64. if actualFile.Dir != expectedFile.dir || actualFile.Basename != expectedFile.basename {
  65. t.Errorf("Did not find expected file %s/%s", actualFile.Dir, actualFile.Basename)
  66. } else if actualFile.Basename == "BUILD.bazel" || actualFile.Basename == "WORKSPACE" {
  67. if actualFile.Contents != "" {
  68. t.Errorf("Expected %s to have no content.", actualFile)
  69. }
  70. } else if actualFile.Contents == "" {
  71. t.Errorf("Contents of %s unexpected empty.", actualFile)
  72. }
  73. }
  74. }
  75. func TestCreateBazelFiles_Bp2Build_CreatesDefaultFiles(t *testing.T) {
  76. testConfig := android.TestConfig("", make(map[string]string), "", make(map[string][]byte))
  77. files, err := soongInjectionFiles(testConfig, CreateCodegenMetrics())
  78. if err != nil {
  79. t.Error(err)
  80. }
  81. expectedFilePaths := []bazelFilepath{
  82. {
  83. dir: "android",
  84. basename: GeneratedBuildFileName,
  85. },
  86. {
  87. dir: "android",
  88. basename: "constants.bzl",
  89. },
  90. {
  91. dir: "cc_toolchain",
  92. basename: GeneratedBuildFileName,
  93. },
  94. {
  95. dir: "cc_toolchain",
  96. basename: "config_constants.bzl",
  97. },
  98. {
  99. dir: "cc_toolchain",
  100. basename: "sanitizer_constants.bzl",
  101. },
  102. {
  103. dir: "java_toolchain",
  104. basename: GeneratedBuildFileName,
  105. },
  106. {
  107. dir: "java_toolchain",
  108. basename: "constants.bzl",
  109. },
  110. {
  111. dir: "apex_toolchain",
  112. basename: GeneratedBuildFileName,
  113. },
  114. {
  115. dir: "apex_toolchain",
  116. basename: "constants.bzl",
  117. },
  118. {
  119. dir: "metrics",
  120. basename: "converted_modules.txt",
  121. },
  122. {
  123. dir: "metrics",
  124. basename: "BUILD.bazel",
  125. },
  126. {
  127. dir: "metrics",
  128. basename: "converted_modules_path_map.json",
  129. },
  130. {
  131. dir: "metrics",
  132. basename: "converted_modules_path_map.bzl",
  133. },
  134. {
  135. dir: "product_config",
  136. basename: "soong_config_variables.bzl",
  137. },
  138. {
  139. dir: "product_config",
  140. basename: "arch_configuration.bzl",
  141. },
  142. {
  143. dir: "api_levels",
  144. basename: GeneratedBuildFileName,
  145. },
  146. {
  147. dir: "api_levels",
  148. basename: "api_levels.json",
  149. },
  150. {
  151. dir: "api_levels",
  152. basename: "platform_versions.bzl",
  153. },
  154. {
  155. dir: "allowlists",
  156. basename: GeneratedBuildFileName,
  157. },
  158. {
  159. dir: "allowlists",
  160. basename: "env.bzl",
  161. },
  162. {
  163. dir: "allowlists",
  164. basename: "mixed_build_prod_allowlist.txt",
  165. },
  166. {
  167. dir: "allowlists",
  168. basename: "mixed_build_staging_allowlist.txt",
  169. },
  170. }
  171. if len(files) != len(expectedFilePaths) {
  172. t.Errorf("Expected %d file, got %d", len(expectedFilePaths), len(files))
  173. }
  174. for i := range files {
  175. actualFile, expectedFile := files[i], expectedFilePaths[i]
  176. if actualFile.Dir != expectedFile.dir || actualFile.Basename != expectedFile.basename {
  177. t.Errorf("Did not find expected file %s/%s", actualFile.Dir, actualFile.Basename)
  178. }
  179. }
  180. }