|
13 | 13 | # limitations under the License.
|
14 | 14 | """Run a py_binary with altered config settings in an sh_test.
|
15 | 15 |
|
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. |
19 | 18 | """
|
20 | 19 |
|
21 | 20 | 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 |
22 | 26 | 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) |
24 | 98 |
|
25 | 99 | def sh_py_run_test(*, name, sh_src, py_src, **kwargs):
|
26 | 100 | """Run a py_binary within a sh_test.
|
|
0 commit comments