xref: /aosp_15_r20/external/bazelbuild-rules_python/python/private/common/py_test_rule_bazel.bzl (revision 60517a1edbc8ecf509223e9af94a7adec7d736b8)
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"""Rule implementation of py_test for Bazel."""
15
16load("@bazel_skylib//lib:dicts.bzl", "dicts")
17load(":attributes.bzl", "AGNOSTIC_TEST_ATTRS")
18load(":common.bzl", "maybe_add_test_execution_info")
19load(
20    ":py_executable_bazel.bzl",
21    "create_executable_rule",
22    "py_executable_bazel_impl",
23)
24
25_BAZEL_PY_TEST_ATTRS = {
26    # This *might* be a magic attribute to help C++ coverage work. There's no
27    # docs about this; see TestActionBuilder.java
28    "_collect_cc_coverage": attr.label(
29        default = "@bazel_tools//tools/test:collect_cc_coverage",
30        executable = True,
31        cfg = "exec",
32    ),
33    # This *might* be a magic attribute to help C++ coverage work. There's no
34    # docs about this; see TestActionBuilder.java
35    "_lcov_merger": attr.label(
36        default = configuration_field(fragment = "coverage", name = "output_generator"),
37        cfg = "exec",
38        executable = True,
39    ),
40}
41
42def _py_test_impl(ctx):
43    providers = py_executable_bazel_impl(
44        ctx = ctx,
45        is_test = True,
46        inherited_environment = ctx.attr.env_inherit,
47    )
48    maybe_add_test_execution_info(providers, ctx)
49    return providers
50
51py_test = create_executable_rule(
52    implementation = _py_test_impl,
53    attrs = dicts.add(AGNOSTIC_TEST_ATTRS, _BAZEL_PY_TEST_ATTRS),
54    test = True,
55)
56