feat(gazelle): allow per-file py_test generation (#1563)
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.
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b755258..5ac2a3f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -64,6 +64,11 @@
* (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
diff --git a/gazelle/python/generate.go b/gazelle/python/generate.go
index 0e47ed7..25fb194 100644
--- a/gazelle/python/generate.go
+++ b/gazelle/python/generate.go
@@ -371,7 +371,8 @@
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.
@@ -396,7 +397,20 @@
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)
})
}
diff --git a/gazelle/python/testdata/per_file/BUILD.out b/gazelle/python/testdata/per_file/BUILD.out
index 2ec825b..6deada8 100644
--- a/gazelle/python/testdata/per_file/BUILD.out
+++ b/gazelle/python/testdata/per_file/BUILD.out
@@ -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
@@ -22,3 +22,13 @@
visibility = ["//:__subpackages__"],
deps = [":custom"],
)
+
+py_test(
+ name = "bar_test",
+ srcs = ["bar_test.py"],
+)
+
+py_test(
+ name = "foo_test",
+ srcs = ["foo_test.py"],
+)
diff --git a/gazelle/python/testdata/per_file/bar_test.py b/gazelle/python/testdata/per_file/bar_test.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/gazelle/python/testdata/per_file/bar_test.py
diff --git a/gazelle/python/testdata/per_file/foo_test.py b/gazelle/python/testdata/per_file/foo_test.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/gazelle/python/testdata/per_file/foo_test.py
diff --git a/gazelle/python/testdata/per_file_subdirs/bar/BUILD.out b/gazelle/python/testdata/per_file_subdirs/bar/BUILD.out
index 7258d27..4da8d9c 100644
--- a/gazelle/python/testdata/per_file_subdirs/bar/BUILD.out
+++ b/gazelle/python/testdata/per_file_subdirs/bar/BUILD.out
@@ -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__",
@@ -11,3 +11,21 @@
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",
+)
diff --git a/gazelle/python/testdata/per_file_subdirs/bar/__test__.py b/gazelle/python/testdata/per_file_subdirs/bar/__test__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/gazelle/python/testdata/per_file_subdirs/bar/__test__.py
diff --git a/gazelle/python/testdata/per_file_subdirs/bar/bar_test.py b/gazelle/python/testdata/per_file_subdirs/bar/bar_test.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/gazelle/python/testdata/per_file_subdirs/bar/bar_test.py
diff --git a/gazelle/python/testdata/per_file_subdirs/bar/foo_test.py b/gazelle/python/testdata/per_file_subdirs/bar/foo_test.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/gazelle/python/testdata/per_file_subdirs/bar/foo_test.py
diff --git a/gazelle/python/testdata/per_file_subdirs/test_target/BUILD.in b/gazelle/python/testdata/per_file_subdirs/test_target/BUILD.in
new file mode 100644
index 0000000..b5733da
--- /dev/null
+++ b/gazelle/python/testdata/per_file_subdirs/test_target/BUILD.in
@@ -0,0 +1,3 @@
+some_target(
+ name = "__test__",
+)
diff --git a/gazelle/python/testdata/per_file_subdirs/test_target/BUILD.out b/gazelle/python/testdata/per_file_subdirs/test_target/BUILD.out
new file mode 100644
index 0000000..f4a9236
--- /dev/null
+++ b/gazelle/python/testdata/per_file_subdirs/test_target/BUILD.out
@@ -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__"],
+)
diff --git a/gazelle/python/testdata/per_file_subdirs/test_target/a_test.py b/gazelle/python/testdata/per_file_subdirs/test_target/a_test.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/gazelle/python/testdata/per_file_subdirs/test_target/a_test.py
diff --git a/gazelle/python/testdata/per_file_subdirs/test_target/b_test.py b/gazelle/python/testdata/per_file_subdirs/test_target/b_test.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/gazelle/python/testdata/per_file_subdirs/test_target/b_test.py