1*f585d8a3SJacky Wang# Copyright (C) 2020 The Dagger Authors. 2*f585d8a3SJacky Wang# 3*f585d8a3SJacky Wang# Licensed under the Apache License, Version 2.0 (the "License"); 4*f585d8a3SJacky Wang# you may not use this file except in compliance with the License. 5*f585d8a3SJacky Wang# You may obtain a copy of the License at 6*f585d8a3SJacky Wang# 7*f585d8a3SJacky Wang# http://www.apache.org/licenses/LICENSE-2.0 8*f585d8a3SJacky Wang# 9*f585d8a3SJacky Wang# Unless required by applicable law or agreed to in writing, software 10*f585d8a3SJacky Wang# distributed under the License is distributed on an "AS IS" BASIS, 11*f585d8a3SJacky Wang# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*f585d8a3SJacky Wang# See the License for the specific language governing permissions and 13*f585d8a3SJacky Wang# limitations under the License. 14*f585d8a3SJacky Wang 15*f585d8a3SJacky Wang"""Macros for building compiler tests.""" 16*f585d8a3SJacky Wang 17*f585d8a3SJacky Wangload("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_jvm_library") 18*f585d8a3SJacky Wang 19*f585d8a3SJacky Wangdef compiler_test(name, size = "large", compiler_deps = None, **kwargs): 20*f585d8a3SJacky Wang """Generates a java_test that tests java compilation with the given compiler deps. 21*f585d8a3SJacky Wang 22*f585d8a3SJacky Wang This macro separates the compiler dependencies from the test dependencies to avoid 23*f585d8a3SJacky Wang 1-version violations. For example, this often happens when the java_test uses java 24*f585d8a3SJacky Wang dependencies but the compiler test expects the android version of the dependencies. 25*f585d8a3SJacky Wang 26*f585d8a3SJacky Wang Args: 27*f585d8a3SJacky Wang name: The name of the java_test. 28*f585d8a3SJacky Wang size: The size of the test (default "large" since this test does disk I/O). 29*f585d8a3SJacky Wang compiler_deps: The deps needed during compilation. 30*f585d8a3SJacky Wang **kwargs: The parameters to pass to the generated java_test. 31*f585d8a3SJacky Wang 32*f585d8a3SJacky Wang Returns: 33*f585d8a3SJacky Wang None 34*f585d8a3SJacky Wang """ 35*f585d8a3SJacky Wang 36*f585d8a3SJacky Wang # This JAR is loaded at runtime and contains the dependencies used by the compiler during tests. 37*f585d8a3SJacky Wang # We separate these dependencies from the java_test dependencies to avoid 1 version violations. 38*f585d8a3SJacky Wang native.java_binary( 39*f585d8a3SJacky Wang name = name + "_compiler_deps", 40*f585d8a3SJacky Wang testonly = 1, 41*f585d8a3SJacky Wang tags = ["notap"], 42*f585d8a3SJacky Wang visibility = ["//visibility:private"], 43*f585d8a3SJacky Wang main_class = "Object.class", 44*f585d8a3SJacky Wang runtime_deps = compiler_deps, 45*f585d8a3SJacky Wang ) 46*f585d8a3SJacky Wang 47*f585d8a3SJacky Wang # Add the compiler deps jar, generated above, to the test's data. 48*f585d8a3SJacky Wang kwargs["data"] = kwargs.get("data", []) + [name + "_compiler_deps_deploy.jar"] 49*f585d8a3SJacky Wang 50*f585d8a3SJacky Wang # Need to check for srcs since for Kotlin tests we use a runtime dep on the kt_jvm_library 51*f585d8a3SJacky Wang # target. We don't need to worry about adding a compile testing dep since kt_compiler_test 52*f585d8a3SJacky Wang # adds that in the kt_jvm_library. Adding this dep automatically is merely a convenience 53*f585d8a3SJacky Wang # for cases with srcs anyway. 54*f585d8a3SJacky Wang if kwargs.get("srcs", None): 55*f585d8a3SJacky Wang # Add a dep to allow usage of CompilerTests. 56*f585d8a3SJacky Wang kwargs["deps"] = kwargs.get("deps", []) + ["//java/dagger/testing/compile"] 57*f585d8a3SJacky Wang 58*f585d8a3SJacky Wang native.java_test(name = name, size = size, **kwargs) 59*f585d8a3SJacky Wang 60*f585d8a3SJacky Wangdef kt_compiler_test(name, srcs = [], deps = [], **kwargs): 61*f585d8a3SJacky Wang """Generates a java_test that tests java compilation with the given compiler deps. 62*f585d8a3SJacky Wang 63*f585d8a3SJacky Wang This macro works the same as the above compiler_test, but for Kotlin sources. 64*f585d8a3SJacky Wang 65*f585d8a3SJacky Wang Args: 66*f585d8a3SJacky Wang name: The name of the java_test. 67*f585d8a3SJacky Wang srcs: Source files for the test (typically should include Kotlin sources). If no 68*f585d8a3SJacky Wang sources are needed, just use compiler_test with runtime_deps. 69*f585d8a3SJacky Wang deps: Deps for compiling the files in srcs. 70*f585d8a3SJacky Wang **kwargs: The parameters to pass to compiler_test 71*f585d8a3SJacky Wang 72*f585d8a3SJacky Wang Returns: 73*f585d8a3SJacky Wang None 74*f585d8a3SJacky Wang """ 75*f585d8a3SJacky Wang kt_jvm_library( 76*f585d8a3SJacky Wang name = name + "_ktlib", 77*f585d8a3SJacky Wang testonly = 1, 78*f585d8a3SJacky Wang srcs = srcs, 79*f585d8a3SJacky Wang deps = deps + ["//java/dagger/testing/compile"], 80*f585d8a3SJacky Wang visibility = ["//visibility:private"], 81*f585d8a3SJacky Wang ) 82*f585d8a3SJacky Wang 83*f585d8a3SJacky Wang compiler_test( 84*f585d8a3SJacky Wang name = name, 85*f585d8a3SJacky Wang runtime_deps = [ 86*f585d8a3SJacky Wang ":" + name + "_ktlib", 87*f585d8a3SJacky Wang ], 88*f585d8a3SJacky Wang **kwargs 89*f585d8a3SJacky Wang ) 90