xref: /aosp_15_r20/external/bazelbuild-rules_python/tests/base_rules/py_test/py_test_tests.bzl (revision 60517a1edbc8ecf509223e9af94a7adec7d736b8)
1# Copyright 2023 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"""Tests for py_test."""
15
16load("@rules_testing//lib:analysis_test.bzl", "analysis_test")
17load("@rules_testing//lib:util.bzl", rt_util = "util")
18load("//python:defs.bzl", "py_test")
19load(
20    "//tests/base_rules:py_executable_base_tests.bzl",
21    "create_executable_tests",
22)
23load("//tests/base_rules:util.bzl", pt_util = "util")
24load("//tests/support:support.bzl", "CC_TOOLCHAIN", "CROSSTOOL_TOP", "LINUX_X86_64", "MAC_X86_64")
25
26# The Windows CI currently runs as root, which breaks when
27# the analysis tests try to install (but not use, because
28# these are analysis tests) a runtime for another platform.
29# This is because the toolchain install has an assert to
30# verify the runtime install is read-only, which it can't
31# be when running as root.
32_SKIP_WINDOWS = {
33    "target_compatible_with": select({
34        "@platforms//os:windows": ["@platforms//:incompatible"],
35        "//conditions:default": [],
36    }),
37}
38
39_tests = []
40
41def _test_mac_requires_darwin_for_execution(name, config):
42    # Bazel 5.4 has a bug where every access of testing.ExecutionInfo is
43    # a different object that isn't equal to any other, which prevents
44    # rules_testing from detecting it properly and fails with an error.
45    # This is fixed in Bazel 6+.
46    if not pt_util.is_bazel_6_or_higher():
47        rt_util.skip_test(name = name)
48        return
49
50    rt_util.helper_target(
51        config.rule,
52        name = name + "_subject",
53        srcs = [name + "_subject.py"],
54    )
55    analysis_test(
56        name = name,
57        impl = _test_mac_requires_darwin_for_execution_impl,
58        target = name + "_subject",
59        config_settings = {
60            "//command_line_option:cpu": "darwin_x86_64",
61            "//command_line_option:crosstool_top": CROSSTOOL_TOP,
62            "//command_line_option:extra_toolchains": CC_TOOLCHAIN,
63            "//command_line_option:platforms": [MAC_X86_64],
64        },
65        attr_values = _SKIP_WINDOWS,
66    )
67
68def _test_mac_requires_darwin_for_execution_impl(env, target):
69    env.expect.that_target(target).provider(
70        testing.ExecutionInfo,
71    ).requirements().keys().contains("requires-darwin")
72
73_tests.append(_test_mac_requires_darwin_for_execution)
74
75def _test_non_mac_doesnt_require_darwin_for_execution(name, config):
76    # Bazel 5.4 has a bug where every access of testing.ExecutionInfo is
77    # a different object that isn't equal to any other, which prevents
78    # rules_testing from detecting it properly and fails with an error.
79    # This is fixed in Bazel 6+.
80    if not pt_util.is_bazel_6_or_higher():
81        rt_util.skip_test(name = name)
82        return
83    rt_util.helper_target(
84        config.rule,
85        name = name + "_subject",
86        srcs = [name + "_subject.py"],
87    )
88    analysis_test(
89        name = name,
90        impl = _test_non_mac_doesnt_require_darwin_for_execution_impl,
91        target = name + "_subject",
92        config_settings = {
93            "//command_line_option:cpu": "k8",
94            "//command_line_option:crosstool_top": CROSSTOOL_TOP,
95            "//command_line_option:extra_toolchains": CC_TOOLCHAIN,
96            "//command_line_option:platforms": [LINUX_X86_64],
97        },
98        attr_values = _SKIP_WINDOWS,
99    )
100
101def _test_non_mac_doesnt_require_darwin_for_execution_impl(env, target):
102    # Non-mac builds don't have the provider at all.
103    if testing.ExecutionInfo not in target:
104        return
105    env.expect.that_target(target).provider(
106        testing.ExecutionInfo,
107    ).requirements().keys().not_contains("requires-darwin")
108
109_tests.append(_test_non_mac_doesnt_require_darwin_for_execution)
110
111def py_test_test_suite(name):
112    config = struct(rule = py_test)
113    native.test_suite(
114        name = name,
115        tests = pt_util.create_tests(_tests, config = config) + create_executable_tests(config),
116    )
117