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 | 3a3e94c | 2019-01-23 15:39:50 -0800 | [diff] [blame] | 18 | "android/soong/android" |
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" |
| 22 | ) |
| 23 | |
| 24 | func TestKotlin(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 25 | ctx, _ := testJava(t, ` |
Colin Cross | 21fc9bb | 2019-01-18 15:05:09 -0800 | [diff] [blame] | 26 | java_library { |
| 27 | name: "foo", |
| 28 | srcs: ["a.java", "b.kt"], |
| 29 | } |
| 30 | |
| 31 | java_library { |
| 32 | name: "bar", |
| 33 | srcs: ["b.kt"], |
| 34 | libs: ["foo"], |
| 35 | static_libs: ["baz"], |
| 36 | } |
| 37 | |
| 38 | java_library { |
| 39 | name: "baz", |
| 40 | srcs: ["c.java"], |
| 41 | } |
| 42 | `) |
| 43 | |
| 44 | fooKotlinc := ctx.ModuleForTests("foo", "android_common").Rule("kotlinc") |
| 45 | fooJavac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
| 46 | fooJar := ctx.ModuleForTests("foo", "android_common").Output("combined/foo.jar") |
| 47 | |
| 48 | if len(fooKotlinc.Inputs) != 2 || fooKotlinc.Inputs[0].String() != "a.java" || |
| 49 | fooKotlinc.Inputs[1].String() != "b.kt" { |
| 50 | t.Errorf(`foo kotlinc inputs %v != ["a.java", "b.kt"]`, fooKotlinc.Inputs) |
| 51 | } |
| 52 | |
| 53 | if len(fooJavac.Inputs) != 1 || fooJavac.Inputs[0].String() != "a.java" { |
| 54 | t.Errorf(`foo inputs %v != ["a.java"]`, fooJavac.Inputs) |
| 55 | } |
| 56 | |
| 57 | if !strings.Contains(fooJavac.Args["classpath"], fooKotlinc.Output.String()) { |
| 58 | t.Errorf("foo classpath %v does not contain %q", |
| 59 | fooJavac.Args["classpath"], fooKotlinc.Output.String()) |
| 60 | } |
| 61 | |
| 62 | if !inList(fooKotlinc.Output.String(), fooJar.Inputs.Strings()) { |
| 63 | t.Errorf("foo jar inputs %v does not contain %q", |
| 64 | fooJar.Inputs.Strings(), fooKotlinc.Output.String()) |
| 65 | } |
| 66 | |
| 67 | fooHeaderJar := ctx.ModuleForTests("foo", "android_common").Output("turbine-combined/foo.jar") |
| 68 | bazHeaderJar := ctx.ModuleForTests("baz", "android_common").Output("turbine-combined/baz.jar") |
| 69 | barKotlinc := ctx.ModuleForTests("bar", "android_common").Rule("kotlinc") |
| 70 | |
| 71 | if len(barKotlinc.Inputs) != 1 || barKotlinc.Inputs[0].String() != "b.kt" { |
| 72 | t.Errorf(`bar kotlinc inputs %v != ["b.kt"]`, barKotlinc.Inputs) |
| 73 | } |
| 74 | |
| 75 | if !inList(fooHeaderJar.Output.String(), barKotlinc.Implicits.Strings()) { |
| 76 | t.Errorf(`expected %q in bar implicits %v`, |
| 77 | fooHeaderJar.Output.String(), barKotlinc.Implicits.Strings()) |
| 78 | } |
| 79 | |
| 80 | if !inList(bazHeaderJar.Output.String(), barKotlinc.Implicits.Strings()) { |
| 81 | t.Errorf(`expected %q in bar implicits %v`, |
| 82 | bazHeaderJar.Output.String(), barKotlinc.Implicits.Strings()) |
| 83 | } |
| 84 | } |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 85 | |
| 86 | func TestKapt(t *testing.T) { |
Colin Cross | 748b2d8 | 2020-11-19 13:52:06 -0800 | [diff] [blame^] | 87 | bp := ` |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 88 | java_library { |
| 89 | name: "foo", |
| 90 | srcs: ["a.java", "b.kt"], |
Colin Cross | 5a11686 | 2020-04-22 11:44:34 -0700 | [diff] [blame] | 91 | plugins: ["bar", "baz"], |
Colin Cross | 748b2d8 | 2020-11-19 13:52:06 -0800 | [diff] [blame^] | 92 | errorprone: { |
| 93 | extra_check_modules: ["my_check"], |
| 94 | }, |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Colin Cross | be9cdb8 | 2019-01-21 21:37:16 -0800 | [diff] [blame] | 97 | java_plugin { |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 98 | name: "bar", |
Colin Cross | 3a3e94c | 2019-01-23 15:39:50 -0800 | [diff] [blame] | 99 | processor_class: "com.bar", |
| 100 | srcs: ["b.java"], |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 101 | } |
Colin Cross | 5a11686 | 2020-04-22 11:44:34 -0700 | [diff] [blame] | 102 | |
| 103 | java_plugin { |
| 104 | name: "baz", |
| 105 | processor_class: "com.baz", |
| 106 | srcs: ["b.java"], |
| 107 | } |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 108 | |
Colin Cross | 748b2d8 | 2020-11-19 13:52:06 -0800 | [diff] [blame^] | 109 | java_plugin { |
| 110 | name: "my_check", |
| 111 | srcs: ["b.java"], |
| 112 | } |
| 113 | ` |
| 114 | t.Run("", func(t *testing.T) { |
| 115 | ctx, _ := testJava(t, bp) |
Colin Cross | 3a3e94c | 2019-01-23 15:39:50 -0800 | [diff] [blame] | 116 | |
Colin Cross | 748b2d8 | 2020-11-19 13:52:06 -0800 | [diff] [blame^] | 117 | buildOS := android.BuildOs.String() |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 118 | |
Colin Cross | 748b2d8 | 2020-11-19 13:52:06 -0800 | [diff] [blame^] | 119 | kapt := ctx.ModuleForTests("foo", "android_common").Rule("kapt") |
| 120 | kotlinc := ctx.ModuleForTests("foo", "android_common").Rule("kotlinc") |
| 121 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
Colin Cross | 3a3e94c | 2019-01-23 15:39:50 -0800 | [diff] [blame] | 122 | |
Colin Cross | 748b2d8 | 2020-11-19 13:52:06 -0800 | [diff] [blame^] | 123 | bar := ctx.ModuleForTests("bar", buildOS+"_common").Rule("javac").Output.String() |
| 124 | baz := ctx.ModuleForTests("baz", buildOS+"_common").Rule("javac").Output.String() |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 125 | |
Colin Cross | 748b2d8 | 2020-11-19 13:52:06 -0800 | [diff] [blame^] | 126 | // Test that the kotlin and java sources are passed to kapt and kotlinc |
| 127 | if len(kapt.Inputs) != 2 || kapt.Inputs[0].String() != "a.java" || kapt.Inputs[1].String() != "b.kt" { |
| 128 | t.Errorf(`foo kapt inputs %v != ["a.java", "b.kt"]`, kapt.Inputs) |
| 129 | } |
| 130 | if len(kotlinc.Inputs) != 2 || kotlinc.Inputs[0].String() != "a.java" || kotlinc.Inputs[1].String() != "b.kt" { |
| 131 | t.Errorf(`foo kotlinc inputs %v != ["a.java", "b.kt"]`, kotlinc.Inputs) |
| 132 | } |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 133 | |
Colin Cross | 748b2d8 | 2020-11-19 13:52:06 -0800 | [diff] [blame^] | 134 | // Test that only the java sources are passed to javac |
| 135 | if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" { |
| 136 | t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs) |
| 137 | } |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 138 | |
Colin Cross | 748b2d8 | 2020-11-19 13:52:06 -0800 | [diff] [blame^] | 139 | // Test that the kapt srcjar is a dependency of kotlinc and javac rules |
| 140 | if !inList(kapt.Output.String(), kotlinc.Implicits.Strings()) { |
| 141 | t.Errorf("expected %q in kotlinc implicits %v", kapt.Output.String(), kotlinc.Implicits.Strings()) |
| 142 | } |
| 143 | if !inList(kapt.Output.String(), javac.Implicits.Strings()) { |
| 144 | t.Errorf("expected %q in javac implicits %v", kapt.Output.String(), javac.Implicits.Strings()) |
| 145 | } |
Colin Cross | 3a3e94c | 2019-01-23 15:39:50 -0800 | [diff] [blame] | 146 | |
Colin Cross | 748b2d8 | 2020-11-19 13:52:06 -0800 | [diff] [blame^] | 147 | // Test that the kapt srcjar is extracted by the kotlinc and javac rules |
| 148 | if kotlinc.Args["srcJars"] != kapt.Output.String() { |
| 149 | t.Errorf("expected %q in kotlinc srcjars %v", kapt.Output.String(), kotlinc.Args["srcJars"]) |
| 150 | } |
| 151 | if javac.Args["srcJars"] != kapt.Output.String() { |
| 152 | t.Errorf("expected %q in javac srcjars %v", kapt.Output.String(), kotlinc.Args["srcJars"]) |
| 153 | } |
Colin Cross | 3a3e94c | 2019-01-23 15:39:50 -0800 | [diff] [blame] | 154 | |
Colin Cross | 748b2d8 | 2020-11-19 13:52:06 -0800 | [diff] [blame^] | 155 | // Test that the processors are passed to kapt |
| 156 | expectedProcessorPath := "-P plugin:org.jetbrains.kotlin.kapt3:apclasspath=" + bar + |
| 157 | " -P plugin:org.jetbrains.kotlin.kapt3:apclasspath=" + baz |
| 158 | if kapt.Args["kaptProcessorPath"] != expectedProcessorPath { |
| 159 | t.Errorf("expected kaptProcessorPath %q, got %q", expectedProcessorPath, kapt.Args["kaptProcessorPath"]) |
| 160 | } |
| 161 | expectedProcessor := "-P plugin:org.jetbrains.kotlin.kapt3:processors=com.bar -P plugin:org.jetbrains.kotlin.kapt3:processors=com.baz" |
| 162 | if kapt.Args["kaptProcessor"] != expectedProcessor { |
| 163 | t.Errorf("expected kaptProcessor %q, got %q", expectedProcessor, kapt.Args["kaptProcessor"]) |
| 164 | } |
| 165 | |
| 166 | // Test that the processors are not passed to javac |
| 167 | if javac.Args["processorpath"] != "" { |
| 168 | t.Errorf("expected processorPath '', got %q", javac.Args["processorpath"]) |
| 169 | } |
| 170 | if javac.Args["processor"] != "-proc:none" { |
| 171 | t.Errorf("expected processor '-proc:none', got %q", javac.Args["processor"]) |
| 172 | } |
| 173 | }) |
| 174 | |
| 175 | t.Run("errorprone", func(t *testing.T) { |
| 176 | env := map[string]string{ |
| 177 | "RUN_ERROR_PRONE": "true", |
| 178 | } |
| 179 | config := testConfig(env, bp, nil) |
| 180 | ctx, _ := testJavaWithConfig(t, config) |
| 181 | |
| 182 | buildOS := android.BuildOs.String() |
| 183 | |
| 184 | kapt := ctx.ModuleForTests("foo", "android_common").Rule("kapt") |
| 185 | //kotlinc := ctx.ModuleForTests("foo", "android_common").Rule("kotlinc") |
| 186 | javac := ctx.ModuleForTests("foo", "android_common").Description("javac") |
| 187 | errorprone := ctx.ModuleForTests("foo", "android_common").Description("errorprone") |
| 188 | |
| 189 | bar := ctx.ModuleForTests("bar", buildOS+"_common").Description("javac").Output.String() |
| 190 | baz := ctx.ModuleForTests("baz", buildOS+"_common").Description("javac").Output.String() |
| 191 | myCheck := ctx.ModuleForTests("my_check", buildOS+"_common").Description("javac").Output.String() |
| 192 | |
| 193 | // Test that the errorprone plugins are not passed to kapt |
| 194 | expectedProcessorPath := "-P plugin:org.jetbrains.kotlin.kapt3:apclasspath=" + bar + |
| 195 | " -P plugin:org.jetbrains.kotlin.kapt3:apclasspath=" + baz |
| 196 | if kapt.Args["kaptProcessorPath"] != expectedProcessorPath { |
| 197 | t.Errorf("expected kaptProcessorPath %q, got %q", expectedProcessorPath, kapt.Args["kaptProcessorPath"]) |
| 198 | } |
| 199 | expectedProcessor := "-P plugin:org.jetbrains.kotlin.kapt3:processors=com.bar -P plugin:org.jetbrains.kotlin.kapt3:processors=com.baz" |
| 200 | if kapt.Args["kaptProcessor"] != expectedProcessor { |
| 201 | t.Errorf("expected kaptProcessor %q, got %q", expectedProcessor, kapt.Args["kaptProcessor"]) |
| 202 | } |
| 203 | |
| 204 | // Test that the errorprone plugins are not passed to javac |
| 205 | if javac.Args["processorpath"] != "" { |
| 206 | t.Errorf("expected processorPath '', got %q", javac.Args["processorpath"]) |
| 207 | } |
| 208 | if javac.Args["processor"] != "-proc:none" { |
| 209 | t.Errorf("expected processor '-proc:none', got %q", javac.Args["processor"]) |
| 210 | } |
| 211 | |
| 212 | // Test that the errorprone plugins are passed to errorprone |
| 213 | expectedProcessorPath = "-processorpath " + myCheck |
| 214 | if errorprone.Args["processorpath"] != expectedProcessorPath { |
| 215 | t.Errorf("expected processorpath %q, got %q", expectedProcessorPath, errorprone.Args["processorpath"]) |
| 216 | } |
| 217 | if errorprone.Args["processor"] != "-proc:none" { |
| 218 | t.Errorf("expected processor '-proc:none', got %q", errorprone.Args["processor"]) |
| 219 | } |
| 220 | }) |
Colin Cross | afbb173 | 2019-01-17 15:42:52 -0800 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | func TestKaptEncodeFlags(t *testing.T) { |
| 224 | // Compares the kaptEncodeFlags against the results of the example implementation at |
| 225 | // https://kotlinlang.org/docs/reference/kapt.html#apjavac-options-encoding |
| 226 | tests := []struct { |
| 227 | in [][2]string |
| 228 | out string |
| 229 | }{ |
| 230 | { |
| 231 | // empty input |
| 232 | in: [][2]string{}, |
| 233 | out: "rO0ABXcEAAAAAA==", |
| 234 | }, |
| 235 | { |
| 236 | // common input |
| 237 | in: [][2]string{ |
| 238 | {"-source", "1.8"}, |
| 239 | {"-target", "1.8"}, |
| 240 | }, |
| 241 | out: "rO0ABXcgAAAAAgAHLXNvdXJjZQADMS44AActdGFyZ2V0AAMxLjg=", |
| 242 | }, |
| 243 | { |
| 244 | // input that serializes to a 255 byte block |
| 245 | in: [][2]string{ |
| 246 | {"-source", "1.8"}, |
| 247 | {"-target", "1.8"}, |
| 248 | {"a", strings.Repeat("b", 218)}, |
| 249 | }, |
| 250 | out: "rO0ABXf/AAAAAwAHLXNvdXJjZQADMS44AActdGFyZ2V0AAMxLjgAAWEA2mJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJi", |
| 251 | }, |
| 252 | { |
| 253 | // input that serializes to a 256 byte block |
| 254 | in: [][2]string{ |
| 255 | {"-source", "1.8"}, |
| 256 | {"-target", "1.8"}, |
| 257 | {"a", strings.Repeat("b", 219)}, |
| 258 | }, |
| 259 | out: "rO0ABXoAAAEAAAAAAwAHLXNvdXJjZQADMS44AActdGFyZ2V0AAMxLjgAAWEA22JiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYg==", |
| 260 | }, |
| 261 | { |
| 262 | // input that serializes to a 257 byte block |
| 263 | in: [][2]string{ |
| 264 | {"-source", "1.8"}, |
| 265 | {"-target", "1.8"}, |
| 266 | {"a", strings.Repeat("b", 220)}, |
| 267 | }, |
| 268 | out: "rO0ABXoAAAEBAAAAAwAHLXNvdXJjZQADMS44AActdGFyZ2V0AAMxLjgAAWEA3GJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmJiYmI=", |
| 269 | }, |
| 270 | } |
| 271 | |
| 272 | for i, test := range tests { |
| 273 | t.Run(strconv.Itoa(i), func(t *testing.T) { |
| 274 | got := kaptEncodeFlags(test.in) |
| 275 | if got != test.out { |
| 276 | t.Errorf("\nwant %q\n got %q", test.out, got) |
| 277 | } |
| 278 | }) |
| 279 | } |
| 280 | } |