blob: 6ab22c5bf72ace8fc8d10820365311e1394454b1 [file] [log] [blame]
Jaewoong Jung4b79e982020-06-01 10:45:49 -07001package sh
Jaewoong Jung8eaeb092019-05-16 14:58:29 -07002
3import (
Jaewoong Jung4b79e982020-06-01 10:45:49 -07004 "io/ioutil"
5 "os"
Jaewoong Jung8eaeb092019-05-16 14:58:29 -07006 "reflect"
7 "testing"
Jaewoong Jung4b79e982020-06-01 10:45:49 -07008
9 "android/soong/android"
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070010)
11
Jaewoong Jung4b79e982020-06-01 10:45:49 -070012var 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) {
Colin Cross98be1bb2019-12-13 20:41:13 -080038 fs := map[string][]byte{
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070039 "test.sh": nil,
40 "testdata/data1": nil,
41 "testdata/sub/data2": nil,
42 }
Colin Cross98be1bb2019-12-13 20:41:13 -080043
Jaewoong Jung4b79e982020-06-01 10:45:49 -070044 config := android.TestArchConfig(buildDir, nil, bp, fs)
Colin Cross98be1bb2019-12-13 20:41:13 -080045
Jaewoong Jung4b79e982020-06-01 10:45:49 -070046 ctx := android.NewTestArchContext()
Colin Cross98be1bb2019-12-13 20:41:13 -080047 ctx.RegisterModuleType("sh_test", ShTestFactory)
48 ctx.RegisterModuleType("sh_test_host", ShTestHostFactory)
49 ctx.Register(config)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070050 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
Jaewoong Jung4b79e982020-06-01 10:45:49 -070051 android.FailIfErrored(t, errs)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070052 _, errs = ctx.PrepareBuildActions(config)
Jaewoong Jung4b79e982020-06-01 10:45:49 -070053 android.FailIfErrored(t, errs)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070054
55 return ctx, config
56}
57
Jaewoong Jung2d4f1a22020-06-10 17:23:46 -070058func TestShTest(t *testing.T) {
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070059 ctx, config := testShBinary(t, `
60 sh_test {
61 name: "foo",
62 src: "test.sh",
63 filename: "test.sh",
64 data: [
65 "testdata/data1",
66 "testdata/sub/data2",
67 ],
68 }
69 `)
70
71 mod := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*ShTest)
72
Jaewoong Jung4b79e982020-06-01 10:45:49 -070073 entries := android.AndroidMkEntriesForTest(t, config, "", mod)[0]
Jaewoong Jung2d4f1a22020-06-10 17:23:46 -070074
75 expectedPath := "/tmp/target/product/test_device/data/nativetest64/foo"
76 actualPath := entries.EntryMap["LOCAL_MODULE_PATH"][0]
77 if expectedPath != actualPath {
78 t.Errorf("Unexpected LOCAL_MODULE_PATH expected: %q, actual: %q", expectedPath, actualPath)
79 }
80
81 expectedData := []string{":testdata/data1", ":testdata/sub/data2"}
82 actualData := entries.EntryMap["LOCAL_TEST_DATA"]
83 if !reflect.DeepEqual(expectedData, actualData) {
84 t.Errorf("Unexpected test data expected: %q, actual: %q", expectedData, actualData)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070085 }
86}
Jaewoong Jung61a83682019-07-01 09:08:50 -070087
88func TestShTestHost(t *testing.T) {
89 ctx, _ := testShBinary(t, `
90 sh_test_host {
91 name: "foo",
92 src: "test.sh",
93 filename: "test.sh",
94 data: [
95 "testdata/data1",
96 "testdata/sub/data2",
97 ],
98 }
99 `)
100
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700101 buildOS := android.BuildOs.String()
Jaewoong Jung61a83682019-07-01 09:08:50 -0700102 mod := ctx.ModuleForTests("foo", buildOS+"_x86_64").Module().(*ShTest)
103 if !mod.Host() {
104 t.Errorf("host bit is not set for a sh_test_host module.")
105 }
106}