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 rt_util.helper_target( 80 _provides_builtin_py_runtime_info, 81 name = name + "_runtime", 82 ) 83 rt_util.helper_target( 84 py_runtime_pair, 85 name = name + "_subject", 86 py3_runtime = name + "_runtime", 87 ) 88 analysis_test( 89 name = name, 90 target = name + "_subject", 91 impl = _test_builtin_py_info_accepted_impl, 92 ) 93 94def _test_builtin_py_info_accepted_impl(env, target): 95 toolchain = env.expect.that_target(target).provider( 96 platform_common.ToolchainInfo, 97 factory = _toolchain_factory, 98 ) 99 toolchain.py3_runtime().python_version().equals("PY3") 100 toolchain.py3_runtime().interpreter_path().equals("builtin") 101 102_tests.append(_test_builtin_py_info_accepted) 103 104def _test_py_runtime_pair_and_binary(name): 105 rt_util.helper_target( 106 py_runtime, 107 name = name + "_runtime", 108 interpreter_path = "/fake_interpreter", 109 python_version = "PY3", 110 ) 111 rt_util.helper_target( 112 py_runtime_pair, 113 name = name + "_pair", 114 py3_runtime = name + "_runtime", 115 ) 116 native.toolchain( 117 name = name + "_toolchain", 118 toolchain = name + "_pair", 119 toolchain_type = "//python:toolchain_type", 120 ) 121 rt_util.helper_target( 122 py_binary, 123 name = name + "_subject", 124 srcs = [name + "_subject.py"], 125 ) 126 analysis_test( 127 name = name, 128 target = name + "_subject", 129 impl = _test_py_runtime_pair_and_binary_impl, 130 config_settings = { 131 "//command_line_option:extra_toolchains": [ 132 "//tests/py_runtime_pair:{}_toolchain".format(name), 133 CC_TOOLCHAIN, 134 ], 135 }, 136 ) 137 138def _test_py_runtime_pair_and_binary_impl(env, target): 139 # Building indicates success, so nothing to assert 140 _ = env, target # @unused 141 142_tests.append(_test_py_runtime_pair_and_binary) 143 144def py_runtime_pair_test_suite(name): 145 test_suite( 146 name = name, 147 tests = _tests, 148 ) 149