config: add Config.{rootpath,inipath}, turn Config.{rootdir,inifile} to properties
This commit is contained in:
parent
3085c99e47
commit
a346028006
|
@ -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.
|
|
@ -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)
|
The internal :class:`Config <_pytest.config.Config>` object (accessible via hooks or through the :fixture:`pytestconfig` fixture)
|
||||||
will subsequently carry these attributes:
|
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``
|
- :attr:`config.inipath <_pytest.config.Config.inipath>`: the determined ``configfile``, may be ``None``
|
||||||
for historical reasons).
|
(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
|
The ``rootdir`` is used as a reference directory for constructing test
|
||||||
addresses ("nodeids") and can be used also by plugins for storing
|
addresses ("nodeids") and can be used also by plugins for storing
|
||||||
|
|
|
@ -916,12 +916,53 @@ class Config:
|
||||||
def invocation_dir(self) -> py.path.local:
|
def invocation_dir(self) -> py.path.local:
|
||||||
"""The directory from which pytest was invoked.
|
"""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
|
:type: py.path.local
|
||||||
"""
|
"""
|
||||||
return py.path.local(str(self.invocation_params.dir))
|
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:
|
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)."""
|
||||||
|
@ -1032,8 +1073,8 @@ class Config:
|
||||||
rootdir_cmd_arg=ns.rootdir or None,
|
rootdir_cmd_arg=ns.rootdir or None,
|
||||||
config=self,
|
config=self,
|
||||||
)
|
)
|
||||||
self.rootdir = py.path.local(str(rootpath))
|
self._rootpath = rootpath
|
||||||
self.inifile = py.path.local(str(inipath)) if inipath else None
|
self._inipath = inipath
|
||||||
self.inicfg = inicfg
|
self.inicfg = inicfg
|
||||||
self._parser.extra_info["rootdir"] = self.rootdir
|
self._parser.extra_info["rootdir"] = self.rootdir
|
||||||
self._parser.extra_info["inifile"] = self.inifile
|
self._parser.extra_info["inifile"] = self.inifile
|
||||||
|
|
Loading…
Reference in New Issue