sh_binary_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package sh
  2. import (
  3. "os"
  4. "path/filepath"
  5. "strconv"
  6. "testing"
  7. "android/soong/android"
  8. "android/soong/cc"
  9. )
  10. func TestMain(m *testing.M) {
  11. os.Exit(m.Run())
  12. }
  13. var prepareForShTest = android.GroupFixturePreparers(
  14. cc.PrepareForTestWithCcBuildComponents,
  15. PrepareForTestWithShBuildComponents,
  16. android.FixtureMergeMockFs(android.MockFS{
  17. "test.sh": nil,
  18. "testdata/data1": nil,
  19. "testdata/sub/data2": nil,
  20. }),
  21. )
  22. // testShBinary runs tests using the prepareForShTest
  23. //
  24. // Do not add any new usages of this, instead use the prepareForShTest directly as it makes it much
  25. // easier to customize the test behavior.
  26. //
  27. // If it is necessary to customize the behavior of an existing test that uses this then please first
  28. // convert the test to using prepareForShTest first and then in a following change add the
  29. // appropriate fixture preparers. Keeping the conversion change separate makes it easy to verify
  30. // that it did not change the test behavior unexpectedly.
  31. //
  32. // deprecated
  33. func testShBinary(t *testing.T, bp string) (*android.TestContext, android.Config) {
  34. result := prepareForShTest.RunTestWithBp(t, bp)
  35. return result.TestContext, result.Config
  36. }
  37. func TestShTestSubDir(t *testing.T) {
  38. ctx, config := testShBinary(t, `
  39. sh_test {
  40. name: "foo",
  41. src: "test.sh",
  42. sub_dir: "foo_test"
  43. }
  44. `)
  45. mod := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*ShTest)
  46. entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
  47. expectedPath := "out/target/product/test_device/data/nativetest64/foo_test"
  48. actualPath := entries.EntryMap["LOCAL_MODULE_PATH"][0]
  49. android.AssertStringPathRelativeToTopEquals(t, "LOCAL_MODULE_PATH[0]", config, expectedPath, actualPath)
  50. }
  51. func TestShTest(t *testing.T) {
  52. ctx, config := testShBinary(t, `
  53. sh_test {
  54. name: "foo",
  55. src: "test.sh",
  56. filename: "test.sh",
  57. data: [
  58. "testdata/data1",
  59. "testdata/sub/data2",
  60. ],
  61. }
  62. `)
  63. mod := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*ShTest)
  64. entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
  65. expectedPath := "out/target/product/test_device/data/nativetest64/foo"
  66. actualPath := entries.EntryMap["LOCAL_MODULE_PATH"][0]
  67. android.AssertStringPathRelativeToTopEquals(t, "LOCAL_MODULE_PATH[0]", config, expectedPath, actualPath)
  68. expectedData := []string{":testdata/data1", ":testdata/sub/data2"}
  69. actualData := entries.EntryMap["LOCAL_TEST_DATA"]
  70. android.AssertDeepEquals(t, "LOCAL_TEST_DATA", expectedData, actualData)
  71. }
  72. func TestShTest_dataModules(t *testing.T) {
  73. ctx, config := testShBinary(t, `
  74. sh_test {
  75. name: "foo",
  76. src: "test.sh",
  77. host_supported: true,
  78. data_bins: ["bar"],
  79. data_libs: ["libbar"],
  80. }
  81. cc_binary {
  82. name: "bar",
  83. host_supported: true,
  84. shared_libs: ["libbar"],
  85. no_libcrt: true,
  86. nocrt: true,
  87. system_shared_libs: [],
  88. stl: "none",
  89. }
  90. cc_library {
  91. name: "libbar",
  92. host_supported: true,
  93. no_libcrt: true,
  94. nocrt: true,
  95. system_shared_libs: [],
  96. stl: "none",
  97. }
  98. `)
  99. buildOS := config.BuildOS.String()
  100. arches := []string{"android_arm64_armv8-a", buildOS + "_x86_64"}
  101. for _, arch := range arches {
  102. variant := ctx.ModuleForTests("foo", arch)
  103. libExt := ".so"
  104. if arch == "darwin_x86_64" {
  105. libExt = ".dylib"
  106. }
  107. relocated := variant.Output("relocated/lib64/libbar" + libExt)
  108. expectedInput := "out/soong/.intermediates/libbar/" + arch + "_shared/libbar" + libExt
  109. android.AssertPathRelativeToTopEquals(t, "relocation input", expectedInput, relocated.Input)
  110. mod := variant.Module().(*ShTest)
  111. entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
  112. expectedData := []string{
  113. filepath.Join("out/soong/.intermediates/bar", arch, ":bar"),
  114. filepath.Join("out/soong/.intermediates/foo", arch, "relocated/:lib64/libbar"+libExt),
  115. }
  116. actualData := entries.EntryMap["LOCAL_TEST_DATA"]
  117. android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_TEST_DATA", config, expectedData, actualData)
  118. }
  119. }
  120. func TestShTestHost(t *testing.T) {
  121. ctx, _ := testShBinary(t, `
  122. sh_test_host {
  123. name: "foo",
  124. src: "test.sh",
  125. filename: "test.sh",
  126. data: [
  127. "testdata/data1",
  128. "testdata/sub/data2",
  129. ],
  130. test_options: {
  131. unit_test: true,
  132. },
  133. }
  134. `)
  135. buildOS := ctx.Config().BuildOS.String()
  136. mod := ctx.ModuleForTests("foo", buildOS+"_x86_64").Module().(*ShTest)
  137. if !mod.Host() {
  138. t.Errorf("host bit is not set for a sh_test_host module.")
  139. }
  140. entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
  141. actualData, _ := strconv.ParseBool(entries.EntryMap["LOCAL_IS_UNIT_TEST"][0])
  142. android.AssertBoolEquals(t, "LOCAL_IS_UNIT_TEST", true, actualData)
  143. }
  144. func TestShTestHost_dataDeviceModules(t *testing.T) {
  145. ctx, config := testShBinary(t, `
  146. sh_test_host {
  147. name: "foo",
  148. src: "test.sh",
  149. data_device_bins: ["bar"],
  150. data_device_libs: ["libbar"],
  151. }
  152. cc_binary {
  153. name: "bar",
  154. shared_libs: ["libbar"],
  155. no_libcrt: true,
  156. nocrt: true,
  157. system_shared_libs: [],
  158. stl: "none",
  159. }
  160. cc_library {
  161. name: "libbar",
  162. no_libcrt: true,
  163. nocrt: true,
  164. system_shared_libs: [],
  165. stl: "none",
  166. }
  167. `)
  168. buildOS := config.BuildOS.String()
  169. variant := ctx.ModuleForTests("foo", buildOS+"_x86_64")
  170. relocated := variant.Output("relocated/lib64/libbar.so")
  171. expectedInput := "out/soong/.intermediates/libbar/android_arm64_armv8-a_shared/libbar.so"
  172. android.AssertPathRelativeToTopEquals(t, "relocation input", expectedInput, relocated.Input)
  173. mod := variant.Module().(*ShTest)
  174. entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
  175. expectedData := []string{
  176. "out/soong/.intermediates/bar/android_arm64_armv8-a/:bar",
  177. // libbar has been relocated, and so has a variant that matches the host arch.
  178. "out/soong/.intermediates/foo/" + buildOS + "_x86_64/relocated/:lib64/libbar.so",
  179. }
  180. actualData := entries.EntryMap["LOCAL_TEST_DATA"]
  181. android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_TEST_DATA", config, expectedData, actualData)
  182. }