Skip to content

Commit 28fda86

Browse files
authored
tests: refactor py_reconfig rules so less boilerplate is needed to add attrs (#2933)
Just some minor refactoring of the py_reconfig rule so that it's easier to add attributes that affect transition state. After this, just two spots have to be modified to add an attribute (map of attrs, map of attr to transition label).
1 parent 3aea414 commit 28fda86

File tree

1 file changed

+78
-4
lines changed

1 file changed

+78
-4
lines changed

tests/support/sh_py_run_test.bzl

Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,88 @@
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 outer environmental
17-
settings and verifying their output without the overhead of a bazel-in-bazel
18-
integration test.
16+
This facilitates verify running binaries with different configuration settings
17+
without the overhead of a bazel-in-bazel integration test.
1918
"""
2019

2120
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
2226
load("//python/private:toolchain_types.bzl", "TARGET_TOOLCHAIN_TYPE") # buildifier: disable=bzl-visibility
23-
load(":py_reconfig.bzl", "py_reconfig_binary")
27+
load("//tests/support:support.bzl", "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+
36+
for attr_name, setting_label in _RECONFIG_ATTR_SETTING_MAP.items():
37+
if getattr(attr, attr_name):
38+
settings[setting_label] = getattr(attr, attr_name)
39+
return settings
40+
41+
# Attributes that, if non-falsey (`if attr.<name>`), will copy their
42+
# value into the output settings
43+
_RECONFIG_ATTR_SETTING_MAP = {
44+
"bootstrap_impl": "//python/config_settings:bootstrap_impl",
45+
"extra_toolchains": "//command_line_option:extra_toolchains",
46+
"python_src": "//python/bin:python_src",
47+
"venvs_site_packages": "//python/config_settings:venvs_site_packages",
48+
"venvs_use_declare_symlink": "//python/config_settings:venvs_use_declare_symlink",
49+
}
50+
51+
_RECONFIG_INPUTS = _RECONFIG_ATTR_SETTING_MAP.values()
52+
_RECONFIG_OUTPUTS = _RECONFIG_INPUTS + [
53+
"//command_line_option:build_python_zip",
54+
VISIBLE_FOR_TESTING,
55+
]
56+
_RECONFIG_INHERITED_OUTPUTS = [v for v in _RECONFIG_OUTPUTS if v in _RECONFIG_INPUTS]
57+
58+
_RECONFIG_ATTRS = {
59+
"bootstrap_impl": attrb.String(),
60+
"build_python_zip": attrb.String(default = "auto"),
61+
"extra_toolchains": attrb.StringList(
62+
doc = """
63+
Value for the --extra_toolchains flag.
64+
65+
NOTE: You'll likely have to also specify //tests/support/cc_toolchains:all (or some CC toolchain)
66+
to make the RBE presubmits happy, which disable auto-detection of a CC
67+
toolchain.
68+
""",
69+
),
70+
"python_src": attrb.Label(),
71+
"venvs_site_packages": attrb.String(),
72+
"venvs_use_declare_symlink": attrb.String(),
73+
}
74+
75+
def _create_reconfig_rule(builder):
76+
builder.attrs.update(_RECONFIG_ATTRS)
77+
78+
base_cfg_impl = builder.cfg.implementation()
79+
builder.cfg.set_implementation(lambda *args: _perform_transition_impl(base_impl = base_cfg_impl, *args))
80+
builder.cfg.update_inputs(_RECONFIG_INPUTS)
81+
builder.cfg.update_outputs(_RECONFIG_OUTPUTS)
82+
return builder.build()
83+
84+
_py_reconfig_binary = _create_reconfig_rule(create_py_binary_rule_builder())
85+
86+
_py_reconfig_test = _create_reconfig_rule(create_py_test_rule_builder())
87+
88+
def py_reconfig_test(**kwargs):
89+
"""Create a py_test with customized build settings for testing.
90+
91+
Args:
92+
**kwargs: kwargs to pass along to _py_reconfig_test.
93+
"""
94+
py_test_macro(_py_reconfig_test, **kwargs)
95+
96+
def py_reconfig_binary(**kwargs):
97+
py_binary_macro(_py_reconfig_binary, **kwargs)
2498

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

0 commit comments

Comments
 (0)