diff --git a/CHANGELOG.md b/CHANGELOG.md index 0df0de8cee..b0df94c037 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -66,6 +66,8 @@ END_UNRELEASED_TEMPLATE ### Fixed * (tests) No more coverage warnings are being printed if there are no sources. ([#2762](https://github.com/bazel-contrib/rules_python/issues/2762)) +* (gazelle) Ancestor `conftest.py` files are added in addition to sibling `conftest.py`. + ([#3497](https://github.com/bazel-contrib/rules_python/issues/3497)) {#v0-0-0-added} ### Added diff --git a/gazelle/python/generate.go b/gazelle/python/generate.go index cbceea4693..49e9921fa9 100644 --- a/gazelle/python/generate.go +++ b/gazelle/python/generate.go @@ -66,6 +66,24 @@ func matchesAnyGlob(s string, globs []string) bool { return false } +// findConftestPaths returns package paths containing conftest.py, from currentPkg +// up through ancestors, stopping at pythonProjectRoot (or repo root if empty). +func findConftestPaths(repoRoot, currentPkg, pythonProjectRoot string) []string { + var result []string + for pkg := currentPkg; ; pkg = filepath.Dir(pkg) { + if pkg == "." { + pkg = "" + } + if _, err := os.Stat(filepath.Join(repoRoot, pkg, conftestFilename)); err == nil { + result = append(result, pkg) + } + if pkg == "" || pkg == pythonProjectRoot { + break + } + } + return result +} + // GenerateRules extracts build metadata from source files in a directory. // GenerateRules is called in each directory where an update is requested // in depth-first post-order. @@ -481,14 +499,15 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes } for _, pyTestTarget := range pyTestTargets { - if conftest != nil { - conftestModule := Module{Name: importSpecFromSrc(pythonProjectRoot, args.Rel, conftestFilename).Imp} - if pyTestTarget.annotations.includePytestConftest == nil { - // unset; default behavior - pyTestTarget.addModuleDependency(conftestModule) - } else if *pyTestTarget.annotations.includePytestConftest { - // set; add if true, do not add if false - pyTestTarget.addModuleDependency(conftestModule) + shouldAddConftest := pyTestTarget.annotations.includePytestConftest == nil || + *pyTestTarget.annotations.includePytestConftest + + if shouldAddConftest { + for _, conftestPkg := range findConftestPaths(args.Config.RepoRoot, args.Rel, pythonProjectRoot) { + conftestModule := Module{ + Name: importSpecFromSrc(pythonProjectRoot, conftestPkg, conftestFilename).Imp, + } + pyTestTarget.deps.Add(conftestModule) } } pyTest := pyTestTarget.build() diff --git a/gazelle/python/testdata/simple_test_with_conftest/bar/BUILD.out b/gazelle/python/testdata/simple_test_with_conftest/bar/BUILD.out index 4a1204e989..9b500d4733 100644 --- a/gazelle/python/testdata/simple_test_with_conftest/bar/BUILD.out +++ b/gazelle/python/testdata/simple_test_with_conftest/bar/BUILD.out @@ -23,5 +23,6 @@ py_test( deps = [ ":bar", ":conftest", + "//:conftest", ], ) diff --git a/gazelle/python/testdata/simple_test_with_conftest_sibling_imports_disabled/bar/BUILD.out b/gazelle/python/testdata/simple_test_with_conftest_sibling_imports_disabled/bar/BUILD.out index ef8591f199..25a2d39672 100644 --- a/gazelle/python/testdata/simple_test_with_conftest_sibling_imports_disabled/bar/BUILD.out +++ b/gazelle/python/testdata/simple_test_with_conftest_sibling_imports_disabled/bar/BUILD.out @@ -22,6 +22,7 @@ py_test( main = "__test__.py", deps = [ ":conftest", + "//:conftest", "//:simple_test_with_conftest_sibling_imports_disabled", ], )