Skip to content

add requirements deps automatically #10

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 1 commit 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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ From the release you wish to use:
<https://github.com/caseyduquettesc/rules_python_pytest/releases>
copy the WORKSPACE snippet into your `WORKSPACE` file or the `bazel_dep` if you use bzlmod.

#################################################

# loadup the requirements for pytest
load("@rules_python_pytest//python_pytest:repositories.bzl", "setup_pytest_requirements")
# this requires you to pass in your python interpreter
setup_pytest_requirements(interpreter = interpreter)
load("@pytest_requirements//:requirements.bzl", "install_deps")
install_deps()

#################################################
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a bzlmod example?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll have to cook one up. Haven't done much with bazelmod yet. It's been high friction so far.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's a bad idea for requirements to be installed by external packages. They aren't constraint-solved with the user's app code, they don't see the lockfile, and they don't have control over the version.
Just expect the user to provide a pytest label (can default to @pip//pytest for convenience)



## Usage

```py
Expand Down
1 change: 1 addition & 0 deletions WORKSPACE.bazel
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# Marker that this is the root of a Bazel workspace.
workspace("rules_python_pytest")
2 changes: 1 addition & 1 deletion e2e/smoke/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# This file is autogenerated by pip-compile with Python 3.9
# This file is autogenerated by pip-compile with Python 3.10
# by the following command:
#
# pip-compile --allow-unsafe --generate-hashes --no-emit-index-url requirements.in
Expand Down
13 changes: 5 additions & 8 deletions python_pytest/defs.bzl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Public API"""

load("@rules_python//python:defs.bzl", "py_test")
# load("@py_deps//:requirements.bzl", "requirement")
load("@pytest_requirements//: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.
Expand All @@ -12,8 +12,6 @@ def py_pytest_test(name, srcs, deps = [], args = [], **kwargs):
size = "small",
srcs = ["test.py"],
deps = [
# TODO Add this for the user
requirement("pytest"),
],
)
```
Expand All @@ -31,10 +29,9 @@ 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 + [
requirement("pytest"),
requirement("pluggy"),
],
**kwargs
)
9 changes: 9 additions & 0 deletions python_pytest/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ See https://docs.bazel.build/versions/main/skylark/deploying.html#dependencies

load("@bazel_tools//tools/build_defs/repo:http.bzl", _http_archive = "http_archive")
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
load("@rules_python//python:pip.bzl", "pip_parse")

def http_archive(name, **kwargs):
maybe(_http_archive, name = name, **kwargs)
Expand Down Expand Up @@ -34,3 +35,11 @@ def rules_python_pytest_dependencies():
strip_prefix = "rules_python-0.6.0",
url = "https://github.com/bazelbuild/rules_python/archive/0.6.0.tar.gz",
)

def setup_pytest_requirements(interpreter):
pip_parse(
name = "pytest_requirements",
python_interpreter_target = interpreter,
quiet = False,
requirements_lock = "@rules_python_pytest//:e2e/smoke/requirements.txt",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we want production code depending on test code. Are these requirements something we can bring into python_pytest/?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, we don't. This proves the concept. Can certainly reshape this to be its own target. Any suggestions?

)