blob: 9e7e594628bbb00b66f206b329317972e863a38f [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"
Jaewoong Jung8eaeb092019-05-16 14:58:29 -07006 "testing"
Jaewoong Jung4b79e982020-06-01 10:45:49 -07007
8 "android/soong/android"
Jaewoong Jung6e0eee52020-05-29 16:15:32 -07009 "android/soong/cc"
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070010)
11
Jaewoong Jung4b79e982020-06-01 10:45:49 -070012func TestMain(m *testing.M) {
Paul Duffin32b06c22021-03-16 07:50:37 +000013 os.Exit(m.Run())
Jaewoong Jung4b79e982020-06-01 10:45:49 -070014}
15
Paul Duffin89648f92021-03-20 00:36:55 +000016var prepareForShTest = android.GroupFixturePreparers(
Paul Duffin56fb8ee2021-03-08 15:05:52 +000017 cc.PrepareForTestWithCcBuildComponents,
18 PrepareForTestWithShBuildComponents,
19 android.FixtureMergeMockFs(android.MockFS{
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070020 "test.sh": nil,
21 "testdata/data1": nil,
22 "testdata/sub/data2": nil,
Paul Duffin56fb8ee2021-03-08 15:05:52 +000023 }),
24)
Colin Cross98be1bb2019-12-13 20:41:13 -080025
Paul Duffin89648f92021-03-20 00:36:55 +000026// testShBinary runs tests using the prepareForShTest
Paul Duffin56fb8ee2021-03-08 15:05:52 +000027//
Paul Duffin89648f92021-03-20 00:36:55 +000028// 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 +000029// easier to customize the test behavior.
30//
31// 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 +000032// convert the test to using prepareForShTest first and then in a following change add the
Paul Duffin56fb8ee2021-03-08 15:05:52 +000033// appropriate fixture preparers. Keeping the conversion change separate makes it easy to verify
34// that it did not change the test behavior unexpectedly.
35//
36// deprecated
37func testShBinary(t *testing.T, bp string) (*android.TestContext, android.Config) {
Paul Duffin89648f92021-03-20 00:36:55 +000038 result := prepareForShTest.RunTestWithBp(t, bp)
Colin Cross98be1bb2019-12-13 20:41:13 -080039
Paul Duffin56fb8ee2021-03-08 15:05:52 +000040 return result.TestContext, result.Config
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070041}
42
Jaewoong Jung4aedc862020-06-10 17:23:46 -070043func TestShTestSubDir(t *testing.T) {
Paul Duffin32b06c22021-03-16 07:50:37 +000044 ctx, config := testShBinary(t, `
Jaewoong Jung4aedc862020-06-10 17:23:46 -070045 sh_test {
46 name: "foo",
47 src: "test.sh",
48 sub_dir: "foo_test"
49 }
50 `)
51
52 mod := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*ShTest)
53
Colin Crossaa255532020-07-03 13:18:24 -070054 entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
Jaewoong Jung4aedc862020-06-10 17:23:46 -070055
Paul Duffin32b06c22021-03-16 07:50:37 +000056 expectedPath := "out/target/product/test_device/data/nativetest64/foo_test"
Jaewoong Jung4aedc862020-06-10 17:23:46 -070057 actualPath := entries.EntryMap["LOCAL_MODULE_PATH"][0]
Paul Duffin32b06c22021-03-16 07:50:37 +000058 android.AssertStringPathRelativeToTopEquals(t, "LOCAL_MODULE_PATH[0]", config, expectedPath, actualPath)
Jaewoong Jung4aedc862020-06-10 17:23:46 -070059}
60
61func TestShTest(t *testing.T) {
Paul Duffin32b06c22021-03-16 07:50:37 +000062 ctx, config := testShBinary(t, `
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070063 sh_test {
64 name: "foo",
65 src: "test.sh",
66 filename: "test.sh",
67 data: [
68 "testdata/data1",
69 "testdata/sub/data2",
70 ],
71 }
72 `)
73
74 mod := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*ShTest)
75
Colin Crossaa255532020-07-03 13:18:24 -070076 entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
Jaewoong Jung4aedc862020-06-10 17:23:46 -070077
Paul Duffin32b06c22021-03-16 07:50:37 +000078 expectedPath := "out/target/product/test_device/data/nativetest64/foo"
Jaewoong Jung4aedc862020-06-10 17:23:46 -070079 actualPath := entries.EntryMap["LOCAL_MODULE_PATH"][0]
Paul Duffin32b06c22021-03-16 07:50:37 +000080 android.AssertStringPathRelativeToTopEquals(t, "LOCAL_MODULE_PATH[0]", config, expectedPath, actualPath)
Jaewoong Jung4aedc862020-06-10 17:23:46 -070081
82 expectedData := []string{":testdata/data1", ":testdata/sub/data2"}
83 actualData := entries.EntryMap["LOCAL_TEST_DATA"]
Paul Duffin32b06c22021-03-16 07:50:37 +000084 android.AssertDeepEquals(t, "LOCAL_TEST_DATA", expectedData, actualData)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070085}
Jaewoong Jung61a83682019-07-01 09:08:50 -070086
Jaewoong Jung6e0eee52020-05-29 16:15:32 -070087func TestShTest_dataModules(t *testing.T) {
Paul Duffin32b06c22021-03-16 07:50:37 +000088 ctx, config := testShBinary(t, `
Jaewoong Jung6e0eee52020-05-29 16:15:32 -070089 sh_test {
90 name: "foo",
91 src: "test.sh",
92 host_supported: true,
93 data_bins: ["bar"],
94 data_libs: ["libbar"],
95 }
96
97 cc_binary {
98 name: "bar",
99 host_supported: true,
100 shared_libs: ["libbar"],
101 no_libcrt: true,
102 nocrt: true,
103 system_shared_libs: [],
104 stl: "none",
105 }
106
107 cc_library {
108 name: "libbar",
109 host_supported: true,
110 no_libcrt: true,
111 nocrt: true,
112 system_shared_libs: [],
113 stl: "none",
114 }
115 `)
116
117 buildOS := android.BuildOs.String()
118 arches := []string{"android_arm64_armv8-a", buildOS + "_x86_64"}
119 for _, arch := range arches {
120 variant := ctx.ModuleForTests("foo", arch)
121
122 libExt := ".so"
123 if arch == "darwin_x86_64" {
124 libExt = ".dylib"
125 }
126 relocated := variant.Output("relocated/lib64/libbar" + libExt)
Paul Duffin32b06c22021-03-16 07:50:37 +0000127 expectedInput := "out/soong/.intermediates/libbar/" + arch + "_shared/libbar" + libExt
128 android.AssertPathRelativeToTopEquals(t, "relocation input", expectedInput, relocated.Input)
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700129
130 mod := variant.Module().(*ShTest)
Colin Crossaa255532020-07-03 13:18:24 -0700131 entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700132 expectedData := []string{
Paul Duffin32b06c22021-03-16 07:50:37 +0000133 filepath.Join("out/soong/.intermediates/bar", arch, ":bar"),
134 filepath.Join("out/soong/.intermediates/foo", arch, "relocated/:lib64/libbar"+libExt),
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700135 }
136 actualData := entries.EntryMap["LOCAL_TEST_DATA"]
Paul Duffin32b06c22021-03-16 07:50:37 +0000137 android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_TEST_DATA", config, expectedData, actualData)
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700138 }
139}
140
Jaewoong Jung61a83682019-07-01 09:08:50 -0700141func TestShTestHost(t *testing.T) {
142 ctx, _ := testShBinary(t, `
143 sh_test_host {
144 name: "foo",
145 src: "test.sh",
146 filename: "test.sh",
147 data: [
148 "testdata/data1",
149 "testdata/sub/data2",
150 ],
151 }
152 `)
153
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700154 buildOS := android.BuildOs.String()
Jaewoong Jung61a83682019-07-01 09:08:50 -0700155 mod := ctx.ModuleForTests("foo", buildOS+"_x86_64").Module().(*ShTest)
156 if !mod.Host() {
157 t.Errorf("host bit is not set for a sh_test_host module.")
158 }
159}
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700160
161func TestShTestHost_dataDeviceModules(t *testing.T) {
Paul Duffin32b06c22021-03-16 07:50:37 +0000162 ctx, config := testShBinary(t, `
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700163 sh_test_host {
164 name: "foo",
165 src: "test.sh",
166 data_device_bins: ["bar"],
167 data_device_libs: ["libbar"],
168 }
169
170 cc_binary {
171 name: "bar",
172 shared_libs: ["libbar"],
173 no_libcrt: true,
174 nocrt: true,
175 system_shared_libs: [],
176 stl: "none",
177 }
178
179 cc_library {
180 name: "libbar",
181 no_libcrt: true,
182 nocrt: true,
183 system_shared_libs: [],
184 stl: "none",
185 }
186 `)
187
188 buildOS := android.BuildOs.String()
189 variant := ctx.ModuleForTests("foo", buildOS+"_x86_64")
190
191 relocated := variant.Output("relocated/lib64/libbar.so")
Paul Duffin32b06c22021-03-16 07:50:37 +0000192 expectedInput := "out/soong/.intermediates/libbar/android_arm64_armv8-a_shared/libbar.so"
193 android.AssertPathRelativeToTopEquals(t, "relocation input", expectedInput, relocated.Input)
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700194
195 mod := variant.Module().(*ShTest)
Colin Crossaa255532020-07-03 13:18:24 -0700196 entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700197 expectedData := []string{
Paul Duffin32b06c22021-03-16 07:50:37 +0000198 "out/soong/.intermediates/bar/android_arm64_armv8-a/:bar",
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700199 // libbar has been relocated, and so has a variant that matches the host arch.
Paul Duffin32b06c22021-03-16 07:50:37 +0000200 "out/soong/.intermediates/foo/" + buildOS + "_x86_64/relocated/:lib64/libbar.so",
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700201 }
202 actualData := entries.EntryMap["LOCAL_TEST_DATA"]
Paul Duffin32b06c22021-03-16 07:50:37 +0000203 android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_TEST_DATA", config, expectedData, actualData)
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700204}