Skip to content
Closed
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 docs/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ bzl_library(
name = "defs",
srcs = [
"//python:defs.bzl",
"//python/py_pytest_main:defs.bzl",
"//python/private:reexports.bzl",
],
deps = [":bazel_python_tools"],
Expand Down
19 changes: 19 additions & 0 deletions docs/python.md

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

28 changes: 27 additions & 1 deletion examples/build_file_generation/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ load("@pip//:requirements.bzl", "all_whl_requirements")
load("@rules_python//gazelle:def.bzl", "GAZELLE_PYTHON_RUNTIME_DEPS")
load("@rules_python//gazelle/manifest:defs.bzl", "gazelle_python_manifest")
load("@rules_python//gazelle/modules_mapping:def.bzl", "modules_mapping")
load("@rules_python//python:defs.bzl", "py_binary", "py_library")
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
load("@rules_python//python:defs.bzl", "py_binary", "py_library", "py_pytest_main", "py_test")

compile_pip_requirements(
name = "pip_compile",
requirements_in = "requirements.txt",
requirements_txt = "requirements_lock.txt",
)

# This rule fetches the metadata for python packages we depend on. That data is
# required for the gazelle_python_manifest rule to update our manifest file.
Expand Down Expand Up @@ -54,3 +61,22 @@ py_binary(
visibility = ["//:__subpackages__"],
deps = [":build_file_generation"],
)

# Generates an entrypoint for pytest that is picked up by Gazelle for the py_test target below.
py_pytest_main(
name = "__test__",
)

Choose a reason for hiding this comment

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

How come py_pytest_main becomes a dependency to a py_test, instead of subsuming the py_test rule?

Wholly replacing the py_test rule with a macro/rule/rule+macro seems a cleaner user interface.

Copy link
Contributor Author

@mattem mattem Jun 10, 2022

Choose a reason for hiding this comment

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

This provides an entry point for pytest, the Python test runner, and doesn't force changing loads or existing rules, and keepspy_test the bazel rule a generic "run a python binary that will return 0 or 1". There are other launchers, perhaps a nose entrypoint etc.

This model also fits with the gazelle generation already present here.


py_test(
name = "build_file_generation_test",
srcs = [
"foo_test.py",
":__test__",
],
main = ":__test__.py",
deps = [
":__test__",
":build_file_generation",
"@pip_pytest//:pkg",
],
)
9 changes: 9 additions & 0 deletions examples/build_file_generation/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,19 @@ local_repository(
path = "../..",
)

load("@rules_python//python:repositories.bzl", "python_register_toolchains")

python_register_toolchains(
name = "python39",
python_version = "3.9",
)

load("@python39//:defs.bzl", "interpreter")
load("@rules_python//python:pip.bzl", "pip_parse")

pip_parse(
name = "pip",
python_interpreter_target = interpreter,
requirements_lock = "//:requirements_lock.txt",
)

Expand Down
4 changes: 4 additions & 0 deletions examples/build_file_generation/foo_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import pytest

def test_add():
assert 1 + 1 == 2, "Expected 1 + 1 to equal 2"
182 changes: 181 additions & 1 deletion examples/build_file_generation/gazelle_python.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,98 @@

manifest:
modules_mapping:
_pytest: pytest
_pytest.__init__: pytest
_pytest._argcomplete: pytest
_pytest._code: pytest
_pytest._code.__init__: pytest
_pytest._code.code: pytest
_pytest._code.source: pytest
_pytest._io: pytest
_pytest._io.__init__: pytest
_pytest._io.saferepr: pytest
_pytest._io.terminalwriter: pytest
_pytest._io.wcwidth: pytest
_pytest._version: pytest
_pytest.assertion: pytest
_pytest.assertion.__init__: pytest
_pytest.assertion.rewrite: pytest
_pytest.assertion.truncate: pytest
_pytest.assertion.util: pytest
_pytest.cacheprovider: pytest
_pytest.capture: pytest
_pytest.compat: pytest
_pytest.config: pytest
_pytest.config.__init__: pytest
_pytest.config.argparsing: pytest
_pytest.config.compat: pytest
_pytest.config.exceptions: pytest
_pytest.config.findpaths: pytest
_pytest.debugging: pytest
_pytest.deprecated: pytest
_pytest.doctest: pytest
_pytest.faulthandler: pytest
_pytest.fixtures: pytest
_pytest.freeze_support: pytest
_pytest.helpconfig: pytest
_pytest.hookspec: pytest
_pytest.junitxml: pytest
_pytest.legacypath: pytest
_pytest.logging: pytest
_pytest.main: pytest
_pytest.mark: pytest
_pytest.mark.__init__: pytest
_pytest.mark.expression: pytest
_pytest.mark.structures: pytest
_pytest.monkeypatch: pytest
_pytest.nodes: pytest
_pytest.nose: pytest
_pytest.outcomes: pytest
_pytest.pastebin: pytest
_pytest.pathlib: pytest
_pytest.pytester: pytest
_pytest.pytester_assertions: pytest
_pytest.python: pytest
_pytest.python_api: pytest
_pytest.python_path: pytest
_pytest.recwarn: pytest
_pytest.reports: pytest
_pytest.runner: pytest
_pytest.scope: pytest
_pytest.setuponly: pytest
_pytest.setupplan: pytest
_pytest.skipping: pytest
_pytest.stash: pytest
_pytest.stepwise: pytest
_pytest.terminal: pytest
_pytest.threadexception: pytest
_pytest.timing: pytest
_pytest.tmpdir: pytest
_pytest.unittest: pytest
_pytest.unraisableexception: pytest
_pytest.warning_types: pytest
_pytest.warnings: pytest
attr: attrs
attr.__init__: attrs
attr._cmp: attrs
attr._compat: attrs
attr._config: attrs
attr._funcs: attrs
attr._make: attrs
attr._next_gen: attrs
attr._version_info: attrs
attr.converters: attrs
attr.exceptions: attrs
attr.filters: attrs
attr.setters: attrs
attr.validators: attrs
attrs: attrs
attrs.__init__: attrs
attrs.converters: attrs
attrs.exceptions: attrs
attrs.filters: attrs
attrs.setters: attrs
attrs.validators: attrs
certifi: certifi
certifi.__init__: certifi
certifi.__main__: certifi
Expand Down Expand Up @@ -61,6 +153,89 @@ manifest:
idna.intranges: idna
idna.package_data: idna
idna.uts46data: idna
iniconfig: iniconfig
iniconfig.__init__: iniconfig
packaging: packaging
packaging.__about__: packaging
packaging.__init__: packaging
packaging._manylinux: packaging
packaging._musllinux: packaging
packaging._structures: packaging
packaging.markers: packaging
packaging.requirements: packaging
packaging.specifiers: packaging
packaging.tags: packaging
packaging.utils: packaging
packaging.version: packaging
pluggy: pluggy
pluggy.__init__: pluggy
pluggy._callers: pluggy
pluggy._hooks: pluggy
pluggy._manager: pluggy
pluggy._result: pluggy
pluggy._tracing: pluggy
pluggy._version: pluggy
py: py
py.__init__: py
py.__metainfo: py
py._builtin: py
py._code: py
py._code.__init__: py
py._code._assertionnew: py
py._code._assertionold: py
py._code._py2traceback: py
py._code.assertion: py
py._code.code: py
py._code.source: py
py._error: py
py._io: py
py._io.__init__: py
py._io.capture: py
py._io.saferepr: py
py._io.terminalwriter: py
py._log: py
py._log.__init__: py
py._log.log: py
py._log.warning: py
py._path: py
py._path.__init__: py
py._path.cacheutil: py
py._path.common: py
py._path.local: py
py._path.svnurl: py
py._path.svnwc: py
py._process: py
py._process.__init__: py
py._process.cmdexec: py
py._process.forkedfunc: py
py._process.killproc: py
py._std: py
py._vendored_packages: py
py._vendored_packages.__init__: py
py._vendored_packages.apipkg: py
py._vendored_packages.apipkg.__init__: py
py._vendored_packages.apipkg.version: py
py._vendored_packages.iniconfig: py
py._vendored_packages.iniconfig.__init__: py
py._version: py
py._xmlgen: py
py.test: py
pyparsing: pyparsing
pyparsing.__init__: pyparsing
pyparsing.actions: pyparsing
pyparsing.common: pyparsing
pyparsing.core: pyparsing
pyparsing.diagram: pyparsing
pyparsing.diagram.__init__: pyparsing
pyparsing.exceptions: pyparsing
pyparsing.helpers: pyparsing
pyparsing.results: pyparsing
pyparsing.testing: pyparsing
pyparsing.unicode: pyparsing
pyparsing.util: pyparsing
pytest: pytest
pytest.__init__: pytest
pytest.__main__: pytest
requests: requests
requests.__init__: requests
requests.__version__: requests
Expand All @@ -80,6 +255,11 @@ manifest:
requests.status_codes: requests
requests.structures: requests
requests.utils: requests
tomli: tomli
tomli.__init__: tomli
tomli._parser: tomli
tomli._re: tomli
tomli._types: tomli
urllib3: urllib3
urllib3.__init__: urllib3
urllib3._collections: urllib3
Expand Down Expand Up @@ -129,4 +309,4 @@ manifest:
pip_repository:
name: pip
incremental: true
integrity: c47bf2ca0a185cf6b8815d4a61e26e7457564e931de76c70653277e4eccfadc8
integrity: 71c787ef3d0dca470bb9c1913a5ef3ca4373db13cbd24acada1c7fcb40ef10ee
1 change: 1 addition & 0 deletions examples/build_file_generation/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
requests==2.25.1
pytest~=7.1
34 changes: 33 additions & 1 deletion examples/build_file_generation/requirements_lock.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
# This file is autogenerated by pip-compile with python 3.9
# To update, run:
#
# pip-compile --generate-hashes --output-file=requirements_lock.txt requirements.txt
# bazel run //:pip_compile.update
#
attrs==21.4.0 \
--hash=sha256:2d27e3784d7a565d36ab851fe94887c5eccd6a463168875832a1be79c82828b4 \
--hash=sha256:626ba8234211db98e869df76230a137c4c40a12d72445c45d5f5b716f076e2fd
# via pytest
certifi==2020.12.5 \
--hash=sha256:1a4995114262bffbc2413b159f2a1a480c969de6e6eb13ee966d470af86af59c \
--hash=sha256:719a74fb9e33b9bd44cc7f3a8d94bc35e4049deebe19ba7d8e108280cfd59830
Expand All @@ -16,10 +20,38 @@ idna==2.10 \
--hash=sha256:b307872f855b18632ce0c21c5e45be78c0ea7ae4c15c828c20788b26921eb3f6 \
--hash=sha256:b97d804b1e9b523befed77c48dacec60e6dcb0b5391d57af6a65a312a90648c0
# via requests
iniconfig==1.1.1 \
--hash=sha256:011e24c64b7f47f6ebd835bb12a743f2fbe9a26d4cecaa7f53bc4f35ee9da8b3 \
--hash=sha256:bc3af051d7d14b2ee5ef9969666def0cd1a000e121eaea580d4a313df4b37f32
# via pytest
packaging==21.3 \
--hash=sha256:dd47c42927d89ab911e606518907cc2d3a1f38bbd026385970643f9c5b8ecfeb \
--hash=sha256:ef103e05f519cdc783ae24ea4e2e0f508a9c99b2d4969652eed6a2e1ea5bd522
# via pytest
pluggy==1.0.0 \
--hash=sha256:4224373bacce55f955a878bf9cfa763c1e360858e330072059e10bad68531159 \
--hash=sha256:74134bbf457f031a36d68416e1509f34bd5ccc019f0bcc952c7b909d06b37bd3
# via pytest
py==1.11.0 \
--hash=sha256:51c75c4126074b472f746a24399ad32f6053d1b34b68d2fa41e558e6f4a98719 \
--hash=sha256:607c53218732647dff4acdfcd50cb62615cedf612e72d1724fb1a0cc6405b378
# via pytest
pyparsing==3.0.9 \
--hash=sha256:2b020ecf7d21b687f219b71ecad3631f644a47f01403fa1d1036b0c6416d70fb \
--hash=sha256:5026bae9a10eeaefb61dab2f09052b9f4307d44aee4eda64b309723d8d206bbc
# via packaging
pytest==7.1.2 \
--hash=sha256:13d0e3ccfc2b6e26be000cb6568c832ba67ba32e719443bfe725814d3c42433c \
--hash=sha256:a06a0425453864a270bc45e71f783330a7428defb4230fb5e6a731fde06ecd45
# via -r requirements.txt
requests==2.25.1 \
--hash=sha256:27973dd4a904a4f13b263a19c866c13b92a39ed1c964655f025f3f8d3d75b804 \
--hash=sha256:c210084e36a42ae6b9219e00e48287def368a26d03a048ddad7bfee44f75871e
# via -r requirements.txt
tomli==2.0.1 \
--hash=sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc \
--hash=sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f
# via pytest
urllib3==1.26.5 \
--hash=sha256:753a0374df26658f99d826cfe40394a686d05985786d946fbe4165b5148f5a7c \
--hash=sha256:a7acd0977125325f516bda9735fa7142b909a8d01e8b2e4c8108d0984e6e0098
Expand Down
4 changes: 4 additions & 0 deletions gazelle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ dependencies are added to the `deps` attribute.

Python test files are those ending in `_test.py`.

Gazelle will add a `py_test` target whenever it finds either of the following:
* A target in the same Bazel package named __test__. The macro `py_pytest_main` provide these entrypoint wrappers.
* A file in the same directory named __test__.py.

A `py_test` target is added containing all test files as `srcs`.

### Binaries
Expand Down
3 changes: 3 additions & 0 deletions python/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ load(
_py_runtime = "py_runtime",
_py_test = "py_test",
)
load("//python/py_pytest_main:defs.bzl", _py_pytest_main = "py_pytest_main")

# Exports of native-defined providers.

Expand Down Expand Up @@ -140,3 +141,5 @@ py_binary = _py_binary
py_test = _py_test

py_runtime = _py_runtime

py_pytest_main = _py_pytest_main
Loading