From 84722a7904af41191165dcb5458eb641fd2c4d8b Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Sat, 16 Oct 2021 10:58:31 +0300 Subject: [PATCH] Move Config.{invocation_dir,rootdir,inifile} to legacypath plugin --- src/_pytest/config/__init__.py | 32 ---------------------------- src/_pytest/legacypath.py | 38 ++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 32 deletions(-) diff --git a/src/_pytest/config/__init__.py b/src/_pytest/config/__init__.py index 34a7a80e1..bee2fb22a 100644 --- a/src/_pytest/config/__init__.py +++ b/src/_pytest/config/__init__.py @@ -49,7 +49,6 @@ from _pytest._code import filter_traceback 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 @@ -950,17 +949,6 @@ class Config: 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 `, - 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 `. @@ -971,16 +959,6 @@ class Config: """ return self._rootpath - @property - def rootdir(self) -> LEGACY_PATH: - """The path to the :ref:`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 `. @@ -991,16 +969,6 @@ class Config: """ return self._inipath - @property - def inifile(self) -> Optional[LEGACY_PATH]: - """The path to the :ref:`configfile `. - - 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).""" diff --git a/src/_pytest/legacypath.py b/src/_pytest/legacypath.py index cac22927c..78afeb7d1 100644 --- a/src/_pytest/legacypath.py +++ b/src/_pytest/legacypath.py @@ -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 `, + 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 `. + + 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 `. + + 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) @@ -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)