xref: /aosp_15_r20/external/bazelbuild-kotlin-rules/kotlin/jvm_test.bzl (revision 3a22c0a33dd99bcca39a024d43e6fbcc55c2806e)
1*3a22c0a3SAlix# Copyright 2022 Google LLC. All rights reserved.
2*3a22c0a3SAlix#
3*3a22c0a3SAlix# Licensed under the Apache License, Version 2.0 (the License);
4*3a22c0a3SAlix# you may not use this file except in compliance with the License.
5*3a22c0a3SAlix# You may obtain a copy of the License at
6*3a22c0a3SAlix#
7*3a22c0a3SAlix#     http://www.apache.org/licenses/LICENSE-2.0
8*3a22c0a3SAlix#
9*3a22c0a3SAlix# Unless required by applicable law or agreed to in writing, software
10*3a22c0a3SAlix# distributed under the License is distributed on an "AS IS" BASIS,
11*3a22c0a3SAlix# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*3a22c0a3SAlix# See the License for the specific language governing permissions and
13*3a22c0a3SAlix# limitations under the License.
14*3a22c0a3SAlix
15*3a22c0a3SAlix"""Kotlin macro for building and running tests on a JVM."""
16*3a22c0a3SAlix
17*3a22c0a3SAlixload("//bazel:stubs.bzl", "register_extension_info")
18*3a22c0a3SAlixload("//:visibility.bzl", "RULES_KOTLIN")
19*3a22c0a3SAlixload(":jvm_library.bzl", "kt_jvm_library")
20*3a22c0a3SAlix
21*3a22c0a3SAlixvisibility(RULES_KOTLIN)
22*3a22c0a3SAlix
23*3a22c0a3SAlixdef _lib_name(name):
24*3a22c0a3SAlix    return "%s_DO_NOT_DEPEND_LIB" % name
25*3a22c0a3SAlix
26*3a22c0a3SAlixdef kt_jvm_test(
27*3a22c0a3SAlix        name,
28*3a22c0a3SAlix        custom_kotlincopts = None,
29*3a22c0a3SAlix        deps = None,
30*3a22c0a3SAlix        disable_lint_checks = None,
31*3a22c0a3SAlix        features = None,
32*3a22c0a3SAlix        javacopts = None,
33*3a22c0a3SAlix        plugins = None,
34*3a22c0a3SAlix        runtime_deps = None,
35*3a22c0a3SAlix        srcs = None,
36*3a22c0a3SAlix        resources = None,
37*3a22c0a3SAlix        tags = None,
38*3a22c0a3SAlix        **kwargs):
39*3a22c0a3SAlix    """Wrapper around kt_jvm_library and java_test to conveniently declare tests written in Kotlin.
40*3a22c0a3SAlix
41*3a22c0a3SAlix    Use of this rule is discouraged for simple unit tests, which should instead use
42*3a22c0a3SAlix     go/junit_test_suites or other, more efficient ways of compiling and running unit tests.
43*3a22c0a3SAlix
44*3a22c0a3SAlix    Args:
45*3a22c0a3SAlix      name: Name of the target.
46*3a22c0a3SAlix      custom_kotlincopts: Additional flags to pass to Kotlin compiler defined by kt_compiler_opt.
47*3a22c0a3SAlix      deps: A list of dependencies.
48*3a22c0a3SAlix      disable_lint_checks: A list of AndroidLint checks to be skipped.
49*3a22c0a3SAlix      features: A list of enabled features, see go/be#common.features.
50*3a22c0a3SAlix      javacopts: Additional flags to pass to javac if used.
51*3a22c0a3SAlix      plugins: Java annotation processors to run at compile-time.
52*3a22c0a3SAlix      runtime_deps: A list of runtime dependencies.
53*3a22c0a3SAlix      srcs: A list of sources to compile.
54*3a22c0a3SAlix      tags: A list of string tags passed to generated targets.
55*3a22c0a3SAlix      resources: A list of data files to include in the jar file.
56*3a22c0a3SAlix      **kwargs: Additional parameters to pass on to generated java_test, see go/be-java#java_test.
57*3a22c0a3SAlix    """
58*3a22c0a3SAlix    if srcs:
59*3a22c0a3SAlix        runtime_deps = [_lib_name(name)] + (runtime_deps or [])
60*3a22c0a3SAlix
61*3a22c0a3SAlix        kt_jvm_library(
62*3a22c0a3SAlix            name = _lib_name(name),
63*3a22c0a3SAlix            srcs = srcs,
64*3a22c0a3SAlix            resources = resources,
65*3a22c0a3SAlix            deps = deps,
66*3a22c0a3SAlix            plugins = plugins,
67*3a22c0a3SAlix            javacopts = javacopts,
68*3a22c0a3SAlix            custom_kotlincopts = custom_kotlincopts,
69*3a22c0a3SAlix            disable_lint_checks = disable_lint_checks,
70*3a22c0a3SAlix            tags = tags,
71*3a22c0a3SAlix            features = features,
72*3a22c0a3SAlix            testonly = 1,
73*3a22c0a3SAlix            visibility = ["//visibility:private"],
74*3a22c0a3SAlix        )
75*3a22c0a3SAlix    elif deps:
76*3a22c0a3SAlix        fail("deps specified without sources. Use runtime_deps instead to specify any dependencies needed to run this test.")
77*3a22c0a3SAlix
78*3a22c0a3SAlix    native.java_test(
79*3a22c0a3SAlix        name = name,
80*3a22c0a3SAlix        runtime_deps = runtime_deps,
81*3a22c0a3SAlix        tags = tags,
82*3a22c0a3SAlix        features = features,
83*3a22c0a3SAlix        **kwargs
84*3a22c0a3SAlix    )
85*3a22c0a3SAlix
86*3a22c0a3SAlixregister_extension_info(
87*3a22c0a3SAlix    extension = kt_jvm_test,
88*3a22c0a3SAlix    label_regex_for_dep = "{extension_name}_DO_NOT_DEPEND_LIB",
89*3a22c0a3SAlix)
90