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 | |
Paul Duffin | 56fb8ee | 2021-03-08 15:05:52 +0000 | [diff] [blame^] | 40 | var shFixtureFactory = android.NewFixtureFactory( |
| 41 | &buildDir, |
| 42 | cc.PrepareForTestWithCcBuildComponents, |
| 43 | PrepareForTestWithShBuildComponents, |
| 44 | android.FixtureMergeMockFs(android.MockFS{ |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 45 | "test.sh": nil, |
| 46 | "testdata/data1": nil, |
| 47 | "testdata/sub/data2": nil, |
Paul Duffin | 56fb8ee | 2021-03-08 15:05:52 +0000 | [diff] [blame^] | 48 | }), |
| 49 | ) |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 50 | |
Paul Duffin | 56fb8ee | 2021-03-08 15:05:52 +0000 | [diff] [blame^] | 51 | // testShBinary runs tests using the shFixtureFactory |
| 52 | // |
| 53 | // Do not add any new usages of this, instead use the shFixtureFactory directly as it makes it much |
| 54 | // easier to customize the test behavior. |
| 55 | // |
| 56 | // If it is necessary to customize the behavior of an existing test that uses this then please first |
| 57 | // convert the test to using shFixtureFactory first and then in a following change add the |
| 58 | // appropriate fixture preparers. Keeping the conversion change separate makes it easy to verify |
| 59 | // that it did not change the test behavior unexpectedly. |
| 60 | // |
| 61 | // deprecated |
| 62 | func testShBinary(t *testing.T, bp string) (*android.TestContext, android.Config) { |
| 63 | result := shFixtureFactory.RunTestWithBp(t, bp) |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 64 | |
Paul Duffin | 56fb8ee | 2021-03-08 15:05:52 +0000 | [diff] [blame^] | 65 | return result.TestContext, result.Config |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Jaewoong Jung | 4aedc86 | 2020-06-10 17:23:46 -0700 | [diff] [blame] | 68 | func TestShTestSubDir(t *testing.T) { |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 69 | ctx, _ := testShBinary(t, ` |
Jaewoong Jung | 4aedc86 | 2020-06-10 17:23:46 -0700 | [diff] [blame] | 70 | sh_test { |
| 71 | name: "foo", |
| 72 | src: "test.sh", |
| 73 | sub_dir: "foo_test" |
| 74 | } |
| 75 | `) |
| 76 | |
| 77 | mod := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*ShTest) |
| 78 | |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 79 | entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0] |
Jaewoong Jung | 4aedc86 | 2020-06-10 17:23:46 -0700 | [diff] [blame] | 80 | |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 81 | expectedPath := path.Join(buildDir, |
| 82 | "../target/product/test_device/data/nativetest64/foo_test") |
Jaewoong Jung | 4aedc86 | 2020-06-10 17:23:46 -0700 | [diff] [blame] | 83 | actualPath := entries.EntryMap["LOCAL_MODULE_PATH"][0] |
| 84 | if expectedPath != actualPath { |
| 85 | t.Errorf("Unexpected LOCAL_MODULE_PATH expected: %q, actual: %q", expectedPath, actualPath) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | func TestShTest(t *testing.T) { |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 90 | ctx, _ := testShBinary(t, ` |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 91 | sh_test { |
| 92 | name: "foo", |
| 93 | src: "test.sh", |
| 94 | filename: "test.sh", |
| 95 | data: [ |
| 96 | "testdata/data1", |
| 97 | "testdata/sub/data2", |
| 98 | ], |
| 99 | } |
| 100 | `) |
| 101 | |
| 102 | mod := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*ShTest) |
| 103 | |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 104 | entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0] |
Jaewoong Jung | 4aedc86 | 2020-06-10 17:23:46 -0700 | [diff] [blame] | 105 | |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 106 | expectedPath := path.Join(buildDir, |
| 107 | "../target/product/test_device/data/nativetest64/foo") |
Jaewoong Jung | 4aedc86 | 2020-06-10 17:23:46 -0700 | [diff] [blame] | 108 | actualPath := entries.EntryMap["LOCAL_MODULE_PATH"][0] |
| 109 | if expectedPath != actualPath { |
| 110 | t.Errorf("Unexpected LOCAL_MODULE_PATH expected: %q, actual: %q", expectedPath, actualPath) |
| 111 | } |
| 112 | |
| 113 | expectedData := []string{":testdata/data1", ":testdata/sub/data2"} |
| 114 | actualData := entries.EntryMap["LOCAL_TEST_DATA"] |
| 115 | if !reflect.DeepEqual(expectedData, actualData) { |
| 116 | t.Errorf("Unexpected test data expected: %q, actual: %q", expectedData, actualData) |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 117 | } |
| 118 | } |
Jaewoong Jung | 61a8368 | 2019-07-01 09:08:50 -0700 | [diff] [blame] | 119 | |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 120 | func TestShTest_dataModules(t *testing.T) { |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 121 | ctx, _ := testShBinary(t, ` |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 122 | sh_test { |
| 123 | name: "foo", |
| 124 | src: "test.sh", |
| 125 | host_supported: true, |
| 126 | data_bins: ["bar"], |
| 127 | data_libs: ["libbar"], |
| 128 | } |
| 129 | |
| 130 | cc_binary { |
| 131 | name: "bar", |
| 132 | host_supported: true, |
| 133 | shared_libs: ["libbar"], |
| 134 | no_libcrt: true, |
| 135 | nocrt: true, |
| 136 | system_shared_libs: [], |
| 137 | stl: "none", |
| 138 | } |
| 139 | |
| 140 | cc_library { |
| 141 | name: "libbar", |
| 142 | host_supported: true, |
| 143 | no_libcrt: true, |
| 144 | nocrt: true, |
| 145 | system_shared_libs: [], |
| 146 | stl: "none", |
| 147 | } |
| 148 | `) |
| 149 | |
| 150 | buildOS := android.BuildOs.String() |
| 151 | arches := []string{"android_arm64_armv8-a", buildOS + "_x86_64"} |
| 152 | for _, arch := range arches { |
| 153 | variant := ctx.ModuleForTests("foo", arch) |
| 154 | |
| 155 | libExt := ".so" |
| 156 | if arch == "darwin_x86_64" { |
| 157 | libExt = ".dylib" |
| 158 | } |
| 159 | relocated := variant.Output("relocated/lib64/libbar" + libExt) |
| 160 | expectedInput := filepath.Join(buildDir, ".intermediates/libbar/"+arch+"_shared/libbar"+libExt) |
| 161 | if relocated.Input.String() != expectedInput { |
| 162 | t.Errorf("Unexpected relocation input, expected: %q, actual: %q", |
| 163 | expectedInput, relocated.Input.String()) |
| 164 | } |
| 165 | |
| 166 | mod := variant.Module().(*ShTest) |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 167 | entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0] |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 168 | expectedData := []string{ |
| 169 | filepath.Join(buildDir, ".intermediates/bar", arch, ":bar"), |
| 170 | filepath.Join(buildDir, ".intermediates/foo", arch, "relocated/:lib64/libbar"+libExt), |
| 171 | } |
| 172 | actualData := entries.EntryMap["LOCAL_TEST_DATA"] |
| 173 | if !reflect.DeepEqual(expectedData, actualData) { |
| 174 | t.Errorf("Unexpected test data, expected: %q, actual: %q", expectedData, actualData) |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
Jaewoong Jung | 61a8368 | 2019-07-01 09:08:50 -0700 | [diff] [blame] | 179 | func TestShTestHost(t *testing.T) { |
| 180 | ctx, _ := testShBinary(t, ` |
| 181 | sh_test_host { |
| 182 | name: "foo", |
| 183 | src: "test.sh", |
| 184 | filename: "test.sh", |
| 185 | data: [ |
| 186 | "testdata/data1", |
| 187 | "testdata/sub/data2", |
| 188 | ], |
| 189 | } |
| 190 | `) |
| 191 | |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 192 | buildOS := android.BuildOs.String() |
Jaewoong Jung | 61a8368 | 2019-07-01 09:08:50 -0700 | [diff] [blame] | 193 | mod := ctx.ModuleForTests("foo", buildOS+"_x86_64").Module().(*ShTest) |
| 194 | if !mod.Host() { |
| 195 | t.Errorf("host bit is not set for a sh_test_host module.") |
| 196 | } |
| 197 | } |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 198 | |
| 199 | func TestShTestHost_dataDeviceModules(t *testing.T) { |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 200 | ctx, _ := testShBinary(t, ` |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 201 | sh_test_host { |
| 202 | name: "foo", |
| 203 | src: "test.sh", |
| 204 | data_device_bins: ["bar"], |
| 205 | data_device_libs: ["libbar"], |
| 206 | } |
| 207 | |
| 208 | cc_binary { |
| 209 | name: "bar", |
| 210 | shared_libs: ["libbar"], |
| 211 | no_libcrt: true, |
| 212 | nocrt: true, |
| 213 | system_shared_libs: [], |
| 214 | stl: "none", |
| 215 | } |
| 216 | |
| 217 | cc_library { |
| 218 | name: "libbar", |
| 219 | no_libcrt: true, |
| 220 | nocrt: true, |
| 221 | system_shared_libs: [], |
| 222 | stl: "none", |
| 223 | } |
| 224 | `) |
| 225 | |
| 226 | buildOS := android.BuildOs.String() |
| 227 | variant := ctx.ModuleForTests("foo", buildOS+"_x86_64") |
| 228 | |
| 229 | relocated := variant.Output("relocated/lib64/libbar.so") |
| 230 | expectedInput := filepath.Join(buildDir, ".intermediates/libbar/android_arm64_armv8-a_shared/libbar.so") |
| 231 | if relocated.Input.String() != expectedInput { |
| 232 | t.Errorf("Unexpected relocation input, expected: %q, actual: %q", |
| 233 | expectedInput, relocated.Input.String()) |
| 234 | } |
| 235 | |
| 236 | mod := variant.Module().(*ShTest) |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 237 | entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0] |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 238 | expectedData := []string{ |
| 239 | filepath.Join(buildDir, ".intermediates/bar/android_arm64_armv8-a/:bar"), |
| 240 | // libbar has been relocated, and so has a variant that matches the host arch. |
| 241 | filepath.Join(buildDir, ".intermediates/foo/"+buildOS+"_x86_64/relocated/:lib64/libbar.so"), |
| 242 | } |
| 243 | actualData := entries.EntryMap["LOCAL_TEST_DATA"] |
| 244 | if !reflect.DeepEqual(expectedData, actualData) { |
| 245 | t.Errorf("Unexpected test data, expected: %q, actual: %q", expectedData, actualData) |
| 246 | } |
| 247 | } |