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