blob: 7fe1d8538db88b9297793d046f8b81d67fb0b673 [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"
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070012)
13
Jaewoong Jung4b79e982020-06-01 10:45:49 -070014func TestMain(m *testing.M) {
Paul Duffin32b06c22021-03-16 07:50:37 +000015 os.Exit(m.Run())
Jaewoong Jung4b79e982020-06-01 10:45:49 -070016}
17
Paul Duffin89648f92021-03-20 00:36:55 +000018var prepareForShTest = android.GroupFixturePreparers(
Paul Duffin56fb8ee2021-03-08 15:05:52 +000019 cc.PrepareForTestWithCcBuildComponents,
20 PrepareForTestWithShBuildComponents,
21 android.FixtureMergeMockFs(android.MockFS{
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070022 "test.sh": nil,
23 "testdata/data1": nil,
24 "testdata/sub/data2": nil,
Paul Duffin56fb8ee2021-03-08 15:05:52 +000025 }),
26)
Colin Cross98be1bb2019-12-13 20:41:13 -080027
Paul Duffin89648f92021-03-20 00:36:55 +000028// testShBinary runs tests using the prepareForShTest
Paul Duffin56fb8ee2021-03-08 15:05:52 +000029//
Paul Duffin89648f92021-03-20 00:36:55 +000030// 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 +000031// easier to customize the test behavior.
32//
33// 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 +000034// convert the test to using prepareForShTest first and then in a following change add the
Paul Duffin56fb8ee2021-03-08 15:05:52 +000035// appropriate fixture preparers. Keeping the conversion change separate makes it easy to verify
36// that it did not change the test behavior unexpectedly.
37//
38// deprecated
39func testShBinary(t *testing.T, bp string) (*android.TestContext, android.Config) {
Paul Duffin89648f92021-03-20 00:36:55 +000040 result := prepareForShTest.RunTestWithBp(t, bp)
Colin Cross98be1bb2019-12-13 20:41:13 -080041
Paul Duffin56fb8ee2021-03-08 15:05:52 +000042 return result.TestContext, result.Config
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070043}
44
Jaewoong Jung4aedc862020-06-10 17:23:46 -070045func TestShTestSubDir(t *testing.T) {
Colin Crossc68db4b2021-11-11 18:59:15 -080046 result := android.GroupFixturePreparers(
47 prepareForShTest,
48 android.FixtureModifyConfig(android.SetKatiEnabledForTests),
49 ).RunTestWithBp(t, `
Jaewoong Jung4aedc862020-06-10 17:23:46 -070050 sh_test {
51 name: "foo",
52 src: "test.sh",
53 sub_dir: "foo_test"
54 }
55 `)
56
Colin Crossc68db4b2021-11-11 18:59:15 -080057 mod := result.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*ShTest)
Jaewoong Jung4aedc862020-06-10 17:23:46 -070058
Colin Crossc68db4b2021-11-11 18:59:15 -080059 entries := android.AndroidMkEntriesForTest(t, result.TestContext, mod)[0]
Jaewoong Jung4aedc862020-06-10 17:23:46 -070060
Paul Duffin32b06c22021-03-16 07:50:37 +000061 expectedPath := "out/target/product/test_device/data/nativetest64/foo_test"
Jaewoong Jung4aedc862020-06-10 17:23:46 -070062 actualPath := entries.EntryMap["LOCAL_MODULE_PATH"][0]
Colin Crossc68db4b2021-11-11 18:59:15 -080063 android.AssertStringPathRelativeToTopEquals(t, "LOCAL_MODULE_PATH[0]", result.Config, expectedPath, actualPath)
Jaewoong Jung4aedc862020-06-10 17:23:46 -070064}
65
66func TestShTest(t *testing.T) {
Colin Crossc68db4b2021-11-11 18:59:15 -080067 result := android.GroupFixturePreparers(
68 prepareForShTest,
69 android.FixtureModifyConfig(android.SetKatiEnabledForTests),
70 ).RunTestWithBp(t, `
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070071 sh_test {
72 name: "foo",
73 src: "test.sh",
74 filename: "test.sh",
75 data: [
76 "testdata/data1",
77 "testdata/sub/data2",
78 ],
79 }
80 `)
81
Colin Crossc68db4b2021-11-11 18:59:15 -080082 mod := result.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*ShTest)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070083
Colin Crossc68db4b2021-11-11 18:59:15 -080084 entries := android.AndroidMkEntriesForTest(t, result.TestContext, mod)[0]
Jaewoong Jung4aedc862020-06-10 17:23:46 -070085
Paul Duffin32b06c22021-03-16 07:50:37 +000086 expectedPath := "out/target/product/test_device/data/nativetest64/foo"
Jaewoong Jung4aedc862020-06-10 17:23:46 -070087 actualPath := entries.EntryMap["LOCAL_MODULE_PATH"][0]
Colin Crossc68db4b2021-11-11 18:59:15 -080088 android.AssertStringPathRelativeToTopEquals(t, "LOCAL_MODULE_PATH[0]", result.Config, expectedPath, actualPath)
Jaewoong Jung4aedc862020-06-10 17:23:46 -070089
90 expectedData := []string{":testdata/data1", ":testdata/sub/data2"}
91 actualData := entries.EntryMap["LOCAL_TEST_DATA"]
Paul Duffin32b06c22021-03-16 07:50:37 +000092 android.AssertDeepEquals(t, "LOCAL_TEST_DATA", expectedData, actualData)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070093}
Jaewoong Jung61a83682019-07-01 09:08:50 -070094
Jaewoong Jung6e0eee52020-05-29 16:15:32 -070095func TestShTest_dataModules(t *testing.T) {
Paul Duffin32b06c22021-03-16 07:50:37 +000096 ctx, config := testShBinary(t, `
Jaewoong Jung6e0eee52020-05-29 16:15:32 -070097 sh_test {
98 name: "foo",
99 src: "test.sh",
100 host_supported: true,
101 data_bins: ["bar"],
102 data_libs: ["libbar"],
103 }
104
105 cc_binary {
106 name: "bar",
107 host_supported: true,
108 shared_libs: ["libbar"],
109 no_libcrt: true,
110 nocrt: true,
111 system_shared_libs: [],
112 stl: "none",
113 }
114
115 cc_library {
116 name: "libbar",
117 host_supported: true,
118 no_libcrt: true,
119 nocrt: true,
120 system_shared_libs: [],
121 stl: "none",
122 }
123 `)
124
Colin Cross0c66bc62021-07-20 09:47:41 -0700125 buildOS := config.BuildOS.String()
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700126 arches := []string{"android_arm64_armv8-a", buildOS + "_x86_64"}
127 for _, arch := range arches {
128 variant := ctx.ModuleForTests("foo", arch)
129
130 libExt := ".so"
131 if arch == "darwin_x86_64" {
132 libExt = ".dylib"
133 }
134 relocated := variant.Output("relocated/lib64/libbar" + libExt)
Paul Duffin32b06c22021-03-16 07:50:37 +0000135 expectedInput := "out/soong/.intermediates/libbar/" + arch + "_shared/libbar" + libExt
136 android.AssertPathRelativeToTopEquals(t, "relocation input", expectedInput, relocated.Input)
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700137
138 mod := variant.Module().(*ShTest)
Colin Crossaa255532020-07-03 13:18:24 -0700139 entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700140 expectedData := []string{
Paul Duffin32b06c22021-03-16 07:50:37 +0000141 filepath.Join("out/soong/.intermediates/bar", arch, ":bar"),
142 filepath.Join("out/soong/.intermediates/foo", arch, "relocated/:lib64/libbar"+libExt),
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700143 }
144 actualData := entries.EntryMap["LOCAL_TEST_DATA"]
Paul Duffin32b06c22021-03-16 07:50:37 +0000145 android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_TEST_DATA", config, expectedData, actualData)
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700146 }
147}
148
Jaewoong Jung61a83682019-07-01 09:08:50 -0700149func TestShTestHost(t *testing.T) {
150 ctx, _ := testShBinary(t, `
151 sh_test_host {
152 name: "foo",
153 src: "test.sh",
154 filename: "test.sh",
155 data: [
156 "testdata/data1",
157 "testdata/sub/data2",
158 ],
Dan Shib40deac2021-05-24 12:04:54 -0700159 test_options: {
160 unit_test: true,
161 },
Jaewoong Jung61a83682019-07-01 09:08:50 -0700162 }
163 `)
164
Colin Cross0c66bc62021-07-20 09:47:41 -0700165 buildOS := ctx.Config().BuildOS.String()
Jaewoong Jung61a83682019-07-01 09:08:50 -0700166 mod := ctx.ModuleForTests("foo", buildOS+"_x86_64").Module().(*ShTest)
167 if !mod.Host() {
168 t.Errorf("host bit is not set for a sh_test_host module.")
169 }
Dan Shib40deac2021-05-24 12:04:54 -0700170 entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
171 actualData, _ := strconv.ParseBool(entries.EntryMap["LOCAL_IS_UNIT_TEST"][0])
172 android.AssertBoolEquals(t, "LOCAL_IS_UNIT_TEST", true, actualData)
Jaewoong Jung61a83682019-07-01 09:08:50 -0700173}
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700174
175func TestShTestHost_dataDeviceModules(t *testing.T) {
Paul Duffin32b06c22021-03-16 07:50:37 +0000176 ctx, config := testShBinary(t, `
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700177 sh_test_host {
178 name: "foo",
179 src: "test.sh",
180 data_device_bins: ["bar"],
181 data_device_libs: ["libbar"],
182 }
183
184 cc_binary {
185 name: "bar",
186 shared_libs: ["libbar"],
187 no_libcrt: true,
188 nocrt: true,
189 system_shared_libs: [],
190 stl: "none",
191 }
192
193 cc_library {
194 name: "libbar",
195 no_libcrt: true,
196 nocrt: true,
197 system_shared_libs: [],
198 stl: "none",
199 }
200 `)
201
Colin Cross0c66bc62021-07-20 09:47:41 -0700202 buildOS := config.BuildOS.String()
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700203 variant := ctx.ModuleForTests("foo", buildOS+"_x86_64")
204
205 relocated := variant.Output("relocated/lib64/libbar.so")
Paul Duffin32b06c22021-03-16 07:50:37 +0000206 expectedInput := "out/soong/.intermediates/libbar/android_arm64_armv8-a_shared/libbar.so"
207 android.AssertPathRelativeToTopEquals(t, "relocation input", expectedInput, relocated.Input)
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700208
209 mod := variant.Module().(*ShTest)
Colin Crossaa255532020-07-03 13:18:24 -0700210 entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700211 expectedData := []string{
Paul Duffin32b06c22021-03-16 07:50:37 +0000212 "out/soong/.intermediates/bar/android_arm64_armv8-a/:bar",
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700213 // libbar has been relocated, and so has a variant that matches the host arch.
Paul Duffin32b06c22021-03-16 07:50:37 +0000214 "out/soong/.intermediates/foo/" + buildOS + "_x86_64/relocated/:lib64/libbar.so",
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700215 }
216 actualData := entries.EntryMap["LOCAL_TEST_DATA"]
Paul Duffin32b06c22021-03-16 07:50:37 +0000217 android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_TEST_DATA", config, expectedData, actualData)
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700218}
Sam Delmericob3342ce2022-01-20 21:10:28 +0000219
220func TestShTestHost_dataDeviceModulesAutogenTradefedConfig(t *testing.T) {
221 ctx, config := testShBinary(t, `
222 sh_test_host {
223 name: "foo",
224 src: "test.sh",
225 data_device_bins: ["bar"],
226 data_device_libs: ["libbar"],
227 }
228
229 cc_binary {
230 name: "bar",
231 shared_libs: ["libbar"],
232 no_libcrt: true,
233 nocrt: true,
234 system_shared_libs: [],
235 stl: "none",
236 }
237
238 cc_library {
239 name: "libbar",
240 no_libcrt: true,
241 nocrt: true,
242 system_shared_libs: [],
243 stl: "none",
244 }
245 `)
246
247 buildOS := config.BuildOS.String()
248 fooModule := ctx.ModuleForTests("foo", buildOS+"_x86_64")
249
250 expectedBinAutogenConfig := `<option name="push-file" key="bar" value="/data/local/tests/unrestricted/foo/bar" />`
251 autogen := fooModule.Rule("autogen")
252 if !strings.Contains(autogen.Args["extraConfigs"], expectedBinAutogenConfig) {
253 t.Errorf("foo extraConfings %v does not contain %q", autogen.Args["extraConfigs"], expectedBinAutogenConfig)
254 }
255}