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