Skip to content

Commit 2c82656

Browse files
authored
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.
1 parent d38100c commit 2c82656

File tree

13 files changed

+79
-4
lines changed

13 files changed

+79
-4
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ A brief description of the categories of changes:
6464
* (gazelle) Use relative paths if possible for dependencies added through
6565
the use of the `resolve` directive.
6666

67+
* (gazelle) When using `python_generation_mode file`, one `py_test` target is
68+
made per test file even if a target named `__test__` or a file named
69+
`__test__.py` exists in the same package. Previously in these cases there
70+
would only be one test target made.
71+
6772
Breaking changes:
6873

6974
* (pip) `pip_install` repository rule in this release has been disabled and

gazelle/python/generate.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,8 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
371371
addModuleDependencies(deps).
372372
generateImportsAttribute()
373373
}
374-
if hasPyTestEntryPointFile || hasPyTestEntryPointTarget || cfg.CoarseGrainedGeneration() {
374+
if (hasPyTestEntryPointFile || hasPyTestEntryPointTarget || cfg.CoarseGrainedGeneration()) && !cfg.PerFileGeneration() {
375+
// Create one py_test target per package
375376
if hasPyTestEntryPointFile {
376377
// Only add the pyTestEntrypointFilename to the pyTestFilenames if
377378
// the file exists on disk.
@@ -396,7 +397,20 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
396397
pyTestFilenames.Each(func(index int, testFile interface{}) {
397398
srcs := treeset.NewWith(godsutils.StringComparator, testFile)
398399
pyTestTargetName := strings.TrimSuffix(filepath.Base(testFile.(string)), ".py")
399-
pyTestTargets = append(pyTestTargets, newPyTestTargetBuilder(srcs, pyTestTargetName))
400+
pyTestTarget := newPyTestTargetBuilder(srcs, pyTestTargetName)
401+
402+
if hasPyTestEntryPointTarget {
403+
entrypointTarget := fmt.Sprintf(":%s", pyTestEntrypointTargetname)
404+
main := fmt.Sprintf(":%s", pyTestEntrypointFilename)
405+
pyTestTarget.
406+
addSrc(entrypointTarget).
407+
addResolvedDependency(entrypointTarget).
408+
setMain(main)
409+
} else if hasPyTestEntryPointFile {
410+
pyTestTarget.addSrc(pyTestEntrypointFilename)
411+
pyTestTarget.setMain(pyTestEntrypointFilename)
412+
}
413+
pyTestTargets = append(pyTestTargets, pyTestTarget)
400414
})
401415
}
402416

gazelle/python/testdata/per_file/BUILD.out

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load("@rules_python//python:defs.bzl", "py_library")
1+
load("@rules_python//python:defs.bzl", "py_library", "py_test")
22

33
# gazelle:python_generation_mode file
44

@@ -22,3 +22,13 @@ py_library(
2222
visibility = ["//:__subpackages__"],
2323
deps = [":custom"],
2424
)
25+
26+
py_test(
27+
name = "bar_test",
28+
srcs = ["bar_test.py"],
29+
)
30+
31+
py_test(
32+
name = "foo_test",
33+
srcs = ["foo_test.py"],
34+
)

gazelle/python/testdata/per_file/bar_test.py

Whitespace-only changes.

gazelle/python/testdata/per_file/foo_test.py

Whitespace-only changes.

gazelle/python/testdata/per_file_subdirs/bar/BUILD.out

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load("@rules_python//python:defs.bzl", "py_library")
1+
load("@rules_python//python:defs.bzl", "py_library", "py_test")
22

33
py_library(
44
name = "__init__",
@@ -11,3 +11,21 @@ py_library(
1111
srcs = ["foo.py"],
1212
visibility = ["//:__subpackages__"],
1313
)
14+
15+
py_test(
16+
name = "bar_test",
17+
srcs = [
18+
"__test__.py",
19+
"bar_test.py",
20+
],
21+
main = "__test__.py",
22+
)
23+
24+
py_test(
25+
name = "foo_test",
26+
srcs = [
27+
"__test__.py",
28+
"foo_test.py",
29+
],
30+
main = "__test__.py",
31+
)

gazelle/python/testdata/per_file_subdirs/bar/__test__.py

Whitespace-only changes.

gazelle/python/testdata/per_file_subdirs/bar/bar_test.py

Whitespace-only changes.

gazelle/python/testdata/per_file_subdirs/bar/foo_test.py

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
some_target(
2+
name = "__test__",
3+
)

0 commit comments

Comments
 (0)