2020-06-02 01:45:49 +08:00
|
|
|
package sh
|
2019-05-17 05:58:29 +08:00
|
|
|
|
|
|
|
import (
|
2020-06-02 01:45:49 +08:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2020-05-30 07:15:32 +08:00
|
|
|
"path/filepath"
|
2019-05-17 05:58:29 +08:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
2020-06-02 01:45:49 +08:00
|
|
|
|
|
|
|
"android/soong/android"
|
2020-05-30 07:15:32 +08:00
|
|
|
"android/soong/cc"
|
2019-05-17 05:58:29 +08:00
|
|
|
)
|
|
|
|
|
2020-06-02 01:45:49 +08:00
|
|
|
var buildDir string
|
|
|
|
|
|
|
|
func setUp() {
|
|
|
|
var err error
|
|
|
|
buildDir, err = ioutil.TempDir("", "soong_sh_test")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func tearDown() {
|
|
|
|
os.RemoveAll(buildDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
run := func() int {
|
|
|
|
setUp()
|
|
|
|
defer tearDown()
|
|
|
|
|
|
|
|
return m.Run()
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Exit(run())
|
|
|
|
}
|
|
|
|
|
|
|
|
func testShBinary(t *testing.T, bp string) (*android.TestContext, android.Config) {
|
2019-12-14 12:41:13 +08:00
|
|
|
fs := map[string][]byte{
|
2019-05-17 05:58:29 +08:00
|
|
|
"test.sh": nil,
|
|
|
|
"testdata/data1": nil,
|
|
|
|
"testdata/sub/data2": nil,
|
|
|
|
}
|
2019-12-14 12:41:13 +08:00
|
|
|
|
2020-06-02 01:45:49 +08:00
|
|
|
config := android.TestArchConfig(buildDir, nil, bp, fs)
|
2019-12-14 12:41:13 +08:00
|
|
|
|
2020-06-02 01:45:49 +08:00
|
|
|
ctx := android.NewTestArchContext()
|
2019-12-14 12:41:13 +08:00
|
|
|
ctx.RegisterModuleType("sh_test", ShTestFactory)
|
|
|
|
ctx.RegisterModuleType("sh_test_host", ShTestHostFactory)
|
2020-05-30 07:15:32 +08:00
|
|
|
|
|
|
|
cc.RegisterRequiredBuildComponentsForTest(ctx)
|
|
|
|
|
2019-12-14 12:41:13 +08:00
|
|
|
ctx.Register(config)
|
2019-05-17 05:58:29 +08:00
|
|
|
_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
|
2020-06-02 01:45:49 +08:00
|
|
|
android.FailIfErrored(t, errs)
|
2019-05-17 05:58:29 +08:00
|
|
|
_, errs = ctx.PrepareBuildActions(config)
|
2020-06-02 01:45:49 +08:00
|
|
|
android.FailIfErrored(t, errs)
|
2019-05-17 05:58:29 +08:00
|
|
|
|
|
|
|
return ctx, config
|
|
|
|
}
|
|
|
|
|
2020-06-11 08:23:46 +08:00
|
|
|
func TestShTestSubDir(t *testing.T) {
|
|
|
|
ctx, config := testShBinary(t, `
|
|
|
|
sh_test {
|
|
|
|
name: "foo",
|
|
|
|
src: "test.sh",
|
|
|
|
sub_dir: "foo_test"
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
|
|
|
|
mod := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*ShTest)
|
|
|
|
|
|
|
|
entries := android.AndroidMkEntriesForTest(t, config, "", mod)[0]
|
|
|
|
|
|
|
|
expectedPath := "/tmp/target/product/test_device/data/nativetest64/foo_test"
|
|
|
|
actualPath := entries.EntryMap["LOCAL_MODULE_PATH"][0]
|
|
|
|
if expectedPath != actualPath {
|
|
|
|
t.Errorf("Unexpected LOCAL_MODULE_PATH expected: %q, actual: %q", expectedPath, actualPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShTest(t *testing.T) {
|
2019-05-17 05:58:29 +08:00
|
|
|
ctx, config := testShBinary(t, `
|
|
|
|
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-06-02 01:45:49 +08:00
|
|
|
entries := android.AndroidMkEntriesForTest(t, config, "", mod)[0]
|
2020-06-11 08:23:46 +08:00
|
|
|
|
|
|
|
expectedPath := "/tmp/target/product/test_device/data/nativetest64/foo"
|
|
|
|
actualPath := entries.EntryMap["LOCAL_MODULE_PATH"][0]
|
|
|
|
if expectedPath != actualPath {
|
|
|
|
t.Errorf("Unexpected LOCAL_MODULE_PATH expected: %q, actual: %q", expectedPath, actualPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedData := []string{":testdata/data1", ":testdata/sub/data2"}
|
|
|
|
actualData := entries.EntryMap["LOCAL_TEST_DATA"]
|
|
|
|
if !reflect.DeepEqual(expectedData, actualData) {
|
|
|
|
t.Errorf("Unexpected test data expected: %q, actual: %q", expectedData, actualData)
|
2019-05-17 05:58:29 +08:00
|
|
|
}
|
|
|
|
}
|
2019-07-02 00:08:50 +08:00
|
|
|
|
2020-05-30 07:15:32 +08:00
|
|
|
func TestShTest_dataModules(t *testing.T) {
|
|
|
|
ctx, config := testShBinary(t, `
|
|
|
|
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",
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
|
|
|
|
buildOS := android.BuildOs.String()
|
|
|
|
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)
|
|
|
|
expectedInput := filepath.Join(buildDir, ".intermediates/libbar/"+arch+"_shared/libbar"+libExt)
|
|
|
|
if relocated.Input.String() != expectedInput {
|
|
|
|
t.Errorf("Unexpected relocation input, expected: %q, actual: %q",
|
|
|
|
expectedInput, relocated.Input.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
mod := variant.Module().(*ShTest)
|
|
|
|
entries := android.AndroidMkEntriesForTest(t, config, "", mod)[0]
|
|
|
|
expectedData := []string{
|
|
|
|
filepath.Join(buildDir, ".intermediates/bar", arch, ":bar"),
|
|
|
|
filepath.Join(buildDir, ".intermediates/foo", arch, "relocated/:lib64/libbar"+libExt),
|
|
|
|
}
|
|
|
|
actualData := entries.EntryMap["LOCAL_TEST_DATA"]
|
|
|
|
if !reflect.DeepEqual(expectedData, actualData) {
|
|
|
|
t.Errorf("Unexpected test data, expected: %q, actual: %q", expectedData, actualData)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-02 00:08:50 +08: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",
|
|
|
|
],
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
|
2020-06-02 01:45:49 +08:00
|
|
|
buildOS := android.BuildOs.String()
|
2019-07-02 00:08:50 +08: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.")
|
|
|
|
}
|
|
|
|
}
|
2020-05-30 07:15:32 +08:00
|
|
|
|
|
|
|
func TestShTestHost_dataDeviceModules(t *testing.T) {
|
|
|
|
ctx, config := testShBinary(t, `
|
|
|
|
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",
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
|
|
|
|
buildOS := android.BuildOs.String()
|
|
|
|
variant := ctx.ModuleForTests("foo", buildOS+"_x86_64")
|
|
|
|
|
|
|
|
relocated := variant.Output("relocated/lib64/libbar.so")
|
|
|
|
expectedInput := filepath.Join(buildDir, ".intermediates/libbar/android_arm64_armv8-a_shared/libbar.so")
|
|
|
|
if relocated.Input.String() != expectedInput {
|
|
|
|
t.Errorf("Unexpected relocation input, expected: %q, actual: %q",
|
|
|
|
expectedInput, relocated.Input.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
mod := variant.Module().(*ShTest)
|
|
|
|
entries := android.AndroidMkEntriesForTest(t, config, "", mod)[0]
|
|
|
|
expectedData := []string{
|
|
|
|
filepath.Join(buildDir, ".intermediates/bar/android_arm64_armv8-a/:bar"),
|
|
|
|
// libbar has been relocated, and so has a variant that matches the host arch.
|
|
|
|
filepath.Join(buildDir, ".intermediates/foo/"+buildOS+"_x86_64/relocated/:lib64/libbar.so"),
|
|
|
|
}
|
|
|
|
actualData := entries.EntryMap["LOCAL_TEST_DATA"]
|
|
|
|
if !reflect.DeepEqual(expectedData, actualData) {
|
|
|
|
t.Errorf("Unexpected test data, expected: %q, actual: %q", expectedData, actualData)
|
|
|
|
}
|
|
|
|
}
|