-
Notifications
You must be signed in to change notification settings - Fork 543
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]>
- Loading branch information
Showing
13 changed files
with
235 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
gazelle/python/testdata/per_package_test_target_without_entry_point/BUILD.in
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# gazelle:python_generation_mode package | ||
# gazelle:python_generation_mode_per_package_require_test_entry_point false |
18 changes: 18 additions & 0 deletions
18
gazelle/python/testdata/per_package_test_target_without_entry_point/BUILD.out
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
load("@rules_python//python:defs.bzl", "py_library", "py_test") | ||
|
||
# gazelle:python_generation_mode package | ||
# gazelle:python_generation_mode_per_package_require_test_entry_point false | ||
|
||
py_library( | ||
name = "per_package_test_target_without_entry_point", | ||
srcs = ["__init__.py"], | ||
visibility = ["//:__subpackages__"], | ||
) | ||
|
||
py_test( | ||
name = "per_package_test_target_without_entry_point_test", | ||
srcs = [ | ||
"bar_test.py", | ||
"foo_test.py", | ||
], | ||
) |
3 changes: 3 additions & 0 deletions
3
gazelle/python/testdata/per_package_test_target_without_entry_point/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# One test target per package without entry point | ||
|
||
This test case asserts that one test target is generated per package without entry point when `gazelle:python_generation_mode_per_package_require_test_entry_point false` |
1 change: 1 addition & 0 deletions
1
gazelle/python/testdata/per_package_test_target_without_entry_point/WORKSPACE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# This is a Bazel workspace for the Gazelle test data. |
Empty file.
24 changes: 24 additions & 0 deletions
24
gazelle/python/testdata/per_package_test_target_without_entry_point/bar_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Copyright 2023 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import unittest | ||
|
||
|
||
class BarTest(unittest.TestCase): | ||
def test_foo(self): | ||
pass | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
24 changes: 24 additions & 0 deletions
24
gazelle/python/testdata/per_package_test_target_without_entry_point/foo_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Copyright 2023 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import unittest | ||
|
||
|
||
class FooTest(unittest.TestCase): | ||
def test_foo(self): | ||
pass | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
17 changes: 17 additions & 0 deletions
17
gazelle/python/testdata/per_package_test_target_without_entry_point/test.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Copyright 2023 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
--- | ||
expect: | ||
exit_code: 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters