blob: 37450b0e287f78a5a12221ea53bd609238ab680a [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 "os"
Jaewoong Jung6e0eee52020-05-29 16:15:32 -07005 "path/filepath"
Dan Shib40deac2021-05-24 12:04:54 -07006 "strconv"
Sam Delmericob3342ce2022-01-20 21:10:28 +00007 "strings"
Jaewoong Jung8eaeb092019-05-16 14:58:29 -07008 "testing"
Jaewoong Jung4b79e982020-06-01 10:45:49 -07009
10 "android/soong/android"
Jaewoong Jung6e0eee52020-05-29 16:15:32 -070011 "android/soong/cc"
Makoto Onuki1725b202023-08-24 22:17:56 +000012 "android/soong/java"
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070013)
14
Jaewoong Jung4b79e982020-06-01 10:45:49 -070015func TestMain(m *testing.M) {
Paul Duffin32b06c22021-03-16 07:50:37 +000016 os.Exit(m.Run())
Jaewoong Jung4b79e982020-06-01 10:45:49 -070017}
18
Paul Duffin89648f92021-03-20 00:36:55 +000019var prepareForShTest = android.GroupFixturePreparers(
Paul Duffin56fb8ee2021-03-08 15:05:52 +000020 cc.PrepareForTestWithCcBuildComponents,
Makoto Onuki1725b202023-08-24 22:17:56 +000021 java.PrepareForTestWithJavaDefaultModules,
Paul Duffin56fb8ee2021-03-08 15:05:52 +000022 PrepareForTestWithShBuildComponents,
23 android.FixtureMergeMockFs(android.MockFS{
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070024 "test.sh": nil,
25 "testdata/data1": nil,
26 "testdata/sub/data2": nil,
Paul Duffin56fb8ee2021-03-08 15:05:52 +000027 }),
28)
Colin Cross98be1bb2019-12-13 20:41:13 -080029
Paul Duffin89648f92021-03-20 00:36:55 +000030// testShBinary runs tests using the prepareForShTest
Paul Duffin56fb8ee2021-03-08 15:05:52 +000031//
Paul Duffin89648f92021-03-20 00:36:55 +000032// Do not add any new usages of this, instead use the prepareForShTest directly as it makes it much
Paul Duffin56fb8ee2021-03-08 15:05:52 +000033// easier to customize the test behavior.
34//
35// If it is necessary to customize the behavior of an existing test that uses this then please first
Paul Duffin89648f92021-03-20 00:36:55 +000036// convert the test to using prepareForShTest first and then in a following change add the
Paul Duffin56fb8ee2021-03-08 15:05:52 +000037// appropriate fixture preparers. Keeping the conversion change separate makes it easy to verify
38// that it did not change the test behavior unexpectedly.
39//
40// deprecated
41func testShBinary(t *testing.T, bp string) (*android.TestContext, android.Config) {
Colin Cross06c80eb2022-02-10 10:34:19 -080042 bp = bp + cc.GatherRequiredDepsForTest(android.Android)
43
Paul Duffin89648f92021-03-20 00:36:55 +000044 result := prepareForShTest.RunTestWithBp(t, bp)
Colin Cross98be1bb2019-12-13 20:41:13 -080045
Paul Duffin56fb8ee2021-03-08 15:05:52 +000046 return result.TestContext, result.Config
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070047}
48
Jaewoong Jung4aedc862020-06-10 17:23:46 -070049func TestShTestSubDir(t *testing.T) {
Colin Crossc68db4b2021-11-11 18:59:15 -080050 result := android.GroupFixturePreparers(
51 prepareForShTest,
52 android.FixtureModifyConfig(android.SetKatiEnabledForTests),
53 ).RunTestWithBp(t, `
Jaewoong Jung4aedc862020-06-10 17:23:46 -070054 sh_test {
55 name: "foo",
56 src: "test.sh",
57 sub_dir: "foo_test"
58 }
59 `)
60
Colin Crossc68db4b2021-11-11 18:59:15 -080061 mod := result.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*ShTest)
Jaewoong Jung4aedc862020-06-10 17:23:46 -070062
Colin Crossc68db4b2021-11-11 18:59:15 -080063 entries := android.AndroidMkEntriesForTest(t, result.TestContext, mod)[0]
Jaewoong Jung4aedc862020-06-10 17:23:46 -070064
Paul Duffin32b06c22021-03-16 07:50:37 +000065 expectedPath := "out/target/product/test_device/data/nativetest64/foo_test"
Jaewoong Jung4aedc862020-06-10 17:23:46 -070066 actualPath := entries.EntryMap["LOCAL_MODULE_PATH"][0]
Colin Crossc68db4b2021-11-11 18:59:15 -080067 android.AssertStringPathRelativeToTopEquals(t, "LOCAL_MODULE_PATH[0]", result.Config, expectedPath, actualPath)
Jaewoong Jung4aedc862020-06-10 17:23:46 -070068}
69
70func TestShTest(t *testing.T) {
Colin Crossc68db4b2021-11-11 18:59:15 -080071 result := android.GroupFixturePreparers(
72 prepareForShTest,
73 android.FixtureModifyConfig(android.SetKatiEnabledForTests),
74 ).RunTestWithBp(t, `
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070075 sh_test {
76 name: "foo",
77 src: "test.sh",
78 filename: "test.sh",
79 data: [
80 "testdata/data1",
81 "testdata/sub/data2",
82 ],
83 }
84 `)
85
Colin Crossc68db4b2021-11-11 18:59:15 -080086 mod := result.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*ShTest)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070087
Colin Crossc68db4b2021-11-11 18:59:15 -080088 entries := android.AndroidMkEntriesForTest(t, result.TestContext, mod)[0]
Jaewoong Jung4aedc862020-06-10 17:23:46 -070089
Paul Duffin32b06c22021-03-16 07:50:37 +000090 expectedPath := "out/target/product/test_device/data/nativetest64/foo"
Jaewoong Jung4aedc862020-06-10 17:23:46 -070091 actualPath := entries.EntryMap["LOCAL_MODULE_PATH"][0]
Colin Crossc68db4b2021-11-11 18:59:15 -080092 android.AssertStringPathRelativeToTopEquals(t, "LOCAL_MODULE_PATH[0]", result.Config, expectedPath, actualPath)
Jaewoong Jung4aedc862020-06-10 17:23:46 -070093
94 expectedData := []string{":testdata/data1", ":testdata/sub/data2"}
95 actualData := entries.EntryMap["LOCAL_TEST_DATA"]
Paul Duffin32b06c22021-03-16 07:50:37 +000096 android.AssertDeepEquals(t, "LOCAL_TEST_DATA", expectedData, actualData)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070097}
Jaewoong Jung61a83682019-07-01 09:08:50 -070098
Jaewoong Jung6e0eee52020-05-29 16:15:32 -070099func TestShTest_dataModules(t *testing.T) {
Paul Duffin32b06c22021-03-16 07:50:37 +0000100 ctx, config := testShBinary(t, `
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700101 sh_test {
102 name: "foo",
103 src: "test.sh",
104 host_supported: true,
105 data_bins: ["bar"],
106 data_libs: ["libbar"],
107 }
108
109 cc_binary {
110 name: "bar",
111 host_supported: true,
112 shared_libs: ["libbar"],
113 no_libcrt: true,
114 nocrt: true,
115 system_shared_libs: [],
116 stl: "none",
117 }
118
119 cc_library {
120 name: "libbar",
121 host_supported: true,
122 no_libcrt: true,
123 nocrt: true,
124 system_shared_libs: [],
125 stl: "none",
126 }
127 `)
128
Colin Cross0c66bc62021-07-20 09:47:41 -0700129 buildOS := config.BuildOS.String()
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700130 arches := []string{"android_arm64_armv8-a", buildOS + "_x86_64"}
131 for _, arch := range arches {
132 variant := ctx.ModuleForTests("foo", arch)
133
134 libExt := ".so"
135 if arch == "darwin_x86_64" {
136 libExt = ".dylib"
137 }
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800138 relocated := variant.Output(filepath.Join("out/soong/.intermediates/foo", arch, "relocated/lib64/libbar"+libExt))
Paul Duffin32b06c22021-03-16 07:50:37 +0000139 expectedInput := "out/soong/.intermediates/libbar/" + arch + "_shared/libbar" + libExt
140 android.AssertPathRelativeToTopEquals(t, "relocation input", expectedInput, relocated.Input)
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700141
142 mod := variant.Module().(*ShTest)
Colin Crossaa255532020-07-03 13:18:24 -0700143 entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700144 expectedData := []string{
Paul Duffin32b06c22021-03-16 07:50:37 +0000145 filepath.Join("out/soong/.intermediates/bar", arch, ":bar"),
146 filepath.Join("out/soong/.intermediates/foo", arch, "relocated/:lib64/libbar"+libExt),
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700147 }
148 actualData := entries.EntryMap["LOCAL_TEST_DATA"]
Paul Duffin32b06c22021-03-16 07:50:37 +0000149 android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_TEST_DATA", config, expectedData, actualData)
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700150 }
151}
152
Jaewoong Jung61a83682019-07-01 09:08:50 -0700153func TestShTestHost(t *testing.T) {
154 ctx, _ := testShBinary(t, `
155 sh_test_host {
156 name: "foo",
157 src: "test.sh",
158 filename: "test.sh",
159 data: [
160 "testdata/data1",
161 "testdata/sub/data2",
162 ],
Dan Shib40deac2021-05-24 12:04:54 -0700163 test_options: {
164 unit_test: true,
165 },
Jaewoong Jung61a83682019-07-01 09:08:50 -0700166 }
167 `)
168
Colin Cross0c66bc62021-07-20 09:47:41 -0700169 buildOS := ctx.Config().BuildOS.String()
Jaewoong Jung61a83682019-07-01 09:08:50 -0700170 mod := ctx.ModuleForTests("foo", buildOS+"_x86_64").Module().(*ShTest)
171 if !mod.Host() {
172 t.Errorf("host bit is not set for a sh_test_host module.")
173 }
Dan Shib40deac2021-05-24 12:04:54 -0700174 entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
175 actualData, _ := strconv.ParseBool(entries.EntryMap["LOCAL_IS_UNIT_TEST"][0])
176 android.AssertBoolEquals(t, "LOCAL_IS_UNIT_TEST", true, actualData)
Jaewoong Jung61a83682019-07-01 09:08:50 -0700177}
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700178
179func TestShTestHost_dataDeviceModules(t *testing.T) {
Paul Duffin32b06c22021-03-16 07:50:37 +0000180 ctx, config := testShBinary(t, `
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700181 sh_test_host {
182 name: "foo",
183 src: "test.sh",
184 data_device_bins: ["bar"],
185 data_device_libs: ["libbar"],
186 }
187
188 cc_binary {
189 name: "bar",
190 shared_libs: ["libbar"],
191 no_libcrt: true,
192 nocrt: true,
193 system_shared_libs: [],
194 stl: "none",
195 }
196
197 cc_library {
198 name: "libbar",
199 no_libcrt: true,
200 nocrt: true,
201 system_shared_libs: [],
202 stl: "none",
203 }
204 `)
205
Colin Cross0c66bc62021-07-20 09:47:41 -0700206 buildOS := config.BuildOS.String()
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800207 variant := buildOS + "_x86_64"
208 foo := ctx.ModuleForTests("foo", variant)
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700209
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800210 relocated := foo.Output(filepath.Join("out/soong/.intermediates/foo", variant, "relocated/lib64/libbar.so"))
Paul Duffin32b06c22021-03-16 07:50:37 +0000211 expectedInput := "out/soong/.intermediates/libbar/android_arm64_armv8-a_shared/libbar.so"
212 android.AssertPathRelativeToTopEquals(t, "relocation input", expectedInput, relocated.Input)
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700213
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800214 mod := foo.Module().(*ShTest)
Colin Crossaa255532020-07-03 13:18:24 -0700215 entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700216 expectedData := []string{
Paul Duffin32b06c22021-03-16 07:50:37 +0000217 "out/soong/.intermediates/bar/android_arm64_armv8-a/:bar",
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700218 // libbar has been relocated, and so has a variant that matches the host arch.
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800219 "out/soong/.intermediates/foo/" + variant + "/relocated/:lib64/libbar.so",
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700220 }
221 actualData := entries.EntryMap["LOCAL_TEST_DATA"]
Paul Duffin32b06c22021-03-16 07:50:37 +0000222 android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_TEST_DATA", config, expectedData, actualData)
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700223}
Sam Delmericob3342ce2022-01-20 21:10:28 +0000224
225func TestShTestHost_dataDeviceModulesAutogenTradefedConfig(t *testing.T) {
226 ctx, config := testShBinary(t, `
227 sh_test_host {
228 name: "foo",
229 src: "test.sh",
230 data_device_bins: ["bar"],
231 data_device_libs: ["libbar"],
232 }
233
234 cc_binary {
235 name: "bar",
236 shared_libs: ["libbar"],
237 no_libcrt: true,
238 nocrt: true,
239 system_shared_libs: [],
240 stl: "none",
241 }
242
243 cc_library {
244 name: "libbar",
245 no_libcrt: true,
246 nocrt: true,
247 system_shared_libs: [],
248 stl: "none",
249 }
250 `)
251
252 buildOS := config.BuildOS.String()
253 fooModule := ctx.ModuleForTests("foo", buildOS+"_x86_64")
254
255 expectedBinAutogenConfig := `<option name="push-file" key="bar" value="/data/local/tests/unrestricted/foo/bar" />`
256 autogen := fooModule.Rule("autogen")
257 if !strings.Contains(autogen.Args["extraConfigs"], expectedBinAutogenConfig) {
258 t.Errorf("foo extraConfings %v does not contain %q", autogen.Args["extraConfigs"], expectedBinAutogenConfig)
259 }
260}
Makoto Onuki1725b202023-08-24 22:17:56 +0000261
262func TestShTestHost_javaData(t *testing.T) {
263 ctx, config := testShBinary(t, `
264 sh_test_host {
265 name: "foo",
266 src: "test.sh",
267 filename: "test.sh",
268 data: [
269 "testdata/data1",
270 "testdata/sub/data2",
271 ],
272 java_data: [
273 "javalib",
274 ],
275 }
276
277 java_library_host {
278 name: "javalib",
279 srcs: [],
280 }
281 `)
282 buildOS := ctx.Config().BuildOS.String()
283 mod := ctx.ModuleForTests("foo", buildOS+"_x86_64").Module().(*ShTest)
284 if !mod.Host() {
285 t.Errorf("host bit is not set for a sh_test_host module.")
286 }
287 expectedData := []string{
288 ":testdata/data1",
289 ":testdata/sub/data2",
290 "out/soong/.intermediates/javalib/" + buildOS + "_common/combined/:javalib.jar",
291 }
292
293 entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
294 actualData := entries.EntryMap["LOCAL_TEST_DATA"]
295 android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_TEST_DATA", config, expectedData, actualData)
296}