Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 1 | // Copyright 2017 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package java |
| 16 | |
| 17 | import ( |
Paul Duffin | 1a39332 | 2020-11-18 16:36:47 +0000 | [diff] [blame] | 18 | "fmt" |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 19 | "os" |
| 20 | "path/filepath" |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 21 | "reflect" |
Colin Cross | f8d9c49 | 2021-01-26 11:01:43 -0800 | [diff] [blame] | 22 | "runtime" |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 23 | "strconv" |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 24 | "strings" |
| 25 | "testing" |
Colin Cross | 86a60ae | 2018-05-29 14:44:55 -0700 | [diff] [blame] | 26 | |
Jeongik Cha | 28df257 | 2019-11-11 10:46:36 +0900 | [diff] [blame] | 27 | "github.com/google/blueprint/proptools" |
| 28 | |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 29 | "android/soong/android" |
| 30 | "android/soong/cc" |
Colin Cross | c28bb0b | 2019-02-25 14:20:47 -0800 | [diff] [blame] | 31 | "android/soong/dexpreopt" |
Colin Cross | 1661aff | 2021-03-12 17:56:51 -0800 | [diff] [blame] | 32 | "android/soong/genrule" |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 33 | "android/soong/python" |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 34 | ) |
| 35 | |
Paul Duffin | 70d3bee | 2021-03-21 11:26:05 +0000 | [diff] [blame] | 36 | // Legacy preparer used for running tests within the java package. |
| 37 | // |
| 38 | // This includes everything that was needed to run any test in the java package prior to the |
| 39 | // introduction of the test fixtures. Tests that are being converted to use fixtures directly |
| 40 | // rather than through the testJava...() methods should avoid using this and instead use the |
| 41 | // various preparers directly, using android.GroupFixturePreparers(...) to group them when |
| 42 | // necessary. |
| 43 | // |
| 44 | // deprecated |
| 45 | var prepareForJavaTest = android.GroupFixturePreparers( |
Paul Duffin | 95bdab4 | 2021-03-08 21:48:46 +0000 | [diff] [blame] | 46 | genrule.PrepareForTestWithGenRuleBuildComponents, |
| 47 | // Get the CC build components but not default modules. |
| 48 | cc.PrepareForTestWithCcBuildComponents, |
| 49 | // Include all the default java modules. |
| 50 | PrepareForTestWithJavaDefaultModules, |
Paul Duffin | 42da69d | 2021-03-22 13:41:36 +0000 | [diff] [blame] | 51 | PrepareForTestWithOverlayBuildComponents, |
Paul Duffin | 220ddd7 | 2021-03-17 23:54:30 +0000 | [diff] [blame] | 52 | python.PrepareForTestWithPythonBuildComponents, |
Paul Duffin | 95bdab4 | 2021-03-08 21:48:46 +0000 | [diff] [blame] | 53 | android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) { |
Paul Duffin | 95bdab4 | 2021-03-08 21:48:46 +0000 | [diff] [blame] | 54 | ctx.RegisterPreSingletonType("sdk_versions", sdkPreSingletonFactory) |
| 55 | }), |
Paul Duffin | 9fc9f53 | 2021-03-23 15:41:11 +0000 | [diff] [blame] | 56 | PrepareForTestWithDexpreopt, |
Paul Duffin | 95bdab4 | 2021-03-08 21:48:46 +0000 | [diff] [blame] | 57 | ) |
| 58 | |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 59 | func TestMain(m *testing.M) { |
Paul Duffin | d6339af | 2021-03-22 18:05:04 +0000 | [diff] [blame] | 60 | os.Exit(m.Run()) |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Paul Duffin | 95bdab4 | 2021-03-08 21:48:46 +0000 | [diff] [blame] | 63 | // testJavaError is a legacy way of running tests of java modules that expect errors. |
| 64 | // |
| 65 | // See testJava for an explanation as to how to stop using this deprecated method. |
| 66 | // |
| 67 | // deprecated |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 68 | func testJavaError(t *testing.T, pattern string, bp string) (*android.TestContext, android.Config) { |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 69 | t.Helper() |
Paul Duffin | 71ae594 | 2021-03-22 15:36:52 +0000 | [diff] [blame] | 70 | result := android.GroupFixturePreparers( |
Paul Duffin | 9fc9f53 | 2021-03-23 15:41:11 +0000 | [diff] [blame] | 71 | prepareForJavaTest, dexpreopt.PrepareForTestByEnablingDexpreopt). |
Paul Duffin | 6bac49c | 2021-03-12 21:28:15 +0000 | [diff] [blame] | 72 | ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)). |
| 73 | RunTestWithBp(t, bp) |
| 74 | return result.TestContext, result.Config |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 75 | } |
| 76 | |
Paul Duffin | 71ae594 | 2021-03-22 15:36:52 +0000 | [diff] [blame] | 77 | // testJavaWithFS runs tests using the prepareForJavaTest |
Paul Duffin | 95bdab4 | 2021-03-08 21:48:46 +0000 | [diff] [blame] | 78 | // |
| 79 | // See testJava for an explanation as to how to stop using this deprecated method. |
| 80 | // |
| 81 | // deprecated |
| 82 | func testJavaWithFS(t *testing.T, bp string, fs android.MockFS) (*android.TestContext, android.Config) { |
Colin Cross | 238c1f3 | 2020-06-07 16:58:18 -0700 | [diff] [blame] | 83 | t.Helper() |
Paul Duffin | 71ae594 | 2021-03-22 15:36:52 +0000 | [diff] [blame] | 84 | result := android.GroupFixturePreparers( |
| 85 | prepareForJavaTest, fs.AddToFixture()).RunTestWithBp(t, bp) |
Paul Duffin | 95bdab4 | 2021-03-08 21:48:46 +0000 | [diff] [blame] | 86 | return result.TestContext, result.Config |
Colin Cross | 238c1f3 | 2020-06-07 16:58:18 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Paul Duffin | 71ae594 | 2021-03-22 15:36:52 +0000 | [diff] [blame] | 89 | // testJava runs tests using the prepareForJavaTest |
Paul Duffin | 95bdab4 | 2021-03-08 21:48:46 +0000 | [diff] [blame] | 90 | // |
Paul Duffin | 71ae594 | 2021-03-22 15:36:52 +0000 | [diff] [blame] | 91 | // Do not add any new usages of this, instead use the prepareForJavaTest directly as it makes it |
Paul Duffin | 95bdab4 | 2021-03-08 21:48:46 +0000 | [diff] [blame] | 92 | // much easier to customize the test behavior. |
| 93 | // |
| 94 | // If it is necessary to customize the behavior of an existing test that uses this then please first |
Paul Duffin | 71ae594 | 2021-03-22 15:36:52 +0000 | [diff] [blame] | 95 | // convert the test to using prepareForJavaTest first and then in a following change add the |
Paul Duffin | 95bdab4 | 2021-03-08 21:48:46 +0000 | [diff] [blame] | 96 | // appropriate fixture preparers. Keeping the conversion change separate makes it easy to verify |
| 97 | // that it did not change the test behavior unexpectedly. |
| 98 | // |
| 99 | // deprecated |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 100 | func testJava(t *testing.T, bp string) (*android.TestContext, android.Config) { |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 101 | t.Helper() |
Paul Duffin | 71ae594 | 2021-03-22 15:36:52 +0000 | [diff] [blame] | 102 | result := prepareForJavaTest.RunTestWithBp(t, bp) |
Paul Duffin | 95bdab4 | 2021-03-08 21:48:46 +0000 | [diff] [blame] | 103 | return result.TestContext, result.Config |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 104 | } |
| 105 | |
Paul Duffin | 95bdab4 | 2021-03-08 21:48:46 +0000 | [diff] [blame] | 106 | // defaultModuleToPath constructs a path to the turbine generate jar for a default test module that |
| 107 | // is defined in PrepareForIntegrationTestWithJava |
| 108 | func defaultModuleToPath(name string) string { |
Paul Duffin | 76101fa | 2021-03-13 09:55:25 +0000 | [diff] [blame] | 109 | switch { |
| 110 | case name == `""`: |
| 111 | return name |
| 112 | case strings.HasSuffix(name, ".jar"): |
| 113 | return name |
| 114 | default: |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 115 | return filepath.Join("out", "soong", ".intermediates", defaultJavaDir, name, "android_common", "turbine-combined", name+".jar") |
Paul Duffin | 76101fa | 2021-03-13 09:55:25 +0000 | [diff] [blame] | 116 | } |
Paul Duffin | 95bdab4 | 2021-03-08 21:48:46 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Paul Duffin | 76e5c8a | 2021-03-20 14:19:46 +0000 | [diff] [blame] | 119 | // Test that the PrepareForTestWithJavaDefaultModules provides all the files that it uses by |
| 120 | // running it in a fixture that requires all source files to exist. |
| 121 | func TestPrepareForTestWithJavaDefaultModules(t *testing.T) { |
| 122 | android.GroupFixturePreparers( |
| 123 | PrepareForTestWithJavaDefaultModules, |
| 124 | android.PrepareForTestDisallowNonExistentPaths, |
| 125 | ).RunTest(t) |
| 126 | } |
| 127 | |
Jeongik Cha | e403e9e | 2019-12-07 00:16:24 +0900 | [diff] [blame] | 128 | func TestJavaLinkType(t *testing.T) { |
| 129 | testJava(t, ` |
| 130 | java_library { |
| 131 | name: "foo", |
| 132 | srcs: ["a.java"], |
| 133 | libs: ["bar"], |
| 134 | static_libs: ["baz"], |
| 135 | } |
| 136 | |
| 137 | java_library { |
| 138 | name: "bar", |
| 139 | sdk_version: "current", |
| 140 | srcs: ["b.java"], |
| 141 | } |
| 142 | |
| 143 | java_library { |
| 144 | name: "baz", |
| 145 | sdk_version: "system_current", |
| 146 | srcs: ["c.java"], |
| 147 | } |
| 148 | `) |
| 149 | |
Steven Moreland | 0029898 | 2020-11-17 21:44:36 +0000 | [diff] [blame] | 150 | testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", ` |
Jeongik Cha | e403e9e | 2019-12-07 00:16:24 +0900 | [diff] [blame] | 151 | java_library { |
| 152 | name: "foo", |
| 153 | srcs: ["a.java"], |
| 154 | libs: ["bar"], |
| 155 | sdk_version: "current", |
| 156 | static_libs: ["baz"], |
| 157 | } |
| 158 | |
| 159 | java_library { |
| 160 | name: "bar", |
| 161 | sdk_version: "current", |
| 162 | srcs: ["b.java"], |
| 163 | } |
| 164 | |
| 165 | java_library { |
| 166 | name: "baz", |
| 167 | sdk_version: "system_current", |
| 168 | srcs: ["c.java"], |
| 169 | } |
| 170 | `) |
| 171 | |
| 172 | testJava(t, ` |
| 173 | java_library { |
| 174 | name: "foo", |
| 175 | srcs: ["a.java"], |
| 176 | libs: ["bar"], |
| 177 | sdk_version: "system_current", |
| 178 | static_libs: ["baz"], |
| 179 | } |
| 180 | |
| 181 | java_library { |
| 182 | name: "bar", |
| 183 | sdk_version: "current", |
| 184 | srcs: ["b.java"], |
| 185 | } |
| 186 | |
| 187 | java_library { |
| 188 | name: "baz", |
| 189 | sdk_version: "system_current", |
| 190 | srcs: ["c.java"], |
| 191 | } |
| 192 | `) |
| 193 | |
Steven Moreland | 0029898 | 2020-11-17 21:44:36 +0000 | [diff] [blame] | 194 | testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", ` |
Jeongik Cha | e403e9e | 2019-12-07 00:16:24 +0900 | [diff] [blame] | 195 | java_library { |
| 196 | name: "foo", |
| 197 | srcs: ["a.java"], |
| 198 | libs: ["bar"], |
| 199 | sdk_version: "system_current", |
| 200 | static_libs: ["baz"], |
| 201 | } |
| 202 | |
| 203 | java_library { |
| 204 | name: "bar", |
| 205 | sdk_version: "current", |
| 206 | srcs: ["b.java"], |
| 207 | } |
| 208 | |
| 209 | java_library { |
| 210 | name: "baz", |
| 211 | srcs: ["c.java"], |
| 212 | } |
| 213 | `) |
| 214 | } |
| 215 | |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 216 | func TestSimple(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 217 | ctx, _ := testJava(t, ` |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 218 | java_library { |
| 219 | name: "foo", |
| 220 | srcs: ["a.java"], |
Colin Cross | e8dc34a | 2017-07-19 11:22:16 -0700 | [diff] [blame] | 221 | libs: ["bar"], |
| 222 | static_libs: ["baz"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | java_library { |
| 226 | name: "bar", |
| 227 | srcs: ["b.java"], |
| 228 | } |
| 229 | |
| 230 | java_library { |
| 231 | name: "baz", |
| 232 | srcs: ["c.java"], |
| 233 | } |
Colin Cross | d5934c8 | 2017-10-02 13:55:26 -0700 | [diff] [blame] | 234 | `) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 235 | |
Paul Duffin | a71a67a | 2021-03-29 00:42:57 +0100 | [diff] [blame] | 236 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 237 | combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac") |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 238 | |
| 239 | if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" { |
| 240 | t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs) |
| 241 | } |
| 242 | |
Colin Cross | 1ee2317 | 2017-10-18 14:44:18 -0700 | [diff] [blame] | 243 | baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String() |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 244 | barTurbine := filepath.Join("out", "soong", ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar") |
| 245 | bazTurbine := filepath.Join("out", "soong", ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar") |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 246 | |
Paul Duffin | 22b77cd | 2021-03-12 19:15:01 +0000 | [diff] [blame] | 247 | android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], barTurbine) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 248 | |
Paul Duffin | 22b77cd | 2021-03-12 19:15:01 +0000 | [diff] [blame] | 249 | android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], bazTurbine) |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 250 | |
| 251 | if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz { |
| 252 | t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | |
Artur Satayev | 9cf4669 | 2019-11-26 18:08:34 +0000 | [diff] [blame] | 256 | func TestExportedPlugins(t *testing.T) { |
| 257 | type Result struct { |
Colin Cross | c9fe10f | 2020-11-19 18:06:03 -0800 | [diff] [blame] | 258 | library string |
| 259 | processors string |
| 260 | disableTurbine bool |
Artur Satayev | 9cf4669 | 2019-11-26 18:08:34 +0000 | [diff] [blame] | 261 | } |
| 262 | var tests = []struct { |
| 263 | name string |
| 264 | extra string |
| 265 | results []Result |
| 266 | }{ |
| 267 | { |
| 268 | name: "Exported plugin is not a direct plugin", |
| 269 | extra: `java_library { name: "exports", srcs: ["a.java"], exported_plugins: ["plugin"] }`, |
| 270 | results: []Result{{library: "exports", processors: "-proc:none"}}, |
| 271 | }, |
| 272 | { |
| 273 | name: "Exports plugin to dependee", |
| 274 | extra: ` |
| 275 | java_library{name: "exports", exported_plugins: ["plugin"]} |
| 276 | java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]} |
| 277 | java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]} |
| 278 | `, |
| 279 | results: []Result{ |
| 280 | {library: "foo", processors: "-processor com.android.TestPlugin"}, |
| 281 | {library: "bar", processors: "-processor com.android.TestPlugin"}, |
| 282 | }, |
| 283 | }, |
| 284 | { |
| 285 | name: "Exports plugin to android_library", |
| 286 | extra: ` |
| 287 | java_library{name: "exports", exported_plugins: ["plugin"]} |
| 288 | android_library{name: "foo", srcs: ["a.java"], libs: ["exports"]} |
| 289 | android_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]} |
| 290 | `, |
| 291 | results: []Result{ |
| 292 | {library: "foo", processors: "-processor com.android.TestPlugin"}, |
| 293 | {library: "bar", processors: "-processor com.android.TestPlugin"}, |
| 294 | }, |
| 295 | }, |
| 296 | { |
| 297 | name: "Exports plugin is not propagated via transitive deps", |
| 298 | extra: ` |
| 299 | java_library{name: "exports", exported_plugins: ["plugin"]} |
| 300 | java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]} |
| 301 | java_library{name: "bar", srcs: ["a.java"], static_libs: ["foo"]} |
| 302 | `, |
| 303 | results: []Result{ |
| 304 | {library: "foo", processors: "-processor com.android.TestPlugin"}, |
| 305 | {library: "bar", processors: "-proc:none"}, |
| 306 | }, |
| 307 | }, |
| 308 | { |
| 309 | name: "Exports plugin appends to plugins", |
| 310 | extra: ` |
| 311 | java_plugin{name: "plugin2", processor_class: "com.android.TestPlugin2"} |
| 312 | java_library{name: "exports", exported_plugins: ["plugin"]} |
| 313 | java_library{name: "foo", srcs: ["a.java"], libs: ["exports"], plugins: ["plugin2"]} |
| 314 | `, |
| 315 | results: []Result{ |
| 316 | {library: "foo", processors: "-processor com.android.TestPlugin,com.android.TestPlugin2"}, |
| 317 | }, |
| 318 | }, |
Colin Cross | c9fe10f | 2020-11-19 18:06:03 -0800 | [diff] [blame] | 319 | { |
| 320 | name: "Exports plugin to with generates_api to dependee", |
| 321 | extra: ` |
| 322 | java_library{name: "exports", exported_plugins: ["plugin_generates_api"]} |
| 323 | java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]} |
| 324 | java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]} |
| 325 | `, |
| 326 | results: []Result{ |
| 327 | {library: "foo", processors: "-processor com.android.TestPlugin", disableTurbine: true}, |
| 328 | {library: "bar", processors: "-processor com.android.TestPlugin", disableTurbine: true}, |
| 329 | }, |
| 330 | }, |
Artur Satayev | 9cf4669 | 2019-11-26 18:08:34 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | for _, test := range tests { |
| 334 | t.Run(test.name, func(t *testing.T) { |
| 335 | ctx, _ := testJava(t, ` |
| 336 | java_plugin { |
| 337 | name: "plugin", |
| 338 | processor_class: "com.android.TestPlugin", |
| 339 | } |
Colin Cross | c9fe10f | 2020-11-19 18:06:03 -0800 | [diff] [blame] | 340 | java_plugin { |
| 341 | name: "plugin_generates_api", |
| 342 | generates_api: true, |
| 343 | processor_class: "com.android.TestPlugin", |
| 344 | } |
Artur Satayev | 9cf4669 | 2019-11-26 18:08:34 +0000 | [diff] [blame] | 345 | `+test.extra) |
| 346 | |
| 347 | for _, want := range test.results { |
| 348 | javac := ctx.ModuleForTests(want.library, "android_common").Rule("javac") |
| 349 | if javac.Args["processor"] != want.processors { |
| 350 | t.Errorf("For library %v, expected %v, found %v", want.library, want.processors, javac.Args["processor"]) |
| 351 | } |
Colin Cross | c9fe10f | 2020-11-19 18:06:03 -0800 | [diff] [blame] | 352 | turbine := ctx.ModuleForTests(want.library, "android_common").MaybeRule("turbine") |
| 353 | disableTurbine := turbine.BuildParams.Rule == nil |
| 354 | if disableTurbine != want.disableTurbine { |
| 355 | t.Errorf("For library %v, expected disableTurbine %v, found %v", want.library, want.disableTurbine, disableTurbine) |
| 356 | } |
Artur Satayev | 9cf4669 | 2019-11-26 18:08:34 +0000 | [diff] [blame] | 357 | } |
| 358 | }) |
| 359 | } |
| 360 | } |
| 361 | |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 362 | func TestSdkVersionByPartition(t *testing.T) { |
| 363 | testJavaError(t, "sdk_version must have a value when the module is located at vendor or product", ` |
Jeongik Cha | 6bd33c1 | 2019-06-25 16:26:18 +0900 | [diff] [blame] | 364 | java_library { |
| 365 | name: "foo", |
| 366 | srcs: ["a.java"], |
| 367 | vendor: true, |
| 368 | } |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 369 | `) |
Jeongik Cha | 6bd33c1 | 2019-06-25 16:26:18 +0900 | [diff] [blame] | 370 | |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 371 | testJava(t, ` |
Jeongik Cha | 6bd33c1 | 2019-06-25 16:26:18 +0900 | [diff] [blame] | 372 | java_library { |
| 373 | name: "bar", |
| 374 | srcs: ["b.java"], |
| 375 | } |
| 376 | `) |
| 377 | |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 378 | for _, enforce := range []bool{true, false} { |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 379 | bp := ` |
| 380 | java_library { |
| 381 | name: "foo", |
| 382 | srcs: ["a.java"], |
| 383 | product_specific: true, |
| 384 | } |
| 385 | ` |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 386 | |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 387 | errorHandler := android.FixtureExpectsNoErrors |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 388 | if enforce { |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 389 | errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern("sdk_version must have a value when the module is located at vendor or product") |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 390 | } |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 391 | |
| 392 | android.GroupFixturePreparers( |
| 393 | PrepareForTestWithJavaDefaultModules, |
| 394 | android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { |
| 395 | variables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce) |
| 396 | }), |
| 397 | ). |
| 398 | ExtendWithErrorHandler(errorHandler). |
| 399 | RunTestWithBp(t, bp) |
Jeongik Cha | 6bd33c1 | 2019-06-25 16:26:18 +0900 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | |
Colin Cross | d5934c8 | 2017-10-02 13:55:26 -0700 | [diff] [blame] | 403 | func TestArchSpecific(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 404 | ctx, _ := testJava(t, ` |
Colin Cross | d5934c8 | 2017-10-02 13:55:26 -0700 | [diff] [blame] | 405 | java_library { |
| 406 | name: "foo", |
| 407 | srcs: ["a.java"], |
| 408 | target: { |
| 409 | android: { |
| 410 | srcs: ["b.java"], |
| 411 | }, |
| 412 | }, |
| 413 | } |
| 414 | `) |
| 415 | |
| 416 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
| 417 | if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" { |
| 418 | t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs) |
| 419 | } |
| 420 | } |
| 421 | |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 422 | func TestBinary(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 423 | ctx, _ := testJava(t, ` |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 424 | java_library_host { |
| 425 | name: "foo", |
| 426 | srcs: ["a.java"], |
| 427 | } |
| 428 | |
| 429 | java_binary_host { |
| 430 | name: "bar", |
| 431 | srcs: ["b.java"], |
| 432 | static_libs: ["foo"], |
Colin Cross | 89226d9 | 2020-10-09 19:00:54 -0700 | [diff] [blame] | 433 | jni_libs: ["libjni"], |
| 434 | } |
| 435 | |
| 436 | cc_library_shared { |
| 437 | name: "libjni", |
| 438 | host_supported: true, |
| 439 | device_supported: false, |
| 440 | stl: "none", |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 441 | } |
| 442 | `) |
| 443 | |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 444 | buildOS := ctx.Config().BuildOS.String() |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 445 | |
| 446 | bar := ctx.ModuleForTests("bar", buildOS+"_common") |
| 447 | barJar := bar.Output("bar.jar").Output.String() |
| 448 | barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64") |
| 449 | barWrapperDeps := barWrapper.Output("bar").Implicits.Strings() |
| 450 | |
Colin Cross | 89226d9 | 2020-10-09 19:00:54 -0700 | [diff] [blame] | 451 | libjni := ctx.ModuleForTests("libjni", buildOS+"_x86_64_shared") |
| 452 | libjniSO := libjni.Rule("Cp").Output.String() |
| 453 | |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 454 | // Test that the install binary wrapper depends on the installed jar file |
Colin Cross | c179ea6 | 2020-10-09 10:54:15 -0700 | [diff] [blame] | 455 | if g, w := barWrapperDeps, barJar; !android.InList(w, g) { |
| 456 | t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g) |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 457 | } |
Colin Cross | 89226d9 | 2020-10-09 19:00:54 -0700 | [diff] [blame] | 458 | |
| 459 | // Test that the install binary wrapper depends on the installed JNI libraries |
| 460 | if g, w := barWrapperDeps, libjniSO; !android.InList(w, g) { |
| 461 | t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g) |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 462 | } |
Alex Humesky | 2070e32 | 2020-06-09 20:23:08 -0400 | [diff] [blame] | 463 | } |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 464 | |
Colin Cross | f8d9c49 | 2021-01-26 11:01:43 -0800 | [diff] [blame] | 465 | func TestTest(t *testing.T) { |
| 466 | ctx, _ := testJava(t, ` |
| 467 | java_test_host { |
| 468 | name: "foo", |
| 469 | srcs: ["a.java"], |
| 470 | jni_libs: ["libjni"], |
| 471 | } |
| 472 | |
| 473 | cc_library_shared { |
| 474 | name: "libjni", |
| 475 | host_supported: true, |
| 476 | device_supported: false, |
| 477 | stl: "none", |
| 478 | } |
| 479 | `) |
| 480 | |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 481 | buildOS := ctx.Config().BuildOS.String() |
Colin Cross | f8d9c49 | 2021-01-26 11:01:43 -0800 | [diff] [blame] | 482 | |
| 483 | foo := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost) |
| 484 | |
| 485 | expected := "lib64/libjni.so" |
| 486 | if runtime.GOOS == "darwin" { |
| 487 | expected = "lib64/libjni.dylib" |
| 488 | } |
| 489 | |
| 490 | fooTestData := foo.data |
| 491 | if len(fooTestData) != 1 || fooTestData[0].Rel() != expected { |
| 492 | t.Errorf(`expected foo test data relative path [%q], got %q`, |
| 493 | expected, fooTestData.Strings()) |
| 494 | } |
| 495 | } |
| 496 | |
Alex Humesky | 2070e32 | 2020-06-09 20:23:08 -0400 | [diff] [blame] | 497 | func TestHostBinaryNoJavaDebugInfoOverride(t *testing.T) { |
| 498 | bp := ` |
| 499 | java_library { |
| 500 | name: "target_library", |
| 501 | srcs: ["a.java"], |
| 502 | } |
| 503 | |
| 504 | java_binary_host { |
| 505 | name: "host_binary", |
| 506 | srcs: ["b.java"], |
| 507 | } |
| 508 | ` |
Alex Humesky | 2070e32 | 2020-06-09 20:23:08 -0400 | [diff] [blame] | 509 | |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 510 | result := android.GroupFixturePreparers( |
| 511 | PrepareForTestWithJavaDefaultModules, |
| 512 | android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { |
| 513 | variables.MinimizeJavaDebugInfo = proptools.BoolPtr(true) |
| 514 | }), |
| 515 | ).RunTestWithBp(t, bp) |
Alex Humesky | 2070e32 | 2020-06-09 20:23:08 -0400 | [diff] [blame] | 516 | |
Liz Kammer | 7941b30 | 2020-07-28 13:27:34 -0700 | [diff] [blame] | 517 | // first, check that the -g flag is added to target modules |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 518 | targetLibrary := result.ModuleForTests("target_library", "android_common") |
Alex Humesky | 2070e32 | 2020-06-09 20:23:08 -0400 | [diff] [blame] | 519 | targetJavaFlags := targetLibrary.Module().VariablesForTests()["javacFlags"] |
| 520 | if !strings.Contains(targetJavaFlags, "-g:source,lines") { |
| 521 | t.Errorf("target library javac flags %v should contain "+ |
| 522 | "-g:source,lines override with MinimizeJavaDebugInfo", targetJavaFlags) |
| 523 | } |
| 524 | |
| 525 | // check that -g is not overridden for host modules |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 526 | buildOS := result.Config.BuildOS.String() |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 527 | hostBinary := result.ModuleForTests("host_binary", buildOS+"_common") |
Alex Humesky | 2070e32 | 2020-06-09 20:23:08 -0400 | [diff] [blame] | 528 | hostJavaFlags := hostBinary.Module().VariablesForTests()["javacFlags"] |
| 529 | if strings.Contains(hostJavaFlags, "-g:source,lines") { |
| 530 | t.Errorf("java_binary_host javac flags %v should not have "+ |
| 531 | "-g:source,lines override with MinimizeJavaDebugInfo", hostJavaFlags) |
| 532 | } |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 533 | } |
| 534 | |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 535 | func TestPrebuilts(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 536 | ctx, _ := testJava(t, ` |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 537 | java_library { |
| 538 | name: "foo", |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 539 | srcs: ["a.java", ":stubs-source"], |
Paul Duffin | fcfd791 | 2020-01-31 17:54:30 +0000 | [diff] [blame] | 540 | libs: ["bar", "sdklib"], |
Colin Cross | e8dc34a | 2017-07-19 11:22:16 -0700 | [diff] [blame] | 541 | static_libs: ["baz"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 542 | } |
| 543 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 544 | java_import { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 545 | name: "bar", |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 546 | jars: ["a.jar"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 547 | } |
| 548 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 549 | java_import { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 550 | name: "baz", |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 551 | jars: ["b.jar"], |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 552 | sdk_version: "current", |
| 553 | compile_dex: true, |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 554 | } |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 555 | |
| 556 | dex_import { |
| 557 | name: "qux", |
| 558 | jars: ["b.jar"], |
| 559 | } |
Colin Cross | 79c7c26 | 2019-04-17 11:11:46 -0700 | [diff] [blame] | 560 | |
| 561 | java_sdk_library_import { |
Paul Duffin | 56d4490 | 2020-01-31 13:36:25 +0000 | [diff] [blame] | 562 | name: "sdklib", |
| 563 | public: { |
| 564 | jars: ["c.jar"], |
| 565 | }, |
| 566 | } |
| 567 | |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 568 | prebuilt_stubs_sources { |
| 569 | name: "stubs-source", |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 570 | srcs: ["stubs/sources"], |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 571 | } |
Paul Duffin | 1b82e6a | 2019-12-03 18:06:47 +0000 | [diff] [blame] | 572 | |
| 573 | java_test_import { |
| 574 | name: "test", |
| 575 | jars: ["a.jar"], |
| 576 | test_suites: ["cts"], |
| 577 | test_config: "AndroidTest.xml", |
| 578 | } |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 579 | `) |
| 580 | |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 581 | fooModule := ctx.ModuleForTests("foo", "android_common") |
| 582 | javac := fooModule.Rule("javac") |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 583 | combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac") |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 584 | barModule := ctx.ModuleForTests("bar", "android_common") |
| 585 | barJar := barModule.Rule("combineJar").Output |
| 586 | bazModule := ctx.ModuleForTests("baz", "android_common") |
| 587 | bazJar := bazModule.Rule("combineJar").Output |
Colin Cross | 79c7c26 | 2019-04-17 11:11:46 -0700 | [diff] [blame] | 588 | sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 589 | |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 590 | fooLibrary := fooModule.Module().(*Library) |
| 591 | assertDeepEquals(t, "foo java sources incorrect", |
| 592 | []string{"a.java"}, fooLibrary.compiledJavaSrcs.Strings()) |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 593 | |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 594 | assertDeepEquals(t, "foo java source jars incorrect", |
| 595 | []string{".intermediates/stubs-source/android_common/stubs-source-stubs.srcjar"}, |
| 596 | android.NormalizePathsForTesting(fooLibrary.compiledSrcJars)) |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 597 | |
Colin Cross | 37f6d79 | 2018-07-12 12:28:41 -0700 | [diff] [blame] | 598 | if !strings.Contains(javac.Args["classpath"], barJar.String()) { |
| 599 | t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String()) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 600 | } |
| 601 | |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 602 | barDexJar := barModule.Module().(*Import).DexJarBuildPath() |
Martin Stjernholm | 8be1e6d | 2021-09-15 03:34:04 +0100 | [diff] [blame] | 603 | if barDexJar.IsSet() { |
| 604 | t.Errorf("bar dex jar build path expected to be set, got %s", barDexJar) |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 605 | } |
| 606 | |
Colin Cross | 79c7c26 | 2019-04-17 11:11:46 -0700 | [diff] [blame] | 607 | if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) { |
| 608 | t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String()) |
| 609 | } |
| 610 | |
Colin Cross | 37f6d79 | 2018-07-12 12:28:41 -0700 | [diff] [blame] | 611 | if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() { |
| 612 | t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String()) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 613 | } |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 614 | |
Martin Stjernholm | 8be1e6d | 2021-09-15 03:34:04 +0100 | [diff] [blame] | 615 | bazDexJar := bazModule.Module().(*Import).DexJarBuildPath().Path() |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 616 | expectedDexJar := "out/soong/.intermediates/baz/android_common/dex/baz.jar" |
| 617 | android.AssertPathRelativeToTopEquals(t, "baz dex jar build path", expectedDexJar, bazDexJar) |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 618 | |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 619 | ctx.ModuleForTests("qux", "android_common").Rule("Cp") |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 620 | } |
| 621 | |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 622 | func assertDeepEquals(t *testing.T, message string, expected interface{}, actual interface{}) { |
| 623 | if !reflect.DeepEqual(expected, actual) { |
| 624 | t.Errorf("%s: expected %q, found %q", message, expected, actual) |
| 625 | } |
| 626 | } |
| 627 | |
Paul Duffin | 1a39332 | 2020-11-18 16:36:47 +0000 | [diff] [blame] | 628 | func TestPrebuiltStubsSources(t *testing.T) { |
| 629 | test := func(t *testing.T, sourcesPath string, expectedInputs []string) { |
| 630 | ctx, _ := testJavaWithFS(t, fmt.Sprintf(` |
| 631 | prebuilt_stubs_sources { |
| 632 | name: "stubs-source", |
| 633 | srcs: ["%s"], |
| 634 | }`, sourcesPath), map[string][]byte{ |
| 635 | "stubs/sources/pkg/A.java": nil, |
| 636 | "stubs/sources/pkg/B.java": nil, |
| 637 | }) |
| 638 | |
| 639 | zipSrc := ctx.ModuleForTests("stubs-source", "android_common").Rule("zip_src") |
| 640 | if expected, actual := expectedInputs, zipSrc.Inputs.Strings(); !reflect.DeepEqual(expected, actual) { |
| 641 | t.Errorf("mismatch of inputs to soong_zip: expected %q, actual %q", expected, actual) |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | t.Run("empty/missing directory", func(t *testing.T) { |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame] | 646 | test(t, "empty-directory", nil) |
Paul Duffin | 1a39332 | 2020-11-18 16:36:47 +0000 | [diff] [blame] | 647 | }) |
| 648 | |
| 649 | t.Run("non-empty set of sources", func(t *testing.T) { |
| 650 | test(t, "stubs/sources", []string{ |
| 651 | "stubs/sources/pkg/A.java", |
| 652 | "stubs/sources/pkg/B.java", |
| 653 | }) |
| 654 | }) |
| 655 | } |
| 656 | |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 657 | func TestDefaults(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 658 | ctx, _ := testJava(t, ` |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 659 | java_defaults { |
| 660 | name: "defaults", |
| 661 | srcs: ["a.java"], |
| 662 | libs: ["bar"], |
| 663 | static_libs: ["baz"], |
Sasha Smundak | 2057f82 | 2019-04-16 17:16:58 -0700 | [diff] [blame] | 664 | optimize: {enabled: false}, |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | java_library { |
| 668 | name: "foo", |
| 669 | defaults: ["defaults"], |
| 670 | } |
| 671 | |
| 672 | java_library { |
| 673 | name: "bar", |
| 674 | srcs: ["b.java"], |
| 675 | } |
| 676 | |
| 677 | java_library { |
| 678 | name: "baz", |
| 679 | srcs: ["c.java"], |
| 680 | } |
Sasha Smundak | 2057f82 | 2019-04-16 17:16:58 -0700 | [diff] [blame] | 681 | |
| 682 | android_test { |
| 683 | name: "atestOptimize", |
| 684 | defaults: ["defaults"], |
| 685 | optimize: {enabled: true}, |
| 686 | } |
| 687 | |
| 688 | android_test { |
| 689 | name: "atestNoOptimize", |
| 690 | defaults: ["defaults"], |
| 691 | } |
| 692 | |
| 693 | android_test { |
| 694 | name: "atestDefault", |
| 695 | srcs: ["a.java"], |
| 696 | } |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 697 | `) |
| 698 | |
Paul Duffin | a71a67a | 2021-03-29 00:42:57 +0100 | [diff] [blame] | 699 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 700 | combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac") |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 701 | |
| 702 | if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" { |
| 703 | t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs) |
| 704 | } |
| 705 | |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 706 | barTurbine := filepath.Join("out", "soong", ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar") |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 707 | if !strings.Contains(javac.Args["classpath"], barTurbine) { |
| 708 | t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine) |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 709 | } |
| 710 | |
Colin Cross | 1ee2317 | 2017-10-18 14:44:18 -0700 | [diff] [blame] | 711 | baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String() |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 712 | if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz { |
| 713 | t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz) |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 714 | } |
Sasha Smundak | 2057f82 | 2019-04-16 17:16:58 -0700 | [diff] [blame] | 715 | |
| 716 | atestOptimize := ctx.ModuleForTests("atestOptimize", "android_common").MaybeRule("r8") |
| 717 | if atestOptimize.Output == nil { |
| 718 | t.Errorf("atestOptimize should optimize APK") |
| 719 | } |
| 720 | |
| 721 | atestNoOptimize := ctx.ModuleForTests("atestNoOptimize", "android_common").MaybeRule("d8") |
| 722 | if atestNoOptimize.Output == nil { |
| 723 | t.Errorf("atestNoOptimize should not optimize APK") |
| 724 | } |
| 725 | |
| 726 | atestDefault := ctx.ModuleForTests("atestDefault", "android_common").MaybeRule("r8") |
| 727 | if atestDefault.Output == nil { |
| 728 | t.Errorf("atestDefault should optimize APK") |
| 729 | } |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 730 | } |
| 731 | |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 732 | func TestResources(t *testing.T) { |
| 733 | var table = []struct { |
| 734 | name string |
| 735 | prop string |
| 736 | extra string |
| 737 | args string |
| 738 | }{ |
| 739 | { |
Colin Cross | af9c55b | 2017-10-03 14:50:08 -0700 | [diff] [blame] | 740 | // Test that a module with java_resource_dirs includes the files |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 741 | name: "resource dirs", |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame] | 742 | prop: `java_resource_dirs: ["java-res"]`, |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 743 | args: "-C java-res -f java-res/a/a -f java-res/b/b", |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 744 | }, |
| 745 | { |
| 746 | // Test that a module with java_resources includes the files |
| 747 | name: "resource files", |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 748 | prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`, |
| 749 | args: "-C . -f java-res/a/a -f java-res/b/b", |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 750 | }, |
| 751 | { |
| 752 | // Test that a module with a filegroup in java_resources includes the files with the |
| 753 | // path prefix |
| 754 | name: "resource filegroup", |
| 755 | prop: `java_resources: [":foo-res"]`, |
| 756 | extra: ` |
| 757 | filegroup { |
| 758 | name: "foo-res", |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame] | 759 | path: "java-res", |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 760 | srcs: ["java-res/a/a", "java-res/b/b"], |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 761 | }`, |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 762 | args: "-C java-res -f java-res/a/a -f java-res/b/b", |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 763 | }, |
| 764 | { |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 765 | // Test that a module with wildcards in java_resource_dirs has the correct path prefixes |
| 766 | name: "wildcard dirs", |
| 767 | prop: `java_resource_dirs: ["java-res/*"]`, |
| 768 | args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b", |
| 769 | }, |
| 770 | { |
| 771 | // Test that a module exclude_java_resource_dirs excludes the files |
| 772 | name: "wildcard dirs", |
| 773 | prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`, |
| 774 | args: "-C java-res/a -f java-res/a/a", |
| 775 | }, |
Colin Cross | cedd476 | 2018-09-13 11:26:19 -0700 | [diff] [blame] | 776 | { |
| 777 | // Test wildcards in java_resources |
| 778 | name: "wildcard files", |
| 779 | prop: `java_resources: ["java-res/**/*"]`, |
| 780 | args: "-C . -f java-res/a/a -f java-res/b/b", |
| 781 | }, |
| 782 | { |
| 783 | // Test exclude_java_resources with java_resources |
| 784 | name: "wildcard files with exclude", |
| 785 | prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`, |
| 786 | args: "-C . -f java-res/a/a", |
| 787 | }, |
| 788 | { |
| 789 | // Test exclude_java_resources with java_resource_dirs |
| 790 | name: "resource dirs with exclude files", |
| 791 | prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`, |
| 792 | args: "-C java-res -f java-res/a/a", |
| 793 | }, |
| 794 | { |
| 795 | // Test exclude_java_resource_dirs with java_resource_dirs |
| 796 | name: "resource dirs with exclude files", |
| 797 | prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`, |
| 798 | args: "-C java-res -f java-res/a/a -f java-res/b/b", |
| 799 | }, |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | for _, test := range table { |
| 803 | t.Run(test.name, func(t *testing.T) { |
Colin Cross | 238c1f3 | 2020-06-07 16:58:18 -0700 | [diff] [blame] | 804 | ctx, _ := testJavaWithFS(t, ` |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 805 | java_library { |
| 806 | name: "foo", |
| 807 | srcs: [ |
| 808 | "a.java", |
| 809 | "b.java", |
| 810 | "c.java", |
| 811 | ], |
| 812 | `+test.prop+`, |
| 813 | } |
Colin Cross | 238c1f3 | 2020-06-07 16:58:18 -0700 | [diff] [blame] | 814 | `+test.extra, |
| 815 | map[string][]byte{ |
| 816 | "java-res/a/a": nil, |
| 817 | "java-res/b/b": nil, |
| 818 | "java-res2/a": nil, |
| 819 | }, |
| 820 | ) |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 821 | |
Colin Cross | 331a121 | 2018-08-15 20:40:52 -0700 | [diff] [blame] | 822 | foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar") |
Colin Cross | 1ee2317 | 2017-10-18 14:44:18 -0700 | [diff] [blame] | 823 | fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar") |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 824 | |
| 825 | if !inList(fooRes.Output.String(), foo.Inputs.Strings()) { |
| 826 | t.Errorf("foo combined jars %v does not contain %q", |
| 827 | foo.Inputs.Strings(), fooRes.Output.String()) |
| 828 | } |
| 829 | |
Colin Cross | af9c55b | 2017-10-03 14:50:08 -0700 | [diff] [blame] | 830 | if fooRes.Args["jarArgs"] != test.args { |
| 831 | t.Errorf("foo resource jar args %q is not %q", |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 832 | fooRes.Args["jarArgs"], test.args) |
| 833 | } |
| 834 | }) |
| 835 | } |
| 836 | } |
| 837 | |
Colin Cross | 0c4ce21 | 2019-05-03 15:28:19 -0700 | [diff] [blame] | 838 | func TestIncludeSrcs(t *testing.T) { |
Colin Cross | 238c1f3 | 2020-06-07 16:58:18 -0700 | [diff] [blame] | 839 | ctx, _ := testJavaWithFS(t, ` |
Colin Cross | 0c4ce21 | 2019-05-03 15:28:19 -0700 | [diff] [blame] | 840 | java_library { |
| 841 | name: "foo", |
| 842 | srcs: [ |
| 843 | "a.java", |
| 844 | "b.java", |
| 845 | "c.java", |
| 846 | ], |
| 847 | include_srcs: true, |
| 848 | } |
| 849 | |
| 850 | java_library { |
| 851 | name: "bar", |
| 852 | srcs: [ |
| 853 | "a.java", |
| 854 | "b.java", |
| 855 | "c.java", |
| 856 | ], |
| 857 | java_resource_dirs: ["java-res"], |
| 858 | include_srcs: true, |
| 859 | } |
Colin Cross | 238c1f3 | 2020-06-07 16:58:18 -0700 | [diff] [blame] | 860 | `, map[string][]byte{ |
| 861 | "java-res/a/a": nil, |
| 862 | "java-res/b/b": nil, |
| 863 | "java-res2/a": nil, |
| 864 | }) |
Colin Cross | 0c4ce21 | 2019-05-03 15:28:19 -0700 | [diff] [blame] | 865 | |
| 866 | // Test a library with include_srcs: true |
| 867 | foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar") |
| 868 | fooSrcJar := ctx.ModuleForTests("foo", "android_common").Output("foo.srcjar") |
| 869 | |
| 870 | if g, w := fooSrcJar.Output.String(), foo.Inputs.Strings(); !inList(g, w) { |
| 871 | t.Errorf("foo combined jars %v does not contain %q", w, g) |
| 872 | } |
| 873 | |
| 874 | if g, w := fooSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w { |
| 875 | t.Errorf("foo source jar args %q is not %q", w, g) |
| 876 | } |
| 877 | |
| 878 | // Test a library with include_srcs: true and resources |
| 879 | bar := ctx.ModuleForTests("bar", "android_common").Output("withres/bar.jar") |
| 880 | barResCombined := ctx.ModuleForTests("bar", "android_common").Output("res-combined/bar.jar") |
| 881 | barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar") |
| 882 | barSrcJar := ctx.ModuleForTests("bar", "android_common").Output("bar.srcjar") |
| 883 | |
| 884 | if g, w := barSrcJar.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) { |
| 885 | t.Errorf("bar combined resource jars %v does not contain %q", w, g) |
| 886 | } |
| 887 | |
| 888 | if g, w := barRes.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) { |
| 889 | t.Errorf("bar combined resource jars %v does not contain %q", w, g) |
| 890 | } |
| 891 | |
| 892 | if g, w := barResCombined.Output.String(), bar.Inputs.Strings(); !inList(g, w) { |
| 893 | t.Errorf("bar combined jars %v does not contain %q", w, g) |
| 894 | } |
| 895 | |
| 896 | if g, w := barSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w { |
| 897 | t.Errorf("bar source jar args %q is not %q", w, g) |
| 898 | } |
| 899 | |
| 900 | if g, w := barRes.Args["jarArgs"], "-C java-res -f java-res/a/a -f java-res/b/b"; g != w { |
| 901 | t.Errorf("bar resource jar args %q is not %q", w, g) |
| 902 | } |
| 903 | } |
| 904 | |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 905 | func TestGeneratedSources(t *testing.T) { |
Colin Cross | 238c1f3 | 2020-06-07 16:58:18 -0700 | [diff] [blame] | 906 | ctx, _ := testJavaWithFS(t, ` |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 907 | java_library { |
| 908 | name: "foo", |
| 909 | srcs: [ |
| 910 | "a*.java", |
| 911 | ":gen", |
| 912 | "b*.java", |
| 913 | ], |
| 914 | } |
| 915 | |
| 916 | genrule { |
| 917 | name: "gen", |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame] | 918 | tool_files: ["java-res/a"], |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 919 | out: ["gen.java"], |
| 920 | } |
Colin Cross | 238c1f3 | 2020-06-07 16:58:18 -0700 | [diff] [blame] | 921 | `, map[string][]byte{ |
| 922 | "a.java": nil, |
| 923 | "b.java": nil, |
| 924 | }) |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 925 | |
| 926 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
| 927 | genrule := ctx.ModuleForTests("gen", "").Rule("generator") |
| 928 | |
Colin Cross | 15e86d9 | 2017-10-20 15:07:08 -0700 | [diff] [blame] | 929 | if filepath.Base(genrule.Output.String()) != "gen.java" { |
| 930 | t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String()) |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 931 | } |
| 932 | |
| 933 | if len(javac.Inputs) != 3 || |
| 934 | javac.Inputs[0].String() != "a.java" || |
Colin Cross | 15e86d9 | 2017-10-20 15:07:08 -0700 | [diff] [blame] | 935 | javac.Inputs[1].String() != genrule.Output.String() || |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 936 | javac.Inputs[2].String() != "b.java" { |
| 937 | t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs) |
| 938 | } |
| 939 | } |
| 940 | |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 941 | func TestTurbine(t *testing.T) { |
Paul Duffin | 71ae594 | 2021-03-22 15:36:52 +0000 | [diff] [blame] | 942 | result := android.GroupFixturePreparers( |
| 943 | prepareForJavaTest, FixtureWithPrebuiltApis(map[string][]string{"14": {"foo"}})). |
Paul Duffin | 3d9f268 | 2021-03-13 09:47:16 +0000 | [diff] [blame] | 944 | RunTestWithBp(t, ` |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 945 | java_library { |
| 946 | name: "foo", |
| 947 | srcs: ["a.java"], |
Jiyong Park | 2d49294 | 2018-03-05 17:44:10 +0900 | [diff] [blame] | 948 | sdk_version: "14", |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | java_library { |
| 952 | name: "bar", |
Colin Cross | 9bc4343 | 2017-12-15 20:20:39 -0800 | [diff] [blame] | 953 | srcs: ["b.java"], |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 954 | static_libs: ["foo"], |
Jiyong Park | 2d49294 | 2018-03-05 17:44:10 +0900 | [diff] [blame] | 955 | sdk_version: "14", |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | java_library { |
| 959 | name: "baz", |
| 960 | srcs: ["c.java"], |
| 961 | libs: ["bar"], |
| 962 | sdk_version: "14", |
| 963 | } |
| 964 | `) |
| 965 | |
Paul Duffin | a71a67a | 2021-03-29 00:42:57 +0100 | [diff] [blame] | 966 | fooTurbine := result.ModuleForTests("foo", "android_common").Rule("turbine") |
| 967 | barTurbine := result.ModuleForTests("bar", "android_common").Rule("turbine") |
| 968 | barJavac := result.ModuleForTests("bar", "android_common").Rule("javac") |
| 969 | barTurbineCombined := result.ModuleForTests("bar", "android_common").Description("for turbine") |
| 970 | bazJavac := result.ModuleForTests("baz", "android_common").Rule("javac") |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 971 | |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 972 | android.AssertPathsRelativeToTopEquals(t, "foo inputs", []string{"a.java"}, fooTurbine.Inputs) |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 973 | |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 974 | fooHeaderJar := filepath.Join("out", "soong", ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar") |
| 975 | barTurbineJar := filepath.Join("out", "soong", ".intermediates", "bar", "android_common", "turbine", "bar.jar") |
Colin Cross | f61766e | 2022-03-16 18:06:48 -0700 | [diff] [blame] | 976 | android.AssertStringDoesContain(t, "bar turbine classpath", barTurbine.Args["turbineFlags"], fooHeaderJar) |
Paul Duffin | 3d9f268 | 2021-03-13 09:47:16 +0000 | [diff] [blame] | 977 | android.AssertStringDoesContain(t, "bar javac classpath", barJavac.Args["classpath"], fooHeaderJar) |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 978 | android.AssertPathsRelativeToTopEquals(t, "bar turbine combineJar", []string{barTurbineJar, fooHeaderJar}, barTurbineCombined.Inputs) |
Paul Duffin | 3d9f268 | 2021-03-13 09:47:16 +0000 | [diff] [blame] | 979 | android.AssertStringDoesContain(t, "baz javac classpath", bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | func TestSharding(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 983 | ctx, _ := testJava(t, ` |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 984 | java_library { |
| 985 | name: "bar", |
| 986 | srcs: ["a.java","b.java","c.java"], |
| 987 | javac_shard_size: 1 |
| 988 | } |
| 989 | `) |
| 990 | |
Colin Cross | 3d56ed5 | 2021-11-18 22:23:12 -0800 | [diff] [blame] | 991 | barHeaderJar := filepath.Join("out", "soong", ".intermediates", "bar", "android_common", "turbine", "bar.jar") |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 992 | for i := 0; i < 3; i++ { |
Paul Duffin | a71a67a | 2021-03-29 00:42:57 +0100 | [diff] [blame] | 993 | barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i)) |
Colin Cross | 3d56ed5 | 2021-11-18 22:23:12 -0800 | [diff] [blame] | 994 | if !strings.HasPrefix(barJavac.Args["classpath"], "-classpath "+barHeaderJar+":") { |
| 995 | t.Errorf("bar javac classpath %v does start with %q", barJavac.Args["classpath"], barHeaderJar) |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 996 | } |
| 997 | } |
| 998 | } |
| 999 | |
Nan Zhang | 27e284d | 2018-02-09 21:03:53 +0000 | [diff] [blame] | 1000 | func TestExcludeFileGroupInSrcs(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 1001 | ctx, _ := testJava(t, ` |
Nan Zhang | 27e284d | 2018-02-09 21:03:53 +0000 | [diff] [blame] | 1002 | java_library { |
| 1003 | name: "foo", |
| 1004 | srcs: ["a.java", ":foo-srcs"], |
| 1005 | exclude_srcs: ["a.java", ":foo-excludes"], |
| 1006 | } |
| 1007 | |
| 1008 | filegroup { |
| 1009 | name: "foo-srcs", |
| 1010 | srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"], |
| 1011 | } |
| 1012 | |
| 1013 | filegroup { |
| 1014 | name: "foo-excludes", |
| 1015 | srcs: ["java-fg/a.java", "java-fg/b.java"], |
| 1016 | } |
| 1017 | `) |
| 1018 | |
| 1019 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
| 1020 | |
| 1021 | if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" { |
| 1022 | t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs) |
| 1023 | } |
| 1024 | } |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1025 | |
Paul Duffin | 52d398a | 2019-06-11 12:31:14 +0100 | [diff] [blame] | 1026 | func TestJavaLibrary(t *testing.T) { |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1027 | testJavaWithFS(t, "", map[string][]byte{ |
Paul Duffin | 52d398a | 2019-06-11 12:31:14 +0100 | [diff] [blame] | 1028 | "libcore/Android.bp": []byte(` |
| 1029 | java_library { |
| 1030 | name: "core", |
| 1031 | sdk_version: "none", |
| 1032 | system_modules: "none", |
Paul Duffin | aa55f74 | 2020-10-06 17:20:13 +0100 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | filegroup { |
| 1036 | name: "core-jar", |
| 1037 | srcs: [":core{.jar}"], |
| 1038 | } |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1039 | `), |
Paul Duffin | aa55f74 | 2020-10-06 17:20:13 +0100 | [diff] [blame] | 1040 | }) |
Paul Duffin | aa55f74 | 2020-10-06 17:20:13 +0100 | [diff] [blame] | 1041 | } |
| 1042 | |
| 1043 | func TestJavaImport(t *testing.T) { |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1044 | testJavaWithFS(t, "", map[string][]byte{ |
Paul Duffin | aa55f74 | 2020-10-06 17:20:13 +0100 | [diff] [blame] | 1045 | "libcore/Android.bp": []byte(` |
| 1046 | java_import { |
| 1047 | name: "core", |
| 1048 | sdk_version: "none", |
| 1049 | } |
| 1050 | |
| 1051 | filegroup { |
| 1052 | name: "core-jar", |
| 1053 | srcs: [":core{.jar}"], |
| 1054 | } |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1055 | `), |
Paul Duffin | 52d398a | 2019-06-11 12:31:14 +0100 | [diff] [blame] | 1056 | }) |
Paul Duffin | 52d398a | 2019-06-11 12:31:14 +0100 | [diff] [blame] | 1057 | } |
| 1058 | |
Zoran Jovanovic | 8736ce2 | 2018-08-21 17:10:29 +0200 | [diff] [blame] | 1059 | var compilerFlagsTestCases = []struct { |
| 1060 | in string |
| 1061 | out bool |
| 1062 | }{ |
| 1063 | { |
| 1064 | in: "a", |
| 1065 | out: false, |
| 1066 | }, |
| 1067 | { |
| 1068 | in: "-a", |
| 1069 | out: true, |
| 1070 | }, |
| 1071 | { |
| 1072 | in: "-no-jdk", |
| 1073 | out: false, |
| 1074 | }, |
| 1075 | { |
| 1076 | in: "-no-stdlib", |
| 1077 | out: false, |
| 1078 | }, |
| 1079 | { |
| 1080 | in: "-kotlin-home", |
| 1081 | out: false, |
| 1082 | }, |
| 1083 | { |
| 1084 | in: "-kotlin-home /some/path", |
| 1085 | out: false, |
| 1086 | }, |
| 1087 | { |
| 1088 | in: "-include-runtime", |
| 1089 | out: false, |
| 1090 | }, |
| 1091 | { |
| 1092 | in: "-Xintellij-plugin-root", |
| 1093 | out: false, |
| 1094 | }, |
| 1095 | } |
| 1096 | |
| 1097 | type mockContext struct { |
| 1098 | android.ModuleContext |
| 1099 | result bool |
| 1100 | } |
| 1101 | |
| 1102 | func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) { |
| 1103 | // CheckBadCompilerFlags calls this function when the flag should be rejected |
| 1104 | ctx.result = false |
| 1105 | } |
| 1106 | |
| 1107 | func TestCompilerFlags(t *testing.T) { |
| 1108 | for _, testCase := range compilerFlagsTestCases { |
| 1109 | ctx := &mockContext{result: true} |
| 1110 | CheckKotlincFlags(ctx, []string{testCase.in}) |
| 1111 | if ctx.result != testCase.out { |
| 1112 | t.Errorf("incorrect output:") |
| 1113 | t.Errorf(" input: %#v", testCase.in) |
| 1114 | t.Errorf(" expected: %#v", testCase.out) |
| 1115 | t.Errorf(" got: %#v", ctx.result) |
| 1116 | } |
| 1117 | } |
| 1118 | } |
Jaewoong Jung | 38e4fb2 | 2018-12-12 09:01:34 -0800 | [diff] [blame] | 1119 | |
| 1120 | // TODO(jungjw): Consider making this more robust by ignoring path order. |
| 1121 | func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) { |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1122 | variables := ctx.ModuleForTests(moduleName, "android_common").VariablesForTestsRelativeToTop() |
Jaewoong Jung | 38e4fb2 | 2018-12-12 09:01:34 -0800 | [diff] [blame] | 1123 | flags := strings.Split(variables["javacFlags"], " ") |
| 1124 | got := "" |
| 1125 | for _, flag := range flags { |
| 1126 | keyEnd := strings.Index(flag, "=") |
| 1127 | if keyEnd > -1 && flag[:keyEnd] == "--patch-module" { |
| 1128 | got = flag[keyEnd+1:] |
| 1129 | break |
| 1130 | } |
| 1131 | } |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 1132 | if expected != android.StringPathRelativeToTop(ctx.Config().SoongOutDir(), got) { |
Jaewoong Jung | 38e4fb2 | 2018-12-12 09:01:34 -0800 | [diff] [blame] | 1133 | t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got) |
| 1134 | } |
| 1135 | } |
| 1136 | |
| 1137 | func TestPatchModule(t *testing.T) { |
Pete Gillin | 0c2143e | 2019-05-02 15:32:11 +0100 | [diff] [blame] | 1138 | t.Run("Java language level 8", func(t *testing.T) { |
Pete Gillin | 1b3370f | 2019-10-01 13:57:31 +0100 | [diff] [blame] | 1139 | // Test with legacy javac -source 1.8 -target 1.8 |
Pete Gillin | bdf5d71 | 2019-10-21 14:29:58 +0100 | [diff] [blame] | 1140 | bp := ` |
| 1141 | java_library { |
| 1142 | name: "foo", |
| 1143 | srcs: ["a.java"], |
| 1144 | java_version: "1.8", |
| 1145 | } |
| 1146 | |
| 1147 | java_library { |
| 1148 | name: "bar", |
| 1149 | srcs: ["b.java"], |
| 1150 | sdk_version: "none", |
| 1151 | system_modules: "none", |
| 1152 | patch_module: "java.base", |
| 1153 | java_version: "1.8", |
| 1154 | } |
| 1155 | |
| 1156 | java_library { |
| 1157 | name: "baz", |
| 1158 | srcs: ["c.java"], |
| 1159 | patch_module: "java.base", |
| 1160 | java_version: "1.8", |
| 1161 | } |
| 1162 | ` |
| 1163 | ctx, _ := testJava(t, bp) |
Jaewoong Jung | 38e4fb2 | 2018-12-12 09:01:34 -0800 | [diff] [blame] | 1164 | |
| 1165 | checkPatchModuleFlag(t, ctx, "foo", "") |
| 1166 | checkPatchModuleFlag(t, ctx, "bar", "") |
| 1167 | checkPatchModuleFlag(t, ctx, "baz", "") |
| 1168 | }) |
| 1169 | |
Pete Gillin | 0c2143e | 2019-05-02 15:32:11 +0100 | [diff] [blame] | 1170 | t.Run("Java language level 9", func(t *testing.T) { |
Pete Gillin | 1b3370f | 2019-10-01 13:57:31 +0100 | [diff] [blame] | 1171 | // Test with default javac -source 9 -target 9 |
Pete Gillin | bdf5d71 | 2019-10-21 14:29:58 +0100 | [diff] [blame] | 1172 | bp := ` |
| 1173 | java_library { |
| 1174 | name: "foo", |
| 1175 | srcs: ["a.java"], |
| 1176 | } |
| 1177 | |
| 1178 | java_library { |
| 1179 | name: "bar", |
| 1180 | srcs: ["b.java"], |
| 1181 | sdk_version: "none", |
| 1182 | system_modules: "none", |
| 1183 | patch_module: "java.base", |
| 1184 | } |
| 1185 | |
| 1186 | java_library { |
| 1187 | name: "baz", |
Jingwen Chen | 5136a6e | 2020-10-30 01:01:35 -0400 | [diff] [blame] | 1188 | srcs: [ |
| 1189 | "c.java", |
| 1190 | // Tests for b/150878007 |
| 1191 | "dir/d.java", |
| 1192 | "dir2/e.java", |
| 1193 | "dir2/f.java", |
| 1194 | "nested/dir/g.java" |
| 1195 | ], |
Pete Gillin | bdf5d71 | 2019-10-21 14:29:58 +0100 | [diff] [blame] | 1196 | patch_module: "java.base", |
| 1197 | } |
| 1198 | ` |
Pete Gillin | 1b3370f | 2019-10-01 13:57:31 +0100 | [diff] [blame] | 1199 | ctx, _ := testJava(t, bp) |
Jaewoong Jung | 38e4fb2 | 2018-12-12 09:01:34 -0800 | [diff] [blame] | 1200 | |
| 1201 | checkPatchModuleFlag(t, ctx, "foo", "") |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1202 | expected := "java.base=.:out/soong" |
Jaewoong Jung | 38e4fb2 | 2018-12-12 09:01:34 -0800 | [diff] [blame] | 1203 | checkPatchModuleFlag(t, ctx, "bar", expected) |
Jingwen Chen | 5136a6e | 2020-10-30 01:01:35 -0400 | [diff] [blame] | 1204 | expected = "java.base=" + strings.Join([]string{ |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1205 | ".", "out/soong", "dir", "dir2", "nested", defaultModuleToPath("ext"), defaultModuleToPath("framework")}, ":") |
Jaewoong Jung | 38e4fb2 | 2018-12-12 09:01:34 -0800 | [diff] [blame] | 1206 | checkPatchModuleFlag(t, ctx, "baz", expected) |
| 1207 | }) |
| 1208 | } |
Paul Duffin | a7b9f42 | 2020-01-10 17:12:18 +0000 | [diff] [blame] | 1209 | |
Paul Duffin | 83a2d96 | 2019-11-19 19:44:10 +0000 | [diff] [blame] | 1210 | func TestJavaLibraryWithSystemModules(t *testing.T) { |
| 1211 | ctx, _ := testJava(t, ` |
| 1212 | java_library { |
| 1213 | name: "lib-with-source-system-modules", |
| 1214 | srcs: [ |
| 1215 | "a.java", |
| 1216 | ], |
| 1217 | sdk_version: "none", |
| 1218 | system_modules: "source-system-modules", |
| 1219 | } |
| 1220 | |
| 1221 | java_library { |
| 1222 | name: "source-jar", |
| 1223 | srcs: [ |
| 1224 | "a.java", |
| 1225 | ], |
| 1226 | } |
| 1227 | |
| 1228 | java_system_modules { |
| 1229 | name: "source-system-modules", |
| 1230 | libs: ["source-jar"], |
| 1231 | } |
| 1232 | |
| 1233 | java_library { |
| 1234 | name: "lib-with-prebuilt-system-modules", |
| 1235 | srcs: [ |
| 1236 | "a.java", |
| 1237 | ], |
| 1238 | sdk_version: "none", |
| 1239 | system_modules: "prebuilt-system-modules", |
| 1240 | } |
| 1241 | |
| 1242 | java_import { |
| 1243 | name: "prebuilt-jar", |
| 1244 | jars: ["a.jar"], |
| 1245 | } |
| 1246 | |
| 1247 | java_system_modules_import { |
| 1248 | name: "prebuilt-system-modules", |
| 1249 | libs: ["prebuilt-jar"], |
| 1250 | } |
| 1251 | `) |
| 1252 | |
| 1253 | checkBootClasspathForSystemModule(t, ctx, "lib-with-source-system-modules", "/source-jar.jar") |
| 1254 | |
| 1255 | checkBootClasspathForSystemModule(t, ctx, "lib-with-prebuilt-system-modules", "/prebuilt-jar.jar") |
| 1256 | } |
| 1257 | |
| 1258 | func checkBootClasspathForSystemModule(t *testing.T, ctx *android.TestContext, moduleName string, expectedSuffix string) { |
| 1259 | javacRule := ctx.ModuleForTests(moduleName, "android_common").Rule("javac") |
| 1260 | bootClasspath := javacRule.Args["bootClasspath"] |
| 1261 | if strings.HasPrefix(bootClasspath, "--system ") && strings.HasSuffix(bootClasspath, expectedSuffix) { |
| 1262 | t.Errorf("bootclasspath of %q must start with --system and end with %q, but was %#v.", moduleName, expectedSuffix, bootClasspath) |
| 1263 | } |
| 1264 | } |
Jiyong Park | 19604de | 2020-03-24 16:44:11 +0900 | [diff] [blame] | 1265 | |
| 1266 | func TestAidlExportIncludeDirsFromImports(t *testing.T) { |
| 1267 | ctx, _ := testJava(t, ` |
| 1268 | java_library { |
| 1269 | name: "foo", |
| 1270 | srcs: ["aidl/foo/IFoo.aidl"], |
| 1271 | libs: ["bar"], |
| 1272 | } |
| 1273 | |
| 1274 | java_import { |
| 1275 | name: "bar", |
| 1276 | jars: ["a.jar"], |
| 1277 | aidl: { |
| 1278 | export_include_dirs: ["aidl/bar"], |
| 1279 | }, |
| 1280 | } |
| 1281 | `) |
| 1282 | |
| 1283 | aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command |
| 1284 | expectedAidlFlag := "-Iaidl/bar" |
| 1285 | if !strings.Contains(aidlCommand, expectedAidlFlag) { |
| 1286 | t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag) |
| 1287 | } |
| 1288 | } |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 1289 | |
Jooyung Han | e197d8b | 2021-01-05 10:33:16 +0900 | [diff] [blame] | 1290 | func TestAidlFlagsArePassedToTheAidlCompiler(t *testing.T) { |
| 1291 | ctx, _ := testJava(t, ` |
| 1292 | java_library { |
| 1293 | name: "foo", |
| 1294 | srcs: ["aidl/foo/IFoo.aidl"], |
| 1295 | aidl: { flags: ["-Werror"], }, |
| 1296 | } |
| 1297 | `) |
| 1298 | |
| 1299 | aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command |
| 1300 | expectedAidlFlag := "-Werror" |
| 1301 | if !strings.Contains(aidlCommand, expectedAidlFlag) { |
| 1302 | t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag) |
| 1303 | } |
| 1304 | } |
| 1305 | |
Jooyung Han | 07f70c0 | 2021-11-06 07:08:45 +0900 | [diff] [blame] | 1306 | func TestAidlFlagsWithMinSdkVersion(t *testing.T) { |
| 1307 | fixture := android.GroupFixturePreparers( |
| 1308 | prepareForJavaTest, FixtureWithPrebuiltApis(map[string][]string{"14": {"foo"}})) |
| 1309 | |
| 1310 | for _, tc := range []struct { |
| 1311 | name string |
| 1312 | sdkVersion string |
| 1313 | expected string |
| 1314 | }{ |
| 1315 | {"default is current", "", "current"}, |
| 1316 | {"use sdk_version", `sdk_version: "14"`, "14"}, |
| 1317 | {"system_current", `sdk_version: "system_current"`, "current"}, |
| 1318 | } { |
| 1319 | t.Run(tc.name, func(t *testing.T) { |
| 1320 | ctx := fixture.RunTestWithBp(t, ` |
| 1321 | java_library { |
| 1322 | name: "foo", |
| 1323 | srcs: ["aidl/foo/IFoo.aidl"], |
| 1324 | `+tc.sdkVersion+` |
| 1325 | } |
| 1326 | `) |
| 1327 | aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command |
| 1328 | expectedAidlFlag := "--min_sdk_version=" + tc.expected |
| 1329 | if !strings.Contains(aidlCommand, expectedAidlFlag) { |
| 1330 | t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag) |
| 1331 | } |
| 1332 | }) |
| 1333 | } |
| 1334 | } |
| 1335 | |
Thiébaud Weksteen | de8417c | 2022-02-10 15:41:46 +1100 | [diff] [blame] | 1336 | func TestAidlEnforcePermissions(t *testing.T) { |
| 1337 | ctx, _ := testJava(t, ` |
| 1338 | java_library { |
| 1339 | name: "foo", |
| 1340 | srcs: ["aidl/foo/IFoo.aidl"], |
| 1341 | aidl: { enforce_permissions: true }, |
| 1342 | } |
| 1343 | `) |
| 1344 | |
| 1345 | aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command |
| 1346 | expectedAidlFlag := "-Wmissing-permission-annotation -Werror" |
| 1347 | if !strings.Contains(aidlCommand, expectedAidlFlag) { |
| 1348 | t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag) |
| 1349 | } |
| 1350 | } |
| 1351 | |
| 1352 | func TestAidlEnforcePermissionsException(t *testing.T) { |
| 1353 | ctx, _ := testJava(t, ` |
| 1354 | java_library { |
| 1355 | name: "foo", |
| 1356 | srcs: ["aidl/foo/IFoo.aidl", "aidl/foo/IFoo2.aidl"], |
| 1357 | aidl: { enforce_permissions: true, enforce_permissions_exceptions: ["aidl/foo/IFoo2.aidl"] }, |
| 1358 | } |
| 1359 | `) |
| 1360 | |
| 1361 | aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command |
| 1362 | expectedAidlFlag := "$$FLAGS -Wmissing-permission-annotation -Werror aidl/foo/IFoo.aidl" |
| 1363 | if !strings.Contains(aidlCommand, expectedAidlFlag) { |
| 1364 | t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag) |
| 1365 | } |
| 1366 | expectedAidlFlag = "$$FLAGS aidl/foo/IFoo2.aidl" |
| 1367 | if !strings.Contains(aidlCommand, expectedAidlFlag) { |
| 1368 | t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag) |
| 1369 | } |
| 1370 | } |
| 1371 | |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 1372 | func TestDataNativeBinaries(t *testing.T) { |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 1373 | ctx, _ := testJava(t, ` |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 1374 | java_test_host { |
| 1375 | name: "foo", |
| 1376 | srcs: ["a.java"], |
| 1377 | data_native_bins: ["bin"] |
| 1378 | } |
| 1379 | |
| 1380 | python_binary_host { |
| 1381 | name: "bin", |
| 1382 | srcs: ["bin.py"], |
| 1383 | } |
| 1384 | `) |
| 1385 | |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 1386 | buildOS := ctx.Config().BuildOS.String() |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 1387 | |
| 1388 | test := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost) |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 1389 | entries := android.AndroidMkEntriesForTest(t, ctx, test)[0] |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1390 | expected := []string{"out/soong/.intermediates/bin/" + buildOS + "_x86_64_PY3/bin:bin"} |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 1391 | actual := entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"] |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1392 | android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_COMPATIBILITY_SUPPORT_FILES", ctx.Config(), expected, actual) |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 1393 | } |
Yuexi Ma | 627263f | 2021-03-04 13:47:56 -0800 | [diff] [blame] | 1394 | |
| 1395 | func TestDefaultInstallable(t *testing.T) { |
| 1396 | ctx, _ := testJava(t, ` |
| 1397 | java_test_host { |
| 1398 | name: "foo" |
| 1399 | } |
| 1400 | `) |
| 1401 | |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 1402 | buildOS := ctx.Config().BuildOS.String() |
Yuexi Ma | 627263f | 2021-03-04 13:47:56 -0800 | [diff] [blame] | 1403 | module := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost) |
| 1404 | assertDeepEquals(t, "Default installable value should be true.", proptools.BoolPtr(true), |
| 1405 | module.properties.Installable) |
| 1406 | } |
Cole Faust | 75fffb1 | 2021-06-13 15:23:16 -0700 | [diff] [blame] | 1407 | |
| 1408 | func TestErrorproneEnabled(t *testing.T) { |
| 1409 | ctx, _ := testJava(t, ` |
| 1410 | java_library { |
| 1411 | name: "foo", |
| 1412 | srcs: ["a.java"], |
| 1413 | errorprone: { |
| 1414 | enabled: true, |
| 1415 | }, |
| 1416 | } |
| 1417 | `) |
| 1418 | |
| 1419 | javac := ctx.ModuleForTests("foo", "android_common").Description("javac") |
| 1420 | |
| 1421 | // Test that the errorprone plugins are passed to javac |
| 1422 | expectedSubstring := "-Xplugin:ErrorProne" |
| 1423 | if !strings.Contains(javac.Args["javacFlags"], expectedSubstring) { |
Cole Faust | 2b1536e | 2021-06-18 12:25:54 -0700 | [diff] [blame] | 1424 | t.Errorf("expected javacFlags to contain %q, got %q", expectedSubstring, javac.Args["javacFlags"]) |
Cole Faust | 75fffb1 | 2021-06-13 15:23:16 -0700 | [diff] [blame] | 1425 | } |
| 1426 | |
| 1427 | // Modules with errorprone { enabled: true } will include errorprone checks |
| 1428 | // in the main javac build rule. Only when RUN_ERROR_PRONE is true will |
| 1429 | // the explicit errorprone build rule be created. |
| 1430 | errorprone := ctx.ModuleForTests("foo", "android_common").MaybeDescription("errorprone") |
| 1431 | if errorprone.RuleParams.Description != "" { |
| 1432 | t.Errorf("expected errorprone build rule to not exist, but it did") |
| 1433 | } |
| 1434 | } |
Cole Faust | 2b1536e | 2021-06-18 12:25:54 -0700 | [diff] [blame] | 1435 | |
| 1436 | func TestErrorproneDisabled(t *testing.T) { |
| 1437 | bp := ` |
| 1438 | java_library { |
| 1439 | name: "foo", |
| 1440 | srcs: ["a.java"], |
| 1441 | errorprone: { |
| 1442 | enabled: false, |
| 1443 | }, |
| 1444 | } |
| 1445 | ` |
| 1446 | ctx := android.GroupFixturePreparers( |
| 1447 | PrepareForTestWithJavaDefaultModules, |
| 1448 | android.FixtureMergeEnv(map[string]string{ |
| 1449 | "RUN_ERROR_PRONE": "true", |
| 1450 | }), |
| 1451 | ).RunTestWithBp(t, bp) |
| 1452 | |
| 1453 | javac := ctx.ModuleForTests("foo", "android_common").Description("javac") |
| 1454 | |
| 1455 | // Test that the errorprone plugins are not passed to javac, like they would |
| 1456 | // be if enabled was true. |
| 1457 | expectedSubstring := "-Xplugin:ErrorProne" |
| 1458 | if strings.Contains(javac.Args["javacFlags"], expectedSubstring) { |
| 1459 | t.Errorf("expected javacFlags to not contain %q, got %q", expectedSubstring, javac.Args["javacFlags"]) |
| 1460 | } |
| 1461 | |
| 1462 | // Check that no errorprone build rule is created, like there would be |
| 1463 | // if enabled was unset and RUN_ERROR_PRONE was true. |
| 1464 | errorprone := ctx.ModuleForTests("foo", "android_common").MaybeDescription("errorprone") |
| 1465 | if errorprone.RuleParams.Description != "" { |
| 1466 | t.Errorf("expected errorprone build rule to not exist, but it did") |
| 1467 | } |
| 1468 | } |
| 1469 | |
| 1470 | func TestErrorproneEnabledOnlyByEnvironmentVariable(t *testing.T) { |
| 1471 | bp := ` |
| 1472 | java_library { |
| 1473 | name: "foo", |
| 1474 | srcs: ["a.java"], |
| 1475 | } |
| 1476 | ` |
| 1477 | ctx := android.GroupFixturePreparers( |
| 1478 | PrepareForTestWithJavaDefaultModules, |
| 1479 | android.FixtureMergeEnv(map[string]string{ |
| 1480 | "RUN_ERROR_PRONE": "true", |
| 1481 | }), |
| 1482 | ).RunTestWithBp(t, bp) |
| 1483 | |
| 1484 | javac := ctx.ModuleForTests("foo", "android_common").Description("javac") |
| 1485 | errorprone := ctx.ModuleForTests("foo", "android_common").Description("errorprone") |
| 1486 | |
| 1487 | // Check that the errorprone plugins are not passed to javac, because they |
| 1488 | // will instead be passed to the separate errorprone compilation |
| 1489 | expectedSubstring := "-Xplugin:ErrorProne" |
| 1490 | if strings.Contains(javac.Args["javacFlags"], expectedSubstring) { |
| 1491 | t.Errorf("expected javacFlags to not contain %q, got %q", expectedSubstring, javac.Args["javacFlags"]) |
| 1492 | } |
| 1493 | |
| 1494 | // Check that the errorprone plugin is enabled |
| 1495 | if !strings.Contains(errorprone.Args["javacFlags"], expectedSubstring) { |
| 1496 | t.Errorf("expected errorprone to contain %q, got %q", expectedSubstring, javac.Args["javacFlags"]) |
| 1497 | } |
| 1498 | } |
Sam Delmerico | b3342ce | 2022-01-20 21:10:28 +0000 | [diff] [blame] | 1499 | |
| 1500 | func TestDataDeviceBinsBuildsDeviceBinary(t *testing.T) { |
Sam Delmerico | b706b4e | 2022-06-01 15:45:02 +0000 | [diff] [blame] | 1501 | testCases := []struct { |
| 1502 | dataDeviceBinType string |
| 1503 | depCompileMultilib string |
| 1504 | variants []string |
| 1505 | expectedError string |
| 1506 | }{ |
| 1507 | { |
| 1508 | dataDeviceBinType: "first", |
| 1509 | depCompileMultilib: "first", |
| 1510 | variants: []string{"android_arm64_armv8-a"}, |
| 1511 | }, |
| 1512 | { |
| 1513 | dataDeviceBinType: "first", |
| 1514 | depCompileMultilib: "both", |
| 1515 | variants: []string{"android_arm64_armv8-a"}, |
| 1516 | }, |
| 1517 | { |
| 1518 | // this is true because our testing framework is set up with |
| 1519 | // Targets ~ [<64bit target>, <32bit target>], where 64bit is "first" |
| 1520 | dataDeviceBinType: "first", |
| 1521 | depCompileMultilib: "32", |
| 1522 | expectedError: `Android.bp:2:3: dependency "bar" of "foo" missing variant`, |
| 1523 | }, |
| 1524 | { |
| 1525 | dataDeviceBinType: "first", |
| 1526 | depCompileMultilib: "64", |
| 1527 | variants: []string{"android_arm64_armv8-a"}, |
| 1528 | }, |
| 1529 | { |
| 1530 | dataDeviceBinType: "both", |
| 1531 | depCompileMultilib: "both", |
| 1532 | variants: []string{ |
| 1533 | "android_arm_armv7-a-neon", |
| 1534 | "android_arm64_armv8-a", |
| 1535 | }, |
| 1536 | }, |
| 1537 | { |
| 1538 | dataDeviceBinType: "both", |
| 1539 | depCompileMultilib: "32", |
| 1540 | expectedError: `Android.bp:2:3: dependency "bar" of "foo" missing variant`, |
| 1541 | }, |
| 1542 | { |
| 1543 | dataDeviceBinType: "both", |
| 1544 | depCompileMultilib: "64", |
| 1545 | expectedError: `Android.bp:2:3: dependency "bar" of "foo" missing variant`, |
| 1546 | }, |
| 1547 | { |
| 1548 | dataDeviceBinType: "both", |
| 1549 | depCompileMultilib: "first", |
| 1550 | expectedError: `Android.bp:2:3: dependency "bar" of "foo" missing variant`, |
| 1551 | }, |
| 1552 | { |
| 1553 | dataDeviceBinType: "32", |
| 1554 | depCompileMultilib: "32", |
| 1555 | variants: []string{"android_arm_armv7-a-neon"}, |
| 1556 | }, |
| 1557 | { |
| 1558 | dataDeviceBinType: "32", |
| 1559 | depCompileMultilib: "first", |
| 1560 | expectedError: `Android.bp:2:3: dependency "bar" of "foo" missing variant`, |
| 1561 | }, |
| 1562 | { |
| 1563 | dataDeviceBinType: "32", |
| 1564 | depCompileMultilib: "both", |
| 1565 | variants: []string{"android_arm_armv7-a-neon"}, |
| 1566 | }, |
| 1567 | { |
| 1568 | dataDeviceBinType: "32", |
| 1569 | depCompileMultilib: "64", |
| 1570 | expectedError: `Android.bp:2:3: dependency "bar" of "foo" missing variant`, |
| 1571 | }, |
| 1572 | { |
| 1573 | dataDeviceBinType: "64", |
| 1574 | depCompileMultilib: "64", |
| 1575 | variants: []string{"android_arm64_armv8-a"}, |
| 1576 | }, |
| 1577 | { |
| 1578 | dataDeviceBinType: "64", |
| 1579 | depCompileMultilib: "both", |
| 1580 | variants: []string{"android_arm64_armv8-a"}, |
| 1581 | }, |
| 1582 | { |
| 1583 | dataDeviceBinType: "64", |
| 1584 | depCompileMultilib: "first", |
| 1585 | variants: []string{"android_arm64_armv8-a"}, |
| 1586 | }, |
| 1587 | { |
| 1588 | dataDeviceBinType: "64", |
| 1589 | depCompileMultilib: "32", |
| 1590 | expectedError: `Android.bp:2:3: dependency "bar" of "foo" missing variant`, |
| 1591 | }, |
| 1592 | { |
| 1593 | dataDeviceBinType: "prefer32", |
| 1594 | depCompileMultilib: "32", |
| 1595 | variants: []string{"android_arm_armv7-a-neon"}, |
| 1596 | }, |
| 1597 | { |
| 1598 | dataDeviceBinType: "prefer32", |
| 1599 | depCompileMultilib: "both", |
| 1600 | variants: []string{"android_arm_armv7-a-neon"}, |
| 1601 | }, |
| 1602 | { |
| 1603 | dataDeviceBinType: "prefer32", |
| 1604 | depCompileMultilib: "first", |
| 1605 | expectedError: `Android.bp:2:3: dependency "bar" of "foo" missing variant`, |
| 1606 | }, |
| 1607 | { |
| 1608 | dataDeviceBinType: "prefer32", |
| 1609 | depCompileMultilib: "64", |
| 1610 | expectedError: `Android.bp:2:3: dependency "bar" of "foo" missing variant`, |
| 1611 | }, |
Sam Delmerico | b3342ce | 2022-01-20 21:10:28 +0000 | [diff] [blame] | 1612 | } |
Sam Delmerico | b3342ce | 2022-01-20 21:10:28 +0000 | [diff] [blame] | 1613 | |
Sam Delmerico | b706b4e | 2022-06-01 15:45:02 +0000 | [diff] [blame] | 1614 | bpTemplate := ` |
Sam Delmerico | b3342ce | 2022-01-20 21:10:28 +0000 | [diff] [blame] | 1615 | java_test_host { |
| 1616 | name: "foo", |
| 1617 | srcs: ["test.java"], |
Sam Delmerico | b706b4e | 2022-06-01 15:45:02 +0000 | [diff] [blame] | 1618 | data_device_bins_%s: ["bar"], |
Sam Delmerico | b3342ce | 2022-01-20 21:10:28 +0000 | [diff] [blame] | 1619 | } |
| 1620 | |
| 1621 | cc_binary { |
| 1622 | name: "bar", |
Sam Delmerico | b706b4e | 2022-06-01 15:45:02 +0000 | [diff] [blame] | 1623 | compile_multilib: "%s", |
Sam Delmerico | b3342ce | 2022-01-20 21:10:28 +0000 | [diff] [blame] | 1624 | } |
| 1625 | ` |
| 1626 | |
Sam Delmerico | b706b4e | 2022-06-01 15:45:02 +0000 | [diff] [blame] | 1627 | for _, tc := range testCases { |
| 1628 | bp := fmt.Sprintf(bpTemplate, tc.dataDeviceBinType, tc.depCompileMultilib) |
Sam Delmerico | b3342ce | 2022-01-20 21:10:28 +0000 | [diff] [blame] | 1629 | |
Sam Delmerico | b706b4e | 2022-06-01 15:45:02 +0000 | [diff] [blame] | 1630 | errorHandler := android.FixtureExpectsNoErrors |
| 1631 | if tc.expectedError != "" { |
| 1632 | errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(tc.expectedError) |
| 1633 | } |
Sam Delmerico | b3342ce | 2022-01-20 21:10:28 +0000 | [diff] [blame] | 1634 | |
Sam Delmerico | b706b4e | 2022-06-01 15:45:02 +0000 | [diff] [blame] | 1635 | testName := fmt.Sprintf(`data_device_bins_%s with compile_multilib:"%s"`, tc.dataDeviceBinType, tc.depCompileMultilib) |
| 1636 | t.Run(testName, func(t *testing.T) { |
| 1637 | ctx := android.GroupFixturePreparers(PrepareForIntegrationTestWithJava). |
| 1638 | ExtendWithErrorHandler(errorHandler). |
| 1639 | RunTestWithBp(t, bp) |
| 1640 | if tc.expectedError != "" { |
| 1641 | return |
| 1642 | } |
| 1643 | |
| 1644 | buildOS := ctx.Config.BuildOS.String() |
| 1645 | fooVariant := ctx.ModuleForTests("foo", buildOS+"_common") |
| 1646 | fooMod := fooVariant.Module().(*TestHost) |
| 1647 | entries := android.AndroidMkEntriesForTest(t, ctx.TestContext, fooMod)[0] |
| 1648 | |
| 1649 | expectedAutogenConfig := `<option name="push-file" key="bar" value="/data/local/tests/unrestricted/foo/bar" />` |
| 1650 | autogen := fooVariant.Rule("autogen") |
| 1651 | if !strings.Contains(autogen.Args["extraConfigs"], expectedAutogenConfig) { |
| 1652 | t.Errorf("foo extraConfigs %v does not contain %q", autogen.Args["extraConfigs"], expectedAutogenConfig) |
| 1653 | } |
| 1654 | |
| 1655 | expectedData := []string{} |
| 1656 | for _, variant := range tc.variants { |
| 1657 | barVariant := ctx.ModuleForTests("bar", variant) |
| 1658 | relocated := barVariant.Output("bar") |
| 1659 | expectedInput := fmt.Sprintf("out/soong/.intermediates/bar/%s/unstripped/bar", variant) |
| 1660 | android.AssertPathRelativeToTopEquals(t, "relocation input", expectedInput, relocated.Input) |
| 1661 | |
| 1662 | expectedData = append(expectedData, fmt.Sprintf("out/soong/.intermediates/bar/%s/bar:bar", variant)) |
| 1663 | } |
| 1664 | |
| 1665 | actualData := entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"] |
| 1666 | android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_TEST_DATA", ctx.Config, expectedData, actualData) |
| 1667 | }) |
Sam Delmerico | b3342ce | 2022-01-20 21:10:28 +0000 | [diff] [blame] | 1668 | } |
| 1669 | } |