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