@@ -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 }
0 commit comments