Skip to content

Commit 38255c7

Browse files
committed
feat(gazelle) generate conftest dependencies on parent folders
1 parent 61dad38 commit 38255c7

21 files changed

Lines changed: 107 additions & 139 deletions

File tree

gazelle/python/generate.go

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -667,16 +667,14 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
667667
}
668668

669669
for _, pyTestTarget := range pyTestTargets {
670-
if conftest != nil {
671-
conftestModule := Module{Name: importSpecFromSrc(pythonProjectRoot, args.Rel, conftestFilename).Imp}
672-
if pyTestTarget.annotations.includePytestConftest == nil {
673-
// unset; default behavior
674-
pyTestTarget.addModuleDependency(conftestModule)
675-
} else if *pyTestTarget.annotations.includePytestConftest {
676-
// set; add if true, do not add if false
677-
pyTestTarget.addModuleDependency(conftestModule)
670+
if pyTestTarget.annotations.includePytestConftest == nil || *pyTestTarget.annotations.includePytestConftest {
671+
// Add conftest files as module dependencies with proper Python module paths
672+
parentConftestModules := findConftestFiles(args.Rel, pythonProjectRoot, args.Config.RepoRoot)
673+
for _, module := range parentConftestModules {
674+
pyTestTarget.addModuleDependency(module)
678675
}
679676
}
677+
680678
pyTest := pyTestTarget.build()
681679

682680
result.Gen = append(result.Gen, pyTest)
@@ -819,3 +817,50 @@ func generateProtoLibraries(args language.GenerateArgs, cfg *pythonconfig.Config
819817
}
820818

821819
}
820+
821+
// findConftestFiles finds conftest.py files in parent directories up to and including the python_root.
822+
// It returns a list of Module objects with Python module paths relative to python_root.
823+
func findConftestFiles(currentRel string, pythonProjectRoot string, repoRoot string) []Module {
824+
var conftestModules []Module
825+
826+
// Determine the python_root path relative to repo root
827+
pythonRootRel := ""
828+
if pythonProjectRoot != "" {
829+
pythonRootRel = pythonProjectRoot
830+
}
831+
832+
// Start from currentRel
833+
current := currentRel
834+
835+
// Walk up the directory tree until we reach the repo root
836+
for {
837+
// Check if conftest.py exists in this directory
838+
conftestPath := filepath.Join(repoRoot, current, conftestFilename)
839+
if _, err := os.Stat(conftestPath); err == nil {
840+
// Calculate the Python module path relative to python_root
841+
var modulePath string
842+
relPath, err := filepath.Rel(pythonRootRel, current)
843+
if err != nil || relPath == "." {
844+
// If we can't get relative path or it's the python_root itself
845+
modulePath = "conftest"
846+
} else {
847+
modulePath = strings.ReplaceAll(relPath, "/", ".") + ".conftest"
848+
}
849+
850+
module := Module{
851+
Name: modulePath,
852+
}
853+
conftestModules = append(conftestModules, module)
854+
}
855+
856+
// Stop if we've reached the python_root
857+
if current == pythonRootRel || current == "." {
858+
break
859+
}
860+
861+
// Move to parent directory
862+
current = filepath.Dir(current)
863+
}
864+
865+
return conftestModules
866+
}

gazelle/python/testdata/simple_test_with_conftest/BUILD.out

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,9 @@ py_library(
1111
visibility = ["//:__subpackages__"],
1212
)
1313

14-
py_library(
15-
name = "conftest",
16-
testonly = True,
17-
srcs = ["conftest.py"],
18-
visibility = ["//:__subpackages__"],
19-
)
20-
2114
py_test(
2215
name = "simple_test_with_conftest_test",
2316
srcs = ["__test__.py"],
2417
main = "__test__.py",
25-
deps = [
26-
":conftest",
27-
":simple_test_with_conftest",
28-
],
18+
deps = [":simple_test_with_conftest"],
2919
)
Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +0,0 @@
1-
# Copyright 2023 The Bazel Authors. All rights reserved.
2-
#
3-
# Licensed under the Apache License, Version 2.0 (the "License");
4-
# you may not use this file except in compliance with the License.
5-
# You may obtain a copy of the License at
6-
#
7-
# http://www.apache.org/licenses/LICENSE-2.0
8-
#
9-
# Unless required by applicable law or agreed to in writing, software
10-
# distributed under the License is distributed on an "AS IS" BASIS,
11-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
# See the License for the specific language governing permissions and
13-
# limitations under the License.
14-
15-
from foo import foo
16-
17-
_ = foo

gazelle/python/testdata/simple_test_with_conftest/__test__.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,6 @@
1-
# Copyright 2023 The Bazel Authors. All rights reserved.
2-
#
3-
# Licensed under the Apache License, Version 2.0 (the "License");
4-
# you may not use this file except in compliance with the License.
5-
# You may obtain a copy of the License at
6-
#
7-
# http://www.apache.org/licenses/LICENSE-2.0
8-
#
9-
# Unless required by applicable law or agreed to in writing, software
10-
# distributed under the License is distributed on an "AS IS" BASIS,
11-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
# See the License for the specific language governing permissions and
13-
# limitations under the License.
14-
151
import unittest
162

17-
from __init__ import foo
3+
from foo import foo
184

195

206
class FooTest(unittest.TestCase):
Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +0,0 @@
1-
# Copyright 2023 The Bazel Authors. All rights reserved.
2-
#
3-
# Licensed under the Apache License, Version 2.0 (the "License");
4-
# you may not use this file except in compliance with the License.
5-
# You may obtain a copy of the License at
6-
#
7-
# http://www.apache.org/licenses/LICENSE-2.0
8-
#
9-
# Unless required by applicable law or agreed to in writing, software
10-
# distributed under the License is distributed on an "AS IS" BASIS,
11-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
# See the License for the specific language governing permissions and
13-
# limitations under the License.
14-
15-
from bar.bar import bar
16-
17-
_ = bar

gazelle/python/testdata/simple_test_with_conftest/bar/__test__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import unittest
1616

17-
from bar.__init__ import bar
17+
from bar import bar
1818

1919

2020
class BarTest(unittest.TestCase):
Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,2 @@
1-
# Copyright 2023 The Bazel Authors. All rights reserved.
2-
#
3-
# Licensed under the Apache License, Version 2.0 (the "License");
4-
# you may not use this file except in compliance with the License.
5-
# You may obtain a copy of the License at
6-
#
7-
# http://www.apache.org/licenses/LICENSE-2.0
8-
#
9-
# Unless required by applicable law or agreed to in writing, software
10-
# distributed under the License is distributed on an "AS IS" BASIS,
11-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
# See the License for the specific language governing permissions and
13-
# limitations under the License.
14-
15-
161
def bar():
172
return "bar"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
load("@rules_python//python:defs.bzl", "py_library")
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
load("@rules_python//python:defs.bzl", "py_library", "py_test")
2+
3+
py_library(
4+
name = "baz",
5+
srcs = ["__init__.py"],
6+
visibility = ["//:__subpackages__"],
7+
)
8+
9+
py_library(
10+
name = "conftest",
11+
testonly = True,
12+
srcs = ["conftest.py"],
13+
visibility = ["//:__subpackages__"],
14+
)
15+
16+
py_test(
17+
name = "baz_test",
18+
srcs = ["__test__.py"],
19+
main = "__test__.py",
20+
deps = [
21+
":conftest",
22+
"//bar:conftest",
23+
],
24+
)

gazelle/python/testdata/simple_test_with_conftest/bar/baz/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)