xref: /aosp_15_r20/external/bazelbuild-kotlin-rules/kotlin/jvm_test.bzl (revision 3a22c0a33dd99bcca39a024d43e6fbcc55c2806e)
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
15"""Kotlin macro for building and running tests on a JVM."""
16
17load("//bazel:stubs.bzl", "register_extension_info")
18load("//:visibility.bzl", "RULES_KOTLIN")
19load(":jvm_library.bzl", "kt_jvm_library")
20
21visibility(RULES_KOTLIN)
22
23def _lib_name(name):
24    return "%s_DO_NOT_DEPEND_LIB" % name
25
26def kt_jvm_test(
27        name,
28        custom_kotlincopts = None,
29        deps = None,
30        disable_lint_checks = None,
31        features = None,
32        javacopts = None,
33        plugins = None,
34        runtime_deps = None,
35        srcs = None,
36        resources = None,
37        tags = None,
38        **kwargs):
39    """Wrapper around kt_jvm_library and java_test to conveniently declare tests written in Kotlin.
40
41    Use of this rule is discouraged for simple unit tests, which should instead use
42     go/junit_test_suites or other, more efficient ways of compiling and running unit tests.
43
44    Args:
45      name: Name of the target.
46      custom_kotlincopts: Additional flags to pass to Kotlin compiler defined by kt_compiler_opt.
47      deps: A list of dependencies.
48      disable_lint_checks: A list of AndroidLint checks to be skipped.
49      features: A list of enabled features, see go/be#common.features.
50      javacopts: Additional flags to pass to javac if used.
51      plugins: Java annotation processors to run at compile-time.
52      runtime_deps: A list of runtime dependencies.
53      srcs: A list of sources to compile.
54      tags: A list of string tags passed to generated targets.
55      resources: A list of data files to include in the jar file.
56      **kwargs: Additional parameters to pass on to generated java_test, see go/be-java#java_test.
57    """
58    if srcs:
59        runtime_deps = [_lib_name(name)] + (runtime_deps or [])
60
61        kt_jvm_library(
62            name = _lib_name(name),
63            srcs = srcs,
64            resources = resources,
65            deps = deps,
66            plugins = plugins,
67            javacopts = javacopts,
68            custom_kotlincopts = custom_kotlincopts,
69            disable_lint_checks = disable_lint_checks,
70            tags = tags,
71            features = features,
72            testonly = 1,
73            visibility = ["//visibility:private"],
74        )
75    elif deps:
76        fail("deps specified without sources. Use runtime_deps instead to specify any dependencies needed to run this test.")
77
78    native.java_test(
79        name = name,
80        runtime_deps = runtime_deps,
81        tags = tags,
82        features = features,
83        **kwargs
84    )
85
86register_extension_info(
87    extension = kt_jvm_test,
88    label_regex_for_dep = "{extension_name}_DO_NOT_DEPEND_LIB",
89)
90