Skip to content

Commit ccdadb6

Browse files
committed
main: add Session.startpath, make Session.startdir a property that can be deprecated
Same as in Config.
1 parent 6a174af commit ccdadb6

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/_pytest/main.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import _pytest._code
2626
from _pytest import nodes
2727
from _pytest.compat import final
28+
from _pytest.compat import LEGACY_PATH
2829
from _pytest.compat import legacy_path
2930
from _pytest.config import Config
3031
from _pytest.config import directory_arg
@@ -301,7 +302,7 @@ def wrap_session(
301302
finally:
302303
# Explicitly break reference cycle.
303304
excinfo = None # type: ignore
304-
session.startdir.chdir()
305+
os.chdir(session.startpath)
305306
if initstate >= 2:
306307
try:
307308
config.hook.pytest_sessionfinish(
@@ -476,7 +477,6 @@ def __init__(self, config: Config) -> None:
476477
self.shouldstop: Union[bool, str] = False
477478
self.shouldfail: Union[bool, str] = False
478479
self.trace = config.trace.root.get("collection")
479-
self.startdir = config.invocation_dir
480480
self._initialpaths: FrozenSet[Path] = frozenset()
481481

482482
self._bestrelpathcache: Dict[Path, str] = _bestrelpath_cache(config.rootpath)
@@ -497,6 +497,24 @@ def __repr__(self) -> str:
497497
self.testscollected,
498498
)
499499

500+
@property
501+
def startpath(self) -> Path:
502+
"""The path from which pytest was invoked.
503+
504+
.. versionadded:: 6.3.0
505+
"""
506+
return self.config.invocation_params.dir
507+
508+
@property
509+
def stardir(self) -> LEGACY_PATH:
510+
"""The path from which pytest was invoked.
511+
512+
Prefer to use ``startpath`` which is a :class:`pathlib.Path`.
513+
514+
:type: LEGACY_PATH
515+
"""
516+
return legacy_path(self.startpath)
517+
500518
def _node_location_to_relpath(self, node_path: Path) -> str:
501519
# bestrelpath is a quite slow function.
502520
return self._bestrelpathcache[node_path]

0 commit comments

Comments
 (0)