Consider paths and pathlists relative to cwd in case of an absent ini-file (#11963)

Previously this would trigger an `AssertionError`.

While it could be considered a bug-fix, but given it now can be relied upon, it is probably better to consider it an improvement.

Fix #11311
This commit is contained in:
Bruno Oliveira 2024-02-14 15:53:28 -03:00 committed by GitHub
parent fe7907d78c
commit 46e6fb12c7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 28 additions and 3 deletions

View File

@ -0,0 +1,4 @@
When using ``--override-ini`` for paths in invocations without a configuration file defined, the current working directory is used
as the relative directory.
Previoulsy this would raise an :class:`AssertionError`.

View File

@ -1563,9 +1563,11 @@ class Config:
# in this case, we already have a list ready to use.
#
if type == "paths":
# TODO: This assert is probably not valid in all cases.
assert self.inipath is not None
dp = self.inipath.parent
dp = (
self.inipath.parent
if self.inipath is not None
else self.invocation_params.dir
)
input_values = shlex.split(value) if isinstance(value, str) else value
return [dp / x for x in input_values]
elif type == "args":

View File

@ -198,9 +198,16 @@ class Parser:
* ``paths``: a list of :class:`pathlib.Path`, separated as in a shell
* ``pathlist``: a list of ``py.path``, separated as in a shell
For ``paths`` and ``pathlist`` types, they are considered relative to the ini-file.
In case the execution is happening without an ini-file defined,
they will be considered relative to the current working directory (for example with ``--override-ini``).
.. versionadded:: 7.0
The ``paths`` variable type.
.. versionadded:: 8.1
Use the current working directory to resolve ``paths`` and ``pathlist`` in the absence of an ini-file.
Defaults to ``string`` if ``None`` or not passed.
:param default:
Default value if no ini-file option exists but is queried.

View File

@ -1874,6 +1874,18 @@ class TestOverrideIniArgs:
assert "ERROR:" not in result.stderr.str()
result.stdout.fnmatch_lines(["collected 1 item", "*= 1 passed in *="])
def test_override_ini_without_config_file(self, pytester: Pytester) -> None:
pytester.makepyfile(**{"src/override_ini_without_config_file.py": ""})
pytester.makepyfile(
**{
"tests/test_override_ini_without_config_file.py": (
"import override_ini_without_config_file\ndef test(): pass"
),
}
)
result = pytester.runpytest("--override-ini", "pythonpath=src")
assert result.parseoutcomes() == {"passed": 1}
def test_help_via_addopts(pytester: Pytester) -> None:
pytester.makeini(