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" |
Dan Shi | b40deac | 2021-05-24 12:04:54 -0700 | [diff] [blame] | 6 | "strconv" |
Sam Delmerico | b3342ce | 2022-01-20 21:10:28 +0000 | [diff] [blame] | 7 | "strings" |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 8 | "testing" |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 9 | |
| 10 | "android/soong/android" |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 11 | "android/soong/cc" |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 12 | ) |
| 13 | |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 14 | func TestMain(m *testing.M) { |
Paul Duffin | 32b06c2 | 2021-03-16 07:50:37 +0000 | [diff] [blame] | 15 | os.Exit(m.Run()) |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 16 | } |
| 17 | |
Paul Duffin | 89648f9 | 2021-03-20 00:36:55 +0000 | [diff] [blame] | 18 | var prepareForShTest = android.GroupFixturePreparers( |
Paul Duffin | 56fb8ee | 2021-03-08 15:05:52 +0000 | [diff] [blame] | 19 | cc.PrepareForTestWithCcBuildComponents, |
| 20 | PrepareForTestWithShBuildComponents, |
| 21 | android.FixtureMergeMockFs(android.MockFS{ |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 22 | "test.sh": nil, |
| 23 | "testdata/data1": nil, |
| 24 | "testdata/sub/data2": nil, |
Paul Duffin | 56fb8ee | 2021-03-08 15:05:52 +0000 | [diff] [blame] | 25 | }), |
| 26 | ) |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 27 | |
Paul Duffin | 89648f9 | 2021-03-20 00:36:55 +0000 | [diff] [blame] | 28 | // testShBinary runs tests using the prepareForShTest |
Paul Duffin | 56fb8ee | 2021-03-08 15:05:52 +0000 | [diff] [blame] | 29 | // |
Paul Duffin | 89648f9 | 2021-03-20 00:36:55 +0000 | [diff] [blame] | 30 | // 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] | 31 | // easier to customize the test behavior. |
| 32 | // |
| 33 | // 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] | 34 | // 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] | 35 | // appropriate fixture preparers. Keeping the conversion change separate makes it easy to verify |
| 36 | // that it did not change the test behavior unexpectedly. |
| 37 | // |
| 38 | // deprecated |
| 39 | func testShBinary(t *testing.T, bp string) (*android.TestContext, android.Config) { |
Colin Cross | 06c80eb | 2022-02-10 10:34:19 -0800 | [diff] [blame] | 40 | bp = bp + cc.GatherRequiredDepsForTest(android.Android) |
| 41 | |
Paul Duffin | 89648f9 | 2021-03-20 00:36:55 +0000 | [diff] [blame] | 42 | result := prepareForShTest.RunTestWithBp(t, bp) |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 43 | |
Paul Duffin | 56fb8ee | 2021-03-08 15:05:52 +0000 | [diff] [blame] | 44 | return result.TestContext, result.Config |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Jaewoong Jung | 4aedc86 | 2020-06-10 17:23:46 -0700 | [diff] [blame] | 47 | func TestShTestSubDir(t *testing.T) { |
Colin Cross | c68db4b | 2021-11-11 18:59:15 -0800 | [diff] [blame] | 48 | result := android.GroupFixturePreparers( |
| 49 | prepareForShTest, |
| 50 | android.FixtureModifyConfig(android.SetKatiEnabledForTests), |
| 51 | ).RunTestWithBp(t, ` |
Jaewoong Jung | 4aedc86 | 2020-06-10 17:23:46 -0700 | [diff] [blame] | 52 | sh_test { |
| 53 | name: "foo", |
| 54 | src: "test.sh", |
| 55 | sub_dir: "foo_test" |
| 56 | } |
| 57 | `) |
| 58 | |
Colin Cross | c68db4b | 2021-11-11 18:59:15 -0800 | [diff] [blame] | 59 | mod := result.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*ShTest) |
Jaewoong Jung | 4aedc86 | 2020-06-10 17:23:46 -0700 | [diff] [blame] | 60 | |
Colin Cross | c68db4b | 2021-11-11 18:59:15 -0800 | [diff] [blame] | 61 | entries := android.AndroidMkEntriesForTest(t, result.TestContext, mod)[0] |
Jaewoong Jung | 4aedc86 | 2020-06-10 17:23:46 -0700 | [diff] [blame] | 62 | |
Paul Duffin | 32b06c2 | 2021-03-16 07:50:37 +0000 | [diff] [blame] | 63 | expectedPath := "out/target/product/test_device/data/nativetest64/foo_test" |
Jaewoong Jung | 4aedc86 | 2020-06-10 17:23:46 -0700 | [diff] [blame] | 64 | actualPath := entries.EntryMap["LOCAL_MODULE_PATH"][0] |
Colin Cross | c68db4b | 2021-11-11 18:59:15 -0800 | [diff] [blame] | 65 | android.AssertStringPathRelativeToTopEquals(t, "LOCAL_MODULE_PATH[0]", result.Config, expectedPath, actualPath) |
Jaewoong Jung | 4aedc86 | 2020-06-10 17:23:46 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | func TestShTest(t *testing.T) { |
Colin Cross | c68db4b | 2021-11-11 18:59:15 -0800 | [diff] [blame] | 69 | result := android.GroupFixturePreparers( |
| 70 | prepareForShTest, |
| 71 | android.FixtureModifyConfig(android.SetKatiEnabledForTests), |
| 72 | ).RunTestWithBp(t, ` |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 73 | sh_test { |
| 74 | name: "foo", |
| 75 | src: "test.sh", |
| 76 | filename: "test.sh", |
| 77 | data: [ |
| 78 | "testdata/data1", |
| 79 | "testdata/sub/data2", |
| 80 | ], |
| 81 | } |
| 82 | `) |
| 83 | |
Colin Cross | c68db4b | 2021-11-11 18:59:15 -0800 | [diff] [blame] | 84 | mod := result.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*ShTest) |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 85 | |
Colin Cross | c68db4b | 2021-11-11 18:59:15 -0800 | [diff] [blame] | 86 | entries := android.AndroidMkEntriesForTest(t, result.TestContext, mod)[0] |
Jaewoong Jung | 4aedc86 | 2020-06-10 17:23:46 -0700 | [diff] [blame] | 87 | |
Paul Duffin | 32b06c2 | 2021-03-16 07:50:37 +0000 | [diff] [blame] | 88 | expectedPath := "out/target/product/test_device/data/nativetest64/foo" |
Jaewoong Jung | 4aedc86 | 2020-06-10 17:23:46 -0700 | [diff] [blame] | 89 | actualPath := entries.EntryMap["LOCAL_MODULE_PATH"][0] |
Colin Cross | c68db4b | 2021-11-11 18:59:15 -0800 | [diff] [blame] | 90 | android.AssertStringPathRelativeToTopEquals(t, "LOCAL_MODULE_PATH[0]", result.Config, expectedPath, actualPath) |
Jaewoong Jung | 4aedc86 | 2020-06-10 17:23:46 -0700 | [diff] [blame] | 91 | |
| 92 | expectedData := []string{":testdata/data1", ":testdata/sub/data2"} |
| 93 | actualData := entries.EntryMap["LOCAL_TEST_DATA"] |
Paul Duffin | 32b06c2 | 2021-03-16 07:50:37 +0000 | [diff] [blame] | 94 | android.AssertDeepEquals(t, "LOCAL_TEST_DATA", expectedData, actualData) |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 95 | } |
Jaewoong Jung | 61a8368 | 2019-07-01 09:08:50 -0700 | [diff] [blame] | 96 | |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 97 | func TestShTest_dataModules(t *testing.T) { |
Paul Duffin | 32b06c2 | 2021-03-16 07:50:37 +0000 | [diff] [blame] | 98 | ctx, config := testShBinary(t, ` |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 99 | sh_test { |
| 100 | name: "foo", |
| 101 | src: "test.sh", |
| 102 | host_supported: true, |
| 103 | data_bins: ["bar"], |
| 104 | data_libs: ["libbar"], |
| 105 | } |
| 106 | |
| 107 | cc_binary { |
| 108 | name: "bar", |
| 109 | host_supported: true, |
| 110 | shared_libs: ["libbar"], |
| 111 | no_libcrt: true, |
| 112 | nocrt: true, |
| 113 | system_shared_libs: [], |
| 114 | stl: "none", |
| 115 | } |
| 116 | |
| 117 | cc_library { |
| 118 | name: "libbar", |
| 119 | host_supported: true, |
| 120 | no_libcrt: true, |
| 121 | nocrt: true, |
| 122 | system_shared_libs: [], |
| 123 | stl: "none", |
| 124 | } |
| 125 | `) |
| 126 | |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 127 | buildOS := config.BuildOS.String() |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 128 | arches := []string{"android_arm64_armv8-a", buildOS + "_x86_64"} |
| 129 | for _, arch := range arches { |
| 130 | variant := ctx.ModuleForTests("foo", arch) |
| 131 | |
| 132 | libExt := ".so" |
| 133 | if arch == "darwin_x86_64" { |
| 134 | libExt = ".dylib" |
| 135 | } |
| 136 | relocated := variant.Output("relocated/lib64/libbar" + libExt) |
Paul Duffin | 32b06c2 | 2021-03-16 07:50:37 +0000 | [diff] [blame] | 137 | expectedInput := "out/soong/.intermediates/libbar/" + arch + "_shared/libbar" + libExt |
| 138 | android.AssertPathRelativeToTopEquals(t, "relocation input", expectedInput, relocated.Input) |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 139 | |
| 140 | mod := variant.Module().(*ShTest) |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 141 | entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0] |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 142 | expectedData := []string{ |
Paul Duffin | 32b06c2 | 2021-03-16 07:50:37 +0000 | [diff] [blame] | 143 | filepath.Join("out/soong/.intermediates/bar", arch, ":bar"), |
| 144 | filepath.Join("out/soong/.intermediates/foo", arch, "relocated/:lib64/libbar"+libExt), |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 145 | } |
| 146 | actualData := entries.EntryMap["LOCAL_TEST_DATA"] |
Paul Duffin | 32b06c2 | 2021-03-16 07:50:37 +0000 | [diff] [blame] | 147 | android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_TEST_DATA", config, expectedData, actualData) |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
Jaewoong Jung | 61a8368 | 2019-07-01 09:08:50 -0700 | [diff] [blame] | 151 | func TestShTestHost(t *testing.T) { |
| 152 | ctx, _ := testShBinary(t, ` |
| 153 | sh_test_host { |
| 154 | name: "foo", |
| 155 | src: "test.sh", |
| 156 | filename: "test.sh", |
| 157 | data: [ |
| 158 | "testdata/data1", |
| 159 | "testdata/sub/data2", |
| 160 | ], |
Dan Shi | b40deac | 2021-05-24 12:04:54 -0700 | [diff] [blame] | 161 | test_options: { |
| 162 | unit_test: true, |
| 163 | }, |
Jaewoong Jung | 61a8368 | 2019-07-01 09:08:50 -0700 | [diff] [blame] | 164 | } |
| 165 | `) |
| 166 | |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 167 | buildOS := ctx.Config().BuildOS.String() |
Jaewoong Jung | 61a8368 | 2019-07-01 09:08:50 -0700 | [diff] [blame] | 168 | mod := ctx.ModuleForTests("foo", buildOS+"_x86_64").Module().(*ShTest) |
| 169 | if !mod.Host() { |
| 170 | t.Errorf("host bit is not set for a sh_test_host module.") |
| 171 | } |
Dan Shi | b40deac | 2021-05-24 12:04:54 -0700 | [diff] [blame] | 172 | entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0] |
| 173 | actualData, _ := strconv.ParseBool(entries.EntryMap["LOCAL_IS_UNIT_TEST"][0]) |
| 174 | android.AssertBoolEquals(t, "LOCAL_IS_UNIT_TEST", true, actualData) |
Jaewoong Jung | 61a8368 | 2019-07-01 09:08:50 -0700 | [diff] [blame] | 175 | } |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 176 | |
| 177 | func TestShTestHost_dataDeviceModules(t *testing.T) { |
Paul Duffin | 32b06c2 | 2021-03-16 07:50:37 +0000 | [diff] [blame] | 178 | ctx, config := testShBinary(t, ` |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 179 | sh_test_host { |
| 180 | name: "foo", |
| 181 | src: "test.sh", |
| 182 | data_device_bins: ["bar"], |
| 183 | data_device_libs: ["libbar"], |
| 184 | } |
| 185 | |
| 186 | cc_binary { |
| 187 | name: "bar", |
| 188 | shared_libs: ["libbar"], |
| 189 | no_libcrt: true, |
| 190 | nocrt: true, |
| 191 | system_shared_libs: [], |
| 192 | stl: "none", |
| 193 | } |
| 194 | |
| 195 | cc_library { |
| 196 | name: "libbar", |
| 197 | no_libcrt: true, |
| 198 | nocrt: true, |
| 199 | system_shared_libs: [], |
| 200 | stl: "none", |
| 201 | } |
| 202 | `) |
| 203 | |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 204 | buildOS := config.BuildOS.String() |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 205 | variant := ctx.ModuleForTests("foo", buildOS+"_x86_64") |
| 206 | |
| 207 | relocated := variant.Output("relocated/lib64/libbar.so") |
Paul Duffin | 32b06c2 | 2021-03-16 07:50:37 +0000 | [diff] [blame] | 208 | expectedInput := "out/soong/.intermediates/libbar/android_arm64_armv8-a_shared/libbar.so" |
| 209 | android.AssertPathRelativeToTopEquals(t, "relocation input", expectedInput, relocated.Input) |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 210 | |
| 211 | mod := variant.Module().(*ShTest) |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 212 | entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0] |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 213 | expectedData := []string{ |
Paul Duffin | 32b06c2 | 2021-03-16 07:50:37 +0000 | [diff] [blame] | 214 | "out/soong/.intermediates/bar/android_arm64_armv8-a/:bar", |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 215 | // 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] | 216 | "out/soong/.intermediates/foo/" + buildOS + "_x86_64/relocated/:lib64/libbar.so", |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 217 | } |
| 218 | actualData := entries.EntryMap["LOCAL_TEST_DATA"] |
Paul Duffin | 32b06c2 | 2021-03-16 07:50:37 +0000 | [diff] [blame] | 219 | android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_TEST_DATA", config, expectedData, actualData) |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 220 | } |
Sam Delmerico | b3342ce | 2022-01-20 21:10:28 +0000 | [diff] [blame] | 221 | |
| 222 | func TestShTestHost_dataDeviceModulesAutogenTradefedConfig(t *testing.T) { |
| 223 | ctx, config := testShBinary(t, ` |
| 224 | sh_test_host { |
| 225 | name: "foo", |
| 226 | src: "test.sh", |
| 227 | data_device_bins: ["bar"], |
| 228 | data_device_libs: ["libbar"], |
| 229 | } |
| 230 | |
| 231 | cc_binary { |
| 232 | name: "bar", |
| 233 | shared_libs: ["libbar"], |
| 234 | no_libcrt: true, |
| 235 | nocrt: true, |
| 236 | system_shared_libs: [], |
| 237 | stl: "none", |
| 238 | } |
| 239 | |
| 240 | cc_library { |
| 241 | name: "libbar", |
| 242 | no_libcrt: true, |
| 243 | nocrt: true, |
| 244 | system_shared_libs: [], |
| 245 | stl: "none", |
| 246 | } |
| 247 | `) |
| 248 | |
| 249 | buildOS := config.BuildOS.String() |
| 250 | fooModule := ctx.ModuleForTests("foo", buildOS+"_x86_64") |
| 251 | |
| 252 | expectedBinAutogenConfig := `<option name="push-file" key="bar" value="/data/local/tests/unrestricted/foo/bar" />` |
| 253 | autogen := fooModule.Rule("autogen") |
| 254 | if !strings.Contains(autogen.Args["extraConfigs"], expectedBinAutogenConfig) { |
| 255 | t.Errorf("foo extraConfings %v does not contain %q", autogen.Args["extraConfigs"], expectedBinAutogenConfig) |
| 256 | } |
| 257 | } |