Skip to content
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

feat(gazelle): allow per-file py_test generation #1563

Merged
merged 1 commit into from
Nov 16, 2023
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ A brief description of the categories of changes:
* (gazelle) Use relative paths if possible for dependencies added through
the use of the `resolve` directive.

* (gazelle) When using `python_generation_mode file`, one `py_test` target is
made per test file even if a target named `__test__` or a file named
`__test__.py` exists in the same package. Previously in these cases there
would only be one test target made.

Breaking changes:

* (pip) `pip_install` repository rule in this release has been disabled and
Expand Down
18 changes: 16 additions & 2 deletions gazelle/python/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,8 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
addModuleDependencies(deps).
generateImportsAttribute()
}
if hasPyTestEntryPointFile || hasPyTestEntryPointTarget || cfg.CoarseGrainedGeneration() {
if (hasPyTestEntryPointFile || hasPyTestEntryPointTarget || cfg.CoarseGrainedGeneration()) && !cfg.PerFileGeneration() {
// Create one py_test target per package
if hasPyTestEntryPointFile {
// Only add the pyTestEntrypointFilename to the pyTestFilenames if
// the file exists on disk.
Expand All @@ -396,7 +397,20 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
pyTestFilenames.Each(func(index int, testFile interface{}) {
srcs := treeset.NewWith(godsutils.StringComparator, testFile)
pyTestTargetName := strings.TrimSuffix(filepath.Base(testFile.(string)), ".py")
pyTestTargets = append(pyTestTargets, newPyTestTargetBuilder(srcs, pyTestTargetName))
pyTestTarget := newPyTestTargetBuilder(srcs, pyTestTargetName)

if hasPyTestEntryPointTarget {
entrypointTarget := fmt.Sprintf(":%s", pyTestEntrypointTargetname)
main := fmt.Sprintf(":%s", pyTestEntrypointFilename)
pyTestTarget.
addSrc(entrypointTarget).
addResolvedDependency(entrypointTarget).
setMain(main)
} else if hasPyTestEntryPointFile {
pyTestTarget.addSrc(pyTestEntrypointFilename)
pyTestTarget.setMain(pyTestEntrypointFilename)
}
pyTestTargets = append(pyTestTargets, pyTestTarget)
})
}

Expand Down
12 changes: 11 additions & 1 deletion gazelle/python/testdata/per_file/BUILD.out
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@rules_python//python:defs.bzl", "py_library")
load("@rules_python//python:defs.bzl", "py_library", "py_test")

# gazelle:python_generation_mode file

Expand All @@ -22,3 +22,13 @@ py_library(
visibility = ["//:__subpackages__"],
deps = [":custom"],
)

py_test(
name = "bar_test",
srcs = ["bar_test.py"],
)

py_test(
name = "foo_test",
srcs = ["foo_test.py"],
)
Empty file.
Empty file.
20 changes: 19 additions & 1 deletion gazelle/python/testdata/per_file_subdirs/bar/BUILD.out
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@rules_python//python:defs.bzl", "py_library")
load("@rules_python//python:defs.bzl", "py_library", "py_test")

py_library(
name = "__init__",
Expand All @@ -11,3 +11,21 @@ py_library(
srcs = ["foo.py"],
visibility = ["//:__subpackages__"],
)

py_test(
name = "bar_test",
srcs = [
"__test__.py",
aignas marked this conversation as resolved.
Show resolved Hide resolved
"bar_test.py",
],
main = "__test__.py",
)

py_test(
name = "foo_test",
srcs = [
"__test__.py",
"foo_test.py",
],
main = "__test__.py",
)
Empty file.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
some_target(
name = "__test__",
)
25 changes: 25 additions & 0 deletions gazelle/python/testdata/per_file_subdirs/test_target/BUILD.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
load("@rules_python//python:defs.bzl", "py_test")

some_target(
name = "__test__",
)

py_test(
aignas marked this conversation as resolved.
Show resolved Hide resolved
name = "a_test",
srcs = [
"a_test.py",
":__test__",
],
main = ":__test__.py",
deps = [":__test__"],
)

py_test(
name = "b_test",
srcs = [
"b_test.py",
":__test__",
],
main = ":__test__.py",
deps = [":__test__"],
)
Empty file.
Empty file.