From 90e7f4f7cedca037798ea7579728043154f3e538 Mon Sep 17 00:00:00 2001 From: Richard Levasseur Date: Tue, 10 Sep 2024 13:56:41 -0700 Subject: [PATCH] tests: make precompile tests pass when other toolchains are defined This makes the precompile_tests pass when the environment defines custom toolchains that don't match what rules_python defines in its dev environment. This keeps the tests independent of whatever the user's environment is. Along the way: * Also force `--allow_unresolved_symlinks=true` in the tests. This flag isn't enabled in certain environments, but is implicitly relied upon by the `current_interpreter_executable` rule when a platform runtime is used (as they are in the tests). * Move the Python testing toolchains to support/py_toolchains, to match where the cc testing toolchains were moved. --- .../precompile/precompile_tests.bzl | 37 +++++------- tests/support/BUILD.bazel | 23 -------- tests/support/py_toolchains/BUILD | 59 +++++++++++++++++++ tests/support/support.bzl | 2 +- 4 files changed, 74 insertions(+), 47 deletions(-) create mode 100644 tests/support/py_toolchains/BUILD diff --git a/tests/base_rules/precompile/precompile_tests.bzl b/tests/base_rules/precompile/precompile_tests.bzl index e2ffdc8bb4..4c0f936ac5 100644 --- a/tests/base_rules/precompile/precompile_tests.bzl +++ b/tests/base_rules/precompile/precompile_tests.bzl @@ -28,13 +28,19 @@ load( "//tests/support:support.bzl", "CC_TOOLCHAIN", "EXEC_TOOLS_TOOLCHAIN", - "PLATFORM_TOOLCHAIN", "PRECOMPILE", "PRECOMPILE_ADD_TO_RUNFILES", "PRECOMPILE_SOURCE_RETENTION", + "PY_TOOLCHAINS", ) -_TEST_TOOLCHAINS = [PLATFORM_TOOLCHAIN, CC_TOOLCHAIN] +_COMMON_CONFIG_SETTINGS = { + # This isn't enabled in all environments the tests run in, so disable + # it for conformity. + "//command_line_option:allow_unresolved_symlinks": True, + "//command_line_option:extra_toolchains": [PY_TOOLCHAINS, CC_TOOLCHAIN], + EXEC_TOOLS_TOOLCHAIN: "enabled", +} _tests = [] @@ -60,10 +66,7 @@ def _test_precompile_enabled_setup(name, py_rule, **kwargs): name = name, impl = _test_precompile_enabled_impl, target = name + "_subject", - config_settings = { - "//command_line_option:extra_toolchains": _TEST_TOOLCHAINS, - EXEC_TOOLS_TOOLCHAIN: "enabled", - }, + config_settings = _COMMON_CONFIG_SETTINGS, ) def _test_precompile_enabled_impl(env, target): @@ -118,10 +121,8 @@ def _test_pyc_only(name): analysis_test( name = name, impl = _test_pyc_only_impl, - config_settings = { - "//command_line_option:extra_toolchains": _TEST_TOOLCHAINS, + config_settings = _COMMON_CONFIG_SETTINGS | { ##PRECOMPILE_SOURCE_RETENTION: "omit_source", - EXEC_TOOLS_TOOLCHAIN: "enabled", PRECOMPILE: "enabled", }, target = name + "_subject", @@ -163,10 +164,7 @@ def _test_precompile_if_generated(name): name = name, impl = _test_precompile_if_generated_impl, target = name + "_subject", - config_settings = { - "//command_line_option:extra_toolchains": _TEST_TOOLCHAINS, - EXEC_TOOLS_TOOLCHAIN: "enabled", - }, + config_settings = _COMMON_CONFIG_SETTINGS, ) _tests.append(_test_precompile_if_generated) @@ -205,10 +203,8 @@ def _test_omit_source_if_generated_source(name): name = name, impl = _test_omit_source_if_generated_source_impl, target = name + "_subject", - config_settings = { - "//command_line_option:extra_toolchains": _TEST_TOOLCHAINS, + config_settings = _COMMON_CONFIG_SETTINGS | { PRECOMPILE_SOURCE_RETENTION: "omit_if_generated_source", - EXEC_TOOLS_TOOLCHAIN: "enabled", }, ) @@ -254,11 +250,9 @@ def _test_precompile_add_to_runfiles_decided_elsewhere(name): "binary": name + "_binary", "library": name + "_lib", }, - config_settings = { - "//command_line_option:extra_toolchains": _TEST_TOOLCHAINS, + config_settings = _COMMON_CONFIG_SETTINGS | { PRECOMPILE_ADD_TO_RUNFILES: "decided_elsewhere", PRECOMPILE: "enabled", - EXEC_TOOLS_TOOLCHAIN: "enabled", }, ) @@ -293,10 +287,7 @@ def _test_precompiler_action(name): name = name, impl = _test_precompiler_action_impl, target = name + "_subject", - config_settings = { - "//command_line_option:extra_toolchains": _TEST_TOOLCHAINS, - EXEC_TOOLS_TOOLCHAIN: "enabled", - }, + config_settings = _COMMON_CONFIG_SETTINGS, ) _tests.append(_test_precompiler_action) diff --git a/tests/support/BUILD.bazel b/tests/support/BUILD.bazel index 58c74d6d48..9fb5cd0760 100644 --- a/tests/support/BUILD.bazel +++ b/tests/support/BUILD.bazel @@ -18,8 +18,6 @@ # to force them to resolve in the proper context. # ==================== -load("//python:py_runtime.bzl", "py_runtime") -load("//python:py_runtime_pair.bzl", "py_runtime_pair") load(":sh_py_run_test.bzl", "current_build_settings") package( @@ -89,27 +87,6 @@ platform( ], ) -py_runtime( - name = "platform_runtime", - implementation_name = "fakepy", - interpreter_path = "/fake/python3.9", - interpreter_version_info = { - "major": "4", - "minor": "5", - }, -) - -py_runtime_pair( - name = "platform_runtime_pair", - py3_runtime = ":platform_runtime", -) - -toolchain( - name = "platform_toolchain", - toolchain = ":platform_runtime_pair", - toolchain_type = "//python:toolchain_type", -) - current_build_settings( name = "current_build_settings", ) diff --git a/tests/support/py_toolchains/BUILD b/tests/support/py_toolchains/BUILD new file mode 100644 index 0000000000..185c7ae2da --- /dev/null +++ b/tests/support/py_toolchains/BUILD @@ -0,0 +1,59 @@ +# Copyright 2024 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# ==================== +# NOTE: tests/support/support.bzl has constants to easily refer to +# these toolchains. +# ==================== + +load("//python:py_runtime.bzl", "py_runtime") +load("//python:py_runtime_pair.bzl", "py_runtime_pair") +load("//python/private:py_exec_tools_toolchain.bzl", "py_exec_tools_toolchain") # buildifier: disable=bzl-visibility + +# NOTE: A platform runtime is used because it doesn't include any files. This +# makes it easier for analysis tests to verify content. +py_runtime( + name = "platform_runtime", + implementation_name = "fakepy", + interpreter_path = "/fake/python3.9", + interpreter_version_info = { + "major": "4", + "minor": "5", + }, +) + +py_runtime_pair( + name = "platform_runtime_pair", + py3_runtime = ":platform_runtime", +) + +toolchain( + name = "platform_toolchain", + toolchain = ":platform_runtime_pair", + toolchain_type = "//python:toolchain_type", +) + +toolchain( + name = "exec_toolchain", + toolchain = ":exec_toolchain_impl", + toolchain_type = "//python:exec_tools_toolchain_type", +) + +# An exec toolchain is explicitly defined so that the tests pass when run +# in environments that aren't using the toolchains generated by the +# hermetic runtimes. +py_exec_tools_toolchain( + name = "exec_toolchain_impl", + precompiler = "//tools/precompiler:precompiler", +) diff --git a/tests/support/support.bzl b/tests/support/support.bzl index b7d8fa9fa6..150ca7f4a4 100644 --- a/tests/support/support.bzl +++ b/tests/support/support.bzl @@ -26,7 +26,7 @@ LINUX_X86_64 = Label("//tests/support:linux_x86_64") WINDOWS = Label("//tests/support:windows") WINDOWS_X86_64 = Label("//tests/support:windows_x86_64") -PLATFORM_TOOLCHAIN = str(Label("//tests/support:platform_toolchain")) +PY_TOOLCHAINS = str(Label("//tests/support/py_toolchains:all")) CC_TOOLCHAIN = str(Label("//tests/support/cc_toolchains:all")) CROSSTOOL_TOP = Label("//tests/support/cc_toolchains:cc_toolchain_suite")