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>
13 files changed
tree: 50860250c5fa59e2c80eca1eb9a8f16565246ee3
  1. .bazelci/
  2. .bcr/
  3. .ci/
  4. .github/
  5. docs/
  6. examples/
  7. gazelle/
  8. python/
  9. sphinxdocs/
  10. tests/
  11. third_party/
  12. tools/
  13. .bazelignore
  14. .bazelrc
  15. .bazelversion
  16. .git-blame-ignore-revs
  17. .gitattributes
  18. .gitignore
  19. .pre-commit-config.yaml
  20. .readthedocs.yml
  21. addlicense.sh
  22. AUTHORS
  23. BUILD.bazel
  24. BZLMOD_SUPPORT.md
  25. CHANGELOG.md
  26. CONTRIBUTING.md
  27. CONTRIBUTORS
  28. DEVELOPING.md
  29. internal_deps.bzl
  30. internal_setup.bzl
  31. LICENSE
  32. MODULE.bazel
  33. README.md
  34. version.bzl
  35. WORKSPACE
  36. WORKSPACE.bzlmod
README.md

Python Rules for Bazel

Build status

Overview

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.

Documentation

For detailed documentation, see https://rules-python.readthedocs.io

Bzlmod support

  • Status: Beta
  • Full Feature Parity: No

See Bzlmod support for more details.