8000 tests: move py_reconfig rules to their own file (#2900) · nedwill/rules_python@8e6f73b · GitHub
[go: up one dir, main page]

Skip to content

Commit 8e6f73b

Browse files
authored
tests: move py_reconfig rules to their own file (bazel-contrib#2900)
The py_reconfig code is pretty large, so move it to its own file. It's also be easier to find in its own file rather that part of something named after "shell testing".
1 parent 459e1df commit 8e6f73b

File tree

12 files changed

+116
-94
lines changed
< 8000 div class="d-flex flex-items-center flex-justify-between gap-2 pt-3 pt-lg-4 pb-2 position-sticky top-0 color-bg-default" style="z-index:2">

12 files changed

+116
-94
lines changed

tests/bootstrap_impls/BUILD.bazel

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ load("@rules_shell//shell:sh_test.bzl", "sh_test")
1313
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
16-
load("//tests/support:sh_py_run_test.bzl", "py_reconfig_binary", "py_reconfig_test", "sh_py_run_test")
16+
load("//tests/support:py_reconfig.bzl", "py_reconfig_binary", "py_reconfig_test")
17+
load("//tests/support:sh_py_run_test.bzl", "sh_py_run_test")
1718
load("//tests/support:support.bzl", "SUPPORTS_BOOTSTRAP_SCRIPT")
1819
load(":venv_relative_path_tests.bzl", "relative_path_test_suite")
1920

tests/bootstrap_impls/a/b/c/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER") # buildifier: disable=bzl-visibility
2-
load("//tests/support:sh_py_run_test.bzl", "py_reconfig_test")
2+
load("//tests/support:py_reconfig.bzl", "py_reconfig_test")
33

44
_SUPPORTS_BOOTSTRAP_SCRIPT = select({
55
"@platforms//os:windows": ["@platforms//:incompatible"],

tests/interpreter/interpreter_tests.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
"""This file contains helpers for testing the interpreter rule."""
1616

17-
load("//tests/support:sh_py_run_test.bzl", "py_reconfig_test")
17+
load("//tests/support:py_reconfig.bzl", "py_reconfig_test")
1818

1919
# The versions of Python that we want to run the interpreter tests against.
2020
PYTHON_VERSIONS_TO_TEST = (

tests/no_unsafe_paths/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
load("//tests/support:sh_py_run_test.bzl", "py_reconfig_test")
14+
load("//tests/support:py_reconfig.bzl", "py_reconfig_test")
1515
load("//tests/support:support.bzl", "SUPPORTS_BOOTSTRAP_SCRIPT")
1616

1717
py_reconfig_test(

tests/packaging/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
load("@bazel_skylib//rules:build_test.bzl", "build_test")
1616
load("@rules_pkg//pkg:tar.bzl", "pkg_tar")
17-
load("//tests/support:sh_py_run_test.bzl", "py_reconfig_test")
17+
load("//tests/support:py_reconfig.bzl", "py_reconfig_test")
1818
load("//tests/support:support.bzl", "SUPPORTS_BOOTSTRAP_SCRIPT")
1919

2020
build_test(

tests/repl/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
load("//python:py_library.bzl", "py_library")
2-
load("//tests/support:sh_py_run_test.bzl", "py_reconfig_test")
2+
load("//tests/support:py_reconfig.bzl", "py_reconfig_test")
33

44
# A library that adds a special import path only when this is specified as a
55
# dependency. This makes it easy for a dependency to have this import path

tests/runtime_env_toolchain/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414

1515
load("@rules_python_runtime_env_tc_info//:info.bzl", "PYTHON_VERSION")
16-
load("//tests/support:sh_py_run_test.bzl", "py_reconfig_test")
16+
load("//tests/support:py_reconfig.bzl", "py_reconfig_test")
1717
load("//tests/support:support.bzl", "CC_TOOLCHAIN")
1818
load(":runtime_env_toolchain_tests.bzl", "runtime_env_toolchain_test_suite")
1919

tests/support/py_reconfig.bzl

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
"""Run a py_binary/py_test with altered config settings.
15+
16+
This facilitates verify running binaries with different configuration settings
17+
without the overhead of a bazel-in-bazel integration test.
18+
"""
19+
20+
load("//python/private:attr_builders.bzl", "attrb") # buildifier: disable=bzl-visibility
21+
load("//python/private:py_binary_macro.bzl", "py_binary_macro") # buildifier: disable=bzl-visibility
22+
load("//python/private:py_binary_rule.bzl", "create_py_binary_rule_builder") # buildifier: disable=bzl-visibility
23+
load("//python/private:py_test_macro.bzl", "py_test_macro") # buildifier: disable=bzl-visibility
24+
load("//python/private:py_test_rule.bzl", "create_py_test_rule_builder") # buildifier: disable=bzl-visibility
25+
load("//tests/support:support.bzl", "VISIBLE_FOR_TESTING")
26+
27+
def _perform_transition_impl(input_settings, attr, base_impl):
28+
settings = {k: input_settings[k] for k in _RECONFIG_INHERITED_OUTPUTS if k in input_settings}
29+
settings.update(base_impl(input_settings, attr))
30+
31+
settings[VISIBLE_FOR_TESTING] = True
32+
settings["//command_line_option:build_python_zip"] = attr.build_python_zip
33+
if attr.bootstrap_impl:
34+
settings["//python/config_settings:bootstrap_impl"] = attr.bootstrap_impl
35+
if attr.extra_toolchains:
36+
settings["//command_line_option:extra_toolchains"] = attr.extra_toolchains
37+
if attr.python_src:
38+
settings["//python/bin:python_src"] = attr.python_src
39+
if attr.repl_dep:
40+
settings["//python/bin:repl_dep"] = attr.repl_dep
41+
if attr.venvs_use_declare_symlink:
42+
settings["//python/config_settings:venvs_use_declare_symlink"] = attr.venvs_use_declare_symlink
43+
if attr.venvs_site_packages:
44+
settings["//python/config_settings:venvs_site_packages"] = attr.venvs_site_packages
45+
return settings
46+
47+
_RECONFIG_INPUTS = [
48+
"//python/config_settings:bootstrap_impl",
49+
"//python/bin:python_src",
50+
"//python/bin:repl_dep",
51+
"//command_line_option:extra_toolchains",
52+
"//python/config_settings:venvs_use_declare_symlink",
53+
"//python/config_settings:venvs_site_packages",
54+
]
55+
_RECONFIG_OUTPUTS = _RECONFIG_INPUTS + [
56+
"//command_line_option:build_python_zip",
57+
VISIBLE_FOR_TESTING,
58+
]
59+
_RECONFIG_INHERITED_OUTPUTS = [v for v in _RECONFIG_OUTPUTS if v in _RECONFIG_INPUTS]
60+
61+
_RECONFIG_ATTRS = {
62+
"bootstrap_impl": attrb.String(),
63+
"build_python_zip": attrb.String(default = "auto"),
64+
"extra_toolchains": attrb.StringList(
65+
doc = """
66+
Value for the --extra_toolchains flag.
67+
68+
NOTE: You'll likely have to also specify //tests/support/cc_toolchains:all (or some CC toolchain)
69+
to make the RBE presubmits happy, which disable auto-detection of a CC
70+
toolchain.
71+
""",
72+
),
73+
"python_src": attrb.Label(),
74+
"repl_dep": attrb.Label(),
75+
"venvs_site_packages": attrb.String(),
76+
"venvs_use_declare_symlink": attrb.String(),
77+
}
78+
79+
def _create_reconfig_rule(builder):
80+
builder.attrs.update(_RECONFIG_ATTRS)
81+
82+
base_cfg_impl = builder.cfg.implementation()
83+
builder.cfg.set_implementation(lambda *args: _perform_transition_impl(base_impl = base_cfg_impl, *args))
84+
builder.cfg.update_inputs(_RECONFIG_INPUTS)
85+
builder.cfg.update_outputs(_RECONFIG_OUTPUTS)
86+
return builder.build()
87+
88+
_py_reconfig_binary = _create_reconfig_rule(create_py_binary_rule_builder())
89+
90+
_py_reconfig_test = _create_reconfig_rule(create_py_test_rule_builder())
91+
92+
def py_reconfig_test(**kwargs):
93+
"""Create a py_test with customized build settings for testing.
94+
95+
Args:
96+
**kwargs: kwargs to pass along to _py_reconfig_test.
97+
"""
98+
py_test_macro(_py_reconfig_test, **kwargs)
99+
100+
def py_reconfig_binary(**kwargs):
101+
py_binary_macro(_py_reconfig_binary, **kwargs)

tests/support/sh_py_run_test.bzl

Lines changed: 4 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -13,94 +13,14 @@
1313
# limitations under the License.
1414
"""Run a py_binary with altered config settings in an sh_test.
1515
16-
This facilitates verify running binaries with different configuration settings
17-
without the overhead of a bazel-in-bazel integration test.
16+
This facilitates verify running binaries with different outer environmental
17+
settings and verifying their output without the overhead of a bazel-in-bazel
18+
integration test.
1819
"""
1920

2021
load("@rules_shell//shell:sh_test.bzl", "sh_test")
21-
load("//python/private:attr_builders.bzl", "attrb") # buildifier: disable=bzl-visibility
22-
load("//python/private:py_binary_macro.bzl", "py_binary_macro") # buildifier: disable=bzl-visibility
23-
load("//python/private:py_binary_rule.bzl", "create_py_binary_rule_builder") # buildifier: disable=bzl-visibility
24-
load("//python/private:py_test_macro.bzl", "py_test_macro") # buildifier: disable=bzl-visibility
25-
load("//python/private:py_test_rule.bzl", "create_py_test_rule_builder") # buildifier: disable=bzl-visibility
2622
load("//python/private:toolchain_types.bzl", "TARGET_TOOLCHAIN_TYPE") # buildifier: disable=bzl-visibility
27-
load("//tests/support:support.bzl", &qu F438 ot;VISIBLE_FOR_TESTING")
28-
29-
def _perform_transition_impl(input_settings, attr, base_impl):
30-
settings = {k: input_settings[k] for k in _RECONFIG_INHERITED_OUTPUTS if k in input_settings}
31-
settings.update(base_impl(input_settings, attr))
32-
33-
settings[VISIBLE_FOR_TESTING] = True
34-
settings["//command_line_option:build_python_zip"] = attr.build_python_zip
35-
if attr.bootstrap_impl:
36-
settings["//python/config_settings:bootstrap_impl"] = attr.bootstrap_impl
37-
if attr.extra_toolchains:
38-
settings["//command_line_option:extra_toolchains"] = attr.extra_toolchains
39-
if attr.python_src:
40-
settings["//python/bin:python_src"] = attr.python_src
41-
if attr.repl_dep:
42-
settings["//python/bin:repl_dep"] = attr.repl_dep
43-
if attr.venvs_use_declare_symlink:
44-
settings["//python/config_settings:venvs_use_declare_symlink"] = attr.venvs_use_declare_symlink
45-
if attr.venvs_site_packages:
46-
settings["//python/config_settings:venvs_site_packages"] = attr.venvs_site_packages
47-
return settings
48-
49-
_RECONFIG_INPUTS = [
50-
"//python/config_settings:bootstrap_impl",
51-
"//python/bin:python_src",
52-
"//python/bin:repl_dep",
53-
"//command_line_option:extra_toolchains",
54-
"//python/config_settings:venvs_use_declare_symlink",
55-
"//python/config_settings:venvs_site_packages",
56-
]
57-
_RECONFIG_OUTPUTS = _RECONFIG_INPUTS + [
58-
"//command_line_option:build_python_zip",
59-
VISIBLE_FOR_TESTING,
60-
]
61-
_RECONFIG_INHERITED_OUTPUTS = [v for v in _RECONFIG_OUTPUTS if v in _RECONFIG_INPUTS]
62-
63-
_RECONFIG_ATTRS = {
64-
"bootstrap_impl": attrb.String(),
65-
"build_python_zip": attrb.String(default = "auto"),
66-
"extra_toolchains": attrb.StringList(
67-
doc = """
68-
Value for the --extra_toolchains flag.
69-
70-
NOTE: You'll likely have to also specify //tests/support/cc_toolchains:all (or some CC toolchain)
71-
to make the RBE presubmits happy, which disable auto-detection of a CC
72-
toolchain.
73-
""",
74-
),
75-
"python_src": attrb.Label(),
76-
"repl_dep": attrb.Label(),
77-
"venvs_site_packages": attrb.String(),
78-
"venvs_use_declare_symlink": attrb.String(),
79-
}
80-
81-
def _create_reconfig_rule(builder):
82-
builder.attrs.update(_RECONFIG_ATTRS)
83-
84-
base_cfg_impl = builder.cfg.implementation()
85-
builder.cfg.set_implementation(lambda *args: _perform_transition_impl(base_impl = base_cfg_impl, *args))
86-
builder.cfg.update_inputs(_RECONFIG_INPUTS)
87-
builder.cfg.update_outputs(_RECONFIG_OUTPUTS)
88-
return builder.build()
89-
90-
_py_reconfig_binary = _create_reconfig_rule(create_py_binary_rule_builder())
91-
92-
_py_reconfig_test = _create_reconfig_rule(create_py_test_rule_builder())
93-
94-
def py_reconfig_test(**kwargs):
95-
"""Create a py_test with customized build settings for testing.
96-
97-
Args:
98-
**kwargs: kwargs to pass along to _py_reconfig_test.
99-
"""
100-
py_test_macro(_py_reconfig_test, **kwargs)
101-
102-
def py_reconfig_binary(**kwargs):
103-
py_binary_macro(_py_reconfig_binary, **kwargs)
23+
load(":py_reconfig.bzl", "py_reconfig_binary")
10424

10525
def sh_py_run_test(*, name, sh_src, py_src, **kwargs):
10626
"""Run a py_binary within a sh_test.

tests/toolchains/defs.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
""
1616

1717
load("//python:versions.bzl", "PLATFORMS", "TOOL_VERSIONS")
18-
load("//tests/support:sh_py_run_test.bzl", "py_reconfig_test")
18+
load("//tests/support:py_reconfig.bzl", "py_reconfig_test")
1919

2020
def define_toolchain_tests(name):
2121
"""Define the toolchain tests.

0 commit comments

Comments
 (0)
0