blob: 3bfe6110a6db8d63a1673fa46eb737892087b161 [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 Jung4aedc862020-06-10 17:23:46 -070058func 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) {
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070079 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
Jaewoong Jung4b79e982020-06-01 10:45:49 -070093 entries := android.AndroidMkEntriesForTest(t, config, "", mod)[0]
Jaewoong Jung4aedc862020-06-10 17:23:46 -070094
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)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700105 }
106}
Jaewoong Jung61a83682019-07-01 09:08:50 -0700107
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
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700121 buildOS := android.BuildOs.String()
Jaewoong Jung61a83682019-07-01 09:08:50 -0700122 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}