Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 1 | package sh |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 2 | |
| 3 | import ( |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 4 | "io/ioutil" |
| 5 | "os" |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 6 | "path" |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 7 | "path/filepath" |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 8 | "reflect" |
| 9 | "testing" |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 10 | |
| 11 | "android/soong/android" |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 12 | "android/soong/cc" |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 13 | ) |
| 14 | |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 15 | var buildDir string |
| 16 | |
| 17 | func setUp() { |
| 18 | var err error |
| 19 | buildDir, err = ioutil.TempDir("", "soong_sh_test") |
| 20 | if err != nil { |
| 21 | panic(err) |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | func tearDown() { |
| 26 | os.RemoveAll(buildDir) |
| 27 | } |
| 28 | |
| 29 | func TestMain(m *testing.M) { |
| 30 | run := func() int { |
| 31 | setUp() |
| 32 | defer tearDown() |
| 33 | |
| 34 | return m.Run() |
| 35 | } |
| 36 | |
| 37 | os.Exit(run()) |
| 38 | } |
| 39 | |
| 40 | func testShBinary(t *testing.T, bp string) (*android.TestContext, android.Config) { |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 41 | fs := map[string][]byte{ |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 42 | "test.sh": nil, |
| 43 | "testdata/data1": nil, |
| 44 | "testdata/sub/data2": nil, |
| 45 | } |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 46 | |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 47 | config := android.TestArchConfig(buildDir, nil, bp, fs) |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 48 | |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 49 | ctx := android.NewTestArchContext(config) |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 50 | ctx.RegisterModuleType("sh_test", ShTestFactory) |
| 51 | ctx.RegisterModuleType("sh_test_host", ShTestHostFactory) |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 52 | |
| 53 | cc.RegisterRequiredBuildComponentsForTest(ctx) |
| 54 | |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 55 | ctx.Register() |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 56 | _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 57 | android.FailIfErrored(t, errs) |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 58 | _, errs = ctx.PrepareBuildActions(config) |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 59 | android.FailIfErrored(t, errs) |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 60 | |
| 61 | return ctx, config |
| 62 | } |
| 63 | |
Jaewoong Jung | 4aedc86 | 2020-06-10 17:23:46 -0700 | [diff] [blame] | 64 | func TestShTestSubDir(t *testing.T) { |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 65 | ctx, _ := testShBinary(t, ` |
Jaewoong Jung | 4aedc86 | 2020-06-10 17:23:46 -0700 | [diff] [blame] | 66 | sh_test { |
| 67 | name: "foo", |
| 68 | src: "test.sh", |
| 69 | sub_dir: "foo_test" |
| 70 | } |
| 71 | `) |
| 72 | |
| 73 | mod := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*ShTest) |
| 74 | |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 75 | entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0] |
Jaewoong Jung | 4aedc86 | 2020-06-10 17:23:46 -0700 | [diff] [blame] | 76 | |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 77 | expectedPath := path.Join(buildDir, |
| 78 | "../target/product/test_device/data/nativetest64/foo_test") |
Jaewoong Jung | 4aedc86 | 2020-06-10 17:23:46 -0700 | [diff] [blame] | 79 | actualPath := entries.EntryMap["LOCAL_MODULE_PATH"][0] |
| 80 | if expectedPath != actualPath { |
| 81 | t.Errorf("Unexpected LOCAL_MODULE_PATH expected: %q, actual: %q", expectedPath, actualPath) |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | func TestShTest(t *testing.T) { |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 86 | ctx, _ := testShBinary(t, ` |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 87 | sh_test { |
| 88 | name: "foo", |
| 89 | src: "test.sh", |
| 90 | filename: "test.sh", |
| 91 | data: [ |
| 92 | "testdata/data1", |
| 93 | "testdata/sub/data2", |
| 94 | ], |
| 95 | } |
| 96 | `) |
| 97 | |
| 98 | mod := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*ShTest) |
| 99 | |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 100 | entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0] |
Jaewoong Jung | 4aedc86 | 2020-06-10 17:23:46 -0700 | [diff] [blame] | 101 | |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 102 | expectedPath := path.Join(buildDir, |
| 103 | "../target/product/test_device/data/nativetest64/foo") |
Jaewoong Jung | 4aedc86 | 2020-06-10 17:23:46 -0700 | [diff] [blame] | 104 | actualPath := entries.EntryMap["LOCAL_MODULE_PATH"][0] |
| 105 | if expectedPath != actualPath { |
| 106 | t.Errorf("Unexpected LOCAL_MODULE_PATH expected: %q, actual: %q", expectedPath, actualPath) |
| 107 | } |
| 108 | |
| 109 | expectedData := []string{":testdata/data1", ":testdata/sub/data2"} |
| 110 | actualData := entries.EntryMap["LOCAL_TEST_DATA"] |
| 111 | if !reflect.DeepEqual(expectedData, actualData) { |
| 112 | t.Errorf("Unexpected test data expected: %q, actual: %q", expectedData, actualData) |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 113 | } |
| 114 | } |
Jaewoong Jung | 61a8368 | 2019-07-01 09:08:50 -0700 | [diff] [blame] | 115 | |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 116 | func TestShTest_dataModules(t *testing.T) { |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 117 | ctx, _ := testShBinary(t, ` |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 118 | sh_test { |
| 119 | name: "foo", |
| 120 | src: "test.sh", |
| 121 | host_supported: true, |
| 122 | data_bins: ["bar"], |
| 123 | data_libs: ["libbar"], |
| 124 | } |
| 125 | |
| 126 | cc_binary { |
| 127 | name: "bar", |
| 128 | host_supported: true, |
| 129 | shared_libs: ["libbar"], |
| 130 | no_libcrt: true, |
| 131 | nocrt: true, |
| 132 | system_shared_libs: [], |
| 133 | stl: "none", |
| 134 | } |
| 135 | |
| 136 | cc_library { |
| 137 | name: "libbar", |
| 138 | host_supported: true, |
| 139 | no_libcrt: true, |
| 140 | nocrt: true, |
| 141 | system_shared_libs: [], |
| 142 | stl: "none", |
| 143 | } |
| 144 | `) |
| 145 | |
| 146 | buildOS := android.BuildOs.String() |
| 147 | arches := []string{"android_arm64_armv8-a", buildOS + "_x86_64"} |
| 148 | for _, arch := range arches { |
| 149 | variant := ctx.ModuleForTests("foo", arch) |
| 150 | |
| 151 | libExt := ".so" |
| 152 | if arch == "darwin_x86_64" { |
| 153 | libExt = ".dylib" |
| 154 | } |
| 155 | relocated := variant.Output("relocated/lib64/libbar" + libExt) |
| 156 | expectedInput := filepath.Join(buildDir, ".intermediates/libbar/"+arch+"_shared/libbar"+libExt) |
| 157 | if relocated.Input.String() != expectedInput { |
| 158 | t.Errorf("Unexpected relocation input, expected: %q, actual: %q", |
| 159 | expectedInput, relocated.Input.String()) |
| 160 | } |
| 161 | |
| 162 | mod := variant.Module().(*ShTest) |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 163 | entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0] |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 164 | expectedData := []string{ |
| 165 | filepath.Join(buildDir, ".intermediates/bar", arch, ":bar"), |
| 166 | filepath.Join(buildDir, ".intermediates/foo", arch, "relocated/:lib64/libbar"+libExt), |
| 167 | } |
| 168 | actualData := entries.EntryMap["LOCAL_TEST_DATA"] |
| 169 | if !reflect.DeepEqual(expectedData, actualData) { |
| 170 | t.Errorf("Unexpected test data, expected: %q, actual: %q", expectedData, actualData) |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
Jaewoong Jung | 61a8368 | 2019-07-01 09:08:50 -0700 | [diff] [blame] | 175 | func TestShTestHost(t *testing.T) { |
| 176 | ctx, _ := testShBinary(t, ` |
| 177 | sh_test_host { |
| 178 | name: "foo", |
| 179 | src: "test.sh", |
| 180 | filename: "test.sh", |
| 181 | data: [ |
| 182 | "testdata/data1", |
| 183 | "testdata/sub/data2", |
| 184 | ], |
| 185 | } |
| 186 | `) |
| 187 | |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 188 | buildOS := android.BuildOs.String() |
Jaewoong Jung | 61a8368 | 2019-07-01 09:08:50 -0700 | [diff] [blame] | 189 | mod := ctx.ModuleForTests("foo", buildOS+"_x86_64").Module().(*ShTest) |
| 190 | if !mod.Host() { |
| 191 | t.Errorf("host bit is not set for a sh_test_host module.") |
| 192 | } |
| 193 | } |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 194 | |
| 195 | func TestShTestHost_dataDeviceModules(t *testing.T) { |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 196 | ctx, _ := testShBinary(t, ` |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 197 | sh_test_host { |
| 198 | name: "foo", |
| 199 | src: "test.sh", |
| 200 | data_device_bins: ["bar"], |
| 201 | data_device_libs: ["libbar"], |
| 202 | } |
| 203 | |
| 204 | cc_binary { |
| 205 | name: "bar", |
| 206 | shared_libs: ["libbar"], |
| 207 | no_libcrt: true, |
| 208 | nocrt: true, |
| 209 | system_shared_libs: [], |
| 210 | stl: "none", |
| 211 | } |
| 212 | |
| 213 | cc_library { |
| 214 | name: "libbar", |
| 215 | no_libcrt: true, |
| 216 | nocrt: true, |
| 217 | system_shared_libs: [], |
| 218 | stl: "none", |
| 219 | } |
| 220 | `) |
| 221 | |
| 222 | buildOS := android.BuildOs.String() |
| 223 | variant := ctx.ModuleForTests("foo", buildOS+"_x86_64") |
| 224 | |
| 225 | relocated := variant.Output("relocated/lib64/libbar.so") |
| 226 | expectedInput := filepath.Join(buildDir, ".intermediates/libbar/android_arm64_armv8-a_shared/libbar.so") |
| 227 | if relocated.Input.String() != expectedInput { |
| 228 | t.Errorf("Unexpected relocation input, expected: %q, actual: %q", |
| 229 | expectedInput, relocated.Input.String()) |
| 230 | } |
| 231 | |
| 232 | mod := variant.Module().(*ShTest) |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 233 | entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0] |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 234 | expectedData := []string{ |
| 235 | filepath.Join(buildDir, ".intermediates/bar/android_arm64_armv8-a/:bar"), |
| 236 | // libbar has been relocated, and so has a variant that matches the host arch. |
| 237 | filepath.Join(buildDir, ".intermediates/foo/"+buildOS+"_x86_64/relocated/:lib64/libbar.so"), |
| 238 | } |
| 239 | actualData := entries.EntryMap["LOCAL_TEST_DATA"] |
| 240 | if !reflect.DeepEqual(expectedData, actualData) { |
| 241 | t.Errorf("Unexpected test data, expected: %q, actual: %q", expectedData, actualData) |
| 242 | } |
| 243 | } |