blob: 5887b56af3c3d9e86bebeaeaeac5978a17df8be5 [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 Duffin56fb8ee2021-03-08 15:05:52 +000016var shFixtureFactory = android.NewFixtureFactory(
Paul Duffin32b06c22021-03-16 07:50:37 +000017 nil,
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 Duffin56fb8ee2021-03-08 15:05:52 +000027// testShBinary runs tests using the shFixtureFactory
28//
29// Do not add any new usages of this, instead use the shFixtureFactory directly as it makes it much
30// 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
33// convert the test to using shFixtureFactory first and then in a following change add the
34// 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) {
39 result := shFixtureFactory.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
118 buildOS := android.BuildOs.String()
119 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 ],
152 }
153 `)
154
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700155 buildOS := android.BuildOs.String()
Jaewoong Jung61a83682019-07-01 09:08:50 -0700156 mod := ctx.ModuleForTests("foo", buildOS+"_x86_64").Module().(*ShTest)
157 if !mod.Host() {
158 t.Errorf("host bit is not set for a sh_test_host module.")
159 }
160}
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700161
162func TestShTestHost_dataDeviceModules(t *testing.T) {
Paul Duffin32b06c22021-03-16 07:50:37 +0000163 ctx, config := testShBinary(t, `
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700164 sh_test_host {
165 name: "foo",
166 src: "test.sh",
167 data_device_bins: ["bar"],
168 data_device_libs: ["libbar"],
169 }
170
171 cc_binary {
172 name: "bar",
173 shared_libs: ["libbar"],
174 no_libcrt: true,
175 nocrt: true,
176 system_shared_libs: [],
177 stl: "none",
178 }
179
180 cc_library {
181 name: "libbar",
182 no_libcrt: true,
183 nocrt: true,
184 system_shared_libs: [],
185 stl: "none",
186 }
187 `)
188
189 buildOS := android.BuildOs.String()
190 variant := ctx.ModuleForTests("foo", buildOS+"_x86_64")
191
192 relocated := variant.Output("relocated/lib64/libbar.so")
Paul Duffin32b06c22021-03-16 07:50:37 +0000193 expectedInput := "out/soong/.intermediates/libbar/android_arm64_armv8-a_shared/libbar.so"
194 android.AssertPathRelativeToTopEquals(t, "relocation input", expectedInput, relocated.Input)
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700195
196 mod := variant.Module().(*ShTest)
Colin Crossaa255532020-07-03 13:18:24 -0700197 entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700198 expectedData := []string{
Paul Duffin32b06c22021-03-16 07:50:37 +0000199 "out/soong/.intermediates/bar/android_arm64_armv8-a/:bar",
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700200 // libbar has been relocated, and so has a variant that matches the host arch.
Paul Duffin32b06c22021-03-16 07:50:37 +0000201 "out/soong/.intermediates/foo/" + buildOS + "_x86_64/relocated/:lib64/libbar.so",
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700202 }
203 actualData := entries.EntryMap["LOCAL_TEST_DATA"]
Paul Duffin32b06c22021-03-16 07:50:37 +0000204 android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_TEST_DATA", config, expectedData, actualData)
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700205}