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 | "os" |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 5 | "path/filepath" |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 6 | "testing" |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 7 | |
| 8 | "android/soong/android" |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 9 | "android/soong/cc" |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 10 | ) |
| 11 | |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 12 | func TestMain(m *testing.M) { |
Paul Duffin | 32b06c2 | 2021-03-16 07:50:37 +0000 | [diff] [blame] | 13 | os.Exit(m.Run()) |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 14 | } |
| 15 | |
Paul Duffin | 89648f9 | 2021-03-20 00:36:55 +0000 | [diff] [blame^] | 16 | var prepareForShTest = android.GroupFixturePreparers( |
Paul Duffin | 56fb8ee | 2021-03-08 15:05:52 +0000 | [diff] [blame] | 17 | cc.PrepareForTestWithCcBuildComponents, |
| 18 | PrepareForTestWithShBuildComponents, |
| 19 | android.FixtureMergeMockFs(android.MockFS{ |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 20 | "test.sh": nil, |
| 21 | "testdata/data1": nil, |
| 22 | "testdata/sub/data2": nil, |
Paul Duffin | 56fb8ee | 2021-03-08 15:05:52 +0000 | [diff] [blame] | 23 | }), |
| 24 | ) |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 25 | |
Paul Duffin | 89648f9 | 2021-03-20 00:36:55 +0000 | [diff] [blame^] | 26 | // testShBinary runs tests using the prepareForShTest |
Paul Duffin | 56fb8ee | 2021-03-08 15:05:52 +0000 | [diff] [blame] | 27 | // |
Paul Duffin | 89648f9 | 2021-03-20 00:36:55 +0000 | [diff] [blame^] | 28 | // Do not add any new usages of this, instead use the prepareForShTest directly as it makes it much |
Paul Duffin | 56fb8ee | 2021-03-08 15:05:52 +0000 | [diff] [blame] | 29 | // 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 Duffin | 89648f9 | 2021-03-20 00:36:55 +0000 | [diff] [blame^] | 32 | // convert the test to using prepareForShTest first and then in a following change add the |
Paul Duffin | 56fb8ee | 2021-03-08 15:05:52 +0000 | [diff] [blame] | 33 | // 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 |
| 37 | func testShBinary(t *testing.T, bp string) (*android.TestContext, android.Config) { |
Paul Duffin | 89648f9 | 2021-03-20 00:36:55 +0000 | [diff] [blame^] | 38 | result := prepareForShTest.RunTestWithBp(t, bp) |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 39 | |
Paul Duffin | 56fb8ee | 2021-03-08 15:05:52 +0000 | [diff] [blame] | 40 | return result.TestContext, result.Config |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Jaewoong Jung | 4aedc86 | 2020-06-10 17:23:46 -0700 | [diff] [blame] | 43 | func TestShTestSubDir(t *testing.T) { |
Paul Duffin | 32b06c2 | 2021-03-16 07:50:37 +0000 | [diff] [blame] | 44 | ctx, config := testShBinary(t, ` |
Jaewoong Jung | 4aedc86 | 2020-06-10 17:23:46 -0700 | [diff] [blame] | 45 | 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 Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 54 | entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0] |
Jaewoong Jung | 4aedc86 | 2020-06-10 17:23:46 -0700 | [diff] [blame] | 55 | |
Paul Duffin | 32b06c2 | 2021-03-16 07:50:37 +0000 | [diff] [blame] | 56 | expectedPath := "out/target/product/test_device/data/nativetest64/foo_test" |
Jaewoong Jung | 4aedc86 | 2020-06-10 17:23:46 -0700 | [diff] [blame] | 57 | actualPath := entries.EntryMap["LOCAL_MODULE_PATH"][0] |
Paul Duffin | 32b06c2 | 2021-03-16 07:50:37 +0000 | [diff] [blame] | 58 | android.AssertStringPathRelativeToTopEquals(t, "LOCAL_MODULE_PATH[0]", config, expectedPath, actualPath) |
Jaewoong Jung | 4aedc86 | 2020-06-10 17:23:46 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | func TestShTest(t *testing.T) { |
Paul Duffin | 32b06c2 | 2021-03-16 07:50:37 +0000 | [diff] [blame] | 62 | ctx, config := testShBinary(t, ` |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 63 | 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 Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 76 | entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0] |
Jaewoong Jung | 4aedc86 | 2020-06-10 17:23:46 -0700 | [diff] [blame] | 77 | |
Paul Duffin | 32b06c2 | 2021-03-16 07:50:37 +0000 | [diff] [blame] | 78 | expectedPath := "out/target/product/test_device/data/nativetest64/foo" |
Jaewoong Jung | 4aedc86 | 2020-06-10 17:23:46 -0700 | [diff] [blame] | 79 | actualPath := entries.EntryMap["LOCAL_MODULE_PATH"][0] |
Paul Duffin | 32b06c2 | 2021-03-16 07:50:37 +0000 | [diff] [blame] | 80 | android.AssertStringPathRelativeToTopEquals(t, "LOCAL_MODULE_PATH[0]", config, expectedPath, actualPath) |
Jaewoong Jung | 4aedc86 | 2020-06-10 17:23:46 -0700 | [diff] [blame] | 81 | |
| 82 | expectedData := []string{":testdata/data1", ":testdata/sub/data2"} |
| 83 | actualData := entries.EntryMap["LOCAL_TEST_DATA"] |
Paul Duffin | 32b06c2 | 2021-03-16 07:50:37 +0000 | [diff] [blame] | 84 | android.AssertDeepEquals(t, "LOCAL_TEST_DATA", expectedData, actualData) |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 85 | } |
Jaewoong Jung | 61a8368 | 2019-07-01 09:08:50 -0700 | [diff] [blame] | 86 | |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 87 | func TestShTest_dataModules(t *testing.T) { |
Paul Duffin | 32b06c2 | 2021-03-16 07:50:37 +0000 | [diff] [blame] | 88 | ctx, config := testShBinary(t, ` |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 89 | 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 Duffin | 32b06c2 | 2021-03-16 07:50:37 +0000 | [diff] [blame] | 127 | expectedInput := "out/soong/.intermediates/libbar/" + arch + "_shared/libbar" + libExt |
| 128 | android.AssertPathRelativeToTopEquals(t, "relocation input", expectedInput, relocated.Input) |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 129 | |
| 130 | mod := variant.Module().(*ShTest) |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 131 | entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0] |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 132 | expectedData := []string{ |
Paul Duffin | 32b06c2 | 2021-03-16 07:50:37 +0000 | [diff] [blame] | 133 | filepath.Join("out/soong/.intermediates/bar", arch, ":bar"), |
| 134 | filepath.Join("out/soong/.intermediates/foo", arch, "relocated/:lib64/libbar"+libExt), |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 135 | } |
| 136 | actualData := entries.EntryMap["LOCAL_TEST_DATA"] |
Paul Duffin | 32b06c2 | 2021-03-16 07:50:37 +0000 | [diff] [blame] | 137 | android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_TEST_DATA", config, expectedData, actualData) |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
Jaewoong Jung | 61a8368 | 2019-07-01 09:08:50 -0700 | [diff] [blame] | 141 | func 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 Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 154 | buildOS := android.BuildOs.String() |
Jaewoong Jung | 61a8368 | 2019-07-01 09:08:50 -0700 | [diff] [blame] | 155 | 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 Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 160 | |
| 161 | func TestShTestHost_dataDeviceModules(t *testing.T) { |
Paul Duffin | 32b06c2 | 2021-03-16 07:50:37 +0000 | [diff] [blame] | 162 | ctx, config := testShBinary(t, ` |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 163 | 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 Duffin | 32b06c2 | 2021-03-16 07:50:37 +0000 | [diff] [blame] | 192 | expectedInput := "out/soong/.intermediates/libbar/android_arm64_armv8-a_shared/libbar.so" |
| 193 | android.AssertPathRelativeToTopEquals(t, "relocation input", expectedInput, relocated.Input) |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 194 | |
| 195 | mod := variant.Module().(*ShTest) |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 196 | entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0] |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 197 | expectedData := []string{ |
Paul Duffin | 32b06c2 | 2021-03-16 07:50:37 +0000 | [diff] [blame] | 198 | "out/soong/.intermediates/bar/android_arm64_armv8-a/:bar", |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 199 | // libbar has been relocated, and so has a variant that matches the host arch. |
Paul Duffin | 32b06c2 | 2021-03-16 07:50:37 +0000 | [diff] [blame] | 200 | "out/soong/.intermediates/foo/" + buildOS + "_x86_64/relocated/:lib64/libbar.so", |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 201 | } |
| 202 | actualData := entries.EntryMap["LOCAL_TEST_DATA"] |
Paul Duffin | 32b06c2 | 2021-03-16 07:50:37 +0000 | [diff] [blame] | 203 | android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_TEST_DATA", config, expectedData, actualData) |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 204 | } |