Skip to content

Commit

Permalink
tests: make precompile tests pass when other toolchains are defined (#…
Browse files Browse the repository at this point in the history
…2213)

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.

Basically, the tests rely on a fake Python version 4.5 toolchain for the
precompiler being
defined. They pass today because they fallback to a real toolchain (as
setup by
MODULE.bazel), which doesn't have any version constraints on it. In
comparison, within
Google, there is no "default" toolchain, so the tests fail to find what
they need.

To fix, explicitly define a fake precompiler toolchain and tell the test
to use it.

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.
  • Loading branch information
rickeylev authored Sep 10, 2024
1 parent a6cd158 commit 7d42a93
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 47 deletions.
37 changes: 14 additions & 23 deletions tests/base_rules/precompile/precompile_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []

Expand All @@ -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):
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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",
},
)

Expand Down Expand Up @@ -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",
},
)

Expand Down Expand Up @@ -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)
Expand Down
23 changes: 0 additions & 23 deletions tests/support/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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",
)
59 changes: 59 additions & 0 deletions tests/support/py_toolchains/BUILD
Original file line number Diff line number Diff line change
@@ -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",
)
2 changes: 1 addition & 1 deletion tests/support/support.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down

0 comments on commit 7d42a93

Please sign in to comment.