blob: 7a24168e5faf5c3e9179d26482de270cbfa56ba9 [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
Paul Duffin56fb8ee2021-03-08 15:05:52 +000040var shFixtureFactory = android.NewFixtureFactory(
41 &buildDir,
42 cc.PrepareForTestWithCcBuildComponents,
43 PrepareForTestWithShBuildComponents,
44 android.FixtureMergeMockFs(android.MockFS{
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070045 "test.sh": nil,
46 "testdata/data1": nil,
47 "testdata/sub/data2": nil,
Paul Duffin56fb8ee2021-03-08 15:05:52 +000048 }),
49)
Colin Cross98be1bb2019-12-13 20:41:13 -080050
Paul Duffin56fb8ee2021-03-08 15:05:52 +000051// testShBinary runs tests using the shFixtureFactory
52//
53// Do not add any new usages of this, instead use the shFixtureFactory directly as it makes it much
54// easier to customize the test behavior.
55//
56// If it is necessary to customize the behavior of an existing test that uses this then please first
57// convert the test to using shFixtureFactory first and then in a following change add the
58// appropriate fixture preparers. Keeping the conversion change separate makes it easy to verify
59// that it did not change the test behavior unexpectedly.
60//
61// deprecated
62func testShBinary(t *testing.T, bp string) (*android.TestContext, android.Config) {
63 result := shFixtureFactory.RunTestWithBp(t, bp)
Colin Cross98be1bb2019-12-13 20:41:13 -080064
Paul Duffin56fb8ee2021-03-08 15:05:52 +000065 return result.TestContext, result.Config
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070066}
67
Jaewoong Jung4aedc862020-06-10 17:23:46 -070068func TestShTestSubDir(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -070069 ctx, _ := testShBinary(t, `
Jaewoong Jung4aedc862020-06-10 17:23:46 -070070 sh_test {
71 name: "foo",
72 src: "test.sh",
73 sub_dir: "foo_test"
74 }
75 `)
76
77 mod := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*ShTest)
78
Colin Crossaa255532020-07-03 13:18:24 -070079 entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
Jaewoong Jung4aedc862020-06-10 17:23:46 -070080
Lukacs T. Berki7690c092021-02-26 14:27:36 +010081 expectedPath := path.Join(buildDir,
82 "../target/product/test_device/data/nativetest64/foo_test")
Jaewoong Jung4aedc862020-06-10 17:23:46 -070083 actualPath := entries.EntryMap["LOCAL_MODULE_PATH"][0]
84 if expectedPath != actualPath {
85 t.Errorf("Unexpected LOCAL_MODULE_PATH expected: %q, actual: %q", expectedPath, actualPath)
86 }
87}
88
89func TestShTest(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -070090 ctx, _ := testShBinary(t, `
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070091 sh_test {
92 name: "foo",
93 src: "test.sh",
94 filename: "test.sh",
95 data: [
96 "testdata/data1",
97 "testdata/sub/data2",
98 ],
99 }
100 `)
101
102 mod := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*ShTest)
103
Colin Crossaa255532020-07-03 13:18:24 -0700104 entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
Jaewoong Jung4aedc862020-06-10 17:23:46 -0700105
Lukacs T. Berki7690c092021-02-26 14:27:36 +0100106 expectedPath := path.Join(buildDir,
107 "../target/product/test_device/data/nativetest64/foo")
Jaewoong Jung4aedc862020-06-10 17:23:46 -0700108 actualPath := entries.EntryMap["LOCAL_MODULE_PATH"][0]
109 if expectedPath != actualPath {
110 t.Errorf("Unexpected LOCAL_MODULE_PATH expected: %q, actual: %q", expectedPath, actualPath)
111 }
112
113 expectedData := []string{":testdata/data1", ":testdata/sub/data2"}
114 actualData := entries.EntryMap["LOCAL_TEST_DATA"]
115 if !reflect.DeepEqual(expectedData, actualData) {
116 t.Errorf("Unexpected test data expected: %q, actual: %q", expectedData, actualData)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700117 }
118}
Jaewoong Jung61a83682019-07-01 09:08:50 -0700119
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700120func TestShTest_dataModules(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -0700121 ctx, _ := testShBinary(t, `
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700122 sh_test {
123 name: "foo",
124 src: "test.sh",
125 host_supported: true,
126 data_bins: ["bar"],
127 data_libs: ["libbar"],
128 }
129
130 cc_binary {
131 name: "bar",
132 host_supported: true,
133 shared_libs: ["libbar"],
134 no_libcrt: true,
135 nocrt: true,
136 system_shared_libs: [],
137 stl: "none",
138 }
139
140 cc_library {
141 name: "libbar",
142 host_supported: true,
143 no_libcrt: true,
144 nocrt: true,
145 system_shared_libs: [],
146 stl: "none",
147 }
148 `)
149
150 buildOS := android.BuildOs.String()
151 arches := []string{"android_arm64_armv8-a", buildOS + "_x86_64"}
152 for _, arch := range arches {
153 variant := ctx.ModuleForTests("foo", arch)
154
155 libExt := ".so"
156 if arch == "darwin_x86_64" {
157 libExt = ".dylib"
158 }
159 relocated := variant.Output("relocated/lib64/libbar" + libExt)
160 expectedInput := filepath.Join(buildDir, ".intermediates/libbar/"+arch+"_shared/libbar"+libExt)
161 if relocated.Input.String() != expectedInput {
162 t.Errorf("Unexpected relocation input, expected: %q, actual: %q",
163 expectedInput, relocated.Input.String())
164 }
165
166 mod := variant.Module().(*ShTest)
Colin Crossaa255532020-07-03 13:18:24 -0700167 entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700168 expectedData := []string{
169 filepath.Join(buildDir, ".intermediates/bar", arch, ":bar"),
170 filepath.Join(buildDir, ".intermediates/foo", arch, "relocated/:lib64/libbar"+libExt),
171 }
172 actualData := entries.EntryMap["LOCAL_TEST_DATA"]
173 if !reflect.DeepEqual(expectedData, actualData) {
174 t.Errorf("Unexpected test data, expected: %q, actual: %q", expectedData, actualData)
175 }
176 }
177}
178
Jaewoong Jung61a83682019-07-01 09:08:50 -0700179func TestShTestHost(t *testing.T) {
180 ctx, _ := testShBinary(t, `
181 sh_test_host {
182 name: "foo",
183 src: "test.sh",
184 filename: "test.sh",
185 data: [
186 "testdata/data1",
187 "testdata/sub/data2",
188 ],
189 }
190 `)
191
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700192 buildOS := android.BuildOs.String()
Jaewoong Jung61a83682019-07-01 09:08:50 -0700193 mod := ctx.ModuleForTests("foo", buildOS+"_x86_64").Module().(*ShTest)
194 if !mod.Host() {
195 t.Errorf("host bit is not set for a sh_test_host module.")
196 }
197}
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700198
199func TestShTestHost_dataDeviceModules(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -0700200 ctx, _ := testShBinary(t, `
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700201 sh_test_host {
202 name: "foo",
203 src: "test.sh",
204 data_device_bins: ["bar"],
205 data_device_libs: ["libbar"],
206 }
207
208 cc_binary {
209 name: "bar",
210 shared_libs: ["libbar"],
211 no_libcrt: true,
212 nocrt: true,
213 system_shared_libs: [],
214 stl: "none",
215 }
216
217 cc_library {
218 name: "libbar",
219 no_libcrt: true,
220 nocrt: true,
221 system_shared_libs: [],
222 stl: "none",
223 }
224 `)
225
226 buildOS := android.BuildOs.String()
227 variant := ctx.ModuleForTests("foo", buildOS+"_x86_64")
228
229 relocated := variant.Output("relocated/lib64/libbar.so")
230 expectedInput := filepath.Join(buildDir, ".intermediates/libbar/android_arm64_armv8-a_shared/libbar.so")
231 if relocated.Input.String() != expectedInput {
232 t.Errorf("Unexpected relocation input, expected: %q, actual: %q",
233 expectedInput, relocated.Input.String())
234 }
235
236 mod := variant.Module().(*ShTest)
Colin Crossaa255532020-07-03 13:18:24 -0700237 entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700238 expectedData := []string{
239 filepath.Join(buildDir, ".intermediates/bar/android_arm64_armv8-a/:bar"),
240 // libbar has been relocated, and so has a variant that matches the host arch.
241 filepath.Join(buildDir, ".intermediates/foo/"+buildOS+"_x86_64/relocated/:lib64/libbar.so"),
242 }
243 actualData := entries.EntryMap["LOCAL_TEST_DATA"]
244 if !reflect.DeepEqual(expectedData, actualData) {
245 t.Errorf("Unexpected test data, expected: %q, actual: %q", expectedData, actualData)
246 }
247}