1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
|
package sh
import (
"io/ioutil"
"os"
"path/filepath"
"reflect"
"testing"
"android/soong/android"
"android/soong/cc"
)
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) {
fs := map[string][]byte{
"test.sh": nil,
"testdata/data1": nil,
"testdata/sub/data2": nil,
}
config := android.TestArchConfig(buildDir, nil, bp, fs)
ctx := android.NewTestArchContext()
ctx.RegisterModuleType("sh_test", ShTestFactory)
ctx.RegisterModuleType("sh_test_host", ShTestHostFactory)
cc.RegisterRequiredBuildComponentsForTest(ctx)
ctx.Register(config)
_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
android.FailIfErrored(t, errs)
_, errs = ctx.PrepareBuildActions(config)
android.FailIfErrored(t, errs)
return ctx, config
}
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) {
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)
entries := android.AndroidMkEntriesForTest(t, config, "", mod)[0]
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)
}
}
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)
}
}
}
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",
],
}
`)
buildOS := android.BuildOs.String()
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.")
}
}
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)
}
}
|