Skip to content

Commit 9d2a18c

Browse files
thejcannonrickeylev
authored andcommitted
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 (cherry picked from commit b47b92b)
1 parent 13a37c2 commit 9d2a18c

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ BEGIN_UNRELEASED_TEMPLATE
4747
END_UNRELEASED_TEMPLATE
4848
-->
4949

50-
5150
{#v1-8-0}
5251
## [1.8.0] - 2025-12-19
5352

@@ -125,6 +124,8 @@ END_UNRELEASED_TEMPLATE
125124
* (gazelle) Fix `gazelle_python_manifest.test` so that it accesses manifest files via `runfile` path handling rather than directly ([#3397](https://github.com/bazel-contrib/rules_python/issues/3397)).
126125
* (core rules) For the system_python bootstrap, the runfiles root is added to
127126
sys.path.
127+
* (gazelle) Ancestor `conftest.py` files are added in addition to sibling `conftest.py`.
128+
([#3497](https://github.com/bazel-contrib/rules_python/issues/3497))
128129

129130
{#v1-8-0-added}
130131
### Added
@@ -2082,4 +2083,4 @@ Breaking changes:
20822083
* (pip) Create all_data_requirements alias
20832084
* Expose Python C headers through the toolchain.
20842085

2085-
[0.24.0]: https://github.com/bazel-contrib/rules_python/releases/tag/0.24.0
2086+
[0.24.0]: https://github.com/bazel-contrib/rules_python/releases/tag/0.24.0

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)