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

fix(gazelle): Do not create invalid py_test rules in project generation mode #1809

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions 3 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ A brief description of the categories of changes:
* (whl_library): Fix the experimental_target_platforms overriding for platform
specific wheels when the wheels are for any python interpreter version. Fixes
[#1810](https://github.com/bazelbuild/rules_python/issues/1810).
* (gazelle) In `project` or `package` generation modes, do not generate `py_test`
rules when there are no test files and do not set `main = "__test__.py"` when
that file doesn't exist.

### Added

Expand Down
28 changes: 15 additions & 13 deletions 28 gazelle/python/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,20 +393,22 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
// the file exists on disk.
pyTestFilenames.Add(pyTestEntrypointFilename)
}
pyTestTargetName := cfg.RenderTestName(packageName)
pyTestTarget := newPyTestTargetBuilder(pyTestFilenames, pyTestTargetName)

if hasPyTestEntryPointTarget {
entrypointTarget := fmt.Sprintf(":%s", pyTestEntrypointTargetname)
main := fmt.Sprintf(":%s", pyTestEntrypointFilename)
pyTestTarget.
addSrc(entrypointTarget).
addResolvedDependency(entrypointTarget).
setMain(main)
} else {
pyTestTarget.setMain(pyTestEntrypointFilename)
if (hasPyTestEntryPointTarget || !pyTestFilenames.Empty()) {
pyTestTargetName := cfg.RenderTestName(packageName)
pyTestTarget := newPyTestTargetBuilder(pyTestFilenames, pyTestTargetName)

if hasPyTestEntryPointTarget {
entrypointTarget := fmt.Sprintf(":%s", pyTestEntrypointTargetname)
main := fmt.Sprintf(":%s", pyTestEntrypointFilename)
pyTestTarget.
addSrc(entrypointTarget).
addResolvedDependency(entrypointTarget).
setMain(main)
} else if hasPyTestEntryPointFile {
pyTestTarget.setMain(pyTestEntrypointFilename)
}
pyTestTargets = append(pyTestTargets, pyTestTarget)
}
pyTestTargets = append(pyTestTargets, pyTestTarget)
} else {
// Create one py_test target per file
pyTestFilenames.Each(func(index int, testFile interface{}) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,4 @@ py_test(
"bar/bar_test.py",
"foo/bar/bar_test.py",
],
main = "__test__.py",
)
2 changes: 2 additions & 0 deletions 2 gazelle/python/testdata/project_generation_mode/BUILD.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# gazelle:python_extension enabled
# gazelle:python_generation_mode project
14 changes: 14 additions & 0 deletions 14 gazelle/python/testdata/project_generation_mode/BUILD.out
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__"],
)
3 changes: 3 additions & 0 deletions 3 gazelle/python/testdata/project_generation_mode/README.md
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.
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 gazelle/python/testdata/project_generation_mode/test.yaml
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.

---
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# gazelle:python_extension enabled
# gazelle:python_generation_mode project
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",
)
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.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# This is a Bazel workspace for the Gazelle test data.
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.

---
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# gazelle:python_extension enabled
# gazelle:python_generation_mode project
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"],
)
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`).
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.
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.

---
Morty Proxy This is a proxified and sanitized view of the page, visit original site.