project_json_test.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 rust
  15. import (
  16. "encoding/json"
  17. "io/ioutil"
  18. "path/filepath"
  19. "sort"
  20. "strings"
  21. "testing"
  22. "android/soong/android"
  23. )
  24. // testProjectJson run the generation of rust-project.json. It returns the raw
  25. // content of the generated file.
  26. func testProjectJson(t *testing.T, bp string) []byte {
  27. result := android.GroupFixturePreparers(
  28. prepareForRustTest,
  29. android.FixtureMergeEnv(map[string]string{"SOONG_GEN_RUST_PROJECT": "1"}),
  30. ).RunTestWithBp(t, bp)
  31. // The JSON file is generated via WriteFileToOutputDir. Therefore, it
  32. // won't appear in the Output of the TestingSingleton. Manually verify
  33. // it exists.
  34. content, err := ioutil.ReadFile(filepath.Join(result.Config.BuildDir(), rustProjectJsonFileName))
  35. if err != nil {
  36. t.Errorf("rust-project.json has not been generated")
  37. }
  38. return content
  39. }
  40. // validateJsonCrates validates that content follows the basic structure of
  41. // rust-project.json. It returns the crates attribute if the validation
  42. // succeeded.
  43. // It uses an empty interface instead of relying on a defined structure to
  44. // avoid a strong dependency on our implementation.
  45. func validateJsonCrates(t *testing.T, rawContent []byte) []interface{} {
  46. var content interface{}
  47. err := json.Unmarshal(rawContent, &content)
  48. if err != nil {
  49. t.Errorf("Unable to parse the rust-project.json as JSON: %v", err)
  50. }
  51. root, ok := content.(map[string]interface{})
  52. if !ok {
  53. t.Errorf("Unexpected JSON format: %v", content)
  54. }
  55. if _, ok = root["crates"]; !ok {
  56. t.Errorf("No crates attribute in rust-project.json: %v", root)
  57. }
  58. crates, ok := root["crates"].([]interface{})
  59. if !ok {
  60. t.Errorf("Unexpected crates format: %v", root["crates"])
  61. }
  62. return crates
  63. }
  64. // validateCrate ensures that a crate can be parsed as a map.
  65. func validateCrate(t *testing.T, crate interface{}) map[string]interface{} {
  66. c, ok := crate.(map[string]interface{})
  67. if !ok {
  68. t.Fatalf("Unexpected type for crate: %v", c)
  69. }
  70. return c
  71. }
  72. // validateDependencies parses the dependencies for a crate. It returns a list
  73. // of the dependencies name.
  74. func validateDependencies(t *testing.T, crate map[string]interface{}) []string {
  75. var dependencies []string
  76. deps, ok := crate["deps"].([]interface{})
  77. if !ok {
  78. t.Errorf("Unexpected format for deps: %v", crate["deps"])
  79. }
  80. for _, dep := range deps {
  81. d, ok := dep.(map[string]interface{})
  82. if !ok {
  83. t.Errorf("Unexpected format for dependency: %v", dep)
  84. }
  85. name, ok := d["name"].(string)
  86. if !ok {
  87. t.Errorf("Dependency is missing the name key: %v", d)
  88. }
  89. dependencies = append(dependencies, name)
  90. }
  91. return dependencies
  92. }
  93. func TestProjectJsonDep(t *testing.T) {
  94. bp := `
  95. rust_library {
  96. name: "liba",
  97. srcs: ["a/src/lib.rs"],
  98. crate_name: "a"
  99. }
  100. rust_library {
  101. name: "libb",
  102. srcs: ["b/src/lib.rs"],
  103. crate_name: "b",
  104. rlibs: ["liba"],
  105. }
  106. `
  107. jsonContent := testProjectJson(t, bp)
  108. validateJsonCrates(t, jsonContent)
  109. }
  110. func TestProjectJsonFeature(t *testing.T) {
  111. bp := `
  112. rust_library {
  113. name: "liba",
  114. srcs: ["a/src/lib.rs"],
  115. crate_name: "a",
  116. features: ["f1", "f2"]
  117. }
  118. `
  119. jsonContent := testProjectJson(t, bp)
  120. crates := validateJsonCrates(t, jsonContent)
  121. for _, c := range crates {
  122. crate := validateCrate(t, c)
  123. cfgs, ok := crate["cfg"].([]interface{})
  124. if !ok {
  125. t.Fatalf("Unexpected type for cfgs: %v", crate)
  126. }
  127. expectedCfgs := []string{"feature=\"f1\"", "feature=\"f2\""}
  128. foundCfgs := []string{}
  129. for _, cfg := range cfgs {
  130. cfg, ok := cfg.(string)
  131. if !ok {
  132. t.Fatalf("Unexpected type for cfg: %v", cfg)
  133. }
  134. foundCfgs = append(foundCfgs, cfg)
  135. }
  136. sort.Strings(foundCfgs)
  137. for i, foundCfg := range foundCfgs {
  138. if foundCfg != expectedCfgs[i] {
  139. t.Errorf("Incorrect features: got %v; want %v", foundCfg, expectedCfgs[i])
  140. }
  141. }
  142. }
  143. }
  144. func TestProjectJsonBinary(t *testing.T) {
  145. bp := `
  146. rust_binary {
  147. name: "libz",
  148. srcs: ["z/src/lib.rs"],
  149. crate_name: "z"
  150. }
  151. `
  152. jsonContent := testProjectJson(t, bp)
  153. crates := validateJsonCrates(t, jsonContent)
  154. for _, c := range crates {
  155. crate := validateCrate(t, c)
  156. rootModule, ok := crate["root_module"].(string)
  157. if !ok {
  158. t.Fatalf("Unexpected type for root_module: %v", crate["root_module"])
  159. }
  160. if rootModule == "z/src/lib.rs" {
  161. return
  162. }
  163. }
  164. t.Errorf("Entry for binary %q not found: %s", "a", jsonContent)
  165. }
  166. func TestProjectJsonBindGen(t *testing.T) {
  167. buildOS := android.TestConfig(t.TempDir(), nil, "", nil).BuildOS
  168. bp := `
  169. rust_library {
  170. name: "libd",
  171. srcs: ["d/src/lib.rs"],
  172. rlibs: ["libbindings1"],
  173. crate_name: "d"
  174. }
  175. rust_bindgen {
  176. name: "libbindings1",
  177. crate_name: "bindings1",
  178. source_stem: "bindings1",
  179. host_supported: true,
  180. wrapper_src: "src/any.h",
  181. }
  182. rust_library_host {
  183. name: "libe",
  184. srcs: ["e/src/lib.rs"],
  185. rustlibs: ["libbindings2"],
  186. crate_name: "e"
  187. }
  188. rust_bindgen_host {
  189. name: "libbindings2",
  190. crate_name: "bindings2",
  191. source_stem: "bindings2",
  192. wrapper_src: "src/any.h",
  193. }
  194. `
  195. jsonContent := testProjectJson(t, bp)
  196. crates := validateJsonCrates(t, jsonContent)
  197. for _, c := range crates {
  198. crate := validateCrate(t, c)
  199. rootModule, ok := crate["root_module"].(string)
  200. if !ok {
  201. t.Fatalf("Unexpected type for root_module: %v", crate["root_module"])
  202. }
  203. if strings.Contains(rootModule, "libbindings1") && !strings.Contains(rootModule, "android_arm64") {
  204. t.Errorf("The source path for libbindings1 does not contain android_arm64, got %v", rootModule)
  205. }
  206. if strings.Contains(rootModule, "libbindings2") && !strings.Contains(rootModule, buildOS.String()) {
  207. t.Errorf("The source path for libbindings2 does not contain the BuildOs, got %v; want %v",
  208. rootModule, buildOS.String())
  209. }
  210. // Check that libbindings1 does not depend on itself.
  211. if strings.Contains(rootModule, "libbindings1") {
  212. for _, depName := range validateDependencies(t, crate) {
  213. if depName == "bindings1" {
  214. t.Errorf("libbindings1 depends on itself")
  215. }
  216. }
  217. }
  218. if strings.Contains(rootModule, "d/src/lib.rs") {
  219. // Check that libd depends on libbindings1
  220. found := false
  221. for _, depName := range validateDependencies(t, crate) {
  222. if depName == "bindings1" {
  223. found = true
  224. break
  225. }
  226. }
  227. if !found {
  228. t.Errorf("libd does not depend on libbindings1: %v", crate)
  229. }
  230. // Check that OUT_DIR is populated.
  231. env, ok := crate["env"].(map[string]interface{})
  232. if !ok {
  233. t.Errorf("libd does not have its environment variables set: %v", crate)
  234. }
  235. if _, ok = env["OUT_DIR"]; !ok {
  236. t.Errorf("libd does not have its OUT_DIR set: %v", env)
  237. }
  238. }
  239. }
  240. }
  241. func TestProjectJsonMultiVersion(t *testing.T) {
  242. bp := `
  243. rust_library {
  244. name: "liba1",
  245. srcs: ["a1/src/lib.rs"],
  246. crate_name: "a"
  247. }
  248. rust_library {
  249. name: "liba2",
  250. srcs: ["a2/src/lib.rs"],
  251. crate_name: "a",
  252. }
  253. rust_library {
  254. name: "libb",
  255. srcs: ["b/src/lib.rs"],
  256. crate_name: "b",
  257. rustlibs: ["liba1", "liba2"],
  258. }
  259. `
  260. jsonContent := testProjectJson(t, bp)
  261. crates := validateJsonCrates(t, jsonContent)
  262. for _, c := range crates {
  263. crate := validateCrate(t, c)
  264. rootModule, ok := crate["root_module"].(string)
  265. if !ok {
  266. t.Fatalf("Unexpected type for root_module: %v", crate["root_module"])
  267. }
  268. // Make sure that b has 2 different dependencies.
  269. if rootModule == "b/src/lib.rs" {
  270. aCount := 0
  271. deps := validateDependencies(t, crate)
  272. for _, depName := range deps {
  273. if depName == "a" {
  274. aCount++
  275. }
  276. }
  277. if aCount != 2 {
  278. t.Errorf("Unexpected number of liba dependencies want %v, got %v: %v", 2, aCount, deps)
  279. }
  280. return
  281. }
  282. }
  283. t.Errorf("libb crate has not been found: %v", crates)
  284. }