• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"""Macros for building compiler tests."""
15
16load("@rules_java//java:defs.bzl", "java_binary", "java_test")
17load("@io_bazel_rules_kotlin//kotlin:jvm.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    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    java_test(name = name, size = size, **kwargs)
58
59def kt_compiler_test(name, srcs = [], deps = [], **kwargs):
60    """Generates a java_test that tests java compilation with the given compiler deps.
61
62    This macro works the same as the above compiler_test, but for Kotlin sources.
63
64    Args:
65      name: The name of the java_test.
66      srcs: Source files for the test (typically should include Kotlin sources). If no
67            sources are needed, just use compiler_test with runtime_deps.
68      deps: Deps for compiling the files in srcs.
69      **kwargs: The parameters to pass to compiler_test
70
71    Returns:
72      None
73    """
74    kt_jvm_library(
75        name = name + "_ktlib",
76        testonly = 1,
77        srcs = srcs,
78        deps = deps + ["//java/dagger/testing/compile"],
79        visibility = ["//visibility:private"],
80    )
81
82    compiler_test(
83        name = name,
84        runtime_deps = [
85            ":" + name + "_ktlib",
86        ],
87        **kwargs
88    )
89