Skip to content

Commit 1eb1eb4

Browse files
committed
config: fallback confcutdir to rootpath if inipath is not set
Currently, if `--confcutdir` is not set, `inipath.parent` is used, and if `initpath` is not set, then `confcutdir` is None, which means there is no cutoff. Having no cutoff is not great, it means we potentially start probing stuff all the way up to the filesystem root directory. So let's add another fallback, to `rootpath`, which is always something reasonable.
1 parent 4f3f36c commit 1eb1eb4

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

changelog/11043.improvement.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
In cases when `--confcutdir` is not specified, and there is no config file present, the conftest cutoff directory (confcutdir) is now set to the :ref:`rootdir`.
2+
Previously in such cases, conftest.py files will be probed all the way to the root directory of the filesystem.
3+
If you are badly affected by this change, consider adding an empty config file to your desired cutoff directory, or explicitly set `--confcutdir`.

src/_pytest/config/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,8 +1261,11 @@ def _preparse(self, args: List[str], addopts: bool = True) -> None:
12611261
_pytest.deprecated.STRICT_OPTION, stacklevel=2
12621262
)
12631263

1264-
if self.known_args_namespace.confcutdir is None and self.inipath is not None:
1265-
confcutdir = str(self.inipath.parent)
1264+
if self.known_args_namespace.confcutdir is None:
1265+
if self.inipath is not None:
1266+
confcutdir = str(self.inipath.parent)
1267+
else:
1268+
confcutdir = str(self.rootpath)
12661269
self.known_args_namespace.confcutdir = confcutdir
12671270
try:
12681271
self.hook.pytest_load_initial_conftests(

testing/test_conftest.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,13 @@ def test_parsefactories_relative_node_ids(
594594
print("pytestarg : %s" % testarg)
595595
print("expected pass : %s" % expect_ntests_passed)
596596
os.chdir(dirs[chdir])
597-
reprec = pytester.inline_run(testarg, "-q", "--traceconfig")
597+
reprec = pytester.inline_run(
598+
testarg,
599+
"-q",
600+
"--traceconfig",
601+
"--confcutdir",
602+
pytester.path,
603+
)
598604
reprec.assertoutcome(passed=expect_ntests_passed)
599605

600606

0 commit comments

Comments
 (0)