sh_binary_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. result := android.GroupFixturePreparers(
  39. prepareForShTest,
  40. android.FixtureModifyConfig(android.SetKatiEnabledForTests),
  41. ).RunTestWithBp(t, `
  42. sh_test {
  43. name: "foo",
  44. src: "test.sh",
  45. sub_dir: "foo_test"
  46. }
  47. `)
  48. mod := result.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*ShTest)
  49. entries := android.AndroidMkEntriesForTest(t, result.TestContext, mod)[0]
  50. expectedPath := "out/target/product/test_device/data/nativetest64/foo_test"
  51. actualPath := entries.EntryMap["LOCAL_MODULE_PATH"][0]
  52. android.AssertStringPathRelativeToTopEquals(t, "LOCAL_MODULE_PATH[0]", result.Config, expectedPath, actualPath)
  53. }
  54. func TestShTest(t *testing.T) {
  55. result := android.GroupFixturePreparers(
  56. prepareForShTest,
  57. android.FixtureModifyConfig(android.SetKatiEnabledForTests),
  58. ).RunTestWithBp(t, `
  59. sh_test {
  60. name: "foo",
  61. src: "test.sh",
  62. filename: "test.sh",
  63. data: [
  64. "testdata/data1",
  65. "testdata/sub/data2",
  66. ],
  67. }
  68. `)
  69. mod := result.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*ShTest)
  70. entries := android.AndroidMkEntriesForTest(t, result.TestContext, mod)[0]
  71. expectedPath := "out/target/product/test_device/data/nativetest64/foo"
  72. actualPath := entries.EntryMap["LOCAL_MODULE_PATH"][0]
  73. android.AssertStringPathRelativeToTopEquals(t, "LOCAL_MODULE_PATH[0]", result.Config, expectedPath, actualPath)
  74. expectedData := []string{":testdata/data1", ":testdata/sub/data2"}
  75. actualData := entries.EntryMap["LOCAL_TEST_DATA"]
  76. android.AssertDeepEquals(t, "LOCAL_TEST_DATA", expectedData, actualData)
  77. }
  78. func TestShTest_dataModules(t *testing.T) {
  79. ctx, config := testShBinary(t, `
  80. sh_test {
  81. name: "foo",
  82. src: "test.sh",
  83. host_supported: true,
  84. data_bins: ["bar"],
  85. data_libs: ["libbar"],
  86. }
  87. cc_binary {
  88. name: "bar",
  89. host_supported: true,
  90. shared_libs: ["libbar"],
  91. no_libcrt: true,
  92. nocrt: true,
  93. system_shared_libs: [],
  94. stl: "none",
  95. }
  96. cc_library {
  97. name: "libbar",
  98. host_supported: true,
  99. no_libcrt: true,
  100. nocrt: true,
  101. system_shared_libs: [],
  102. stl: "none",
  103. }
  104. `)
  105. buildOS := config.BuildOS.String()
  106. arches := []string{"android_arm64_armv8-a", buildOS + "_x86_64"}
  107. for _, arch := range arches {
  108. variant := ctx.ModuleForTests("foo", arch)
  109. libExt := ".so"
  110. if arch == "darwin_x86_64" {
  111. libExt = ".dylib"
  112. }
  113. relocated := variant.Output("relocated/lib64/libbar" + libExt)
  114. expectedInput := "out/soong/.intermediates/libbar/" + arch + "_shared/libbar" + libExt
  115. android.AssertPathRelativeToTopEquals(t, "relocation input", expectedInput, relocated.Input)
  116. mod := variant.Module().(*ShTest)
  117. entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
  118. expectedData := []string{
  119. filepath.Join("out/soong/.intermediates/bar", arch, ":bar"),
  120. filepath.Join("out/soong/.intermediates/foo", arch, "relocated/:lib64/libbar"+libExt),
  121. }
  122. actualData := entries.EntryMap["LOCAL_TEST_DATA"]
  123. android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_TEST_DATA", config, expectedData, actualData)
  124. }
  125. }
  126. func TestShTestHost(t *testing.T) {
  127. ctx, _ := testShBinary(t, `
  128. sh_test_host {
  129. name: "foo",
  130. src: "test.sh",
  131. filename: "test.sh",
  132. data: [
  133. "testdata/data1",
  134. "testdata/sub/data2",
  135. ],
  136. test_options: {
  137. unit_test: true,
  138. },
  139. }
  140. `)
  141. buildOS := ctx.Config().BuildOS.String()
  142. mod := ctx.ModuleForTests("foo", buildOS+"_x86_64").Module().(*ShTest)
  143. if !mod.Host() {
  144. t.Errorf("host bit is not set for a sh_test_host module.")
  145. }
  146. entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
  147. actualData, _ := strconv.ParseBool(entries.EntryMap["LOCAL_IS_UNIT_TEST"][0])
  148. android.AssertBoolEquals(t, "LOCAL_IS_UNIT_TEST", true, actualData)
  149. }
  150. func TestShTestHost_dataDeviceModules(t *testing.T) {
  151. ctx, config := testShBinary(t, `
  152. sh_test_host {
  153. name: "foo",
  154. src: "test.sh",
  155. data_device_bins: ["bar"],
  156. data_device_libs: ["libbar"],
  157. }
  158. cc_binary {
  159. name: "bar",
  160. shared_libs: ["libbar"],
  161. no_libcrt: true,
  162. nocrt: true,
  163. system_shared_libs: [],
  164. stl: "none",
  165. }
  166. cc_library {
  167. name: "libbar",
  168. no_libcrt: true,
  169. nocrt: true,
  170. system_shared_libs: [],
  171. stl: "none",
  172. }
  173. `)
  174. buildOS := config.BuildOS.String()
  175. variant := ctx.ModuleForTests("foo", buildOS+"_x86_64")
  176. relocated := variant.Output("relocated/lib64/libbar.so")
  177. expectedInput := "out/soong/.intermediates/libbar/android_arm64_armv8-a_shared/libbar.so"
  178. android.AssertPathRelativeToTopEquals(t, "relocation input", expectedInput, relocated.Input)
  179. mod := variant.Module().(*ShTest)
  180. entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
  181. expectedData := []string{
  182. "out/soong/.intermediates/bar/android_arm64_armv8-a/:bar",
  183. // libbar has been relocated, and so has a variant that matches the host arch.
  184. "out/soong/.intermediates/foo/" + buildOS + "_x86_64/relocated/:lib64/libbar.so",
  185. }
  186. actualData := entries.EntryMap["LOCAL_TEST_DATA"]
  187. android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_TEST_DATA", config, expectedData, actualData)
  188. }