1# Copyright (C) 2020 The Dagger Authors. 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"""Macros for building compiler tests.""" 16 17load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_jvm_library") 18 19def compiler_test(name, size = "large", compiler_deps = None, **kwargs): 20 """Generates a java_test that tests java compilation with the given compiler deps. 21 22 This macro separates the compiler dependencies from the test dependencies to avoid 23 1-version violations. For example, this often happens when the java_test uses java 24 dependencies but the compiler test expects the android version of the dependencies. 25 26 Args: 27 name: The name of the java_test. 28 size: The size of the test (default "large" since this test does disk I/O). 29 compiler_deps: The deps needed during compilation. 30 **kwargs: The parameters to pass to the generated java_test. 31 32 Returns: 33 None 34 """ 35 36 # This JAR is loaded at runtime and contains the dependencies used by the compiler during tests. 37 # We separate these dependencies from the java_test dependencies to avoid 1 version violations. 38 native.java_binary( 39 name = name + "_compiler_deps", 40 testonly = 1, 41 tags = ["notap"], 42 visibility = ["//visibility:private"], 43 main_class = "Object.class", 44 runtime_deps = compiler_deps, 45 ) 46 47 # Add the compiler deps jar, generated above, to the test's data. 48 kwargs["data"] = kwargs.get("data", []) + [name + "_compiler_deps_deploy.jar"] 49 50 # Need to check for srcs since for Kotlin tests we use a runtime dep on the kt_jvm_library 51 # target. We don't need to worry about adding a compile testing dep since kt_compiler_test 52 # adds that in the kt_jvm_library. Adding this dep automatically is merely a convenience 53 # for cases with srcs anyway. 54 if kwargs.get("srcs", None): 55 # Add a dep to allow usage of CompilerTests. 56 kwargs["deps"] = kwargs.get("deps", []) + ["//java/dagger/testing/compile"] 57 58 native.java_test(name = name, size = size, **kwargs) 59 60def kt_compiler_test(name, srcs = [], deps = [], **kwargs): 61 """Generates a java_test that tests java compilation with the given compiler deps. 62 63 This macro works the same as the above compiler_test, but for Kotlin sources. 64 65 Args: 66 name: The name of the java_test. 67 srcs: Source files for the test (typically should include Kotlin sources). If no 68 sources are needed, just use compiler_test with runtime_deps. 69 deps: Deps for compiling the files in srcs. 70 **kwargs: The parameters to pass to compiler_test 71 72 Returns: 73 None 74 """ 75 kt_jvm_library( 76 name = name + "_ktlib", 77 testonly = 1, 78 srcs = srcs, 79 deps = deps + ["//java/dagger/testing/compile"], 80 visibility = ["//visibility:private"], 81 ) 82 83 compiler_test( 84 name = name, 85 runtime_deps = [ 86 ":" + name + "_ktlib", 87 ], 88 **kwargs 89 ) 90