Skip to content

Commit 0943375

Browse files
authored
generate py_test without __test__ (#999)
1 parent 1d283fc commit 0943375

File tree

4 files changed

+47
-33
lines changed

4 files changed

+47
-33
lines changed

gazelle/generate.go

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
6969
// this package or not.
7070
hasPyBinary := false
7171

72-
// hasPyTestFile and hasPyTestTarget control whether a py_test target should
72+
// hasPyTestEntryPointFile and hasPyTestEntryPointTarget control whether a py_test target should
7373
// be generated for this package or not.
74-
hasPyTestFile := false
75-
hasPyTestTarget := false
74+
hasPyTestEntryPointFile := false
75+
hasPyTestEntryPointTarget := false
7676
hasConftestFile := false
7777

7878
for _, f := range args.RegularFiles {
@@ -82,8 +82,8 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
8282
ext := filepath.Ext(f)
8383
if !hasPyBinary && f == pyBinaryEntrypointFilename {
8484
hasPyBinary = true
85-
} else if !hasPyTestFile && f == pyTestEntrypointFilename {
86-
hasPyTestFile = true
85+
} else if !hasPyTestEntryPointFile && f == pyTestEntrypointFilename {
86+
hasPyTestEntryPointFile = true
8787
} else if f == conftestFilename {
8888
hasConftestFile = true
8989
} else if strings.HasSuffix(f, "_test.py") || (strings.HasPrefix(f, "test_") && ext == ".py") {
@@ -95,10 +95,10 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
9595

9696
// If a __test__.py file was not found on disk, search for targets that are
9797
// named __test__.
98-
if !hasPyTestFile && args.File != nil {
98+
if !hasPyTestEntryPointFile && args.File != nil {
9999
for _, rule := range args.File.Rules {
100100
if rule.Name() == pyTestEntrypointTargetname {
101-
hasPyTestTarget = true
101+
hasPyTestEntryPointTarget = true
102102
break
103103
}
104104
}
@@ -185,13 +185,6 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
185185

186186
collisionErrors := singlylinkedlist.New()
187187

188-
if !hasPyTestFile && !hasPyTestTarget {
189-
it := pyTestFilenames.Iterator()
190-
for it.Next() {
191-
pyLibraryFilenames.Add(it.Value())
192-
}
193-
}
194-
195188
var pyLibrary *rule.Rule
196189
if !pyLibraryFilenames.Empty() {
197190
deps, err := parser.parse(pyLibraryFilenames)
@@ -309,19 +302,12 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
309302
result.Imports = append(result.Imports, conftest.PrivateAttr(config.GazelleImportsKey))
310303
}
311304

312-
if hasPyTestFile || hasPyTestTarget {
313-
if hasPyTestFile {
314-
// Only add the pyTestEntrypointFilename to the pyTestFilenames if
315-
// the file exists on disk.
316-
pyTestFilenames.Add(pyTestEntrypointFilename)
317-
}
305+
var pyTestTargets []*targetBuilder
306+
newPyTestTargetBuilder := func(pyTestFilenames *treeset.Set, pyTestTargetName string) *targetBuilder {
318307
deps, err := parser.parse(pyTestFilenames)
319308
if err != nil {
320309
log.Fatalf("ERROR: %v\n", err)
321310
}
322-
323-
pyTestTargetName := cfg.RenderTestName(packageName)
324-
325311
// Check if a target with the same name we are generating already
326312
// exists, and if it is of a different kind from the one we are
327313
// generating. If so, we have to throw an error since Gazelle won't
@@ -338,13 +324,21 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
338324
}
339325
}
340326
}
341-
342-
pyTestTarget := newTargetBuilder(pyTestKind, pyTestTargetName, pythonProjectRoot, args.Rel).
327+
return newTargetBuilder(pyTestKind, pyTestTargetName, pythonProjectRoot, args.Rel).
343328
addSrcs(pyTestFilenames).
344329
addModuleDependencies(deps).
345330
generateImportsAttribute()
331+
}
332+
if hasPyTestEntryPointFile || hasPyTestEntryPointTarget {
333+
if hasPyTestEntryPointFile {
334+
// Only add the pyTestEntrypointFilename to the pyTestFilenames if
335+
// the file exists on disk.
336+
pyTestFilenames.Add(pyTestEntrypointFilename)
337+
}
338+
pyTestTargetName := cfg.RenderTestName(packageName)
339+
pyTestTarget := newPyTestTargetBuilder(pyTestFilenames, pyTestTargetName)
346340

347-
if hasPyTestTarget {
341+
if hasPyTestEntryPointTarget {
348342
entrypointTarget := fmt.Sprintf(":%s", pyTestEntrypointTargetname)
349343
main := fmt.Sprintf(":%s", pyTestEntrypointFilename)
350344
pyTestTarget.
@@ -354,7 +348,17 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
354348
} else {
355349
pyTestTarget.setMain(pyTestEntrypointFilename)
356350
}
351+
pyTestTargets = append(pyTestTargets, pyTestTarget)
352+
} else {
353+
// Create one py_test target per file
354+
pyTestFilenames.Each(func(index int, testFile interface{}) {
355+
srcs := treeset.NewWith(godsutils.StringComparator, testFile)
356+
pyTestTargetName := strings.TrimSuffix(filepath.Base(testFile.(string)), ".py")
357+
pyTestTargets = append(pyTestTargets, newPyTestTargetBuilder(srcs, pyTestTargetName))
358+
})
359+
}
357360

361+
for _, pyTestTarget := range pyTestTargets {
358362
if pyLibrary != nil {
359363
pyTestTarget.addModuleDependency(module{Name: pyLibrary.PrivateAttr(uuidKey).(string)})
360364
}
Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
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 = "python_target_with_test_in_name",
5-
srcs = [
6-
"__init__.py",
7-
"not_a_real_test.py",
8-
"test_not_a_real.py",
9-
],
5+
srcs = ["__init__.py"],
106
visibility = ["//:__subpackages__"],
11-
deps = ["@gazelle_python_test_boto3//:pkg"],
7+
)
8+
9+
py_test(
10+
name = "real_test",
11+
srcs = ["real_test.py"],
12+
deps = [
13+
":python_target_with_test_in_name",
14+
"@gazelle_python_test_boto3//:pkg",
15+
],
16+
)
17+
18+
py_test(
19+
name = "test_reality",
20+
srcs = ["test_reality.py"],
21+
deps = [":python_target_with_test_in_name"],
1222
)

0 commit comments

Comments
 (0)