@@ -69,10 +69,10 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
69
69
// this package or not.
70
70
hasPyBinary := false
71
71
72
- // hasPyTestFile and hasPyTestTarget control whether a py_test target should
72
+ // hasPyTestEntryPointFile and hasPyTestEntryPointTarget control whether a py_test target should
73
73
// be generated for this package or not.
74
- hasPyTestFile := false
75
- hasPyTestTarget := false
74
+ hasPyTestEntryPointFile := false
75
+ hasPyTestEntryPointTarget := false
76
76
hasConftestFile := false
77
77
78
78
for _ , f := range args .RegularFiles {
@@ -82,8 +82,8 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
82
82
ext := filepath .Ext (f )
83
83
if ! hasPyBinary && f == pyBinaryEntrypointFilename {
84
84
hasPyBinary = true
85
- } else if ! hasPyTestFile && f == pyTestEntrypointFilename {
86
- hasPyTestFile = true
85
+ } else if ! hasPyTestEntryPointFile && f == pyTestEntrypointFilename {
86
+ hasPyTestEntryPointFile = true
87
87
} else if f == conftestFilename {
88
88
hasConftestFile = true
89
89
} 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
95
95
96
96
// If a __test__.py file was not found on disk, search for targets that are
97
97
// named __test__.
98
- if ! hasPyTestFile && args .File != nil {
98
+ if ! hasPyTestEntryPointFile && args .File != nil {
99
99
for _ , rule := range args .File .Rules {
100
100
if rule .Name () == pyTestEntrypointTargetname {
101
- hasPyTestTarget = true
101
+ hasPyTestEntryPointTarget = true
102
102
break
103
103
}
104
104
}
@@ -185,13 +185,6 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
185
185
186
186
collisionErrors := singlylinkedlist .New ()
187
187
188
- if ! hasPyTestFile && ! hasPyTestTarget {
189
- it := pyTestFilenames .Iterator ()
190
- for it .Next () {
191
- pyLibraryFilenames .Add (it .Value ())
192
- }
193
- }
194
-
195
188
var pyLibrary * rule.Rule
196
189
if ! pyLibraryFilenames .Empty () {
197
190
deps , err := parser .parse (pyLibraryFilenames )
@@ -309,19 +302,12 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
309
302
result .Imports = append (result .Imports , conftest .PrivateAttr (config .GazelleImportsKey ))
310
303
}
311
304
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 {
318
307
deps , err := parser .parse (pyTestFilenames )
319
308
if err != nil {
320
309
log .Fatalf ("ERROR: %v\n " , err )
321
310
}
322
-
323
- pyTestTargetName := cfg .RenderTestName (packageName )
324
-
325
311
// Check if a target with the same name we are generating already
326
312
// exists, and if it is of a different kind from the one we are
327
313
// 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
338
324
}
339
325
}
340
326
}
341
-
342
- pyTestTarget := newTargetBuilder (pyTestKind , pyTestTargetName , pythonProjectRoot , args .Rel ).
327
+ return newTargetBuilder (pyTestKind , pyTestTargetName , pythonProjectRoot , args .Rel ).
343
328
addSrcs (pyTestFilenames ).
344
329
addModuleDependencies (deps ).
345
330
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 )
346
340
347
- if hasPyTestTarget {
341
+ if hasPyTestEntryPointTarget {
348
342
entrypointTarget := fmt .Sprintf (":%s" , pyTestEntrypointTargetname )
349
343
main := fmt .Sprintf (":%s" , pyTestEntrypointFilename )
350
344
pyTestTarget .
@@ -354,7 +348,17 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
354
348
} else {
355
349
pyTestTarget .setMain (pyTestEntrypointFilename )
356
350
}
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
+ }
357
360
361
+ for _ , pyTestTarget := range pyTestTargets {
358
362
if pyLibrary != nil {
359
363
pyTestTarget .addModuleDependency (module {Name : pyLibrary .PrivateAttr (uuidKey ).(string )})
360
364
}
0 commit comments