@@ -26,6 +26,8 @@ const (
26
26
pyBinaryEntrypointFilename = "__main__.py"
27
27
pyTestEntrypointFilename = "__test__.py"
28
28
pyTestEntrypointTargetname = "__test__"
29
+ conftestFilename = "conftest.py"
30
+ conftestTargetname = "conftest"
29
31
)
30
32
31
33
var (
@@ -71,6 +73,7 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
71
73
// be generated for this package or not.
72
74
hasPyTestFile := false
73
75
hasPyTestTarget := false
76
+ hasConftestFile := false
74
77
75
78
for _ , f := range args .RegularFiles {
76
79
if cfg .IgnoresFile (filepath .Base (f )) {
@@ -81,6 +84,8 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
81
84
hasPyBinary = true
82
85
} else if ! hasPyTestFile && f == pyTestEntrypointFilename {
83
86
hasPyTestFile = true
87
+ } else if f == conftestFilename {
88
+ hasConftestFile = true
84
89
} else if strings .HasSuffix (f , "_test.py" ) || (strings .HasPrefix (f , "test_" ) && ext == ".py" ) {
85
90
pyTestFilenames .Add (f )
86
91
} else if ext == ".py" {
@@ -196,10 +201,10 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
196
201
197
202
pyLibraryTargetName := cfg .RenderLibraryName (packageName )
198
203
199
- // Check if a target with the same name we are generating alredy exists,
200
- // and if it is of a different kind from the one we are generating. If
201
- // so, we have to throw an error since Gazelle won't generate it
202
- // correctly.
204
+ // Check if a target with the same name we are generating already
205
+ // exists, and if it is of a different kind from the one we are
206
+ // generating. If so, we have to throw an error since Gazelle won't
207
+ // generate it correctly.
203
208
if args .File != nil {
204
209
for _ , t := range args .File .Rules {
205
210
if t .Name () == pyLibraryTargetName && t .Kind () != pyLibraryKind {
@@ -233,10 +238,10 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
233
238
234
239
pyBinaryTargetName := cfg .RenderBinaryName (packageName )
235
240
236
- // Check if a target with the same name we are generating alredy exists,
237
- // and if it is of a different kind from the one we are generating. If
238
- // so, we have to throw an error since Gazelle won't generate it
239
- // correctly.
241
+ // Check if a target with the same name we are generating already
242
+ // exists, and if it is of a different kind from the one we are
243
+ // generating. If so, we have to throw an error since Gazelle won't
244
+ // generate it correctly.
240
245
if args .File != nil {
241
246
for _ , t := range args .File .Rules {
242
247
if t .Name () == pyBinaryTargetName && t .Kind () != pyBinaryKind {
@@ -267,6 +272,43 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
267
272
result .Imports = append (result .Imports , pyBinary .PrivateAttr (config .GazelleImportsKey ))
268
273
}
269
274
275
+ var conftest * rule.Rule
276
+ if hasConftestFile {
277
+ deps , err := parser .parseSingle (conftestFilename )
278
+ if err != nil {
279
+ log .Fatalf ("ERROR: %v\n " , err )
280
+ }
281
+
282
+ // Check if a target with the same name we are generating already
283
+ // exists, and if it is of a different kind from the one we are
284
+ // generating. If so, we have to throw an error since Gazelle won't
285
+ // generate it correctly.
286
+ if args .File != nil {
287
+ for _ , t := range args .File .Rules {
288
+ if t .Name () == conftestTargetname && t .Kind () != pyLibraryKind {
289
+ fqTarget := label .New ("" , args .Rel , conftestTargetname )
290
+ err := fmt .Errorf ("failed to generate target %q of kind %q: " +
291
+ "a target of kind %q with the same name already exists." ,
292
+ fqTarget .String (), pyLibraryKind , t .Kind ())
293
+ collisionErrors .Add (err )
294
+ }
295
+ }
296
+ }
297
+
298
+ conftestTarget := newTargetBuilder (pyLibraryKind , conftestTargetname , pythonProjectRoot , args .Rel ).
299
+ setUUID (uuid .Must (uuid .NewUUID ()).String ()).
300
+ addSrc (conftestFilename ).
301
+ addModuleDependencies (deps ).
302
+ addVisibility (visibility ).
303
+ setTestonly ().
304
+ generateImportsAttribute ()
305
+
306
+ conftest = conftestTarget .build ()
307
+
308
+ result .Gen = append (result .Gen , conftest )
309
+ result .Imports = append (result .Imports , conftest .PrivateAttr (config .GazelleImportsKey ))
310
+ }
311
+
270
312
if hasPyTestFile || hasPyTestTarget {
271
313
if hasPyTestFile {
272
314
// Only add the pyTestEntrypointFilename to the pyTestFilenames if
@@ -280,10 +322,10 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
280
322
281
323
pyTestTargetName := cfg .RenderTestName (packageName )
282
324
283
- // Check if a target with the same name we are generating alredy exists,
284
- // and if it is of a different kind from the one we are generating. If
285
- // so, we have to throw an error since Gazelle won't generate it
286
- // correctly.
325
+ // Check if a target with the same name we are generating already
326
+ // exists, and if it is of a different kind from the one we are
327
+ // generating. If so, we have to throw an error since Gazelle won't
328
+ // generate it correctly.
287
329
if args .File != nil {
288
330
for _ , t := range args .File .Rules {
289
331
if t .Name () == pyTestTargetName && t .Kind () != pyTestKind {
@@ -317,6 +359,10 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
317
359
pyTestTarget .addModuleDependency (module {Name : pyLibrary .PrivateAttr (uuidKey ).(string )})
318
360
}
319
361
362
+ if conftest != nil {
363
+ pyTestTarget .addModuleDependency (module {Name : conftest .PrivateAttr (uuidKey ).(string )})
364
+ }
365
+
320
366
pyTest := pyTestTarget .build ()
321
367
322
368
result .Gen = append (result .Gen , pyTest )
0 commit comments