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"""Starlark tests for py_runtime_pair rule.""" 15 16load("@rules_testing//lib:analysis_test.bzl", "analysis_test") 17load("@rules_testing//lib:test_suite.bzl", "test_suite") 18load("@rules_testing//lib:truth.bzl", "matching", "subjects") 19load("@rules_testing//lib:util.bzl", rt_util = "util") 20load("//python:py_binary.bzl", "py_binary") 21load("//python:py_runtime.bzl", "py_runtime") 22load("//python:py_runtime_pair.bzl", "py_runtime_pair") 23load("//python/private:reexports.bzl", "BuiltinPyRuntimeInfo") # buildifier: disable=bzl-visibility 24load("//tests/support:py_runtime_info_subject.bzl", "py_runtime_info_subject") 25load("//tests/support:support.bzl", "CC_TOOLCHAIN") 26 27def _toolchain_factory(value, meta): 28 return subjects.struct( 29 value, 30 meta = meta, 31 attrs = { 32 "py3_runtime": py_runtime_info_subject, 33 }, 34 ) 35 36def _provides_builtin_py_runtime_info_impl(ctx): # @unused 37 return [BuiltinPyRuntimeInfo( 38 python_version = "PY3", 39 interpreter_path = "builtin", 40 )] 41 42_provides_builtin_py_runtime_info = rule( 43 implementation = _provides_builtin_py_runtime_info_impl, 44) 45 46_tests = [] 47 48def _test_basic(name): 49 rt_util.helper_target( 50 py_runtime, 51 name = name + "_runtime", 52 interpreter = "fake_interpreter", 53 python_version = "PY3", 54 files = ["file1.txt"], 55 ) 56 rt_util.helper_target( 57 py_runtime_pair, 58 name = name + "_subject", 59 py3_runtime = name + "_runtime", 60 ) 61 analysis_test( 62 name = name, 63 target = name + "_subject", 64 impl = _test_basic_impl, 65 ) 66 67def _test_basic_impl(env, target): 68 toolchain = env.expect.that_target(target).provider( 69 platform_common.ToolchainInfo, 70 factory = _toolchain_factory, 71 ) 72 toolchain.py3_runtime().python_version().equals("PY3") 73 toolchain.py3_runtime().files().contains_predicate(matching.file_basename_equals("file1.txt")) 74 toolchain.py3_runtime().interpreter().path().contains("fake_interpreter") 75 76_tests.append(_test_basic) 77 78def _test_builtin_py_info_accepted(name): 79 if not BuiltinPyRuntimeInfo: 80 rt_util.skip_test(name = name) 81 return 82 rt_util.helper_target( 83 _provides_builtin_py_runtime_info, 84 name = name + "_runtime", 85 ) 86 rt_util.helper_target( 87 py_runtime_pair, 88 name = name + "_subject", 89 py3_runtime = name + "_runtime", 90 ) 91 analysis_test( 92 name = name, 93 target = name + "_subject", 94 impl = _test_builtin_py_info_accepted_impl, 95 ) 96 97def _test_builtin_py_info_accepted_impl(env, target): 98 toolchain = env.expect.that_target(target).provider( 99 platform_common.ToolchainInfo, 100 factory = _toolchain_factory, 101 ) 102 toolchain.py3_runtime().python_version().equals("PY3") 103 toolchain.py3_runtime().interpreter_path().equals("builtin") 104 105_tests.append(_test_builtin_py_info_accepted) 106 107def _test_py_runtime_pair_and_binary(name): 108 rt_util.helper_target( 109 py_runtime, 110 name = name + "_runtime", 111 interpreter_path = "/fake_interpreter", 112 python_version = "PY3", 113 ) 114 rt_util.helper_target( 115 py_runtime_pair, 116 name = name + "_pair", 117 py3_runtime = name + "_runtime", 118 ) 119 native.toolchain( 120 name = name + "_toolchain", 121 toolchain = name + "_pair", 122 toolchain_type = "//python:toolchain_type", 123 ) 124 rt_util.helper_target( 125 py_binary, 126 name = name + "_subject", 127 srcs = [name + "_subject.py"], 128 ) 129 analysis_test( 130 name = name, 131 target = name + "_subject", 132 impl = _test_py_runtime_pair_and_binary_impl, 133 config_settings = { 134 "//command_line_option:extra_toolchains": [ 135 "//tests/py_runtime_pair:{}_toolchain".format(name), 136 CC_TOOLCHAIN, 137 ], 138 }, 139 ) 140 141def _test_py_runtime_pair_and_binary_impl(env, target): 142 # Building indicates success, so nothing to assert 143 _ = env, target # @unused 144 145_tests.append(_test_py_runtime_pair_and_binary) 146 147def py_runtime_pair_test_suite(name): 148 test_suite( 149 name = name, 150 tests = _tests, 151 ) 152