Skip to content

Commit 394dda2

Browse files
authored
tests: add tests to verify toolchain registration (#3313)
This adds tests that verify toolchains are registered and resolving correctly for the different variants and platforms for the runtimes. This also shows that workspace mode isn't registering musl or freethreaded builds correctly, so use them isn't as easy as simply setting the build flags. For now, the tests skip those in workspace mode. Along the way, fix a bug where py_runtime would crash if it got the python version from the flag, and the version contained more than micro (e.g. "3.14.0rc0")
1 parent f5ab3bc commit 394dda2

File tree

15 files changed

+311
-99
lines changed

15 files changed

+311
-99
lines changed

python/private/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,7 @@ bzl_library(
531531
":py_runtime_info_bzl",
532532
":reexports_bzl",
533533
":rule_builders_bzl",
534+
":version_bzl",
534535
"@bazel_skylib//lib:dicts",
535536
"@bazel_skylib//lib:paths",
536537
"@bazel_skylib//rules:common_settings",

python/private/py_runtime_rule.bzl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ load(":flags.bzl", "FreeThreadedFlag")
2121
load(":py_internal.bzl", "py_internal")
2222
load(":py_runtime_info.bzl", "DEFAULT_STUB_SHEBANG", "PyRuntimeInfo")
2323
load(":reexports.bzl", "BuiltinPyRuntimeInfo")
24+
load(":version.bzl", "version")
2425

2526
_py_builtins = py_internal
2627

@@ -387,11 +388,11 @@ def _is_singleton_depset(files):
387388
return _py_builtins.is_singleton_depset(files)
388389

389390
def _interpreter_version_info_from_version_str(version_str):
390-
parts = version_str.split(".")
391+
v = version.parse(version_str)
391392
version_info = {}
393+
parts = list(v.release)
392394
for key in ("major", "minor", "micro"):
393395
if not parts:
394396
break
395397
version_info[key] = parts.pop(0)
396-
397398
return version_info

python/private/version.bzl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,8 @@ def parse(version_str, strict = False, _fail = fail):
622622
# https://peps.python.org/pep-0440/#public-version-identifiers
623623
return None
624624

625-
return struct(
625+
# buildifier: disable=uninitialized
626+
self = struct(
626627
epoch = _parse_epoch(parts["epoch"], _fail),
627628
release = _parse_release(parts["release"]),
628629
pre = _parse_pre(parts["pre"]),
@@ -631,7 +632,9 @@ def parse(version_str, strict = False, _fail = fail):
631632
local = _parse_local(parts["local"], _fail),
632633
string = parts["norm"],
633634
is_prefix = parts["is_prefix"],
635+
key = lambda *a, **k: _version_key(self, *a, **k),
634636
)
637+
return self
635638

636639
def _parse_epoch(value, fail):
637640
if not value:

tests/base_rules/py_executable_base_tests.bzl

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ load("//python/private:reexports.bzl", "BuiltinPyRuntimeInfo") # buildifier: di
2323
load("//tests/base_rules:base_tests.bzl", "create_base_tests")
2424
load("//tests/base_rules:util.bzl", "WINDOWS_ATTR", pt_util = "util")
2525
load("//tests/support:py_executable_info_subject.bzl", "PyExecutableInfoSubject")
26-
load("//tests/support:support.bzl", "CC_TOOLCHAIN", "CROSSTOOL_TOP", "LINUX_X86_64", "WINDOWS_X86_64")
26+
load("//tests/support:support.bzl", "CC_TOOLCHAIN", "CROSSTOOL_TOP")
27+
load("//tests/support/platforms:platforms.bzl", "platform_targets")
2728

2829
_tests = []
2930

@@ -46,9 +47,9 @@ def _test_basic_windows(name, config):
4647
"//command_line_option:build_python_zip": "true",
4748
"//command_line_option:cpu": "windows_x86_64",
4849
"//command_line_option:crosstool_top": CROSSTOOL_TOP,
49-
"//command_line_option:extra_execution_platforms": [WINDOWS_X86_64],
50+
"//command_line_option:extra_execution_platforms": [platform_targets.WINDOWS_X86_64],
5051
"//command_line_option:extra_toolchains": [CC_TOOLCHAIN],
51-
"//command_line_option:platforms": [WINDOWS_X86_64],
52+
"//command_line_option:platforms": [platform_targets.WINDOWS_X86_64],
5253
},
5354
attr_values = {},
5455
)
@@ -89,9 +90,9 @@ def _test_basic_zip(name, config):
8990
"//command_line_option:build_python_zip": "true",
9091
"//command_line_option:cpu": "linux_x86_64",
9192
"//command_line_option:crosstool_top": CROSSTOOL_TOP,
92-
"//command_line_option:extra_execution_platforms": [LINUX_X86_64],
93+
"//command_line_option:extra_execution_platforms": [platform_targets.LINUX_X86_64],
9394
"//command_line_option:extra_toolchains": [CC_TOOLCHAIN],
94-
"//command_line_option:platforms": [LINUX_X86_64],
95+
"//command_line_option:platforms": [platform_targets.LINUX_X86_64],
9596
},
9697
attr_values = {"target_compatible_with": target_compatible_with},
9798
)
@@ -326,8 +327,8 @@ def _test_main_module_bootstrap_system_python(name, config):
326327
target = name + "_subject",
327328
config_settings = {
328329
labels.BOOTSTRAP_IMPL: "system_python",
329-
"//command_line_option:extra_execution_platforms": ["@bazel_tools//tools:host_platform", LINUX_X86_64],
330-
"//command_line_option:platforms": [LINUX_X86_64],
330+
"//command_line_option:extra_execution_platforms": ["@bazel_tools//tools:host_platform", platform_targets.LINUX_X86_64],
331+
"//command_line_option:platforms": [platform_targets.LINUX_X86_64],
331332
},
332333
)
333334

