Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 27 additions & 8 deletions gazelle/python/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ py_test(
deps = [
":bar",
":conftest",
"//:conftest",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ py_test(
main = "__test__.py",
deps = [
":conftest",
"//:conftest",
"//:simple_test_with_conftest_sibling_imports_disabled",
],
)