Skip to content

Commit 4a1bba2

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 4a1bba2

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-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+
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 would 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_config.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,23 @@ def test_toml_parse_error(self, pytester: Pytester) -> None:
179179
assert result.ret != 0
180180
result.stderr.fnmatch_lines("ERROR: *pyproject.toml: Invalid statement*")
181181

182+
def test_confcutdir_default_without_configfile(self, pytester: Pytester) -> None:
183+
# If --confcutdir is not specified, and there is no configfile, default
184+
# to the roothpath.
185+
sub = pytester.mkdir("sub")
186+
os.chdir(sub)
187+
config = pytester.parseconfigure()
188+
assert config.pluginmanager._confcutdir == sub
189+
190+
def test_confcutdir_default_with_configfile(self, pytester: Pytester) -> None:
191+
# If --confcutdir is not specified, and there is a configfile, default
192+
# to the configfile's directory.
193+
pytester.makeini("[pytest]")
194+
sub = pytester.mkdir("sub")
195+
os.chdir(sub)
196+
config = pytester.parseconfigure()
197+
assert config.pluginmanager._confcutdir == pytester.path
198+
182199
@pytest.mark.xfail(reason="probably not needed")
183200
def test_confcutdir(self, pytester: Pytester) -> None:
184201
sub = pytester.mkdir("sub")

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)