blob: 865d5f3d16dec66bdfd5fee57bde13f077dd3efe [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) {
Paul Duffin32b06c22021-03-16 07:50:37 +000045 ctx, config := testShBinary(t, `
Jaewoong Jung4aedc862020-06-10 17:23:46 -070046 sh_test {
47 name: "foo",
48 src: "test.sh",
49 sub_dir: "foo_test"
50 }
51 `)
52
53 mod := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*ShTest)
54
Colin Crossaa255532020-07-03 13:18:24 -070055 entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
Jaewoong Jung4aedc862020-06-10 17:23:46 -070056
Paul Duffin32b06c22021-03-16 07:50:37 +000057 expectedPath := "out/target/product/test_device/data/nativetest64/foo_test"
Jaewoong Jung4aedc862020-06-10 17:23:46 -070058 actualPath := entries.EntryMap["LOCAL_MODULE_PATH"][0]
Paul Duffin32b06c22021-03-16 07:50:37 +000059 android.AssertStringPathRelativeToTopEquals(t, "LOCAL_MODULE_PATH[0]", config, expectedPath, actualPath)
Jaewoong Jung4aedc862020-06-10 17:23:46 -070060}
61
62func TestShTest(t *testing.T) {
Paul Duffin32b06c22021-03-16 07:50:37 +000063 ctx, config := testShBinary(t, `
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070064 sh_test {
65 name: "foo",
66 src: "test.sh",
67 filename: "test.sh",
68 data: [
69 "testdata/data1",
70 "testdata/sub/data2",
71 ],
72 }
73 `)
74
75 mod := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*ShTest)
76
Colin Crossaa255532020-07-03 13:18:24 -070077 entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
Jaewoong Jung4aedc862020-06-10 17:23:46 -070078
Paul Duffin32b06c22021-03-16 07:50:37 +000079 expectedPath := "out/target/product/test_device/data/nativetest64/foo"
Jaewoong Jung4aedc862020-06-10 17:23:46 -070080 actualPath := entries.EntryMap["LOCAL_MODULE_PATH"][0]
Paul Duffin32b06c22021-03-16 07:50:37 +000081 android.AssertStringPathRelativeToTopEquals(t, "LOCAL_MODULE_PATH[0]", config, expectedPath, actualPath)
Jaewoong Jung4aedc862020-06-10 17:23:46 -070082
83 expectedData := []string{":testdata/data1", ":testdata/sub/data2"}
84 actualData := entries.EntryMap["LOCAL_TEST_DATA"]
Paul Duffin32b06c22021-03-16 07:50:37 +000085 android.AssertDeepEquals(t, "LOCAL_TEST_DATA", expectedData, actualData)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070086}
Jaewoong Jung61a83682019-07-01 09:08:50 -070087
Jaewoong Jung6e0eee52020-05-29 16:15:32 -070088func TestShTest_dataModules(t *testing.T) {
Paul Duffin32b06c22021-03-16 07:50:37 +000089 ctx, config := testShBinary(t, `
Jaewoong Jung6e0eee52020-05-29 16:15:32 -070090 sh_test {
91 name: "foo",
92 src: "test.sh",
93 host_supported: true,
94 data_bins: ["bar"],
95 data_libs: ["libbar"],
96 }
97
98 cc_binary {
99 name: "bar",
100 host_supported: true,
101 shared_libs: ["libbar"],
102 no_libcrt: true,
103 nocrt: true,
104 system_shared_libs: [],
105 stl: "none",
106 }
107
108 cc_library {
109 name: "libbar",
110 host_supported: true,
111 no_libcrt: true,
112 nocrt: true,
113 system_shared_libs: [],
114 stl: "none",
115 }
116 `)
117
Colin Cross0c66bc62021-07-20 09:47:41 -0700118 buildOS := config.BuildOS.String()
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700119 arches := []string{"android_arm64_armv8-a", buildOS + "_x86_64"}
120 for _, arch := range arches {
121 variant := ctx.ModuleForTests("foo", arch)
122
123 libExt := ".so"
124 if arch == "darwin_x86_64" {
125 libExt = ".dylib"
126 }
127 relocated := variant.Output("relocated/lib64/libbar" + libExt)
Paul Duffin32b06c22021-03-16 07:50:37 +0000128 expectedInput := "out/soong/.intermediates/libbar/" + arch + "_shared/libbar" + libExt
129 android.AssertPathRelativeToTopEquals(t, "relocation input", expectedInput, relocated.Input)
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700130
131 mod := variant.Module().(*ShTest)
Colin Crossaa255532020-07-03 13:18:24 -0700132 entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700133 expectedData := []string{
Paul Duffin32b06c22021-03-16 07:50:37 +0000134 filepath.Join("out/soong/.intermediates/bar", arch, ":bar"),
135 filepath.Join("out/soong/.intermediates/foo", arch, "relocated/:lib64/libbar"+libExt),
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700136 }
137 actualData := entries.EntryMap["LOCAL_TEST_DATA"]
Paul Duffin32b06c22021-03-16 07:50:37 +0000138 android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_TEST_DATA", config, expectedData, actualData)
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700139 }
140}
141
Jaewoong Jung61a83682019-07-01 09:08:50 -0700142func TestShTestHost(t *testing.T) {
143 ctx, _ := testShBinary(t, `
144 sh_test_host {
145 name: "foo",
146 src: "test.sh",
147 filename: "test.sh",
148 data: [
149 "testdata/data1",
150 "testdata/sub/data2",
151 ],
Dan Shib40deac2021-05-24 12:04:54 -0700152 test_options: {
153 unit_test: true,
154 },
Jaewoong Jung61a83682019-07-01 09:08:50 -0700155 }
156 `)
157
Colin Cross0c66bc62021-07-20 09:47:41 -0700158 buildOS := ctx.Config().BuildOS.String()
Jaewoong Jung61a83682019-07-01 09:08:50 -0700159 mod := ctx.ModuleForTests("foo", buildOS+"_x86_64").Module().(*ShTest)
160 if !mod.Host() {
161 t.Errorf("host bit is not set for a sh_test_host module.")
162 }
Dan Shib40deac2021-05-24 12:04:54 -0700163 entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
164 actualData, _ := strconv.ParseBool(entries.EntryMap["LOCAL_IS_UNIT_TEST"][0])
165 android.AssertBoolEquals(t, "LOCAL_IS_UNIT_TEST", true, actualData)
Jaewoong Jung61a83682019-07-01 09:08:50 -0700166}
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700167
168func TestShTestHost_dataDeviceModules(t *testing.T) {
Paul Duffin32b06c22021-03-16 07:50:37 +0000169 ctx, config := testShBinary(t, `
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700170 sh_test_host {
171 name: "foo",
172 src: "test.sh",
173 data_device_bins: ["bar"],
174 data_device_libs: ["libbar"],
175 }
176
177 cc_binary {
178 name: "bar",
179 shared_libs: ["libbar"],
180 no_libcrt: true,
181 nocrt: true,
182 system_shared_libs: [],
183 stl: "none",
184 }
185
186 cc_library {
187 name: "libbar",
188 no_libcrt: true,
189 nocrt: true,
190 system_shared_libs: [],
191 stl: "none",
192 }
193 `)
194
Colin Cross0c66bc62021-07-20 09:47:41 -0700195 buildOS := config.BuildOS.String()
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700196 variant := ctx.ModuleForTests("foo", buildOS+"_x86_64")
197
198 relocated := variant.Output("relocated/lib64/libbar.so")
Paul Duffin32b06c22021-03-16 07:50:37 +0000199 expectedInput := "out/soong/.intermediates/libbar/android_arm64_armv8-a_shared/libbar.so"
200 android.AssertPathRelativeToTopEquals(t, "relocation input", expectedInput, relocated.Input)
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700201
202 mod := variant.Module().(*ShTest)
Colin Crossaa255532020-07-03 13:18:24 -0700203 entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700204 expectedData := []string{
Paul Duffin32b06c22021-03-16 07:50:37 +0000205 "out/soong/.intermediates/bar/android_arm64_armv8-a/:bar",
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700206 // libbar has been relocated, and so has a variant that matches the host arch.
Paul Duffin32b06c22021-03-16 07:50:37 +0000207 "out/soong/.intermediates/foo/" + buildOS + "_x86_64/relocated/:lib64/libbar.so",
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700208 }
209 actualData := entries.EntryMap["LOCAL_TEST_DATA"]
Paul Duffin32b06c22021-03-16 07:50:37 +0000210 android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_TEST_DATA", config, expectedData, actualData)
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700211}