|
| 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 | + |
| 15 | +"" |
| 16 | + |
| 17 | +load("@pythons_hub//:versions.bzl", "DEFAULT_PYTHON_VERSION", "MINOR_MAPPING") |
| 18 | +load("@rules_testing//lib:analysis_test.bzl", "analysis_test") |
| 19 | +load("@rules_testing//lib:test_suite.bzl", "test_suite") |
| 20 | +load("@rules_testing//lib:util.bzl", rt_util = "util") |
| 21 | +load("//python:versions.bzl", "TOOL_VERSIONS") |
| 22 | +load("//python/private:bzlmod_enabled.bzl", "BZLMOD_ENABLED") # buildifier: disable=bzl-visibility |
| 23 | +load("//python/private:full_version.bzl", "full_version") # buildifier: disable=bzl-visibility |
| 24 | +load("//python/private:toolchain_types.bzl", "EXEC_TOOLS_TOOLCHAIN_TYPE") # buildifier: disable=bzl-visibility |
| 25 | +load("//tests/support:support.bzl", "PYTHON_VERSION") |
| 26 | + |
| 27 | +_analysis_tests = [] |
| 28 | + |
| 29 | +def _transition_impl(input_settings, attr): |
| 30 | + """Transition based on python_version flag. |
| 31 | +
|
| 32 | + This is a simple transition impl that a user of rules_python may implement |
| 33 | + for their own rule. |
| 34 | + """ |
| 35 | + settings = { |
| 36 | + PYTHON_VERSION: input_settings[PYTHON_VERSION], |
| 37 | + } |
| 38 | + if attr.python_version: |
| 39 | + settings[PYTHON_VERSION] = attr.python_version |
| 40 | + return settings |
| 41 | + |
| 42 | +_python_version_transition = transition( |
| 43 | + implementation = _transition_impl, |
| 44 | + inputs = [PYTHON_VERSION], |
| 45 | + outputs = [PYTHON_VERSION], |
| 46 | +) |
| 47 | + |
| 48 | +TestInfo = provider( |
| 49 | + doc = "A simple test provider to forward the values for the assertion.", |
| 50 | + fields = {"got": "", "want": ""}, |
| 51 | +) |
| 52 | + |
| 53 | +def _impl(ctx): |
| 54 | + if ctx.attr.skip: |
| 55 | + return [TestInfo(got = "", want = "")] |
| 56 | + |
| 57 | + exec_tools = ctx.toolchains[EXEC_TOOLS_TOOLCHAIN_TYPE].exec_tools |
| 58 | + got_version = exec_tools.exec_interpreter[platform_common.ToolchainInfo].py3_runtime.interpreter_version_info |
| 59 | + |
| 60 | + return [ |
| 61 | + TestInfo( |
| 62 | + got = "{}.{}.{}".format( |
| 63 | + got_version.major, |
| 64 | + got_version.minor, |
| 65 | + got_version.micro, |
| 66 | + ), |
| 67 | + want = ctx.attr.want_version, |
| 68 | + ), |
| 69 | + ] |
| 70 | + |
| 71 | +_simple_transition = rule( |
| 72 | + implementation = _impl, |
| 73 | + attrs = { |
| 74 | + "python_version": attr.string( |
| 75 | + doc = "The input python version which we transition on.", |
| 76 | + ), |
| 77 | + "skip": attr.bool( |
| 78 | + doc = "Whether to skip the test", |
| 79 | + ), |
| 80 | + "want_version": attr.string( |
| 81 | + doc = "The python version that we actually expect to receive.", |
| 82 | + ), |
| 83 | + "_allowlist_function_transition": attr.label( |
| 84 | + default = "@bazel_tools//tools/allowlists/function_transition_allowlist", |
| 85 | + ), |
| 86 | + }, |
| 87 | + toolchains = [ |
| 88 | + config_common.toolchain_type( |
| 89 | + EXEC_TOOLS_TOOLCHAIN_TYPE, |
| 90 | + mandatory = False, |
| 91 | + ), |
| 92 | + ], |
| 93 | + cfg = _python_version_transition, |
| 94 | +) |
| 95 | + |
| 96 | +def _test_transitions(*, name, tests, skip = False): |
| 97 | + """A reusable rule so that we can split the tests.""" |
| 98 | + targets = {} |
| 99 | + for test_name, (input_version, want_version) in tests.items(): |
| 100 | + target_name = "{}_{}".format(name, test_name) |
| 101 | + targets["python_" + test_name] = target_name |
| 102 | + rt_util.helper_target( |
| 103 | + _simple_transition, |
| 104 | + name = target_name, |
| 105 | + python_version = input_version, |
| 106 | + want_version = want_version, |
| 107 | + skip = skip, |
| 108 | + ) |
| 109 | + |
| 110 | + analysis_test( |
| 111 | + name = name, |
| 112 | + impl = _test_transition_impl, |
| 113 | + targets = targets, |
| 114 | + ) |
| 115 | + |
| 116 | +def _test_transition_impl(env, targets): |
| 117 | + # Check that the forwarded version from the PyRuntimeInfo is correct |
| 118 | + for target in dir(targets): |
| 119 | + if not target.startswith("python"): |
| 120 | + # Skip other attributes that might be not the ones we set (e.g. to_json, to_proto). |
| 121 | + continue |
| 122 | + |
| 123 | + test_info = env.expect.that_target(getattr(targets, target)).provider( |
| 124 | + TestInfo, |
| 125 | + factory = lambda v, meta: v, |
| 126 | + ) |
| 127 | + env.expect.that_str(test_info.got).equals(test_info.want) |
| 128 | + |
| 129 | +def _test_full_version(name): |
| 130 | + """Check that python_version transitions work. |
| 131 | +
|
| 132 | + Expectation is to get the same full version that we input. |
| 133 | + """ |
| 134 | + _test_transitions( |
| 135 | + name = name, |
| 136 | + tests = { |
| 137 | + v.replace(".", "_"): (v, v) |
| 138 | + for v in TOOL_VERSIONS |
| 139 | + }, |
| 140 | + ) |
| 141 | + |
| 142 | +_analysis_tests.append(_test_full_version) |
| 143 | + |
| 144 | +def _test_minor_versions(name): |
| 145 | + """Ensure that MINOR_MAPPING versions are correctly selected.""" |
| 146 | + _test_transitions( |
| 147 | + name = name, |
| 148 | + skip = not BZLMOD_ENABLED, |
| 149 | + tests = { |
| 150 | + minor.replace(".", "_"): (minor, full) |
| 151 | + for minor, full in MINOR_MAPPING.items() |
| 152 | + }, |
| 153 | + ) |
| 154 | + |
| 155 | +_analysis_tests.append(_test_minor_versions) |
| 156 | + |
| 157 | +def _test_default(name): |
| 158 | + """Check the default version. |
| 159 | +
|
| 160 | + Lastly, if we don't provide any version to the transition, we should |
| 161 | + get the default version |
| 162 | + """ |
| 163 | + default_version = full_version( |
| 164 | + version = DEFAULT_PYTHON_VERSION, |
| 165 | + minor_mapping = MINOR_MAPPING, |
| 166 | + ) if DEFAULT_PYTHON_VERSION else "" |
| 167 | + |
| 168 | + _test_transitions( |
| 169 | + name = name, |
| 170 | + skip = not BZLMOD_ENABLED, |
| 171 | + tests = { |
| 172 | + "default": (None, default_version), |
| 173 | + }, |
| 174 | + ) |
| 175 | + |
| 176 | +_analysis_tests.append(_test_default) |
| 177 | + |
| 178 | +def transitions_test_suite(name): |
| 179 | + test_suite( |
| 180 | + name = name, |
| 181 | + tests = _analysis_tests, |
| 182 | + ) |
0 commit comments