blob: 28b6fb93a736cd2240fccac1b41f5a9871b64adf [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"
Jaewoong Jung8eaeb092019-05-16 14:58:29 -07007 "testing"
Jaewoong Jung4b79e982020-06-01 10:45:49 -07008
9 "android/soong/android"
Jaewoong Jung6e0eee52020-05-29 16:15:32 -070010 "android/soong/cc"
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070011)
12
Jaewoong Jung4b79e982020-06-01 10:45:49 -070013func TestMain(m *testing.M) {
Paul Duffin32b06c22021-03-16 07:50:37 +000014 os.Exit(m.Run())
Jaewoong Jung4b79e982020-06-01 10:45:49 -070015}
16
Paul Duffin89648f92021-03-20 00:36:55 +000017var prepareForShTest = android.GroupFixturePreparers(
Paul Duffin56fb8ee2021-03-08 15:05:52 +000018 cc.PrepareForTestWithCcBuildComponents,
19 PrepareForTestWithShBuildComponents,
20 android.FixtureMergeMockFs(android.MockFS{
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070021 "test.sh": nil,
22 "testdata/data1": nil,
23 "testdata/sub/data2": nil,
Paul Duffin56fb8ee2021-03-08 15:05:52 +000024 }),
25)
Colin Cross98be1bb2019-12-13 20:41:13 -080026
Paul Duffin89648f92021-03-20 00:36:55 +000027// testShBinary runs tests using the prepareForShTest
Paul Duffin56fb8ee2021-03-08 15:05:52 +000028//
Paul Duffin89648f92021-03-20 00:36:55 +000029// 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 +000030// easier to customize the test behavior.
31//
32// 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 +000033// convert the test to using prepareForShTest first and then in a following change add the
Paul Duffin56fb8ee2021-03-08 15:05:52 +000034// appropriate fixture preparers. Keeping the conversion change separate makes it easy to verify
35// that it did not change the test behavior unexpectedly.
36//
37// deprecated
38func testShBinary(t *testing.T, bp string) (*android.TestContext, android.Config) {
Paul Duffin89648f92021-03-20 00:36:55 +000039 result := prepareForShTest.RunTestWithBp(t, bp)
Colin Cross98be1bb2019-12-13 20:41:13 -080040
Paul Duffin56fb8ee2021-03-08 15:05:52 +000041 return result.TestContext, result.Config
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070042}
43
Jaewoong Jung4aedc862020-06-10 17:23:46 -070044func TestShTestSubDir(t *testing.T) {
Colin Crossc68db4b2021-11-11 18:59:15 -080045 result := android.GroupFixturePreparers(
46 prepareForShTest,
47 android.FixtureModifyConfig(android.SetKatiEnabledForTests),
48 ).RunTestWithBp(t, `
Jaewoong Jung4aedc862020-06-10 17:23:46 -070049 sh_test {
50 name: "foo",
51 src: "test.sh",
52 sub_dir: "foo_test"
53 }
54 `)
55
Colin Crossc68db4b2021-11-11 18:59:15 -080056 mod := result.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*ShTest)
Jaewoong Jung4aedc862020-06-10 17:23:46 -070057
Colin Crossc68db4b2021-11-11 18:59:15 -080058 entries := android.AndroidMkEntriesForTest(t, result.TestContext, mod)[0]
Jaewoong Jung4aedc862020-06-10 17:23:46 -070059
Paul Duffin32b06c22021-03-16 07:50:37 +000060 expectedPath := "out/target/product/test_device/data/nativetest64/foo_test"
Jaewoong Jung4aedc862020-06-10 17:23:46 -070061 actualPath := entries.EntryMap["LOCAL_MODULE_PATH"][0]
Colin Crossc68db4b2021-11-11 18:59:15 -080062 android.AssertStringPathRelativeToTopEquals(t, "LOCAL_MODULE_PATH[0]", result.Config, expectedPath, actualPath)
Jaewoong Jung4aedc862020-06-10 17:23:46 -070063}
64
65func TestShTest(t *testing.T) {
Colin Crossc68db4b2021-11-11 18:59:15 -080066 result := android.GroupFixturePreparers(
67 prepareForShTest,
68 android.FixtureModifyConfig(android.SetKatiEnabledForTests),
69 ).RunTestWithBp(t, `
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070070 sh_test {
71 name: "foo",
72 src: "test.sh",
73 filename: "test.sh",
74 data: [
75 "testdata/data1",
76 "testdata/sub/data2",
77 ],
78 }
79 `)
80
Colin Crossc68db4b2021-11-11 18:59:15 -080081 mod := result.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*ShTest)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070082
Colin Crossc68db4b2021-11-11 18:59:15 -080083 entries := android.AndroidMkEntriesForTest(t, result.TestContext, mod)[0]
Jaewoong Jung4aedc862020-06-10 17:23:46 -070084
Paul Duffin32b06c22021-03-16 07:50:37 +000085 expectedPath := "out/target/product/test_device/data/nativetest64/foo"
Jaewoong Jung4aedc862020-06-10 17:23:46 -070086 actualPath := entries.EntryMap["LOCAL_MODULE_PATH"][0]
Colin Crossc68db4b2021-11-11 18:59:15 -080087 android.AssertStringPathRelativeToTopEquals(t, "LOCAL_MODULE_PATH[0]", result.Config, expectedPath, actualPath)
Jaewoong Jung4aedc862020-06-10 17:23:46 -070088
89 expectedData := []string{":testdata/data1", ":testdata/sub/data2"}
90 actualData := entries.EntryMap["LOCAL_TEST_DATA"]
Paul Duffin32b06c22021-03-16 07:50:37 +000091 android.AssertDeepEquals(t, "LOCAL_TEST_DATA", expectedData, actualData)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070092}
Jaewoong Jung61a83682019-07-01 09:08:50 -070093
Jaewoong Jung6e0eee52020-05-29 16:15:32 -070094func TestShTest_dataModules(t *testing.T) {
Paul Duffin32b06c22021-03-16 07:50:37 +000095 ctx, config := testShBinary(t, `
Jaewoong Jung6e0eee52020-05-29 16:15:32 -070096 sh_test {
97 name: "foo",
98 src: "test.sh",
99 host_supported: true,
100 data_bins: ["bar"],
101 data_libs: ["libbar"],
102 }
103
104 cc_binary {
105 name: "bar",
106 host_supported: true,
107 shared_libs: ["libbar"],
108 no_libcrt: true,
109 nocrt: true,
110 system_shared_libs: [],
111 stl: "none",
112 }
113
114 cc_library {
115 name: "libbar",
116 host_supported: true,
117 no_libcrt: true,
118 nocrt: true,
119 system_shared_libs: [],
120 stl: "none",
121 }
122 `)
123
Colin Cross0c66bc62021-07-20 09:47:41 -0700124 buildOS := config.BuildOS.String()
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700125 arches := []string{"android_arm64_armv8-a", buildOS + "_x86_64"}
126 for _, arch := range arches {
127 variant := ctx.ModuleForTests("foo", arch)
128
129 libExt := ".so"
130 if arch == "darwin_x86_64" {
131 libExt = ".dylib"
132 }
133 relocated := variant.Output("relocated/lib64/libbar" + libExt)
Paul Duffin32b06c22021-03-16 07:50:37 +0000134 expectedInput := "out/soong/.intermediates/libbar/" + arch + "_shared/libbar" + libExt
135 android.AssertPathRelativeToTopEquals(t, "relocation input", expectedInput, relocated.Input)
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700136
137 mod := variant.Module().(*ShTest)
Colin Crossaa255532020-07-03 13:18:24 -0700138 entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700139 expectedData := []string{
Paul Duffin32b06c22021-03-16 07:50:37 +0000140 filepath.Join("out/soong/.intermediates/bar", arch, ":bar"),
141 filepath.Join("out/soong/.intermediates/foo", arch, "relocated/:lib64/libbar"+libExt),
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700142 }
143 actualData := entries.EntryMap["LOCAL_TEST_DATA"]
Paul Duffin32b06c22021-03-16 07:50:37 +0000144 android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_TEST_DATA", config, expectedData, actualData)
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700145 }
146}
147
Jaewoong Jung61a83682019-07-01 09:08:50 -0700148func TestShTestHost(t *testing.T) {
149 ctx, _ := testShBinary(t, `
150 sh_test_host {
151 name: "foo",
152 src: "test.sh",
153 filename: "test.sh",
154 data: [
155 "testdata/data1",
156 "testdata/sub/data2",
157 ],
Dan Shib40deac2021-05-24 12:04:54 -0700158 test_options: {
159 unit_test: true,
160 },
Jaewoong Jung61a83682019-07-01 09:08:50 -0700161 }
162 `)
163
Colin Cross0c66bc62021-07-20 09:47:41 -0700164 buildOS := ctx.Config().BuildOS.String()
Jaewoong Jung61a83682019-07-01 09:08:50 -0700165 mod := ctx.ModuleForTests("foo", buildOS+"_x86_64").Module().(*ShTest)
166 if !mod.Host() {
167 t.Errorf("host bit is not set for a sh_test_host module.")
168 }
Dan Shib40deac2021-05-24 12:04:54 -0700169 entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
170 actualData, _ := strconv.ParseBool(entries.EntryMap["LOCAL_IS_UNIT_TEST"][0])
171 android.AssertBoolEquals(t, "LOCAL_IS_UNIT_TEST", true, actualData)
Jaewoong Jung61a83682019-07-01 09:08:50 -0700172}
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700173
174func TestShTestHost_dataDeviceModules(t *testing.T) {
Paul Duffin32b06c22021-03-16 07:50:37 +0000175 ctx, config := testShBinary(t, `
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700176 sh_test_host {
177 name: "foo",
178 src: "test.sh",
179 data_device_bins: ["bar"],
180 data_device_libs: ["libbar"],
181 }
182
183 cc_binary {
184 name: "bar",
185 shared_libs: ["libbar"],
186 no_libcrt: true,
187 nocrt: true,
188 system_shared_libs: [],
189 stl: "none",
190 }
191
192 cc_library {
193 name: "libbar",
194 no_libcrt: true,
195 nocrt: true,
196 system_shared_libs: [],
197 stl: "none",
198 }
199 `)
200
Colin Cross0c66bc62021-07-20 09:47:41 -0700201 buildOS := config.BuildOS.String()
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700202 variant := ctx.ModuleForTests("foo", buildOS+"_x86_64")
203
204 relocated := variant.Output("relocated/lib64/libbar.so")
Paul Duffin32b06c22021-03-16 07:50:37 +0000205 expectedInput := "out/soong/.intermediates/libbar/android_arm64_armv8-a_shared/libbar.so"
206 android.AssertPathRelativeToTopEquals(t, "relocation input", expectedInput, relocated.Input)
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700207
208 mod := variant.Module().(*ShTest)
Colin Crossaa255532020-07-03 13:18:24 -0700209 entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700210 expectedData := []string{
Paul Duffin32b06c22021-03-16 07:50:37 +0000211 "out/soong/.intermediates/bar/android_arm64_armv8-a/:bar",
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700212 // libbar has been relocated, and so has a variant that matches the host arch.
Paul Duffin32b06c22021-03-16 07:50:37 +0000213 "out/soong/.intermediates/foo/" + buildOS + "_x86_64/relocated/:lib64/libbar.so",
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700214 }
215 actualData := entries.EntryMap["LOCAL_TEST_DATA"]
Paul Duffin32b06c22021-03-16 07:50:37 +0000216 android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_TEST_DATA", config, expectedData, actualData)
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700217}