Skip to content

Commit 7d42a93

Browse files
authored
tests: make precompile tests pass when other toolchains are defined (bazel-contrib#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.
1 parent a6cd158 commit 7d42a93

4 files changed

Lines changed: 74 additions & 47 deletions

File tree

tests/base_rules/precompile/precompile_tests.bzl

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,19 @@ load(
2828
"//tests/support:support.bzl",
2929
"CC_TOOLCHAIN",
3030
"EXEC_TOOLS_TOOLCHAIN",
31-
"PLATFORM_TOOLCHAIN",
3231
"PRECOMPILE",
3332
"PRECOMPILE_ADD_TO_RUNFILES",
3433
"PRECOMPILE_SOURCE_RETENTION",
34+
"PY_TOOLCHAINS",
3535
)
3636

37-
_TEST_TOOLCHAINS = [PLATFORM_TOOLCHAIN, CC_TOOLCHAIN]
37+
_COMMON_CONFIG_SETTINGS = {
38+
# This isn't enabled in all environments the tests run in, so disable
39+
# it for conformity.
40+
"//command_line_option:allow_unresolved_symlinks": True,
41+
"//command_line_option:extra_toolchains": [PY_TOOLCHAINS, CC_TOOLCHAIN],
42+
EXEC_TOOLS_TOOLCHAIN: "enabled",
43+
}
3844

3945
_tests = []
4046

@@ -60,10 +66,7 @@ def _test_precompile_enabled_setup(name, py_rule, **kwargs):
6066
name = name,
6167
impl = _test_precompile_enabled_impl,
6268
target = name + "_subject",
63-
config_settings = {
64-
"//command_line_option:extra_toolchains": _TEST_TOOLCHAINS,
65-
EXEC_TOOLS_TOOLCHAIN: "enabled",
66-
},
69+
config_settings = _COMMON_CONFIG_SETTINGS,
6770
)
6871

6972
def _test_precompile_enabled_impl(env, target):
@@ -118,10 +121,8 @@ def _test_pyc_only(name):
118121
analysis_test(
119122
name = name,
120123
impl = _test_pyc_only_impl,
121-
config_settings = {
122-
"//command_line_option:extra_toolchains": _TEST_TOOLCHAINS,
124+
config_settings = _COMMON_CONFIG_SETTINGS | {
123125
##PRECOMPILE_SOURCE_RETENTION: "omit_source",
124-
EXEC_TOOLS_TOOLCHAIN: "enabled",
125126
PRECOMPILE: "enabled",
126127
},
127128
target = name + "_subject",
@@ -163,10 +164,7 @@ def _test_precompile_if_generated(name):
163164
name = name,
164165
impl = _test_precompile_if_generated_impl,
165166
target = name + "_subject",
166-
config_settings = {
167-
"//command_line_option:extra_toolchains": _TEST_TOOLCHAINS,
168-
EXEC_TOOLS_TOOLCHAIN: "enabled",
169-
},
167+
config_settings = _COMMON_CONFIG_SETTINGS,
170168
)
171169

172170
_tests.append(_test_precompile_if_generated)
@@ -205,10 +203,8 @@ def _test_omit_source_if_generated_source(name):
205203
name = name,
206204
impl = _test_omit_source_if_generated_source_impl,
207205
target = name + "_subject",
208-
config_settings = {
209-
"//command_line_option:extra_toolchains": _TEST_TOOLCHAINS,
206+
config_settings = _COMMON_CONFIG_SETTINGS | {
210207
PRECOMPILE_SOURCE_RETENTION: "omit_if_generated_source",
211-
EXEC_TOOLS_TOOLCHAIN: "enabled",
212208
},
213209
)
214210

@@ -254,11 +250,9 @@ def _test_precompile_add_to_runfiles_decided_elsewhere(name):
254250
"binary": name + "_binary",
255251
"library": name + "_lib",
256252
},
257-
config_settings = {
258-
"//command_line_option:extra_toolchains": _TEST_TOOLCHAINS,
253+
config_settings = _COMMON_CONFIG_SETTINGS | {
259254
PRECOMPILE_ADD_TO_RUNFILES: "decided_elsewhere",
260255
PRECOMPILE: "enabled",
261-
EXEC_TOOLS_TOOLCHAIN: "enabled",
262256
},
263257
)
264258

