Skip to content

feat: provide default deps #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
docs/*.md linguist-generated
56 changes: 36 additions & 20 deletions docs/rules.md

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

4 changes: 2 additions & 2 deletions e2e/smoke/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ py_pytest_test(
name = "test_sharded",
size = "small",
srcs = ["test_sharded.py"],
shard_count = 2,
deps = [
pytest_deps = [
requirement("pytest"),
requirement("pytest-shard"),
],
shard_count = 2,
)

build_test(
Expand Down
71 changes: 49 additions & 22 deletions python_pytest/defs.bzl
Original file line number Diff line number Diff line change
@@ -1,25 +1,56 @@
"""Public API"""
"""Use pytest to run tests, using a wrapper script to interface with Bazel.

Example:

```starlark
load("@caseyduquettesc_rules_python_pytest//python_pytest:defs.bzl", "py_pytest_test")

py_pytest_test(
name = "test_w_pytest",
size = "small",
srcs = ["test.py"],
)
```

By default, `@pip//pytest` is added to `deps`.
If sharding is used (when `shard_count > 1`) then `@pip//pytest_shard` is also added.
To instead provide explicit deps for the pytest library, set `pytest_deps`:

```starlark
py_pytest_test(
name = "test_w_my_pytest",
shard_count = 2,
srcs = ["test.py"],
pytest_deps = [requirement("pytest"), requirement("pytest-shard"), ...],
)
```
"""

load("@rules_python//python:defs.bzl", "py_test")
# load("@py_deps//:requirements.bzl", "requirement")

def py_pytest_test(name, srcs, deps = [], args = [], **kwargs):
"""Use pytest to run tests, using a wrapper script to interface with Bazel.

```py
py_pytest_test(
name = "test_w_pytest",
size = "small",
srcs = ["test.py"],
deps = [
# TODO Add this for the user
requirement("pytest"),
],
)
```

def py_pytest_test(name, srcs, deps = [], args = [], pytest_deps = None, pip_repo = "pip", **kwargs):
"""
Wrapper macro for `py_test` which supports pytest.

Args:
name: A unique name for this target.
srcs: Python source files.
deps: Dependencies, typically `py_library`.
args: Additional command-line arguments to pytest.
See https://docs.pytest.org/en/latest/how-to/usage.html
pytest_deps: Labels of the pytest tool and other packages it may import.
pip_repo: Name of the external repository where Python packages are installed.
It's typically created by `pip.parse`.
This attribute is used only when `pytest_deps` is unset.
**kwargs: Additional named parameters to py_test.
"""
shim_label = Label("//python_pytest:pytest_shim.py")

if pytest_deps == None:
pytest_deps = ["@{}//pytest".format(pip_repo)]
if kwargs.get("shard_count", 1) > 1:
pytest_deps.append("@{}//pytest_shard".format(pip_repo))

py_test(
name = name,
srcs = [
Expand All @@ -31,10 +62,6 @@ def py_pytest_test(name, srcs, deps = [], args = [], **kwargs):
] + args + ["$(location :%s)" % x for x in srcs],
# python_version = "PY3",
# srcs_version = "PY3",
# TODO It'd be nice to implicitly include pytest, but I don't know how to know the requirements repo nme
# deps = deps + [
# requirement("pytest"),
# ],
deps = deps,
deps = deps + pytest_deps,
**kwargs
)
Loading