config: add Config.{rootpath,inipath}, turn Config.{rootdir,inifile} to properties

This commit is contained in:
Ran Benita 2020-08-06 14:37:19 +03:00
parent 3085c99e47
commit a346028006
3 changed files with 55 additions and 6 deletions

View File

@ -0,0 +1,3 @@
Added two new attributes :attr:`rootpath <_pytest.config.Config.rootpath>` and :attr:`inipath <_pytest.config.Config.inipath>` to :class:`Config <_pytest.config.Config>`.
These attributes are :class:`pathlib.Path` versions of the existing :attr:`rootdir <_pytest.config.Config.rootdir>` and :attr:`inifile <_pytest.config.Config.inifile>` attributes,
and should be preferred over them when possible.

View File

@ -180,10 +180,15 @@ are never merged - the first match wins.
The internal :class:`Config <_pytest.config.Config>` object (accessible via hooks or through the :fixture:`pytestconfig` fixture)
will subsequently carry these attributes:
- ``config.rootdir``: the determined root directory, guaranteed to exist.
- :attr:`config.rootpath <_pytest.config.Config.rootpath>`: the determined root directory, guaranteed to exist.
- ``config.inifile``: the determined ``configfile``, may be ``None`` (it is named ``inifile``
for historical reasons).
- :attr:`config.inipath <_pytest.config.Config.inipath>`: the determined ``configfile``, may be ``None``
(it is named ``inipath`` for historical reasons).
.. versionadded:: 6.1
The ``config.rootpath`` and ``config.inipath`` properties. They are :class:`pathlib.Path`
versions of the older ``config.rootdir`` and ``config.inifile``, which have type
``py.path.local``, and still exist for backward compatibility.
The ``rootdir`` is used as a reference directory for constructing test
addresses ("nodeids") and can be used also by plugins for storing

View File

@ -916,12 +916,53 @@ class Config:
def invocation_dir(self) -> py.path.local:
"""The directory from which pytest was invoked.
Prefer to use :attr:`invocation_params.dir <InvocationParams.dir>`.
Prefer to use :attr:`invocation_params.dir <InvocationParams.dir>`,
which is a :class:`pathlib.Path`.
:type: py.path.local
"""
return py.path.local(str(self.invocation_params.dir))
@property
def rootpath(self) -> Path:
"""The path to the :ref:`rootdir <rootdir>`.
:type: pathlib.Path
.. versionadded:: 6.1
"""
return self._rootpath
@property
def rootdir(self) -> py.path.local:
"""The path to the :ref:`rootdir <rootdir>`.
Prefer to use :attr:`rootpath`, which is a :class:`pathlib.Path`.
:type: py.path.local
"""
return py.path.local(str(self.rootpath))
@property
def inipath(self) -> Optional[Path]:
"""The path to the :ref:`configfile <configfiles>`.
:type: Optional[pathlib.Path]
.. versionadded:: 6.1
"""
return self._inipath
@property
def inifile(self) -> Optional[py.path.local]:
"""The path to the :ref:`configfile <configfiles>`.
Prefer to use :attr:`inipath`, which is a :class:`pathlib.Path`.
:type: Optional[py.path.local]
"""
return py.path.local(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)."""
@ -1032,8 +1073,8 @@ class Config:
rootdir_cmd_arg=ns.rootdir or None,
config=self,
)
self.rootdir = py.path.local(str(rootpath))
self.inifile = py.path.local(str(inipath)) if inipath else None
self._rootpath = rootpath
self._inipath = inipath
self.inicfg = inicfg
self._parser.extra_info["rootdir"] = self.rootdir
self._parser.extra_info["inifile"] = self.inifile