commit | 990a0538e796f86dcb9a86e0276b7fdf1bf44c18 | [log] [tgz] |
---|---|---|
author | Zhongpeng Lin <zplin@uber.com> | Fri Jul 19 00:49:29 2024 -0700 |
committer | GitHub <noreply@github.com> | Fri Jul 19 07:49:29 2024 +0000 |
tree | 50860250c5fa59e2c80eca1eb9a8f16565246ee3 | |
parent | 9ff6ab7905f79079766c69ff8735542ebf9894bb [diff] |
feat!: Following generation mode when generating test targets (#2044) When `python_generation_mode` is `project` or `file` , the generated `py_test` targets are consistent with `py_library`. However, when `python_generation_mode` is `package`, it becomes inconsistent: it requires either `__test__` target or `__test__.py` file to generate `py_test` in package mode, otherwise it will fall back to file mode. This PR relaxes this requirement with a new directive `gazelle:python_generation_mode_per_package_require_test_entry_point`. Whent it's set to false, Gazelle and generates one `py_test` target per package in package mode even without entry points. This allows people to use `gazelle:map_kind` to map `py_test` to a macro that sets a default test runner, such as [rules_python_pytest](https://github.com/caseyduquettesc/rules_python_pytest) or [pytest-bazel](https://pypi.org/project/pytest-bazel/), and generate one test target per package. The behavior when `gazelle:python_generation_mode` is "file" or "project" remains the same. This fixes #1972 for supporting pytest from Gazelle. With this approach, people can define a thin macro like this to use py_pytest_main: ``` load("@aspect_rules_py//py:defs.bzl", "py_pytest_main") def py_test(name, **kwargs): py_pytest_main( name = "__test__", deps = ["@pip_pytest//:pkg"], # change this to the pytest target in your repo. ) deps = kwargs.pop("deps", []) deps.append(":__test__") py_test( name = name, main = ":__test__.py", deps = deps, **kwargs, ) ``` BREAKING CHANGES: Without `gazelle:map_kind` or `__test__` target or `__test__.py`, the package mode will now generate `py_test` without `main` attribute, which may not be runnable. However, this is already an issue with "python_generation_mode:project" before this PR. The default value of `gazelle:python_generation_mode_per_package_require_test_entry_point` is true to preserve the current behavior. We will flip that default value in the future. --------- Co-authored-by: aignas <240938+aignas@users.noreply.github.com>
This repository is the home of the core Python rules -- py_library
, py_binary
, py_test
, py_proto_library
, and related symbols that provide the basis for Python support in Bazel. It also contains package installation rules for integrating with PyPI and other indices.
Documentation for rules_python is at https://rules-python.readthedocs.io and in the Bazel Build Encyclopedia.
Examples live in the examples directory.
The core rules are stable. Their implementation is subject to Bazel's backward compatibility policy. This repository aims to follow semantic versioning.
The Bazel community maintains this repository. Neither Google nor the Bazel team provides support for the code. However, this repository is part of the test suite used to vet new Bazel releases. See How to contribute page for information on our development workflow.
For detailed documentation, see https://rules-python.readthedocs.io
See Bzlmod support for more details.