blob: f48f7fb292d3f3b25cd3569111351cf5af7b14e6 [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"
Lukacs T. Berki7690c092021-02-26 14:27:36 +01006 "path"
Jaewoong Jung6e0eee52020-05-29 16:15:32 -07007 "path/filepath"
Jaewoong Jung8eaeb092019-05-16 14:58:29 -07008 "reflect"
9 "testing"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070010
11 "android/soong/android"
Jaewoong Jung6e0eee52020-05-29 16:15:32 -070012 "android/soong/cc"
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070013)
14
Jaewoong Jung4b79e982020-06-01 10:45:49 -070015var buildDir string
16
17func setUp() {
18 var err error
19 buildDir, err = ioutil.TempDir("", "soong_sh_test")
20 if err != nil {
21 panic(err)
22 }
23}
24
25func tearDown() {
26 os.RemoveAll(buildDir)
27}
28
29func TestMain(m *testing.M) {
30 run := func() int {
31 setUp()
32 defer tearDown()
33
34 return m.Run()
35 }
36
37 os.Exit(run())
38}
39
40func testShBinary(t *testing.T, bp string) (*android.TestContext, android.Config) {
Colin Cross98be1bb2019-12-13 20:41:13 -080041 fs := map[string][]byte{
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070042 "test.sh": nil,
43 "testdata/data1": nil,
44 "testdata/sub/data2": nil,
45 }
Colin Cross98be1bb2019-12-13 20:41:13 -080046
Jaewoong Jung4b79e982020-06-01 10:45:49 -070047 config := android.TestArchConfig(buildDir, nil, bp, fs)
Colin Cross98be1bb2019-12-13 20:41:13 -080048
Colin Crossae8600b2020-10-29 17:09:13 -070049 ctx := android.NewTestArchContext(config)
Colin Cross98be1bb2019-12-13 20:41:13 -080050 ctx.RegisterModuleType("sh_test", ShTestFactory)
51 ctx.RegisterModuleType("sh_test_host", ShTestHostFactory)
Jaewoong Jung6e0eee52020-05-29 16:15:32 -070052
53 cc.RegisterRequiredBuildComponentsForTest(ctx)
54
Colin Crossae8600b2020-10-29 17:09:13 -070055 ctx.Register()
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070056 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
Jaewoong Jung4b79e982020-06-01 10:45:49 -070057 android.FailIfErrored(t, errs)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070058 _, errs = ctx.PrepareBuildActions(config)
Jaewoong Jung4b79e982020-06-01 10:45:49 -070059 android.FailIfErrored(t, errs)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070060
61 return ctx, config
62}
63
Jaewoong Jung4aedc862020-06-10 17:23:46 -070064func TestShTestSubDir(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -070065 ctx, _ := testShBinary(t, `
Jaewoong Jung4aedc862020-06-10 17:23:46 -070066 sh_test {
67 name: "foo",
68 src: "test.sh",
69 sub_dir: "foo_test"
70 }
71 `)
72
73 mod := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*ShTest)
74
Colin Crossaa255532020-07-03 13:18:24 -070075 entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
Jaewoong Jung4aedc862020-06-10 17:23:46 -070076
Lukacs T. Berki7690c092021-02-26 14:27:36 +010077 expectedPath := path.Join(buildDir,
78 "../target/product/test_device/data/nativetest64/foo_test")
Jaewoong Jung4aedc862020-06-10 17:23:46 -070079 actualPath := entries.EntryMap["LOCAL_MODULE_PATH"][0]
80 if expectedPath != actualPath {
81 t.Errorf("Unexpected LOCAL_MODULE_PATH expected: %q, actual: %q", expectedPath, actualPath)
82 }
83}
84
85func TestShTest(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -070086 ctx, _ := testShBinary(t, `
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070087 sh_test {
88 name: "foo",
89 src: "test.sh",
90 filename: "test.sh",
91 data: [
92 "testdata/data1",
93 "testdata/sub/data2",
94 ],
95 }
96 `)
97
98 mod := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*ShTest)
99
Colin Crossaa255532020-07-03 13:18:24 -0700100 entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
Jaewoong Jung4aedc862020-06-10 17:23:46 -0700101
Lukacs T. Berki7690c092021-02-26 14:27:36 +0100102 expectedPath := path.Join(buildDir,
103 "../target/product/test_device/data/nativetest64/foo")
Jaewoong Jung4aedc862020-06-10 17:23:46 -0700104 actualPath := entries.EntryMap["LOCAL_MODULE_PATH"][0]
105 if expectedPath != actualPath {
106 t.Errorf("Unexpected LOCAL_MODULE_PATH expected: %q, actual: %q", expectedPath, actualPath)
107 }
108
109 expectedData := []string{":testdata/data1", ":testdata/sub/data2"}
110 actualData := entries.EntryMap["LOCAL_TEST_DATA"]
111 if !reflect.DeepEqual(expectedData, actualData) {
112 t.Errorf("Unexpected test data expected: %q, actual: %q", expectedData, actualData)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700113 }
114}
Jaewoong Jung61a83682019-07-01 09:08:50 -0700115
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700116func TestShTest_dataModules(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -0700117 ctx, _ := testShBinary(t, `
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700118 sh_test {
119 name: "foo",
120 src: "test.sh",
121 host_supported: true,
122 data_bins: ["bar"],
123 data_libs: ["libbar"],
124 }
125
126 cc_binary {
127 name: "bar",
128 host_supported: true,
129 shared_libs: ["libbar"],
130 no_libcrt: true,
131 nocrt: true,
132 system_shared_libs: [],
133 stl: "none",
134 }
135
136 cc_library {
137 name: "libbar",
138 host_supported: true,
139 no_libcrt: true,
140 nocrt: true,
141 system_shared_libs: [],
142 stl: "none",
143 }
144 `)
145
146 buildOS := android.BuildOs.String()
147 arches := []string{"android_arm64_armv8-a", buildOS + "_x86_64"}
148 for _, arch := range arches {
149 variant := ctx.ModuleForTests("foo", arch)
150
151 libExt := ".so"
152 if arch == "darwin_x86_64" {
153 libExt = ".dylib"
154 }
155 relocated := variant.Output("relocated/lib64/libbar" + libExt)
156 expectedInput := filepath.Join(buildDir, ".intermediates/libbar/"+arch+"_shared/libbar"+libExt)
157 if relocated.Input.String() != expectedInput {
158 t.Errorf("Unexpected relocation input, expected: %q, actual: %q",
159 expectedInput, relocated.Input.String())
160 }
161
162 mod := variant.Module().(*ShTest)
Colin Crossaa255532020-07-03 13:18:24 -0700163 entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700164 expectedData := []string{
165 filepath.Join(buildDir, ".intermediates/bar", arch, ":bar"),
166 filepath.Join(buildDir, ".intermediates/foo", arch, "relocated/:lib64/libbar"+libExt),
167 }
168 actualData := entries.EntryMap["LOCAL_TEST_DATA"]
169 if !reflect.DeepEqual(expectedData, actualData) {
170 t.Errorf("Unexpected test data, expected: %q, actual: %q", expectedData, actualData)
171 }
172 }
173}
174
Jaewoong Jung61a83682019-07-01 09:08:50 -0700175func TestShTestHost(t *testing.T) {
176 ctx, _ := testShBinary(t, `
177 sh_test_host {
178 name: "foo",
179 src: "test.sh",
180 filename: "test.sh",
181 data: [
182 "testdata/data1",
183 "testdata/sub/data2",
184 ],
185 }
186 `)
187
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700188 buildOS := android.BuildOs.String()
Jaewoong Jung61a83682019-07-01 09:08:50 -0700189 mod := ctx.ModuleForTests("foo", buildOS+"_x86_64").Module().(*ShTest)
190 if !mod.Host() {
191 t.Errorf("host bit is not set for a sh_test_host module.")
192 }
193}
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700194
195func TestShTestHost_dataDeviceModules(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -0700196 ctx, _ := testShBinary(t, `
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700197 sh_test_host {
198 name: "foo",
199 src: "test.sh",
200 data_device_bins: ["bar"],
201 data_device_libs: ["libbar"],
202 }
203
204 cc_binary {
205 name: "bar",
206 shared_libs: ["libbar"],
207 no_libcrt: true,
208 nocrt: true,
209 system_shared_libs: [],
210 stl: "none",
211 }
212
213 cc_library {
214 name: "libbar",
215 no_libcrt: true,
216 nocrt: true,
217 system_shared_libs: [],
218 stl: "none",
219 }
220 `)
221
222 buildOS := android.BuildOs.String()
223 variant := ctx.ModuleForTests("foo", buildOS+"_x86_64")
224
225 relocated := variant.Output("relocated/lib64/libbar.so")
226 expectedInput := filepath.Join(buildDir, ".intermediates/libbar/android_arm64_armv8-a_shared/libbar.so")
227 if relocated.Input.String() != expectedInput {
228 t.Errorf("Unexpected relocation input, expected: %q, actual: %q",
229 expectedInput, relocated.Input.String())
230 }
231
232 mod := variant.Module().(*ShTest)
Colin Crossaa255532020-07-03 13:18:24 -0700233 entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700234 expectedData := []string{
235 filepath.Join(buildDir, ".intermediates/bar/android_arm64_armv8-a/:bar"),
236 // libbar has been relocated, and so has a variant that matches the host arch.
237 filepath.Join(buildDir, ".intermediates/foo/"+buildOS+"_x86_64/relocated/:lib64/libbar.so"),
238 }
239 actualData := entries.EntryMap["LOCAL_TEST_DATA"]
240 if !reflect.DeepEqual(expectedData, actualData) {
241 t.Errorf("Unexpected test data, expected: %q, actual: %q", expectedData, actualData)
242 }
243}