@@ -350,8 +351,8 @@ def _test_main_module_bootstrap_script(name, config):
350351
target = name + "_subject",
351352
config_settings = {
352353
labels.BOOTSTRAP_IMPL: "script",
353-
"//command_line_option:extra_execution_platforms": ["@bazel_tools//tools:host_platform", LINUX_X86_64],
354-
"//command_line_option:platforms": [LINUX_X86_64],
354+
"//command_line_option:extra_execution_platforms": ["@bazel_tools//tools:host_platform", platform_targets.LINUX_X86_64],
355+
"//command_line_option:platforms": [platform_targets.LINUX_X86_64],
355356
},
356357
)
357358

tests/base_rules/py_test/py_test_tests.bzl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ load(
2121
"create_executable_tests",
2222
)
2323
load("//tests/base_rules:util.bzl", pt_util = "util")
24-
load("//tests/support:support.bzl", "CC_TOOLCHAIN", "CROSSTOOL_TOP", "LINUX_X86_64", "MAC_X86_64")
24+
load("//tests/support:support.bzl", "CC_TOOLCHAIN", "CROSSTOOL_TOP")
25+
load("//tests/support/platforms:platforms.bzl", "platform_targets")
2526

2627
# The Windows CI currently runs as root, which breaks when
2728
# the analysis tests try to install (but not use, because
@@ -51,9 +52,9 @@ def _test_mac_requires_darwin_for_execution(name, config):
5152
config_settings = {
5253
"//command_line_option:cpu": "darwin_x86_64",
5354
"//command_line_option:crosstool_top": CROSSTOOL_TOP,
54-
"//command_line_option:extra_execution_platforms": [MAC_X86_64],
55+
"//command_line_option:extra_execution_platforms": [platform_targets.MAC_X86_64],
5556
"//command_line_option:extra_toolchains": [CC_TOOLCHAIN],
56-
"//command_line_option:platforms": [MAC_X86_64],
57+
"//command_line_option:platforms": [platform_targets.MAC_X86_64],
5758
},
5859
attr_values = _SKIP_WINDOWS,
5960
)
@@ -78,9 +79,9 @@ def _test_non_mac_doesnt_require_darwin_for_execution(name, config):
7879
config_settings = {
7980
"//command_line_option:cpu": "k8",
8081
"//command_line_option:crosstool_top": CROSSTOOL_TOP,
81-
"//command_line_option:extra_execution_platforms": [LINUX_X86_64],
82+
"//command_line_option:extra_execution_platforms": [platform_targets.LINUX_X86_64],
8283
"//command_line_option:extra_toolchains": [CC_TOOLCHAIN],
83-
"//command_line_option:platforms": [LINUX_X86_64],
84+
"//command_line_option:platforms": [platform_targets.LINUX_X86_64],
8485
},
8586
attr_values = _SKIP_WINDOWS,
8687
)

