Move Config.{invocation_dir,rootdir,inifile} to legacypath plugin

This commit is contained in:
Ran Benita 2021-10-16 10:58:31 +03:00
parent d979f82fb0
commit 84722a7904
2 changed files with 38 additions and 32 deletions

View File

@ -49,7 +49,6 @@ from _pytest._code import filter_traceback
from _pytest._io import TerminalWriter from _pytest._io import TerminalWriter
from _pytest.compat import final from _pytest.compat import final
from _pytest.compat import importlib_metadata from _pytest.compat import importlib_metadata
from _pytest.compat import LEGACY_PATH
from _pytest.compat import legacy_path from _pytest.compat import legacy_path
from _pytest.outcomes import fail from _pytest.outcomes import fail
from _pytest.outcomes import Skipped from _pytest.outcomes import Skipped
@ -950,17 +949,6 @@ class Config:
self.cache: Optional[Cache] = None self.cache: Optional[Cache] = None
@property
def invocation_dir(self) -> LEGACY_PATH:
"""The directory from which pytest was invoked.
Prefer to use :attr:`invocation_params.dir <InvocationParams.dir>`,
which is a :class:`pathlib.Path`.
:type: LEGACY_PATH
"""
return legacy_path(str(self.invocation_params.dir))
@property @property
def rootpath(self) -> Path: def rootpath(self) -> Path:
"""The path to the :ref:`rootdir <rootdir>`. """The path to the :ref:`rootdir <rootdir>`.
@ -971,16 +959,6 @@ class Config:
""" """
return self._rootpath return self._rootpath
@property
def rootdir(self) -> LEGACY_PATH:
"""The path to the :ref:`rootdir <rootdir>`.
Prefer to use :attr:`rootpath`, which is a :class:`pathlib.Path`.
:type: LEGACY_PATH
"""
return legacy_path(str(self.rootpath))
@property @property
def inipath(self) -> Optional[Path]: def inipath(self) -> Optional[Path]:
"""The path to the :ref:`configfile <configfiles>`. """The path to the :ref:`configfile <configfiles>`.
@ -991,16 +969,6 @@ class Config:
""" """
return self._inipath return self._inipath
@property
def inifile(self) -> Optional[LEGACY_PATH]:
"""The path to the :ref:`configfile <configfiles>`.
Prefer to use :attr:`inipath`, which is a :class:`pathlib.Path`.
:type: Optional[LEGACY_PATH]
"""
return legacy_path(str(self.inipath)) if self.inipath else None
def add_cleanup(self, func: Callable[[], None]) -> None: def add_cleanup(self, func: Callable[[], None]) -> None:
"""Add a function to be called when the config object gets out of """Add a function to be called when the config object gets out of
use (usually coninciding with pytest_unconfigure).""" use (usually coninciding with pytest_unconfigure)."""

View File

@ -331,6 +331,37 @@ def TerminalReporter_startdir(self: TerminalReporter) -> LEGACY_PATH:
return legacy_path(self.startpath) return legacy_path(self.startpath)
def Config_invocation_dir(self: pytest.Config) -> LEGACY_PATH:
"""The directory from which pytest was invoked.
Prefer to use :attr:`invocation_params.dir <InvocationParams.dir>`,
which is a :class:`pathlib.Path`.
:type: LEGACY_PATH
"""
return legacy_path(str(self.invocation_params.dir))
def Config_rootdir(self: pytest.Config) -> LEGACY_PATH:
"""The path to the :ref:`rootdir <rootdir>`.
Prefer to use :attr:`rootpath`, which is a :class:`pathlib.Path`.
:type: LEGACY_PATH
"""
return legacy_path(str(self.rootpath))
def Config_inifile(self: pytest.Config) -> Optional[LEGACY_PATH]:
"""The path to the :ref:`configfile <configfiles>`.
Prefer to use :attr:`inipath`, which is a :class:`pathlib.Path`.
:type: Optional[LEGACY_PATH]
"""
return legacy_path(str(self.inipath)) if self.inipath else None
def pytest_configure(config: pytest.Config) -> None: def pytest_configure(config: pytest.Config) -> None:
mp = pytest.MonkeyPatch() mp = pytest.MonkeyPatch()
config.add_cleanup(mp.undo) config.add_cleanup(mp.undo)
@ -361,3 +392,10 @@ def pytest_configure(config: pytest.Config) -> None:
mp.setattr( mp.setattr(
TerminalReporter, "startdir", property(TerminalReporter_startdir), raising=False TerminalReporter, "startdir", property(TerminalReporter_startdir), raising=False
) )
# Add Config.{invocation_dir,rootdir,inifile} properties.
mp.setattr(
pytest.Config, "invocation_dir", property(Config_invocation_dir), raising=False
)
mp.setattr(pytest.Config, "rootdir", property(Config_rootdir), raising=False)
mp.setattr(pytest.Config, "inifile", property(Config_inifile), raising=False)