1# Copyright 2022 Google LLC. 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 15load( 16 "//kotlin:rules.bzl", 17 "kt_jvm_library", 18 "kt_jvm_test", 19) 20load("@bazel_skylib//rules:build_test.bzl", "build_test") 21 22licenses(["notice"]) 23 24package(default_visibility = [ 25 "//tests/android/java/com/google/local_test:__subpackages__", 26]) 27 28build_test( 29 name = "kapt_regressions", 30 tags = ["darwin_x86_64_compatible"], 31 targets = [ 32 ":test_interface_with_default_impls", 33 ":test_javac", 34 ":test_kapt", 35 ":test_javac", 36 ":test_javac_with_exported_plugin", 37 ":test_kapt_with_exported_plugin", 38 ":test_no_processors", 39 ":test_noop", 40 ":test_noop_processors_ignored", 41 ":test_processor_flags", 42 ":test_processor_with_data", 43 ":test_reference", 44 ":test_stubs", 45 ":test_direct_classpath", 46 ], 47) 48 49# Regression test for no processor outputs (b/79878966) 50# and running non-service processors (b/110540324) 51kt_jvm_library( 52 name = "test_noop", 53 srcs = ["Noop.kt"], 54 plugins = [ 55 ":non_service_processor", 56 ], 57) 58 59kt_jvm_library( 60 name = "test_interface_with_default_impls", 61 srcs = [ 62 "InterfaceWithDefaultImpls.kt", 63 ], 64 plugins = [":AP"], # trigger kapt for regression test (b/121222399) 65) 66 67kt_jvm_library( 68 name = "test_kapt", 69 srcs = [ 70 "KTest.kt", 71 ], 72 plugins = [":AP"], 73 deps = [ 74 ":anno", 75 ], 76) 77 78# Test support for -A processor flags (b/134963914). 79kt_jvm_library( 80 name = "test_processor_flags", 81 srcs = ["FlagTest.kt"], 82 javacopts = ["-Akapt.AP.indexPrefix=Gen"], 83 plugins = [":AP"], 84 deps = [":anno"], 85) 86 87# Skip kapt altogether when no plugin defines a processor (b/110540324). 88kt_jvm_library( 89 name = "test_no_processors", 90 srcs = [ 91 "Noop.kt", 92 ], 93 plugins = [ 94 ":ignored_failing_processor", # would fail if run 95 ], 96 deps = [ 97 ":anno", 98 ], 99) 100 101# Skip plugins without processors, for consistency with how Blaze runs javac (b/110540324). 102kt_jvm_library( 103 name = "test_noop_processors_ignored", 104 srcs = [ 105 "KTest.kt", 106 ], 107 plugins = [ 108 ":AP", 109 ":ignored_failing_processor", # would fail if run 110 ], 111 deps = [ 112 ":anno", 113 ], 114) 115 116# Use kapt to annotation-process a .java file 117kt_jvm_library( 118 name = "test_kapt_java", 119 srcs = [ 120 "Noop.kt", # needed so kapt is used 121 "Test.java", 122 ], 123 plugins = [":AP"], 124 deps = [ 125 ":anno", 126 ], 127) 128 129# No .kt sources: javac is used for annotation processing 130kt_jvm_library( 131 name = "test_javac", 132 srcs = ["Test.java"], 133 plugins = [":AP"], 134 deps = [ 135 ":anno", 136 ], 137) 138 139# Test for java_library picking up exported_plugin from kt_jvm_library 140java_library( 141 name = "test_javac_with_exported_plugin", 142 srcs = ["Test.java"], 143 deps = [ 144 ":anno_with_plugin", 145 ], 146) 147 148# Reference build with java_library 149java_library( 150 name = "test_reference", 151 srcs = ["Test.java"], 152 plugins = [":AP"], 153 deps = [ 154 ":anno", 155 ], 156) 157 158# This target compiles repro code for b/118338417 and b/110373008. Since these are issues with the 159# kapt-generated .java stub files, which aren't currently used by the Kotlin build rules, this 160# target compiles, but we can manually inspect the stubs to see if issues were fixed. 161# TODO: Turn this into a dump test asserting the content of the generated stubs 162kt_jvm_library( 163 name = "test_stubs", 164 srcs = [ 165 "InnerEnumImport.kt", # repro for https://youtrack.jetbrains.net/issue/KT-28220 166 "SealedClasses.kt", # repro for https://youtrack.jetbrains.net/issue/KT-29924 167 "StaticImport.kt", # Static* repro https://youtrack.jetbrains.net/issue/KT-25071 (fixed) 168 "StaticMethod.java", 169 ], 170 plugins = [":AP"], 171) 172 173java_plugin( 174 name = "AP", 175 generates_api = 1, 176 processor_class = "kapt.AP", 177 deps = [":AP_lib"], 178) 179 180java_library( 181 name = "AP_lib", 182 srcs = ["AP.java"], 183 deps = [ 184 ":anno", 185 "//bazel:auto_service", 186 ], 187) 188 189# Processor that doesn't advertise itself to j.u.ServiceLoader (regression for b/110540324) 190java_plugin( 191 name = "non_service_processor", 192 srcs = ["NonServiceProcessor.java"], 193 processor_class = "kapt.NonServiceProcessor", 194) 195 196# Processor that would fail but doesn't declare processor_class and hence shouldn't run. 197java_plugin( 198 name = "ignored_failing_processor", 199 srcs = ["FailingProcessor.java"], 200 deps = [ 201 "//bazel:auto_service", 202 ], 203) 204 205java_library( 206 name = "anno", 207 srcs = ["Count.java"], 208) 209 210java_library( 211 name = "anno-android", 212 srcs = ["Count.java"], 213) 214 215kt_jvm_library( 216 name = "anno_with_plugin", 217 srcs = ["Count.java"], 218 exported_plugins = [":AP"], 219) 220 221kt_jvm_library( 222 name = "test_kapt_with_exported_plugin", 223 srcs = ["KTest.kt"], 224 deps = [ 225 ":anno_with_plugin", 226 ], 227) 228 229# Regression test for b/199932860: processor with data dependency 230java_plugin( 231 name = "ProcessorWithData", 232 data = ["MakeHelperClass.java.template"], 233 processor_class = "kapt.ProcessorWithData", 234 deps = [":processor_with_data"], 235) 236 237kt_jvm_library( 238 name = "processor_with_data", 239 srcs = [ 240 "MakeHelper.kt", 241 "ProcessorWithData.kt", 242 ], 243 deps = [ 244 "//bazel:auto_service", 245 ], 246) 247 248kt_jvm_library( 249 name = "make_helper", 250 srcs = ["MakeHelper.kt"], 251 exported_plugins = [":ProcessorWithData"], 252) 253 254kt_jvm_library( 255 name = "test_processor_with_data", 256 srcs = ["TriggerHelper.kt"], 257 deps = [":make_helper"], 258) 259 260kt_jvm_library( 261 name = "generates_services", 262 srcs = ["GeneratesServices.kt"], 263 deps = [ 264 "//bazel:auto_service", 265 ], 266) 267 268kt_jvm_test( 269 name = "GeneratesServicesTest", 270 srcs = ["GeneratesServicesTest.kt"], 271 tags = ["darwin_x86_64_compatible"], 272 deps = [ 273 ":generates_services", 274 "@maven//:com_google_truth_truth", 275 "@maven//:junit_junit", 276 ], 277) 278 279kt_jvm_library( 280 name = "dagger_c", 281 testonly = 1, 282 srcs = ["DaggerC.kt"], 283 deps = [ 284 "@maven//:javax_inject_jsr330_api", 285 ], 286) 287 288kt_jvm_library( 289 name = "dagger_b", 290 testonly = 1, 291 srcs = ["DaggerB.kt"], 292 deps = [ 293 ":dagger_c", 294 "@maven//:javax_inject_jsr330_api", 295 ], 296) 297 298kt_jvm_library( 299 name = "dagger_a", 300 testonly = 1, 301 srcs = ["DaggerA.kt"], 302 deps = [ 303 ":dagger_b", 304 ":dagger_c", 305 "@dagger", 306 ], 307) 308 309# Tests a scenario where Dagger generates code referencing indirect dependencies, which can confuse 310# Blaze's direct classpath optimization for running Turbine. 311kt_jvm_library( 312 name = "test_direct_classpath", 313 testonly = 1, 314 srcs = ["DaggerTopLevel.kt"], 315 deps = [ 316 ":dagger_a", 317 ], 318) 319 320# Demonstrates a simple compile test that uses Kotlin kapt stubs (b/199411692) 321kt_jvm_test( 322 name = "APTest", 323 srcs = ["APTest.kt"], 324 tags = ["darwin_x86_64_compatible"], 325 deps = [ 326 ":AP_lib", 327 "@maven//:com_google_testing_compile_compile_testing", 328 "@maven//:com_google_truth_truth", 329 "@maven//:junit_junit", 330 ], 331) 332