• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2022 The Bazel Authors. 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"""Bazel java_test rule"""
15
16load("//java/common:java_semantics.bzl", "semantics")
17load("//java/common/rules:java_binary.bzl", "BASE_TEST_ATTRIBUTES")
18load("//java/common/rules:rule_util.bzl", "merge_attrs")
19load("//java/common/rules/impl:java_helper.bzl", "helper")
20load(":bazel_java_binary.bzl", "BASE_BINARY_ATTRS", "bazel_base_binary_impl", "make_binary_rule")
21
22def _bazel_java_test_impl(ctx):
23    return bazel_base_binary_impl(ctx, is_test_rule_class = True) + helper.test_providers(ctx)
24
25def _java_test_initializer(**kwargs):
26    if "stamp" in kwargs and type(kwargs["stamp"]) == type(True):
27        kwargs["stamp"] = 1 if kwargs["stamp"] else 0
28    if "use_launcher" in kwargs and not kwargs["use_launcher"]:
29        kwargs["launcher"] = None
30    else:
31        # If launcher is not set or None, set it to config flag
32        if "launcher" not in kwargs or not kwargs["launcher"]:
33            kwargs["launcher"] = semantics.LAUNCHER_FLAG_LABEL
34    return kwargs
35
36java_test = make_binary_rule(
37    _bazel_java_test_impl,
38    doc = """
39<p>
40A <code>java_test()</code> rule compiles a Java test. A test is a binary wrapper around your
41test code. The test runner's main method is invoked instead of the main class being compiled.
42</p>
43
44<h4 id="java_test_implicit_outputs">Implicit output targets</h4>
45<ul>
46  <li><code><var>name</var>.jar</code>: A Java archive.</li>
47  <li><code><var>name</var>_deploy.jar</code>: A Java archive suitable
48    for deployment. (Only built if explicitly requested.) See the description of the
49    <code><var>name</var>_deploy.jar</code> output from
50    <a href="#java_binary">java_binary</a> for more details.</li>
51</ul>
52
53<p>
54See the section on <code>java_binary()</code> arguments. This rule also
55supports all <a href="https://bazel.build/reference/be/common-definitions#common-attributes-tests">attributes common
56to all test rules (*_test)</a>.
57</p>
58
59<h4 id="java_test_examples">Examples</h4>
60
61<pre class="code">
62<code class="lang-starlark">
63
64java_library(
65    name = "tests",
66    srcs = glob(["*.java"]),
67    deps = [
68        "//java/com/foo/base:testResources",
69        "//java/com/foo/testing/util",
70    ],
71)
72
73java_test(
74    name = "AllTests",
75    size = "small",
76    runtime_deps = [
77        ":tests",
78        "//util/mysql",
79    ],
80)
81</code>
82</pre>
83    """,
84    attrs = merge_attrs(
85        BASE_TEST_ATTRIBUTES,
86        BASE_BINARY_ATTRS,
87        {
88            "_lcov_merger": attr.label(
89                cfg = "exec",
90                default = configuration_field(
91                    fragment = "coverage",
92                    name = "output_generator",
93                ),
94            ),
95            "_collect_cc_coverage": attr.label(
96                cfg = "exec",
97                allow_single_file = True,
98                default = "@bazel_tools//tools/test:collect_cc_coverage",
99            ),
100        },
101        override_attrs = {
102            "use_testrunner": attr.bool(
103                default = True,
104                doc = semantics.DOCS.for_attribute("use_testrunner") + """
105<br/>
106You can use this to override the default
107behavior, which is to use test runner for
108<code>java_test</code> rules,
109and not use it for <code>java_binary</code> rules.  It is unlikely
110you will want to do this.  One use is for <code>AllTest</code>
111rules that are invoked by another rule (to set up a database
112before running the tests, for example).  The <code>AllTest</code>
113rule must be declared as a <code>java_binary</code>, but should
114still use the test runner as its main entry point.
115
116The name of a test runner class can be overridden with <code>main_class</code> attribute.
117                """,
118            ),
119            "stamp": attr.int(
120                default = 0,
121                values = [-1, 0, 1],
122                doc = """
123Whether to encode build information into the binary. Possible values:
124<ul>
125<li>
126  <code>stamp = 1</code>: Always stamp the build information into the binary, even in
127  <a href="https://bazel.build/docs/user-manual#stamp"><code>--nostamp</code></a> builds. <b>This
128  setting should be avoided</b>, since it potentially kills remote caching for the
129  binary and any downstream actions that depend on it.
130</li>
131<li>
132  <code>stamp = 0</code>: Always replace build information by constant values. This
133  gives good build result caching.
134</li>
135<li>
136  <code>stamp = -1</code>: Embedding of build information is controlled by the
137  <a href="https://bazel.build/docs/user-manual#stamp"><code>--[no]stamp</code></a> flag.
138</li>
139</ul>
140<p>Stamped binaries are <em>not</em> rebuilt unless their dependencies change.</p>
141                """,
142            ),
143        },
144        remove_attrs = ["deploy_env"],
145    ),
146    test = True,
147    initializer = _java_test_initializer,
148)
149