Skip to content

Commit b47b92b

Browse files
authored
feat(gazelle): Add ancestor conftest.py files (#3498)
Re-introduces the gazelle behavior of adding conftest targets to tests so that they inherit parent directory configs. Fixes #3497
1 parent 42085b5 commit b47b92b

File tree

4 files changed

+33
-8
lines changed

4 files changed

+33
-8
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ END_UNRELEASED_TEMPLATE
6666
### Fixed
6767
* (tests) No more coverage warnings are being printed if there are no sources.
6868
([#2762](https://github.com/bazel-contrib/rules_python/issues/2762))
69+
* (gazelle) Ancestor `conftest.py` files are added in addition to sibling `conftest.py`.
70+
([#3497](https://github.com/bazel-contrib/rules_python/issues/3497))
6971

7072
{#v0-0-0-added}
7173
### Added

gazelle/python/generate.go

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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()

gazelle/python/testdata/simple_test_with_conftest/bar/BUILD.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@ py_test(
2323
deps = [
2424
":bar",
2525
":conftest",
26+
"//:conftest",
2627
],
2728
)

gazelle/python/testdata/simple_test_with_conftest_sibling_imports_disabled/bar/BUILD.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ py_test(
2222
main = "__test__.py",
2323
deps = [
2424
":conftest",
25+
"//:conftest",
2526
"//:simple_test_with_conftest_sibling_imports_disabled",
2627
],
2728
)

0 commit comments

Comments
 (0)