Skip to content

Commit

Permalink
rename to wrap_for_pytest boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeagle committed Jan 28, 2025
1 parent 92ae365 commit 061a46c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
4 changes: 2 additions & 2 deletions docs/py_test.md

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

9 changes: 5 additions & 4 deletions e2e/use_release/src/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ py_binary(
py_test(
name = "test",
srcs = ["my_test.py"],
pytest_main_deps = [
"@pip//coverage",
"@pip//pytest",
wrap_for_pytest = True,
deps = [
":main",
"@pip//coverage", # keep
"@pip//pytest", # keep
],
deps = [":main"],
)
16 changes: 6 additions & 10 deletions py/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def py_binary(name, srcs = [], main = None, **kwargs):

_py_binary_or_test(name = name, rule = _py_binary, srcs = srcs, main = main, resolutions = resolutions, **kwargs)

def py_test(name, srcs = [], main = None, pytest_main_deps = None, **kwargs):
def py_test(name, srcs = [], main = None, wrap_for_pytest = False, **kwargs):
"""Identical to [py_binary](./py_binary.md), but produces a target that can be used with `bazel test`.
Args:
Expand All @@ -127,8 +127,8 @@ def py_test(name, srcs = [], main = None, pytest_main_deps = None, **kwargs):
Like rules_python, this is treated as a suffix of a file that should appear among the srcs.
If absent, then `[name].py` is tried. As a final fallback, if the srcs has a single file,
that is used as the main.
pytest_main_deps: List of labels. If set, generate a [py_pytest_main](#py_pytest_main) script and use it as the py_test entry point.
The supplied labels define the deps attribute for the generated py_pytest_main.
wrap_for_pytest: If set, generate a [py_pytest_main](#py_pytest_main) script and use it as the py_test entry point.
The deps should include the pytest package (and coverage if needed).
**kwargs: additional named parameters to `py_binary_rule`.
"""

Expand All @@ -141,17 +141,13 @@ def py_test(name, srcs = [], main = None, pytest_main_deps = None, **kwargs):
if resolutions:
resolutions = resolutions.to_label_keyed_dict()

deps = kwargs.pop("deps", [])
if pytest_main_deps:
if wrap_for_pytest:
if main:
fail("When pytest_main_deps is set, the main attribute should not be set.")
pytest_main_target = name + ".pytest_main"
main = pytest_main_target + ".py"
py_pytest_main(
name = pytest_main_target,
deps = pytest_main_deps,
)
py_pytest_main(name = pytest_main_target)
srcs.append(main)
deps.append(pytest_main_target)

_py_binary_or_test(name = name, rule = _py_test, srcs = srcs, deps = deps, main = main, resolutions = resolutions, **kwargs)
_py_binary_or_test(name = name, rule = _py_test, srcs = srcs, main = main, resolutions = resolutions, **kwargs)

0 comments on commit 061a46c

Please sign in to comment.