1package sh 2 3import ( 4 "io/ioutil" 5 "os" 6 "reflect" 7 "testing" 8 9 "android/soong/android" 10) 11 12var buildDir string 13 14func setUp() { 15 var err error 16 buildDir, err = ioutil.TempDir("", "soong_sh_test") 17 if err != nil { 18 panic(err) 19 } 20} 21 22func tearDown() { 23 os.RemoveAll(buildDir) 24} 25 26func TestMain(m *testing.M) { 27 run := func() int { 28 setUp() 29 defer tearDown() 30 31 return m.Run() 32 } 33 34 os.Exit(run()) 35} 36 37func testShBinary(t *testing.T, bp string) (*android.TestContext, android.Config) { 38 fs := map[string][]byte{ 39 "test.sh": nil, 40 "testdata/data1": nil, 41 "testdata/sub/data2": nil, 42 } 43 44 config := android.TestArchConfig(buildDir, nil, bp, fs) 45 46 ctx := android.NewTestArchContext() 47 ctx.RegisterModuleType("sh_test", ShTestFactory) 48 ctx.RegisterModuleType("sh_test_host", ShTestHostFactory) 49 ctx.Register(config) 50 _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) 51 android.FailIfErrored(t, errs) 52 _, errs = ctx.PrepareBuildActions(config) 53 android.FailIfErrored(t, errs) 54 55 return ctx, config 56} 57 58func TestShTestSubDir(t *testing.T) { 59 ctx, config := testShBinary(t, ` 60 sh_test { 61 name: "foo", 62 src: "test.sh", 63 sub_dir: "foo_test" 64 } 65 `) 66 67 mod := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*ShTest) 68 69 entries := android.AndroidMkEntriesForTest(t, config, "", mod)[0] 70 71 expectedPath := "/tmp/target/product/test_device/data/nativetest64/foo_test" 72 actualPath := entries.EntryMap["LOCAL_MODULE_PATH"][0] 73 if expectedPath != actualPath { 74 t.Errorf("Unexpected LOCAL_MODULE_PATH expected: %q, actual: %q", expectedPath, actualPath) 75 } 76} 77 78func TestShTest(t *testing.T) { 79 ctx, config := testShBinary(t, ` 80 sh_test { 81 name: "foo", 82 src: "test.sh", 83 filename: "test.sh", 84 data: [ 85 "testdata/data1", 86 "testdata/sub/data2", 87 ], 88 } 89 `) 90 91 mod := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*ShTest) 92 93 entries := android.AndroidMkEntriesForTest(t, config, "", mod)[0] 94 95 expectedPath := "/tmp/target/product/test_device/data/nativetest64/foo" 96 actualPath := entries.EntryMap["LOCAL_MODULE_PATH"][0] 97 if expectedPath != actualPath { 98 t.Errorf("Unexpected LOCAL_MODULE_PATH expected: %q, actual: %q", expectedPath, actualPath) 99 } 100 101 expectedData := []string{":testdata/data1", ":testdata/sub/data2"} 102 actualData := entries.EntryMap["LOCAL_TEST_DATA"] 103 if !reflect.DeepEqual(expectedData, actualData) { 104 t.Errorf("Unexpected test data expected: %q, actual: %q", expectedData, actualData) 105 } 106} 107 108func TestShTestHost(t *testing.T) { 109 ctx, _ := testShBinary(t, ` 110 sh_test_host { 111 name: "foo", 112 src: "test.sh", 113 filename: "test.sh", 114 data: [ 115 "testdata/data1", 116 "testdata/sub/data2", 117 ], 118 } 119 `) 120 121 buildOS := android.BuildOs.String() 122 mod := ctx.ModuleForTests("foo", buildOS+"_x86_64").Module().(*ShTest) 123 if !mod.Host() { 124 t.Errorf("host bit is not set for a sh_test_host module.") 125 } 126} 127