test_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // Copyright 2019 The Android Open Source Project
  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. "strings"
  17. "testing"
  18. "android/soong/android"
  19. )
  20. func TestRustTest(t *testing.T) {
  21. ctx := testRust(t, `
  22. rust_test_host {
  23. name: "my_test",
  24. srcs: ["foo.rs"],
  25. data: ["data.txt"],
  26. }`)
  27. testingModule := ctx.ModuleForTests("my_test", "linux_glibc_x86_64")
  28. expectedOut := "my_test/linux_glibc_x86_64/my_test"
  29. outPath := testingModule.Output("my_test").Output.String()
  30. if !strings.Contains(outPath, expectedOut) {
  31. t.Errorf("wrong output path: %v; expected: %v", outPath, expectedOut)
  32. }
  33. dataPaths := testingModule.Module().(*Module).compiler.(*testDecorator).dataPaths()
  34. if len(dataPaths) != 1 {
  35. t.Errorf("expected exactly one test data file. test data files: [%s]", dataPaths)
  36. return
  37. }
  38. }
  39. func TestRustTestLinkage(t *testing.T) {
  40. ctx := testRust(t, `
  41. rust_test {
  42. name: "my_test",
  43. srcs: ["foo.rs"],
  44. rustlibs: ["libfoo"],
  45. rlibs: ["libbar"],
  46. }
  47. rust_library {
  48. name: "libfoo",
  49. srcs: ["foo.rs"],
  50. crate_name: "foo",
  51. }
  52. rust_library {
  53. name: "libbar",
  54. srcs: ["foo.rs"],
  55. crate_name: "bar",
  56. }`)
  57. testingModule := ctx.ModuleForTests("my_test", "android_arm64_armv8-a").Module().(*Module)
  58. if !android.InList("libfoo.rlib-std", testingModule.Properties.AndroidMkRlibs) {
  59. t.Errorf("rlib-std variant for libfoo not detected as a rustlib-defined rlib dependency for device rust_test module")
  60. }
  61. if !android.InList("libbar.rlib-std", testingModule.Properties.AndroidMkRlibs) {
  62. t.Errorf("rlib-std variant for libbar not detected as an rlib dependency for device rust_test module")
  63. }
  64. if !android.InList("libstd", testingModule.Properties.AndroidMkRlibs) {
  65. t.Errorf("Device rust_test module 'my_test' does not link libstd as an rlib")
  66. }
  67. }
  68. func TestDataLibs(t *testing.T) {
  69. bp := `
  70. cc_library {
  71. name: "test_lib",
  72. srcs: ["test_lib.cpp"],
  73. }
  74. rust_binary {
  75. name: "rusty",
  76. srcs: ["foo.rs"],
  77. compile_multilib: "both",
  78. }
  79. rust_ffi {
  80. name: "librust_test_lib",
  81. crate_name: "rust_test_lib",
  82. srcs: ["test_lib.rs"],
  83. relative_install_path: "foo/bar/baz",
  84. compile_multilib: "64",
  85. }
  86. rust_test {
  87. name: "main_test",
  88. srcs: ["foo.rs"],
  89. data_libs: ["test_lib"],
  90. data_bins: ["rusty"],
  91. }
  92. `
  93. ctx := testRust(t, bp)
  94. module := ctx.ModuleForTests("main_test", "android_arm64_armv8-a").Module()
  95. testBinary := module.(*Module).compiler.(*testDecorator)
  96. outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
  97. if err != nil {
  98. t.Fatalf("Expected rust_test to produce output files, error: %s", err)
  99. }
  100. if len(outputFiles) != 1 {
  101. t.Fatalf("expected exactly one output file. output files: [%s]", outputFiles)
  102. }
  103. if len(testBinary.dataPaths()) != 2 {
  104. t.Fatalf("expected exactly two test data files. test data files: [%s]", testBinary.dataPaths())
  105. }
  106. outputPath := outputFiles[0].String()
  107. dataLibraryPath := testBinary.dataPaths()[0].SrcPath.String()
  108. dataBinaryPath := testBinary.dataPaths()[1].SrcPath.String()
  109. if !strings.HasSuffix(outputPath, "/main_test") {
  110. t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
  111. }
  112. if !strings.HasSuffix(dataLibraryPath, "/test_lib.so") {
  113. t.Errorf("expected test data file to be 'test_lib.so', but was '%s'", dataLibraryPath)
  114. }
  115. if !strings.HasSuffix(dataBinaryPath, "/rusty") {
  116. t.Errorf("expected test data file to be 'test_lib.so', but was '%s'", dataBinaryPath)
  117. }
  118. }
  119. func TestDataLibsRelativeInstallPath(t *testing.T) {
  120. bp := `
  121. cc_library {
  122. name: "test_lib",
  123. srcs: ["test_lib.cpp"],
  124. relative_install_path: "foo/bar/baz",
  125. compile_multilib: "64",
  126. }
  127. rust_ffi {
  128. name: "librust_test_lib",
  129. crate_name: "rust_test_lib",
  130. srcs: ["test_lib.rs"],
  131. relative_install_path: "foo/bar/baz",
  132. compile_multilib: "64",
  133. }
  134. rust_binary {
  135. name: "rusty",
  136. srcs: ["foo.rs"],
  137. relative_install_path: "foo/bar/baz",
  138. compile_multilib: "64",
  139. }
  140. rust_test {
  141. name: "main_test",
  142. srcs: ["foo.rs"],
  143. data_libs: ["test_lib", "librust_test_lib"],
  144. data_bins: ["rusty"],
  145. compile_multilib: "64",
  146. }
  147. `
  148. ctx := testRust(t, bp)
  149. module := ctx.ModuleForTests("main_test", "android_arm64_armv8-a").Module()
  150. testBinary := module.(*Module).compiler.(*testDecorator)
  151. outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
  152. if err != nil {
  153. t.Fatalf("Expected rust_test to produce output files, error: %s", err)
  154. }
  155. if len(outputFiles) != 1 {
  156. t.Fatalf("expected exactly one output file. output files: [%s]", outputFiles)
  157. }
  158. if len(testBinary.dataPaths()) != 3 {
  159. t.Fatalf("expected exactly two test data files. test data files: [%s]", testBinary.dataPaths())
  160. }
  161. outputPath := outputFiles[0].String()
  162. if !strings.HasSuffix(outputPath, "/main_test") {
  163. t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
  164. }
  165. entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
  166. if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:lib64/foo/bar/baz") {
  167. t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:lib64/foo/bar/baz`,"+
  168. " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
  169. }
  170. if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][1], ":librust_test_lib.so:lib64/foo/bar/baz") {
  171. t.Errorf("expected LOCAL_TEST_DATA to end with `:librust_test_lib.so:lib64/foo/bar/baz`,"+
  172. " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][1])
  173. }
  174. if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][2], ":rusty:foo/bar/baz") {
  175. t.Errorf("expected LOCAL_TEST_DATA to end with `:rusty:foo/bar/baz`,"+
  176. " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][2])
  177. }
  178. }