Skip to content

Commit

Permalink
feat(gazelle): allow per-file py_test generation
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 works for py_test targets
as well. The change is careful to not affect any existing tests, and
new tests have been added to check the new functionality.
  • Loading branch information
adzenith committed Nov 15, 2023
1 parent d96214f commit 02b81ef
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ 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.

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.
11 changes: 10 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,12 @@ py_library(
srcs = ["foo.py"],
visibility = ["//:__subpackages__"],
)

py_test(
name = "bar_test",
srcs = [
"__test__.py",
"bar_test.py",
],
main = "__test__.py",
)
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__",
)
15 changes: 15 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,15 @@
load("@rules_python//python:defs.bzl", "py_test")

some_target(
name = "__test__",
)

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

0 comments on commit 02b81ef

Please sign in to comment.