Skip to content

Commit

Permalink
Move Config.{invocation_dir,rootdir,inifile} to legacypath plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
bluetech committed Oct 28, 2021
1 parent d979f82 commit 84722a7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 32 deletions.
32 changes: 0 additions & 32 deletions src/_pytest/config/__init__.py
Expand Up @@ -49,7 +49,6 @@
from _pytest._io import TerminalWriter
from _pytest.compat import final
from _pytest.compat import importlib_metadata
from _pytest.compat import LEGACY_PATH
from _pytest.compat import legacy_path
from _pytest.outcomes import fail
from _pytest.outcomes import Skipped
Expand Down Expand Up @@ -950,17 +949,6 @@ def __init__(

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
def rootpath(self) -> Path:
"""The path to the :ref:`rootdir <rootdir>`.
Expand All @@ -971,16 +959,6 @@ def rootpath(self) -> Path:
"""
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
def inipath(self) -> Optional[Path]:
"""The path to the :ref:`configfile <configfiles>`.
Expand All @@ -991,16 +969,6 @@ def inipath(self) -> Optional[Path]:
"""
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:
"""Add a function to be called when the config object gets out of
use (usually coninciding with pytest_unconfigure)."""
Expand Down
38 changes: 38 additions & 0 deletions src/_pytest/legacypath.py
Expand Up @@ -331,6 +331,37 @@ def TerminalReporter_startdir(self: TerminalReporter) -> LEGACY_PATH:
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:
mp = pytest.MonkeyPatch()
config.add_cleanup(mp.undo)
Expand Down Expand Up @@ -361,3 +392,10 @@ def pytest_configure(config: pytest.Config) -> None:
mp.setattr(
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)

0 comments on commit 84722a7

Please sign in to comment.