Skip to content

Commit 34fafe4

Browse files
committed
config: avoid Path.cwd() call in _set_initial_conftests
We should aim to remove all `cwd()` calls except one, otherwise things will go bad if the working directory changes. Use the invocation dir instead.
1 parent eefc9d4 commit 34fafe4

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

src/_pytest/config/__init__.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,7 @@ def _set_initial_conftests(
541541
noconftest: bool,
542542
rootpath: Path,
543543
confcutdir: Optional[Path],
544+
invocation_dir: Path,
544545
importmode: Union[ImportMode, str],
545546
) -> None:
546547
"""Load initial conftest files given a preparsed "namespace".
@@ -550,8 +551,9 @@ def _set_initial_conftests(
550551
All builtin and 3rd party plugins will have been loaded, however, so
551552
common options will not confuse our logic here.
552553
"""
553-
current = Path.cwd()
554-
self._confcutdir = absolutepath(current / confcutdir) if confcutdir else None
554+
self._confcutdir = (
555+
absolutepath(invocation_dir / confcutdir) if confcutdir else None
556+
)
555557
self._noconftest = noconftest
556558
self._using_pyargs = pyargs
557559
foundanchor = False
@@ -561,15 +563,15 @@ def _set_initial_conftests(
561563
i = path.find("::")
562564
if i != -1:
563565
path = path[:i]
564-
anchor = absolutepath(current / path)
566+
anchor = absolutepath(invocation_dir / path)
565567

566568
# Ensure we do not break if what appears to be an anchor
567569
# is in fact a very long option (#10169, #11394).
568570
if safe_exists(anchor):
569571
self._try_load_conftest(anchor, importmode, rootpath)
570572
foundanchor = True
571573
if not foundanchor:
572-
self._try_load_conftest(current, importmode, rootpath)
574+
self._try_load_conftest(invocation_dir, importmode, rootpath)
573575

574576
def _is_in_confcutdir(self, path: Path) -> bool:
575577
"""Whether a path is within the confcutdir.
@@ -1168,6 +1170,7 @@ def pytest_load_initial_conftests(self, early_config: "Config") -> None:
11681170
noconftest=early_config.known_args_namespace.noconftest,
11691171
rootpath=early_config.rootpath,
11701172
confcutdir=early_config.known_args_namespace.confcutdir,
1173+
invocation_dir=early_config.invocation_params.dir,
11711174
importmode=early_config.known_args_namespace.importmode,
11721175
)
11731176

@@ -1261,6 +1264,8 @@ def _decide_args(
12611264
"""Decide the args (initial paths/nodeids) to use given the relevant inputs.
12621265
12631266
:param warn: Whether can issue warnings.
1267+
1268+
:returns: The args and the args source. Guaranteed to be non-empty.
12641269
"""
12651270
if args:
12661271
source = Config.ArgsSource.ARGS

testing/test_conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def conftest_setinitial(
3535
noconftest=False,
3636
rootpath=Path(args[0]),
3737
confcutdir=confcutdir,
38+
invocation_dir=Path.cwd(),
3839
importmode="prepend",
3940
)
4041

0 commit comments

Comments
 (0)