@@ -293,10 +287,7 @@ def _test_precompiler_action(name):
293287
name = name,
294288
impl = _test_precompiler_action_impl,
295289
target = name + "_subject",
296-
config_settings = {
297-
"//command_line_option:extra_toolchains": _TEST_TOOLCHAINS,
298-
EXEC_TOOLS_TOOLCHAIN: "enabled",
299-
},
290+
config_settings = _COMMON_CONFIG_SETTINGS,
300291
)
301292

302293
_tests.append(_test_precompiler_action)

tests/support/BUILD.bazel

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
# to force them to resolve in the proper context.
1919
# ====================
2020

21-
load("//python:py_runtime.bzl", "py_runtime")
22-
load("//python:py_runtime_pair.bzl", "py_runtime_pair")
2321
load(":sh_py_run_test.bzl", "current_build_settings")
2422

2523
package(
@@ -89,27 +87,6 @@ platform(
8987
],
9088
)
9189

92-
py_runtime(
93-
name = "platform_runtime",
94-
implementation_name = "fakepy",
95-
interpreter_path = "/fake/python3.9",
96-
interpreter_version_info = {
97-
"major": "4",
98-
"minor": "5",
99-
},
100-
)
101-
102-
py_runtime_pair(
103-
name = "platform_runtime_pair",
104-
py3_runtime = ":platform_runtime",
105-
)
106-
107-
toolchain(
108-
name = "platform_toolchain",
109-
toolchain = ":platform_runtime_pair",
110-
toolchain_type = "//python:toolchain_type",
111-
)
112-
11390
current_build_settings(
11491
name = "current_build_settings",
11592
)

tests/support/py_toolchains/BUILD

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Copyright 2024 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+
# NOTE: tests/support/support.bzl has constants to easily refer to
17+
# these toolchains.
18+
# ====================
19+
20+
load("//python:py_runtime.bzl", "py_runtime")
21+
load("//python:py_runtime_pair.bzl", "py_runtime_pair")
22+
load("//python/private:py_exec_tools_toolchain.bzl", "py_exec_tools_toolchain") # buildifier: disable=bzl-visibility
23+
24+
# NOTE: A platform runtime is used because it doesn't include any files. This
25+
# makes it easier for analysis tests to verify content.
26+
py_runtime(
27+
name = "platform_runtime",
28+
implementation_name = "fakepy",
29+
interpreter_path = "/fake/python3.9",
30+
interpreter_version_info = {
31+
"major": "4",
32+
"minor": "5",
33+
},
34+
)
35+
36+
py_runtime_pair(
37+
name = "platform_runtime_pair",
38+
py3_runtime = ":platform_runtime",
39+
)
40+
41+
toolchain(
42+
name = "platform_toolchain",
43+
toolchain = ":platform_runtime_pair",
44+
toolchain_type = "//python:toolchain_type",
45+
)
46+
47+
toolchain(
48+
name = "exec_toolchain",
49+
toolchain = ":exec_toolchain_impl",
50+
toolchain_type = "//python:exec_tools_toolchain_type",
51+
)
52+
53+
# An exec toolchain is explicitly defined so that the tests pass when run
54+
# in environments that aren't using the toolchains generated by the
55+
# hermetic runtimes.
56+
py_exec_tools_toolchain(
57+
name = "exec_toolchain_impl",
58+
precompiler = "//tools/precompiler:precompiler",
59+
)

tests/support/support.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ LINUX_X86_64 = Label("//tests/support:linux_x86_64")
2626
WINDOWS = Label("//tests/support:windows")
2727
WINDOWS_X86_64 = Label("//tests/support:windows_x86_64")
2828

29-
PLATFORM_TOOLCHAIN = str(Label("//tests/support:platform_toolchain"))
29+
PY_TOOLCHAINS = str(Label("//tests/support/py_toolchains:all"))
3030
CC_TOOLCHAIN = str(Label("//tests/support/cc_toolchains:all"))
3131
CROSSTOOL_TOP = Label("//tests/support/cc_toolchains:cc_toolchain_suite")
3232

0 commit comments

Comments
 (0)