8000 fix(gazelle): Do not create invalid py_test rules in `project` genera… · emfdavid/rules_python@d5945ed · GitHub
[go: up one dir, main page]

Skip to content

Commit d5945ed

Browse files
authored
fix(gazelle): Do not create invalid py_test rules in projec 8000 t 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

File tree

26 files changed

+133
-14
lines changed

26 files changed

+133
-14
lines changed

CHANGELOG.md

Lines 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

Lines 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

Lines 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
)
Lines 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
Lines 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+
)
Lines 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.
Lines 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

Whitespace-only changes.

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

Whitespace-only changes.

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

Whitespace-only changes.

0 commit comments

Comments
 (0)
0