Skip to content

Commit

Permalink
Add env_inherit to py_test (#459)
Browse files Browse the repository at this point in the history
Fixes #457

---------

Co-authored-by: Alex Eagle <[email protected]>
  • Loading branch information
keith and alexeagle authored Dec 5, 2024
1 parent bf9fdb2 commit 743e9a8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
5 changes: 3 additions & 2 deletions docs/py_test.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/pytest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ py_test(
"foo_test.py",
":__test__",
],
env_inherit = ["FOO"],
imports = ["../.."],
main = ":__test__.py",
package_collisions = "warning",
Expand Down
11 changes: 10 additions & 1 deletion py/private/py_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ def _py_binary_rule_impl(ctx):
instrumented_files_info,
RunEnvironmentInfo(
environment = passed_env,
inherited_environment = getattr(ctx.attr, "env_inherit", []),
),
]

Expand Down Expand Up @@ -172,6 +173,13 @@ A collision can occour when multiple packages providing the same file are instal

_attrs.update(**_py_library.attrs)

_test_attrs = dict({
"env_inherit": attr.string_list(
doc = "Specifies additional environment variables to inherit from the external environment when the test is executed by bazel test.",
default = [],
),
})

def _python_version_transition_impl(_, attr):
if not attr.python_version:
return {}
Expand All @@ -186,6 +194,7 @@ _python_version_transition = transition(
py_base = struct(
implementation = _py_binary_rule_impl,
attrs = _attrs,
test_attrs = _test_attrs,
toolchains = [
PY_TOOLCHAIN,
VENV_TOOLCHAIN,
Expand All @@ -205,7 +214,7 @@ py_binary = rule(
py_test = rule(
doc = "Run a Python program under Bazel. Most users should use the [py_test macro](#py_test) instead of loading this directly.",
implementation = py_base.implementation,
attrs = py_base.attrs,
attrs = py_base.attrs | py_base.test_attrs,
toolchains = py_base.toolchains,
test = True,
cfg = py_base.cfg,
Expand Down

0 comments on commit 743e9a8

Please sign in to comment.