tests/config_settings/transition/multi_version_tests.bzl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ load("//python:py_info.bzl", "PyInfo")
2222
load("//python:py_test.bzl", "py_test")
2323
load("//python/private:reexports.bzl", "BuiltinPyInfo") # buildifier: disable=bzl-visibility
2424
load("//tests/support:support.bzl", "CC_TOOLCHAIN")
25+
load("//tests/support/platforms:platforms.bzl", "platform_targets")
2526

2627
# NOTE @aignas 2024-06-04: we are using here something that is registered in the MODULE.Bazel
2728
# and if you find tests failing, it could be because of the toolchain resolution issues here.
@@ -92,7 +93,7 @@ def _setup_py_binary_windows(name, *, impl, build_python_zip):
9293
config_settings = {
9394
"//command_line_option:build_python_zip": build_python_zip,
9495
"//command_line_option:extra_toolchains": CC_TOOLCHAIN,
95-
"//command_line_option:platforms": str(Label("//tests/support:windows_x86_64")),
96+
"//command_line_option:platforms": str(platform_targets.WINDOWS_X86_64),
9697
},
9798
)
9899

tests/exec_toolchain_matching/exec_toolchain_matching_tests.bzl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ load("//python:py_runtime.bzl", "py_runtime")
2020
load("//python:py_runtime_pair.bzl", "py_runtime_pair")
2121
load("//python/private:common_labels.bzl", "labels") # buildifier: disable=bzl-visibility
2222
load("//python/private:toolchain_types.bzl", "EXEC_TOOLS_TOOLCHAIN_TYPE", "TARGET_TOOLCHAIN_TYPE") # buildifier: disable=bzl-visibility
23-
load("//tests/support:support.bzl", "LINUX", "MAC")
23+
load("//tests/support/platforms:platforms.bzl", "platform_targets")
2424

2525
_LookupInfo = provider() # buildifier: disable=provider-params
2626

@@ -126,9 +126,9 @@ def _test_exec_matches_target_python_version(name):
126126
target = name + "_subject",
127127
impl = _test_exec_matches_target_python_version_impl,
128128
config_settings = {
129-
"//command_line_option:extra_execution_platforms": [str(MAC)],
129+
"//command_line_option:extra_execution_platforms": [str(platform_targets.MAC)],
130130
"//command_line_option:extra_toolchains": ["//tests/exec_toolchain_matching:all"],
131-
"//command_line_option:platforms": [str(LINUX)],
131+
"//command_line_option:platforms": [str(platform_targets.LINUX)],
132132
labels.PYTHON_VERSION: "3.12",
133133
},
134134
)

tests/pypi/config_settings/config_settings_tests.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ _subject = rule(
3131
)
3232

