# Copyright 2022 The Bazel Authors. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Bazel java_test rule""" load("//java/common:java_semantics.bzl", "semantics") load("//java/common/rules:java_binary.bzl", "BASE_TEST_ATTRIBUTES") load("//java/common/rules:rule_util.bzl", "merge_attrs") load("//java/common/rules/impl:java_helper.bzl", "helper") load(":bazel_java_binary.bzl", "BASE_BINARY_ATTRS", "bazel_base_binary_impl", "make_binary_rule") def _bazel_java_test_impl(ctx): return bazel_base_binary_impl(ctx, is_test_rule_class = True) + helper.test_providers(ctx) def _java_test_initializer(**kwargs): if "stamp" in kwargs and type(kwargs["stamp"]) == type(True): kwargs["stamp"] = 1 if kwargs["stamp"] else 0 if "use_launcher" in kwargs and not kwargs["use_launcher"]: kwargs["launcher"] = None else: # If launcher is not set or None, set it to config flag if "launcher" not in kwargs or not kwargs["launcher"]: kwargs["launcher"] = semantics.LAUNCHER_FLAG_LABEL return kwargs java_test = make_binary_rule( _bazel_java_test_impl, doc = """
A java_test() rule compiles a Java test. A test is a binary wrapper around your
test code. The test runner's main method is invoked instead of the main class being compiled.
name.jar: A Java archive.name_deploy.jar: A Java archive suitable
for deployment. (Only built if explicitly requested.) See the description of the
name_deploy.jar output from
java_binary for more details.
See the section on java_binary() arguments. This rule also
supports all attributes common
to all test rules (*_test).
java_library(
name = "tests",
srcs = glob(["*.java"]),
deps = [
"//java/com/foo/base:testResources",
"//java/com/foo/testing/util",
],
)
java_test(
name = "AllTests",
size = "small",
runtime_deps = [
":tests",
"//util/mysql",
],
)
""",
attrs = merge_attrs(
BASE_TEST_ATTRIBUTES,
BASE_BINARY_ATTRS,
{
"_lcov_merger": attr.label(
cfg = "exec",
default = configuration_field(
fragment = "coverage",
name = "output_generator",
),
),
"_collect_cc_coverage": attr.label(
cfg = "exec",
allow_single_file = True,
default = "@bazel_tools//tools/test:collect_cc_coverage",
),
},
override_attrs = {
"use_testrunner": attr.bool(
default = True,
doc = semantics.DOCS.for_attribute("use_testrunner") + """
java_test rules,
and not use it for java_binary rules. It is unlikely
you will want to do this. One use is for AllTest
rules that are invoked by another rule (to set up a database
before running the tests, for example). The AllTest
rule must be declared as a java_binary, but should
still use the test runner as its main entry point.
The name of a test runner class can be overridden with main_class attribute.
""",
),
"stamp": attr.int(
default = 0,
values = [-1, 0, 1],
doc = """
Whether to encode build information into the binary. Possible values:
stamp = 1: Always stamp the build information into the binary, even in
--nostamp builds. This
setting should be avoided, since it potentially kills remote caching for the
binary and any downstream actions that depend on it.
stamp = 0: Always replace build information by constant values. This
gives good build result caching.
stamp = -1: Embedding of build information is controlled by the
--[no]stamp flag.
Stamped binaries are not rebuilt unless their dependencies change.
""", ), }, remove_attrs = ["deploy_env"], ), test = True, initializer = _java_test_initializer, )