@@ -66,6 +66,24 @@ func matchesAnyGlob(s string, globs []string) bool {
6666 return false
6767}
6868
69+ // findConftestPaths returns package paths containing conftest.py, from currentPkg
70+ // up through ancestors, stopping at module root.
71+ func findConftestPaths (repoRoot , currentPkg , pythonProjectRoot string ) []string {
72+ var result []string
73+ for pkg := currentPkg ; ; pkg = filepath .Dir (pkg ) {
74+ if pkg == "." {
75+ pkg = ""
76+ }
77+ if _ , err := os .Stat (filepath .Join (repoRoot , pkg , conftestFilename )); err == nil {
78+ result = append (result , pkg )
79+ }
80+ if pkg == "" {
81+ break
82+ }
83+ }
84+ return result
85+ }
86+
6987// GenerateRules extracts build metadata from source files in a directory.
7088// GenerateRules is called in each directory where an update is requested
7189// in depth-first post-order.
@@ -481,14 +499,17 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
481499 }
482500
483501 for _ , pyTestTarget := range pyTestTargets {
484- if conftest != nil {
485- conftestModule := Module {Name : importSpecFromSrc (pythonProjectRoot , args .Rel , conftestFilename ).Imp }
486- if pyTestTarget .annotations .includePytestConftest == nil {
487- // unset; default behavior
488- pyTestTarget .addModuleDependency (conftestModule )
489- } else if * pyTestTarget .annotations .includePytestConftest {
490- // set; add if true, do not add if false
491- pyTestTarget .addModuleDependency (conftestModule )
502+ shouldAddConftest := pyTestTarget .annotations .includePytestConftest == nil ||
503+ * pyTestTarget .annotations .includePytestConftest
504+
505+ if shouldAddConftest {
506+ for _ , conftestPkg := range findConftestPaths (args .Config .RepoRoot , args .Rel , pythonProjectRoot ) {
507+ pyTestTarget .addModuleDependency (
508+ Module {
509+ Name : importSpecFromSrc (pythonProjectRoot , conftestPkg , conftestFilename ).Imp ,
510+ Filepath : filepath .Join (conftestPkg , conftestFilename ),
511+ },
512+ )
492513 }
493514 }
494515 pyTest := pyTestTarget .build ()
0 commit comments