3333
_flag = struct(
34-
platform = lambda x: ("//command_line_option:platforms", str(Label("//tests/support:" + x))),
34+
platform = lambda x: ("//command_line_option:platforms", str(Label("//tests/support/platforms:" + x))),
3535
pip_whl = lambda x: (str(Label("//python/config_settings:pip_whl")), str(x)),
3636
pip_whl_glibc_version = lambda x: (str(Label("//python/config_settings:pip_whl_glibc_version")), str(x)),
3737
pip_whl_muslc_version = lambda x: (str(Label("//python/config_settings:pip_whl_muslc_version")), str(x)),

tests/support/BUILD.bazel

Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -12,82 +12,13 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
# ====================
16-
# NOTE: You probably want to use the constants in test_platforms.bzl
17-
# Otherwise, you'll probably have to manually call Label() on these targets
18-
# to force them to resolve in the proper context.
19-
# ====================
20-
2115
load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
2216
load(":sh_py_run_test.bzl", "current_build_settings")
2317

2418
package(
2519
default_visibility = ["//:__subpackages__"],
2620
)
2721

28-
platform(
29-
name = "mac",
30-
constraint_values = [
31-
"@platforms//os:macos",
32-
],
33-
)
34-
35-
platform(
36-
name = "linux",
37-
constraint_values = [
38-
"@platforms//os:linux",
39-
],
40-
)
41-
42-
platform(
43-
name = "windows",
44-
constraint_values = [
45-
"@platforms//os:windows",
46-
],
47-
)
48-
49-
# Used when testing downloading of toolchains for a different platform
50-
51-
platform(
52-
name = "linux_x86_64",
53-
constraint_values = [
54-
"@platforms//cpu:x86_64",
55-
"@platforms//os:linux",
56-
],
57-
)
58-
59-
platform(
60-
name = "linux_aarch64",
61-
constraint_values = [
62-
"@platforms//cpu:aarch64",
63-
"@platforms//os:linux",
64-
],
65-
)
66-
67-
platform(
68-
name = "mac_x86_64",
69-
constraint_values = [
70-
"@platforms//cpu:x86_64",
71-
"@platforms//os:macos",
72-
],
73-
)
74-
75-
platform(
76-
name = "windows_x86_64",
77-
constraint_values = [
78-
"@platforms//cpu:x86_64",
79-
"@platforms//os:windows",
80-
],
81-
)
82-
83-
platform(
84-
name = "win_aarch64",
85-
constraint_values = [
86-
"@platforms//os:windows",
87-
"@platforms//cpu:aarch64",
88-
],
89-
)
90-
9122
current_build_settings(
9223
name = "current_build_settings",
9324
)
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package(
2+
default_visibility = ["//:__subpackages__"],
3+
)
4+
5+
# ====================
6+
# NOTE: You probably want to use the constants in test_platforms.bzl
7+
# Otherwise, you'll probably have to manually call Label() on these targets
8+
# to force them to resolve in the proper context.
9+
# ====================
10+
platform(
11+
name = "mac",
12+
constraint_values = [
13+
"@platforms//os:macos",
14+
],
15+
)
16+
17+
platform(
18+
name = "linux",
19+
constraint_values = [
20+
"@platforms//os:linux",
21+
],
22+
)
23+
24+
platform(
25+
name = "windows",
26+
constraint_values = [
27+
"@platforms//os:windows",
28+
],
29+
)
30+
31+
platform(
32+
name = "linux_x86_64",
33+
constraint_values = [
34+
"@platforms//cpu:x86_64",
35+
"@platforms//os:linux",
36+
],
37+
)
38+
39+
platform(
40+
name = "linux_aarch64",
41+
constraint_values = [
42+
"@platforms//cpu:aarch64",
43+
"@platforms//os:linux",
44+
],
45+
)
46+
47+
platform(
48+
name = "mac_x86_64",
49+
constraint_values = [
50+
"@platforms//cpu:x86_64",
51+
"@platforms//os:macos",
52+
],
53+
)
54+
55+
platform(
56+
name = "mac_aarch64",
57+
constraint_values = [
58+
"@platforms//cpu:aarch64",
59+
"@platforms//os:macos",
60+
],
61+
)
62+
63+
platform(
64+
name = "windows_x86_64",
65+
constraint_values = [
66+
"@platforms//cpu:x86_64",
67+
"@platforms//os:windows",
68+
],
69+
)
70+
71+
platform(
72+
name = "windows_aarch64",
73+
constraint_values = [
74+
"@platforms//os:windows",
75+
"@platforms//cpu:aarch64",
76+
],
77+
)

0 commit comments

Comments
 (0)