Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d5945ed

Browse filesBrowse files
authored
fix(gazelle): Do not create invalid py_test rules in project generation mode (bazel-contrib#1809)
Since bazel-contrib#1538, when using `gazelle:python_generation_mode project`, a `py_test` rule is created even when there are no test files in the project. fb673ee (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.
1 parent e86252f commit d5945ed
Copy full SHA for d5945ed

File tree

Expand file treeCollapse file tree

26 files changed

+133
-14
lines changed
Filter options
Expand file treeCollapse file tree

26 files changed

+133
-14
lines changed

‎CHANGELOG.md

Copy file name to clipboardExpand all lines: CHANGELOG.md
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ A brief description of the categories of changes:
2626
* (whl_library): Fix the experimental_target_platforms overriding for platform
2727
specific wheels when the wheels are for any python interpreter version. Fixes
2828
[#1810](https://github.com/bazelbuild/rules_python/issues/1810).
29+
* (gazelle) In `project` or `package` generation modes, do not generate `py_test`
30+
rules when there are no test files and do not set `main = "__test__.py"` when
31+
that file doesn't exist.
2932

3033
### Added
3134

‎gazelle/python/generate.go

Copy file name to clipboardExpand all lines: gazelle/python/generate.go
+15-13Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -393,20 +393,22 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
393393
// the file exists on disk.
394394
pyTestFilenames.Add(pyTestEntrypointFilename)
395395
}
396-
pyTestTargetName := cfg.RenderTestName(packageName)
397-
pyTestTarget := newPyTestTargetBuilder(pyTestFilenames, pyTestTargetName)
398-
399-
if hasPyTestEntryPointTarget {
400-
entrypointTarget := fmt.Sprintf(":%s", pyTestEntrypointTargetname)
401-
main := fmt.Sprintf(":%s", pyTestEntrypointFilename)
402-
pyTestTarget.
403-
addSrc(entrypointTarget).
404-
addResolvedDependency(entrypointTarget).
405-
setMain(main)
406-
} else {
407-
pyTestTarget.setMain(pyTestEntrypointFilename)
396+
if (hasPyTestEntryPointTarget || !pyTestFilenames.Empty()) {
397+
pyTestTargetName := cfg.RenderTestName(packageName)
398+
pyTestTarget := newPyTestTargetBuilder(pyTestFilenames, pyTestTargetName)
399+
400+
if hasPyTestEntryPointTarget {
401+
entrypointTarget := fmt.Sprintf(":%s", pyTestEntrypointTargetname)
402+
main := fmt.Sprintf(":%s", pyTestEntrypointFilename)
403+
pyTestTarget.
404+
addSrc(entrypointTarget).
405+
addResolvedDependency(entrypointTarget).
406+
setMain(main)
407+
} else if hasPyTestEntryPointFile {
408+
pyTestTarget.setMain(pyTestEntrypointFilename)
409+
}
410+
pyTestTargets = append(pyTestTargets, pyTestTarget)
408411
}
409-
pyTestTargets = append(pyTestTargets, pyTestTarget)
410412
} else {
411413
// Create one py_test target per file
412414
pyTestFilenames.Each(func(index int, testFile interface{}) {

‎gazelle/python/testdata/monorepo/coarse_grained/BUILD.out

Copy file name to clipboardExpand all lines: gazelle/python/testdata/monorepo/coarse_grained/BUILD.out
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,4 @@ py_test(
2525
"bar/bar_test.py",
2626
"foo/bar/bar_test.py",
2727
],
28-
main = "__test__.py",
2928
)
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# gazelle:python_extension enabled
2+
# gazelle:python_generation_mode project
+14Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
load("@rules_python//python:defs.bzl", "py_library")
2+
3+
# gazelle:python_extension enabled
4+
# gazelle:python_generation_mode project
5+
6+
py_library(
7+
name = "project_generation_mode",
8+
srcs = [
9+
"__init__.py",
10+
"bar/bar.py",
11+
"foo/foo.py",
12+
],
13+
visibility = ["//:__subpackages__"],
14+
)
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Project generation mode
2+
3+
Simple example using `gazelle:python_generation_mode project` in a project with no tests.
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# This is a Bazel workspace for the Gazelle test data.

‎gazelle/python/testdata/project_generation_mode/__init__.py

Copy file name to clipboardExpand all lines: gazelle/python/testdata/project_generation_mode/__init__.py
Whitespace-only changes.

‎gazelle/python/testdata/project_generation_mode/bar/bar.py

Copy file name to clipboardExpand all lines: gazelle/python/testdata/project_generation_mode/bar/bar.py
Whitespace-only changes.

‎gazelle/python/testdata/project_generation_mode/foo/foo.py

Copy file name to clipboardExpand all lines: gazelle/python/testdata/project_generation_mode/foo/foo.py
Whitespace-only changes.
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright 2023 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
---
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# gazelle:python_extension enabled
2+
# gazelle:python_generation_mode project
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
load("@rules_python//python:defs.bzl", "py_library", "py_test")
2+
3+
# gazelle:python_extension enabled
4+
# gazelle:python_generation_mode project
5+
6+
py_library(
7+
name = "project_generation_mode_with_test_entrypoint",
8+
srcs = ["__init__.py"],
9+
visibility = ["//:__subpackages__"],
10+
)
11+
12+
py_test(
13+
name = "project_generation_mode_with_test_entrypoint_test",
14+
srcs = [
15+
"__test__.py",
16+
"foo/foo_test.py",
17+
],
18+
main = "__test__.py",
19+
)
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Project generation mode with test entrypoint
2+
3+
Example using `gazelle:python_generation_mode project` in a project with tests that use an explicit `__test__.py` entrypoint.
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# This is a Bazel workspace for the Gazelle test data.

‎gazelle/python/testdata/project_generation_mode_with_test_entrypoint/__init__.py

Copy file name to clipboardExpand all lines: gazelle/python/testdata/project_generation_mode_with_test_entrypoint/__init__.py
Whitespace-only changes.

‎gazelle/python/testdata/project_generation_mode_with_test_entrypoint/__test__.py

Copy file name to clipboardExpand all lines: gazelle/python/testdata/project_generation_mode_with_test_entrypoint/__test__.py
Whitespace-only changes.

‎gazelle/python/testdata/project_generation_mode_with_test_entrypoint/foo/foo_test.py

Copy file name to clipboardExpand all lines: gazelle/python/testdata/project_generation_mode_with_test_entrypoint/foo/foo_test.py
Whitespace-only changes.
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright 2023 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
---
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# gazelle:python_extension enabled
2+
# gazelle:python_generation_mode project
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
load("@rules_python//python:defs.bzl", "py_library", "py_test")
2+
3+
# gazelle:python_extension enabled
4+
# gazelle:python_generation_mode project
5+
6+
py_library(
7+
name = "project_generation_mode_with_tests",
8+
srcs = ["__init__.py"],
9+
visibility = ["//:__subpackages__"],
10+
)
11+
12+
py_test(
13+
name = "project_generation_mode_with_tests_test",
14+
srcs = ["foo/foo_test.py"],
15+
)
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Project generation mode with tests
2+
3+
Example using `gazelle:python_generation_mode project` in a project with tests, but no `__test__.py` entrypoint.
4+
5+
Note that, in this mode, the `py_test` rule will have no `main` set, which will fail to run with the standard
6+
`py_test` rule. However, this can be used in conjunction with `gazelle:map_kind` to use some other implementation
7+
of `py_test` that is able to handle this sitation (such as `rules_python_pytest`).
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# This is a Bazel workspace for the Gazelle test data.

‎gazelle/python/testdata/project_generation_mode_with_tests/__init__.py

Copy file name to clipboardExpand all lines: gazelle/python/testdata/project_generation_mode_with_tests/__init__.py
Whitespace-only changes.

‎gazelle/python/testdata/project_generation_mode_with_tests/foo/foo_test.py

Copy file name to clipboardExpand all lines: gazelle/python/testdata/project_generation_mode_with_tests/foo/foo_test.py
Whitespace-only changes.
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright 2023 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
---

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.