blob: 232a281330db2ea119e30f7747e9a1998373333f [file] [log] [blame]
Jaewoong Jungfccad6b2020-06-01 10:45:49 -07001package sh
Jaewoong Jung8eaeb092019-05-16 14:58:29 -07002
3import (
Jaewoong Jungfccad6b2020-06-01 10:45:49 -07004 "io/ioutil"
5 "os"
Jaewoong Jung7c3052a2020-05-29 16:15:32 -07006 "path/filepath"
Jaewoong Jung8eaeb092019-05-16 14:58:29 -07007 "reflect"
8 "testing"
Jaewoong Jungfccad6b2020-06-01 10:45:49 -07009
10 "android/soong/android"
Jaewoong Jung7c3052a2020-05-29 16:15:32 -070011 "android/soong/cc"
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070012)
13
Jaewoong Jungfccad6b2020-06-01 10:45:49 -070014var buildDir string
15
16func setUp() {
17 var err error
18 buildDir, err = ioutil.TempDir("", "soong_sh_test")
19 if err != nil {
20 panic(err)
21 }
22}
23
24func tearDown() {
25 os.RemoveAll(buildDir)
26}
27
28func TestMain(m *testing.M) {
29 run := func() int {
30 setUp()
31 defer tearDown()
32
33 return m.Run()
34 }
35
36 os.Exit(run())
37}
38
39func testShBinary(t *testing.T, bp string) (*android.TestContext, android.Config) {
Colin Cross98be1bb2019-12-13 20:41:13 -080040 fs := map[string][]byte{
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070041 "test.sh": nil,
42 "testdata/data1": nil,
43 "testdata/sub/data2": nil,
44 }
Colin Cross98be1bb2019-12-13 20:41:13 -080045
Jaewoong Jungfccad6b2020-06-01 10:45:49 -070046 config := android.TestArchConfig(buildDir, nil, bp, fs)
Colin Cross98be1bb2019-12-13 20:41:13 -080047
Jaewoong Jungfccad6b2020-06-01 10:45:49 -070048 ctx := android.NewTestArchContext()
Colin Cross98be1bb2019-12-13 20:41:13 -080049 ctx.RegisterModuleType("sh_test", ShTestFactory)
50 ctx.RegisterModuleType("sh_test_host", ShTestHostFactory)
Jaewoong Jung7c3052a2020-05-29 16:15:32 -070051
52 cc.RegisterRequiredBuildComponentsForTest(ctx)
53
Colin Cross98be1bb2019-12-13 20:41:13 -080054 ctx.Register(config)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070055 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
Jaewoong Jungfccad6b2020-06-01 10:45:49 -070056 android.FailIfErrored(t, errs)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070057 _, errs = ctx.PrepareBuildActions(config)
Jaewoong Jungfccad6b2020-06-01 10:45:49 -070058 android.FailIfErrored(t, errs)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070059
60 return ctx, config
61}
62
63func TestShTestTestData(t *testing.T) {
64 ctx, config := testShBinary(t, `
65 sh_test {
66 name: "foo",
67 src: "test.sh",
68 filename: "test.sh",
69 data: [
70 "testdata/data1",
71 "testdata/sub/data2",
72 ],
73 }
74 `)
75
76 mod := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*ShTest)
77
Jaewoong Jungfccad6b2020-06-01 10:45:49 -070078 entries := android.AndroidMkEntriesForTest(t, config, "", mod)[0]
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070079 expected := []string{":testdata/data1", ":testdata/sub/data2"}
80 actual := entries.EntryMap["LOCAL_TEST_DATA"]
81 if !reflect.DeepEqual(expected, actual) {
82 t.Errorf("Unexpected test data expected: %q, actual: %q", expected, actual)
83 }
84}
Jaewoong Jung61a83682019-07-01 09:08:50 -070085
Jaewoong Jung7c3052a2020-05-29 16:15:32 -070086func TestShTest_dataModules(t *testing.T) {
87 ctx, config := testShBinary(t, `
88 sh_test {
89 name: "foo",
90 src: "test.sh",
91 host_supported: true,
92 data_bins: ["bar"],
93 data_libs: ["libbar"],
94 }
95
96 cc_binary {
97 name: "bar",
98 host_supported: true,
99 shared_libs: ["libbar"],
100 no_libcrt: true,
101 nocrt: true,
102 system_shared_libs: [],
103 stl: "none",
104 }
105
106 cc_library {
107 name: "libbar",
108 host_supported: true,
109 no_libcrt: true,
110 nocrt: true,
111 system_shared_libs: [],
112 stl: "none",
113 }
114 `)
115
116 buildOS := android.BuildOs.String()
117 arches := []string{"android_arm64_armv8-a", buildOS + "_x86_64"}
118 for _, arch := range arches {
119 variant := ctx.ModuleForTests("foo", arch)
120
121 libExt := ".so"
122 if arch == "darwin_x86_64" {
123 libExt = ".dylib"
124 }
125 relocated := variant.Output("relocated/lib64/libbar" + libExt)
126 expectedInput := filepath.Join(buildDir, ".intermediates/libbar/"+arch+"_shared/libbar"+libExt)
127 if relocated.Input.String() != expectedInput {
128 t.Errorf("Unexpected relocation input, expected: %q, actual: %q",
129 expectedInput, relocated.Input.String())
130 }
131
132 mod := variant.Module().(*ShTest)
133 entries := android.AndroidMkEntriesForTest(t, config, "", mod)[0]
134 expectedData := []string{
135 filepath.Join(buildDir, ".intermediates/bar", arch, ":bar"),
136 filepath.Join(buildDir, ".intermediates/foo", arch, "relocated/:lib64/libbar"+libExt),
137 }
138 actualData := entries.EntryMap["LOCAL_TEST_DATA"]
139 if !reflect.DeepEqual(expectedData, actualData) {
140 t.Errorf("Unexpected test data, expected: %q, actual: %q", expectedData, actualData)
141 }
142 }
143}
144
Jaewoong Jung61a83682019-07-01 09:08:50 -0700145func TestShTestHost(t *testing.T) {
146 ctx, _ := testShBinary(t, `
147 sh_test_host {
148 name: "foo",
149 src: "test.sh",
150 filename: "test.sh",
151 data: [
152 "testdata/data1",
153 "testdata/sub/data2",
154 ],
155 }
156 `)
157
Jaewoong Jungfccad6b2020-06-01 10:45:49 -0700158 buildOS := android.BuildOs.String()
Jaewoong Jung61a83682019-07-01 09:08:50 -0700159 mod := ctx.ModuleForTests("foo", buildOS+"_x86_64").Module().(*ShTest)
160 if !mod.Host() {
161 t.Errorf("host bit is not set for a sh_test_host module.")
162 }
163}
Jaewoong Jung7c3052a2020-05-29 16:15:32 -0700164
165func TestShTestHost_dataDeviceModules(t *testing.T) {
166 ctx, config := testShBinary(t, `
167 sh_test_host {
168 name: "foo",
169 src: "test.sh",
170 data_device_bins: ["bar"],
171 data_device_libs: ["libbar"],
172 }
173
174 cc_binary {
175 name: "bar",
176 shared_libs: ["libbar"],
177 no_libcrt: true,
178 nocrt: true,
179 system_shared_libs: [],
180 stl: "none",
181 }
182
183 cc_library {
184 name: "libbar",
185 no_libcrt: true,
186 nocrt: true,
187 system_shared_libs: [],
188 stl: "none",
189 }
190 `)
191
192 buildOS := android.BuildOs.String()
193 variant := ctx.ModuleForTests("foo", buildOS+"_x86_64")
194
195 relocated := variant.Output("relocated/lib64/libbar.so")
196 expectedInput := filepath.Join(buildDir, ".intermediates/libbar/android_arm64_armv8-a_shared/libbar.so")
197 if relocated.Input.String() != expectedInput {
198 t.Errorf("Unexpected relocation input, expected: %q, actual: %q",
199 expectedInput, relocated.Input.String())
200 }
201
202 mod := variant.Module().(*ShTest)
203 entries := android.AndroidMkEntriesForTest(t, config, "", mod)[0]
204 expectedData := []string{
205 filepath.Join(buildDir, ".intermediates/bar/android_arm64_armv8-a/:bar"),
206 // libbar has been relocated, and so has a variant that matches the host arch.
207 filepath.Join(buildDir, ".intermediates/foo/"+buildOS+"_x86_64/relocated/:lib64/libbar.so"),
208 }
209 actualData := entries.EntryMap["LOCAL_TEST_DATA"]
210 if !reflect.DeepEqual(expectedData, actualData) {
211 t.Errorf("Unexpected test data, expected: %q, actual: %q", expectedData, actualData)
212 }
213}