Skip to content

Commit

Permalink
feat(gazelle): allow per-file py_test generation (#1563)
Browse files Browse the repository at this point in the history
Previously the per-file target generation only worked for py_library
targets. This change makes it so that this feature works for py_test
targets as well.
The change is careful to not affect any existing tests, so I'm not sure
if it should count as a breaking change. New tests have been added to
check the new functionality.
  • Loading branch information
adzenith authored Nov 16, 2023
1 parent d38100c commit 2c82656
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,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",
"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.
3 changes: 3 additions & 0 deletions gazelle/python/testdata/per_file_subdirs/test_target/BUILD.in
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(
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.

0 comments on commit 2c82656

Please sign in to comment.