2020-06-01 19:45:49 +02:00
|
|
|
package sh
|
2019-05-16 23:58:29 +02:00
|
|
|
|
|
|
|
import (
|
2020-06-01 19:45:49 +02:00
|
|
|
"os"
|
2020-05-30 01:15:32 +02:00
|
|
|
"path/filepath"
|
2021-05-24 21:04:54 +02:00
|
|
|
"strconv"
|
2019-05-16 23:58:29 +02:00
|
|
|
"testing"
|
2020-06-01 19:45:49 +02:00
|
|
|
|
|
|
|
"android/soong/android"
|
2020-05-30 01:15:32 +02:00
|
|
|
"android/soong/cc"
|
2019-05-16 23:58:29 +02:00
|
|
|
)
|
|
|
|
|
2020-06-01 19:45:49 +02:00
|
|
|
func TestMain(m *testing.M) {
|
2021-03-16 08:50:37 +01:00
|
|
|
os.Exit(m.Run())
|
2020-06-01 19:45:49 +02:00
|
|
|
}
|
|
|
|
|
2021-03-20 01:36:55 +01:00
|
|
|
var prepareForShTest = android.GroupFixturePreparers(
|
2021-03-08 16:05:52 +01:00
|
|
|
cc.PrepareForTestWithCcBuildComponents,
|
|
|
|
PrepareForTestWithShBuildComponents,
|
|
|
|
android.FixtureMergeMockFs(android.MockFS{
|
2019-05-16 23:58:29 +02:00
|
|
|
"test.sh": nil,
|
|
|
|
"testdata/data1": nil,
|
|
|
|
"testdata/sub/data2": nil,
|
2021-03-08 16:05:52 +01:00
|
|
|
}),
|
|
|
|
)
|
2020-05-30 01:15:32 +02:00
|
|
|
|
2021-03-20 01:36:55 +01:00
|
|
|
// testShBinary runs tests using the prepareForShTest
|
2021-03-08 16:05:52 +01:00
|
|
|
//
|
2021-03-20 01:36:55 +01:00
|
|
|
// Do not add any new usages of this, instead use the prepareForShTest directly as it makes it much
|
2021-03-08 16:05:52 +01:00
|
|
|
// easier to customize the test behavior.
|
|
|
|
//
|
|
|
|
// If it is necessary to customize the behavior of an existing test that uses this then please first
|
2021-03-20 01:36:55 +01:00
|
|
|
// convert the test to using prepareForShTest first and then in a following change add the
|
2021-03-08 16:05:52 +01:00
|
|
|
// appropriate fixture preparers. Keeping the conversion change separate makes it easy to verify
|
|
|
|
// that it did not change the test behavior unexpectedly.
|
|
|
|
//
|
|
|
|
// deprecated
|
|
|
|
func testShBinary(t *testing.T, bp string) (*android.TestContext, android.Config) {
|
2021-03-20 01:36:55 +01:00
|
|
|
result := prepareForShTest.RunTestWithBp(t, bp)
|
2019-05-16 23:58:29 +02:00
|
|
|
|
2021-03-08 16:05:52 +01:00
|
|
|
return result.TestContext, result.Config
|
2019-05-16 23:58:29 +02:00
|
|
|
}
|
|
|
|
|
2020-06-11 02:23:46 +02:00
|
|
|
func TestShTestSubDir(t *testing.T) {
|
2021-03-16 08:50:37 +01:00
|
|
|
ctx, config := testShBinary(t, `
|
2020-06-11 02:23:46 +02:00
|
|
|
sh_test {
|
|
|
|
name: "foo",
|
|
|
|
src: "test.sh",
|
|
|
|
sub_dir: "foo_test"
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
|
|
|
|
mod := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*ShTest)
|
|
|
|
|
2020-07-03 22:18:24 +02:00
|
|
|
entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
|
2020-06-11 02:23:46 +02:00
|
|
|
|
2021-03-16 08:50:37 +01:00
|
|
|
expectedPath := "out/target/product/test_device/data/nativetest64/foo_test"
|
2020-06-11 02:23:46 +02:00
|
|
|
actualPath := entries.EntryMap["LOCAL_MODULE_PATH"][0]
|
2021-03-16 08:50:37 +01:00
|
|
|
android.AssertStringPathRelativeToTopEquals(t, "LOCAL_MODULE_PATH[0]", config, expectedPath, actualPath)
|
2020-06-11 02:23:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestShTest(t *testing.T) {
|
2021-03-16 08:50:37 +01:00
|
|
|
ctx, config := testShBinary(t, `
|
2019-05-16 23:58:29 +02:00
|
|
|
sh_test {
|
|
|
|
name: "foo",
|
|
|
|
src: "test.sh",
|
|
|
|
filename: "test.sh",
|
|
|
|
data: [
|
|
|
|
"testdata/data1",
|
|
|
|
"testdata/sub/data2",
|
|
|
|
],
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
|
|
|
|
mod := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*ShTest)
|
|
|
|
|
2020-07-03 22:18:24 +02:00
|
|
|
entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
|
2020-06-11 02:23:46 +02:00
|
|
|
|
2021-03-16 08:50:37 +01:00
|
|
|
expectedPath := "out/target/product/test_device/data/nativetest64/foo"
|
2020-06-11 02:23:46 +02:00
|
|
|
actualPath := entries.EntryMap["LOCAL_MODULE_PATH"][0]
|
2021-03-16 08:50:37 +01:00
|
|
|
android.AssertStringPathRelativeToTopEquals(t, "LOCAL_MODULE_PATH[0]", config, expectedPath, actualPath)
|
2020-06-11 02:23:46 +02:00
|
|
|
|
|
|
|
expectedData := []string{":testdata/data1", ":testdata/sub/data2"}
|
|
|
|
actualData := entries.EntryMap["LOCAL_TEST_DATA"]
|
2021-03-16 08:50:37 +01:00
|
|
|
android.AssertDeepEquals(t, "LOCAL_TEST_DATA", expectedData, actualData)
|
2019-05-16 23:58:29 +02:00
|
|
|
}
|
2019-07-01 18:08:50 +02:00
|
|
|
|
2020-05-30 01:15:32 +02:00
|
|
|
func TestShTest_dataModules(t *testing.T) {
|
2021-03-16 08:50:37 +01:00
|
|
|
ctx, config := testShBinary(t, `
|
2020-05-30 01:15:32 +02:00
|
|
|
sh_test {
|
|
|
|
name: "foo",
|
|
|
|
src: "test.sh",
|
|
|
|
host_supported: true,
|
|
|
|
data_bins: ["bar"],
|
|
|
|
data_libs: ["libbar"],
|
|
|
|
}
|
|
|
|
|
|
|
|
cc_binary {
|
|
|
|
name: "bar",
|
|
|
|
host_supported: true,
|
|
|
|
shared_libs: ["libbar"],
|
|
|
|
no_libcrt: true,
|
|
|
|
nocrt: true,
|
|
|
|
system_shared_libs: [],
|
|
|
|
stl: "none",
|
|
|
|
}
|
|
|
|
|
|
|
|
cc_library {
|
|
|
|
name: "libbar",
|
|
|
|
host_supported: true,
|
|
|
|
no_libcrt: true,
|
|
|
|
nocrt: true,
|
|
|
|
system_shared_libs: [],
|
|
|
|
stl: "none",
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
|
2021-07-20 18:47:41 +02:00
|
|
|
buildOS := config.BuildOS.String()
|
2020-05-30 01:15:32 +02:00
|
|
|
arches := []string{"android_arm64_armv8-a", buildOS + "_x86_64"}
|
|
|
|
for _, arch := range arches {
|
|
|
|
variant := ctx.ModuleForTests("foo", arch)
|
|
|
|
|
|
|
|
libExt := ".so"
|
|
|
|
if arch == "darwin_x86_64" {
|
|
|
|
libExt = ".dylib"
|
|
|
|
}
|
|
|
|
relocated := variant.Output("relocated/lib64/libbar" + libExt)
|
2021-03-16 08:50:37 +01:00
|
|
|
expectedInput := "out/soong/.intermediates/libbar/" + arch + "_shared/libbar" + libExt
|
|
|
|
android.AssertPathRelativeToTopEquals(t, "relocation input", expectedInput, relocated.Input)
|
2020-05-30 01:15:32 +02:00
|
|
|
|
|
|
|
mod := variant.Module().(*ShTest)
|
2020-07-03 22:18:24 +02:00
|
|
|
entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
|
2020-05-30 01:15:32 +02:00
|
|
|
expectedData := []string{
|
2021-03-16 08:50:37 +01:00
|
|
|
filepath.Join("out/soong/.intermediates/bar", arch, ":bar"),
|
|
|
|
filepath.Join("out/soong/.intermediates/foo", arch, "relocated/:lib64/libbar"+libExt),
|
2020-05-30 01:15:32 +02:00
|
|
|
}
|
|
|
|
actualData := entries.EntryMap["LOCAL_TEST_DATA"]
|
2021-03-16 08:50:37 +01:00
|
|
|
android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_TEST_DATA", config, expectedData, actualData)
|
2020-05-30 01:15:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-01 18:08:50 +02:00
|
|
|
func TestShTestHost(t *testing.T) {
|
|
|
|
ctx, _ := testShBinary(t, `
|
|
|
|
sh_test_host {
|
|
|
|
name: "foo",
|
|
|
|
src: "test.sh",
|
|
|
|
filename: "test.sh",
|
|
|
|
data: [
|
|
|
|
"testdata/data1",
|
|
|
|
"testdata/sub/data2",
|
|
|
|
],
|
2021-05-24 21:04:54 +02:00
|
|
|
test_options: {
|
|
|
|
unit_test: true,
|
|
|
|
},
|
2019-07-01 18:08:50 +02:00
|
|
|
}
|
|
|
|
`)
|
|
|
|
|
2021-07-20 18:47:41 +02:00
|
|
|
buildOS := ctx.Config().BuildOS.String()
|
2019-07-01 18:08:50 +02:00
|
|
|
mod := ctx.ModuleForTests("foo", buildOS+"_x86_64").Module().(*ShTest)
|
|
|
|
if !mod.Host() {
|
|
|
|
t.Errorf("host bit is not set for a sh_test_host module.")
|
|
|
|
}
|
2021-05-24 21:04:54 +02:00
|
|
|
entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
|
|
|
|
actualData, _ := strconv.ParseBool(entries.EntryMap["LOCAL_IS_UNIT_TEST"][0])
|
|
|
|
android.AssertBoolEquals(t, "LOCAL_IS_UNIT_TEST", true, actualData)
|
2019-07-01 18:08:50 +02:00
|
|
|
}
|
2020-05-30 01:15:32 +02:00
|
|
|
|
|
|
|
func TestShTestHost_dataDeviceModules(t *testing.T) {
|
2021-03-16 08:50:37 +01:00
|
|
|
ctx, config := testShBinary(t, `
|
2020-05-30 01:15:32 +02:00
|
|
|
sh_test_host {
|
|
|
|
name: "foo",
|
|
|
|
src: "test.sh",
|
|
|
|
data_device_bins: ["bar"],
|
|
|
|
data_device_libs: ["libbar"],
|
|
|
|
}
|
|
|
|
|
|
|
|
cc_binary {
|
|
|
|
name: "bar",
|
|
|
|
shared_libs: ["libbar"],
|
|
|
|
no_libcrt: true,
|
|
|
|
nocrt: true,
|
|
|
|
system_shared_libs: [],
|
|
|
|
stl: "none",
|
|
|
|
}
|
|
|
|
|
|
|
|
cc_library {
|
|
|
|
name: "libbar",
|
|
|
|
no_libcrt: true,
|
|
|
|
nocrt: true,
|
|
|
|
system_shared_libs: [],
|
|
|
|
stl: "none",
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
|
2021-07-20 18:47:41 +02:00
|
|
|
buildOS := config.BuildOS.String()
|
2020-05-30 01:15:32 +02:00
|
|
|
variant := ctx.ModuleForTests("foo", buildOS+"_x86_64")
|
|
|
|
|
|
|
|
relocated := variant.Output("relocated/lib64/libbar.so")
|
2021-03-16 08:50:37 +01:00
|
|
|
expectedInput := "out/soong/.intermediates/libbar/android_arm64_armv8-a_shared/libbar.so"
|
|
|
|
android.AssertPathRelativeToTopEquals(t, "relocation input", expectedInput, relocated.Input)
|
2020-05-30 01:15:32 +02:00
|
|
|
|
|
|
|
mod := variant.Module().(*ShTest)
|
2020-07-03 22:18:24 +02:00
|
|
|
entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
|
2020-05-30 01:15:32 +02:00
|
|
|
expectedData := []string{
|
2021-03-16 08:50:37 +01:00
|
|
|
"out/soong/.intermediates/bar/android_arm64_armv8-a/:bar",
|
2020-05-30 01:15:32 +02:00
|
|
|
// libbar has been relocated, and so has a variant that matches the host arch.
|
2021-03-16 08:50:37 +01:00
|
|
|
"out/soong/.intermediates/foo/" + buildOS + "_x86_64/relocated/:lib64/libbar.so",
|
2020-05-30 01:15:32 +02:00
|
|
|
}
|
|
|
|
actualData := entries.EntryMap["LOCAL_TEST_DATA"]
|
2021-03-16 08:50:37 +01:00
|
|
|
android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_TEST_DATA", config, expectedData, actualData)
|
2020-05-30 01:15:32 +02:00
|
|
|
}
|