-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(gazelle): Do not create invalid py_test rules in
project
genera…
…tion mode (#1809) Since bazelbuild/rules_python#1538, when using `gazelle:python_generation_mode project`, a `py_test` rule is created even when there are no test files in the project. fb673ee47b3268a65a18a154edd574b6509c38c7 (first commit on the PR branch) reproduces this issue - it shows that a `py_test` rule is created even when there is no test entrypoint file (`__test__.py`) nor any test file in the entire project. Additionally, test rules created on `project` or `package` generation mode will always set `main = "__test__.py"`, even when that file doesn't exist. This PR fixes it by only generating a `py_test` rule if it can find some test file - either an explicit `__test__.py`, or any other file prefixed with `test_` or suffixed with `_test.py`. It also changes the generated test rule to only add `main = "__test__.py"` if there is an actual `__test__.py` file. Note that, in the case where a `__test__.py` file doesn't exist, the generated `py_test` rule is likely invalid as-is due to the lack of `main`; this can be fixed by mapping to some other `py_test` implementation, and I believe the new behavior makes more sense than pointing `main` to a non-existing file. It may be useful to review per-commit (all tests pass on each commit): - First commit reproduces the issue; - Second commit fixes the issue and the corresponding tests; - Third commit adds additional test cases.
- Loading branch information
Showing
25 changed files
with
130 additions
and
14 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
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 |
---|---|---|
|
@@ -25,5 +25,4 @@ py_test( | |
"bar/bar_test.py", | ||
"foo/bar/bar_test.py", | ||
], | ||
main = "__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,2 @@ | ||
# gazelle:python_extension enabled | ||
# gazelle:python_generation_mode project |
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,14 @@ | ||
load("@rules_python//python:defs.bzl", "py_library") | ||
|
||
# gazelle:python_extension enabled | ||
# gazelle:python_generation_mode project | ||
|
||
py_library( | ||
name = "project_generation_mode", | ||
srcs = [ | ||
"__init__.py", | ||
"bar/bar.py", | ||
"foo/foo.py", | ||
], | ||
visibility = ["//:__subpackages__"], | ||
) |
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 @@ | ||
# Project generation mode | ||
|
||
Simple example using `gazelle:python_generation_mode project` in a project with no tests. |
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.
Empty file.
Empty file.
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,15 @@ | ||
# 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. | ||
|
||
--- |
2 changes: 2 additions & 0 deletions
2
python/testdata/project_generation_mode_with_test_entrypoint/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_extension enabled | ||
# gazelle:python_generation_mode project |
19 changes: 19 additions & 0 deletions
19
python/testdata/project_generation_mode_with_test_entrypoint/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,19 @@ | ||
load("@rules_python//python:defs.bzl", "py_library", "py_test") | ||
|
||
# gazelle:python_extension enabled | ||
# gazelle:python_generation_mode project | ||
|
||
py_library( | ||
name = "project_generation_mode_with_test_entrypoint", | ||
srcs = ["__init__.py"], | ||
visibility = ["//:__subpackages__"], | ||
) | ||
|
||
py_test( | ||
name = "project_generation_mode_with_test_entrypoint_test", | ||
srcs = [ | ||
"__test__.py", | ||
"foo/foo_test.py", | ||
], | ||
main = "__test__.py", | ||
) |
3 changes: 3 additions & 0 deletions
3
python/testdata/project_generation_mode_with_test_entrypoint/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 @@ | ||
# Project generation mode with test entrypoint | ||
|
||
Example using `gazelle:python_generation_mode project` in a project with tests that use an explicit `__test__.py` entrypoint. |
1 change: 1 addition & 0 deletions
1
python/testdata/project_generation_mode_with_test_entrypoint/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.
Empty file.
Empty file.
15 changes: 15 additions & 0 deletions
15
python/testdata/project_generation_mode_with_test_entrypoint/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,15 @@ | ||
# 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. | ||
|
||
--- |
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_extension enabled | ||
# gazelle:python_generation_mode project |
15 changes: 15 additions & 0 deletions
15
python/testdata/project_generation_mode_with_tests/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,15 @@ | ||
load("@rules_python//python:defs.bzl", "py_library", "py_test") | ||
|
||
# gazelle:python_extension enabled | ||
# gazelle:python_generation_mode project | ||
|
||
py_library( | ||
name = "project_generation_mode_with_tests", | ||
srcs = ["__init__.py"], | ||
visibility = ["//:__subpackages__"], | ||
) | ||
|
||
py_test( | ||
name = "project_generation_mode_with_tests_test", | ||
srcs = ["foo/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,7 @@ | ||
# Project generation mode with tests | ||
|
||
Example using `gazelle:python_generation_mode project` in a project with tests, but no `__test__.py` entrypoint. | ||
|
||
Note that, in this mode, the `py_test` rule will have no `main` set, which will fail to run with the standard | ||
`py_test` rule. However, this can be used in conjunction with `gazelle:map_kind` to use some other implementation | ||
of `py_test` that is able to handle this sitation (such as `rules_python_pytest`). |
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.
Empty file.
15 changes: 15 additions & 0 deletions
15
python/testdata/project_generation_mode_with_tests/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,15 @@ | ||
# 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. | ||
|
||
--- |