Skip to content

Commit

Permalink
config: fallback confcutdir to rootpath if inipath is not set
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bluetech committed May 27, 2023
1 parent 4f3f36c commit 1eb1eb4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
3 changes: 3 additions & 0 deletions changelog/11043.improvement.rst
@@ -0,0 +1,3 @@
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`.
Previously in such cases, conftest.py files will be probed all the way to the root directory of the filesystem.
If you are badly affected by this change, consider adding an empty config file to your desired cutoff directory, or explicitly set `--confcutdir`.
7 changes: 5 additions & 2 deletions src/_pytest/config/__init__.py
Expand Up @@ -1261,8 +1261,11 @@ def _preparse(self, args: List[str], addopts: bool = True) -> None:
_pytest.deprecated.STRICT_OPTION, stacklevel=2
)

if self.known_args_namespace.confcutdir is None and self.inipath is not None:
confcutdir = str(self.inipath.parent)
if self.known_args_namespace.confcutdir is None:
if self.inipath is not None:
confcutdir = str(self.inipath.parent)
else:
confcutdir = str(self.rootpath)
self.known_args_namespace.confcutdir = confcutdir
try:
self.hook.pytest_load_initial_conftests(
Expand Down
8 changes: 7 additions & 1 deletion testing/test_conftest.py
Expand Up @@ -594,7 +594,13 @@ def test_parsefactories_relative_node_ids(
print("pytestarg : %s" % testarg)
print("expected pass : %s" % expect_ntests_passed)
os.chdir(dirs[chdir])
reprec = pytester.inline_run(testarg, "-q", "--traceconfig")
reprec = pytester.inline_run(
testarg,
"-q",
"--traceconfig",
"--confcutdir",
pytester.path,
)
reprec.assertoutcome(passed=expect_ntests_passed)


Expand Down

0 comments on commit 1eb1eb4

Please sign in to comment.