fix(gazelle): generate a single `py_test` for coarse_grained setups (#1538)
With a recent change to generate a single test target per python file
we introduced a regression for projects using
`# gazelle:python_generation_mode project` configuration directive
if there are multiple files with the same filenames but under different
directories within the source tree. This PR fixes the behaviour so that
we just generate a single target containing all test files as there is
no sure way to denormalize the paths so that they never clash.
Fixes #1442.
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ebbde9a..ab4c4d2 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -75,6 +75,9 @@
* (py_wheel) Produce deterministic wheel files and make `RECORD` file entries
follow the order of files written to the `.whl` archive.
+* (gazelle) Generate a single `py_test` target when `gazelle:python_generation_mode project`
+ is used.
+
### Added
* (bzlmod) Added `.whl` patching support via `patches` and `patch_strip`
diff --git a/gazelle/python/generate.go b/gazelle/python/generate.go
index ede4d2a..0e47ed7 100644
--- a/gazelle/python/generate.go
+++ b/gazelle/python/generate.go
@@ -371,7 +371,7 @@
addModuleDependencies(deps).
generateImportsAttribute()
}
- if hasPyTestEntryPointFile || hasPyTestEntryPointTarget {
+ if hasPyTestEntryPointFile || hasPyTestEntryPointTarget || cfg.CoarseGrainedGeneration() {
if hasPyTestEntryPointFile {
// Only add the pyTestEntrypointFilename to the pyTestFilenames if
// the file exists on disk.
diff --git a/gazelle/python/testdata/monorepo/coarse_grained/BUILD.out b/gazelle/python/testdata/monorepo/coarse_grained/BUILD.out
index 0357705..3a33111 100644
--- a/gazelle/python/testdata/monorepo/coarse_grained/BUILD.out
+++ b/gazelle/python/testdata/monorepo/coarse_grained/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_extension enabled
# gazelle:python_root
@@ -18,3 +18,12 @@
visibility = ["//:__subpackages__"],
deps = ["@root_pip_deps//rootboto3"],
)
+
+py_test(
+ name = "coarse_grained_test",
+ srcs = [
+ "bar/bar_test.py",
+ "foo/bar/bar_test.py",
+ ],
+ main = "__test__.py",
+)
diff --git a/gazelle/python/testdata/monorepo/coarse_grained/bar/bar_test.py b/gazelle/python/testdata/monorepo/coarse_grained/bar/bar_test.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/gazelle/python/testdata/monorepo/coarse_grained/bar/bar_test.py
diff --git a/gazelle/python/testdata/monorepo/coarse_grained/foo/bar/bar_test.py b/gazelle/python/testdata/monorepo/coarse_grained/foo/bar/bar_test.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/gazelle/python/testdata/monorepo/coarse_grained/foo/bar/bar_test.py