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.
This commit is contained in:
Ran Benita 2023-05-27 18:42:37 +03:00
parent 4f3f36c396
commit 4a1bba25b9
4 changed files with 32 additions and 3 deletions

View File

@ -0,0 +1,3 @@
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 would 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`.

View File

@ -1261,8 +1261,11 @@ class Config:
_pytest.deprecated.STRICT_OPTION, stacklevel=2 _pytest.deprecated.STRICT_OPTION, stacklevel=2
) )
if self.known_args_namespace.confcutdir is None and self.inipath is not None: if self.known_args_namespace.confcutdir is None:
if self.inipath is not None:
confcutdir = str(self.inipath.parent) confcutdir = str(self.inipath.parent)
else:
confcutdir = str(self.rootpath)
self.known_args_namespace.confcutdir = confcutdir self.known_args_namespace.confcutdir = confcutdir
try: try:
self.hook.pytest_load_initial_conftests( self.hook.pytest_load_initial_conftests(

View File

@ -179,6 +179,23 @@ class TestParseIni:
assert result.ret != 0 assert result.ret != 0
result.stderr.fnmatch_lines("ERROR: *pyproject.toml: Invalid statement*") result.stderr.fnmatch_lines("ERROR: *pyproject.toml: Invalid statement*")
def test_confcutdir_default_without_configfile(self, pytester: Pytester) -> None:
# If --confcutdir is not specified, and there is no configfile, default
# to the roothpath.
sub = pytester.mkdir("sub")
os.chdir(sub)
config = pytester.parseconfigure()
assert config.pluginmanager._confcutdir == sub
def test_confcutdir_default_with_configfile(self, pytester: Pytester) -> None:
# If --confcutdir is not specified, and there is a configfile, default
# to the configfile's directory.
pytester.makeini("[pytest]")
sub = pytester.mkdir("sub")
os.chdir(sub)
config = pytester.parseconfigure()
assert config.pluginmanager._confcutdir == pytester.path
@pytest.mark.xfail(reason="probably not needed") @pytest.mark.xfail(reason="probably not needed")
def test_confcutdir(self, pytester: Pytester) -> None: def test_confcutdir(self, pytester: Pytester) -> None:
sub = pytester.mkdir("sub") sub = pytester.mkdir("sub")

View File

@ -594,7 +594,13 @@ class TestConftestVisibility:
print("pytestarg : %s" % testarg) print("pytestarg : %s" % testarg)
print("expected pass : %s" % expect_ntests_passed) print("expected pass : %s" % expect_ntests_passed)
os.chdir(dirs[chdir]) 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) reprec.assertoutcome(passed=expect_ntests_passed)