Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [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 ( |
Colin Cross | 882d600 | 2024-08-14 10:24:06 -0700 | [diff] [blame] | 18 | "slices" |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 19 | "strconv" |
Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 20 | "strings" |
| 21 | "testing" |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 22 | |
| 23 | "android/soong/android" |
Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 24 | ) |
| 25 | |
| 26 | func TestKotlin(t *testing.T) { |
Colin Cross | 844cb6a | 2025-01-29 15:53:21 -0800 | [diff] [blame^] | 27 | t.Parallel() |
Colin Cross | 882d600 | 2024-08-14 10:24:06 -0700 | [diff] [blame] | 28 | bp := ` |
Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 29 | java_library { |
| 30 | name: "foo", |
| 31 | srcs: ["a.java", "b.kt"], |
Colin Cross | 882d600 | 2024-08-14 10:24:06 -0700 | [diff] [blame] | 32 | static_libs: ["quz"], |
Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | java_library { |
| 36 | name: "bar", |
| 37 | srcs: ["b.kt"], |
| 38 | libs: ["foo"], |
| 39 | static_libs: ["baz"], |
| 40 | } |
| 41 | |
| 42 | java_library { |
| 43 | name: "baz", |
| 44 | srcs: ["c.java"], |
Colin Cross | 882d600 | 2024-08-14 10:24:06 -0700 | [diff] [blame] | 45 | static_libs: ["quz"], |
Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 46 | } |
Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 47 | |
Colin Cross | 882d600 | 2024-08-14 10:24:06 -0700 | [diff] [blame] | 48 | java_library { |
| 49 | name: "quz", |
| 50 | srcs: ["d.kt"], |
| 51 | }` |
Colin Cross | 0635447 | 2022-05-03 14:20:24 -0700 | [diff] [blame] | 52 | |
Colin Cross | 882d600 | 2024-08-14 10:24:06 -0700 | [diff] [blame] | 53 | kotlinStdlibTurbineCombinedJars := []string{ |
| 54 | "out/soong/.intermediates/default/java/kotlin-stdlib/android_common/turbine-combined/kotlin-stdlib.jar", |
| 55 | "out/soong/.intermediates/default/java/kotlin-stdlib-jdk7/android_common/turbine-combined/kotlin-stdlib-jdk7.jar", |
| 56 | "out/soong/.intermediates/default/java/kotlin-stdlib-jdk8/android_common/turbine-combined/kotlin-stdlib-jdk8.jar", |
| 57 | "out/soong/.intermediates/default/java/kotlin-annotations/android_common/turbine-combined/kotlin-annotations.jar", |
Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Colin Cross | c9b4f6b | 2024-07-26 15:25:46 -0700 | [diff] [blame] | 60 | kotlinStdlibTurbineJars := []string{ |
| 61 | "out/soong/.intermediates/default/java/kotlin-stdlib/android_common/turbine/kotlin-stdlib.jar", |
| 62 | "out/soong/.intermediates/default/java/kotlin-stdlib-jdk7/android_common/turbine/kotlin-stdlib-jdk7.jar", |
| 63 | "out/soong/.intermediates/default/java/kotlin-stdlib-jdk8/android_common/turbine/kotlin-stdlib-jdk8.jar", |
| 64 | "out/soong/.intermediates/default/java/kotlin-annotations/android_common/turbine/kotlin-annotations.jar", |
| 65 | } |
| 66 | |
Colin Cross | 882d600 | 2024-08-14 10:24:06 -0700 | [diff] [blame] | 67 | kotlinStdlibJavacJars := []string{ |
| 68 | "out/soong/.intermediates/default/java/kotlin-stdlib/android_common/javac/kotlin-stdlib.jar", |
| 69 | "out/soong/.intermediates/default/java/kotlin-stdlib-jdk7/android_common/javac/kotlin-stdlib-jdk7.jar", |
| 70 | "out/soong/.intermediates/default/java/kotlin-stdlib-jdk8/android_common/javac/kotlin-stdlib-jdk8.jar", |
| 71 | "out/soong/.intermediates/default/java/kotlin-annotations/android_common/javac/kotlin-annotations.jar", |
Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 72 | } |
| 73 | |
Colin Cross | 882d600 | 2024-08-14 10:24:06 -0700 | [diff] [blame] | 74 | bootclasspathTurbineCombinedJars := []string{ |
| 75 | "out/soong/.intermediates/default/java/stable.core.platform.api.stubs/android_common/turbine-combined/stable.core.platform.api.stubs.jar", |
| 76 | "out/soong/.intermediates/default/java/core-lambda-stubs/android_common/turbine-combined/core-lambda-stubs.jar", |
Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Colin Cross | c9b4f6b | 2024-07-26 15:25:46 -0700 | [diff] [blame] | 79 | bootclasspathTurbineJars := []string{ |
| 80 | "out/soong/.intermediates/default/java/stable.core.platform.api.stubs/android_common/turbine/stable.core.platform.api.stubs.jar", |
| 81 | "out/soong/.intermediates/default/java/core-lambda-stubs/android_common/turbine/core-lambda-stubs.jar", |
| 82 | } |
| 83 | |
Colin Cross | 882d600 | 2024-08-14 10:24:06 -0700 | [diff] [blame] | 84 | frameworkTurbineCombinedJars := []string{ |
| 85 | "out/soong/.intermediates/default/java/ext/android_common/turbine-combined/ext.jar", |
| 86 | "out/soong/.intermediates/default/java/framework/android_common/turbine-combined/framework.jar", |
Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Colin Cross | c9b4f6b | 2024-07-26 15:25:46 -0700 | [diff] [blame] | 89 | frameworkTurbineJars := []string{ |
| 90 | "out/soong/.intermediates/default/java/ext/android_common/turbine/ext.jar", |
| 91 | "out/soong/.intermediates/default/java/framework/android_common/turbine/framework.jar", |
| 92 | } |
| 93 | |
Colin Cross | 882d600 | 2024-08-14 10:24:06 -0700 | [diff] [blame] | 94 | testCases := []struct { |
| 95 | name string |
| 96 | |
| 97 | preparer android.FixturePreparer |
| 98 | |
| 99 | fooKotlincInputs []string |
| 100 | fooJavacInputs []string |
| 101 | fooKotlincClasspath []string |
| 102 | fooJavacClasspath []string |
| 103 | fooCombinedInputs []string |
| 104 | fooHeaderCombinedInputs []string |
| 105 | |
| 106 | barKotlincInputs []string |
| 107 | barKotlincClasspath []string |
| 108 | barCombinedInputs []string |
| 109 | barHeaderCombinedInputs []string |
| 110 | }{ |
| 111 | { |
| 112 | name: "normal", |
| 113 | preparer: android.NullFixturePreparer, |
| 114 | fooKotlincInputs: []string{"a.java", "b.kt"}, |
| 115 | fooJavacInputs: []string{"a.java"}, |
| 116 | fooKotlincClasspath: slices.Concat( |
| 117 | bootclasspathTurbineCombinedJars, |
| 118 | frameworkTurbineCombinedJars, |
| 119 | []string{"out/soong/.intermediates/quz/android_common/turbine-combined/quz.jar"}, |
| 120 | kotlinStdlibTurbineCombinedJars, |
| 121 | ), |
| 122 | fooJavacClasspath: slices.Concat( |
| 123 | []string{"out/soong/.intermediates/foo/android_common/kotlin_headers/foo.jar"}, |
| 124 | frameworkTurbineCombinedJars, |
| 125 | []string{"out/soong/.intermediates/quz/android_common/turbine-combined/quz.jar"}, |
| 126 | kotlinStdlibTurbineCombinedJars, |
| 127 | ), |
| 128 | fooCombinedInputs: slices.Concat( |
| 129 | []string{ |
| 130 | "out/soong/.intermediates/foo/android_common/kotlin/foo.jar", |
| 131 | "out/soong/.intermediates/foo/android_common/javac/foo.jar", |
| 132 | "out/soong/.intermediates/quz/android_common/combined/quz.jar", |
| 133 | }, |
| 134 | kotlinStdlibJavacJars, |
| 135 | ), |
| 136 | fooHeaderCombinedInputs: slices.Concat( |
| 137 | []string{ |
| 138 | "out/soong/.intermediates/foo/android_common/turbine/foo.jar", |
| 139 | "out/soong/.intermediates/foo/android_common/kotlin_headers/foo.jar", |
| 140 | "out/soong/.intermediates/quz/android_common/turbine-combined/quz.jar", |
| 141 | }, |
| 142 | kotlinStdlibTurbineCombinedJars, |
| 143 | ), |
| 144 | |
| 145 | barKotlincInputs: []string{"b.kt"}, |
| 146 | barKotlincClasspath: slices.Concat( |
| 147 | bootclasspathTurbineCombinedJars, |
| 148 | frameworkTurbineCombinedJars, |
| 149 | []string{ |
| 150 | "out/soong/.intermediates/foo/android_common/turbine-combined/foo.jar", |
| 151 | "out/soong/.intermediates/baz/android_common/turbine-combined/baz.jar", |
| 152 | }, |
| 153 | kotlinStdlibTurbineCombinedJars, |
| 154 | ), |
| 155 | barCombinedInputs: slices.Concat( |
| 156 | []string{ |
| 157 | "out/soong/.intermediates/bar/android_common/kotlin/bar.jar", |
| 158 | "out/soong/.intermediates/baz/android_common/combined/baz.jar", |
| 159 | }, |
| 160 | kotlinStdlibJavacJars, |
| 161 | []string{}, |
| 162 | ), |
| 163 | barHeaderCombinedInputs: slices.Concat( |
| 164 | []string{ |
| 165 | "out/soong/.intermediates/bar/android_common/kotlin_headers/bar.jar", |
| 166 | "out/soong/.intermediates/baz/android_common/turbine-combined/baz.jar", |
| 167 | }, |
| 168 | kotlinStdlibTurbineCombinedJars, |
| 169 | ), |
| 170 | }, |
Colin Cross | c9b4f6b | 2024-07-26 15:25:46 -0700 | [diff] [blame] | 171 | { |
| 172 | name: "transitive classpath", |
| 173 | preparer: PrepareForTestWithTransitiveClasspathEnabled, |
| 174 | fooKotlincInputs: []string{"a.java", "b.kt"}, |
| 175 | fooJavacInputs: []string{"a.java"}, |
| 176 | fooKotlincClasspath: slices.Concat( |
| 177 | bootclasspathTurbineJars, |
| 178 | frameworkTurbineJars, |
| 179 | []string{"out/soong/.intermediates/quz/android_common/kotlin_headers/quz.jar"}, |
| 180 | kotlinStdlibTurbineJars, |
| 181 | ), |
| 182 | fooJavacClasspath: slices.Concat( |
| 183 | []string{"out/soong/.intermediates/foo/android_common/kotlin_headers/foo.jar"}, |
| 184 | frameworkTurbineJars, |
| 185 | []string{"out/soong/.intermediates/quz/android_common/kotlin_headers/quz.jar"}, |
| 186 | kotlinStdlibTurbineJars, |
| 187 | ), |
| 188 | fooCombinedInputs: slices.Concat( |
| 189 | []string{ |
| 190 | "out/soong/.intermediates/foo/android_common/kotlin/foo.jar", |
| 191 | "out/soong/.intermediates/foo/android_common/javac/foo.jar", |
| 192 | "out/soong/.intermediates/quz/android_common/kotlin/quz.jar", |
| 193 | }, |
| 194 | kotlinStdlibJavacJars, |
| 195 | ), |
| 196 | fooHeaderCombinedInputs: slices.Concat( |
| 197 | []string{ |
| 198 | "out/soong/.intermediates/foo/android_common/turbine/foo.jar", |
| 199 | "out/soong/.intermediates/foo/android_common/kotlin_headers/foo.jar", |
| 200 | "out/soong/.intermediates/quz/android_common/kotlin_headers/quz.jar", |
| 201 | }, |
| 202 | kotlinStdlibTurbineJars, |
| 203 | ), |
| 204 | |
| 205 | barKotlincInputs: []string{"b.kt"}, |
| 206 | barKotlincClasspath: slices.Concat( |
| 207 | bootclasspathTurbineJars, |
| 208 | frameworkTurbineJars, |
| 209 | []string{ |
| 210 | "out/soong/.intermediates/foo/android_common/turbine/foo.jar", |
| 211 | "out/soong/.intermediates/foo/android_common/kotlin_headers/foo.jar", |
| 212 | "out/soong/.intermediates/quz/android_common/kotlin_headers/quz.jar", |
| 213 | }, |
| 214 | kotlinStdlibTurbineJars, |
| 215 | []string{"out/soong/.intermediates/baz/android_common/turbine/baz.jar"}, |
| 216 | ), |
| 217 | barCombinedInputs: slices.Concat( |
| 218 | []string{ |
| 219 | "out/soong/.intermediates/bar/android_common/kotlin/bar.jar", |
| 220 | "out/soong/.intermediates/baz/android_common/javac/baz.jar", |
| 221 | "out/soong/.intermediates/quz/android_common/kotlin/quz.jar", |
| 222 | }, |
| 223 | kotlinStdlibJavacJars, |
| 224 | ), |
| 225 | barHeaderCombinedInputs: slices.Concat( |
| 226 | []string{ |
| 227 | "out/soong/.intermediates/bar/android_common/kotlin_headers/bar.jar", |
| 228 | "out/soong/.intermediates/baz/android_common/turbine/baz.jar", |
| 229 | "out/soong/.intermediates/quz/android_common/kotlin_headers/quz.jar", |
| 230 | }, |
| 231 | kotlinStdlibTurbineJars, |
| 232 | ), |
| 233 | }, |
Colin Cross | 0635447 | 2022-05-03 14:20:24 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Colin Cross | 882d600 | 2024-08-14 10:24:06 -0700 | [diff] [blame] | 236 | for _, tt := range testCases { |
| 237 | t.Run(tt.name, func(t *testing.T) { |
Colin Cross | 844cb6a | 2025-01-29 15:53:21 -0800 | [diff] [blame^] | 238 | t.Parallel() |
Colin Cross | 882d600 | 2024-08-14 10:24:06 -0700 | [diff] [blame] | 239 | result := android.GroupFixturePreparers( |
| 240 | PrepareForTestWithJavaDefaultModules, |
| 241 | tt.preparer, |
| 242 | ).RunTestWithBp(t, bp) |
| 243 | foo := result.ModuleForTests("foo", "android_common") |
| 244 | fooKotlinc := foo.Rule("kotlinc") |
| 245 | android.AssertPathsRelativeToTopEquals(t, "foo kotlinc inputs", tt.fooKotlincInputs, fooKotlinc.Inputs) |
Sam Delmerico | 9f9c0a2 | 2022-11-29 11:19:37 -0500 | [diff] [blame] | 246 | |
Colin Cross | 882d600 | 2024-08-14 10:24:06 -0700 | [diff] [blame] | 247 | fooKotlincClasspath := android.ContentFromFileRuleForTests(t, result.TestContext, foo.Output("kotlinc/classpath.rsp")) |
| 248 | android.AssertStringPathsRelativeToTopEquals(t, "foo kotlinc classpath", result.Config, tt.fooKotlincClasspath, strings.Fields(fooKotlincClasspath)) |
Sam Delmerico | 9f9c0a2 | 2022-11-29 11:19:37 -0500 | [diff] [blame] | 249 | |
Colin Cross | 882d600 | 2024-08-14 10:24:06 -0700 | [diff] [blame] | 250 | fooJavac := foo.Rule("javac") |
| 251 | android.AssertPathsRelativeToTopEquals(t, "foo javac inputs", tt.fooJavacInputs, fooJavac.Inputs) |
Colin Cross | 0635447 | 2022-05-03 14:20:24 -0700 | [diff] [blame] | 252 | |
Colin Cross | 882d600 | 2024-08-14 10:24:06 -0700 | [diff] [blame] | 253 | fooJavacClasspath := fooJavac.Args["classpath"] |
| 254 | android.AssertStringPathsRelativeToTopEquals(t, "foo javac classpath", result.Config, tt.fooJavacClasspath, |
| 255 | strings.Split(strings.TrimPrefix(fooJavacClasspath, "-classpath "), ":")) |
Colin Cross | 220a9a1 | 2022-03-28 17:08:01 -0700 | [diff] [blame] | 256 | |
Colin Cross | 882d600 | 2024-08-14 10:24:06 -0700 | [diff] [blame] | 257 | fooCombinedJar := foo.Output("combined/foo.jar") |
| 258 | android.AssertPathsRelativeToTopEquals(t, "foo combined inputs", tt.fooCombinedInputs, fooCombinedJar.Inputs) |
Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 259 | |
Colin Cross | 882d600 | 2024-08-14 10:24:06 -0700 | [diff] [blame] | 260 | fooCombinedHeaderJar := foo.Output("turbine-combined/foo.jar") |
| 261 | android.AssertPathsRelativeToTopEquals(t, "foo header combined inputs", tt.fooHeaderCombinedInputs, fooCombinedHeaderJar.Inputs) |
Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 262 | |
Colin Cross | 882d600 | 2024-08-14 10:24:06 -0700 | [diff] [blame] | 263 | bar := result.ModuleForTests("bar", "android_common") |
| 264 | barKotlinc := bar.Rule("kotlinc") |
| 265 | android.AssertPathsRelativeToTopEquals(t, "bar kotlinc inputs", tt.barKotlincInputs, barKotlinc.Inputs) |
Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 266 | |
Colin Cross | 882d600 | 2024-08-14 10:24:06 -0700 | [diff] [blame] | 267 | barKotlincClasspath := android.ContentFromFileRuleForTests(t, result.TestContext, bar.Output("kotlinc/classpath.rsp")) |
| 268 | android.AssertStringPathsRelativeToTopEquals(t, "bar kotlinc classpath", result.Config, tt.barKotlincClasspath, strings.Fields(barKotlincClasspath)) |
| 269 | |
| 270 | barCombinedJar := bar.Output("combined/bar.jar") |
| 271 | android.AssertPathsRelativeToTopEquals(t, "bar combined inputs", tt.barCombinedInputs, barCombinedJar.Inputs) |
| 272 | |
| 273 | barCombinedHeaderJar := bar.Output("turbine-combined/bar.jar") |
| 274 | android.AssertPathsRelativeToTopEquals(t, "bar header combined inputs", tt.barHeaderCombinedInputs, barCombinedHeaderJar.Inputs) |
| 275 | }) |
Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 276 | } |
| 277 | } |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 278 | |
| 279 | func TestKapt(t *testing.T) { |
Colin Cross | 844cb6a | 2025-01-29 15:53:21 -0800 | [diff] [blame^] | 280 | t.Parallel() |
Colin Cross | 748b2d8 | 2020-11-19 13:52:06 -0800 | [diff] [blame] | 281 | bp := ` |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 282 | java_library { |
| 283 | name: "foo", |
| 284 | srcs: ["a.java", "b.kt"], |
Colin Cross | 5a11686 | 2020-04-22 11:44:34 -0700 | [diff] [blame] | 285 | plugins: ["bar", "baz"], |
Colin Cross | 748b2d8 | 2020-11-19 13:52:06 -0800 | [diff] [blame] | 286 | errorprone: { |
| 287 | extra_check_modules: ["my_check"], |
| 288 | }, |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 289 | } |
| 290 | |
Colin Cross | be9cdb8 | 2019-01-21 21:37:16 -0800 | [diff] [blame] | 291 | java_plugin { |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 292 | name: "bar", |
Colin Cross | 3a3e94c | 2019-01-23 15:39:50 -0800 | [diff] [blame] | 293 | processor_class: "com.bar", |
| 294 | srcs: ["b.java"], |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 295 | } |
Colin Cross | 5a11686 | 2020-04-22 11:44:34 -0700 | [diff] [blame] | 296 | |
| 297 | java_plugin { |
| 298 | name: "baz", |
| 299 | processor_class: "com.baz", |
| 300 | srcs: ["b.java"], |
| 301 | } |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 302 | |
Colin Cross | 748b2d8 | 2020-11-19 13:52:06 -0800 | [diff] [blame] | 303 | java_plugin { |
| 304 | name: "my_check", |
| 305 | srcs: ["b.java"], |
| 306 | } |
| 307 | ` |
| 308 | t.Run("", func(t *testing.T) { |
Colin Cross | 844cb6a | 2025-01-29 15:53:21 -0800 | [diff] [blame^] | 309 | t.Parallel() |
Colin Cross | 748b2d8 | 2020-11-19 13:52:06 -0800 | [diff] [blame] | 310 | ctx, _ := testJava(t, bp) |
Colin Cross | 3a3e94c | 2019-01-23 15:39:50 -0800 | [diff] [blame] | 311 | |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 312 | buildOS := ctx.Config().BuildOS.String() |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 313 | |
Colin Cross | f61766e | 2022-03-16 18:06:48 -0700 | [diff] [blame] | 314 | foo := ctx.ModuleForTests("foo", "android_common") |
| 315 | kaptStubs := foo.Rule("kapt") |
| 316 | turbineApt := foo.Description("turbine apt") |
| 317 | kotlinc := foo.Rule("kotlinc") |
| 318 | javac := foo.Rule("javac") |
Colin Cross | 3a3e94c | 2019-01-23 15:39:50 -0800 | [diff] [blame] | 319 | |
Colin Cross | 748b2d8 | 2020-11-19 13:52:06 -0800 | [diff] [blame] | 320 | bar := ctx.ModuleForTests("bar", buildOS+"_common").Rule("javac").Output.String() |
| 321 | baz := ctx.ModuleForTests("baz", buildOS+"_common").Rule("javac").Output.String() |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 322 | |
Colin Cross | 748b2d8 | 2020-11-19 13:52:06 -0800 | [diff] [blame] | 323 | // Test that the kotlin and java sources are passed to kapt and kotlinc |
Colin Cross | f61766e | 2022-03-16 18:06:48 -0700 | [diff] [blame] | 324 | if len(kaptStubs.Inputs) != 2 || kaptStubs.Inputs[0].String() != "a.java" || kaptStubs.Inputs[1].String() != "b.kt" { |
| 325 | t.Errorf(`foo kapt inputs %v != ["a.java", "b.kt"]`, kaptStubs.Inputs) |
Colin Cross | 748b2d8 | 2020-11-19 13:52:06 -0800 | [diff] [blame] | 326 | } |
| 327 | if len(kotlinc.Inputs) != 2 || kotlinc.Inputs[0].String() != "a.java" || kotlinc.Inputs[1].String() != "b.kt" { |
| 328 | t.Errorf(`foo kotlinc inputs %v != ["a.java", "b.kt"]`, kotlinc.Inputs) |
| 329 | } |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 330 | |
Colin Cross | f61766e | 2022-03-16 18:06:48 -0700 | [diff] [blame] | 331 | // Test that only the java sources are passed to turbine-apt and javac |
| 332 | if len(turbineApt.Inputs) != 1 || turbineApt.Inputs[0].String() != "a.java" { |
| 333 | t.Errorf(`foo turbine apt inputs %v != ["a.java"]`, turbineApt.Inputs) |
| 334 | } |
Colin Cross | 748b2d8 | 2020-11-19 13:52:06 -0800 | [diff] [blame] | 335 | if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" { |
| 336 | t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs) |
| 337 | } |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 338 | |
Colin Cross | f61766e | 2022-03-16 18:06:48 -0700 | [diff] [blame] | 339 | // Test that the kapt stubs jar is a dependency of turbine-apt |
| 340 | if !inList(kaptStubs.Output.String(), turbineApt.Implicits.Strings()) { |
| 341 | t.Errorf("expected %q in turbine-apt implicits %v", kaptStubs.Output.String(), kotlinc.Implicits.Strings()) |
Colin Cross | 748b2d8 | 2020-11-19 13:52:06 -0800 | [diff] [blame] | 342 | } |
Colin Cross | 3a3e94c | 2019-01-23 15:39:50 -0800 | [diff] [blame] | 343 | |
Colin Cross | f61766e | 2022-03-16 18:06:48 -0700 | [diff] [blame] | 344 | // Test that the turbine-apt srcjar is a dependency of kotlinc and javac rules |
Isaac Chiou | a23d994 | 2022-04-06 06:14:38 +0000 | [diff] [blame] | 345 | if !inList(turbineApt.Output.String(), kotlinc.Implicits.Strings()) { |
| 346 | t.Errorf("expected %q in kotlinc implicits %v", turbineApt.Output.String(), kotlinc.Implicits.Strings()) |
Colin Cross | 748b2d8 | 2020-11-19 13:52:06 -0800 | [diff] [blame] | 347 | } |
Isaac Chiou | a23d994 | 2022-04-06 06:14:38 +0000 | [diff] [blame] | 348 | if !inList(turbineApt.Output.String(), javac.Implicits.Strings()) { |
| 349 | t.Errorf("expected %q in javac implicits %v", turbineApt.Output.String(), javac.Implicits.Strings()) |
Colin Cross | f61766e | 2022-03-16 18:06:48 -0700 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | // Test that the turbine-apt srcjar is extracted by the kotlinc and javac rules |
Isaac Chiou | a23d994 | 2022-04-06 06:14:38 +0000 | [diff] [blame] | 353 | if kotlinc.Args["srcJars"] != turbineApt.Output.String() { |
| 354 | t.Errorf("expected %q in kotlinc srcjars %v", turbineApt.Output.String(), kotlinc.Args["srcJars"]) |
Colin Cross | f61766e | 2022-03-16 18:06:48 -0700 | [diff] [blame] | 355 | } |
Isaac Chiou | a23d994 | 2022-04-06 06:14:38 +0000 | [diff] [blame] | 356 | if javac.Args["srcJars"] != turbineApt.Output.String() { |
| 357 | t.Errorf("expected %q in javac srcjars %v", turbineApt.Output.String(), kotlinc.Args["srcJars"]) |
Colin Cross | 748b2d8 | 2020-11-19 13:52:06 -0800 | [diff] [blame] | 358 | } |
Colin Cross | 3a3e94c | 2019-01-23 15:39:50 -0800 | [diff] [blame] | 359 | |
Colin Cross | 748b2d8 | 2020-11-19 13:52:06 -0800 | [diff] [blame] | 360 | // Test that the processors are passed to kapt |
| 361 | expectedProcessorPath := "-P plugin:org.jetbrains.kotlin.kapt3:apclasspath=" + bar + |
| 362 | " -P plugin:org.jetbrains.kotlin.kapt3:apclasspath=" + baz |
Colin Cross | f61766e | 2022-03-16 18:06:48 -0700 | [diff] [blame] | 363 | if kaptStubs.Args["kaptProcessorPath"] != expectedProcessorPath { |
| 364 | t.Errorf("expected kaptProcessorPath %q, got %q", expectedProcessorPath, kaptStubs.Args["kaptProcessorPath"]) |
Colin Cross | 748b2d8 | 2020-11-19 13:52:06 -0800 | [diff] [blame] | 365 | } |
| 366 | expectedProcessor := "-P plugin:org.jetbrains.kotlin.kapt3:processors=com.bar -P plugin:org.jetbrains.kotlin.kapt3:processors=com.baz" |
Colin Cross | f61766e | 2022-03-16 18:06:48 -0700 | [diff] [blame] | 367 | if kaptStubs.Args["kaptProcessor"] != expectedProcessor { |
| 368 | t.Errorf("expected kaptProcessor %q, got %q", expectedProcessor, kaptStubs.Args["kaptProcessor"]) |
| 369 | } |
| 370 | |
| 371 | // Test that the processors are passed to turbine-apt |
| 372 | expectedProcessorPath = "--processorpath " + bar + " " + baz |
| 373 | if !strings.Contains(turbineApt.Args["turbineFlags"], expectedProcessorPath) { |
| 374 | t.Errorf("expected turbine-apt processorpath %q, got %q", expectedProcessorPath, turbineApt.Args["turbineFlags"]) |
| 375 | } |
| 376 | expectedProcessor = "--processors com.bar com.baz" |
| 377 | if !strings.Contains(turbineApt.Args["turbineFlags"], expectedProcessor) { |
| 378 | t.Errorf("expected turbine-apt processor %q, got %q", expectedProcessor, turbineApt.Args["turbineFlags"]) |
Colin Cross | 748b2d8 | 2020-11-19 13:52:06 -0800 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | // Test that the processors are not passed to javac |
| 382 | if javac.Args["processorpath"] != "" { |
| 383 | t.Errorf("expected processorPath '', got %q", javac.Args["processorpath"]) |
| 384 | } |
| 385 | if javac.Args["processor"] != "-proc:none" { |
| 386 | t.Errorf("expected processor '-proc:none', got %q", javac.Args["processor"]) |
| 387 | } |
| 388 | }) |
| 389 | |
| 390 | t.Run("errorprone", func(t *testing.T) { |
Colin Cross | 844cb6a | 2025-01-29 15:53:21 -0800 | [diff] [blame^] | 391 | t.Parallel() |
Colin Cross | 748b2d8 | 2020-11-19 13:52:06 -0800 | [diff] [blame] | 392 | env := map[string]string{ |
| 393 | "RUN_ERROR_PRONE": "true", |
| 394 | } |
Paul Duffin | b148a49 | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 395 | |
| 396 | result := android.GroupFixturePreparers( |
| 397 | PrepareForTestWithJavaDefaultModules, |
| 398 | android.FixtureMergeEnv(env), |
| 399 | ).RunTestWithBp(t, bp) |
Colin Cross | 748b2d8 | 2020-11-19 13:52:06 -0800 | [diff] [blame] | 400 | |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 401 | buildOS := result.Config.BuildOS.String() |
Colin Cross | 748b2d8 | 2020-11-19 13:52:06 -0800 | [diff] [blame] | 402 | |
Paul Duffin | b148a49 | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 403 | kapt := result.ModuleForTests("foo", "android_common").Rule("kapt") |
Paul Duffin | b148a49 | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 404 | javac := result.ModuleForTests("foo", "android_common").Description("javac") |
| 405 | errorprone := result.ModuleForTests("foo", "android_common").Description("errorprone") |
Colin Cross | 748b2d8 | 2020-11-19 13:52:06 -0800 | [diff] [blame] | 406 | |
Paul Duffin | b148a49 | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 407 | bar := result.ModuleForTests("bar", buildOS+"_common").Description("javac").Output.String() |
| 408 | baz := result.ModuleForTests("baz", buildOS+"_common").Description("javac").Output.String() |
| 409 | myCheck := result.ModuleForTests("my_check", buildOS+"_common").Description("javac").Output.String() |
Colin Cross | 748b2d8 | 2020-11-19 13:52:06 -0800 | [diff] [blame] | 410 | |
| 411 | // Test that the errorprone plugins are not passed to kapt |
| 412 | expectedProcessorPath := "-P plugin:org.jetbrains.kotlin.kapt3:apclasspath=" + bar + |
| 413 | " -P plugin:org.jetbrains.kotlin.kapt3:apclasspath=" + baz |
| 414 | if kapt.Args["kaptProcessorPath"] != expectedProcessorPath { |
| 415 | t.Errorf("expected kaptProcessorPath %q, got %q", expectedProcessorPath, kapt.Args["kaptProcessorPath"]) |
| 416 | } |
| 417 | expectedProcessor := "-P plugin:org.jetbrains.kotlin.kapt3:processors=com.bar -P plugin:org.jetbrains.kotlin.kapt3:processors=com.baz" |
| 418 | if kapt.Args["kaptProcessor"] != expectedProcessor { |
| 419 | t.Errorf("expected kaptProcessor %q, got %q", expectedProcessor, kapt.Args["kaptProcessor"]) |
| 420 | } |
| 421 | |
| 422 | // Test that the errorprone plugins are not passed to javac |
| 423 | if javac.Args["processorpath"] != "" { |
| 424 | t.Errorf("expected processorPath '', got %q", javac.Args["processorpath"]) |
| 425 | } |
| 426 | if javac.Args["processor"] != "-proc:none" { |
| 427 | t.Errorf("expected processor '-proc:none', got %q", javac.Args["processor"]) |
| 428 | } |
| 429 | |
| 430 | // Test that the errorprone plugins are passed to errorprone |
| 431 | expectedProcessorPath = "-processorpath " + myCheck |
| 432 | if errorprone.Args["processorpath"] != expectedProcessorPath { |
| 433 | t.Errorf("expected processorpath %q, got %q", expectedProcessorPath, errorprone.Args["processorpath"]) |
| 434 | } |
| 435 | if errorprone.Args["processor"] != "-proc:none" { |
| 436 | t.Errorf("expected processor '-proc:none', got %q", errorprone.Args["processor"]) |
| 437 | } |
| 438 | }) |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | func TestKaptEncodeFlags(t *testing.T) { |
Colin Cross | 844cb6a | 2025-01-29 15:53:21 -0800 | [diff] [blame^] | 442 | t.Parallel() |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 443 | // Compares the kaptEncodeFlags against the results of the example implementation at |
| 444 | // https://kotlinlang.org/docs/reference/kapt.html#apjavac-options-encoding |
| 445 | tests := []struct { |
| 446 | in [][2]string |
| 447 | out string |
| 448 | }{ |
| 449 | { |
| 450 | // empty input |
| 451 | in: [][2]string{}, |
| 452 | out: "rO0ABXcEAAAAAA==", |
| 453 | }, |
| 454 | { |
| 455 | // common input |
| 456 | in: [][2]string{ |
| 457 | {"-source", "1.8"}, |
| 458 | {"-target", "1.8"}, |
| 459 | }, |
| 460 | out: "rO0ABXcgAAAAAgAHLXNvdXJjZQADMS44AActdGFyZ2V0AAMxLjg=", |
| 461 | }, |
| 462 | { |
| 463 | // input that serializes to a 255 byte block |
| 464 | in: [][2]string{ |
| 465 | {"-source", "1.8"}, |
| 466 | {"-target", "1.8"}, |
| 467 | {"a", strings.Repeat("b", 218)}, |
| 468 | }, |
| 469 | out: "rO0ABXf/AAAAAwAHLXNvdXJjZQADMS44AActdGFyZ2V0AAMxLjgAAWEA2mJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJi", |
| 470 | }, |
| 471 | { |
| 472 | // input that serializes to a 256 byte block |
| 473 | in: [][2]string{ |
| 474 | {"-source", "1.8"}, |
| 475 | {"-target", "1.8"}, |
| 476 | {"a", strings.Repeat("b", 219)}, |
| 477 | }, |
| 478 | out: "rO0ABXoAAAEAAAAAAwAHLXNvdXJjZQADMS44AActdGFyZ2V0AAMxLjgAAWEA22JiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYg==", |
| 479 | }, |
| 480 | { |
| 481 | // input that serializes to a 257 byte block |
| 482 | in: [][2]string{ |
| 483 | {"-source", "1.8"}, |
| 484 | {"-target", "1.8"}, |
| 485 | {"a", strings.Repeat("b", 220)}, |
| 486 | }, |
| 487 | out: "rO0ABXoAAAEBAAAAAwAHLXNvdXJjZQADMS44AActdGFyZ2V0AAMxLjgAAWEA3GJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmI=", |
| 488 | }, |
| 489 | } |
| 490 | |
| 491 | for i, test := range tests { |
| 492 | t.Run(strconv.Itoa(i), func(t *testing.T) { |
Colin Cross | 844cb6a | 2025-01-29 15:53:21 -0800 | [diff] [blame^] | 493 | t.Parallel() |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 494 | got := kaptEncodeFlags(test.in) |
| 495 | if got != test.out { |
| 496 | t.Errorf("\nwant %q\n got %q", test.out, got) |
| 497 | } |
| 498 | }) |
| 499 | } |
| 500 | } |
Colin Cross | a1ff7c6 | 2021-09-17 14:11:52 -0700 | [diff] [blame] | 501 | |
| 502 | func TestKotlinCompose(t *testing.T) { |
Colin Cross | 844cb6a | 2025-01-29 15:53:21 -0800 | [diff] [blame^] | 503 | t.Parallel() |
Colin Cross | a1ff7c6 | 2021-09-17 14:11:52 -0700 | [diff] [blame] | 504 | result := android.GroupFixturePreparers( |
| 505 | PrepareForTestWithJavaDefaultModules, |
| 506 | ).RunTestWithBp(t, ` |
| 507 | java_library { |
| 508 | name: "androidx.compose.runtime_runtime", |
| 509 | } |
| 510 | |
Luca Stefani | 50098f7 | 2024-10-12 17:55:31 +0200 | [diff] [blame] | 511 | kotlin_plugin { |
Dave Mankoff | 0a3ef84 | 2024-10-02 18:47:24 +0000 | [diff] [blame] | 512 | name: "kotlin-compose-compiler-plugin", |
Colin Cross | a1ff7c6 | 2021-09-17 14:11:52 -0700 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | java_library { |
| 516 | name: "withcompose", |
| 517 | srcs: ["a.kt"], |
Colin Cross | 08b0a1c | 2022-05-02 13:59:33 -0700 | [diff] [blame] | 518 | plugins: ["plugin"], |
Colin Cross | a1ff7c6 | 2021-09-17 14:11:52 -0700 | [diff] [blame] | 519 | static_libs: ["androidx.compose.runtime_runtime"], |
| 520 | } |
| 521 | |
| 522 | java_library { |
| 523 | name: "nocompose", |
| 524 | srcs: ["a.kt"], |
| 525 | } |
Colin Cross | 08b0a1c | 2022-05-02 13:59:33 -0700 | [diff] [blame] | 526 | |
| 527 | java_plugin { |
| 528 | name: "plugin", |
| 529 | } |
Colin Cross | a1ff7c6 | 2021-09-17 14:11:52 -0700 | [diff] [blame] | 530 | `) |
| 531 | |
| 532 | buildOS := result.Config.BuildOS.String() |
| 533 | |
Dave Mankoff | 0a3ef84 | 2024-10-02 18:47:24 +0000 | [diff] [blame] | 534 | composeCompiler := result.ModuleForTests("kotlin-compose-compiler-plugin", buildOS+"_common").Rule("combineJar").Output |
Colin Cross | a1ff7c6 | 2021-09-17 14:11:52 -0700 | [diff] [blame] | 535 | withCompose := result.ModuleForTests("withcompose", "android_common") |
| 536 | noCompose := result.ModuleForTests("nocompose", "android_common") |
| 537 | |
| 538 | android.AssertStringListContains(t, "missing compose compiler dependency", |
| 539 | withCompose.Rule("kotlinc").Implicits.Strings(), composeCompiler.String()) |
| 540 | |
| 541 | android.AssertStringDoesContain(t, "missing compose compiler plugin", |
| 542 | withCompose.VariablesForTestsRelativeToTop()["kotlincFlags"], "-Xplugin="+composeCompiler.String()) |
| 543 | |
Colin Cross | 08b0a1c | 2022-05-02 13:59:33 -0700 | [diff] [blame] | 544 | android.AssertStringListContains(t, "missing kapt compose compiler dependency", |
| 545 | withCompose.Rule("kapt").Implicits.Strings(), composeCompiler.String()) |
| 546 | |
Colin Cross | a1ff7c6 | 2021-09-17 14:11:52 -0700 | [diff] [blame] | 547 | android.AssertStringListDoesNotContain(t, "unexpected compose compiler dependency", |
| 548 | noCompose.Rule("kotlinc").Implicits.Strings(), composeCompiler.String()) |
| 549 | |
| 550 | android.AssertStringDoesNotContain(t, "unexpected compose compiler plugin", |
| 551 | noCompose.VariablesForTestsRelativeToTop()["kotlincFlags"], "-Xplugin="+composeCompiler.String()) |
| 552 | } |
Luca Stefani | 50098f7 | 2024-10-12 17:55:31 +0200 | [diff] [blame] | 553 | |
| 554 | func TestKotlinPlugin(t *testing.T) { |
Colin Cross | 844cb6a | 2025-01-29 15:53:21 -0800 | [diff] [blame^] | 555 | t.Parallel() |
Luca Stefani | 50098f7 | 2024-10-12 17:55:31 +0200 | [diff] [blame] | 556 | result := android.GroupFixturePreparers( |
| 557 | PrepareForTestWithJavaDefaultModules, |
| 558 | ).RunTestWithBp(t, ` |
| 559 | kotlin_plugin { |
| 560 | name: "kotlin_plugin", |
| 561 | } |
| 562 | |
| 563 | java_library { |
| 564 | name: "with_kotlin_plugin", |
| 565 | srcs: ["a.kt"], |
| 566 | plugins: ["plugin"], |
| 567 | kotlin_plugins: ["kotlin_plugin"], |
| 568 | } |
| 569 | |
| 570 | java_library { |
| 571 | name: "no_kotlin_plugin", |
| 572 | srcs: ["a.kt"], |
| 573 | } |
| 574 | |
| 575 | java_plugin { |
| 576 | name: "plugin", |
| 577 | } |
| 578 | `) |
| 579 | |
| 580 | buildOS := result.Config.BuildOS.String() |
| 581 | |
| 582 | kotlinPlugin := result.ModuleForTests("kotlin_plugin", buildOS+"_common").Rule("combineJar").Output |
| 583 | withKotlinPlugin := result.ModuleForTests("with_kotlin_plugin", "android_common") |
| 584 | noKotlinPlugin := result.ModuleForTests("no_kotlin_plugin", "android_common") |
| 585 | |
| 586 | android.AssertStringListContains(t, "missing plugin compiler dependency", |
| 587 | withKotlinPlugin.Rule("kotlinc").Implicits.Strings(), kotlinPlugin.String()) |
| 588 | |
| 589 | android.AssertStringDoesContain(t, "missing kotlin plugin", |
| 590 | withKotlinPlugin.VariablesForTestsRelativeToTop()["kotlincFlags"], "-Xplugin="+kotlinPlugin.String()) |
| 591 | |
| 592 | android.AssertStringListContains(t, "missing kapt kotlin plugin dependency", |
| 593 | withKotlinPlugin.Rule("kapt").Implicits.Strings(), kotlinPlugin.String()) |
| 594 | |
| 595 | android.AssertStringListDoesNotContain(t, "unexpected kotlin plugin dependency", |
| 596 | noKotlinPlugin.Rule("kotlinc").Implicits.Strings(), kotlinPlugin.String()) |
| 597 | |
| 598 | android.AssertStringDoesNotContain(t, "unexpected kotlin plugin", |
| 599 | noKotlinPlugin.VariablesForTestsRelativeToTop()["kotlincFlags"], "-Xplugin="+kotlinPlugin.String()) |
| 600 | } |