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 "//tools/build_defs/python/tests:py_executable_base_tests.bzl", 21 "create_executable_tests", 22) 23load("//tools/build_defs/python/tests:util.bzl", pt_util = "util") 24 25# Explicit Label() calls are required so that it resolves in @rules_python context instead of 26# @rules_testing context. 27_FAKE_CC_TOOLCHAIN = Label("//tests/cc:cc_toolchain_suite") 28_FAKE_CC_TOOLCHAINS = [str(Label("//tests/cc:all"))] 29_PLATFORM_MAC = Label("//tools/build_defs/python/tests:mac") 30_PLATFORM_LINUX = Label("//tools/build_defs/python/tests:linux") 31 32_tests = [] 33 34def _test_mac_requires_darwin_for_execution(name, config): 35 # Bazel 5.4 has a bug where every access of testing.ExecutionInfo is 36 # a different object that isn't equal to any other, which prevents 37 # rules_testing from detecting it properly and fails with an error. 38 # This is fixed in Bazel 6+. 39 if not pt_util.is_bazel_6_or_higher(): 40 rt_util.skip_test(name = name) 41 return 42 43 rt_util.helper_target( 44 config.rule, 45 name = name + "_subject", 46 srcs = [name + "_subject.py"], 47 ) 48 analysis_test( 49 name = name, 50 impl = _test_mac_requires_darwin_for_execution_impl, 51 target = name + "_subject", 52 config_settings = { 53 "//command_line_option:cpu": "darwin_x86_64", 54 "//command_line_option:crosstool_top": _FAKE_CC_TOOLCHAIN, 55 "//command_line_option:extra_toolchains": _FAKE_CC_TOOLCHAINS, 56 "//command_line_option:platforms": [_PLATFORM_MAC], 57 }, 58 ) 59 60def _test_mac_requires_darwin_for_execution_impl(env, target): 61 env.expect.that_target(target).provider( 62 testing.ExecutionInfo, 63 ).requirements().keys().contains("requires-darwin") 64 65_tests.append(_test_mac_requires_darwin_for_execution) 66 67def _test_non_mac_doesnt_require_darwin_for_execution(name, config): 68 # Bazel 5.4 has a bug where every access of testing.ExecutionInfo is 69 # a different object that isn't equal to any other, which prevents 70 # rules_testing from detecting it properly and fails with an error. 71 # This is fixed in Bazel 6+. 72 if not pt_util.is_bazel_6_or_higher(): 73 rt_util.skip_test(name = name) 74 return 75 rt_util.helper_target( 76 config.rule, 77 name = name + "_subject", 78 srcs = [name + "_subject.py"], 79 ) 80 analysis_test( 81 name = name, 82 impl = _test_non_mac_doesnt_require_darwin_for_execution_impl, 83 target = name + "_subject", 84 config_settings = { 85 "//command_line_option:cpu": "k8", 86 "//command_line_option:crosstool_top": _FAKE_CC_TOOLCHAIN, 87 "//command_line_option:extra_toolchains": _FAKE_CC_TOOLCHAINS, 88 "//command_line_option:platforms": [_PLATFORM_LINUX], 89 }, 90 ) 91 92def _test_non_mac_doesnt_require_darwin_for_execution_impl(env, target): 93 # Non-mac builds don't have the provider at all. 94 if testing.ExecutionInfo not in target: 95 return 96 env.expect.that_target(target).provider( 97 testing.ExecutionInfo, 98 ).requirements().keys().not_contains("requires-darwin") 99 100_tests.append(_test_non_mac_doesnt_require_darwin_for_execution) 101 102def py_test_test_suite(name): 103 config = struct(rule = py_test) 104 native.test_suite( 105 name = name, 106 tests = pt_util.create_tests(_tests, config = config) + create_executable_tests(config), 107 ) 108